1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
11 #include <asm/types.h>
12 #include <asm/byteorder.h>
13 #include <linux/errno.h>
14 #include <asm/types.h>
15 #include <asm/unaligned.h>
20 #include <fdt_support.h>
22 #include <linux/kconfig.h>
23 #include <u-boot/rsa-mod-exp.h>
24 #include <u-boot/rsa.h>
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.
35 #undef CONFIG_RSA_VERIFY_WITH_PKEY
38 /* Default public exponent for backward compatibility */
39 #define RSA_DEFAULT_PUBEXP 65537
42 * rsa_verify_padding() - Verify RSA message padding is valid
44 * Verify a RSA message's padding is consistent with PKCS1.5
45 * padding as described in the RSA PKCS#1 v2.1 standard.
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
52 static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
53 struct checksum_algo *algo)
58 /* first byte must be 0x00 */
60 /* second byte must be 0x01 */
62 /* next ff_len bytes must be 0xff */
63 ff_len = pad_len - algo->der_len - 3;
65 ret |= memcmp(msg, msg+1, ff_len-1);
67 /* next byte must be 0x00 */
69 /* next der_len bytes must match der_prefix */
70 ret |= memcmp(msg, algo->der_prefix, algo->der_len);
75 int padding_pkcs_15_verify(struct image_sign_info *info,
76 uint8_t *msg, int msg_len,
77 const uint8_t *hash, int hash_len)
79 struct checksum_algo *checksum = info->checksum;
80 int ret, pad_len = msg_len - checksum->checksum_len;
82 /* Check pkcs1.5 padding bytes. */
83 ret = rsa_verify_padding(msg, pad_len, checksum);
85 debug("In RSAVerify(): Padding check failed!\n");
90 if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
91 debug("In RSAVerify(): Hash check failed!\n");
98 #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
99 static void u32_i2osp(uint32_t val, uint8_t *buf)
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);
108 * mask_generation_function1() - generate an octet string
110 * Generate an octet string used to check rsa signature.
111 * It use an input octet string and a hash function.
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
120 static int mask_generation_function1(struct checksum_algo *checksum,
121 uint8_t *seed, int seed_len,
122 uint8_t *output, int output_len)
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;
130 memset(output, 0, output_len);
132 region[0].data = seed;
133 region[0].size = seed_len;
134 region[1].data = &buf_counter[0];
137 tmp = malloc(hash_len);
139 debug("%s: can't allocate array tmp\n", __func__);
144 while (i_output < output_len) {
145 u32_i2osp(counter, &buf_counter[0]);
147 ret = checksum->calculate(checksum->name,
148 region, region_count,
151 debug("%s: Error in checksum calculation\n", __func__);
156 while ((i_output < output_len) && (i < hash_len)) {
157 output[i_output] = tmp[i];
171 static 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,
177 struct image_region region[3];
178 int ret, region_count = 3;
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;
187 ret = checksum->calculate(checksum->name, region, region_count, hprime);
189 debug("%s: Error in checksum calculation\n", __func__);
198 * padding_pss_verify() - verify the pss padding of a signature
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
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
210 int padding_pss_verify(struct image_sign_info *info,
211 uint8_t *msg, int msg_len,
212 const uint8_t *hash, int hash_len)
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;
227 /* first, allocate everything */
228 masked_db = malloc(masked_db_len);
230 db_mask = malloc(db_mask_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__);
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__);
248 memcpy(masked_db, msg, masked_db_len);
249 memcpy(h, msg + masked_db_len, h_len);
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");
261 mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
264 for (i = 0; i < db_len; i++)
265 db[i] = masked_db[i] ^ db_mask[i];
268 db[0] &= 0xff >> leftmost_bits;
272 printf("%s: invalid pss padding ", __func__);
273 printf("(leftmost byte of db isn't 0x01)\n");
279 memcpy(salt, &db[1], salt_len);
282 compute_hash_prime(checksum, pad_zero, 8,
283 (uint8_t *)hash, hash_len,
284 salt, salt_len, hprime);
287 ret = memcmp(h, hprime, hash_len);
301 #if CONFIG_IS_ENABLED(FIT_SIGNATURE) || CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY)
303 * rsa_verify_key() - Verify a signature against some data using RSA Key
305 * Verify a RSA PKCS1.5 signature against an expected hash using
306 * the RSA Key properties in prop structure.
308 * @info: Specifies key and FIT information
309 * @prop: Specifies key
311 * @sig_len: Number of bytes in signature
312 * @hash: Pointer to the expected hash
313 * @key_len: Number of bytes in rsa key
314 * @return 0 if verified, -ve on error
316 static int rsa_verify_key(struct image_sign_info *info,
317 struct key_prop *prop, const uint8_t *sig,
318 const uint32_t sig_len, const uint8_t *hash,
319 const uint32_t key_len)
322 #if !defined(USE_HOSTCC)
323 struct udevice *mod_exp_dev;
325 struct checksum_algo *checksum = info->checksum;
326 struct padding_algo *padding = info->padding;
329 if (!prop || !sig || !hash || !checksum)
332 if (sig_len != (prop->num_bits / 8)) {
333 debug("Signature is of incorrect length %d\n", sig_len);
337 debug("Checksum algorithm: %s", checksum->name);
339 /* 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);
346 uint8_t buf[sig_len];
347 hash_len = checksum->checksum_len;
349 #if !defined(USE_HOSTCC)
350 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
352 printf("RSA: Can't find Modular Exp implementation\n");
356 ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
358 ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
361 debug("Error in Modular exponentation\n");
365 ret = padding->verify(info, buf, key_len, hash, hash_len);
367 debug("In RSAVerify(): padding check failed!\n");
375 #if CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY)
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
382 * @sig_len: Number of bytes in signature
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.
388 * Return 0 if verified, -ve on error
390 int rsa_verify_with_pkey(struct image_sign_info *info,
391 const void *hash, uint8_t *sig, uint sig_len)
393 struct key_prop *prop;
396 /* Public key is self-described to fill key_prop */
397 ret = rsa_gen_key_prop(info->key, info->keylen, &prop);
399 debug("Generating necessary parameter for decoding failed\n");
403 ret = rsa_verify_key(info, prop, sig, sig_len, hash,
404 info->crypto->key_len);
406 rsa_free_key_prop(prop);
411 int rsa_verify_with_pkey(struct image_sign_info *info,
412 const void *hash, uint8_t *sig, uint sig_len)
418 #if CONFIG_IS_ENABLED(FIT_SIGNATURE)
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.
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
427 * @info: Specifies key and FIT information
428 * @hash: Pointer to the expected hash
430 * @sig_len: Number of bytes in signature
431 * @node: Node having the RSA Key properties
432 * @return 0 if verified, -ve on error
434 static int rsa_verify_with_keynode(struct image_sign_info *info,
435 const void *hash, uint8_t *sig,
436 uint sig_len, int node)
438 const void *blob = info->fdt_blob;
439 struct key_prop prop;
445 debug("%s: Skipping invalid node", __func__);
449 algo = fdt_getprop(blob, node, "algo", NULL);
450 if (strcmp(info->name, algo))
453 prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
455 prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
457 prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
458 if (!prop.public_exponent || length < sizeof(uint64_t))
459 prop.public_exponent = NULL;
461 prop.exp_len = sizeof(uint64_t);
463 prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
465 prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
467 if (!prop.num_bits || !prop.modulus || !prop.rr) {
468 debug("%s: Missing RSA key info", __func__);
472 ret = rsa_verify_key(info, &prop, sig, sig_len, hash,
473 info->crypto->key_len);
478 static int rsa_verify_with_keynode(struct image_sign_info *info,
479 const void *hash, uint8_t *sig,
480 uint sig_len, int node)
486 int rsa_verify_hash(struct image_sign_info *info,
487 const uint8_t *hash, uint8_t *sig, uint sig_len)
491 if (CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) {
492 /* don't rely on fdt properties */
493 ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
498 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
499 const void *blob = info->fdt_blob;
504 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
506 debug("%s: No signature node found\n", __func__);
510 /* See if we must use a particular key */
511 if (info->required_keynode != -1) {
512 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
513 info->required_keynode);
517 /* Look for a key that matches our hint */
518 snprintf(name, sizeof(name), "key-%s", info->keyname);
519 node = fdt_subnode_offset(blob, sig_node, name);
520 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
524 /* No luck, so try each of the keys in turn */
525 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node,
527 (noffset >= 0) && (ndepth > 0);
528 noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
529 if (ndepth == 1 && noffset != node) {
530 ret = rsa_verify_with_keynode(info, hash,
542 int rsa_verify(struct image_sign_info *info,
543 const struct image_region region[], int region_count,
544 uint8_t *sig, uint sig_len)
546 /* Reserve memory for maximum checksum-length */
547 uint8_t hash[info->crypto->key_len];
551 * Verify that the checksum-length does not exceed the
552 * rsa-signature-length
554 if (info->checksum->checksum_len >
555 info->crypto->key_len) {
556 debug("%s: invlaid checksum-algorithm %s for %s\n",
557 __func__, info->checksum->name, info->crypto->name);
561 /* Calculate checksum with checksum-algorithm */
562 ret = info->checksum->calculate(info->checksum->name,
563 region, region_count, hash);
565 debug("%s: Error in checksum calculation\n", __func__);
569 return rsa_verify_hash(info, hash, sig, sig_len);