diff options
author | 2017-04-25 03:31:15 -0700 | |
---|---|---|
committer | 2017-05-22 06:48:08 +0000 | |
commit | bb756eebdac6fd24e8919e2c43f7d2c8c4091f59 (patch) | |
tree | ca11e03542edf2d8f631efeca5e1626d211107e3 /qemu/roms/u-boot/lib/rsa | |
parent | a14b48d18a9ed03ec191cf16b162206998a895ce (diff) |
Adding qemu as a submodule of KVMFORNFV
This Patch includes the changes to add qemu as a submodule to
kvmfornfv repo and make use of the updated latest qemu for the
execution of all testcase
Change-Id: I1280af507a857675c7f81d30c95255635667bdd7
Signed-off-by:RajithaY<rajithax.yerrumsetty@intel.com>
Diffstat (limited to 'qemu/roms/u-boot/lib/rsa')
-rw-r--r-- | qemu/roms/u-boot/lib/rsa/Makefile | 10 | ||||
-rw-r--r-- | qemu/roms/u-boot/lib/rsa/rsa-checksum.c | 163 | ||||
-rw-r--r-- | qemu/roms/u-boot/lib/rsa/rsa-sign.c | 448 | ||||
-rw-r--r-- | qemu/roms/u-boot/lib/rsa/rsa-verify.c | 339 |
4 files changed, 0 insertions, 960 deletions
diff --git a/qemu/roms/u-boot/lib/rsa/Makefile b/qemu/roms/u-boot/lib/rsa/Makefile deleted file mode 100644 index a5a96cb68..000000000 --- a/qemu/roms/u-boot/lib/rsa/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -# -# Copyright (c) 2013, Google Inc. -# -# (C) Copyright 2000-2007 -# Wolfgang Denk, DENX Software Engineering, wd@denx.de. -# -# SPDX-License-Identifier: GPL-2.0+ -# - -obj-$(CONFIG_FIT_SIGNATURE) += rsa-verify.o rsa-checksum.o diff --git a/qemu/roms/u-boot/lib/rsa/rsa-checksum.c b/qemu/roms/u-boot/lib/rsa/rsa-checksum.c deleted file mode 100644 index 32d6602e9..000000000 --- a/qemu/roms/u-boot/lib/rsa/rsa-checksum.c +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (c) 2013, Andreas Oetken. - * - * SPDX-License-Identifier: GPL-2.0+ - */ - -#ifndef USE_HOSTCC -#include <common.h> -#include <fdtdec.h> -#include <asm/byteorder.h> -#include <asm/errno.h> -#include <asm/unaligned.h> -#else -#include "fdt_host.h" -#endif -#include <rsa.h> -#include <sha1.h> -#include <sha256.h> - -/* PKCS 1.5 paddings as described in the RSA PKCS#1 v2.1 standard. */ - -const uint8_t padding_sha256_rsa2048[RSA2048_BYTES - SHA256_SUM_LEN] = { -0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, -0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x31, 0x30, -0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, -0x00, 0x04, 0x20 -}; - -const uint8_t padding_sha1_rsa2048[RSA2048_BYTES - SHA1_SUM_LEN] = { - 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x21, 0x30, - 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, - 0x05, 0x00, 0x04, 0x14 -}; - -const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = { - 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x31, 0x30, - 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, - 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 -}; - -void sha1_calculate(const struct image_region region[], int region_count, - uint8_t *checksum) -{ - sha1_context ctx; - uint32_t i; - i = 0; - - sha1_starts(&ctx); - for (i = 0; i < region_count; i++) - sha1_update(&ctx, region[i].data, region[i].size); - sha1_finish(&ctx, checksum); -} - -void sha256_calculate(const struct image_region region[], int region_count, - uint8_t *checksum) -{ - sha256_context ctx; - uint32_t i; - i = 0; - - sha256_starts(&ctx); - for (i = 0; i < region_count; i++) - sha256_update(&ctx, region[i].data, region[i].size); - sha256_finish(&ctx, checksum); -} diff --git a/qemu/roms/u-boot/lib/rsa/rsa-sign.c b/qemu/roms/u-boot/lib/rsa/rsa-sign.c deleted file mode 100644 index ca8c120d9..000000000 --- a/qemu/roms/u-boot/lib/rsa/rsa-sign.c +++ /dev/null @@ -1,448 +0,0 @@ -/* - * Copyright (c) 2013, Google Inc. - * - * SPDX-License-Identifier: GPL-2.0+ - */ - -#include "mkimage.h" -#include <stdio.h> -#include <string.h> -#include <image.h> -#include <time.h> -#include <openssl/rsa.h> -#include <openssl/pem.h> -#include <openssl/err.h> -#include <openssl/ssl.h> -#include <openssl/evp.h> - -#if OPENSSL_VERSION_NUMBER >= 0x10000000L -#define HAVE_ERR_REMOVE_THREAD_STATE -#endif - -static int rsa_err(const char *msg) -{ - unsigned long sslErr = ERR_get_error(); - - fprintf(stderr, "%s", msg); - fprintf(stderr, ": %s\n", - ERR_error_string(sslErr, 0)); - - return -1; -} - -/** - * rsa_get_pub_key() - read a public key from a .crt file - * - * @keydir: Directory containins the key - * @name Name of key file (will have a .crt extension) - * @rsap Returns RSA object, or NULL on failure - * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) - */ -static int rsa_get_pub_key(const char *keydir, const char *name, RSA **rsap) -{ - char path[1024]; - EVP_PKEY *key; - X509 *cert; - RSA *rsa; - FILE *f; - int ret; - - *rsap = NULL; - snprintf(path, sizeof(path), "%s/%s.crt", keydir, name); - f = fopen(path, "r"); - if (!f) { - fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", - path, strerror(errno)); - return -EACCES; - } - - /* Read the certificate */ - cert = NULL; - if (!PEM_read_X509(f, &cert, NULL, NULL)) { - rsa_err("Couldn't read certificate"); - ret = -EINVAL; - goto err_cert; - } - - /* Get the public key from the certificate. */ - key = X509_get_pubkey(cert); - if (!key) { - rsa_err("Couldn't read public key\n"); - ret = -EINVAL; - goto err_pubkey; - } - - /* Convert to a RSA_style key. */ - rsa = EVP_PKEY_get1_RSA(key); - if (!rsa) { - rsa_err("Couldn't convert to a RSA style key"); - goto err_rsa; - } - fclose(f); - EVP_PKEY_free(key); - X509_free(cert); - *rsap = rsa; - - return 0; - -err_rsa: - EVP_PKEY_free(key); -err_pubkey: - X509_free(cert); -err_cert: - fclose(f); - return ret; -} - -/** - * rsa_get_priv_key() - read a private key from a .key file - * - * @keydir: Directory containins the key - * @name Name of key file (will have a .key extension) - * @rsap Returns RSA object, or NULL on failure - * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) - */ -static int rsa_get_priv_key(const char *keydir, const char *name, RSA **rsap) -{ - char path[1024]; - RSA *rsa; - FILE *f; - - *rsap = NULL; - snprintf(path, sizeof(path), "%s/%s.key", keydir, name); - f = fopen(path, "r"); - if (!f) { - fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n", - path, strerror(errno)); - return -ENOENT; - } - - rsa = PEM_read_RSAPrivateKey(f, 0, NULL, path); - if (!rsa) { - rsa_err("Failure reading private key"); - fclose(f); - return -EPROTO; - } - fclose(f); - *rsap = rsa; - - return 0; -} - -static int rsa_init(void) -{ - int ret; - - ret = SSL_library_init(); - if (!ret) { - fprintf(stderr, "Failure to init SSL library\n"); - return -1; - } - SSL_load_error_strings(); - - OpenSSL_add_all_algorithms(); - OpenSSL_add_all_digests(); - OpenSSL_add_all_ciphers(); - - return 0; -} - -static void rsa_remove(void) -{ - CRYPTO_cleanup_all_ex_data(); - ERR_free_strings(); -#ifdef HAVE_ERR_REMOVE_THREAD_STATE - ERR_remove_thread_state(NULL); -#else - ERR_remove_state(0); -#endif - EVP_cleanup(); -} - -static int rsa_sign_with_key(RSA *rsa, struct checksum_algo *checksum_algo, - const struct image_region region[], int region_count, - uint8_t **sigp, uint *sig_size) -{ - EVP_PKEY *key; - EVP_MD_CTX *context; - int size, ret = 0; - uint8_t *sig; - int i; - - key = EVP_PKEY_new(); - if (!key) - return rsa_err("EVP_PKEY object creation failed"); - - if (!EVP_PKEY_set1_RSA(key, rsa)) { - ret = rsa_err("EVP key setup failed"); - goto err_set; - } - - size = EVP_PKEY_size(key); - sig = malloc(size); - if (!sig) { - fprintf(stderr, "Out of memory for signature (%d bytes)\n", - size); - ret = -ENOMEM; - goto err_alloc; - } - - context = EVP_MD_CTX_create(); - if (!context) { - ret = rsa_err("EVP context creation failed"); - goto err_create; - } - EVP_MD_CTX_init(context); - if (!EVP_SignInit(context, checksum_algo->calculate_sign())) { - ret = rsa_err("Signer setup failed"); - goto err_sign; - } - - for (i = 0; i < region_count; i++) { - if (!EVP_SignUpdate(context, region[i].data, region[i].size)) { - ret = rsa_err("Signing data failed"); - goto err_sign; - } - } - - if (!EVP_SignFinal(context, sig, sig_size, key)) { - ret = rsa_err("Could not obtain signature"); - goto err_sign; - } - EVP_MD_CTX_cleanup(context); - EVP_MD_CTX_destroy(context); - EVP_PKEY_free(key); - - debug("Got signature: %d bytes, expected %d\n", *sig_size, size); - *sigp = sig; - *sig_size = size; - - return 0; - -err_sign: - EVP_MD_CTX_destroy(context); -err_create: - free(sig); -err_alloc: -err_set: - EVP_PKEY_free(key); - return ret; -} - -int rsa_sign(struct image_sign_info *info, - const struct image_region region[], int region_count, - uint8_t **sigp, uint *sig_len) -{ - RSA *rsa; - int ret; - - ret = rsa_init(); - if (ret) - return ret; - - ret = rsa_get_priv_key(info->keydir, info->keyname, &rsa); - if (ret) - goto err_priv; - ret = rsa_sign_with_key(rsa, info->algo->checksum, region, - region_count, sigp, sig_len); - if (ret) - goto err_sign; - - RSA_free(rsa); - rsa_remove(); - - return ret; - -err_sign: - RSA_free(rsa); -err_priv: - rsa_remove(); - return ret; -} - -/* - * rsa_get_params(): - Get the important parameters of an RSA public key - */ -int rsa_get_params(RSA *key, uint32_t *n0_invp, BIGNUM **modulusp, - BIGNUM **r_squaredp) -{ - BIGNUM *big1, *big2, *big32, *big2_32; - BIGNUM *n, *r, *r_squared, *tmp; - BN_CTX *bn_ctx = BN_CTX_new(); - int ret = 0; - - /* Initialize BIGNUMs */ - big1 = BN_new(); - big2 = BN_new(); - big32 = BN_new(); - r = BN_new(); - r_squared = BN_new(); - tmp = BN_new(); - big2_32 = BN_new(); - n = BN_new(); - if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 || - !n) { - fprintf(stderr, "Out of memory (bignum)\n"); - return -ENOMEM; - } - - if (!BN_copy(n, key->n) || !BN_set_word(big1, 1L) || - !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L)) - ret = -1; - - /* big2_32 = 2^32 */ - if (!BN_exp(big2_32, big2, big32, bn_ctx)) - ret = -1; - - /* Calculate n0_inv = -1 / n[0] mod 2^32 */ - if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) || - !BN_sub(tmp, big2_32, tmp)) - ret = -1; - *n0_invp = BN_get_word(tmp); - - /* Calculate R = 2^(# of key bits) */ - if (!BN_set_word(tmp, BN_num_bits(n)) || - !BN_exp(r, big2, tmp, bn_ctx)) - ret = -1; - - /* Calculate r_squared = R^2 mod n */ - if (!BN_copy(r_squared, r) || - !BN_mul(tmp, r_squared, r, bn_ctx) || - !BN_mod(r_squared, tmp, n, bn_ctx)) - ret = -1; - - *modulusp = n; - *r_squaredp = r_squared; - - BN_free(big1); - BN_free(big2); - BN_free(big32); - BN_free(r); - BN_free(tmp); - BN_free(big2_32); - if (ret) { - fprintf(stderr, "Bignum operations failed\n"); - return -ENOMEM; - } - - return ret; -} - -static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, - BIGNUM *num, int num_bits) -{ - int nwords = num_bits / 32; - int size; - uint32_t *buf, *ptr; - BIGNUM *tmp, *big2, *big32, *big2_32; - BN_CTX *ctx; - int ret; - - tmp = BN_new(); - big2 = BN_new(); - big32 = BN_new(); - big2_32 = BN_new(); - if (!tmp || !big2 || !big32 || !big2_32) { - fprintf(stderr, "Out of memory (bignum)\n"); - return -ENOMEM; - } - ctx = BN_CTX_new(); - if (!tmp) { - fprintf(stderr, "Out of memory (bignum context)\n"); - return -ENOMEM; - } - BN_set_word(big2, 2L); - BN_set_word(big32, 32L); - BN_exp(big2_32, big2, big32, ctx); /* B = 2^32 */ - - size = nwords * sizeof(uint32_t); - buf = malloc(size); - if (!buf) { - fprintf(stderr, "Out of memory (%d bytes)\n", size); - return -ENOMEM; - } - - /* Write out modulus as big endian array of integers */ - for (ptr = buf + nwords - 1; ptr >= buf; ptr--) { - BN_mod(tmp, num, big2_32, ctx); /* n = N mod B */ - *ptr = cpu_to_fdt32(BN_get_word(tmp)); - BN_rshift(num, num, 32); /* N = N/B */ - } - - ret = fdt_setprop(blob, noffset, prop_name, buf, size); - if (ret) { - fprintf(stderr, "Failed to write public key to FIT\n"); - return -ENOSPC; - } - free(buf); - BN_free(tmp); - BN_free(big2); - BN_free(big32); - BN_free(big2_32); - - return ret; -} - -int rsa_add_verify_data(struct image_sign_info *info, void *keydest) -{ - BIGNUM *modulus, *r_squared; - uint32_t n0_inv; - int parent, node; - char name[100]; - int ret; - int bits; - RSA *rsa; - - debug("%s: Getting verification data\n", __func__); - ret = rsa_get_pub_key(info->keydir, info->keyname, &rsa); - if (ret) - return ret; - ret = rsa_get_params(rsa, &n0_inv, &modulus, &r_squared); - if (ret) - return ret; - bits = BN_num_bits(modulus); - parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME); - if (parent == -FDT_ERR_NOTFOUND) { - parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME); - if (parent < 0) { - fprintf(stderr, "Couldn't create signature node: %s\n", - fdt_strerror(parent)); - return -EINVAL; - } - } - - /* Either create or overwrite the named key node */ - snprintf(name, sizeof(name), "key-%s", info->keyname); - node = fdt_subnode_offset(keydest, parent, name); - if (node == -FDT_ERR_NOTFOUND) { - node = fdt_add_subnode(keydest, parent, name); - if (node < 0) { - fprintf(stderr, "Could not create key subnode: %s\n", - fdt_strerror(node)); - return -EINVAL; - } - } else if (node < 0) { - fprintf(stderr, "Cannot select keys parent: %s\n", - fdt_strerror(node)); - return -ENOSPC; - } - - ret = fdt_setprop_string(keydest, node, "key-name-hint", - info->keyname); - ret |= fdt_setprop_u32(keydest, node, "rsa,num-bits", bits); - ret |= fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv); - ret |= fdt_add_bignum(keydest, node, "rsa,modulus", modulus, bits); - ret |= fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared, bits); - ret |= fdt_setprop_string(keydest, node, FIT_ALGO_PROP, - info->algo->name); - if (info->require_keys) { - fdt_setprop_string(keydest, node, "required", - info->require_keys); - } - BN_free(modulus); - BN_free(r_squared); - if (ret) - return -EIO; - - return 0; -} diff --git a/qemu/roms/u-boot/lib/rsa/rsa-verify.c b/qemu/roms/u-boot/lib/rsa/rsa-verify.c deleted file mode 100644 index 587da5b47..000000000 --- a/qemu/roms/u-boot/lib/rsa/rsa-verify.c +++ /dev/null @@ -1,339 +0,0 @@ -/* - * Copyright (c) 2013, Google Inc. - * - * SPDX-License-Identifier: GPL-2.0+ - */ - -#ifndef USE_HOSTCC -#include <common.h> -#include <fdtdec.h> -#include <asm/types.h> -#include <asm/byteorder.h> -#include <asm/errno.h> -#include <asm/types.h> -#include <asm/unaligned.h> -#else -#include "fdt_host.h" -#include "mkimage.h" -#include <fdt_support.h> -#endif -#include <rsa.h> -#include <sha1.h> -#include <sha256.h> - -#define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby))) - -#define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a) -#define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a)) - -/** - * subtract_modulus() - subtract modulus from the given value - * - * @key: Key containing modulus to subtract - * @num: Number to subtract modulus from, as little endian word array - */ -static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[]) -{ - int64_t acc = 0; - uint i; - - for (i = 0; i < key->len; i++) { - acc += (uint64_t)num[i] - key->modulus[i]; - num[i] = (uint32_t)acc; - acc >>= 32; - } -} - -/** - * greater_equal_modulus() - check if a value is >= modulus - * - * @key: Key containing modulus to check - * @num: Number to check against modulus, as little endian word array - * @return 0 if num < modulus, 1 if num >= modulus - */ -static int greater_equal_modulus(const struct rsa_public_key *key, - uint32_t num[]) -{ - uint32_t i; - - for (i = key->len - 1; i >= 0; i--) { - if (num[i] < key->modulus[i]) - return 0; - if (num[i] > key->modulus[i]) - return 1; - } - - return 1; /* equal */ -} - -/** - * montgomery_mul_add_step() - Perform montgomery multiply-add step - * - * Operation: montgomery result[] += a * b[] / n0inv % modulus - * - * @key: RSA key - * @result: Place to put result, as little endian word array - * @a: Multiplier - * @b: Multiplicand, as little endian word array - */ -static void montgomery_mul_add_step(const struct rsa_public_key *key, - uint32_t result[], const uint32_t a, const uint32_t b[]) -{ - uint64_t acc_a, acc_b; - uint32_t d0; - uint i; - - acc_a = (uint64_t)a * b[0] + result[0]; - d0 = (uint32_t)acc_a * key->n0inv; - acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a; - for (i = 1; i < key->len; i++) { - acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i]; - acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] + - (uint32_t)acc_a; - result[i - 1] = (uint32_t)acc_b; - } - - acc_a = (acc_a >> 32) + (acc_b >> 32); - - result[i - 1] = (uint32_t)acc_a; - - if (acc_a >> 32) - subtract_modulus(key, result); -} - -/** - * montgomery_mul() - Perform montgomery mutitply - * - * Operation: montgomery result[] = a[] * b[] / n0inv % modulus - * - * @key: RSA key - * @result: Place to put result, as little endian word array - * @a: Multiplier, as little endian word array - * @b: Multiplicand, as little endian word array - */ -static void montgomery_mul(const struct rsa_public_key *key, - uint32_t result[], uint32_t a[], const uint32_t b[]) -{ - uint i; - - for (i = 0; i < key->len; ++i) - result[i] = 0; - for (i = 0; i < key->len; ++i) - montgomery_mul_add_step(key, result, a[i], b); -} - -/** - * pow_mod() - in-place public exponentiation - * - * @key: RSA key - * @inout: Big-endian word array containing value and result - */ -static int pow_mod(const struct rsa_public_key *key, uint32_t *inout) -{ - uint32_t *result, *ptr; - uint i; - - /* Sanity check for stack size - key->len is in 32-bit words */ - if (key->len > RSA_MAX_KEY_BITS / 32) { - debug("RSA key words %u exceeds maximum %d\n", key->len, - RSA_MAX_KEY_BITS / 32); - return -EINVAL; - } - - uint32_t val[key->len], acc[key->len], tmp[key->len]; - result = tmp; /* Re-use location. */ - - /* Convert from big endian byte array to little endian word array. */ - for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--) - val[i] = get_unaligned_be32(ptr); - - montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */ - for (i = 0; i < 16; i += 2) { - montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */ - montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */ - } - montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */ - - /* Make sure result < mod; result is at most 1x mod too large. */ - if (greater_equal_modulus(key, result)) - subtract_modulus(key, result); - - /* Convert to bigendian byte array */ - for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++) - put_unaligned_be32(result[i], ptr); - return 0; -} - -static int rsa_verify_key(const struct rsa_public_key *key, const uint8_t *sig, - const uint32_t sig_len, const uint8_t *hash, - struct checksum_algo *algo) -{ - const uint8_t *padding; - int pad_len; - int ret; - - if (!key || !sig || !hash || !algo) - return -EIO; - - if (sig_len != (key->len * sizeof(uint32_t))) { - debug("Signature is of incorrect length %d\n", sig_len); - return -EINVAL; - } - - debug("Checksum algorithm: %s", algo->name); - - /* 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; - } - - uint32_t buf[sig_len / sizeof(uint32_t)]; - - memcpy(buf, sig, sig_len); - - ret = pow_mod(key, buf); - if (ret) - return ret; - - padding = algo->rsa_padding; - pad_len = algo->pad_len - algo->checksum_len; - - /* Check pkcs1.5 padding bytes. */ - if (memcmp(buf, padding, pad_len)) { - 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; -} - -static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len) -{ - int i; - - for (i = 0; i < len; i++) - dst[i] = fdt32_to_cpu(src[len - 1 - i]); -} - -static int rsa_verify_with_keynode(struct image_sign_info *info, - const void *hash, uint8_t *sig, uint sig_len, int node) -{ - const void *blob = info->fdt_blob; - struct rsa_public_key key; - const void *modulus, *rr; - int ret; - - if (node < 0) { - debug("%s: Skipping invalid node", __func__); - return -EBADF; - } - if (!fdt_getprop(blob, node, "rsa,n0-inverse", NULL)) { - debug("%s: Missing rsa,n0-inverse", __func__); - return -EFAULT; - } - key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0); - key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); - modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); - rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); - if (!key.len || !modulus || !rr) { - debug("%s: Missing RSA key info", __func__); - return -EFAULT; - } - - /* Sanity check for stack size */ - if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) { - debug("RSA key bits %u outside allowed range %d..%d\n", - key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS); - return -EFAULT; - } - key.len /= sizeof(uint32_t) * 8; - uint32_t key1[key.len], key2[key.len]; - - key.modulus = key1; - key.rr = key2; - rsa_convert_big_endian(key.modulus, modulus, key.len); - rsa_convert_big_endian(key.rr, rr, key.len); - if (!key.modulus || !key.rr) { - debug("%s: Out of memory", __func__); - return -ENOMEM; - } - - debug("key length %d\n", key.len); - ret = rsa_verify_key(&key, sig, sig_len, hash, info->algo->checksum); - if (ret) { - printf("%s: RSA failed to verify: %d\n", __func__, ret); - return ret; - } - - return 0; -} - -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; - /* Reserve memory for maximum checksum-length */ - uint8_t hash[info->algo->checksum->pad_len]; - int ndepth, noffset; - int sig_node, node; - char name[100]; - int ret; - - /* - * Verify that the checksum-length does not exceed the - * rsa-signature-length - */ - if (info->algo->checksum->checksum_len > - info->algo->checksum->pad_len) { - debug("%s: invlaid checksum-algorithm %s for %s\n", - __func__, info->algo->checksum->name, info->algo->name); - return -EINVAL; - } - - sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); - if (sig_node < 0) { - debug("%s: No signature node found\n", __func__); - return -ENOENT; - } - - /* Calculate checksum with checksum-algorithm */ - info->algo->checksum->calculate(region, region_count, hash); - - /* 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; -} |