2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-06-13 22:10:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Google Inc.
|
|
|
|
*/
|
|
|
|
|
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>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.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
|
2020-02-21 06:12:59 +00:00
|
|
|
#include <linux/kconfig.h>
|
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
|
|
|
|
2020-02-21 06:12:59 +00:00
|
|
|
#ifndef __UBOOT__
|
|
|
|
/*
|
|
|
|
* NOTE:
|
|
|
|
* Since host tools, like mkimage, make use of openssl library for
|
|
|
|
* RSA encryption, rsa_verify_with_pkey()/rsa_gen_key_prop() are
|
|
|
|
* of no use and should not be compiled in.
|
|
|
|
* So just turn off CONFIG_RSA_VERIFY_WITH_PKEY.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef CONFIG_RSA_VERIFY_WITH_PKEY
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:51:00 +00:00
|
|
|
int padding_pkcs_15_verify(struct image_sign_info *info,
|
|
|
|
uint8_t *msg, int msg_len,
|
|
|
|
const uint8_t *hash, int hash_len)
|
|
|
|
{
|
|
|
|
struct checksum_algo *checksum = info->checksum;
|
|
|
|
int ret, pad_len = msg_len - checksum->checksum_len;
|
|
|
|
|
|
|
|
/* Check pkcs1.5 padding bytes. */
|
|
|
|
ret = rsa_verify_padding(msg, pad_len, checksum);
|
|
|
|
if (ret) {
|
|
|
|
debug("In RSAVerify(): Padding check failed!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check hash. */
|
|
|
|
if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
|
|
|
|
debug("In RSAVerify(): Hash check failed!\n");
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:51:01 +00:00
|
|
|
#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
|
|
|
|
static void u32_i2osp(uint32_t val, uint8_t *buf)
|
|
|
|
{
|
|
|
|
buf[0] = (uint8_t)((val >> 24) & 0xff);
|
|
|
|
buf[1] = (uint8_t)((val >> 16) & 0xff);
|
|
|
|
buf[2] = (uint8_t)((val >> 8) & 0xff);
|
|
|
|
buf[3] = (uint8_t)((val >> 0) & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mask_generation_function1() - generate an octet string
|
|
|
|
*
|
|
|
|
* Generate an octet string used to check rsa signature.
|
|
|
|
* It use an input octet string and a hash function.
|
|
|
|
*
|
|
|
|
* @checksum: A Hash function
|
|
|
|
* @seed: Specifies an input variable octet string
|
|
|
|
* @seed_len: Size of the input octet string
|
|
|
|
* @output: Specifies the output octet string
|
|
|
|
* @output_len: Size of the output octet string
|
|
|
|
* @return 0 if the octet string was correctly generated, others on error
|
|
|
|
*/
|
|
|
|
static int mask_generation_function1(struct checksum_algo *checksum,
|
|
|
|
uint8_t *seed, int seed_len,
|
|
|
|
uint8_t *output, int output_len)
|
|
|
|
{
|
|
|
|
struct image_region region[2];
|
|
|
|
int ret = 0, i, i_output = 0, region_count = 2;
|
|
|
|
uint32_t counter = 0;
|
|
|
|
uint8_t buf_counter[4], *tmp;
|
|
|
|
int hash_len = checksum->checksum_len;
|
|
|
|
|
|
|
|
memset(output, 0, output_len);
|
|
|
|
|
|
|
|
region[0].data = seed;
|
|
|
|
region[0].size = seed_len;
|
|
|
|
region[1].data = &buf_counter[0];
|
|
|
|
region[1].size = 4;
|
|
|
|
|
|
|
|
tmp = malloc(hash_len);
|
|
|
|
if (!tmp) {
|
|
|
|
debug("%s: can't allocate array tmp\n", __func__);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i_output < output_len) {
|
|
|
|
u32_i2osp(counter, &buf_counter[0]);
|
|
|
|
|
|
|
|
ret = checksum->calculate(checksum->name,
|
|
|
|
region, region_count,
|
|
|
|
tmp);
|
|
|
|
if (ret < 0) {
|
|
|
|
debug("%s: Error in checksum calculation\n", __func__);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while ((i_output < output_len) && (i < hash_len)) {
|
|
|
|
output[i_output] = tmp[i];
|
|
|
|
i_output++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compute_hash_prime(struct checksum_algo *checksum,
|
|
|
|
uint8_t *pad, int pad_len,
|
|
|
|
uint8_t *hash, int hash_len,
|
|
|
|
uint8_t *salt, int salt_len,
|
|
|
|
uint8_t *hprime)
|
|
|
|
{
|
|
|
|
struct image_region region[3];
|
|
|
|
int ret, region_count = 3;
|
|
|
|
|
|
|
|
region[0].data = pad;
|
|
|
|
region[0].size = pad_len;
|
|
|
|
region[1].data = hash;
|
|
|
|
region[1].size = hash_len;
|
|
|
|
region[2].data = salt;
|
|
|
|
region[2].size = salt_len;
|
|
|
|
|
|
|
|
ret = checksum->calculate(checksum->name, region, region_count, hprime);
|
|
|
|
if (ret < 0) {
|
|
|
|
debug("%s: Error in checksum calculation\n", __func__);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int padding_pss_verify(struct image_sign_info *info,
|
|
|
|
uint8_t *msg, int msg_len,
|
|
|
|
const uint8_t *hash, int hash_len)
|
|
|
|
{
|
|
|
|
uint8_t *masked_db = NULL;
|
|
|
|
int masked_db_len = msg_len - hash_len - 1;
|
|
|
|
uint8_t *h = NULL, *hprime = NULL;
|
|
|
|
int h_len = hash_len;
|
|
|
|
uint8_t *db_mask = NULL;
|
|
|
|
int db_mask_len = masked_db_len;
|
|
|
|
uint8_t *db = NULL, *salt = NULL;
|
|
|
|
int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
|
|
|
|
uint8_t pad_zero[8] = { 0 };
|
|
|
|
int ret, i, leftmost_bits = 1;
|
|
|
|
uint8_t leftmost_mask;
|
|
|
|
struct checksum_algo *checksum = info->checksum;
|
|
|
|
|
|
|
|
/* first, allocate everything */
|
|
|
|
masked_db = malloc(masked_db_len);
|
|
|
|
h = malloc(h_len);
|
|
|
|
db_mask = malloc(db_mask_len);
|
|
|
|
db = malloc(db_len);
|
|
|
|
salt = malloc(salt_len);
|
|
|
|
hprime = malloc(hash_len);
|
|
|
|
if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
|
|
|
|
printf("%s: can't allocate some buffer\n", __func__);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* step 4: check if the last byte is 0xbc */
|
|
|
|
if (msg[msg_len - 1] != 0xbc) {
|
|
|
|
printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* step 5 */
|
|
|
|
memcpy(masked_db, msg, masked_db_len);
|
|
|
|
memcpy(h, msg + masked_db_len, h_len);
|
|
|
|
|
|
|
|
/* step 6 */
|
|
|
|
leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
|
|
|
|
if (masked_db[0] & leftmost_mask) {
|
|
|
|
printf("%s: invalid pss padding ", __func__);
|
|
|
|
printf("(leftmost bit of maskedDB not zero)\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* step 7 */
|
|
|
|
mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
|
|
|
|
|
|
|
|
/* step 8 */
|
|
|
|
for (i = 0; i < db_len; i++)
|
|
|
|
db[i] = masked_db[i] ^ db_mask[i];
|
|
|
|
|
|
|
|
/* step 9 */
|
|
|
|
db[0] &= 0xff >> leftmost_bits;
|
|
|
|
|
|
|
|
/* step 10 */
|
|
|
|
if (db[0] != 0x01) {
|
|
|
|
printf("%s: invalid pss padding ", __func__);
|
|
|
|
printf("(leftmost byte of db isn't 0x01)\n");
|
|
|
|
ret = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* step 11 */
|
|
|
|
memcpy(salt, &db[1], salt_len);
|
|
|
|
|
|
|
|
/* step 12 & 13 */
|
|
|
|
compute_hash_prime(checksum, pad_zero, 8,
|
|
|
|
(uint8_t *)hash, hash_len,
|
|
|
|
salt, salt_len, hprime);
|
|
|
|
|
|
|
|
/* step 14 */
|
|
|
|
ret = memcmp(h, hprime, hash_len);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(hprime);
|
|
|
|
free(salt);
|
|
|
|
free(db);
|
|
|
|
free(db_mask);
|
|
|
|
free(h);
|
|
|
|
free(masked_db);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-21 06:12:59 +00:00
|
|
|
#if CONFIG_IS_ENABLED(FIT_SIGNATURE) || IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY)
|
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
|
|
|
*
|
2018-11-14 12:51:00 +00:00
|
|
|
* @info: Specifies key and FIT information
|
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
|
2016-11-08 18:53:41 +00:00
|
|
|
* @key_len: Number of bytes in rsa key
|
2015-01-23 10:31:50 +00:00
|
|
|
* @return 0 if verified, -ve on error
|
2013-06-13 22:10:02 +00:00
|
|
|
*/
|
2018-11-14 12:51:00 +00:00
|
|
|
static int rsa_verify_key(struct image_sign_info *info,
|
|
|
|
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,
|
2018-11-14 12:51:00 +00:00
|
|
|
const uint32_t key_len)
|
2013-06-13 22:10:02 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2015-01-23 10:31:54 +00:00
|
|
|
#if !defined(USE_HOSTCC)
|
|
|
|
struct udevice *mod_exp_dev;
|
|
|
|
#endif
|
2018-11-14 12:51:00 +00:00
|
|
|
struct checksum_algo *checksum = info->checksum;
|
|
|
|
struct padding_algo *padding = info->padding;
|
2019-03-19 09:55:40 +00:00
|
|
|
int hash_len;
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2018-11-14 12:51:00 +00:00
|
|
|
if (!prop || !sig || !hash || !checksum)
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:51:00 +00:00
|
|
|
debug("Checksum algorithm: %s", checksum->name);
|
2014-03-03 11:19:26 +00:00
|
|
|
|
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];
|
2019-03-19 09:55:40 +00:00
|
|
|
hash_len = checksum->checksum_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
|
|
|
|
2018-11-14 12:51:00 +00:00
|
|
|
ret = padding->verify(info, buf, key_len, hash, hash_len);
|
2016-11-08 18:53:40 +00:00
|
|
|
if (ret) {
|
2018-11-14 12:51:00 +00:00
|
|
|
debug("In RSAVerify(): padding check failed!\n");
|
|
|
|
return ret;
|
2013-06-13 22:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-21 06:12:55 +00:00
|
|
|
#endif
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2020-02-21 06:12:59 +00:00
|
|
|
#ifdef CONFIG_RSA_VERIFY_WITH_PKEY
|
|
|
|
/**
|
|
|
|
* rsa_verify_with_pkey() - Verify a signature against some data using
|
|
|
|
* only modulus and exponent as RSA key properties.
|
|
|
|
* @info: Specifies key information
|
|
|
|
* @hash: Pointer to the expected hash
|
|
|
|
* @sig: Signature
|
|
|
|
* @sig_len: Number of bytes in signature
|
|
|
|
*
|
|
|
|
* Parse a RSA public key blob in DER format pointed to in @info and fill
|
|
|
|
* a key_prop structure with properties of the key. Then verify a RSA PKCS1.5
|
|
|
|
* signature against an expected hash using the calculated properties.
|
|
|
|
*
|
|
|
|
* Return 0 if verified, -ve on error
|
|
|
|
*/
|
|
|
|
static int rsa_verify_with_pkey(struct image_sign_info *info,
|
|
|
|
const void *hash, uint8_t *sig, uint sig_len)
|
|
|
|
{
|
|
|
|
struct key_prop *prop;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Public key is self-described to fill key_prop */
|
|
|
|
ret = rsa_gen_key_prop(info->key, info->keylen, &prop);
|
|
|
|
if (ret) {
|
|
|
|
debug("Generating necessary parameter for decoding failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rsa_verify_key(info, prop, sig, sig_len, hash,
|
|
|
|
info->crypto->key_len);
|
|
|
|
|
|
|
|
rsa_free_key_prop(prop);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int rsa_verify_with_pkey(struct image_sign_info *info,
|
|
|
|
const void *hash, uint8_t *sig, uint sig_len)
|
|
|
|
{
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-21 06:12:55 +00:00
|
|
|
#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
|
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
|
|
|
}
|
|
|
|
|
2018-11-14 12:51:00 +00:00
|
|
|
ret = rsa_verify_key(info, &prop, sig, sig_len, hash,
|
|
|
|
info->crypto->key_len);
|
2015-01-23 10:31:50 +00:00
|
|
|
|
|
|
|
return ret;
|
2013-06-13 22:10:02 +00:00
|
|
|
}
|
2020-02-21 06:12:55 +00:00
|
|
|
#else
|
|
|
|
static int rsa_verify_with_keynode(struct image_sign_info *info,
|
|
|
|
const void *hash, uint8_t *sig,
|
|
|
|
uint sig_len, int node)
|
|
|
|
{
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
#endif
|
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)
|
|
|
|
{
|
2014-03-03 11:19:26 +00:00
|
|
|
/* Reserve memory for maximum checksum-length */
|
2016-11-08 18:53:41 +00:00
|
|
|
uint8_t hash[info->crypto->key_len];
|
2020-02-21 06:12:55 +00:00
|
|
|
int ret = -EACCES;
|
2014-03-03 11:19:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the checksum-length does not exceed the
|
|
|
|
* rsa-signature-length
|
|
|
|
*/
|
2016-11-08 18:53:41 +00:00
|
|
|
if (info->checksum->checksum_len >
|
|
|
|
info->crypto->key_len) {
|
2014-03-03 11:19:27 +00:00
|
|
|
debug("%s: invlaid checksum-algorithm %s for %s\n",
|
2016-11-08 18:53:41 +00:00
|
|
|
__func__, info->checksum->name, info->crypto->name);
|
2014-03-03 11:19:26 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2014-03-03 11:19:26 +00:00
|
|
|
/* Calculate checksum with checksum-algorithm */
|
2016-11-08 18:53:41 +00:00
|
|
|
ret = info->checksum->calculate(info->checksum->name,
|
2015-01-23 10:31:59 +00:00
|
|
|
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
|
|
|
|
2020-02-21 06:12:59 +00:00
|
|
|
if (IS_ENABLED(CONFIG_RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) {
|
|
|
|
/* don't rely on fdt properties */
|
|
|
|
ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-21 06:12:55 +00:00
|
|
|
if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
|
|
|
|
const void *blob = info->fdt_blob;
|
|
|
|
int ndepth, noffset;
|
|
|
|
int sig_node, node;
|
|
|
|
char name[100];
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2020-02-21 06:12:55 +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;
|
|
|
|
}
|
2013-06-13 22:10:02 +00:00
|
|
|
|
2020-02-21 06:12:55 +00:00
|
|
|
/* See if we must use a particular key */
|
|
|
|
if (info->required_keynode != -1) {
|
2013-06-13 22:10:02 +00:00
|
|
|
ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
|
2020-02-21 06:12:55 +00:00
|
|
|
info->required_keynode);
|
|
|
|
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;
|
|
|
|
}
|
2013-06-13 22:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|