Skip to contentSkip to conversation

Implement generalised RSA public exponents for verified boot

PATCH Superseded Michael van der Westhuizen 3 participants 11 messages May 26, 2014 – Aug 10, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement generalised RSA public exponents for verified boot

Remove the verified boot limitation that only allows a single RSA public exponent of 65537 (F4). This allows use with existing PKI infrastructure, and has been tested with HSM-based PKI.

Change the configuration OF tree format to store the RSA public exponent as a 64 bit integer and implement backward compatibility for verified boot configuration trees without this extra field.

Parameterise vboot_test.sh to test different public exponents and do a drive-by fix to get vboot_test.sh working again.

Mathematics and other hard work by Andrew Bott.

Tested with the following public exponents: 3, 5, 17, 257, 39981, 50457, 65537 and 4294967297.

---
 doc/uImage.FIT/signature.txt |  4 +-
 include/rsa.h                |  1 +
 lib/rsa/rsa-sign.c           | 56 +++++++++++++++++++++++++++-
 lib/rsa/rsa-verify.c         | 88 +++++++++++++++++++++++++++++++++++++++++---
 test/vboot/vboot_test.sh     | 11 +++++-
 5 files changed, 151 insertions(+), 9 deletions(-)
doc/uImage.FIT/signature.txt +3 -1
index 9502037..cc314a3 100644
@@ -66,7 +66,8 @@ Creating an RSA key and certificate
 -----------------------------------
 To create a new public key, size 2048 bits:
 
-$ openssl genrsa -F4 -out keys/dev.key 2048
+$ openssl genpkey -algorithm RSA -out keys/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537
 
 To create a certificate for this:
 
@@ -159,6 +160,7 @@ For RSA the following are mandatory:
 
 - rsa,num-bits: Number of key bits (e.g. 2048)
 - rsa,modulus: Modulus (N) as a big-endian multi-word integer
+- rsa,exponent: Public exponent (E) as a 64 bit unsigned integer
 - rsa,r-squared: (2^num-bits)^2 as a big-endian multi-word integer
 - rsa,n0-inverse: -1 / modulus[0] mod 2^32
 
include/rsa.h +1 -0
index a5680ab..b84d82a 100644
@@ -27,6 +27,7 @@ struct rsa_public_key {
  uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */
  uint32_t *modulus; /* modulus as little endian array */
  uint32_t *rr; /* R^2 as little endian array */
+ uint64_t exponent; /* public exponent */
 };
 
 #if IMAGE_ENABLE_SIGN
lib/rsa/rsa-sign.c +48 -1

57 lines not on this page · raw message

BIGNUM **modulusp,
return -ENOMEM;
}

+ if (0 != rsa_get_e(key, e))
+ ret = -1;
+
if (!BN_copy(n, key->n) || !BN_set_word(big1, 1L) ||

     !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))

ret = -1;
@@ -386,6 +436,7 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
int rsa_add_verify_data(struct image_sign_info *info, void *keydest) {
BIGNUM *modulus, *r_squared;
+ uint64_t e;
uint32_t n0_inv;
int parent, node;
char name[100];
@@ -397,7 +448,7 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
ret = rsa_get_pub_key(info->keydir, info->keyname, &rsa); if (ret)
return ret;
- ret = rsa_get_params(rsa, &n0_inv, &modulus, &r_squared);
+ ret = rsa_get_params(rsa, &e, &n0_inv, &modulus, &r_squared); if (ret)
return ret;
bits = BN_num_bits(modulus);
@@ -431,6 +482,7 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
info->keyname);

  ret |= fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
  ret |= fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
+ ret |= fdt_setprop_u64(keydest, node, "rsa,exponent", e);
  ret |= fdt_add_bignum(keydest, node, "rsa,modulus", modulus, bits);
  ret |= fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared, bits);
  ret |= fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
lib/rsa/rsa-verify.c +3 -0
index 587da5b..878f92a 100644
@@ -26,6 +26,9 @@
 #define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a)
 #define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a))
 
+/* The default public exponent is 65537 */
+#define RSA_DEFAULT_PUBEXP 0x10001
+
 /**
  * subtract_modulus() - subtract modulus from the given value
  *
@@ -123,6 +126,42 @@ static void montgomery_mul(const struct

rsa_public_key *key,
}

/**
+ * num_pub_exponent_bits() - Number of bits in the public exponent
+ *
+ * @key: RSA key
+ * @k: Storage for the number of public exponent bits
+ */
+static int num_public_exponent_bits(const struct rsa_public_key *key, int *k) +{
+ uint64_t e;
+ const uint nbits = (sizeof(e) * 8);
+
+ *k = 0;
+ e = key->exponent;
+
+ if (!e)
+ return 0;
+
+ for (*k = 1; *k < nbits + 1; ++*k)
+ if (!(e >>= 1))
+ return 0;
+
+ return -EINVAL;
+}
+
+/**
+ * is_public_exponent_bit_set() - Check if a bit in the public exponent is set
+ *
+ * @key: RSA key
+ * @pos: The bit position to check
+ */
+static int is_public_exponent_bit_set(const struct rsa_public_key *key,
+ int pos)
+{
+ return key->exponent & (1 << pos);
+}
+
+/**
* pow_mod() - in-place public exponentiation
*
* @key: RSA key
@@ -132,6 +171,7 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
{
uint32_t *result, *ptr;
uint i;
+ int j, k;

/* Sanity check for stack size - key->len is in 32-bit words */ if (key->len > RSA_MAX_KEY_BITS / 32) {
@@ -141,18 +181,49 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
}

uint32_t val[key->len], acc[key->len], tmp[key->len];
+ uint32_t a_scaled[key->len];
result = tmp; /* Re-use location. */

/* Convert from big endian byte array to little endian word array. */ for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--) val[i] = get_unaligned_be32(ptr);

- montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
- for (i = 0; i < 16; i += 2) {
- montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
- montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
+ if (0 != num_public_exponent_bits(key, &k))
+ return -EINVAL;
+
+ if (k < 2) {
+ debug("Public exponent is too short (%d bits, minimum 2)\n",
+ k);
+ return -EINVAL;
+ }
+
+ if (!is_public_exponent_bit_set(key, k - 1) ||
+ !is_public_exponent_bit_set(key, 0)) {
+ debug("Invalid RSA public exponent 0x%llx.\n", key->exponent);
+ return -EINVAL;
+ }
+
+ /* the bit at e[k-1] is 1 by definition, so start with: C := M */
+ montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
+ /* retain scaled version for intermediate use */
+ memcpy(a_scaled, acc, key->len * 4);
+
+ for (j = k - 2; j > 0; --j) {
+ montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */ +
+ if (is_public_exponent_bit_set(key, j)) {
+ /* acc = tmp * val / R mod n */
+ montgomery_mul(key, acc, tmp, a_scaled);
+ } else {
+ /* e[j] == 0, copy tmp back to acc for next operation */
+ memcpy(acc, tmp, key->len * 4);
+ }
}
- montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */ +
+ /* the bit at e[0] is always 1 */
+ montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+ montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
+ memcpy(result, acc, key->len * 4);

/* Make sure result < mod; result is@most 1x mod too large. */ if (greater_equal_modulus(key, result))
@@ -229,6 +300,8 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
const void *blob = info->fdt_blob;
struct rsa_public_key key;
const void *modulus, *rr;
+ const uint64_t *pe;
+ int length;
int ret;

if (node < 0) {
@@ -241,6 +314,11 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
}
key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0); key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
+ pe = fdt_getprop(blob, node, "rsa,exponent", &length);
+ if (!pe || length < sizeof(*pe))
+ key.exponent = RSA_DEFAULT_PUBEXP;
+ else
+ key.exponent = fdt64_to_cpu(*pe);
modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); if (!key.len || !modulus || !rr) {

test/vboot/vboot_test.sh +10 -1
index 3c6efa7..885e869 100755
@@ -14,6 +14,7 @@ set -e
 run_uboot() {
  echo -n "Test Verified Boot Run: $1: "
  ${uboot} -d sandbox-u-boot.dtb >${tmp} -c '
+sb bind 0 test.fit;
 sb load host 0 100 test.fit;
 fdt addr 100;
 bootm 100;
@@ -54,8 +55,16 @@ echo ${mkimage} -D "${dtc}"
 echo "Build keys"
 mkdir -p ${keys}
 
+PUBLIC_EXPONENT=${1}
+
+if [ x"${PUBLIC_EXPONENT}" = x"" ]; then
+ PUBLIC_EXPONENT=65537
+fi
+
 # Create an RSA key pair
-openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null
+openssl genpkey -algorithm RSA -out ${keys}/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 \
+    -pkeyopt rsa_keygen_pubexp:${PUBLIC_EXPONENT} 2>/dev/null
 
 # Create a certificate containing the public key
 openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt
Signature · 1 line
2.0.0.rc0
#1 lore raw

PATCH v2 1/1 Implement generalised RSA public exponents for verified boot

From: Michael van der Westhuizen <[email protected]>

Remove the verified boot limitation that only allows a single RSA public exponent of 65537 (F4). This allows use with existing PKI infrastructure, and has been tested with HSM-based PKI.

Change the configuration OF tree format to store the RSA public exponent as a 64 bit integer and implement backward compatibility for verified boot configuration trees without this extra field.

Parameterise vboot_test.sh to test different public exponents and do a drive-by fix to get vboot_test.sh working again.

Mathematics and other hard work by Andrew Bott.

Tested with the following public exponents: 3, 5, 17, 257, 39981, 50457, 65537 and 4294967297.

---
Changes for v2:
- None. Resend to address line wrapping issues.

 doc/uImage.FIT/signature.txt |  4 +-
 include/rsa.h                |  1 +
 lib/rsa/rsa-sign.c           | 56 +++++++++++++++++++++++++++-
 lib/rsa/rsa-verify.c         | 88 +++++++++++++++++++++++++++++++++++++++++---
 test/vboot/vboot_test.sh     | 11 +++++-
 5 files changed, 151 insertions(+), 9 deletions(-)
doc/uImage.FIT/signature.txt +3 -1
index 9502037..cc314a3 100644
@@ -66,7 +66,8 @@ Creating an RSA key and certificate
 -----------------------------------
 To create a new public key, size 2048 bits:
 
-$ openssl genrsa -F4 -out keys/dev.key 2048
+$ openssl genpkey -algorithm RSA -out keys/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537
 
 To create a certificate for this:
 
@@ -159,6 +160,7 @@ For RSA the following are mandatory:
 
 - rsa,num-bits: Number of key bits (e.g. 2048)
 - rsa,modulus: Modulus (N) as a big-endian multi-word integer
+- rsa,exponent: Public exponent (E) as a 64 bit unsigned integer
 - rsa,r-squared: (2^num-bits)^2 as a big-endian multi-word integer
 - rsa,n0-inverse: -1 / modulus[0] mod 2^32
 
include/rsa.h +1 -0
index a5680ab..b84d82a 100644
@@ -27,6 +27,7 @@ struct rsa_public_key {
 	uint32_t n0inv;		/* -1 / modulus[0] mod 2^32 */
 	uint32_t *modulus;	/* modulus as little endian array */
 	uint32_t *rr;		/* R^2 as little endian array */
+	uint64_t exponent;	/* public exponent */
 };
 
 #if IMAGE_ENABLE_SIGN
lib/rsa/rsa-sign.c +54 -2

88 lines not on this page · raw message

lib/rsa/rsa-verify.c +83 -5

131 lines not on this page · raw message

test/vboot/vboot_test.sh +10 -1
index 3c6efa7..885e869 100755
@@ -14,6 +14,7 @@ set -e
 run_uboot() {
 	echo -n "Test Verified Boot Run: $1: "
 	${uboot} -d sandbox-u-boot.dtb >${tmp} -c '
+sb bind 0 test.fit;
 sb load host 0 100 test.fit;
 fdt addr 100;
 bootm 100;
@@ -54,8 +55,16 @@ echo ${mkimage} -D "${dtc}"
 echo "Build keys"
 mkdir -p ${keys}
 
+PUBLIC_EXPONENT=${1}
+
+if [ x"${PUBLIC_EXPONENT}" = x"" ]; then
+	PUBLIC_EXPONENT=65537
+fi
+
 # Create an RSA key pair
-openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null
+openssl genpkey -algorithm RSA -out ${keys}/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 \
+    -pkeyopt rsa_keygen_pubexp:${PUBLIC_EXPONENT} 2>/dev/null
 
 # Create a certificate containing the public key
 openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt
Signature · 1 line
2.0.0.rc0
#2 lore raw

PATCH v2 1/1 Implement generalised RSA public exponents for verified boot

Hi Michael,

Quoted 6 lines · On 26 May 2014 08:56, <[email protected]> wrote:

From: Michael van der Westhuizen <[email protected]>

Remove the verified boot limitation that only allows a single RSA public exponent of 65537 (F4). This allows use with existing PKI infrastructure, and has been tested with HSM-based PKI.

Quoted 8 lines

Change the configuration OF tree format to store the RSA public exponent as a 64 bit integer and implement backward compatibility for verified boot configuration trees without this extra field.

Parameterise vboot_test.sh to test different public exponents and do a drive-by fix to get vboot_test.sh working again.

Mathematics and other hard work by Andrew Bott.

This looks good. I mostly have some nits below.

Quoted 58 lines

Tested with the following public exponents: 3, 5, 17, 257, 39981, 50457, 65537 and 4294967297.

---
Changes for v2:
- None. Resend to address line wrapping issues.

 doc/uImage.FIT/signature.txt |  4 +-
 include/rsa.h                |  1 +
 lib/rsa/rsa-sign.c           | 56 +++++++++++++++++++++++++++-
 lib/rsa/rsa-verify.c         | 88 +++++++++++++++++++++++++++++++++++++++++---
 test/vboot/vboot_test.sh     | 11 +++++-
 5 files changed, 151 insertions(+), 9 deletions(-)
doc/uImage.FIT/signature.txt +3 -1
index 9502037..cc314a3 100644
@@ -66,7 +66,8 @@ Creating an RSA key and certificate
 -----------------------------------
 To create a new public key, size 2048 bits:
 
-$ openssl genrsa -F4 -out keys/dev.key 2048
+$ openssl genpkey -algorithm RSA -out keys/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537
 
 To create a certificate for this:
 
@@ -159,6 +160,7 @@ For RSA the following are mandatory:
 
 - rsa,num-bits: Number of key bits (e.g. 2048)
 - rsa,modulus: Modulus (N) as a big-endian multi-word integer
+- rsa,exponent: Public exponent (E) as a 64 bit unsigned integer
 - rsa,r-squared: (2^num-bits)^2 as a big-endian multi-word integer
 - rsa,n0-inverse: -1 / modulus[0] mod 2^32
 
include/rsa.h +1 -0
index a5680ab..b84d82a 100644
@@ -27,6 +27,7 @@ struct rsa_public_key {
        uint32_t n0inv;         /* -1 / modulus[0] mod 2^32 */
        uint32_t *modulus;      /* modulus as little endian array */
        uint32_t *rr;           /* R^2 as little endian array */
+       uint64_t exponent;      /* public exponent */
 };
 
 #if IMAGE_ENABLE_SIGN
lib/rsa/rsa-sign.c +0 -0
index ca8c120..f20ae28 100644

lib/rsa/rsa-sign.c err_priv: · line 266

Quoted 6 lines
lib/rsa/rsa-sign.c +3 -0
@@ -261,9 +261,56 @@ err_priv:
 }
 
 /*
+ * rsa_get_e(): - Get the public exponent from an RSA public key
+ */
+static int rsa_get_e(RSA *key, uint64_t *e)

rsa_get_exponent() might be better. Also s/e/exponent/ or perhaps exp (although I see I made the same mistake in the following function).

lib/rsa/rsa-sign.c err_priv: · line 272

Quoted 6 lines
lib/rsa/rsa-sign.c +6 -0
+{
+       int ret;
+       BIGNUM *bn_te;
+       uint64_t te;
+
+       ret = -1;

-EINVAL

Quoted 33 lines
lib/rsa/rsa-sign.c +33 -0
+       bn_te = NULL;
+
+       if (!e)
+               goto cleanup;
+
+       if (BN_num_bits(key->e) > 64)
+               goto cleanup;
+
+       *e = BN_get_word(key->e);
+
+       if (BN_num_bits(key->e) < 33) {
+               ret = 0;
+               goto cleanup;
+       }
+
+       bn_te = BN_dup(key->e);
+       if (!bn_te)
+               goto cleanup;
+
+       if (!BN_rshift(bn_te, bn_te, 32))
+               goto cleanup;
+
+       if (!BN_mask_bits(bn_te, 32))
+               goto cleanup;
+
+       te = BN_get_word(bn_te);
+       te <<= 32;
+       *e |= te;
+       ret = 0;
+
+cleanup:
+       if (bn_te)
+               BN_free(bn_te);

lib/rsa/rsa-sign.c err_priv: · line 314

Quoted 10 lines
lib/rsa/rsa-sign.c +6 -1
+
+       return ret;
+}
+
+/*
  * rsa_get_params(): - Get the important parameters of an RSA public key
  */
-int rsa_get_params(RSA *key, uint32_t *n0_invp, BIGNUM **modulusp,
+int rsa_get_params(RSA *key, uint64_t *e, uint32_t *n0_invp, BIGNUM **modulusp,
                   BIGNUM **r_squaredp)

If you don't mind, you could change 'e' to 'exp' or 'exponent' in the same patch.

Quoted 13 lines
lib/rsa/rsa-sign.c +3 -0
 {
        BIGNUM *big1, *big2, *big32, *big2_32;
@@ -286,6 +333,9 @@ int rsa_get_params(RSA *key, uint32_t *n0_invp, BIGNUM **modulusp,
                return -ENOMEM;
        }
 
+       if (0 != rsa_get_e(key, e))
+               ret = -1;
+
        if (!BN_copy(n, key->n) || !BN_set_word(big1, 1L) ||
            !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
                ret = -1;

lib/rsa/rsa-sign.c static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, · line 439

Quoted 4 lines
lib/rsa/rsa-sign.c +1 -0
@@ -386,6 +436,7 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
 int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
 {
        BIGNUM *modulus, *r_squared;
+       uint64_t e;

And here.

Quoted 25 lines
lib/rsa/rsa-sign.c +2 -1
        uint32_t n0_inv;
        int parent, node;
        char name[100];
@@ -397,7 +448,7 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
        ret = rsa_get_pub_key(info->keydir, info->keyname, &rsa);
        if (ret)
                return ret;
-       ret = rsa_get_params(rsa, &n0_inv, &modulus, &r_squared);
+       ret = rsa_get_params(rsa, &e, &n0_inv, &modulus, &r_squared);
        if (ret)
                return ret;
        bits = BN_num_bits(modulus);
@@ -431,6 +482,7 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
                                 info->keyname);
        ret |= fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
        ret |= fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
+       ret |= fdt_setprop_u64(keydest, node, "rsa,exponent", e);
        ret |= fdt_add_bignum(keydest, node, "rsa,modulus", modulus, bits);
        ret |= fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared, bits);
        ret |= fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
lib/rsa/rsa-verify.c +0 -0
index 587da5b..878f92a 100644

lib/rsa/rsa-verify.c line 30

Quoted 5 lines
lib/rsa/rsa-verify.c +2 -0
@@ -26,6 +26,9 @@
 #define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a)
 #define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a))
 
+/* The default public exponent is 65537 */
+#define RSA_DEFAULT_PUBEXP     0x10001

Then why use hex? Can we decide on either decimal or hex?

Quoted 5 lines
lib/rsa/rsa-verify.c +1 -0
+
 /**
  * subtract_modulus() - subtract modulus from the given value
  *

lib/rsa/rsa-verify.c static void montgomery_mul(const struct rsa_public_key *key, · line 134

Quoted 9 lines
lib/rsa/rsa-verify.c +6 -0
@@ -123,6 +126,42 @@ static void montgomery_mul(const struct rsa_public_key *key,
 }
 
 /**
+ * num_pub_exponent_bits() - Number of bits in the public exponent
+ *
+ * @key:       RSA key
+ * @k:         Storage for the number of public exponent bits
+ */
+static int num_public_exponent_bits(const struct rsa_public_key *key, int *k)

s/k/keyp/ or something like that.

Also is this function able to just use ffs() or similar?

lib/rsa/rsa-verify.c static void montgomery_mul(const struct rsa_public_key *key, · line 136

Quoted 2 lines
lib/rsa/rsa-verify.c +2 -0
+{
+       uint64_t e;

s/e/exponent/ or exp

lib/rsa/rsa-verify.c static void montgomery_mul(const struct rsa_public_key *key, · line 139

Quoted 3 lines
lib/rsa/rsa-verify.c +3 -0
+       const uint nbits = (sizeof(e) * 8);
+
+       *k = 0;

How about a local variable for this instead of using *k everywhere?

Quoted 12 lines
lib/rsa/rsa-verify.c +12 -0
+       e = key->exponent;
+
+       if (!e)
+               return 0;
+
+       for (*k = 1; *k < nbits + 1; ++*k)
+               if (!(e >>= 1))
+                       return 0;
+
+       return -EINVAL;
+}
+

lib/rsa/rsa-verify.c static void montgomery_mul(const struct rsa_public_key *key, · line 161

Quoted 10 lines
lib/rsa/rsa-verify.c +10 -0
+/**
+ * is_public_exponent_bit_set() - Check if a bit in the public exponent is set
+ *
+ * @key:       RSA key
+ * @pos:       The bit position to check
+ */
+static int is_public_exponent_bit_set(const struct rsa_public_key *key,
+               int pos)
+{
+       return key->exponent & (1 << pos);

If this is 64-but, then perhaps 1ULL << pos

Quoted 31 lines
lib/rsa/rsa-verify.c +7 -4
+}
+
+/**
  * pow_mod() - in-place public exponentiation
  *
  * @key:       RSA key
@@ -132,6 +171,7 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
 {
        uint32_t *result, *ptr;
        uint i;
+       int j, k;
 
        /* Sanity check for stack size - key->len is in 32-bit words */
        if (key->len > RSA_MAX_KEY_BITS / 32) {
@@ -141,18 +181,49 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
        }
 
        uint32_t val[key->len], acc[key->len], tmp[key->len];
+       uint32_t a_scaled[key->len];
        result = tmp;  /* Re-use location. */
 
        /* Convert from big endian byte array to little endian word array. */
        for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
                val[i] = get_unaligned_be32(ptr);
 
-       montgomery_mul(key, acc, val, key->rr);  /* axx = a * RR / R mod M */
-       for (i = 0; i < 16; i += 2) {
-               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
-               montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
+       if (0 != num_public_exponent_bits(key, &k))
+               return -EINVAL;

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 202

Quoted 10 lines
lib/rsa/rsa-verify.c +10 -0
+
+       if (k < 2) {
+               debug("Public exponent is too short (%d bits, minimum 2)\n",
+                     k);
+               return -EINVAL;
+       }
+
+       if (!is_public_exponent_bit_set(key, k - 1) ||
+           !is_public_exponent_bit_set(key, 0)) {
+               debug("Invalid RSA public exponent 0x%llx.\n", key->exponent);

What is invalid about it? Perhaps expand the message?

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 208

Quoted 6 lines
lib/rsa/rsa-verify.c +6 -0
+               return -EINVAL;
+       }
+
+       /* the bit at e[k-1] is 1 by definition, so start with: C := M */
+       montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
+       /* retain scaled version for intermediate use */

Join these two comments, also the style should be:

/*
* The bit at ...
* ...
*/

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 209

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+       memcpy(a_scaled, acc, key->len * 4);

What is 4? is it sizeof(uint32_t?). I wonder if we could replace the line immediately above and remove this memcpy()?

For example, if you did:

       montgomery_mul(key, result, val, key->rr); /* acc = a * RR / R mod n */
Quoted 8 lines
lib/rsa/rsa-verify.c +8 -0
+
+       for (j = k - 2; j > 0; --j) {
+               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+
+               if (is_public_exponent_bit_set(key, j)) {
+                       /* acc = tmp * val / R mod n */
+                       montgomery_mul(key, acc, tmp, a_scaled);
+               } else {

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 226

Quoted 10 lines
lib/rsa/rsa-verify.c +8 -1
+                       /* e[j] == 0, copy tmp back to acc for next operation */
+                       memcpy(acc, tmp, key->len * 4);
+               }
        }
-       montgomery_mul(key, result, acc, val);  /* result = XX * a / R mod M */
+
+       /* the bit at e[0] is always 1 */
+       montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+       montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
+       memcpy(result, acc, key->len * 4);

Do you need this special case, or if bit 0 of e is always 1, perhaps you can just extend the loop by one more step?

lib/rsa/rsa-verify.c static int rsa_verify_with_keynode(struct image_sign_info *info, · line 303

Quoted 8 lines
lib/rsa/rsa-verify.c +1 -0
        /* Make sure result < mod; result is@most 1x mod too large. */
        if (greater_equal_modulus(key, result))
@@ -229,6 +300,8 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
        const void *blob = info->fdt_blob;
        struct rsa_public_key key;
        const void *modulus, *rr;
+       const uint64_t *pe;

s/pe/public_exponent/ or similar?

Quoted 29 lines
lib/rsa/rsa-verify.c +6 -0
+       int length;
        int ret;
 
        if (node < 0) {
@@ -241,6 +314,11 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
        }
        key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
        key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
+       pe = fdt_getprop(blob, node, "rsa,exponent", &length);
+       if (!pe || length < sizeof(*pe))
+               key.exponent = RSA_DEFAULT_PUBEXP;
+       else
+               key.exponent = fdt64_to_cpu(*pe);
        modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
        rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
        if (!key.len || !modulus || !rr) {
test/vboot/vboot_test.sh +1 -0
index 3c6efa7..885e869 100755
@@ -14,6 +14,7 @@ set -e
 run_uboot() {
        echo -n "Test Verified Boot Run: $1: "
        ${uboot} -d sandbox-u-boot.dtb >${tmp} -c '
+sb bind 0 test.fit;
 sb load host 0 100 test.fit;
 fdt addr 100;
 bootm 100;

test/vboot/vboot_test.sh echo ${mkimage} -D "${dtc}" · line 62

Quoted 8 lines
test/vboot/vboot_test.sh +5 -0
@@ -54,8 +55,16 @@ echo ${mkimage} -D "${dtc}"
 echo "Build keys"
 mkdir -p ${keys}
 
+PUBLIC_EXPONENT=${1}
+
+if [ x"${PUBLIC_EXPONENT}" = x"" ]; then
+       PUBLIC_EXPONENT=65537
+fi

Do we need this, or can we just do:

   if [ -n "${PUBLIC_EXPONENT}" ]; then
Quoted 10 lines
test/vboot/vboot_test.sh +4 -2
+
 # Create an RSA key pair
-openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null
+openssl genpkey -algorithm RSA -out ${keys}/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 \
+    -pkeyopt rsa_keygen_pubexp:${PUBLIC_EXPONENT} 2>/dev/null
 
 # Create a certificate containing the public key
 openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt
--
Quoted 2 lines

2.0.0.rc0

Regards,
Simon

#3 lore raw

PATCH v2 1/1 Implement generalised RSA public exponents for verified boot

Hi Simon,

Thanks for the feedback.

I'll take care of the nits and look into removing some special casing.

Quoted 2 lines · Simon Glass

Hi Michael,

<snip>

Quoted 9 lines
Quoted 8 lines

/**
+ * num_pub_exponent_bits() - Number of bits in the public exponent
+ *
+ * @key: RSA key
+ * @k: Storage for the number of public exponent bits
+ */
+static int num_public_exponent_bits(const struct rsa_public_key *key, int *k)

Quoted 3 lines

s/k/keyp/ or something like that.

Also is this function able to just use ffs() or similar?

flsll() yes, but that's not portable to Linux.

Quoted 1 line

<snip>

Quoted 58 lines
Quoted 31 lines
lib/rsa/rsa-verify.c +7 -4
+}
+
+/**
 * pow_mod() - in-place public exponentiation
 *
 * @key:       RSA key
@@ -132,6 +171,7 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
 {
       uint32_t *result, *ptr;
       uint i;
+       int j, k;
 
       /* Sanity check for stack size - key->len is in 32-bit words */
       if (key->len > RSA_MAX_KEY_BITS / 32) {
@@ -141,18 +181,49 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
       }
 
       uint32_t val[key->len], acc[key->len], tmp[key->len];
+       uint32_t a_scaled[key->len];
       result = tmp;  /* Re-use location. */
 
       /* Convert from big endian byte array to little endian word array. */
       for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
               val[i] = get_unaligned_be32(ptr);
 
-       montgomery_mul(key, acc, val, key->rr);  /* axx = a * RR / R mod M */
-       for (i = 0; i < 16; i += 2) {
-               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
-               montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
+       if (0 != num_public_exponent_bits(key, &k))
+               return -EINVAL;

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 202

Quoted 10 lines
lib/rsa/rsa-verify.c +10 -0
+
+       if (k < 2) {
+               debug("Public exponent is too short (%d bits, minimum 2)\n",
+                     k);
+               return -EINVAL;
+       }
+
+       if (!is_public_exponent_bit_set(key, k - 1) ||
+           !is_public_exponent_bit_set(key, 0)) {
+               debug("Invalid RSA public exponent 0x%llx.\n", key->exponent);

What is invalid about it? Perhaps expand the message?

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 208

Quoted 6 lines
lib/rsa/rsa-verify.c +6 -0
+               return -EINVAL;
+       }
+
+       /* the bit at e[k-1] is 1 by definition, so start with: C := M */
+       montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
+       /* retain scaled version for intermediate use */

Join these two comments, also the style should be:

/*
* The bit at ...
* ...
*/

Quoted 8 lines

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 209

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+       memcpy(a_scaled, acc, key->len * 4);

What is 4? is it sizeof(uint32_t?). I wonder if we could replace the line immediately above and remove this memcpy()?

For example, if you did:

      montgomery_mul(key, result, val, key->rr); /* acc = a * RR / R mod n */

Yes, it's sizeof(uint32_t) - that would probably be a good thing to spell out too.

result points to tmp, which is going to be repeatedly overwritten in the loop, but we need a_scaled to stay constant (as the result of the first montgomery_mul) throughout... or did I misunderstand your point?

Quoted 11 lines
Quoted 11 lines
lib/rsa/rsa-verify.c +10 -0
+
+       for (j = k - 2; j > 0; --j) {
+               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+
+               if (is_public_exponent_bit_set(key, j)) {
+                       /* acc = tmp * val / R mod n */
+                       montgomery_mul(key, acc, tmp, a_scaled);
+               } else {
+                       /* e[j] == 0, copy tmp back to acc for next operation */
+                       memcpy(acc, tmp, key->len * 4);
Quoted 8 lines

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 226

Quoted 8 lines
lib/rsa/rsa-verify.c +6 -1
+               }
       }
-       montgomery_mul(key, result, acc, val);  /* result = XX * a / R mod M */
+
+       /* the bit at e[0] is always 1 */
+       montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+       montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
+       memcpy(result, acc, key->len * 4);

<snip>

Michael

#4 lore raw

PATCH v2 1/1 Implement generalised RSA public exponents for verified boot

Hi Michael,

Quoted 20 lines · Michael van der Westhuizen

Hi Simon,

Thanks for the feedback.

I'll take care of the nits and look into removing some special casing.

Quoted 2 lines · Simon Glass

Hi Michael,

<snip>

Quoted 9 lines
Quoted 8 lines

/**
+ * num_pub_exponent_bits() - Number of bits in the public exponent
+ *
+ * @key: RSA key
+ * @k: Storage for the number of public exponent bits
+ */
+static int num_public_exponent_bits(const struct rsa_public_key *key, int *k)

Quoted 5 lines
Quoted 3 lines

s/k/keyp/ or something like that.

Also is this function able to just use ffs() or similar?

flsll() yes, but that's not portable to Linux.

Does that matter?

Quoted 67 lines
Quoted 2 lines

<snip>

Quoted 63 lines
Quoted 31 lines
lib/rsa/rsa-verify.c +7 -4
+}
+
+/**
 * pow_mod() - in-place public exponentiation
 *
 * @key:       RSA key
@@ -132,6 +171,7 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
 {
       uint32_t *result, *ptr;
       uint i;
+       int j, k;
 
       /* Sanity check for stack size - key->len is in 32-bit words */
       if (key->len > RSA_MAX_KEY_BITS / 32) {
@@ -141,18 +181,49 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
       }
 
       uint32_t val[key->len], acc[key->len], tmp[key->len];
+       uint32_t a_scaled[key->len];
       result = tmp;  /* Re-use location. */
 
       /* Convert from big endian byte array to little endian word array. */
       for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
               val[i] = get_unaligned_be32(ptr);
 
-       montgomery_mul(key, acc, val, key->rr);  /* axx = a * RR / R mod M */
-       for (i = 0; i < 16; i += 2) {
-               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
-               montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
+       if (0 != num_public_exponent_bits(key, &k))
+               return -EINVAL;

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 202

Quoted 10 lines
lib/rsa/rsa-verify.c +10 -0
+
+       if (k < 2) {
+               debug("Public exponent is too short (%d bits, minimum 2)\n",
+                     k);
+               return -EINVAL;
+       }
+
+       if (!is_public_exponent_bit_set(key, k - 1) ||
+           !is_public_exponent_bit_set(key, 0)) {
+               debug("Invalid RSA public exponent 0x%llx.\n", key->exponent);

What is invalid about it? Perhaps expand the message?

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 208

Quoted 6 lines
lib/rsa/rsa-verify.c +6 -0
+               return -EINVAL;
+       }
+
+       /* the bit at e[k-1] is 1 by definition, so start with: C := M */
+       montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
+       /* retain scaled version for intermediate use */

Join these two comments, also the style should be:

/*
* The bit at ...
* ...
*/

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 209

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+       memcpy(a_scaled, acc, key->len * 4);

What is 4? is it sizeof(uint32_t?). I wonder if we could replace the line immediately above and remove this memcpy()?

Quoted 7 lines
Quoted 3 lines

For example, if you did:

      montgomery_mul(key, result, val, key->rr); /* acc = a * RR / R mod n */

Yes, it's sizeof(uint32_t) - that would probably be a good thing to spell out too.

result points to tmp, which is going to be repeatedly overwritten in the loop, but we need a_scaled to stay constant (as the result of the first montgomery_mul) throughout... or did I misunderstand your point?

OK I see thanks. Perhaps add a new temporary instead of using memcpy()?

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
lib/rsa/rsa-verify.c +11 -1
+
+       for (j = k - 2; j > 0; --j) {
+               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+
+               if (is_public_exponent_bit_set(key, j)) {
+                       /* acc = tmp * val / R mod n */
+                       montgomery_mul(key, acc, tmp, a_scaled);
+               } else {
+                       /* e[j] == 0, copy tmp back to acc for next operation */
+                       memcpy(acc, tmp, key->len * 4);
+               }
       }
-       montgomery_mul(key, result, acc, val);  /* result = XX * a / R mod M */
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
lib/rsa/rsa-verify.c +5 -0
+
+       /* the bit at e[0] is always 1 */
+       montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+       montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
+       memcpy(result, acc, key->len * 4);

<snip>

Regards,
Simon

#5 lore raw

PATCH v2 1/1 Implement generalised RSA public exponents for verified boot

Hi Simon,

Quoted 24 lines · Simon Glass

Hi Michael,

Quoted 20 lines · Michael van der Westhuizen

Hi Simon,

Thanks for the feedback.

I'll take care of the nits and look into removing some special casing.

Quoted 2 lines · Simon Glass

Hi Michael,

<snip>

Quoted 9 lines
Quoted 8 lines

/**
+ * num_pub_exponent_bits() - Number of bits in the public exponent
+ *
+ * @key: RSA key
+ * @k: Storage for the number of public exponent bits
+ */
+static int num_public_exponent_bits(const struct rsa_public_key *key, int *k)

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

s/k/keyp/ or something like that.

Also is this function able to just use ffs() or similar?

flsll() yes, but that's not portable to Linux.

Does that matter?

This code compiles on the host, so unfortunately yes. That's the same reason I had to work around the lack of handy *_u64 fdt helpers when reading the public exponent.

Quoted 70 lines
Quoted 70 lines
Quoted 3 lines

<snip>

Quoted 65 lines
Quoted 31 lines
lib/rsa/rsa-verify.c +3 -0
+}
+
+/**

* pow_mod() - in-place public exponentiation
*
* @key: RSA key

lib/rsa/rsa-verify.c +4 -4
@@ -132,6 +171,7 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
 {
      uint32_t *result, *ptr;
      uint i;
+       int j, k;
 
      /* Sanity check for stack size - key->len is in 32-bit words */
      if (key->len > RSA_MAX_KEY_BITS / 32) {
@@ -141,18 +181,49 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
      }
 
      uint32_t val[key->len], acc[key->len], tmp[key->len];
+       uint32_t a_scaled[key->len];
      result = tmp;  /* Re-use location. */
 
      /* Convert from big endian byte array to little endian word array. */
      for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
              val[i] = get_unaligned_be32(ptr);
 
-       montgomery_mul(key, acc, val, key->rr);  /* axx = a * RR / R mod M */
-       for (i = 0; i < 16; i += 2) {
-               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
-               montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
+       if (0 != num_public_exponent_bits(key, &k))
+               return -EINVAL;

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 202

Quoted 10 lines
lib/rsa/rsa-verify.c +10 -0
+
+       if (k < 2) {
+               debug("Public exponent is too short (%d bits, minimum 2)\n",
+                     k);
+               return -EINVAL;
+       }
+
+       if (!is_public_exponent_bit_set(key, k - 1) ||
+           !is_public_exponent_bit_set(key, 0)) {
+               debug("Invalid RSA public exponent 0x%llx.\n", key->exponent);

What is invalid about it? Perhaps expand the message?

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 208

Quoted 6 lines
lib/rsa/rsa-verify.c +6 -0
+               return -EINVAL;
+       }
+
+       /* the bit at e[k-1] is 1 by definition, so start with: C := M */
+       montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
+       /* retain scaled version for intermediate use */

Join these two comments, also the style should be:

/*
* The bit at ...
* ...
*/

lib/rsa/rsa-verify.c static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) · line 209

Quoted 1 line
lib/rsa/rsa-verify.c +1 -0
+       memcpy(a_scaled, acc, key->len * 4);

What is 4? is it sizeof(uint32_t?). I wonder if we could replace the line immediately above and remove this memcpy()?

For example, if you did:

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
     montgomery_mul(key, result, val, key->rr); /* acc = a * RR / R mod n */

Yes, it's sizeof(uint32_t) - that would probably be a good thing to spell out too.

result points to tmp, which is going to be repeatedly overwritten in the loop, but we need a_scaled to stay constant (as the result of the first montgomery_mul) throughout... or did I misunderstand your point?

OK I see thanks. Perhaps add a new temporary instead of using memcpy()?

And set it up using montgomery_mul? Would that not be more expensive than a memcpy?

Thanks,
Michael

#6 lore raw

PATCH v2 1/1 Implement generalised RSA public exponents for verified boot

Hi Michael,

Quoted 40 lines · Michael van der Westhuizen

Hi Simon,

On 30 May 2014, at 10:50 PM, Simon Glass <[email protected]> wrote:

Hi Michael,

On 30 May 2014 14:47, Michael van der Westhuizen
<[email protected]> wrote:

Hi Simon,

Thanks for the feedback.

I'll take care of the nits and look into removing some special casing.

On 30 May 2014, at 9:04 PM, Simon Glass <[email protected]> wrote:

Hi Michael,

<snip>

/**
+ * num_pub_exponent_bits() - Number of bits in the public exponent
+ *
+ * @key: RSA key
+ * @k: Storage for the number of public exponent bits
+ */
+static int num_public_exponent_bits(const struct rsa_public_key *key, int *k)

s/k/keyp/ or something like that.

Also is this function able to just use ffs() or similar?

flsll() yes, but that's not portable to Linux.

Quoted 6 lines

Does that matter?

This code compiles on the host, so unfortunately yes. That's the same reason I had to work around the lack of handy *_u64 fdt helpers when reading the public exponent.

OK, although Linux might have replacements. But if not, perhaps add a comment as to why you need the helper function.

[snip]

Quoted 13 lines

For example, if you did:

     montgomery_mul(key, result, val, key->rr); /* acc = a * RR / R mod n */

Yes, it's sizeof(uint32_t) - that would probably be a good thing to spell out too.

result points to tmp, which is going to be repeatedly overwritten in the loop, but we need a_scaled to stay constant (as the result of the first montgomery_mul) throughout... or did I misunderstand your point?

Quoted 5 lines

OK I see thanks. Perhaps add a new temporary instead of using memcpy()?

And set it up using montgomery_mul? Would that not be more expensive than a memcpy?

On the host? Not sure we care about that. I was really just wondering if an assignment is better than a memcpy() which makes assumptions about the format.

Regards,
Simon

#7 lore raw

PATCH v2 1/1 Implement generalised RSA public exponents for verified boot

Hi Simon,

On 30 May 2014, at 11:11 PM, Simon Glass <[email protected]> wrote:

<snip>

Quoted 6 lines
Quoted 3 lines

This code compiles on the host, so unfortunately yes. That's the same reason I had to work around the lack of handy *_u64 fdt helpers when reading the public exponent.

OK, although Linux might have replacements. But if not, perhaps add a comment as to why you need the helper function.

Will do.

Quoted 19 lines

[snip]

Quoted 16 lines

For example, if you did:

    montgomery_mul(key, result, val, key->rr); /* acc = a * RR / R mod n */

Yes, it's sizeof(uint32_t) - that would probably be a good thing to spell out too.

result points to tmp, which is going to be repeatedly overwritten in the loop, but we need a_scaled to stay constant (as the result of the first montgomery_mul) throughout... or did I misunderstand your point?

OK I see thanks. Perhaps add a new temporary instead of using memcpy()?

Quoted 6 lines
Quoted 2 lines

And set it up using montgomery_mul? Would that not be more expensive than a memcpy?

On the host? Not sure we care about that. I was really just wondering if an assignment is better than a memcpy() which makes assumptions about the format.

In theory it should optimise to the same code, but I'll run tests with assignment to make sure it works as expected.

Michael

#8 lore raw

PATCH v3 1/1 Implement generalised RSA public exponents for verified boot

In reply to michael at smart-africa.com

From: Michael van der Westhuizen <[email protected]>

Remove the verified boot limitation that only allows a single RSA public exponent of 65537 (F4). This change allows use with existing PKI infrastructure and has been tested with HSM-based PKI.

Change the configuration OF tree format to store the RSA public exponent as a 64 bit integer and implement backward compatibility for verified boot configuration trees without this extra field.

Parameterise vboot_test.sh to test different public exponents.

Mathematics and other hard work by Andrew Bott.

Tested with the following public exponents: 3, 5, 17, 257, 39981, 50457, 65537 and 4294967297.

---
Changes for v2:
- None. Resend to address line wrapping issues.
Changes for v3:
- Address review comments, cleaning up variable names and rewording error messages.
- Rebase against current master.
- Apply as many suggested simplifications as feasible.

 doc/uImage.FIT/signature.txt |  4 +-
 include/u-boot/rsa.h         |  1 +
 lib/rsa/rsa-sign.c           | 60 ++++++++++++++++++++++++++--
 lib/rsa/rsa-verify.c         | 93 +++++++++++++++++++++++++++++++++++++++++---
 test/vboot/vboot_test.sh     | 10 ++++-
 5 files changed, 158 insertions(+), 10 deletions(-)
doc/uImage.FIT/signature.txt +3 -1
index a6ab543..b2f89fc 100644
@@ -66,7 +66,8 @@ Creating an RSA key and certificate
 -----------------------------------
 To create a new public key, size 2048 bits:
 
-$ openssl genrsa -F4 -out keys/dev.key 2048
+$ openssl genpkey -algorithm RSA -out keys/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537
 
 To create a certificate for this:
 
@@ -159,6 +160,7 @@ For RSA the following are mandatory:
 
 - rsa,num-bits: Number of key bits (e.g. 2048)
 - rsa,modulus: Modulus (N) as a big-endian multi-word integer
+- rsa,exponent: Public exponent (E) as a 64 bit unsigned integer
 - rsa,r-squared: (2^num-bits)^2 as a big-endian multi-word integer
 - rsa,n0-inverse: -1 / modulus[0] mod 2^32
 
include/u-boot/rsa.h +1 -0
index 325751a..fd08a61 100644
@@ -27,6 +27,7 @@ struct rsa_public_key {
 	uint32_t n0inv;		/* -1 / modulus[0] mod 2^32 */
 	uint32_t *modulus;	/* modulus as little endian array */
 	uint32_t *rr;		/* R^2 as little endian array */
+	uint64_t exponent;	/* public exponent */
 };
 
 #if IMAGE_ENABLE_SIGN
lib/rsa/rsa-sign.c +57 -3

92 lines not on this page · raw message

lib/rsa/rsa-verify.c +88 -5

136 lines not on this page · raw message

test/vboot/vboot_test.sh +9 -1
index 8074fc6..6d7abb8 100755
@@ -54,8 +54,16 @@ echo ${mkimage} -D "${dtc}"
 echo "Build keys"
 mkdir -p ${keys}
 
+PUBLIC_EXPONENT=${1}
+
+if [ -z "${PUBLIC_EXPONENT}" ]; then
+	PUBLIC_EXPONENT=65537
+fi
+
 # Create an RSA key pair
-openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null
+openssl genpkey -algorithm RSA -out ${keys}/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 \
+    -pkeyopt rsa_keygen_pubexp:${PUBLIC_EXPONENT} 2>/dev/null
 
 # Create a certificate containing the public key
 openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt
Signature · 1 line
1.9.1
#9 lore raw

PATCH v3 1/1 Implement generalised RSA public exponents for verified boot

Hi Michael,

Quoted 8 lines · On 2 July 2014 02:17, <[email protected]> wrote:

From: Michael van der Westhuizen <[email protected]>

Remove the verified boot limitation that only allows a single RSA public exponent of 65537 (F4). This change allows use with existing PKI infrastructure and has been tested with HSM-based PKI.

Quoted 7 lines

Change the configuration OF tree format to store the RSA public exponent as a 64 bit integer and implement backward compatibility for verified boot configuration trees without this extra field.

Parameterise vboot_test.sh to test different public exponents.

Mathematics and other hard work by Andrew Bott.

Sorry, I still have a few nits on this, but it looks good.

Quoted 41 lines

Tested with the following public exponents: 3, 5, 17, 257, 39981, 50457, 65537 and 4294967297.

---
Changes for v2:
- None. Resend to address line wrapping issues.
Changes for v3:
- Address review comments, cleaning up variable names and rewording error messages.
- Rebase against current master.
- Apply as many suggested simplifications as feasible.

 doc/uImage.FIT/signature.txt |  4 +-
 include/u-boot/rsa.h         |  1 +
 lib/rsa/rsa-sign.c           | 60 ++++++++++++++++++++++++++--
 lib/rsa/rsa-verify.c         | 93 +++++++++++++++++++++++++++++++++++++++++---
 test/vboot/vboot_test.sh     | 10 ++++-
 5 files changed, 158 insertions(+), 10 deletions(-)
doc/uImage.FIT/signature.txt +2 -1
index a6ab543..b2f89fc 100644
@@ -66,7 +66,8 @@ Creating an RSA key and certificate
 -----------------------------------
 To create a new public key, size 2048 bits:
 
-$ openssl genrsa -F4 -out keys/dev.key 2048
+$ openssl genpkey -algorithm RSA -out keys/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537
 
 To create a certificate for this:
 

doc/uImage.FIT/signature.txt For RSA the following are mandatory: · line 163

Quoted 4 lines
doc/uImage.FIT/signature.txt +1 -0
@@ -159,6 +160,7 @@ For RSA the following are mandatory:
 
 - rsa,num-bits: Number of key bits (e.g. 2048)
 - rsa,modulus: Modulus (N) as a big-endian multi-word integer
+- rsa,exponent: Public exponent (E) as a 64 bit unsigned integer

This is listed as mandatory, but the code makes it options. I think it should probably be optional, and documented that way here.

Quoted 67 lines
doc/uImage.FIT/signature.txt +0 -0
 - rsa,r-squared: (2^num-bits)^2 as a big-endian multi-word integer
 - rsa,n0-inverse: -1 / modulus[0] mod 2^32
 
include/u-boot/rsa.h +1 -0
index 325751a..fd08a61 100644
@@ -27,6 +27,7 @@ struct rsa_public_key {
        uint32_t n0inv;         /* -1 / modulus[0] mod 2^32 */
        uint32_t *modulus;      /* modulus as little endian array */
        uint32_t *rr;           /* R^2 as little endian array */
+       uint64_t exponent;      /* public exponent */
 };
 
 #if IMAGE_ENABLE_SIGN
lib/rsa/rsa-sign.c +43 -0
index 83f5e87..f4d4338 100644
@@ -261,10 +261,57 @@ err_priv:
 }
 
 /*
+ * rsa_get_exponent(): - Get the public exponent from an RSA key
+ */
+static int rsa_get_exponent(RSA *key, uint64_t *e)
 
+{
+       int ret;
+       BIGNUM *bn_te;
+       uint64_t te;
+
+       ret = -EINVAL;
+       bn_te = NULL;
+
+       if (!e)
+               goto cleanup;
+
+       if (BN_num_bits(key->e) > 64)
+               goto cleanup;
+
+       *e = BN_get_word(key->e);
+
+       if (BN_num_bits(key->e) < 33) {
+               ret = 0;
+               goto cleanup;
+       }
+
+       bn_te = BN_dup(key->e);
+       if (!bn_te)
+               goto cleanup;
+
+       if (!BN_rshift(bn_te, bn_te, 32))
+               goto cleanup;
+
+       if (!BN_mask_bits(bn_te, 32))
+               goto cleanup;
+
+       te = BN_get_word(bn_te);
+       te <<= 32;
+       *e |= te;
+       ret = 0;
+
+cleanup:
+       if (bn_te)
+               BN_free(bn_te);
+

lib/rsa/rsa-sign.c err_priv: · line 315

Quoted 10 lines
lib/rsa/rsa-sign.c +6 -2
+       return ret;
+}
+
+/*
  * rsa_get_params(): - Get the important parameters of an RSA public key
  */
-int rsa_get_params(RSA *key, uint32_t *n0_invp, BIGNUM **modulusp,
-                  BIGNUM **r_squaredp)
+int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
+                  BIGNUM **modulusp, BIGNUM **r_squaredp)

To be consistent, can we use exponentp as the parameter name? It makes it clear it is a pointer.

Quoted 32 lines
lib/rsa/rsa-sign.c +0 -0
 {
        BIGNUM *big1, *big2, *big32, *big2_32;
        BIGNUM *n, *r, *r_squared, *tmp;
lib/rsa/rsa-sign.c +5 -1
@@ -286,6 +333,9 @@ int rsa_get_params(RSA *key, uint32_t *n0_invp, BIGNUM **modulusp,
                return -ENOMEM;
        }
 
+       if (0 != rsa_get_exponent(key, exponent))
+               ret = -1;
+
        if (!BN_copy(n, key->n) || !BN_set_word(big1, 1L) ||
            !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
                ret = -1;
@@ -386,6 +436,7 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
 int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
 {
        BIGNUM *modulus, *r_squared;
+       uint64_t exponent;
        uint32_t n0_inv;
        int parent, node;
        char name[100];
@@ -397,7 +448,7 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
        ret = rsa_get_pub_key(info->keydir, info->keyname, &rsa);
        if (ret)
                return ret;
-       ret = rsa_get_params(rsa, &n0_inv, &modulus, &r_squared);
+       ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
        if (ret)
                return ret;
        bits = BN_num_bits(modulus);

lib/rsa/rsa-sign.c int rsa_add_verify_data(struct image_sign_info *info, void *keydest) · line 497

Quoted 5 lines
lib/rsa/rsa-sign.c +2 -0
@@ -442,6 +493,9 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
        if (!ret)
                ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
        if (!ret) {
+               ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
+       }

You don't need the {} around this.

Quoted 156 lines
lib/rsa/rsa-sign.c +1 -0
+       if (!ret) {
                ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
                                     bits);
        }
lib/rsa/rsa-verify.c +88 -5
index bcb9063..c5bcdb6 100644
@@ -26,6 +26,9 @@
 #define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a)
 #define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a))
 
+/* Default public exponent for backward compatibility */
+#define RSA_DEFAULT_PUBEXP     65537
+
 /**
  * subtract_modulus() - subtract modulus from the given value
  *
@@ -123,6 +126,48 @@ static void montgomery_mul(const struct rsa_public_key *key,
 }
 
 /**
+ * num_pub_exponent_bits() - Number of bits in the public exponent
+ *
+ * @key:       RSA key
+ * @num_bits:  Storage for the number of public exponent bits
+ */
+static int num_public_exponent_bits(const struct rsa_public_key *key,
+               int *num_bits)
+{
+       uint64_t exponent;
+       int exponent_bits;
+       const uint max_bits = (sizeof(exponent) * 8);
+
+       exponent = key->exponent;
+       exponent_bits = 0;
+
+       if (!exponent) {
+               *num_bits = exponent_bits;
+               return 0;
+       }
+
+       for (exponent_bits = 1; exponent_bits < max_bits + 1; ++exponent_bits)
+               if (!(exponent >>= 1)) {
+                       *num_bits = exponent_bits;
+                       return 0;
+               }
+
+       return -EINVAL;
+}
+
+/**
+ * is_public_exponent_bit_set() - Check if a bit in the public exponent is set
+ *
+ * @key:       RSA key
+ * @pos:       The bit position to check
+ */
+static int is_public_exponent_bit_set(const struct rsa_public_key *key,
+               int pos)
+{
+       return key->exponent & (1ULL << pos);
+}
+
+/**
  * pow_mod() - in-place public exponentiation
  *
  * @key:       RSA key
@@ -132,6 +177,7 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
 {
        uint32_t *result, *ptr;
        uint i;
+       int j, k;
 
        /* Sanity check for stack size - key->len is in 32-bit words */
        if (key->len > RSA_MAX_KEY_BITS / 32) {
@@ -141,18 +187,48 @@ static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
        }
 
        uint32_t val[key->len], acc[key->len], tmp[key->len];
+       uint32_t a_scaled[key->len];
        result = tmp;  /* Re-use location. */
 
        /* Convert from big endian byte array to little endian word array. */
        for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
                val[i] = get_unaligned_be32(ptr);
 
-       montgomery_mul(key, acc, val, key->rr);  /* axx = a * RR / R mod M */
-       for (i = 0; i < 16; i += 2) {
-               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
-               montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
+       if (0 != num_public_exponent_bits(key, &k))
+               return -EINVAL;
+
+       if (k < 2) {
+               debug("Public exponent is too short (%d bits, minimum 2)\n",
+                     k);
+               return -EINVAL;
        }
-       montgomery_mul(key, result, acc, val);  /* result = XX * a / R mod M */
+
+       if (!is_public_exponent_bit_set(key, 0)) {
+               debug("LSB of RSA public exponent must be set.\n");
+               return -EINVAL;
+       }
+
+       /* the bit at e[k-1] is 1 by definition, so start with: C := M */
+       montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
+       /* retain scaled version for intermediate use */
+       memcpy(a_scaled, acc, key->len * sizeof(a_scaled[0]));
+
+       for (j = k - 2; j > 0; --j) {
+               montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+
+               if (is_public_exponent_bit_set(key, j)) {
+                       /* acc = tmp * val / R mod n */
+                       montgomery_mul(key, acc, tmp, a_scaled);
+               } else {
+                       /* e[j] == 0, copy tmp back to acc for next operation */
+                       memcpy(acc, tmp, key->len * sizeof(acc[0]));
+               }
+       }
+
+       /* the bit at e[0] is always 1 */
+       montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
+       montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
+       memcpy(result, acc, key->len * sizeof(result[0]));
 
        /* Make sure result < mod; result is@most 1x mod too large. */
        if (greater_equal_modulus(key, result))
@@ -229,6 +305,8 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
        const void *blob = info->fdt_blob;
        struct rsa_public_key key;
        const void *modulus, *rr;
+       const uint64_t *public_exponent;
+       int length;
        int ret;
 
        if (node < 0) {
@@ -241,6 +319,11 @@ static int rsa_verify_with_keynode(struct image_sign_info *info,
        }
        key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
        key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
+       public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
+       if (!public_exponent || length < sizeof(*public_exponent))
+               key.exponent = RSA_DEFAULT_PUBEXP;
+       else
+               key.exponent = fdt64_to_cpu(*public_exponent);
        modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
        rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
        if (!key.len || !modulus || !rr) {
test/vboot/vboot_test.sh +0 -0
index 8074fc6..6d7abb8 100755

test/vboot/vboot_test.sh echo ${mkimage} -D "${dtc}" · line 61

Quoted 8 lines
test/vboot/vboot_test.sh +5 -0
@@ -54,8 +54,16 @@ echo ${mkimage} -D "${dtc}"
 echo "Build keys"
 mkdir -p ${keys}
 
+PUBLIC_EXPONENT=${1}
+
+if [ -z "${PUBLIC_EXPONENT}" ]; then
+       PUBLIC_EXPONENT=65537
+fi

Tests should not take parameters - you should adjust the script to run the tests twice - once with the default exponent and once with another chosen exponent. Then running the test tests all the code. See what was done for sha256 and sha1 as an example. Note you don't need to test all combinations of sha1/sha256/default exp,/special exp. unless that is easier.

Quoted 11 lines
test/vboot/vboot_test.sh +4 -2
+
 # Create an RSA key pair
-openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null
+openssl genpkey -algorithm RSA -out ${keys}/dev.key \
+    -pkeyopt rsa_keygen_bits:2048 \
+    -pkeyopt rsa_keygen_pubexp:${PUBLIC_EXPONENT} 2>/dev/null
 
 # Create a certificate containing the public key
 openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt
--
Quoted 2 lines

1.9.1

Regards,
Simon

#10 lore raw

PATCH v3 1/1 Implement generalised RSA public exponents for verified boot

In reply to michael at smart-africa.com
Quoted 15 lines · Michael van der Westhuizen

From: Michael van der Westhuizen <[email protected]>

Remove the verified boot limitation that only allows a single RSA public exponent of 65537 (F4). This change allows use with existing PKI infrastructure and has been tested with HSM-based PKI.

Change the configuration OF tree format to store the RSA public exponent as a 64 bit integer and implement backward compatibility for verified boot configuration trees without this extra field.

Parameterise vboot_test.sh to test different public exponents.

Mathematics and other hard work by Andrew Bott.

Quoted 8 lines

Tested with the following public exponents: 3, 5, 17, 257, 39981, 50457, 65537 and 4294967297.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140810/f70725cf/attachment.pgp>