xref: /rk3399_rockchip-uboot/lib/rsa/rsa-verify.c (revision c937ff6dc2ee3fcd8f97087427fe8ba5086852c4)
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>
15*c937ff6dSRuchika 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 /**
28fc2f4246SRuchika Gupta  * rsa_verify_key() - Verify a signature against some data using RSA Key
2919c402afSSimon Glass  *
30fc2f4246SRuchika Gupta  * Verify a RSA PKCS1.5 signature against an expected hash using
31fc2f4246SRuchika Gupta  * the RSA Key properties in prop structure.
32fc2f4246SRuchika Gupta  *
33fc2f4246SRuchika Gupta  * @prop:	Specifies key
34fc2f4246SRuchika Gupta  * @sig:	Signature
35fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
36fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
37fc2f4246SRuchika Gupta  * @algo:	Checksum algo structure having information on RSA padding etc.
38fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
3919c402afSSimon Glass  */
40fc2f4246SRuchika Gupta static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
41646257d1SHeiko Schocher 			  const uint32_t sig_len, const uint8_t *hash,
42646257d1SHeiko Schocher 			  struct checksum_algo *algo)
4319c402afSSimon Glass {
4419c402afSSimon Glass 	const uint8_t *padding;
4519c402afSSimon Glass 	int pad_len;
4619c402afSSimon Glass 	int ret;
47*c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC)
48*c937ff6dSRuchika Gupta 	struct udevice *mod_exp_dev;
49*c937ff6dSRuchika Gupta #endif
5019c402afSSimon Glass 
51fc2f4246SRuchika Gupta 	if (!prop || !sig || !hash || !algo)
5219c402afSSimon Glass 		return -EIO;
5319c402afSSimon Glass 
54fc2f4246SRuchika Gupta 	if (sig_len != (prop->num_bits / 8)) {
5519c402afSSimon Glass 		debug("Signature is of incorrect length %d\n", sig_len);
5619c402afSSimon Glass 		return -EINVAL;
5719c402afSSimon Glass 	}
5819c402afSSimon Glass 
59646257d1SHeiko Schocher 	debug("Checksum algorithm: %s", algo->name);
60646257d1SHeiko Schocher 
6119c402afSSimon Glass 	/* Sanity check for stack size */
6219c402afSSimon Glass 	if (sig_len > RSA_MAX_SIG_BITS / 8) {
6319c402afSSimon Glass 		debug("Signature length %u exceeds maximum %d\n", sig_len,
6419c402afSSimon Glass 		      RSA_MAX_SIG_BITS / 8);
6519c402afSSimon Glass 		return -EINVAL;
6619c402afSSimon Glass 	}
6719c402afSSimon Glass 
68fc2f4246SRuchika Gupta 	uint8_t buf[sig_len];
6919c402afSSimon Glass 
70*c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC)
71*c937ff6dSRuchika Gupta 	ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
72*c937ff6dSRuchika Gupta 	if (ret) {
73*c937ff6dSRuchika Gupta 		printf("RSA: Can't find Modular Exp implementation\n");
74*c937ff6dSRuchika Gupta 		return -EINVAL;
75*c937ff6dSRuchika Gupta 	}
76*c937ff6dSRuchika Gupta 
77*c937ff6dSRuchika Gupta 	ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
78*c937ff6dSRuchika Gupta #else
79fc2f4246SRuchika Gupta 	ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
80*c937ff6dSRuchika Gupta #endif
81fc2f4246SRuchika Gupta 	if (ret) {
82fc2f4246SRuchika Gupta 		debug("Error in Modular exponentation\n");
8319c402afSSimon Glass 		return ret;
84fc2f4246SRuchika Gupta 	}
8519c402afSSimon Glass 
86646257d1SHeiko Schocher 	padding = algo->rsa_padding;
87db1b5f3dSHeiko Schocher 	pad_len = algo->pad_len - algo->checksum_len;
8819c402afSSimon Glass 
8919c402afSSimon Glass 	/* Check pkcs1.5 padding bytes. */
9019c402afSSimon Glass 	if (memcmp(buf, padding, pad_len)) {
9119c402afSSimon Glass 		debug("In RSAVerify(): Padding check failed!\n");
9219c402afSSimon Glass 		return -EINVAL;
9319c402afSSimon Glass 	}
9419c402afSSimon Glass 
9519c402afSSimon Glass 	/* Check hash. */
9619c402afSSimon Glass 	if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
9719c402afSSimon Glass 		debug("In RSAVerify(): Hash check failed!\n");
9819c402afSSimon Glass 		return -EACCES;
9919c402afSSimon Glass 	}
10019c402afSSimon Glass 
10119c402afSSimon Glass 	return 0;
10219c402afSSimon Glass }
10319c402afSSimon Glass 
104fc2f4246SRuchika Gupta /**
105fc2f4246SRuchika Gupta  * rsa_verify_with_keynode() - Verify a signature against some data using
106fc2f4246SRuchika Gupta  * information in node with prperties of RSA Key like modulus, exponent etc.
107fc2f4246SRuchika Gupta  *
108fc2f4246SRuchika Gupta  * Parse sign-node and fill a key_prop structure with properties of the
109fc2f4246SRuchika Gupta  * key.  Verify a RSA PKCS1.5 signature against an expected hash using
110fc2f4246SRuchika Gupta  * the properties parsed
111fc2f4246SRuchika Gupta  *
112fc2f4246SRuchika Gupta  * @info:	Specifies key and FIT information
113fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
114fc2f4246SRuchika Gupta  * @sig:	Signature
115fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
116fc2f4246SRuchika Gupta  * @node:	Node having the RSA Key properties
117fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
118fc2f4246SRuchika Gupta  */
11919c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info,
120fc2f4246SRuchika Gupta 				   const void *hash, uint8_t *sig,
121fc2f4246SRuchika Gupta 				   uint sig_len, int node)
12219c402afSSimon Glass {
12319c402afSSimon Glass 	const void *blob = info->fdt_blob;
124fc2f4246SRuchika Gupta 	struct key_prop prop;
125e0f2f155SMichael van der Westhuizen 	int length;
126fc2f4246SRuchika Gupta 	int ret = 0;
12719c402afSSimon Glass 
12819c402afSSimon Glass 	if (node < 0) {
12919c402afSSimon Glass 		debug("%s: Skipping invalid node", __func__);
13019c402afSSimon Glass 		return -EBADF;
13119c402afSSimon Glass 	}
132fc2f4246SRuchika Gupta 
133fc2f4246SRuchika Gupta 	prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
134fc2f4246SRuchika Gupta 
135fc2f4246SRuchika Gupta 	prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
136fc2f4246SRuchika Gupta 
137fc2f4246SRuchika Gupta 	prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
138fc2f4246SRuchika Gupta 	if (!prop.public_exponent || length < sizeof(uint64_t))
139fc2f4246SRuchika Gupta 		prop.public_exponent = NULL;
140fc2f4246SRuchika Gupta 
141fc2f4246SRuchika Gupta 	prop.exp_len = sizeof(uint64_t);
142fc2f4246SRuchika Gupta 
143fc2f4246SRuchika Gupta 	prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
144fc2f4246SRuchika Gupta 
145fc2f4246SRuchika Gupta 	prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
146fc2f4246SRuchika Gupta 
147fc2f4246SRuchika Gupta 	if (!prop.num_bits || !prop.modulus) {
14819c402afSSimon Glass 		debug("%s: Missing RSA key info", __func__);
14919c402afSSimon Glass 		return -EFAULT;
15019c402afSSimon Glass 	}
15119c402afSSimon Glass 
152fc2f4246SRuchika Gupta 	ret = rsa_verify_key(&prop, sig, sig_len, hash, info->algo->checksum);
15319c402afSSimon Glass 
15419c402afSSimon Glass 	return ret;
15519c402afSSimon Glass }
15619c402afSSimon Glass 
15719c402afSSimon Glass int rsa_verify(struct image_sign_info *info,
15819c402afSSimon Glass 	       const struct image_region region[], int region_count,
15919c402afSSimon Glass 	       uint8_t *sig, uint sig_len)
16019c402afSSimon Glass {
16119c402afSSimon Glass 	const void *blob = info->fdt_blob;
162646257d1SHeiko Schocher 	/* Reserve memory for maximum checksum-length */
163db1b5f3dSHeiko Schocher 	uint8_t hash[info->algo->checksum->pad_len];
16419c402afSSimon Glass 	int ndepth, noffset;
16519c402afSSimon Glass 	int sig_node, node;
16619c402afSSimon Glass 	char name[100];
167646257d1SHeiko Schocher 	int ret;
168646257d1SHeiko Schocher 
169646257d1SHeiko Schocher 	/*
170646257d1SHeiko Schocher 	 * Verify that the checksum-length does not exceed the
171646257d1SHeiko Schocher 	 * rsa-signature-length
172646257d1SHeiko Schocher 	 */
173db1b5f3dSHeiko Schocher 	if (info->algo->checksum->checksum_len >
174db1b5f3dSHeiko Schocher 	    info->algo->checksum->pad_len) {
175db1b5f3dSHeiko Schocher 		debug("%s: invlaid checksum-algorithm %s for %s\n",
176db1b5f3dSHeiko Schocher 		      __func__, info->algo->checksum->name, info->algo->name);
177646257d1SHeiko Schocher 		return -EINVAL;
178646257d1SHeiko Schocher 	}
17919c402afSSimon Glass 
18019c402afSSimon Glass 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
18119c402afSSimon Glass 	if (sig_node < 0) {
18219c402afSSimon Glass 		debug("%s: No signature node found\n", __func__);
18319c402afSSimon Glass 		return -ENOENT;
18419c402afSSimon Glass 	}
18519c402afSSimon Glass 
186646257d1SHeiko Schocher 	/* Calculate checksum with checksum-algorithm */
187646257d1SHeiko Schocher 	info->algo->checksum->calculate(region, region_count, hash);
18819c402afSSimon Glass 
18919c402afSSimon Glass 	/* See if we must use a particular key */
19019c402afSSimon Glass 	if (info->required_keynode != -1) {
19119c402afSSimon Glass 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
19219c402afSSimon Glass 			info->required_keynode);
19319c402afSSimon Glass 		if (!ret)
19419c402afSSimon Glass 			return ret;
19519c402afSSimon Glass 	}
19619c402afSSimon Glass 
19719c402afSSimon Glass 	/* Look for a key that matches our hint */
19819c402afSSimon Glass 	snprintf(name, sizeof(name), "key-%s", info->keyname);
19919c402afSSimon Glass 	node = fdt_subnode_offset(blob, sig_node, name);
20019c402afSSimon Glass 	ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
20119c402afSSimon Glass 	if (!ret)
20219c402afSSimon Glass 		return ret;
20319c402afSSimon Glass 
20419c402afSSimon Glass 	/* No luck, so try each of the keys in turn */
20519c402afSSimon Glass 	for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
20619c402afSSimon Glass 			(noffset >= 0) && (ndepth > 0);
20719c402afSSimon Glass 			noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
20819c402afSSimon Glass 		if (ndepth == 1 && noffset != node) {
20919c402afSSimon Glass 			ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
21019c402afSSimon Glass 						      noffset);
21119c402afSSimon Glass 			if (!ret)
21219c402afSSimon Glass 				break;
21319c402afSSimon Glass 		}
21419c402afSSimon Glass 	}
21519c402afSSimon Glass 
21619c402afSSimon Glass 	return ret;
21719c402afSSimon Glass }
218