blob: 4abd3c080f78100897b7721e9638cca10222ef65 [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>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020019#include <u-boot/rsa.h>
20#include <u-boot/rsa-checksum.h>
Simon Glass58fe7e52013-06-13 15:10:00 -070021
Simon Glass56ab8d62013-06-13 15:10:09 -070022#define IMAGE_MAX_HASHED_NODES 100
23
Heiko Schocheredaf9b12014-03-03 12:19:26 +010024struct checksum_algo checksum_algos[] = {
25 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090026 .name = "sha1",
27 .checksum_len = SHA1_SUM_LEN,
28 .der_len = SHA1_DER_LEN,
29 .der_prefix = sha1_der_prefix,
Heiko Schocheredaf9b12014-03-03 12:19:26 +010030#if IMAGE_ENABLE_SIGN
Masahiro Yamada79f3c592017-10-23 10:03:40 +090031 .calculate_sign = EVP_sha1,
Heiko Schocherd7b42322014-03-03 12:19:30 +010032#endif
Masahiro Yamada79f3c592017-10-23 10:03:40 +090033 .calculate = hash_calculate,
Heiko Schocheredaf9b12014-03-03 12:19:26 +010034 },
35 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090036 .name = "sha256",
37 .checksum_len = SHA256_SUM_LEN,
38 .der_len = SHA256_DER_LEN,
39 .der_prefix = sha256_der_prefix,
Heiko Schocheredaf9b12014-03-03 12:19:26 +010040#if IMAGE_ENABLE_SIGN
Masahiro Yamada79f3c592017-10-23 10:03:40 +090041 .calculate_sign = EVP_sha256,
Heiko Schocherd7b42322014-03-03 12:19:30 +010042#endif
Masahiro Yamada79f3c592017-10-23 10:03:40 +090043 .calculate = hash_calculate,
Reuben Dowle1908fd92020-04-16 17:36:52 +120044 },
45#ifdef CONFIG_SHA384
46 {
47 .name = "sha384",
48 .checksum_len = SHA384_SUM_LEN,
49 .der_len = SHA384_DER_LEN,
50 .der_prefix = sha384_der_prefix,
51#if IMAGE_ENABLE_SIGN
52 .calculate_sign = EVP_sha384,
53#endif
54 .calculate = hash_calculate,
55 },
56#endif
57#ifdef CONFIG_SHA512
58 {
59 .name = "sha512",
60 .checksum_len = SHA512_SUM_LEN,
61 .der_len = SHA512_DER_LEN,
62 .der_prefix = sha512_der_prefix,
63#if IMAGE_ENABLE_SIGN
64 .calculate_sign = EVP_sha512,
65#endif
66 .calculate = hash_calculate,
67 },
68#endif
Andrew Duda06ca6d62016-11-08 18:53:41 +000069
70};
71
72struct crypto_algo crypto_algos[] = {
73 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090074 .name = "rsa2048",
75 .key_len = RSA2048_BYTES,
76 .sign = rsa_sign,
77 .add_verify_data = rsa_add_verify_data,
78 .verify = rsa_verify,
Heiko Schocher4b817562014-03-03 12:19:27 +010079 },
80 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090081 .name = "rsa4096",
82 .key_len = RSA4096_BYTES,
83 .sign = rsa_sign,
84 .add_verify_data = rsa_add_verify_data,
85 .verify = rsa_verify,
Heiko Schocheredaf9b12014-03-03 12:19:26 +010086 }
Heiko Schocher4b817562014-03-03 12:19:27 +010087
Heiko Schocheredaf9b12014-03-03 12:19:26 +010088};
Heiko Schocher4b817562014-03-03 12:19:27 +010089
Philippe Reynes12468352018-11-14 13:51:00 +010090struct padding_algo padding_algos[] = {
91 {
92 .name = "pkcs-1.5",
93 .verify = padding_pkcs_15_verify,
94 },
Philippe Reynes47d73f02018-11-14 13:51:01 +010095#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
96 {
97 .name = "pss",
98 .verify = padding_pss_verify,
99 }
100#endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
Philippe Reynes12468352018-11-14 13:51:00 +0100101};
102
Andrew Duda6616c822016-11-08 18:53:41 +0000103struct checksum_algo *image_get_checksum_algo(const char *full_name)
104{
105 int i;
106 const char *name;
107
T Karthik Reddy0164dc82019-03-16 15:23:03 +0530108#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
109 static bool done;
110
111 if (!done) {
112 done = true;
113 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
114 checksum_algos[i].name += gd->reloc_off;
115#if IMAGE_ENABLE_SIGN
116 checksum_algos[i].calculate_sign += gd->reloc_off;
117#endif
118 checksum_algos[i].calculate += gd->reloc_off;
119 }
120 }
121#endif
122
Andrew Duda6616c822016-11-08 18:53:41 +0000123 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
124 name = checksum_algos[i].name;
125 /* Make sure names match and next char is a comma */
126 if (!strncmp(name, full_name, strlen(name)) &&
127 full_name[strlen(name)] == ',')
128 return &checksum_algos[i];
Simon Glass35191a32013-06-13 15:10:02 -0700129 }
Heiko Schocher4b817562014-03-03 12:19:27 +0100130
Andrew Duda6616c822016-11-08 18:53:41 +0000131 return NULL;
132}
Simon Glass58fe7e52013-06-13 15:10:00 -0700133
Andrew Duda6616c822016-11-08 18:53:41 +0000134struct crypto_algo *image_get_crypto_algo(const char *full_name)
Simon Glass58fe7e52013-06-13 15:10:00 -0700135{
136 int i;
Andrew Duda6616c822016-11-08 18:53:41 +0000137 const char *name;
138
T Karthik Reddy0164dc82019-03-16 15:23:03 +0530139#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
140 static bool done;
141
142 if (!done) {
143 done = true;
144 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
145 crypto_algos[i].name += gd->reloc_off;
146 crypto_algos[i].sign += gd->reloc_off;
147 crypto_algos[i].add_verify_data += gd->reloc_off;
148 crypto_algos[i].verify += gd->reloc_off;
149 }
150 }
151#endif
152
Andrew Duda6616c822016-11-08 18:53:41 +0000153 /* Move name to after the comma */
154 name = strchr(full_name, ',');
155 if (!name)
156 return NULL;
157 name += 1;
Simon Glass58fe7e52013-06-13 15:10:00 -0700158
Andrew Duda6616c822016-11-08 18:53:41 +0000159 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
160 if (!strcmp(crypto_algos[i].name, name))
161 return &crypto_algos[i];
Simon Glass58fe7e52013-06-13 15:10:00 -0700162 }
163
164 return NULL;
165}
Simon Glassfbabc0f2013-06-13 15:10:01 -0700166
Philippe Reynes12468352018-11-14 13:51:00 +0100167struct padding_algo *image_get_padding_algo(const char *name)
168{
169 int i;
170
171 if (!name)
172 return NULL;
173
174 for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
175 if (!strcmp(padding_algos[i].name, name))
176 return &padding_algos[i];
177 }
178
179 return NULL;
180}