xref: /rk3399_rockchip-uboot/lib/rsa/rsa-verify.c (revision 0c1d74fda7c0063eeca4d8d9fa8674e6ec2ef685)
119c402afSSimon Glass /*
219c402afSSimon Glass  * Copyright (c) 2013, Google Inc.
319c402afSSimon Glass  *
41a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
519c402afSSimon Glass  */
619c402afSSimon Glass 
729a23f9dSHeiko Schocher #ifndef USE_HOSTCC
819c402afSSimon Glass #include <common.h>
919c402afSSimon Glass #include <fdtdec.h>
1029a23f9dSHeiko Schocher #include <asm/types.h>
1129a23f9dSHeiko Schocher #include <asm/byteorder.h>
121221ce45SMasahiro Yamada #include <linux/errno.h>
1329a23f9dSHeiko Schocher #include <asm/types.h>
1429a23f9dSHeiko Schocher #include <asm/unaligned.h>
15c937ff6dSRuchika Gupta #include <dm.h>
1629a23f9dSHeiko Schocher #else
1729a23f9dSHeiko Schocher #include "fdt_host.h"
1829a23f9dSHeiko Schocher #include "mkimage.h"
1929a23f9dSHeiko Schocher #include <fdt_support.h>
2029a23f9dSHeiko Schocher #endif
21fc2f4246SRuchika Gupta #include <u-boot/rsa-mod-exp.h>
222b9912e6SJeroen Hofstee #include <u-boot/rsa.h>
2329a23f9dSHeiko Schocher 
24e0f2f155SMichael van der Westhuizen /* Default public exponent for backward compatibility */
25e0f2f155SMichael van der Westhuizen #define RSA_DEFAULT_PUBEXP	65537
26e0f2f155SMichael van der Westhuizen 
2719c402afSSimon Glass /**
28da29f299SAndrew Duda  * rsa_verify_padding() - Verify RSA message padding is valid
29da29f299SAndrew Duda  *
30da29f299SAndrew Duda  * Verify a RSA message's padding is consistent with PKCS1.5
31da29f299SAndrew Duda  * padding as described in the RSA PKCS#1 v2.1 standard.
32da29f299SAndrew Duda  *
33da29f299SAndrew Duda  * @msg:	Padded message
34da29f299SAndrew Duda  * @pad_len:	Number of expected padding bytes
35da29f299SAndrew Duda  * @algo:	Checksum algo structure having information on DER encoding etc.
36da29f299SAndrew Duda  * @return 0 on success, != 0 on failure
37da29f299SAndrew Duda  */
38da29f299SAndrew Duda static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
39da29f299SAndrew Duda 			      struct checksum_algo *algo)
40da29f299SAndrew Duda {
41da29f299SAndrew Duda 	int ff_len;
42da29f299SAndrew Duda 	int ret;
43da29f299SAndrew Duda 
44da29f299SAndrew Duda 	/* first byte must be 0x00 */
45da29f299SAndrew Duda 	ret = *msg++;
46da29f299SAndrew Duda 	/* second byte must be 0x01 */
47da29f299SAndrew Duda 	ret |= *msg++ ^ 0x01;
48da29f299SAndrew Duda 	/* next ff_len bytes must be 0xff */
49da29f299SAndrew Duda 	ff_len = pad_len - algo->der_len - 3;
50da29f299SAndrew Duda 	ret |= *msg ^ 0xff;
51da29f299SAndrew Duda 	ret |= memcmp(msg, msg+1, ff_len-1);
52da29f299SAndrew Duda 	msg += ff_len;
53da29f299SAndrew Duda 	/* next byte must be 0x00 */
54da29f299SAndrew Duda 	ret |= *msg++;
55da29f299SAndrew Duda 	/* next der_len bytes must match der_prefix */
56da29f299SAndrew Duda 	ret |= memcmp(msg, algo->der_prefix, algo->der_len);
57da29f299SAndrew Duda 
58da29f299SAndrew Duda 	return ret;
59da29f299SAndrew Duda }
60da29f299SAndrew Duda 
61da29f299SAndrew Duda /**
62fc2f4246SRuchika Gupta  * rsa_verify_key() - Verify a signature against some data using RSA Key
6319c402afSSimon Glass  *
64fc2f4246SRuchika Gupta  * Verify a RSA PKCS1.5 signature against an expected hash using
65fc2f4246SRuchika Gupta  * the RSA Key properties in prop structure.
66fc2f4246SRuchika Gupta  *
67fc2f4246SRuchika Gupta  * @prop:	Specifies key
68fc2f4246SRuchika Gupta  * @sig:	Signature
69fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
70fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
71*0c1d74fdSAndrew Duda  * @key_len:	Number of bytes in rsa key
72*0c1d74fdSAndrew Duda  * @algo:	Checksum algo structure having information on DER encoding etc.
73fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
7419c402afSSimon Glass  */
75fc2f4246SRuchika Gupta static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
76646257d1SHeiko Schocher 			  const uint32_t sig_len, const uint8_t *hash,
77*0c1d74fdSAndrew Duda 			  const uint32_t key_len, struct checksum_algo *algo)
7819c402afSSimon Glass {
7919c402afSSimon Glass 	int pad_len;
8019c402afSSimon Glass 	int ret;
81c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC)
82c937ff6dSRuchika Gupta 	struct udevice *mod_exp_dev;
83c937ff6dSRuchika Gupta #endif
8419c402afSSimon Glass 
85fc2f4246SRuchika Gupta 	if (!prop || !sig || !hash || !algo)
8619c402afSSimon Glass 		return -EIO;
8719c402afSSimon Glass 
88fc2f4246SRuchika Gupta 	if (sig_len != (prop->num_bits / 8)) {
8919c402afSSimon Glass 		debug("Signature is of incorrect length %d\n", sig_len);
9019c402afSSimon Glass 		return -EINVAL;
9119c402afSSimon Glass 	}
9219c402afSSimon Glass 
93646257d1SHeiko Schocher 	debug("Checksum algorithm: %s", algo->name);
94646257d1SHeiko Schocher 
9519c402afSSimon Glass 	/* Sanity check for stack size */
9619c402afSSimon Glass 	if (sig_len > RSA_MAX_SIG_BITS / 8) {
9719c402afSSimon Glass 		debug("Signature length %u exceeds maximum %d\n", sig_len,
9819c402afSSimon Glass 		      RSA_MAX_SIG_BITS / 8);
9919c402afSSimon Glass 		return -EINVAL;
10019c402afSSimon Glass 	}
10119c402afSSimon Glass 
102fc2f4246SRuchika Gupta 	uint8_t buf[sig_len];
10319c402afSSimon Glass 
104c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC)
105c937ff6dSRuchika Gupta 	ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
106c937ff6dSRuchika Gupta 	if (ret) {
107c937ff6dSRuchika Gupta 		printf("RSA: Can't find Modular Exp implementation\n");
108c937ff6dSRuchika Gupta 		return -EINVAL;
109c937ff6dSRuchika Gupta 	}
110c937ff6dSRuchika Gupta 
111c937ff6dSRuchika Gupta 	ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
112c937ff6dSRuchika Gupta #else
113fc2f4246SRuchika Gupta 	ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
114c937ff6dSRuchika Gupta #endif
115fc2f4246SRuchika Gupta 	if (ret) {
116fc2f4246SRuchika Gupta 		debug("Error in Modular exponentation\n");
11719c402afSSimon Glass 		return ret;
118fc2f4246SRuchika Gupta 	}
11919c402afSSimon Glass 
120*0c1d74fdSAndrew Duda 	pad_len = key_len - algo->checksum_len;
12119c402afSSimon Glass 
12219c402afSSimon Glass 	/* Check pkcs1.5 padding bytes. */
123da29f299SAndrew Duda 	ret = rsa_verify_padding(buf, pad_len, algo);
124da29f299SAndrew Duda 	if (ret) {
12519c402afSSimon Glass 		debug("In RSAVerify(): Padding check failed!\n");
12619c402afSSimon Glass 		return -EINVAL;
12719c402afSSimon Glass 	}
12819c402afSSimon Glass 
12919c402afSSimon Glass 	/* Check hash. */
13019c402afSSimon Glass 	if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
13119c402afSSimon Glass 		debug("In RSAVerify(): Hash check failed!\n");
13219c402afSSimon Glass 		return -EACCES;
13319c402afSSimon Glass 	}
13419c402afSSimon Glass 
13519c402afSSimon Glass 	return 0;
13619c402afSSimon Glass }
13719c402afSSimon Glass 
138fc2f4246SRuchika Gupta /**
139fc2f4246SRuchika Gupta  * rsa_verify_with_keynode() - Verify a signature against some data using
140fc2f4246SRuchika Gupta  * information in node with prperties of RSA Key like modulus, exponent etc.
141fc2f4246SRuchika Gupta  *
142fc2f4246SRuchika Gupta  * Parse sign-node and fill a key_prop structure with properties of the
143fc2f4246SRuchika Gupta  * key.  Verify a RSA PKCS1.5 signature against an expected hash using
144fc2f4246SRuchika Gupta  * the properties parsed
145fc2f4246SRuchika Gupta  *
146fc2f4246SRuchika Gupta  * @info:	Specifies key and FIT information
147fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
148fc2f4246SRuchika Gupta  * @sig:	Signature
149fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
150fc2f4246SRuchika Gupta  * @node:	Node having the RSA Key properties
151fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
152fc2f4246SRuchika Gupta  */
15319c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info,
154fc2f4246SRuchika Gupta 				   const void *hash, uint8_t *sig,
155fc2f4246SRuchika Gupta 				   uint sig_len, int node)
15619c402afSSimon Glass {
15719c402afSSimon Glass 	const void *blob = info->fdt_blob;
158fc2f4246SRuchika Gupta 	struct key_prop prop;
159e0f2f155SMichael van der Westhuizen 	int length;
160fc2f4246SRuchika Gupta 	int ret = 0;
16119c402afSSimon Glass 
16219c402afSSimon Glass 	if (node < 0) {
16319c402afSSimon Glass 		debug("%s: Skipping invalid node", __func__);
16419c402afSSimon Glass 		return -EBADF;
16519c402afSSimon Glass 	}
166fc2f4246SRuchika Gupta 
167fc2f4246SRuchika Gupta 	prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
168fc2f4246SRuchika Gupta 
169fc2f4246SRuchika Gupta 	prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
170fc2f4246SRuchika Gupta 
171fc2f4246SRuchika Gupta 	prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
172fc2f4246SRuchika Gupta 	if (!prop.public_exponent || length < sizeof(uint64_t))
173fc2f4246SRuchika Gupta 		prop.public_exponent = NULL;
174fc2f4246SRuchika Gupta 
175fc2f4246SRuchika Gupta 	prop.exp_len = sizeof(uint64_t);
176fc2f4246SRuchika Gupta 
177fc2f4246SRuchika Gupta 	prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
178fc2f4246SRuchika Gupta 
179fc2f4246SRuchika Gupta 	prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
180fc2f4246SRuchika Gupta 
181fc2f4246SRuchika Gupta 	if (!prop.num_bits || !prop.modulus) {
18219c402afSSimon Glass 		debug("%s: Missing RSA key info", __func__);
18319c402afSSimon Glass 		return -EFAULT;
18419c402afSSimon Glass 	}
18519c402afSSimon Glass 
186*0c1d74fdSAndrew Duda 	ret = rsa_verify_key(&prop, sig, sig_len, hash,
187*0c1d74fdSAndrew Duda 			     info->algo->crypto->key_len,
188*0c1d74fdSAndrew Duda 			     info->algo->checksum);
18919c402afSSimon Glass 
19019c402afSSimon Glass 	return ret;
19119c402afSSimon Glass }
19219c402afSSimon Glass 
19319c402afSSimon Glass int rsa_verify(struct image_sign_info *info,
19419c402afSSimon Glass 	       const struct image_region region[], int region_count,
19519c402afSSimon Glass 	       uint8_t *sig, uint sig_len)
19619c402afSSimon Glass {
19719c402afSSimon Glass 	const void *blob = info->fdt_blob;
198646257d1SHeiko Schocher 	/* Reserve memory for maximum checksum-length */
199*0c1d74fdSAndrew Duda 	uint8_t hash[info->algo->crypto->key_len];
20019c402afSSimon Glass 	int ndepth, noffset;
20119c402afSSimon Glass 	int sig_node, node;
20219c402afSSimon Glass 	char name[100];
203646257d1SHeiko Schocher 	int ret;
204646257d1SHeiko Schocher 
205646257d1SHeiko Schocher 	/*
206646257d1SHeiko Schocher 	 * Verify that the checksum-length does not exceed the
207646257d1SHeiko Schocher 	 * rsa-signature-length
208646257d1SHeiko Schocher 	 */
209db1b5f3dSHeiko Schocher 	if (info->algo->checksum->checksum_len >
210*0c1d74fdSAndrew Duda 	    info->algo->crypto->key_len) {
211db1b5f3dSHeiko Schocher 		debug("%s: invlaid checksum-algorithm %s for %s\n",
212*0c1d74fdSAndrew Duda 		      __func__, info->algo->checksum->name,
213*0c1d74fdSAndrew Duda 		      info->algo->crypto->name);
214646257d1SHeiko Schocher 		return -EINVAL;
215646257d1SHeiko Schocher 	}
21619c402afSSimon Glass 
21719c402afSSimon Glass 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
21819c402afSSimon Glass 	if (sig_node < 0) {
21919c402afSSimon Glass 		debug("%s: No signature node found\n", __func__);
22019c402afSSimon Glass 		return -ENOENT;
22119c402afSSimon Glass 	}
22219c402afSSimon Glass 
223646257d1SHeiko Schocher 	/* Calculate checksum with checksum-algorithm */
224b37b46f0SRuchika Gupta 	ret = info->algo->checksum->calculate(info->algo->checksum->name,
225b37b46f0SRuchika Gupta 					region, region_count, hash);
226b37b46f0SRuchika Gupta 	if (ret < 0) {
227b37b46f0SRuchika Gupta 		debug("%s: Error in checksum calculation\n", __func__);
228b37b46f0SRuchika Gupta 		return -EINVAL;
229b37b46f0SRuchika Gupta 	}
23019c402afSSimon Glass 
23119c402afSSimon Glass 	/* See if we must use a particular key */
23219c402afSSimon Glass 	if (info->required_keynode != -1) {
23319c402afSSimon Glass 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
23419c402afSSimon Glass 			info->required_keynode);
23519c402afSSimon Glass 		if (!ret)
23619c402afSSimon Glass 			return ret;
23719c402afSSimon Glass 	}
23819c402afSSimon Glass 
23919c402afSSimon Glass 	/* Look for a key that matches our hint */
24019c402afSSimon Glass 	snprintf(name, sizeof(name), "key-%s", info->keyname);
24119c402afSSimon Glass 	node = fdt_subnode_offset(blob, sig_node, name);
24219c402afSSimon Glass 	ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
24319c402afSSimon Glass 	if (!ret)
24419c402afSSimon Glass 		return ret;
24519c402afSSimon Glass 
24619c402afSSimon Glass 	/* No luck, so try each of the keys in turn */
24719c402afSSimon Glass 	for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
24819c402afSSimon Glass 			(noffset >= 0) && (ndepth > 0);
24919c402afSSimon Glass 			noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
25019c402afSSimon Glass 		if (ndepth == 1 && noffset != node) {
25119c402afSSimon Glass 			ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
25219c402afSSimon Glass 						      noffset);
25319c402afSSimon Glass 			if (!ret)
25419c402afSSimon Glass 				break;
25519c402afSSimon Glass 		}
25619c402afSSimon Glass 	}
25719c402afSSimon Glass 
25819c402afSSimon Glass 	return ret;
25919c402afSSimon Glass }
260