arm: Remove aspenite board
[platform/kernel/u-boot.git] / common / image-sig.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013, Google Inc.
4  */
5
6 #include <common.h>
7 #include <log.h>
8 #include <malloc.h>
9 #include <asm/global_data.h>
10 DECLARE_GLOBAL_DATA_PTR;
11 #include <image.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         {
20                 .name = "sha1",
21                 .checksum_len = SHA1_SUM_LEN,
22                 .der_len = SHA1_DER_LEN,
23                 .der_prefix = sha1_der_prefix,
24                 .calculate = hash_calculate,
25         },
26         {
27                 .name = "sha256",
28                 .checksum_len = SHA256_SUM_LEN,
29                 .der_len = SHA256_DER_LEN,
30                 .der_prefix = sha256_der_prefix,
31                 .calculate = hash_calculate,
32         },
33 #ifdef CONFIG_SHA384
34         {
35                 .name = "sha384",
36                 .checksum_len = SHA384_SUM_LEN,
37                 .der_len = SHA384_DER_LEN,
38                 .der_prefix = sha384_der_prefix,
39                 .calculate = hash_calculate,
40         },
41 #endif
42 #ifdef CONFIG_SHA512
43         {
44                 .name = "sha512",
45                 .checksum_len = SHA512_SUM_LEN,
46                 .der_len = SHA512_DER_LEN,
47                 .der_prefix = sha512_der_prefix,
48                 .calculate = hash_calculate,
49         },
50 #endif
51
52 };
53
54 struct checksum_algo *image_get_checksum_algo(const char *full_name)
55 {
56         int i;
57         const char *name;
58
59 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
60         static bool done;
61
62         if (!done) {
63                 done = true;
64                 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
65                         checksum_algos[i].name += gd->reloc_off;
66                         checksum_algos[i].calculate += gd->reloc_off;
67                 }
68         }
69 #endif
70
71         for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
72                 name = checksum_algos[i].name;
73                 /* Make sure names match and next char is a comma */
74                 if (!strncmp(name, full_name, strlen(name)) &&
75                     full_name[strlen(name)] == ',')
76                         return &checksum_algos[i];
77         }
78
79         return NULL;
80 }
81
82 struct crypto_algo *image_get_crypto_algo(const char *full_name)
83 {
84         struct crypto_algo *crypto, *end;
85         const char *name;
86
87 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
88         static bool done;
89
90         if (!done) {
91                 crypto = ll_entry_start(struct crypto_algo, cryptos);
92                 end = ll_entry_end(struct crypto_algo, cryptos);
93                 for (; crypto < end; crypto++) {
94                         crypto->name += gd->reloc_off;
95                         crypto->verify += gd->reloc_off;
96                 }
97         }
98 #endif
99
100         /* Move name to after the comma */
101         name = strchr(full_name, ',');
102         if (!name)
103                 return NULL;
104         name += 1;
105
106         crypto = ll_entry_start(struct crypto_algo, cryptos);
107         end = ll_entry_end(struct crypto_algo, cryptos);
108         for (; crypto < end; crypto++) {
109                 if (!strcmp(crypto->name, name))
110                         return crypto;
111         }
112
113         /* Not found */
114         return NULL;
115 }
116
117 struct padding_algo *image_get_padding_algo(const char *name)
118 {
119         struct padding_algo *padding, *end;
120
121         if (!name)
122                 return NULL;
123
124         padding = ll_entry_start(struct padding_algo, paddings);
125         end = ll_entry_end(struct padding_algo, paddings);
126         for (; padding < end; padding++) {
127                 if (!strcmp(padding->name, name))
128                         return padding;
129         }
130
131         return NULL;
132 }