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 hash_calculate_sw(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 int hash_calculate(const char *name, 57 const struct image_region region[], 58 int region_count, uint8_t *checksum) 59 { 60 return hash_calculate_sw(name, region, region_count, checksum); 61 } 62 #else 63 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 64 int hash_calculate(const char *name, 65 const struct image_region region[], 66 int region_count, uint8_t *checksum) 67 68 { 69 struct udevice *dev; 70 sha_context ctx; 71 72 if (!name) 73 return -EINVAL; 74 75 if (!strcmp(name, "sha1")) 76 ctx.algo = CRYPTO_SHA1; 77 else if (!strcmp(name, "sha256")) 78 ctx.algo = CRYPTO_SHA256; 79 else 80 return -EPERM; 81 82 dev = crypto_get_device(ctx.algo); 83 if (!dev) { 84 printf("No crypto device for expected capability\n"); 85 return -ENODEV; 86 } 87 88 return crypto_sha_regions_csum(dev, &ctx, region, 89 region_count, checksum); 90 } 91 #else 92 int hash_calculate(const char *name, 93 const struct image_region region[], 94 int region_count, uint8_t *checksum) 95 { 96 return hash_calculate_sw(name, region, region_count, checksum); 97 } 98 #endif 99 #endif 100