blob: 5122dc948f0d61c5cf8a4536696de236edf3e268 [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 Glass56518e72013-06-13 15:10:01 -070013#include <malloc.h>
14DECLARE_GLOBAL_DATA_PTR;
Simon Glass3e569a62013-06-13 15:10:00 -070015#endif /* !USE_HOSTCC*/
Simon Glass3e569a62013-06-13 15:10:00 -070016#include <image.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020017#include <u-boot/rsa.h>
18#include <u-boot/rsa-checksum.h>
Simon Glass3e569a62013-06-13 15:10:00 -070019
Simon Glass4d098522013-06-13 15:10:09 -070020#define IMAGE_MAX_HASHED_NODES 100
21
Heiko Schocher646257d2014-03-03 12:19:26 +010022struct checksum_algo checksum_algos[] = {
23 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090024 .name = "sha1",
25 .checksum_len = SHA1_SUM_LEN,
26 .der_len = SHA1_DER_LEN,
27 .der_prefix = sha1_der_prefix,
Heiko Schocher646257d2014-03-03 12:19:26 +010028#if IMAGE_ENABLE_SIGN
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090029 .calculate_sign = EVP_sha1,
Heiko Schocher29a23f92014-03-03 12:19:30 +010030#endif
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090031 .calculate = hash_calculate,
Heiko Schocher646257d2014-03-03 12:19:26 +010032 },
33 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090034 .name = "sha256",
35 .checksum_len = SHA256_SUM_LEN,
36 .der_len = SHA256_DER_LEN,
37 .der_prefix = sha256_der_prefix,
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010038#if IMAGE_ENABLE_SIGN
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090039 .calculate_sign = EVP_sha256,
Heiko Schocher29a23f92014-03-03 12:19:30 +010040#endif
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090041 .calculate = hash_calculate,
Heiko Schocher646257d2014-03-03 12:19:26 +010042 }
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010043
Heiko Schocher646257d2014-03-03 12:19:26 +010044};
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010045
Andrew Duda0c1d74f2016-11-08 18:53:41 +000046struct crypto_algo crypto_algos[] = {
Simon Glass19c402a2013-06-13 15:10:02 -070047 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090048 .name = "rsa2048",
49 .key_len = RSA2048_BYTES,
50 .sign = rsa_sign,
51 .add_verify_data = rsa_add_verify_data,
52 .verify = rsa_verify,
Andrew Duda0c1d74f2016-11-08 18:53:41 +000053 },
54 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090055 .name = "rsa4096",
56 .key_len = RSA4096_BYTES,
57 .sign = rsa_sign,
58 .add_verify_data = rsa_add_verify_data,
59 .verify = rsa_verify,
Andrew Duda0c1d74f2016-11-08 18:53:41 +000060 }
61
62};
63
Philippe Reynes20031562018-11-14 13:51:00 +010064struct padding_algo padding_algos[] = {
65 {
66 .name = "pkcs-1.5",
67 .verify = padding_pkcs_15_verify,
68 },
Philippe Reynes061daa02018-11-14 13:51:01 +010069#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 */
Philippe Reynes20031562018-11-14 13:51:00 +010075};
76
Andrew Duda83dd98e2016-11-08 18:53:41 +000077struct checksum_algo *image_get_checksum_algo(const char *full_name)
Simon Glass3e569a62013-06-13 15:10:00 -070078{
79 int i;
Andrew Duda83dd98e2016-11-08 18:53:41 +000080 const char *name;
Simon Glass3e569a62013-06-13 15:10:00 -070081
T Karthik Reddy1ed8c132019-03-16 15:23:03 +053082#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
Andrew Duda83dd98e2016-11-08 18:53:41 +000097 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
108struct crypto_algo *image_get_crypto_algo(const char *full_name)
109{
110 int i;
111 const char *name;
112
T Karthik Reddy1ed8c132019-03-16 15:23:03 +0530113#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
Andrew Duda83dd98e2016-11-08 18:53:41 +0000127 /* 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];
Simon Glass3e569a62013-06-13 15:10:00 -0700136 }
137
138 return NULL;
139}
Simon Glass56518e72013-06-13 15:10:01 -0700140
Philippe Reynes20031562018-11-14 13:51:00 +0100141struct 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}