2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/types.h>
11 #include <asm/byteorder.h>
12 #include <asm/errno.h>
13 #include <asm/types.h>
14 #include <asm/unaligned.h>
18 #include <fdt_support.h>
20 #include <u-boot/rsa.h>
21 #include <u-boot/sha1.h>
22 #include <u-boot/sha256.h>
24 #define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
26 #define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a)
27 #define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a))
30 * subtract_modulus() - subtract modulus from the given value
32 * @key: Key containing modulus to subtract
33 * @num: Number to subtract modulus from, as little endian word array
35 static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
40 for (i = 0; i < key->len; i++) {
41 acc += (uint64_t)num[i] - key->modulus[i];
42 num[i] = (uint32_t)acc;
48 * greater_equal_modulus() - check if a value is >= modulus
50 * @key: Key containing modulus to check
51 * @num: Number to check against modulus, as little endian word array
52 * @return 0 if num < modulus, 1 if num >= modulus
54 static int greater_equal_modulus(const struct rsa_public_key *key,
59 for (i = key->len - 1; i >= 0; i--) {
60 if (num[i] < key->modulus[i])
62 if (num[i] > key->modulus[i])
70 * montgomery_mul_add_step() - Perform montgomery multiply-add step
72 * Operation: montgomery result[] += a * b[] / n0inv % modulus
75 * @result: Place to put result, as little endian word array
77 * @b: Multiplicand, as little endian word array
79 static void montgomery_mul_add_step(const struct rsa_public_key *key,
80 uint32_t result[], const uint32_t a, const uint32_t b[])
82 uint64_t acc_a, acc_b;
86 acc_a = (uint64_t)a * b[0] + result[0];
87 d0 = (uint32_t)acc_a * key->n0inv;
88 acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
89 for (i = 1; i < key->len; i++) {
90 acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
91 acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
93 result[i - 1] = (uint32_t)acc_b;
96 acc_a = (acc_a >> 32) + (acc_b >> 32);
98 result[i - 1] = (uint32_t)acc_a;
101 subtract_modulus(key, result);
105 * montgomery_mul() - Perform montgomery mutitply
107 * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
110 * @result: Place to put result, as little endian word array
111 * @a: Multiplier, as little endian word array
112 * @b: Multiplicand, as little endian word array
114 static void montgomery_mul(const struct rsa_public_key *key,
115 uint32_t result[], uint32_t a[], const uint32_t b[])
119 for (i = 0; i < key->len; ++i)
121 for (i = 0; i < key->len; ++i)
122 montgomery_mul_add_step(key, result, a[i], b);
126 * pow_mod() - in-place public exponentiation
129 * @inout: Big-endian word array containing value and result
131 static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
133 uint32_t *result, *ptr;
136 /* Sanity check for stack size - key->len is in 32-bit words */
137 if (key->len > RSA_MAX_KEY_BITS / 32) {
138 debug("RSA key words %u exceeds maximum %d\n", key->len,
139 RSA_MAX_KEY_BITS / 32);
143 uint32_t val[key->len], acc[key->len], tmp[key->len];
144 result = tmp; /* Re-use location. */
146 /* Convert from big endian byte array to little endian word array. */
147 for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
148 val[i] = get_unaligned_be32(ptr);
150 montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
151 for (i = 0; i < 16; i += 2) {
152 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
153 montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
155 montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
157 /* Make sure result < mod; result is at most 1x mod too large. */
158 if (greater_equal_modulus(key, result))
159 subtract_modulus(key, result);
161 /* Convert to bigendian byte array */
162 for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
163 put_unaligned_be32(result[i], ptr);
167 static int rsa_verify_key(const struct rsa_public_key *key, const uint8_t *sig,
168 const uint32_t sig_len, const uint8_t *hash,
169 struct checksum_algo *algo)
171 const uint8_t *padding;
175 if (!key || !sig || !hash || !algo)
178 if (sig_len != (key->len * sizeof(uint32_t))) {
179 debug("Signature is of incorrect length %d\n", sig_len);
183 debug("Checksum algorithm: %s", algo->name);
185 /* Sanity check for stack size */
186 if (sig_len > RSA_MAX_SIG_BITS / 8) {
187 debug("Signature length %u exceeds maximum %d\n", sig_len,
188 RSA_MAX_SIG_BITS / 8);
192 uint32_t buf[sig_len / sizeof(uint32_t)];
194 memcpy(buf, sig, sig_len);
196 ret = pow_mod(key, buf);
200 padding = algo->rsa_padding;
201 pad_len = algo->pad_len - algo->checksum_len;
203 /* Check pkcs1.5 padding bytes. */
204 if (memcmp(buf, padding, pad_len)) {
205 debug("In RSAVerify(): Padding check failed!\n");
210 if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
211 debug("In RSAVerify(): Hash check failed!\n");
218 static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
222 for (i = 0; i < len; i++)
223 dst[i] = fdt32_to_cpu(src[len - 1 - i]);
226 static int rsa_verify_with_keynode(struct image_sign_info *info,
227 const void *hash, uint8_t *sig, uint sig_len, int node)
229 const void *blob = info->fdt_blob;
230 struct rsa_public_key key;
231 const void *modulus, *rr;
235 debug("%s: Skipping invalid node", __func__);
238 if (!fdt_getprop(blob, node, "rsa,n0-inverse", NULL)) {
239 debug("%s: Missing rsa,n0-inverse", __func__);
242 key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
243 key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
244 modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
245 rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
246 if (!key.len || !modulus || !rr) {
247 debug("%s: Missing RSA key info", __func__);
251 /* Sanity check for stack size */
252 if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
253 debug("RSA key bits %u outside allowed range %d..%d\n",
254 key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
257 key.len /= sizeof(uint32_t) * 8;
258 uint32_t key1[key.len], key2[key.len];
262 rsa_convert_big_endian(key.modulus, modulus, key.len);
263 rsa_convert_big_endian(key.rr, rr, key.len);
264 if (!key.modulus || !key.rr) {
265 debug("%s: Out of memory", __func__);
269 debug("key length %d\n", key.len);
270 ret = rsa_verify_key(&key, sig, sig_len, hash, info->algo->checksum);
272 printf("%s: RSA failed to verify: %d\n", __func__, ret);
279 int rsa_verify(struct image_sign_info *info,
280 const struct image_region region[], int region_count,
281 uint8_t *sig, uint sig_len)
283 const void *blob = info->fdt_blob;
284 /* Reserve memory for maximum checksum-length */
285 uint8_t hash[info->algo->checksum->pad_len];
292 * Verify that the checksum-length does not exceed the
293 * rsa-signature-length
295 if (info->algo->checksum->checksum_len >
296 info->algo->checksum->pad_len) {
297 debug("%s: invlaid checksum-algorithm %s for %s\n",
298 __func__, info->algo->checksum->name, info->algo->name);
302 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
304 debug("%s: No signature node found\n", __func__);
308 /* Calculate checksum with checksum-algorithm */
309 info->algo->checksum->calculate(region, region_count, hash);
311 /* See if we must use a particular key */
312 if (info->required_keynode != -1) {
313 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
314 info->required_keynode);
319 /* Look for a key that matches our hint */
320 snprintf(name, sizeof(name), "key-%s", info->keyname);
321 node = fdt_subnode_offset(blob, sig_node, name);
322 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
326 /* No luck, so try each of the keys in turn */
327 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
328 (noffset >= 0) && (ndepth > 0);
329 noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
330 if (ndepth == 1 && noffset != node) {
331 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,