blob: 0f8e592aba7dbfbedb96ebcc2cd78366dff067b6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3e569a62013-06-13 15:10:00 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass3e569a62013-06-13 15:10:00 -07004 */
5
6#ifdef USE_HOSTCC
7#include "mkimage.h"
Simon Glass4d72caa2020-05-10 11:40:01 -06008#include <fdt_support.h>
Simon Glass3e569a62013-06-13 15:10:00 -07009#include <time.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060010#include <linux/libfdt.h>
Simon Glass3e569a62013-06-13 15:10:00 -070011#else
12#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass56518e72013-06-13 15:10:01 -070014#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glass56518e72013-06-13 15:10:01 -070016DECLARE_GLOBAL_DATA_PTR;
Simon Glass3e569a62013-06-13 15:10:00 -070017#endif /* !USE_HOSTCC*/
Simon Glass3e569a62013-06-13 15:10:00 -070018#include <image.h>
Alexandru Gagniuced6c9e02021-02-19 12:45:12 -060019#include <u-boot/ecdsa.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020020#include <u-boot/rsa.h>
Alexandru Gagniuc0bcb28d2021-02-19 12:45:10 -060021#include <u-boot/hash-checksum.h>
Simon Glass3e569a62013-06-13 15:10:00 -070022
Simon Glass4d098522013-06-13 15:10:09 -070023#define IMAGE_MAX_HASHED_NODES 100
24
Heiko Schocher646257d2014-03-03 12:19:26 +010025struct checksum_algo checksum_algos[] = {
26 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090027 .name = "sha1",
28 .checksum_len = SHA1_SUM_LEN,
29 .der_len = SHA1_DER_LEN,
30 .der_prefix = sha1_der_prefix,
Heiko Schocher646257d2014-03-03 12:19:26 +010031#if IMAGE_ENABLE_SIGN
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090032 .calculate_sign = EVP_sha1,
Heiko Schocher29a23f92014-03-03 12:19:30 +010033#endif
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090034 .calculate = hash_calculate,
Heiko Schocher646257d2014-03-03 12:19:26 +010035 },
36 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090037 .name = "sha256",
38 .checksum_len = SHA256_SUM_LEN,
39 .der_len = SHA256_DER_LEN,
40 .der_prefix = sha256_der_prefix,
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010041#if IMAGE_ENABLE_SIGN
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090042 .calculate_sign = EVP_sha256,
Heiko Schocher29a23f92014-03-03 12:19:30 +010043#endif
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090044 .calculate = hash_calculate,
Reuben Dowled16b38f2020-04-16 17:36:52 +120045 },
46#ifdef CONFIG_SHA384
47 {
48 .name = "sha384",
49 .checksum_len = SHA384_SUM_LEN,
50 .der_len = SHA384_DER_LEN,
51 .der_prefix = sha384_der_prefix,
52#if IMAGE_ENABLE_SIGN
53 .calculate_sign = EVP_sha384,
54#endif
55 .calculate = hash_calculate,
56 },
57#endif
58#ifdef CONFIG_SHA512
59 {
60 .name = "sha512",
61 .checksum_len = SHA512_SUM_LEN,
62 .der_len = SHA512_DER_LEN,
63 .der_prefix = sha512_der_prefix,
64#if IMAGE_ENABLE_SIGN
65 .calculate_sign = EVP_sha512,
66#endif
67 .calculate = hash_calculate,
68 },
69#endif
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010070
Heiko Schocher646257d2014-03-03 12:19:26 +010071};
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010072
Andrew Duda0c1d74f2016-11-08 18:53:41 +000073struct crypto_algo crypto_algos[] = {
Simon Glass19c402a2013-06-13 15:10:02 -070074 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090075 .name = "rsa2048",
76 .key_len = RSA2048_BYTES,
77 .sign = rsa_sign,
78 .add_verify_data = rsa_add_verify_data,
79 .verify = rsa_verify,
Andrew Duda0c1d74f2016-11-08 18:53:41 +000080 },
81 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090082 .name = "rsa4096",
83 .key_len = RSA4096_BYTES,
84 .sign = rsa_sign,
85 .add_verify_data = rsa_add_verify_data,
86 .verify = rsa_verify,
Alexandru Gagniuced6c9e02021-02-19 12:45:12 -060087 },
88 {
89 .name = "ecdsa256",
90 .key_len = ECDSA256_BYTES,
91 .sign = ecdsa_sign,
92 .add_verify_data = ecdsa_add_verify_data,
93 .verify = ecdsa_verify,
94 },
Andrew Duda0c1d74f2016-11-08 18:53:41 +000095};
96
Philippe Reynes20031562018-11-14 13:51:00 +010097struct padding_algo padding_algos[] = {
98 {
99 .name = "pkcs-1.5",
100 .verify = padding_pkcs_15_verify,
101 },
Philippe Reynes061daa02018-11-14 13:51:01 +0100102#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
103 {
104 .name = "pss",
105 .verify = padding_pss_verify,
106 }
107#endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
Philippe Reynes20031562018-11-14 13:51:00 +0100108};
109
Andrew Duda83dd98e2016-11-08 18:53:41 +0000110struct checksum_algo *image_get_checksum_algo(const char *full_name)
Simon Glass3e569a62013-06-13 15:10:00 -0700111{
112 int i;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000113 const char *name;
Simon Glass3e569a62013-06-13 15:10:00 -0700114
T Karthik Reddy1ed8c132019-03-16 15:23:03 +0530115#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
116 static bool done;
117
118 if (!done) {
119 done = true;
120 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
121 checksum_algos[i].name += gd->reloc_off;
122#if IMAGE_ENABLE_SIGN
123 checksum_algos[i].calculate_sign += gd->reloc_off;
124#endif
125 checksum_algos[i].calculate += gd->reloc_off;
126 }
127 }
128#endif
129
Andrew Duda83dd98e2016-11-08 18:53:41 +0000130 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
131 name = checksum_algos[i].name;
132 /* Make sure names match and next char is a comma */
133 if (!strncmp(name, full_name, strlen(name)) &&
134 full_name[strlen(name)] == ',')
135 return &checksum_algos[i];
136 }
137
138 return NULL;
139}
140
141struct crypto_algo *image_get_crypto_algo(const char *full_name)
142{
143 int i;
144 const char *name;
145
T Karthik Reddy1ed8c132019-03-16 15:23:03 +0530146#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
147 static bool done;
148
149 if (!done) {
150 done = true;
151 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
152 crypto_algos[i].name += gd->reloc_off;
153 crypto_algos[i].sign += gd->reloc_off;
154 crypto_algos[i].add_verify_data += gd->reloc_off;
155 crypto_algos[i].verify += gd->reloc_off;
156 }
157 }
158#endif
159
Andrew Duda83dd98e2016-11-08 18:53:41 +0000160 /* Move name to after the comma */
161 name = strchr(full_name, ',');
162 if (!name)
163 return NULL;
164 name += 1;
165
166 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
167 if (!strcmp(crypto_algos[i].name, name))
168 return &crypto_algos[i];
Simon Glass3e569a62013-06-13 15:10:00 -0700169 }
170
171 return NULL;
172}
Simon Glass56518e72013-06-13 15:10:01 -0700173
Philippe Reynes20031562018-11-14 13:51:00 +0100174struct padding_algo *image_get_padding_algo(const char *name)
175{
176 int i;
177
178 if (!name)
179 return NULL;
180
181 for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
182 if (!strcmp(padding_algos[i].name, name))
183 return &padding_algos[i];
184 }
185
186 return NULL;
187}