blob: 2057f6819db8ac721ad99d4980ac9d579f3ff5b6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass35191a32013-06-13 15:10:02 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass35191a32013-06-13 15:10:02 -07004 */
5
Heiko Schocherd7b42322014-03-03 12:19:30 +01006#ifndef USE_HOSTCC
Simon Glass35191a32013-06-13 15:10:02 -07007#include <common.h>
8#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Heiko Schocherd7b42322014-03-03 12:19:30 +010011#include <asm/types.h>
Simon Glass35191a32013-06-13 15:10:02 -070012#include <asm/byteorder.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Heiko Schocherd7b42322014-03-03 12:19:30 +010014#include <asm/types.h>
Simon Glass35191a32013-06-13 15:10:02 -070015#include <asm/unaligned.h>
Ruchika Guptaa0d3ca62015-01-23 16:01:54 +053016#include <dm.h>
Heiko Schocherd7b42322014-03-03 12:19:30 +010017#else
18#include "fdt_host.h"
19#include "mkimage.h"
20#include <fdt_support.h>
21#endif
AKASHI Takahiro75822f42020-02-21 15:12:59 +090022#include <linux/kconfig.h>
Ruchika Guptab92ebab2015-01-23 16:01:50 +053023#include <u-boot/rsa-mod-exp.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020024#include <u-boot/rsa.h>
Heiko Schocherd7b42322014-03-03 12:19:30 +010025
AKASHI Takahiro75822f42020-02-21 15:12:59 +090026#ifndef __UBOOT__
27/*
28 * NOTE:
29 * Since host tools, like mkimage, make use of openssl library for
30 * RSA encryption, rsa_verify_with_pkey()/rsa_gen_key_prop() are
31 * of no use and should not be compiled in.
32 * So just turn off CONFIG_RSA_VERIFY_WITH_PKEY.
33 */
34
35#undef CONFIG_RSA_VERIFY_WITH_PKEY
36#endif
37
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +020038/* Default public exponent for backward compatibility */
39#define RSA_DEFAULT_PUBEXP 65537
40
Simon Glass35191a32013-06-13 15:10:02 -070041/**
Andrew Duda3db9ff02016-11-08 18:53:40 +000042 * rsa_verify_padding() - Verify RSA message padding is valid
43 *
44 * Verify a RSA message's padding is consistent with PKCS1.5
45 * padding as described in the RSA PKCS#1 v2.1 standard.
46 *
47 * @msg: Padded message
48 * @pad_len: Number of expected padding bytes
49 * @algo: Checksum algo structure having information on DER encoding etc.
50 * @return 0 on success, != 0 on failure
51 */
52static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
53 struct checksum_algo *algo)
54{
55 int ff_len;
56 int ret;
57
58 /* first byte must be 0x00 */
59 ret = *msg++;
60 /* second byte must be 0x01 */
61 ret |= *msg++ ^ 0x01;
62 /* next ff_len bytes must be 0xff */
63 ff_len = pad_len - algo->der_len - 3;
64 ret |= *msg ^ 0xff;
65 ret |= memcmp(msg, msg+1, ff_len-1);
66 msg += ff_len;
67 /* next byte must be 0x00 */
68 ret |= *msg++;
69 /* next der_len bytes must match der_prefix */
70 ret |= memcmp(msg, algo->der_prefix, algo->der_len);
71
72 return ret;
73}
74
Philippe Reynes12468352018-11-14 13:51:00 +010075int padding_pkcs_15_verify(struct image_sign_info *info,
76 uint8_t *msg, int msg_len,
77 const uint8_t *hash, int hash_len)
78{
79 struct checksum_algo *checksum = info->checksum;
80 int ret, pad_len = msg_len - checksum->checksum_len;
81
82 /* Check pkcs1.5 padding bytes. */
83 ret = rsa_verify_padding(msg, pad_len, checksum);
84 if (ret) {
85 debug("In RSAVerify(): Padding check failed!\n");
86 return -EINVAL;
87 }
88
89 /* Check hash. */
90 if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
91 debug("In RSAVerify(): Hash check failed!\n");
92 return -EACCES;
93 }
94
95 return 0;
96}
97
Philippe Reynes47d73f02018-11-14 13:51:01 +010098#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
99static void u32_i2osp(uint32_t val, uint8_t *buf)
100{
101 buf[0] = (uint8_t)((val >> 24) & 0xff);
102 buf[1] = (uint8_t)((val >> 16) & 0xff);
103 buf[2] = (uint8_t)((val >> 8) & 0xff);
104 buf[3] = (uint8_t)((val >> 0) & 0xff);
105}
106
107/**
108 * mask_generation_function1() - generate an octet string
109 *
110 * Generate an octet string used to check rsa signature.
111 * It use an input octet string and a hash function.
112 *
113 * @checksum: A Hash function
114 * @seed: Specifies an input variable octet string
115 * @seed_len: Size of the input octet string
116 * @output: Specifies the output octet string
117 * @output_len: Size of the output octet string
118 * @return 0 if the octet string was correctly generated, others on error
119 */
120static int mask_generation_function1(struct checksum_algo *checksum,
121 uint8_t *seed, int seed_len,
122 uint8_t *output, int output_len)
123{
124 struct image_region region[2];
125 int ret = 0, i, i_output = 0, region_count = 2;
126 uint32_t counter = 0;
127 uint8_t buf_counter[4], *tmp;
128 int hash_len = checksum->checksum_len;
129
130 memset(output, 0, output_len);
131
132 region[0].data = seed;
133 region[0].size = seed_len;
134 region[1].data = &buf_counter[0];
135 region[1].size = 4;
136
137 tmp = malloc(hash_len);
138 if (!tmp) {
139 debug("%s: can't allocate array tmp\n", __func__);
140 ret = -ENOMEM;
141 goto out;
142 }
143
144 while (i_output < output_len) {
145 u32_i2osp(counter, &buf_counter[0]);
146
147 ret = checksum->calculate(checksum->name,
148 region, region_count,
149 tmp);
150 if (ret < 0) {
151 debug("%s: Error in checksum calculation\n", __func__);
152 goto out;
153 }
154
155 i = 0;
156 while ((i_output < output_len) && (i < hash_len)) {
157 output[i_output] = tmp[i];
158 i_output++;
159 i++;
160 }
161
162 counter++;
163 }
164
165out:
166 free(tmp);
167
168 return ret;
169}
170
171static int compute_hash_prime(struct checksum_algo *checksum,
172 uint8_t *pad, int pad_len,
173 uint8_t *hash, int hash_len,
174 uint8_t *salt, int salt_len,
175 uint8_t *hprime)
176{
177 struct image_region region[3];
178 int ret, region_count = 3;
179
180 region[0].data = pad;
181 region[0].size = pad_len;
182 region[1].data = hash;
183 region[1].size = hash_len;
184 region[2].data = salt;
185 region[2].size = salt_len;
186
187 ret = checksum->calculate(checksum->name, region, region_count, hprime);
188 if (ret < 0) {
189 debug("%s: Error in checksum calculation\n", __func__);
190 goto out;
191 }
192
193out:
194 return ret;
195}
196
Heiko Stuebner1944dc12020-06-18 16:23:26 +0200197/*
198 * padding_pss_verify() - verify the pss padding of a signature
199 *
200 * Only works with a rsa_pss_saltlen:-2 (default value) right now
201 * saltlen:-1 "set the salt length to the digest length" is currently
202 * not supported.
203 *
204 * @info: Specifies key and FIT information
205 * @msg: byte array of message, len equal to msg_len
206 * @msg_len: Message length
207 * @hash: Pointer to the expected hash
208 * @hash_len: Length of the hash
209 */
Philippe Reynes47d73f02018-11-14 13:51:01 +0100210int padding_pss_verify(struct image_sign_info *info,
211 uint8_t *msg, int msg_len,
212 const uint8_t *hash, int hash_len)
213{
214 uint8_t *masked_db = NULL;
215 int masked_db_len = msg_len - hash_len - 1;
216 uint8_t *h = NULL, *hprime = NULL;
217 int h_len = hash_len;
218 uint8_t *db_mask = NULL;
219 int db_mask_len = masked_db_len;
220 uint8_t *db = NULL, *salt = NULL;
221 int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
222 uint8_t pad_zero[8] = { 0 };
223 int ret, i, leftmost_bits = 1;
224 uint8_t leftmost_mask;
225 struct checksum_algo *checksum = info->checksum;
226
227 /* first, allocate everything */
228 masked_db = malloc(masked_db_len);
229 h = malloc(h_len);
230 db_mask = malloc(db_mask_len);
231 db = malloc(db_len);
232 salt = malloc(salt_len);
233 hprime = malloc(hash_len);
234 if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
235 printf("%s: can't allocate some buffer\n", __func__);
236 ret = -ENOMEM;
237 goto out;
238 }
239
240 /* step 4: check if the last byte is 0xbc */
241 if (msg[msg_len - 1] != 0xbc) {
242 printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
243 ret = -EINVAL;
244 goto out;
245 }
246
247 /* step 5 */
248 memcpy(masked_db, msg, masked_db_len);
249 memcpy(h, msg + masked_db_len, h_len);
250
251 /* step 6 */
252 leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
253 if (masked_db[0] & leftmost_mask) {
254 printf("%s: invalid pss padding ", __func__);
255 printf("(leftmost bit of maskedDB not zero)\n");
256 ret = -EINVAL;
257 goto out;
258 }
259
260 /* step 7 */
261 mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
262
263 /* step 8 */
264 for (i = 0; i < db_len; i++)
265 db[i] = masked_db[i] ^ db_mask[i];
266
267 /* step 9 */
268 db[0] &= 0xff >> leftmost_bits;
269
270 /* step 10 */
271 if (db[0] != 0x01) {
272 printf("%s: invalid pss padding ", __func__);
273 printf("(leftmost byte of db isn't 0x01)\n");
274 ret = EINVAL;
275 goto out;
276 }
277
278 /* step 11 */
279 memcpy(salt, &db[1], salt_len);
280
281 /* step 12 & 13 */
282 compute_hash_prime(checksum, pad_zero, 8,
283 (uint8_t *)hash, hash_len,
284 salt, salt_len, hprime);
285
286 /* step 14 */
287 ret = memcmp(h, hprime, hash_len);
288
289out:
290 free(hprime);
291 free(salt);
292 free(db);
293 free(db_mask);
294 free(h);
295 free(masked_db);
296
297 return ret;
298}
299#endif
300
Heiko Stuebner72c46802020-06-18 16:23:22 +0200301#if CONFIG_IS_ENABLED(FIT_SIGNATURE) || CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY)
Andrew Duda3db9ff02016-11-08 18:53:40 +0000302/**
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530303 * rsa_verify_key() - Verify a signature against some data using RSA Key
Simon Glass35191a32013-06-13 15:10:02 -0700304 *
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530305 * Verify a RSA PKCS1.5 signature against an expected hash using
306 * the RSA Key properties in prop structure.
Simon Glass35191a32013-06-13 15:10:02 -0700307 *
Philippe Reynes12468352018-11-14 13:51:00 +0100308 * @info: Specifies key and FIT information
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530309 * @prop: Specifies key
310 * @sig: Signature
311 * @sig_len: Number of bytes in signature
312 * @hash: Pointer to the expected hash
Andrew Duda06ca6d62016-11-08 18:53:41 +0000313 * @key_len: Number of bytes in rsa key
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530314 * @return 0 if verified, -ve on error
Simon Glass35191a32013-06-13 15:10:02 -0700315 */
Philippe Reynes12468352018-11-14 13:51:00 +0100316static int rsa_verify_key(struct image_sign_info *info,
317 struct key_prop *prop, const uint8_t *sig,
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100318 const uint32_t sig_len, const uint8_t *hash,
Philippe Reynes12468352018-11-14 13:51:00 +0100319 const uint32_t key_len)
Simon Glass35191a32013-06-13 15:10:02 -0700320{
Simon Glass35191a32013-06-13 15:10:02 -0700321 int ret;
Ruchika Guptaa0d3ca62015-01-23 16:01:54 +0530322#if !defined(USE_HOSTCC)
323 struct udevice *mod_exp_dev;
324#endif
Philippe Reynes12468352018-11-14 13:51:00 +0100325 struct checksum_algo *checksum = info->checksum;
326 struct padding_algo *padding = info->padding;
Philippe Reynes42332272019-03-19 10:55:40 +0100327 int hash_len;
Simon Glass35191a32013-06-13 15:10:02 -0700328
Philippe Reynes12468352018-11-14 13:51:00 +0100329 if (!prop || !sig || !hash || !checksum)
Simon Glass35191a32013-06-13 15:10:02 -0700330 return -EIO;
331
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530332 if (sig_len != (prop->num_bits / 8)) {
Simon Glass35191a32013-06-13 15:10:02 -0700333 debug("Signature is of incorrect length %d\n", sig_len);
334 return -EINVAL;
335 }
336
Philippe Reynes12468352018-11-14 13:51:00 +0100337 debug("Checksum algorithm: %s", checksum->name);
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100338
Simon Glass35191a32013-06-13 15:10:02 -0700339 /* Sanity check for stack size */
340 if (sig_len > RSA_MAX_SIG_BITS / 8) {
341 debug("Signature length %u exceeds maximum %d\n", sig_len,
342 RSA_MAX_SIG_BITS / 8);
343 return -EINVAL;
344 }
345
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530346 uint8_t buf[sig_len];
Philippe Reynes42332272019-03-19 10:55:40 +0100347 hash_len = checksum->checksum_len;
Simon Glass35191a32013-06-13 15:10:02 -0700348
Ruchika Guptaa0d3ca62015-01-23 16:01:54 +0530349#if !defined(USE_HOSTCC)
350 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
351 if (ret) {
352 printf("RSA: Can't find Modular Exp implementation\n");
353 return -EINVAL;
354 }
355
356 ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
357#else
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530358 ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
Ruchika Guptaa0d3ca62015-01-23 16:01:54 +0530359#endif
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530360 if (ret) {
361 debug("Error in Modular exponentation\n");
Simon Glass35191a32013-06-13 15:10:02 -0700362 return ret;
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530363 }
Simon Glass35191a32013-06-13 15:10:02 -0700364
Philippe Reynes12468352018-11-14 13:51:00 +0100365 ret = padding->verify(info, buf, key_len, hash, hash_len);
Andrew Duda3db9ff02016-11-08 18:53:40 +0000366 if (ret) {
Philippe Reynes12468352018-11-14 13:51:00 +0100367 debug("In RSAVerify(): padding check failed!\n");
368 return ret;
Simon Glass35191a32013-06-13 15:10:02 -0700369 }
370
371 return 0;
372}
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900373#endif
Simon Glass35191a32013-06-13 15:10:02 -0700374
Heiko Stuebner72c46802020-06-18 16:23:22 +0200375#if CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY)
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900376/**
377 * rsa_verify_with_pkey() - Verify a signature against some data using
378 * only modulus and exponent as RSA key properties.
379 * @info: Specifies key information
380 * @hash: Pointer to the expected hash
381 * @sig: Signature
382 * @sig_len: Number of bytes in signature
383 *
384 * Parse a RSA public key blob in DER format pointed to in @info and fill
385 * a key_prop structure with properties of the key. Then verify a RSA PKCS1.5
386 * signature against an expected hash using the calculated properties.
387 *
388 * Return 0 if verified, -ve on error
389 */
AKASHI Takahiro30962bf2020-06-16 14:26:48 +0900390int rsa_verify_with_pkey(struct image_sign_info *info,
391 const void *hash, uint8_t *sig, uint sig_len)
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900392{
393 struct key_prop *prop;
394 int ret;
395
396 /* Public key is self-described to fill key_prop */
397 ret = rsa_gen_key_prop(info->key, info->keylen, &prop);
398 if (ret) {
399 debug("Generating necessary parameter for decoding failed\n");
400 return ret;
401 }
402
403 ret = rsa_verify_key(info, prop, sig, sig_len, hash,
404 info->crypto->key_len);
405
406 rsa_free_key_prop(prop);
407
408 return ret;
409}
410#else
AKASHI Takahiro30962bf2020-06-16 14:26:48 +0900411int rsa_verify_with_pkey(struct image_sign_info *info,
412 const void *hash, uint8_t *sig, uint sig_len)
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900413{
414 return -EACCES;
415}
416#endif
417
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900418#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530419/**
420 * rsa_verify_with_keynode() - Verify a signature against some data using
421 * information in node with prperties of RSA Key like modulus, exponent etc.
422 *
423 * Parse sign-node and fill a key_prop structure with properties of the
424 * key. Verify a RSA PKCS1.5 signature against an expected hash using
425 * the properties parsed
426 *
427 * @info: Specifies key and FIT information
428 * @hash: Pointer to the expected hash
429 * @sig: Signature
430 * @sig_len: Number of bytes in signature
431 * @node: Node having the RSA Key properties
432 * @return 0 if verified, -ve on error
433 */
Simon Glass35191a32013-06-13 15:10:02 -0700434static int rsa_verify_with_keynode(struct image_sign_info *info,
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530435 const void *hash, uint8_t *sig,
436 uint sig_len, int node)
Simon Glass35191a32013-06-13 15:10:02 -0700437{
438 const void *blob = info->fdt_blob;
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530439 struct key_prop prop;
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200440 int length;
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530441 int ret = 0;
Simon Glass35191a32013-06-13 15:10:02 -0700442
443 if (node < 0) {
444 debug("%s: Skipping invalid node", __func__);
445 return -EBADF;
446 }
Simon Glass35191a32013-06-13 15:10:02 -0700447
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530448 prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
Simon Glass35191a32013-06-13 15:10:02 -0700449
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530450 prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
Simon Glass35191a32013-06-13 15:10:02 -0700451
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530452 prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
453 if (!prop.public_exponent || length < sizeof(uint64_t))
454 prop.public_exponent = NULL;
455
456 prop.exp_len = sizeof(uint64_t);
457
458 prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
459
460 prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
461
Jan Kiszka3637b8b2020-05-07 20:36:13 +0200462 if (!prop.num_bits || !prop.modulus || !prop.rr) {
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530463 debug("%s: Missing RSA key info", __func__);
464 return -EFAULT;
Simon Glass35191a32013-06-13 15:10:02 -0700465 }
466
Philippe Reynes12468352018-11-14 13:51:00 +0100467 ret = rsa_verify_key(info, &prop, sig, sig_len, hash,
468 info->crypto->key_len);
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530469
470 return ret;
Simon Glass35191a32013-06-13 15:10:02 -0700471}
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900472#else
473static int rsa_verify_with_keynode(struct image_sign_info *info,
474 const void *hash, uint8_t *sig,
475 uint sig_len, int node)
476{
477 return -EACCES;
478}
479#endif
Simon Glass35191a32013-06-13 15:10:02 -0700480
Heiko Stuebnerb4044092020-05-22 16:20:33 +0200481int rsa_verify_hash(struct image_sign_info *info,
482 const uint8_t *hash, uint8_t *sig, uint sig_len)
Simon Glass35191a32013-06-13 15:10:02 -0700483{
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900484 int ret = -EACCES;
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100485
Heiko Stuebner72c46802020-06-18 16:23:22 +0200486 if (CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) {
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900487 /* don't rely on fdt properties */
488 ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
489
490 return ret;
491 }
492
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900493 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
494 const void *blob = info->fdt_blob;
495 int ndepth, noffset;
496 int sig_node, node;
497 char name[100];
Simon Glass35191a32013-06-13 15:10:02 -0700498
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900499 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
500 if (sig_node < 0) {
501 debug("%s: No signature node found\n", __func__);
502 return -ENOENT;
503 }
Simon Glass35191a32013-06-13 15:10:02 -0700504
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900505 /* See if we must use a particular key */
506 if (info->required_keynode != -1) {
Simon Glass35191a32013-06-13 15:10:02 -0700507 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900508 info->required_keynode);
509 return ret;
510 }
511
512 /* Look for a key that matches our hint */
513 snprintf(name, sizeof(name), "key-%s", info->keyname);
514 node = fdt_subnode_offset(blob, sig_node, name);
515 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
516 if (!ret)
517 return ret;
518
519 /* No luck, so try each of the keys in turn */
520 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node,
521 &ndepth);
522 (noffset >= 0) && (ndepth > 0);
523 noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
524 if (ndepth == 1 && noffset != node) {
525 ret = rsa_verify_with_keynode(info, hash,
526 sig, sig_len,
527 noffset);
528 if (!ret)
529 break;
530 }
Simon Glass35191a32013-06-13 15:10:02 -0700531 }
532 }
533
534 return ret;
535}
Heiko Stuebnerb4044092020-05-22 16:20:33 +0200536
537int rsa_verify(struct image_sign_info *info,
538 const struct image_region region[], int region_count,
539 uint8_t *sig, uint sig_len)
540{
541 /* Reserve memory for maximum checksum-length */
542 uint8_t hash[info->crypto->key_len];
543 int ret = -EACCES;
544
545 /*
546 * Verify that the checksum-length does not exceed the
547 * rsa-signature-length
548 */
549 if (info->checksum->checksum_len >
550 info->crypto->key_len) {
551 debug("%s: invlaid checksum-algorithm %s for %s\n",
552 __func__, info->checksum->name, info->crypto->name);
553 return -EINVAL;
554 }
555
556 /* Calculate checksum with checksum-algorithm */
557 ret = info->checksum->calculate(info->checksum->name,
558 region, region_count, hash);
559 if (ret < 0) {
560 debug("%s: Error in checksum calculation\n", __func__);
561 return -EINVAL;
562 }
563
564 return rsa_verify_hash(info, hash, sig, sig_len);
565}