blob: 0f8e592aba7dbfbedb96ebcc2cd78366dff067b6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass58fe7e52013-06-13 15:10:00 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass58fe7e52013-06-13 15:10:00 -07004 */
5
6#ifdef USE_HOSTCC
7#include "mkimage.h"
Simon Glass2dc9c342020-05-10 11:40:01 -06008#include <fdt_support.h>
Simon Glass58fe7e52013-06-13 15:10:00 -07009#include <time.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060010#include <linux/libfdt.h>
Simon Glass58fe7e52013-06-13 15:10:00 -070011#else
12#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassfbabc0f2013-06-13 15:10:01 -070014#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassfbabc0f2013-06-13 15:10:01 -070016DECLARE_GLOBAL_DATA_PTR;
Simon Glass58fe7e52013-06-13 15:10:00 -070017#endif /* !USE_HOSTCC*/
Simon Glass58fe7e52013-06-13 15:10:00 -070018#include <image.h>
Alexandru Gagniuc3ebd2002021-02-19 12:45:12 -060019#include <u-boot/ecdsa.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020020#include <u-boot/rsa.h>
Alexandru Gagniucdb182c42021-02-19 12:45:10 -060021#include <u-boot/hash-checksum.h>
Simon Glass58fe7e52013-06-13 15:10:00 -070022
Simon Glass56ab8d62013-06-13 15:10:09 -070023#define IMAGE_MAX_HASHED_NODES 100
24
Heiko Schocheredaf9b12014-03-03 12:19:26 +010025struct checksum_algo checksum_algos[] = {
26 {
Masahiro Yamada79f3c592017-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 Schocheredaf9b12014-03-03 12:19:26 +010031#if IMAGE_ENABLE_SIGN
Masahiro Yamada79f3c592017-10-23 10:03:40 +090032 .calculate_sign = EVP_sha1,
Heiko Schocherd7b42322014-03-03 12:19:30 +010033#endif
Masahiro Yamada79f3c592017-10-23 10:03:40 +090034 .calculate = hash_calculate,
Heiko Schocheredaf9b12014-03-03 12:19:26 +010035 },
36 {
Masahiro Yamada79f3c592017-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 Schocheredaf9b12014-03-03 12:19:26 +010041#if IMAGE_ENABLE_SIGN
Masahiro Yamada79f3c592017-10-23 10:03:40 +090042 .calculate_sign = EVP_sha256,
Heiko Schocherd7b42322014-03-03 12:19:30 +010043#endif
Masahiro Yamada79f3c592017-10-23 10:03:40 +090044 .calculate = hash_calculate,
Reuben Dowle1908fd92020-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
Andrew Duda06ca6d62016-11-08 18:53:41 +000070
71};
72
73struct crypto_algo crypto_algos[] = {
74 {
Masahiro Yamada79f3c592017-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,
Heiko Schocher4b817562014-03-03 12:19:27 +010080 },
81 {
Masahiro Yamada79f3c592017-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 Gagniuc3ebd2002021-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 },
Heiko Schocheredaf9b12014-03-03 12:19:26 +010095};
Heiko Schocher4b817562014-03-03 12:19:27 +010096
Philippe Reynes12468352018-11-14 13:51:00 +010097struct padding_algo padding_algos[] = {
98 {
99 .name = "pkcs-1.5",
100 .verify = padding_pkcs_15_verify,
101 },
Philippe Reynes47d73f02018-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 Reynes12468352018-11-14 13:51:00 +0100108};
109
Andrew Duda6616c822016-11-08 18:53:41 +0000110struct checksum_algo *image_get_checksum_algo(const char *full_name)
111{
112 int i;
113 const char *name;
114
T Karthik Reddy0164dc82019-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 Duda6616c822016-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];
Simon Glass35191a32013-06-13 15:10:02 -0700136 }
Heiko Schocher4b817562014-03-03 12:19:27 +0100137
Andrew Duda6616c822016-11-08 18:53:41 +0000138 return NULL;
139}
Simon Glass58fe7e52013-06-13 15:10:00 -0700140
Andrew Duda6616c822016-11-08 18:53:41 +0000141struct crypto_algo *image_get_crypto_algo(const char *full_name)
Simon Glass58fe7e52013-06-13 15:10:00 -0700142{
143 int i;
Andrew Duda6616c822016-11-08 18:53:41 +0000144 const char *name;
145
T Karthik Reddy0164dc82019-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 Duda6616c822016-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;
Simon Glass58fe7e52013-06-13 15:10:00 -0700165
Andrew Duda6616c822016-11-08 18:53:41 +0000166 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
167 if (!strcmp(crypto_algos[i].name, name))
168 return &crypto_algos[i];
Simon Glass58fe7e52013-06-13 15:10:00 -0700169 }
170
171 return NULL;
172}
Simon Glassfbabc0f2013-06-13 15:10:01 -0700173
Philippe Reynes12468352018-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}