blob: 9605c376390a9ef8ba00e39aac487208200a7eb1 [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.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010050 * Return: 0 on success, != 0 on failure
Andrew Duda3db9ff02016-11-08 18:53:40 +000051 */
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,
SESA6444251304b462022-03-09 01:27:15 -080076 const uint8_t *msg, int msg_len,
Philippe Reynes12468352018-11-14 13:51:00 +010077 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
Simon Glass101d3492021-11-12 12:28:02 -070082 /* Check pkcs1.5 padding bytes */
Philippe Reynes12468352018-11-14 13:51:00 +010083 ret = rsa_verify_padding(msg, pad_len, checksum);
84 if (ret) {
85 debug("In RSAVerify(): Padding check failed!\n");
86 return -EINVAL;
87 }
88
Simon Glass101d3492021-11-12 12:28:02 -070089 /* Check hash */
Philippe Reynes12468352018-11-14 13:51:00 +010090 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
Alexandru Gagniucf3b5e582021-08-18 17:49:02 -050098#ifndef USE_HOSTCC
99U_BOOT_PADDING_ALGO(pkcs_15) = {
100 .name = "pkcs-1.5",
101 .verify = padding_pkcs_15_verify,
102};
103#endif
104
Simon Glasseffa4522021-09-25 19:43:23 -0600105#if CONFIG_IS_ENABLED(FIT_RSASSA_PSS)
Philippe Reynes47d73f02018-11-14 13:51:01 +0100106static void u32_i2osp(uint32_t val, uint8_t *buf)
107{
108 buf[0] = (uint8_t)((val >> 24) & 0xff);
109 buf[1] = (uint8_t)((val >> 16) & 0xff);
110 buf[2] = (uint8_t)((val >> 8) & 0xff);
111 buf[3] = (uint8_t)((val >> 0) & 0xff);
112}
113
114/**
115 * mask_generation_function1() - generate an octet string
116 *
117 * Generate an octet string used to check rsa signature.
118 * It use an input octet string and a hash function.
119 *
120 * @checksum: A Hash function
121 * @seed: Specifies an input variable octet string
122 * @seed_len: Size of the input octet string
123 * @output: Specifies the output octet string
124 * @output_len: Size of the output octet string
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100125 * Return: 0 if the octet string was correctly generated, others on error
Philippe Reynes47d73f02018-11-14 13:51:01 +0100126 */
127static int mask_generation_function1(struct checksum_algo *checksum,
SESA6444251304b462022-03-09 01:27:15 -0800128 const uint8_t *seed, int seed_len,
Philippe Reynes47d73f02018-11-14 13:51:01 +0100129 uint8_t *output, int output_len)
130{
131 struct image_region region[2];
132 int ret = 0, i, i_output = 0, region_count = 2;
133 uint32_t counter = 0;
134 uint8_t buf_counter[4], *tmp;
135 int hash_len = checksum->checksum_len;
136
137 memset(output, 0, output_len);
138
139 region[0].data = seed;
140 region[0].size = seed_len;
141 region[1].data = &buf_counter[0];
142 region[1].size = 4;
143
144 tmp = malloc(hash_len);
145 if (!tmp) {
146 debug("%s: can't allocate array tmp\n", __func__);
147 ret = -ENOMEM;
148 goto out;
149 }
150
151 while (i_output < output_len) {
152 u32_i2osp(counter, &buf_counter[0]);
153
154 ret = checksum->calculate(checksum->name,
155 region, region_count,
156 tmp);
157 if (ret < 0) {
158 debug("%s: Error in checksum calculation\n", __func__);
159 goto out;
160 }
161
162 i = 0;
163 while ((i_output < output_len) && (i < hash_len)) {
164 output[i_output] = tmp[i];
165 i_output++;
166 i++;
167 }
168
169 counter++;
170 }
171
172out:
173 free(tmp);
174
175 return ret;
176}
177
178static int compute_hash_prime(struct checksum_algo *checksum,
SESA6444251304b462022-03-09 01:27:15 -0800179 const uint8_t *pad, int pad_len,
180 const uint8_t *hash, int hash_len,
181 const uint8_t *salt, int salt_len,
Philippe Reynes47d73f02018-11-14 13:51:01 +0100182 uint8_t *hprime)
183{
184 struct image_region region[3];
185 int ret, region_count = 3;
186
187 region[0].data = pad;
188 region[0].size = pad_len;
189 region[1].data = hash;
190 region[1].size = hash_len;
191 region[2].data = salt;
192 region[2].size = salt_len;
193
194 ret = checksum->calculate(checksum->name, region, region_count, hprime);
195 if (ret < 0) {
196 debug("%s: Error in checksum calculation\n", __func__);
197 goto out;
198 }
199
200out:
201 return ret;
202}
203
Heiko Stuebner1944dc12020-06-18 16:23:26 +0200204/*
205 * padding_pss_verify() - verify the pss padding of a signature
206 *
SESA64442538945ac2022-03-09 01:27:17 -0800207 * Works with any salt length
Heiko Stuebner1944dc12020-06-18 16:23:26 +0200208 *
SESA644425732d2702022-03-09 01:27:16 -0800209 * msg is a concatenation of : masked_db + h + 0xbc
210 * Once unmasked, db is a concatenation of : [0x00]* + 0x01 + salt
211 * Length of 0-padding at begin of db depends on salt length.
212 *
Heiko Stuebner1944dc12020-06-18 16:23:26 +0200213 * @info: Specifies key and FIT information
214 * @msg: byte array of message, len equal to msg_len
215 * @msg_len: Message length
216 * @hash: Pointer to the expected hash
217 * @hash_len: Length of the hash
Heinrich Schuchardtdf5117a2022-08-31 21:13:40 +0200218 *
219 * Return: 0 if padding is correct, non-zero otherwise
Heiko Stuebner1944dc12020-06-18 16:23:26 +0200220 */
Philippe Reynes47d73f02018-11-14 13:51:01 +0100221int padding_pss_verify(struct image_sign_info *info,
SESA6444251304b462022-03-09 01:27:15 -0800222 const uint8_t *msg, int msg_len,
Philippe Reynes47d73f02018-11-14 13:51:01 +0100223 const uint8_t *hash, int hash_len)
224{
SESA644425732d2702022-03-09 01:27:16 -0800225 const uint8_t *masked_db = NULL;
Philippe Reynes47d73f02018-11-14 13:51:01 +0100226 uint8_t *db_mask = NULL;
SESA644425732d2702022-03-09 01:27:16 -0800227 uint8_t *db = NULL;
228 int db_len = msg_len - hash_len - 1;
229 const uint8_t *h = NULL;
230 uint8_t *hprime = NULL;
231 int h_len = hash_len;
SESA64442538945ac2022-03-09 01:27:17 -0800232 uint8_t *db_nopad = NULL, *salt = NULL;
233 int db_padlen, salt_len;
Philippe Reynes47d73f02018-11-14 13:51:01 +0100234 uint8_t pad_zero[8] = { 0 };
235 int ret, i, leftmost_bits = 1;
236 uint8_t leftmost_mask;
237 struct checksum_algo *checksum = info->checksum;
238
Heinrich Schuchardtdf5117a2022-08-31 21:13:40 +0200239 if (db_len <= 0)
240 return -EINVAL;
241
Philippe Reynes47d73f02018-11-14 13:51:01 +0100242 /* first, allocate everything */
SESA644425732d2702022-03-09 01:27:16 -0800243 db_mask = malloc(db_len);
Philippe Reynes47d73f02018-11-14 13:51:01 +0100244 db = malloc(db_len);
Philippe Reynes47d73f02018-11-14 13:51:01 +0100245 hprime = malloc(hash_len);
SESA644425732d2702022-03-09 01:27:16 -0800246 if (!db_mask || !db || !hprime) {
Philippe Reynes47d73f02018-11-14 13:51:01 +0100247 printf("%s: can't allocate some buffer\n", __func__);
248 ret = -ENOMEM;
249 goto out;
250 }
251
252 /* step 4: check if the last byte is 0xbc */
253 if (msg[msg_len - 1] != 0xbc) {
254 printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
255 ret = -EINVAL;
256 goto out;
257 }
258
259 /* step 5 */
SESA644425732d2702022-03-09 01:27:16 -0800260 masked_db = &msg[0];
261 h = &msg[db_len];
Philippe Reynes47d73f02018-11-14 13:51:01 +0100262
263 /* step 6 */
264 leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
265 if (masked_db[0] & leftmost_mask) {
266 printf("%s: invalid pss padding ", __func__);
267 printf("(leftmost bit of maskedDB not zero)\n");
268 ret = -EINVAL;
269 goto out;
270 }
271
272 /* step 7 */
SESA644425732d2702022-03-09 01:27:16 -0800273 mask_generation_function1(checksum, h, h_len, db_mask, db_len);
Philippe Reynes47d73f02018-11-14 13:51:01 +0100274
275 /* step 8 */
276 for (i = 0; i < db_len; i++)
277 db[i] = masked_db[i] ^ db_mask[i];
278
279 /* step 9 */
280 db[0] &= 0xff >> leftmost_bits;
281
282 /* step 10 */
SESA64442538945ac2022-03-09 01:27:17 -0800283 db_padlen = 0;
284 while (db[db_padlen] == 0x00 && db_padlen < (db_len - 1))
285 db_padlen++;
286 db_nopad = &db[db_padlen];
287 if (db_nopad[0] != 0x01) {
Philippe Reynes47d73f02018-11-14 13:51:01 +0100288 printf("%s: invalid pss padding ", __func__);
SESA64442538945ac2022-03-09 01:27:17 -0800289 printf("(leftmost byte of db after 0-padding isn't 0x01)\n");
Philippe Reynes47d73f02018-11-14 13:51:01 +0100290 ret = EINVAL;
291 goto out;
292 }
293
294 /* step 11 */
SESA64442538945ac2022-03-09 01:27:17 -0800295 salt_len = db_len - db_padlen - 1;
296 salt = &db_nopad[1];
Philippe Reynes47d73f02018-11-14 13:51:01 +0100297
298 /* step 12 & 13 */
299 compute_hash_prime(checksum, pad_zero, 8,
SESA6444251304b462022-03-09 01:27:15 -0800300 hash, hash_len,
Philippe Reynes47d73f02018-11-14 13:51:01 +0100301 salt, salt_len, hprime);
302
303 /* step 14 */
304 ret = memcmp(h, hprime, hash_len);
305
306out:
307 free(hprime);
Philippe Reynes47d73f02018-11-14 13:51:01 +0100308 free(db);
309 free(db_mask);
Philippe Reynes47d73f02018-11-14 13:51:01 +0100310
311 return ret;
312}
Alexandru Gagniucf3b5e582021-08-18 17:49:02 -0500313
314#ifndef USE_HOSTCC
315U_BOOT_PADDING_ALGO(pss) = {
316 .name = "pss",
317 .verify = padding_pss_verify,
318};
319#endif
320
Philippe Reynes47d73f02018-11-14 13:51:01 +0100321#endif
322
Andrew Duda3db9ff02016-11-08 18:53:40 +0000323/**
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530324 * rsa_verify_key() - Verify a signature against some data using RSA Key
Simon Glass35191a32013-06-13 15:10:02 -0700325 *
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530326 * Verify a RSA PKCS1.5 signature against an expected hash using
327 * the RSA Key properties in prop structure.
Simon Glass35191a32013-06-13 15:10:02 -0700328 *
Philippe Reynes12468352018-11-14 13:51:00 +0100329 * @info: Specifies key and FIT information
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530330 * @prop: Specifies key
331 * @sig: Signature
332 * @sig_len: Number of bytes in signature
333 * @hash: Pointer to the expected hash
Andrew Duda06ca6d62016-11-08 18:53:41 +0000334 * @key_len: Number of bytes in rsa key
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100335 * Return: 0 if verified, -ve on error
Simon Glass35191a32013-06-13 15:10:02 -0700336 */
Philippe Reynes12468352018-11-14 13:51:00 +0100337static int rsa_verify_key(struct image_sign_info *info,
338 struct key_prop *prop, const uint8_t *sig,
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100339 const uint32_t sig_len, const uint8_t *hash,
Philippe Reynes12468352018-11-14 13:51:00 +0100340 const uint32_t key_len)
Simon Glass35191a32013-06-13 15:10:02 -0700341{
Simon Glass35191a32013-06-13 15:10:02 -0700342 int ret;
Ruchika Guptaa0d3ca62015-01-23 16:01:54 +0530343#if !defined(USE_HOSTCC)
344 struct udevice *mod_exp_dev;
345#endif
Philippe Reynes12468352018-11-14 13:51:00 +0100346 struct checksum_algo *checksum = info->checksum;
347 struct padding_algo *padding = info->padding;
Philippe Reynes42332272019-03-19 10:55:40 +0100348 int hash_len;
Simon Glass35191a32013-06-13 15:10:02 -0700349
Philippe Reynesfd40a9b2021-10-15 11:28:47 +0200350 if (!prop || !sig || !hash || !checksum || !padding)
Simon Glass35191a32013-06-13 15:10:02 -0700351 return -EIO;
352
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530353 if (sig_len != (prop->num_bits / 8)) {
Simon Glass35191a32013-06-13 15:10:02 -0700354 debug("Signature is of incorrect length %d\n", sig_len);
355 return -EINVAL;
356 }
357
Philippe Reynes12468352018-11-14 13:51:00 +0100358 debug("Checksum algorithm: %s", checksum->name);
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100359
Simon Glass35191a32013-06-13 15:10:02 -0700360 /* Sanity check for stack size */
361 if (sig_len > RSA_MAX_SIG_BITS / 8) {
362 debug("Signature length %u exceeds maximum %d\n", sig_len,
363 RSA_MAX_SIG_BITS / 8);
364 return -EINVAL;
365 }
366
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530367 uint8_t buf[sig_len];
Philippe Reynes42332272019-03-19 10:55:40 +0100368 hash_len = checksum->checksum_len;
Simon Glass35191a32013-06-13 15:10:02 -0700369
Ruchika Guptaa0d3ca62015-01-23 16:01:54 +0530370#if !defined(USE_HOSTCC)
371 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
372 if (ret) {
373 printf("RSA: Can't find Modular Exp implementation\n");
374 return -EINVAL;
375 }
376
377 ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
378#else
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530379 ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
Ruchika Guptaa0d3ca62015-01-23 16:01:54 +0530380#endif
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530381 if (ret) {
382 debug("Error in Modular exponentation\n");
Simon Glass35191a32013-06-13 15:10:02 -0700383 return ret;
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530384 }
Simon Glass35191a32013-06-13 15:10:02 -0700385
Philippe Reynes12468352018-11-14 13:51:00 +0100386 ret = padding->verify(info, buf, key_len, hash, hash_len);
Andrew Duda3db9ff02016-11-08 18:53:40 +0000387 if (ret) {
Philippe Reynes12468352018-11-14 13:51:00 +0100388 debug("In RSAVerify(): padding check failed!\n");
389 return ret;
Simon Glass35191a32013-06-13 15:10:02 -0700390 }
391
392 return 0;
393}
394
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900395/**
396 * rsa_verify_with_pkey() - Verify a signature against some data using
397 * only modulus and exponent as RSA key properties.
398 * @info: Specifies key information
399 * @hash: Pointer to the expected hash
400 * @sig: Signature
401 * @sig_len: Number of bytes in signature
402 *
403 * Parse a RSA public key blob in DER format pointed to in @info and fill
404 * a key_prop structure with properties of the key. Then verify a RSA PKCS1.5
405 * signature against an expected hash using the calculated properties.
406 *
407 * Return 0 if verified, -ve on error
408 */
AKASHI Takahiro30962bf2020-06-16 14:26:48 +0900409int rsa_verify_with_pkey(struct image_sign_info *info,
410 const void *hash, uint8_t *sig, uint sig_len)
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900411{
412 struct key_prop *prop;
413 int ret;
414
Simon Glasseffa4522021-09-25 19:43:23 -0600415 if (!CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY))
416 return -EACCES;
417
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900418 /* Public key is self-described to fill key_prop */
419 ret = rsa_gen_key_prop(info->key, info->keylen, &prop);
420 if (ret) {
421 debug("Generating necessary parameter for decoding failed\n");
422 return ret;
423 }
424
425 ret = rsa_verify_key(info, prop, sig, sig_len, hash,
426 info->crypto->key_len);
427
428 rsa_free_key_prop(prop);
429
430 return ret;
431}
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900432
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900433#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530434/**
435 * rsa_verify_with_keynode() - Verify a signature against some data using
436 * information in node with prperties of RSA Key like modulus, exponent etc.
437 *
438 * Parse sign-node and fill a key_prop structure with properties of the
439 * key. Verify a RSA PKCS1.5 signature against an expected hash using
440 * the properties parsed
441 *
442 * @info: Specifies key and FIT information
443 * @hash: Pointer to the expected hash
444 * @sig: Signature
445 * @sig_len: Number of bytes in signature
446 * @node: Node having the RSA Key properties
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100447 * Return: 0 if verified, -ve on error
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530448 */
Simon Glass35191a32013-06-13 15:10:02 -0700449static int rsa_verify_with_keynode(struct image_sign_info *info,
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530450 const void *hash, uint8_t *sig,
451 uint sig_len, int node)
Simon Glass35191a32013-06-13 15:10:02 -0700452{
453 const void *blob = info->fdt_blob;
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530454 struct key_prop prop;
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200455 int length;
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530456 int ret = 0;
Matthieu CASTET335b01c2020-09-23 19:11:44 +0200457 const char *algo;
Simon Glass35191a32013-06-13 15:10:02 -0700458
459 if (node < 0) {
460 debug("%s: Skipping invalid node", __func__);
461 return -EBADF;
462 }
Simon Glass35191a32013-06-13 15:10:02 -0700463
Matthieu CASTET335b01c2020-09-23 19:11:44 +0200464 algo = fdt_getprop(blob, node, "algo", NULL);
Sean Andersonb6710802021-02-16 11:40:15 -0500465 if (strcmp(info->name, algo)) {
466 debug("%s: Wrong algo: have %s, expected %s", __func__,
467 info->name, algo);
Matthieu CASTET335b01c2020-09-23 19:11:44 +0200468 return -EFAULT;
Sean Andersonb6710802021-02-16 11:40:15 -0500469 }
Matthieu CASTET335b01c2020-09-23 19:11:44 +0200470
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530471 prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
Simon Glass35191a32013-06-13 15:10:02 -0700472
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530473 prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
Simon Glass35191a32013-06-13 15:10:02 -0700474
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530475 prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
476 if (!prop.public_exponent || length < sizeof(uint64_t))
477 prop.public_exponent = NULL;
478
479 prop.exp_len = sizeof(uint64_t);
480
481 prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
482
483 prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
484
Jan Kiszka3637b8b2020-05-07 20:36:13 +0200485 if (!prop.num_bits || !prop.modulus || !prop.rr) {
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530486 debug("%s: Missing RSA key info", __func__);
487 return -EFAULT;
Simon Glass35191a32013-06-13 15:10:02 -0700488 }
489
Philippe Reynes12468352018-11-14 13:51:00 +0100490 ret = rsa_verify_key(info, &prop, sig, sig_len, hash,
491 info->crypto->key_len);
Ruchika Guptab92ebab2015-01-23 16:01:50 +0530492
493 return ret;
Simon Glass35191a32013-06-13 15:10:02 -0700494}
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900495#else
496static int rsa_verify_with_keynode(struct image_sign_info *info,
497 const void *hash, uint8_t *sig,
498 uint sig_len, int node)
499{
500 return -EACCES;
501}
502#endif
Simon Glass35191a32013-06-13 15:10:02 -0700503
Heiko Stuebnerb4044092020-05-22 16:20:33 +0200504int rsa_verify_hash(struct image_sign_info *info,
505 const uint8_t *hash, uint8_t *sig, uint sig_len)
Simon Glass35191a32013-06-13 15:10:02 -0700506{
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900507 int ret = -EACCES;
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100508
Heiko Stuebner72c46802020-06-18 16:23:22 +0200509 if (CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) {
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900510 /* don't rely on fdt properties */
511 ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
Simon Glass101d3492021-11-12 12:28:02 -0700512 if (ret)
513 debug("%s: rsa_verify_with_pkey() failed\n", __func__);
AKASHI Takahiro75822f42020-02-21 15:12:59 +0900514 return ret;
515 }
516
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900517 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
518 const void *blob = info->fdt_blob;
519 int ndepth, noffset;
520 int sig_node, node;
521 char name[100];
Simon Glass35191a32013-06-13 15:10:02 -0700522
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900523 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
524 if (sig_node < 0) {
525 debug("%s: No signature node found\n", __func__);
526 return -ENOENT;
527 }
Simon Glass35191a32013-06-13 15:10:02 -0700528
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900529 /* See if we must use a particular key */
530 if (info->required_keynode != -1) {
Simon Glass35191a32013-06-13 15:10:02 -0700531 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900532 info->required_keynode);
Simon Glass101d3492021-11-12 12:28:02 -0700533 if (ret)
534 debug("%s: Failed to verify required_keynode\n",
535 __func__);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900536 return ret;
537 }
538
539 /* Look for a key that matches our hint */
540 snprintf(name, sizeof(name), "key-%s", info->keyname);
541 node = fdt_subnode_offset(blob, sig_node, name);
542 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
543 if (!ret)
544 return ret;
Simon Glass101d3492021-11-12 12:28:02 -0700545 debug("%s: Could not verify key '%s', trying all\n", __func__,
546 name);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900547
548 /* No luck, so try each of the keys in turn */
Philippe Reynes7258c3f2021-01-12 19:18:54 +0100549 for (ndepth = 0, noffset = fdt_next_node(blob, sig_node,
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900550 &ndepth);
551 (noffset >= 0) && (ndepth > 0);
Philippe Reynes7258c3f2021-01-12 19:18:54 +0100552 noffset = fdt_next_node(blob, noffset, &ndepth)) {
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900553 if (ndepth == 1 && noffset != node) {
554 ret = rsa_verify_with_keynode(info, hash,
555 sig, sig_len,
556 noffset);
557 if (!ret)
558 break;
559 }
Simon Glass35191a32013-06-13 15:10:02 -0700560 }
561 }
Simon Glass101d3492021-11-12 12:28:02 -0700562 debug("%s: Failed to verify by any means\n", __func__);
Simon Glass35191a32013-06-13 15:10:02 -0700563
564 return ret;
565}
Heiko Stuebnerb4044092020-05-22 16:20:33 +0200566
567int rsa_verify(struct image_sign_info *info,
568 const struct image_region region[], int region_count,
569 uint8_t *sig, uint sig_len)
570{
571 /* Reserve memory for maximum checksum-length */
572 uint8_t hash[info->crypto->key_len];
Heinrich Schuchardt574fab92020-10-08 20:53:13 +0200573 int ret;
Heiko Stuebnerb4044092020-05-22 16:20:33 +0200574
575 /*
576 * Verify that the checksum-length does not exceed the
577 * rsa-signature-length
578 */
579 if (info->checksum->checksum_len >
580 info->crypto->key_len) {
Thomas Perrot4e1e2a22021-07-19 16:04:44 +0200581 debug("%s: invalid checksum-algorithm %s for %s\n",
Heiko Stuebnerb4044092020-05-22 16:20:33 +0200582 __func__, info->checksum->name, info->crypto->name);
583 return -EINVAL;
584 }
585
586 /* Calculate checksum with checksum-algorithm */
587 ret = info->checksum->calculate(info->checksum->name,
588 region, region_count, hash);
589 if (ret < 0) {
590 debug("%s: Error in checksum calculation\n", __func__);
591 return -EINVAL;
592 }
593
594 return rsa_verify_hash(info, hash, sig, sig_len);
595}
Alexandru Gagniuc92c5fdc2021-07-14 17:05:40 -0500596
597#ifndef USE_HOSTCC
598
599U_BOOT_CRYPTO_ALGO(rsa2048) = {
600 .name = "rsa2048",
601 .key_len = RSA2048_BYTES,
602 .verify = rsa_verify,
603};
604
Jamin Lin5975ad72022-01-19 16:23:21 +0800605U_BOOT_CRYPTO_ALGO(rsa3072) = {
606 .name = "rsa3072",
607 .key_len = RSA3072_BYTES,
608 .verify = rsa_verify,
609};
610
Alexandru Gagniuc92c5fdc2021-07-14 17:05:40 -0500611U_BOOT_CRYPTO_ALGO(rsa4096) = {
612 .name = "rsa4096",
613 .key_len = RSA4096_BYTES,
614 .verify = rsa_verify,
615};
616
617#endif