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>
19 #include <fdt_support.h>
21 #include <u-boot/rsa-mod-exp.h>
22 #include <u-boot/rsa.h>
24 /* Default public exponent for backward compatibility */
25 #define RSA_DEFAULT_PUBEXP 65537
28 * rsa_verify_key() - Verify a signature against some data using RSA Key
30 * Verify a RSA PKCS1.5 signature against an expected hash using
31 * the RSA Key properties in prop structure.
33 * @prop: Specifies key
35 * @sig_len: Number of bytes in signature
36 * @hash: Pointer to the expected hash
37 * @algo: Checksum algo structure having information on RSA padding etc.
38 * @return 0 if verified, -ve on error
40 static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
41 const uint32_t sig_len, const uint8_t *hash,
42 struct checksum_algo *algo)
44 const uint8_t *padding;
47 #if !defined(USE_HOSTCC)
48 struct udevice *mod_exp_dev;
51 if (!prop || !sig || !hash || !algo)
54 if (sig_len != (prop->num_bits / 8)) {
55 debug("Signature is of incorrect length %d\n", sig_len);
59 debug("Checksum algorithm: %s", algo->name);
61 /* Sanity check for stack size */
62 if (sig_len > RSA_MAX_SIG_BITS / 8) {
63 debug("Signature length %u exceeds maximum %d\n", sig_len,
64 RSA_MAX_SIG_BITS / 8);
70 #if !defined(USE_HOSTCC)
71 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
73 printf("RSA: Can't find Modular Exp implementation\n");
77 ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
79 ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
82 debug("Error in Modular exponentation\n");
86 padding = algo->rsa_padding;
87 pad_len = algo->pad_len - algo->checksum_len;
89 /* Check pkcs1.5 padding bytes. */
90 if (memcmp(buf, padding, pad_len)) {
91 debug("In RSAVerify(): Padding check failed!\n");
96 if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
97 debug("In RSAVerify(): Hash check failed!\n");
105 * rsa_verify_with_keynode() - Verify a signature against some data using
106 * information in node with prperties of RSA Key like modulus, exponent etc.
108 * Parse sign-node and fill a key_prop structure with properties of the
109 * key. Verify a RSA PKCS1.5 signature against an expected hash using
110 * the properties parsed
112 * @info: Specifies key and FIT information
113 * @hash: Pointer to the expected hash
115 * @sig_len: Number of bytes in signature
116 * @node: Node having the RSA Key properties
117 * @return 0 if verified, -ve on error
119 static int rsa_verify_with_keynode(struct image_sign_info *info,
120 const void *hash, uint8_t *sig,
121 uint sig_len, int node)
123 const void *blob = info->fdt_blob;
124 struct key_prop prop;
129 debug("%s: Skipping invalid node", __func__);
133 prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
135 prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
137 prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
138 if (!prop.public_exponent || length < sizeof(uint64_t))
139 prop.public_exponent = NULL;
141 prop.exp_len = sizeof(uint64_t);
143 prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
145 prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
147 if (!prop.num_bits || !prop.modulus) {
148 debug("%s: Missing RSA key info", __func__);
152 ret = rsa_verify_key(&prop, sig, sig_len, hash, info->algo->checksum);
157 int rsa_verify(struct image_sign_info *info,
158 const struct image_region region[], int region_count,
159 uint8_t *sig, uint sig_len)
161 const void *blob = info->fdt_blob;
162 /* Reserve memory for maximum checksum-length */
163 uint8_t hash[info->algo->checksum->pad_len];
170 * Verify that the checksum-length does not exceed the
171 * rsa-signature-length
173 if (info->algo->checksum->checksum_len >
174 info->algo->checksum->pad_len) {
175 debug("%s: invlaid checksum-algorithm %s for %s\n",
176 __func__, info->algo->checksum->name, info->algo->name);
180 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
182 debug("%s: No signature node found\n", __func__);
186 /* Calculate checksum with checksum-algorithm */
187 info->algo->checksum->calculate(region, region_count, hash);
189 /* See if we must use a particular key */
190 if (info->required_keynode != -1) {
191 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
192 info->required_keynode);
197 /* Look for a key that matches our hint */
198 snprintf(name, sizeof(name), "key-%s", info->keyname);
199 node = fdt_subnode_offset(blob, sig_node, name);
200 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
204 /* No luck, so try each of the keys in turn */
205 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
206 (noffset >= 0) && (ndepth > 0);
207 noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
208 if (ndepth == 1 && noffset != node) {
209 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,