1 /* 2 * Copyright (c) 2013, Andreas Oetken. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #ifndef USE_HOSTCC 8 #include <common.h> 9 #include <crypto.h> 10 #include <fdtdec.h> 11 #include <asm/byteorder.h> 12 #include <linux/errno.h> 13 #include <asm/unaligned.h> 14 #include <hash.h> 15 #else 16 #include "fdt_host.h" 17 #endif 18 #include <u-boot/rsa.h> 19 20 int rsa_hash_calculate(const char *name, 21 const struct image_region region[], 22 int region_count, uint8_t *checksum) 23 { 24 struct hash_algo *algo; 25 int ret = 0; 26 void *ctx; 27 uint32_t i; 28 i = 0; 29 30 ret = hash_progressive_lookup_algo(name, &algo); 31 if (ret) 32 return ret; 33 34 ret = algo->hash_init(algo, &ctx); 35 if (ret) 36 return ret; 37 38 for (i = 0; i < region_count - 1; i++) { 39 ret = algo->hash_update(algo, ctx, region[i].data, 40 region[i].size, 0); 41 if (ret) 42 return ret; 43 } 44 45 ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1); 46 if (ret) 47 return ret; 48 ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size); 49 if (ret) 50 return ret; 51 52 return 0; 53 } 54 55 #if !defined(USE_HOSTCC) 56 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 57 int hw_rsa_hash_calculate(const char *name, 58 const struct image_region region[], 59 int region_count, uint8_t *checksum) 60 61 { 62 struct udevice *dev; 63 sha_context ctx; 64 65 if (!name) 66 return -EINVAL; 67 68 if (!strcmp(name, "sha1")) 69 ctx.algo = CRYPTO_SHA1; 70 else if (!strcmp(name, "sha256")) 71 ctx.algo = CRYPTO_SHA256; 72 else 73 return -EPERM; 74 75 dev = crypto_get_device(ctx.algo); 76 if (!dev) { 77 printf("No crypto device for expected capability\n"); 78 return -ENODEV; 79 } 80 81 return crypto_sha_regions_csum(dev, &ctx, region, 82 region_count, checksum); 83 } 84 #endif 85 #endif 86 87 int hash_calculate(const char *name, 88 const struct image_region region[], 89 int region_count, uint8_t *checksum) 90 { 91 #if defined(USE_HOSTCC) 92 return rsa_hash_calculate(name, region, region_count, checksum); 93 #else 94 #if !CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 95 return rsa_hash_calculate(name, region, region_count, checksum); 96 #else 97 return hw_rsa_hash_calculate(name, region, region_count, checksum); 98 #endif 99 #endif 100 } 101