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