1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2013, Google Inc.
4 */
5
6 #include <log.h>
7 #include <malloc.h>
8 #include <asm/global_data.h>
9 DECLARE_GLOBAL_DATA_PTR;
10 #include <image.h>
11 #include <relocate.h>
12 #include <u-boot/ecdsa.h>
13 #include <u-boot/rsa.h>
14 #include <u-boot/hash-checksum.h>
15
16 #define IMAGE_MAX_HASHED_NODES 100
17
18 struct checksum_algo checksum_algos[] = {
19 #if CONFIG_IS_ENABLED(SHA1)
20 {
21 .name = "sha1",
22 .checksum_len = SHA1_SUM_LEN,
23 .der_len = SHA1_DER_LEN,
24 .der_prefix = sha1_der_prefix,
25 .calculate = hash_calculate,
26 },
27 #endif
28 #if CONFIG_IS_ENABLED(SHA256)
29 {
30 .name = "sha256",
31 .checksum_len = SHA256_SUM_LEN,
32 .der_len = SHA256_DER_LEN,
33 .der_prefix = sha256_der_prefix,
34 .calculate = hash_calculate,
35 },
36 #endif
37 #if CONFIG_IS_ENABLED(SHA384)
38 {
39 .name = "sha384",
40 .checksum_len = SHA384_SUM_LEN,
41 .der_len = SHA384_DER_LEN,
42 .der_prefix = sha384_der_prefix,
43 .calculate = hash_calculate,
44 },
45 #endif
46 #if CONFIG_IS_ENABLED(SHA512)
47 {
48 .name = "sha512",
49 .checksum_len = SHA512_SUM_LEN,
50 .der_len = SHA512_DER_LEN,
51 .der_prefix = sha512_der_prefix,
52 .calculate = hash_calculate,
53 },
54 #endif
55
56 };
57
image_get_checksum_algo(const char * full_name)58 struct checksum_algo *image_get_checksum_algo(const char *full_name)
59 {
60 int i;
61 const char *name;
62
63 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
64 name = checksum_algos[i].name;
65 /* Make sure names match and next char is a comma */
66 if (!strncmp(name, full_name, strlen(name)) &&
67 full_name[strlen(name)] == ',')
68 return &checksum_algos[i];
69 }
70
71 return NULL;
72 }
73
image_get_crypto_algo(const char * full_name)74 struct crypto_algo *image_get_crypto_algo(const char *full_name)
75 {
76 struct crypto_algo *crypto, *end;
77 const char *name;
78
79 /* Move name to after the comma */
80 name = strchr(full_name, ',');
81 if (!name)
82 return NULL;
83 name += 1;
84
85 crypto = ll_entry_start(struct crypto_algo, cryptos);
86 end = ll_entry_end(struct crypto_algo, cryptos);
87 for (; crypto < end; crypto++) {
88 if (!strcmp(crypto->name, name))
89 return crypto;
90 }
91
92 /* Not found */
93 return NULL;
94 }
95
image_get_padding_algo(const char * name)96 struct padding_algo *image_get_padding_algo(const char *name)
97 {
98 struct padding_algo *padding, *end;
99
100 if (!name)
101 return NULL;
102
103 padding = ll_entry_start(struct padding_algo, paddings);
104 end = ll_entry_end(struct padding_algo, paddings);
105 for (; padding < end; padding++) {
106 if (!strcmp(padding->name, name))
107 return padding;
108 }
109
110 return NULL;
111 }
112