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 <linux/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_padding() - Verify RSA message padding is valid
30 * Verify a RSA message's padding is consistent with PKCS1.5
31 * padding as described in the RSA PKCS#1 v2.1 standard.
33 * @msg: Padded message
34 * @pad_len: Number of expected padding bytes
35 * @algo: Checksum algo structure having information on DER encoding etc.
36 * @return 0 on success, != 0 on failure
38 static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
39 struct checksum_algo *algo)
44 /* first byte must be 0x00 */
46 /* second byte must be 0x01 */
48 /* next ff_len bytes must be 0xff */
49 ff_len = pad_len - algo->der_len - 3;
51 ret |= memcmp(msg, msg+1, ff_len-1);
53 /* next byte must be 0x00 */
55 /* next der_len bytes must match der_prefix */
56 ret |= memcmp(msg, algo->der_prefix, algo->der_len);
62 * rsa_verify_key() - Verify a signature against some data using RSA Key
64 * Verify a RSA PKCS1.5 signature against an expected hash using
65 * the RSA Key properties in prop structure.
67 * @prop: Specifies key
69 * @sig_len: Number of bytes in signature
70 * @hash: Pointer to the expected hash
71 * @key_len: Number of bytes in rsa key
72 * @algo: Checksum algo structure having information on DER encoding etc.
73 * @return 0 if verified, -ve on error
75 static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
76 const uint32_t sig_len, const uint8_t *hash,
77 const uint32_t key_len, struct checksum_algo *algo)
81 #if !defined(USE_HOSTCC)
82 struct udevice *mod_exp_dev;
85 if (!prop || !sig || !hash || !algo)
88 if (sig_len != (prop->num_bits / 8)) {
89 debug("Signature is of incorrect length %d\n", sig_len);
93 debug("Checksum algorithm: %s", algo->name);
95 /* Sanity check for stack size */
96 if (sig_len > RSA_MAX_SIG_BITS / 8) {
97 debug("Signature length %u exceeds maximum %d\n", sig_len,
98 RSA_MAX_SIG_BITS / 8);
102 uint8_t buf[sig_len];
104 #if !defined(USE_HOSTCC)
105 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
107 printf("RSA: Can't find Modular Exp implementation\n");
111 ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
113 ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
116 debug("Error in Modular exponentation\n");
120 pad_len = key_len - algo->checksum_len;
122 /* Check pkcs1.5 padding bytes. */
123 ret = rsa_verify_padding(buf, pad_len, algo);
125 debug("In RSAVerify(): Padding check failed!\n");
130 if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
131 debug("In RSAVerify(): Hash check failed!\n");
139 * rsa_verify_with_keynode() - Verify a signature against some data using
140 * information in node with prperties of RSA Key like modulus, exponent etc.
142 * Parse sign-node and fill a key_prop structure with properties of the
143 * key. Verify a RSA PKCS1.5 signature against an expected hash using
144 * the properties parsed
146 * @info: Specifies key and FIT information
147 * @hash: Pointer to the expected hash
149 * @sig_len: Number of bytes in signature
150 * @node: Node having the RSA Key properties
151 * @return 0 if verified, -ve on error
153 static int rsa_verify_with_keynode(struct image_sign_info *info,
154 const void *hash, uint8_t *sig,
155 uint sig_len, int node)
157 const void *blob = info->fdt_blob;
158 struct key_prop prop;
163 debug("%s: Skipping invalid node", __func__);
167 prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
169 prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
171 prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
172 if (!prop.public_exponent || length < sizeof(uint64_t))
173 prop.public_exponent = NULL;
175 prop.exp_len = sizeof(uint64_t);
177 prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
179 prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
181 if (!prop.num_bits || !prop.modulus) {
182 debug("%s: Missing RSA key info", __func__);
186 ret = rsa_verify_key(&prop, sig, sig_len, hash,
187 info->crypto->key_len, info->checksum);
192 int rsa_verify(struct image_sign_info *info,
193 const struct image_region region[], int region_count,
194 uint8_t *sig, uint sig_len)
196 const void *blob = info->fdt_blob;
197 /* Reserve memory for maximum checksum-length */
198 uint8_t hash[info->crypto->key_len];
205 * Verify that the checksum-length does not exceed the
206 * rsa-signature-length
208 if (info->checksum->checksum_len >
209 info->crypto->key_len) {
210 debug("%s: invlaid checksum-algorithm %s for %s\n",
211 __func__, info->checksum->name, info->crypto->name);
215 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
217 debug("%s: No signature node found\n", __func__);
221 /* Calculate checksum with checksum-algorithm */
222 ret = info->checksum->calculate(info->checksum->name,
223 region, region_count, hash);
225 debug("%s: Error in checksum calculation\n", __func__);
229 /* See if we must use a particular key */
230 if (info->required_keynode != -1) {
231 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
232 info->required_keynode);
237 /* Look for a key that matches our hint */
238 snprintf(name, sizeof(name), "key-%s", info->keyname);
239 node = fdt_subnode_offset(blob, sig_node, name);
240 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
244 /* No luck, so try each of the keys in turn */
245 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
246 (noffset >= 0) && (ndepth > 0);
247 noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
248 if (ndepth == 1 && noffset != node) {
249 ret = rsa_verify_with_keynode(info, hash, sig, sig_len,