5122dc948f0d61c5cf8a4536696de236edf3e268
[oweals/u-boot.git] / common / image-sig.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013, Google Inc.
4  */
5
6 #ifdef USE_HOSTCC
7 #include "mkimage.h"
8 #include <fdt_support.h>
9 #include <time.h>
10 #include <linux/libfdt.h>
11 #else
12 #include <common.h>
13 #include <malloc.h>
14 DECLARE_GLOBAL_DATA_PTR;
15 #endif /* !USE_HOSTCC*/
16 #include <image.h>
17 #include <u-boot/rsa.h>
18 #include <u-boot/rsa-checksum.h>
19
20 #define IMAGE_MAX_HASHED_NODES          100
21
22 struct checksum_algo checksum_algos[] = {
23         {
24                 .name = "sha1",
25                 .checksum_len = SHA1_SUM_LEN,
26                 .der_len = SHA1_DER_LEN,
27                 .der_prefix = sha1_der_prefix,
28 #if IMAGE_ENABLE_SIGN
29                 .calculate_sign = EVP_sha1,
30 #endif
31                 .calculate = hash_calculate,
32         },
33         {
34                 .name = "sha256",
35                 .checksum_len = SHA256_SUM_LEN,
36                 .der_len = SHA256_DER_LEN,
37                 .der_prefix = sha256_der_prefix,
38 #if IMAGE_ENABLE_SIGN
39                 .calculate_sign = EVP_sha256,
40 #endif
41                 .calculate = hash_calculate,
42         }
43
44 };
45
46 struct crypto_algo crypto_algos[] = {
47         {
48                 .name = "rsa2048",
49                 .key_len = RSA2048_BYTES,
50                 .sign = rsa_sign,
51                 .add_verify_data = rsa_add_verify_data,
52                 .verify = rsa_verify,
53         },
54         {
55                 .name = "rsa4096",
56                 .key_len = RSA4096_BYTES,
57                 .sign = rsa_sign,
58                 .add_verify_data = rsa_add_verify_data,
59                 .verify = rsa_verify,
60         }
61
62 };
63
64 struct padding_algo padding_algos[] = {
65         {
66                 .name = "pkcs-1.5",
67                 .verify = padding_pkcs_15_verify,
68         },
69 #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
70         {
71                 .name = "pss",
72                 .verify = padding_pss_verify,
73         }
74 #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
75 };
76
77 struct checksum_algo *image_get_checksum_algo(const char *full_name)
78 {
79         int i;
80         const char *name;
81
82 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
83         static bool done;
84
85         if (!done) {
86                 done = true;
87                 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
88                         checksum_algos[i].name += gd->reloc_off;
89 #if IMAGE_ENABLE_SIGN
90                         checksum_algos[i].calculate_sign += gd->reloc_off;
91 #endif
92                         checksum_algos[i].calculate += gd->reloc_off;
93                 }
94         }
95 #endif
96
97         for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
98                 name = checksum_algos[i].name;
99                 /* Make sure names match and next char is a comma */
100                 if (!strncmp(name, full_name, strlen(name)) &&
101                     full_name[strlen(name)] == ',')
102                         return &checksum_algos[i];
103         }
104
105         return NULL;
106 }
107
108 struct crypto_algo *image_get_crypto_algo(const char *full_name)
109 {
110         int i;
111         const char *name;
112
113 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
114         static bool done;
115
116         if (!done) {
117                 done = true;
118                 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
119                         crypto_algos[i].name += gd->reloc_off;
120                         crypto_algos[i].sign += gd->reloc_off;
121                         crypto_algos[i].add_verify_data += gd->reloc_off;
122                         crypto_algos[i].verify += gd->reloc_off;
123                 }
124         }
125 #endif
126
127         /* Move name to after the comma */
128         name = strchr(full_name, ',');
129         if (!name)
130                 return NULL;
131         name += 1;
132
133         for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
134                 if (!strcmp(crypto_algos[i].name, name))
135                         return &crypto_algos[i];
136         }
137
138         return NULL;
139 }
140
141 struct padding_algo *image_get_padding_algo(const char *name)
142 {
143         int i;
144
145         if (!name)
146                 return NULL;
147
148         for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
149                 if (!strcmp(padding_algos[i].name, name))
150                         return &padding_algos[i];
151         }
152
153         return NULL;
154 }