2013-06-13 22:10:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Google Inc.
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2013-06-13 22:10:02 +00:00
|
|
|
*/
|
|
|
|
|
2014-03-03 11:19:30 +00:00
|
|
|
#ifndef USE_HOSTCC
|
2013-06-13 22:10:02 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <fdtdec.h>
|
2014-03-03 11:19:30 +00:00
|
|
|
#include <asm/types.h>
|
2013-06-13 22:10:02 +00:00
|
|
|
#include <asm/byteorder.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2014-03-03 11:19:30 +00:00
|
|
|
#include <asm/types.h>
|
2013-06-13 22:10:02 +00:00
|
|
|
#include <asm/unaligned.h>
|
2015-01-23 10:31:54 +00:00
|
|
|
#include <dm.h>
|
2014-03-03 11:19:30 +00:00
|
|
|
#else
|
|
|
|
#include "fdt_host.h"
|
|
|
|
#include "mkimage.h"
|
|
|
|
#include <fdt_support.h>
|
|
|
|
#endif
|
2015-01-23 10:31:50 +00:00
|
|
|
#include <u-boot/rsa-mod-exp.h>
|
2014-06-12 20:27:12 +00:00
|
|
|
#include <u-boot/rsa.h>
|
2014-03-03 11:19:30 +00:00
|
|
|
|
2014-07-02 08:17:26 +00:00
|
|
|
/* Default public exponent for backward compatibility */
|
|
|
|
#define RSA_DEFAULT_PUBEXP 65537
|
|
|
|
|
2016-11-08 18:53:40 +00:00
|
|
|
/**
|
|
|
|
* rsa_verify_padding() - Verify RSA message padding is valid
|
|
|
|
*
|
|
|
|
* Verify a RSA message's padding is consistent with PKCS1.5
|
|
|
|
* padding as described in the RSA PKCS#1 v2.1 standard.
|
|
|
|
*
|
|
|
|
* @msg: Padded message
|
|
|
|
* @pad_len: Number of expected padding bytes
|
|
|
|
* @algo: Checksum algo structure having information on DER encoding etc.
|
|
|
|
* @return 0 on success, != 0 on failure
|
|
|
|
*/
|
|
|
|
static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
|
|
|
|
struct checksum_algo *algo)
|
|
|
|
{
|
|
|
|
int ff_len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* first byte must be 0x00 */
|
|
|
|
ret = *msg++;
|
|
|
|
/* second byte must be 0x01 */
|
|
|
|
ret |= *msg++ ^ 0x01;
|
|
|
|
/* next ff_len bytes must be 0xff */
|
|
|
|
ff_len = pad_len - algo->der_len - 3;
|
|
|
|
ret |= *msg ^ 0xff;
|
|
|
|
ret |= memcmp(msg, msg+1, ff_len-1);
|
|
|
|
msg += ff_len;
|
|
|
|
/* next byte must be 0x00 */
|
|
|
|
ret |= *msg++;
|
|
|
|
/* next der_len bytes must match der_prefix */
|
|
|
|
ret |= memcmp(msg, algo->der_prefix, algo->der_len);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-13 22:10:02 +00:00
|
|
|
/**
|
2015-01-23 10:31:50 +00:00
|
|
|
* rsa_verify_key() - Verify a signature against some data using RSA Key
|
2013-06-13 22:10:02 +00:00
|
|
|
*
|
2015-01-23 10:31:50 +00:00
|
|
|
* Verify a RSA PKCS1.5 signature against an expected hash using
|
|
|
|
* the RSA Key properties in prop structure.
|
2013-06-13 22:10:02 +00:00
|
|
|
*
|
2015-01-23 10:31:50 +00:00
|
|
|
* @prop: Specifies key
|
|
|
|
* @sig: Signature
|
|
|
|
* @sig_len: Number of bytes in signature
|
|
|
|
* @hash: Pointer to the expected hash
|
|
|
|
* @algo: Checksum algo structure having information on RSA padding etc.
|
|
|
|
* @return 0 if verified, -ve on error
|
2013-06-13 22:10:02 +00:00
|
|
|
*/
|
2015-01-23 10:31:50 +00:00
|
|
|
static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
|
2014-03-03 11:19:26 +00:00
|
|
|
const uint32_t sig_len, const uint8_t *hash,
|
|
|
|
struct checksum_algo *algo)
|
2013-06-13 22:10:02 +00:00
|
|
|
{
|
|
|
|
const uint8_t *padding;
|
|
|
|
int pad_len;
|
|
|
|
int ret;
|
2015-01-23 10:31:54 +00:00
|
|
|
#if !defined(USE_HOSTCC)
|
|
|
|
struct udevice *mod_exp_dev;
|
|
|
|
#endif
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
if (!prop || !sig || !hash || !algo)
|
2013-06-13 22:10:02 +00:00
|
|
|
return -EIO;
|
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
if (sig_len != (prop->num_bits / 8)) {
|
2013-06-13 22:10:02 +00:00
|
|
|
debug("Signature is of incorrect length %d\n", sig_len);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-03-03 11:19:26 +00:00
|
|
|
debug("Checksum algorithm: %s", algo->name);
|
|
|
|
|
2013-06-13 22:10:02 +00:00
|
|
|
/* Sanity check for stack size */
|
|
|
|
if (sig_len > RSA_MAX_SIG_BITS / 8) {
|
|
|
|
debug("Signature length %u exceeds maximum %d\n", sig_len,
|
|
|
|
RSA_MAX_SIG_BITS / 8);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
uint8_t buf[sig_len];
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2015-01-23 10:31:54 +00:00
|
|
|
#if !defined(USE_HOSTCC)
|
|
|
|
ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
|
|
|
|
if (ret) {
|
|
|
|
printf("RSA: Can't find Modular Exp implementation\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
|
|
|
|
#else
|
2015-01-23 10:31:50 +00:00
|
|
|
ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
|
2015-01-23 10:31:54 +00:00
|
|
|
#endif
|
2015-01-23 10:31:50 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("Error in Modular exponentation\n");
|
2013-06-13 22:10:02 +00:00
|
|
|
return ret;
|
2015-01-23 10:31:50 +00:00
|
|
|
}
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2016-11-08 18:53:39 +00:00
|
|
|
pad_len = algo->key_len - algo->checksum_len;
|
2013-06-13 22:10:02 +00:00
|
|
|
|
|
|
|
/* Check pkcs1.5 padding bytes. */
|
2016-11-08 18:53:40 +00:00
|
|
|
ret = rsa_verify_padding(buf, pad_len, algo);
|
|
|
|
if (ret) {
|
2013-06-13 22:10:02 +00:00
|
|
|
debug("In RSAVerify(): Padding check failed!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check hash. */
|
|
|
|
if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
|
|
|
|
debug("In RSAVerify(): Hash check failed!\n");
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
/**
|
|
|
|
* rsa_verify_with_keynode() - Verify a signature against some data using
|
|
|
|
* information in node with prperties of RSA Key like modulus, exponent etc.
|
|
|
|
*
|
|
|
|
* Parse sign-node and fill a key_prop structure with properties of the
|
|
|
|
* key. Verify a RSA PKCS1.5 signature against an expected hash using
|
|
|
|
* the properties parsed
|
|
|
|
*
|
|
|
|
* @info: Specifies key and FIT information
|
|
|
|
* @hash: Pointer to the expected hash
|
|
|
|
* @sig: Signature
|
|
|
|
* @sig_len: Number of bytes in signature
|
|
|
|
* @node: Node having the RSA Key properties
|
|
|
|
* @return 0 if verified, -ve on error
|
|
|
|
*/
|
2013-06-13 22:10:02 +00:00
|
|
|
static int rsa_verify_with_keynode(struct image_sign_info *info,
|
2015-01-23 10:31:50 +00:00
|
|
|
const void *hash, uint8_t *sig,
|
|
|
|
uint sig_len, int node)
|
2013-06-13 22:10:02 +00:00
|
|
|
{
|
|
|
|
const void *blob = info->fdt_blob;
|
2015-01-23 10:31:50 +00:00
|
|
|
struct key_prop prop;
|
2014-07-02 08:17:26 +00:00
|
|
|
int length;
|
2015-01-23 10:31:50 +00:00
|
|
|
int ret = 0;
|
2013-06-13 22:10:02 +00:00
|
|
|
|
|
|
|
if (node < 0) {
|
|
|
|
debug("%s: Skipping invalid node", __func__);
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
|
|
|
|
if (!prop.public_exponent || length < sizeof(uint64_t))
|
|
|
|
prop.public_exponent = NULL;
|
|
|
|
|
|
|
|
prop.exp_len = sizeof(uint64_t);
|
|
|
|
|
|
|
|
prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
|
|
|
|
|
|
|
|
prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
|
|
|
|
|
|
|
|
if (!prop.num_bits || !prop.modulus) {
|
|
|
|
debug("%s: Missing RSA key info", __func__);
|
|
|
|
return -EFAULT;
|
2013-06-13 22:10:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 10:31:50 +00:00
|
|
|
ret = rsa_verify_key(&prop, sig, sig_len, hash, info->algo->checksum);
|
|
|
|
|
|
|
|
return ret;
|
2013-06-13 22:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rsa_verify(struct image_sign_info *info,
|
|
|
|
const struct image_region region[], int region_count,
|
|
|
|
uint8_t *sig, uint sig_len)
|
|
|
|
{
|
|
|
|
const void *blob = info->fdt_blob;
|
2014-03-03 11:19:26 +00:00
|
|
|
/* Reserve memory for maximum checksum-length */
|
2016-11-08 18:53:39 +00:00
|
|
|
uint8_t hash[info->algo->checksum->key_len];
|
2013-06-13 22:10:02 +00:00
|
|
|
int ndepth, noffset;
|
|
|
|
int sig_node, node;
|
|
|
|
char name[100];
|
2014-03-03 11:19:26 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the checksum-length does not exceed the
|
|
|
|
* rsa-signature-length
|
|
|
|
*/
|
2014-03-03 11:19:27 +00:00
|
|
|
if (info->algo->checksum->checksum_len >
|
2016-11-08 18:53:39 +00:00
|
|
|
info->algo->checksum->key_len) {
|
2014-03-03 11:19:27 +00:00
|
|
|
debug("%s: invlaid checksum-algorithm %s for %s\n",
|
|
|
|
__func__, info->algo->checksum->name, info->algo->name);
|
2014-03-03 11:19:26 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-06-13 22:10:02 +00:00
|
|
|
|
|
|
|
sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
|
|
|
|
if (sig_node < 0) {
|
|
|
|
debug("%s: No signature node found\n", __func__);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2014-03-03 11:19:26 +00:00
|
|
|
/* Calculate checksum with checksum-algorithm */
|
2015-01-23 10:31:59 +00:00
|
|
|
ret = info->algo->checksum->calculate(info->algo->checksum->name,
|
|
|
|
region, region_count, hash);
|
|
|
|
if (ret < 0) {
|
|
|
|
debug("%s: Error in checksum calculation\n", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-06-13 22:10:02 +00:00
|
|
|
|
|
|
|
/* See if we must use a particular key */
|
|
|
|
if (info->required_keynode != -1) {
|
|
|
|
ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
|
|
|
|
info->required_keynode);
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for a key that matches our hint */
|
|
|
|
snprintf(name, sizeof(name), "key-%s", info->keyname);
|
|
|
|
node = fdt_subnode_offset(blob, sig_node, name);
|
|
|
|
ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* No luck, so try each of the keys in turn */
|
|
|
|
for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
|
|
|
|
(noffset >= 0) && (ndepth > 0);
|
|
|
|
noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
|
|
|
|
if (ndepth == 1 && noffset != node) {
|
|
|
|
ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
|
|
|
|
noffset);
|
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|