Commit 31d2b4fd90f9305aad2e79872067e6c0243267de

Authored by Ruchika Gupta
Committed by Simon Glass
1 parent c4beb22fcd

DM: crypto/rsa_mod_exp: Add rsa Modular Exponentiation DM driver

Add a new rsa uclass for performing modular exponentiation and implement
the software driver basing on this uclass.

Signed-off-by: Ruchika Gupta <ruchika.gupta@freescale.com>
CC: Simon Glass <sjg@chromium.org>
Acked-by: Simon Glass <sjg@chromium.org>

Showing 7 changed files with 117 additions and 1 deletions Side-by-side Diff

drivers/crypto/Makefile
... ... @@ -6,5 +6,6 @@
6 6 #
7 7  
8 8 obj-$(CONFIG_EXYNOS_ACE_SHA) += ace_sha.o
  9 +obj-y += rsa_mod_exp/
9 10 obj-y += fsl/
drivers/crypto/rsa_mod_exp/Kconfig
  1 +config DM_MOD_EXP
  2 + bool "Enable Driver Model for RSA Modular Exponentiation"
  3 + depends on DM
  4 + help
  5 + If you want to use driver model for RSA Modular Exponentiation, say Y.
drivers/crypto/rsa_mod_exp/Makefile
  1 +#
  2 +# (C) Copyright 2014 Freescale Semiconductor, Inc.
  3 +#
  4 +# SPDX-License-Identifier: GPL-2.0+
  5 +#
  6 +
  7 +obj-$(CONFIG_RSA) += mod_exp_uclass.o mod_exp_sw.o
drivers/crypto/rsa_mod_exp/mod_exp_sw.c
  1 +/*
  2 + * (C) Copyright 2014 Freescale Semiconductor, Inc.
  3 + * Author: Ruchika Gupta <ruchika.gupta@freescale.com>
  4 + *
  5 + * SPDX-License-Identifier: GPL-2.0+
  6 + */
  7 +
  8 +#include <config.h>
  9 +#include <common.h>
  10 +#include <dm.h>
  11 +#include <u-boot/rsa-mod-exp.h>
  12 +
  13 +int mod_exp_sw(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
  14 + struct key_prop *prop, uint8_t *out)
  15 +{
  16 + int ret = 0;
  17 +
  18 + ret = rsa_mod_exp_sw(sig, sig_len, prop, out);
  19 + if (ret) {
  20 + debug("%s: RSA failed to verify: %d\n", __func__, ret);
  21 + return ret;
  22 + }
  23 +
  24 + return 0;
  25 +}
  26 +
  27 +static const struct mod_exp_ops mod_exp_ops_sw = {
  28 + .mod_exp = mod_exp_sw,
  29 +};
  30 +
  31 +U_BOOT_DRIVER(mod_exp_sw) = {
  32 + .name = "mod_exp_sw",
  33 + .id = UCLASS_MOD_EXP,
  34 + .ops = &mod_exp_ops_sw,
  35 +};
  36 +
  37 +U_BOOT_DEVICE(mod_exp_sw) = {
  38 + .name = "mod_exp_sw",
  39 +};
drivers/crypto/rsa_mod_exp/mod_exp_uclass.c
  1 +/*
  2 + * (C) Copyright 2014 Freescale Semiconductor, Inc
  3 + * Author: Ruchika Gupta <ruchika.gupta@freescale.com>
  4 + *
  5 + * SPDX-License-Identifier: GPL-2.0+
  6 + */
  7 +
  8 +#include <common.h>
  9 +#include <dm.h>
  10 +#include <u-boot/rsa-mod-exp.h>
  11 +#include <errno.h>
  12 +#include <fdtdec.h>
  13 +#include <malloc.h>
  14 +#include <asm/io.h>
  15 +#include <linux/list.h>
  16 +
  17 +int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
  18 + struct key_prop *node, uint8_t *out)
  19 +{
  20 + const struct mod_exp_ops *ops = device_get_ops(dev);
  21 +
  22 + if (!ops->mod_exp)
  23 + return -ENOSYS;
  24 +
  25 + return ops->mod_exp(dev, sig, sig_len, node, out);
  26 +}
  27 +
  28 +UCLASS_DRIVER(mod_exp) = {
  29 + .id = UCLASS_MOD_EXP,
  30 + .name = "rsa_mod_exp",
  31 +};
include/dm/uclass-id.h
... ... @@ -33,6 +33,7 @@
33 33 UCLASS_I2C, /* I2C bus */
34 34 UCLASS_I2C_GENERIC, /* Generic I2C device */
35 35 UCLASS_I2C_EEPROM, /* I2C EEPROM device */
  36 + UCLASS_MOD_EXP, /* RSA Mod Exp device */
36 37  
37 38 UCLASS_COUNT,
38 39 UCLASS_INVALID = -1,
include/u-boot/rsa-mod-exp.h
... ... @@ -35,10 +35,42 @@
35 35 * @sig: RSA PKCS1.5 signature
36 36 * @sig_len: Length of signature in number of bytes
37 37 * @node: Node with RSA key elements like modulus, exponent, R^2, n0inv
38   - * @out: Result in form of byte array
  38 + * @out: Result in form of byte array of len equal to sig_len
39 39 */
40 40 int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
41 41 struct key_prop *node, uint8_t *out);
  42 +
  43 +int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
  44 + struct key_prop *node, uint8_t *out);
  45 +
  46 +/**
  47 + * struct struct mod_exp_ops - Driver model for RSA Modular Exponentiation
  48 + * operations
  49 + *
  50 + * The uclass interface is implemented by all crypto devices which use
  51 + * driver model.
  52 + */
  53 +struct mod_exp_ops {
  54 + /**
  55 + * Perform Modular Exponentiation
  56 + *
  57 + * Operation: out[] = sig ^ exponent % modulus
  58 + *
  59 + * @dev: RSA Device
  60 + * @sig: RSA PKCS1.5 signature
  61 + * @sig_len: Length of signature in number of bytes
  62 + * @node: Node with RSA key elements like modulus, exponent,
  63 + * R^2, n0inv
  64 + * @out: Result in form of byte array of len equal to sig_len
  65 + *
  66 + * This function computes exponentiation over the signature.
  67 + * Returns: 0 if exponentiation is successful, or a negative value
  68 + * if it wasn't.
  69 + */
  70 + int (*mod_exp)(struct udevice *dev, const uint8_t *sig,
  71 + uint32_t sig_len, struct key_prop *node,
  72 + uint8_t *outp);
  73 +};
42 74  
43 75 #endif