2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/byteorder.h>
13 #include <asm/errno.h>
14 #include <asm/unaligned.h>
16 #define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
18 #define RSA2048_BYTES (2048 / 8)
20 /* This is the minimum/maximum key size we support, in bits */
21 #define RSA_MIN_KEY_BITS 2048
22 #define RSA_MAX_KEY_BITS 2048
24 /* This is the maximum signature length that we support, in bits */
25 #define RSA_MAX_SIG_BITS 2048
28 * subtract_modulus() - subtract modulus from the given value
30 * @key: Key containing modulus to subtract
31 * @num: Number to subtract modulus from, as little endian word array
33 static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
38 for (i = 0; i < key->len; i++) {
39 acc += (uint64_t)num[i] - key->modulus[i];
40 num[i] = (uint32_t)acc;
46 * greater_equal_modulus() - check if a value is >= modulus
48 * @key: Key containing modulus to check
49 * @num: Number to check against modulus, as little endian word array
50 * @return 0 if num < modulus, 1 if num >= modulus
52 static int greater_equal_modulus(const struct rsa_public_key *key,
57 for (i = key->len - 1; i >= 0; i--) {
58 if (num[i] < key->modulus[i])
60 if (num[i] > key->modulus[i])
68 * montgomery_mul_add_step() - Perform montgomery multiply-add step
70 * Operation: montgomery result[] += a * b[] / n0inv % modulus
73 * @result: Place to put result, as little endian word array
75 * @b: Multiplicand, as little endian word array
77 static void montgomery_mul_add_step(const struct rsa_public_key *key,
78 uint32_t result[], const uint32_t a, const uint32_t b[])
80 uint64_t acc_a, acc_b;
84 acc_a = (uint64_t)a * b[0] + result[0];
85 d0 = (uint32_t)acc_a * key->n0inv;
86 acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
87 for (i = 1; i < key->len; i++) {
88 acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
89 acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
91 result[i - 1] = (uint32_t)acc_b;
94 acc_a = (acc_a >> 32) + (acc_b >> 32);
96 result[i - 1] = (uint32_t)acc_a;
99 subtract_modulus(key, result);
103 * montgomery_mul() - Perform montgomery mutitply
105 * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
108 * @result: Place to put result, as little endian word array
109 * @a: Multiplier, as little endian word array
110 * @b: Multiplicand, as little endian word array
112 static void montgomery_mul(const struct rsa_public_key *key,
113 uint32_t result[], uint32_t a[], const uint32_t b[])
117 for (i = 0; i < key->len; ++i)
119 for (i = 0; i < key->len; ++i)
120 montgomery_mul_add_step(key, result, a[i], b);
124 * pow_mod() - in-place public exponentiation
127 * @inout: Big-endian word array containing value and result
129 static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
131 uint32_t *result, *ptr;
134 /* Sanity check for stack size - key->len is in 32-bit words */
135 if (key->len > RSA_MAX_KEY_BITS / 32) {
136 debug("RSA key words %u exceeds maximum %d\n", key->len,
137 RSA_MAX_KEY_BITS / 32);
141 uint32_t val[key->len], acc[key->len], tmp[key->len];
142 result = tmp; /* Re-use location. */
144 /* Convert from big endian byte array to little endian word array. */
145 for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
146 val[i] = get_unaligned_be32(ptr);
148 montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
149 for (i = 0; i < 16; i += 2) {
150 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
151 montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
153 montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
155 /* Make sure result < mod; result is at most 1x mod too large. */
156 if (greater_equal_modulus(key, result))
157 subtract_modulus(key, result);
159 /* Convert to bigendian byte array */
160 for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
161 put_unaligned_be32(result[i], ptr);
166 static int rsa_verify_key(const struct rsa_public_key *key, const uint8_t *sig,
167 const uint32_t sig_len, const uint8_t *hash,
168 struct checksum_algo *algo)
170 const uint8_t *padding;
174 if (!key || !sig || !hash || !algo)
177 if (sig_len != (key->len * sizeof(uint32_t))) {
178 debug("Signature is of incorrect length %d\n", sig_len);
182 debug("Checksum algorithm: %s", algo->name);
184 /* Sanity check for stack size */
185 if (sig_len > RSA_MAX_SIG_BITS / 8) {
186 debug("Signature length %u exceeds maximum %d\n", sig_len,
187 RSA_MAX_SIG_BITS / 8);
191 uint32_t buf[sig_len / sizeof(uint32_t)];
193 memcpy(buf, sig, sig_len);
195 ret = pow_mod(key, buf);
199 padding = algo->rsa_padding;
200 pad_len = RSA2048_BYTES - algo->checksum_len;
202 /* Check pkcs1.5 padding bytes. */
203 if (memcmp(buf, padding, pad_len)) {
204 debug("In RSAVerify(): Padding check failed!\n");
209 if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
210 debug("In RSAVerify(): Hash check failed!\n");
217 static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
221 for (i = 0; i < len; i++)
222 dst[i] = fdt32_to_cpu(src[len - 1 - i]);
225 static int rsa_verify_with_keynode(struct image_sign_info *info,
226 const void *hash, uint8_t *sig, uint sig_len, int node)
228 const void *blob = info->fdt_blob;
229 struct rsa_public_key key;
230 const void *modulus, *rr;
234 debug("%s: Skipping invalid node", __func__);
237 if (!fdt_getprop(blob, node, "rsa,n0-inverse", NULL)) {
238 debug("%s: Missing rsa,n0-inverse", __func__);
241 key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
242 key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
243 modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
244 rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
245 if (!key.len || !modulus || !rr) {
246 debug("%s: Missing RSA key info", __func__);
250 /* Sanity check for stack size */
251 if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
252 debug("RSA key bits %u outside allowed range %d..%d\n",
253 key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
256 key.len /= sizeof(uint32_t) * 8;
257 uint32_t key1[key.len], key2[key.len];
261 rsa_convert_big_endian(key.modulus, modulus, key.len);
262 rsa_convert_big_endian(key.rr, rr, key.len);
263 if (!key.modulus || !key.rr) {
264 debug("%s: Out of memory", __func__);
268 debug("key length %d\n", key.len);
269 ret = rsa_verify_key(&key, sig, sig_len, hash, info->algo->checksum);
271 printf("%s: RSA failed to verify: %d\n", __func__, ret);
278 int rsa_verify(struct image_sign_info *info,
279 const struct image_region region[], int region_count,
280 uint8_t *sig, uint sig_len)
282 const void *blob = info->fdt_blob;
283 /* Reserve memory for maximum checksum-length */
284 uint8_t hash[RSA2048_BYTES];
291 * Verify that the checksum-length does not exceed the
292 * rsa-signature-length
294 if (info->algo->checksum->checksum_len > RSA2048_BYTES) {
295 debug("%s: invlaid checksum-algorithm %s for RSA2048\n",
296 __func__, info->algo->checksum->name);
300 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
302 debug("%s: No signature node found\n", __func__);
306 /* Calculate checksum with checksum-algorithm */
307 info->algo->checksum->calculate(region, region_count, hash);
309 /* See if we must use a particular key */
310 if (info->required_keynode != -1) {
311 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
312 info->required_keynode);
317 /* Look for a key that matches our hint */
318 snprintf(name, sizeof(name), "key-%s", info->keyname);
319 node = fdt_subnode_offset(blob, sig_node, name);
320 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
324 /* No luck, so try each of the keys in turn */
325 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
326 (noffset >= 0) && (ndepth > 0);
327 noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
328 if (ndepth == 1 && noffset != node) {
329 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,