2 * Copyright (c) 2013, Google Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <asm/byteorder.h>
25 #include <asm/errno.h>
26 #include <asm/unaligned.h>
29 * struct rsa_public_key - holder for a public key
31 * An RSA public key consists of a modulus (typically called N), the inverse
32 * and R^2, where R is 2^(# key bits).
34 struct rsa_public_key {
35 uint len; /* Length of modulus[] in number of uint32_t */
36 uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */
37 uint32_t *modulus; /* modulus as little endian array */
38 uint32_t *rr; /* R^2 as little endian array */
41 #define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
43 #define RSA2048_BYTES (2048 / 8)
45 /* This is the minimum/maximum key size we support, in bits */
46 #define RSA_MIN_KEY_BITS 2048
47 #define RSA_MAX_KEY_BITS 2048
49 /* This is the maximum signature length that we support, in bits */
50 #define RSA_MAX_SIG_BITS 2048
52 static const uint8_t padding_sha1_rsa2048[RSA2048_BYTES - SHA1_SUM_LEN] = {
53 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x21, 0x30,
81 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a,
82 0x05, 0x00, 0x04, 0x14
86 * subtract_modulus() - subtract modulus from the given value
88 * @key: Key containing modulus to subtract
89 * @num: Number to subtract modulus from, as little endian word array
91 static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
96 for (i = 0; i < key->len; i++) {
97 acc += (uint64_t)num[i] - key->modulus[i];
98 num[i] = (uint32_t)acc;
104 * greater_equal_modulus() - check if a value is >= modulus
106 * @key: Key containing modulus to check
107 * @num: Number to check against modulus, as little endian word array
108 * @return 0 if num < modulus, 1 if num >= modulus
110 static int greater_equal_modulus(const struct rsa_public_key *key,
115 for (i = key->len - 1; i >= 0; i--) {
116 if (num[i] < key->modulus[i])
118 if (num[i] > key->modulus[i])
122 return 1; /* equal */
126 * montgomery_mul_add_step() - Perform montgomery multiply-add step
128 * Operation: montgomery result[] += a * b[] / n0inv % modulus
131 * @result: Place to put result, as little endian word array
133 * @b: Multiplicand, as little endian word array
135 static void montgomery_mul_add_step(const struct rsa_public_key *key,
136 uint32_t result[], const uint32_t a, const uint32_t b[])
138 uint64_t acc_a, acc_b;
142 acc_a = (uint64_t)a * b[0] + result[0];
143 d0 = (uint32_t)acc_a * key->n0inv;
144 acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
145 for (i = 1; i < key->len; i++) {
146 acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
147 acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
149 result[i - 1] = (uint32_t)acc_b;
152 acc_a = (acc_a >> 32) + (acc_b >> 32);
154 result[i - 1] = (uint32_t)acc_a;
157 subtract_modulus(key, result);
161 * montgomery_mul() - Perform montgomery mutitply
163 * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
166 * @result: Place to put result, as little endian word array
167 * @a: Multiplier, as little endian word array
168 * @b: Multiplicand, as little endian word array
170 static void montgomery_mul(const struct rsa_public_key *key,
171 uint32_t result[], uint32_t a[], const uint32_t b[])
175 for (i = 0; i < key->len; ++i)
177 for (i = 0; i < key->len; ++i)
178 montgomery_mul_add_step(key, result, a[i], b);
182 * pow_mod() - in-place public exponentiation
185 * @inout: Big-endian word array containing value and result
187 static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
189 uint32_t *result, *ptr;
192 /* Sanity check for stack size - key->len is in 32-bit words */
193 if (key->len > RSA_MAX_KEY_BITS / 32) {
194 debug("RSA key words %u exceeds maximum %d\n", key->len,
195 RSA_MAX_KEY_BITS / 32);
199 uint32_t val[key->len], acc[key->len], tmp[key->len];
200 result = tmp; /* Re-use location. */
202 /* Convert from big endian byte array to little endian word array. */
203 for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
204 val[i] = get_unaligned_be32(ptr);
206 montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
207 for (i = 0; i < 16; i += 2) {
208 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
209 montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
211 montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
213 /* Make sure result < mod; result is at most 1x mod too large. */
214 if (greater_equal_modulus(key, result))
215 subtract_modulus(key, result);
217 /* Convert to bigendian byte array */
218 for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
219 put_unaligned_be32(result[i], ptr);
224 static int rsa_verify_key(const struct rsa_public_key *key, const uint8_t *sig,
225 const uint32_t sig_len, const uint8_t *hash)
227 const uint8_t *padding;
231 if (!key || !sig || !hash)
234 if (sig_len != (key->len * sizeof(uint32_t))) {
235 debug("Signature is of incorrect length %d\n", sig_len);
239 /* Sanity check for stack size */
240 if (sig_len > RSA_MAX_SIG_BITS / 8) {
241 debug("Signature length %u exceeds maximum %d\n", sig_len,
242 RSA_MAX_SIG_BITS / 8);
246 uint32_t buf[sig_len / sizeof(uint32_t)];
248 memcpy(buf, sig, sig_len);
250 ret = pow_mod(key, buf);
254 /* Determine padding to use depending on the signature type. */
255 padding = padding_sha1_rsa2048;
256 pad_len = RSA2048_BYTES - SHA1_SUM_LEN;
258 /* Check pkcs1.5 padding bytes. */
259 if (memcmp(buf, padding, pad_len)) {
260 debug("In RSAVerify(): Padding check failed!\n");
265 if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
266 debug("In RSAVerify(): Hash check failed!\n");
273 static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
277 for (i = 0; i < len; i++)
278 dst[i] = fdt32_to_cpu(src[len - 1 - i]);
281 static int rsa_verify_with_keynode(struct image_sign_info *info,
282 const void *hash, uint8_t *sig, uint sig_len, int node)
284 const void *blob = info->fdt_blob;
285 struct rsa_public_key key;
286 const void *modulus, *rr;
290 debug("%s: Skipping invalid node", __func__);
293 if (!fdt_getprop(blob, node, "rsa,n0-inverse", NULL)) {
294 debug("%s: Missing rsa,n0-inverse", __func__);
297 key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
298 key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
299 modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
300 rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
301 if (!key.len || !modulus || !rr) {
302 debug("%s: Missing RSA key info", __func__);
306 /* Sanity check for stack size */
307 if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
308 debug("RSA key bits %u outside allowed range %d..%d\n",
309 key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
312 key.len /= sizeof(uint32_t) * 8;
313 uint32_t key1[key.len], key2[key.len];
317 rsa_convert_big_endian(key.modulus, modulus, key.len);
318 rsa_convert_big_endian(key.rr, rr, key.len);
319 if (!key.modulus || !key.rr) {
320 debug("%s: Out of memory", __func__);
324 debug("key length %d\n", key.len);
325 ret = rsa_verify_key(&key, sig, sig_len, hash);
327 printf("%s: RSA failed to verify: %d\n", __func__, ret);
334 int rsa_verify(struct image_sign_info *info,
335 const struct image_region region[], int region_count,
336 uint8_t *sig, uint sig_len)
338 const void *blob = info->fdt_blob;
339 uint8_t hash[SHA1_SUM_LEN];
346 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
348 debug("%s: No signature node found\n", __func__);
353 for (i = 0; i < region_count; i++)
354 sha1_update(&ctx, region[i].data, region[i].size);
355 sha1_finish(&ctx, hash);
357 /* See if we must use a particular key */
358 if (info->required_keynode != -1) {
359 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
360 info->required_keynode);
365 /* Look for a key that matches our hint */
366 snprintf(name, sizeof(name), "key-%s", info->keyname);
367 node = fdt_subnode_offset(blob, sig_node, name);
368 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
372 /* No luck, so try each of the keys in turn */
373 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
374 (noffset >= 0) && (ndepth > 0);
375 noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
376 if (ndepth == 1 && noffset != node) {
377 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,