xref: /rk3399_rockchip-uboot/lib/rsa/rsa-verify.c (revision fc2f4246b4b3b750e8c5aa08440ec5e1c952088e)
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>
1229a23f9dSHeiko Schocher #include <asm/errno.h>
1329a23f9dSHeiko Schocher #include <asm/types.h>
1429a23f9dSHeiko Schocher #include <asm/unaligned.h>
1529a23f9dSHeiko Schocher #else
1629a23f9dSHeiko Schocher #include "fdt_host.h"
1729a23f9dSHeiko Schocher #include "mkimage.h"
1829a23f9dSHeiko Schocher #include <fdt_support.h>
1929a23f9dSHeiko Schocher #endif
20*fc2f4246SRuchika Gupta #include <u-boot/rsa-mod-exp.h>
212b9912e6SJeroen Hofstee #include <u-boot/rsa.h>
2229a23f9dSHeiko Schocher 
23e0f2f155SMichael van der Westhuizen /* Default public exponent for backward compatibility */
24e0f2f155SMichael van der Westhuizen #define RSA_DEFAULT_PUBEXP	65537
25e0f2f155SMichael van der Westhuizen 
2619c402afSSimon Glass /**
27*fc2f4246SRuchika Gupta  * rsa_verify_key() - Verify a signature against some data using RSA Key
2819c402afSSimon Glass  *
29*fc2f4246SRuchika Gupta  * Verify a RSA PKCS1.5 signature against an expected hash using
30*fc2f4246SRuchika Gupta  * the RSA Key properties in prop structure.
31*fc2f4246SRuchika Gupta  *
32*fc2f4246SRuchika Gupta  * @prop:	Specifies key
33*fc2f4246SRuchika Gupta  * @sig:	Signature
34*fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
35*fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
36*fc2f4246SRuchika Gupta  * @algo:	Checksum algo structure having information on RSA padding etc.
37*fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
3819c402afSSimon Glass  */
39*fc2f4246SRuchika Gupta static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
40646257d1SHeiko Schocher 			  const uint32_t sig_len, const uint8_t *hash,
41646257d1SHeiko Schocher 			  struct checksum_algo *algo)
4219c402afSSimon Glass {
4319c402afSSimon Glass 	const uint8_t *padding;
4419c402afSSimon Glass 	int pad_len;
4519c402afSSimon Glass 	int ret;
4619c402afSSimon Glass 
47*fc2f4246SRuchika Gupta 	if (!prop || !sig || !hash || !algo)
4819c402afSSimon Glass 		return -EIO;
4919c402afSSimon Glass 
50*fc2f4246SRuchika Gupta 	if (sig_len != (prop->num_bits / 8)) {
5119c402afSSimon Glass 		debug("Signature is of incorrect length %d\n", sig_len);
5219c402afSSimon Glass 		return -EINVAL;
5319c402afSSimon Glass 	}
5419c402afSSimon Glass 
55646257d1SHeiko Schocher 	debug("Checksum algorithm: %s", algo->name);
56646257d1SHeiko Schocher 
5719c402afSSimon Glass 	/* Sanity check for stack size */
5819c402afSSimon Glass 	if (sig_len > RSA_MAX_SIG_BITS / 8) {
5919c402afSSimon Glass 		debug("Signature length %u exceeds maximum %d\n", sig_len,
6019c402afSSimon Glass 		      RSA_MAX_SIG_BITS / 8);
6119c402afSSimon Glass 		return -EINVAL;
6219c402afSSimon Glass 	}
6319c402afSSimon Glass 
64*fc2f4246SRuchika Gupta 	uint8_t buf[sig_len];
6519c402afSSimon Glass 
66*fc2f4246SRuchika Gupta 	ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
67*fc2f4246SRuchika Gupta 	if (ret) {
68*fc2f4246SRuchika Gupta 		debug("Error in Modular exponentation\n");
6919c402afSSimon Glass 		return ret;
70*fc2f4246SRuchika Gupta 	}
7119c402afSSimon Glass 
72646257d1SHeiko Schocher 	padding = algo->rsa_padding;
73db1b5f3dSHeiko Schocher 	pad_len = algo->pad_len - algo->checksum_len;
7419c402afSSimon Glass 
7519c402afSSimon Glass 	/* Check pkcs1.5 padding bytes. */
7619c402afSSimon Glass 	if (memcmp(buf, padding, pad_len)) {
7719c402afSSimon Glass 		debug("In RSAVerify(): Padding check failed!\n");
7819c402afSSimon Glass 		return -EINVAL;
7919c402afSSimon Glass 	}
8019c402afSSimon Glass 
8119c402afSSimon Glass 	/* Check hash. */
8219c402afSSimon Glass 	if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
8319c402afSSimon Glass 		debug("In RSAVerify(): Hash check failed!\n");
8419c402afSSimon Glass 		return -EACCES;
8519c402afSSimon Glass 	}
8619c402afSSimon Glass 
8719c402afSSimon Glass 	return 0;
8819c402afSSimon Glass }
8919c402afSSimon Glass 
90*fc2f4246SRuchika Gupta /**
91*fc2f4246SRuchika Gupta  * rsa_verify_with_keynode() - Verify a signature against some data using
92*fc2f4246SRuchika Gupta  * information in node with prperties of RSA Key like modulus, exponent etc.
93*fc2f4246SRuchika Gupta  *
94*fc2f4246SRuchika Gupta  * Parse sign-node and fill a key_prop structure with properties of the
95*fc2f4246SRuchika Gupta  * key.  Verify a RSA PKCS1.5 signature against an expected hash using
96*fc2f4246SRuchika Gupta  * the properties parsed
97*fc2f4246SRuchika Gupta  *
98*fc2f4246SRuchika Gupta  * @info:	Specifies key and FIT information
99*fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
100*fc2f4246SRuchika Gupta  * @sig:	Signature
101*fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
102*fc2f4246SRuchika Gupta  * @node:	Node having the RSA Key properties
103*fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
104*fc2f4246SRuchika Gupta  */
10519c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info,
106*fc2f4246SRuchika Gupta 				   const void *hash, uint8_t *sig,
107*fc2f4246SRuchika Gupta 				   uint sig_len, int node)
10819c402afSSimon Glass {
10919c402afSSimon Glass 	const void *blob = info->fdt_blob;
110*fc2f4246SRuchika Gupta 	struct key_prop prop;
111e0f2f155SMichael van der Westhuizen 	int length;
112*fc2f4246SRuchika Gupta 	int ret = 0;
11319c402afSSimon Glass 
11419c402afSSimon Glass 	if (node < 0) {
11519c402afSSimon Glass 		debug("%s: Skipping invalid node", __func__);
11619c402afSSimon Glass 		return -EBADF;
11719c402afSSimon Glass 	}
118*fc2f4246SRuchika Gupta 
119*fc2f4246SRuchika Gupta 	prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
120*fc2f4246SRuchika Gupta 
121*fc2f4246SRuchika Gupta 	prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
122*fc2f4246SRuchika Gupta 
123*fc2f4246SRuchika Gupta 	prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
124*fc2f4246SRuchika Gupta 	if (!prop.public_exponent || length < sizeof(uint64_t))
125*fc2f4246SRuchika Gupta 		prop.public_exponent = NULL;
126*fc2f4246SRuchika Gupta 
127*fc2f4246SRuchika Gupta 	prop.exp_len = sizeof(uint64_t);
128*fc2f4246SRuchika Gupta 
129*fc2f4246SRuchika Gupta 	prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
130*fc2f4246SRuchika Gupta 
131*fc2f4246SRuchika Gupta 	prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
132*fc2f4246SRuchika Gupta 
133*fc2f4246SRuchika Gupta 	if (!prop.num_bits || !prop.modulus) {
13419c402afSSimon Glass 		debug("%s: Missing RSA key info", __func__);
13519c402afSSimon Glass 		return -EFAULT;
13619c402afSSimon Glass 	}
13719c402afSSimon Glass 
138*fc2f4246SRuchika Gupta 	ret = rsa_verify_key(&prop, sig, sig_len, hash, info->algo->checksum);
13919c402afSSimon Glass 
14019c402afSSimon Glass 	return ret;
14119c402afSSimon Glass }
14219c402afSSimon Glass 
14319c402afSSimon Glass int rsa_verify(struct image_sign_info *info,
14419c402afSSimon Glass 	       const struct image_region region[], int region_count,
14519c402afSSimon Glass 	       uint8_t *sig, uint sig_len)
14619c402afSSimon Glass {
14719c402afSSimon Glass 	const void *blob = info->fdt_blob;
148646257d1SHeiko Schocher 	/* Reserve memory for maximum checksum-length */
149db1b5f3dSHeiko Schocher 	uint8_t hash[info->algo->checksum->pad_len];
15019c402afSSimon Glass 	int ndepth, noffset;
15119c402afSSimon Glass 	int sig_node, node;
15219c402afSSimon Glass 	char name[100];
153646257d1SHeiko Schocher 	int ret;
154646257d1SHeiko Schocher 
155646257d1SHeiko Schocher 	/*
156646257d1SHeiko Schocher 	 * Verify that the checksum-length does not exceed the
157646257d1SHeiko Schocher 	 * rsa-signature-length
158646257d1SHeiko Schocher 	 */
159db1b5f3dSHeiko Schocher 	if (info->algo->checksum->checksum_len >
160db1b5f3dSHeiko Schocher 	    info->algo->checksum->pad_len) {
161db1b5f3dSHeiko Schocher 		debug("%s: invlaid checksum-algorithm %s for %s\n",
162db1b5f3dSHeiko Schocher 		      __func__, info->algo->checksum->name, info->algo->name);
163646257d1SHeiko Schocher 		return -EINVAL;
164646257d1SHeiko Schocher 	}
16519c402afSSimon Glass 
16619c402afSSimon Glass 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
16719c402afSSimon Glass 	if (sig_node < 0) {
16819c402afSSimon Glass 		debug("%s: No signature node found\n", __func__);
16919c402afSSimon Glass 		return -ENOENT;
17019c402afSSimon Glass 	}
17119c402afSSimon Glass 
172646257d1SHeiko Schocher 	/* Calculate checksum with checksum-algorithm */
173646257d1SHeiko Schocher 	info->algo->checksum->calculate(region, region_count, hash);
17419c402afSSimon Glass 
17519c402afSSimon Glass 	/* See if we must use a particular key */
17619c402afSSimon Glass 	if (info->required_keynode != -1) {
17719c402afSSimon Glass 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
17819c402afSSimon Glass 			info->required_keynode);
17919c402afSSimon Glass 		if (!ret)
18019c402afSSimon Glass 			return ret;
18119c402afSSimon Glass 	}
18219c402afSSimon Glass 
18319c402afSSimon Glass 	/* Look for a key that matches our hint */
18419c402afSSimon Glass 	snprintf(name, sizeof(name), "key-%s", info->keyname);
18519c402afSSimon Glass 	node = fdt_subnode_offset(blob, sig_node, name);
18619c402afSSimon Glass 	ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
18719c402afSSimon Glass 	if (!ret)
18819c402afSSimon Glass 		return ret;
18919c402afSSimon Glass 
19019c402afSSimon Glass 	/* No luck, so try each of the keys in turn */
19119c402afSSimon Glass 	for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
19219c402afSSimon Glass 			(noffset >= 0) && (ndepth > 0);
19319c402afSSimon Glass 			noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
19419c402afSSimon Glass 		if (ndepth == 1 && noffset != node) {
19519c402afSSimon Glass 			ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
19619c402afSSimon Glass 						      noffset);
19719c402afSSimon Glass 			if (!ret)
19819c402afSSimon Glass 				break;
19919c402afSSimon Glass 		}
20019c402afSSimon Glass 	}
20119c402afSSimon Glass 
20219c402afSSimon Glass 	return ret;
20319c402afSSimon Glass }
204