blob: 1aa0b586450a50d5bbaf87bbdcef826db6c9e04c [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
Simon Glass58fe7e52013-06-13 15:10:00 -07006#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glassfbabc0f2013-06-13 15:10:01 -07008#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glassfbabc0f2013-06-13 15:10:01 -070010DECLARE_GLOBAL_DATA_PTR;
Simon Glass58fe7e52013-06-13 15:10:00 -070011#include <image.h>
Simon Glass48a1b682021-09-25 07:03:19 -060012#include <relocate.h>
Alexandru Gagniuc3ebd2002021-02-19 12:45:12 -060013#include <u-boot/ecdsa.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020014#include <u-boot/rsa.h>
Alexandru Gagniucdb182c42021-02-19 12:45:10 -060015#include <u-boot/hash-checksum.h>
Simon Glass58fe7e52013-06-13 15:10:00 -070016
Simon Glass56ab8d62013-06-13 15:10:09 -070017#define IMAGE_MAX_HASHED_NODES 100
18
Heiko Schocheredaf9b12014-03-03 12:19:26 +010019struct checksum_algo checksum_algos[] = {
20 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090021 .name = "sha1",
22 .checksum_len = SHA1_SUM_LEN,
23 .der_len = SHA1_DER_LEN,
24 .der_prefix = sha1_der_prefix,
Masahiro Yamada79f3c592017-10-23 10:03:40 +090025 .calculate = hash_calculate,
Heiko Schocheredaf9b12014-03-03 12:19:26 +010026 },
27 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090028 .name = "sha256",
29 .checksum_len = SHA256_SUM_LEN,
30 .der_len = SHA256_DER_LEN,
31 .der_prefix = sha256_der_prefix,
Masahiro Yamada79f3c592017-10-23 10:03:40 +090032 .calculate = hash_calculate,
Reuben Dowle1908fd92020-04-16 17:36:52 +120033 },
34#ifdef CONFIG_SHA384
35 {
36 .name = "sha384",
37 .checksum_len = SHA384_SUM_LEN,
38 .der_len = SHA384_DER_LEN,
39 .der_prefix = sha384_der_prefix,
Reuben Dowle1908fd92020-04-16 17:36:52 +120040 .calculate = hash_calculate,
41 },
42#endif
43#ifdef CONFIG_SHA512
44 {
45 .name = "sha512",
46 .checksum_len = SHA512_SUM_LEN,
47 .der_len = SHA512_DER_LEN,
48 .der_prefix = sha512_der_prefix,
Reuben Dowle1908fd92020-04-16 17:36:52 +120049 .calculate = hash_calculate,
50 },
51#endif
Andrew Duda06ca6d62016-11-08 18:53:41 +000052
53};
54
Andrew Duda6616c822016-11-08 18:53:41 +000055struct checksum_algo *image_get_checksum_algo(const char *full_name)
56{
57 int i;
58 const char *name;
59
Simon Glass48a1b682021-09-25 07:03:19 -060060 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
61 static bool done;
T Karthik Reddy0164dc82019-03-16 15:23:03 +053062
Simon Glass48a1b682021-09-25 07:03:19 -060063 if (!done) {
64 done = true;
65 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
66 struct checksum_algo *algo = &checksum_algos[i];
67
68 MANUAL_RELOC(algo->name);
69 MANUAL_RELOC(algo->calculate);
70 }
T Karthik Reddy0164dc82019-03-16 15:23:03 +053071 }
72 }
T Karthik Reddy0164dc82019-03-16 15:23:03 +053073
Andrew Duda6616c822016-11-08 18:53:41 +000074 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
75 name = checksum_algos[i].name;
76 /* Make sure names match and next char is a comma */
77 if (!strncmp(name, full_name, strlen(name)) &&
78 full_name[strlen(name)] == ',')
79 return &checksum_algos[i];
Simon Glass35191a32013-06-13 15:10:02 -070080 }
Heiko Schocher4b817562014-03-03 12:19:27 +010081
Andrew Duda6616c822016-11-08 18:53:41 +000082 return NULL;
83}
Simon Glass58fe7e52013-06-13 15:10:00 -070084
Andrew Duda6616c822016-11-08 18:53:41 +000085struct crypto_algo *image_get_crypto_algo(const char *full_name)
Simon Glass58fe7e52013-06-13 15:10:00 -070086{
Alexandru Gagniucb04e9182021-07-14 17:05:39 -050087 struct crypto_algo *crypto, *end;
Andrew Duda6616c822016-11-08 18:53:41 +000088 const char *name;
89
Simon Glass48a1b682021-09-25 07:03:19 -060090 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
91 static bool done;
Alexandru Gagniucb2c5f9b2021-07-14 17:05:46 -050092
Simon Glass48a1b682021-09-25 07:03:19 -060093 if (!done) {
94 done = true;
95 crypto = ll_entry_start(struct crypto_algo, cryptos);
96 end = ll_entry_end(struct crypto_algo, cryptos);
97 for (; crypto < end; crypto++) {
98 MANUAL_RELOC(crypto->name);
99 MANUAL_RELOC(crypto->verify);
100 }
Alexandru Gagniucb2c5f9b2021-07-14 17:05:46 -0500101 }
102 }
Alexandru Gagniucb2c5f9b2021-07-14 17:05:46 -0500103
Andrew Duda6616c822016-11-08 18:53:41 +0000104 /* Move name to after the comma */
105 name = strchr(full_name, ',');
106 if (!name)
107 return NULL;
108 name += 1;
Simon Glass58fe7e52013-06-13 15:10:00 -0700109
Alexandru Gagniucb04e9182021-07-14 17:05:39 -0500110 crypto = ll_entry_start(struct crypto_algo, cryptos);
111 end = ll_entry_end(struct crypto_algo, cryptos);
112 for (; crypto < end; crypto++) {
113 if (!strcmp(crypto->name, name))
114 return crypto;
115 }
116
117 /* Not found */
Simon Glass58fe7e52013-06-13 15:10:00 -0700118 return NULL;
119}
Simon Glassfbabc0f2013-06-13 15:10:01 -0700120
Philippe Reynes12468352018-11-14 13:51:00 +0100121struct padding_algo *image_get_padding_algo(const char *name)
122{
Alexandru Gagniucf3b5e582021-08-18 17:49:02 -0500123 struct padding_algo *padding, *end;
Philippe Reynes12468352018-11-14 13:51:00 +0100124
125 if (!name)
126 return NULL;
127
Alexandru Gagniucf3b5e582021-08-18 17:49:02 -0500128 padding = ll_entry_start(struct padding_algo, paddings);
129 end = ll_entry_end(struct padding_algo, paddings);
130 for (; padding < end; padding++) {
131 if (!strcmp(padding->name, name))
132 return padding;
Philippe Reynes12468352018-11-14 13:51:00 +0100133 }
134
135 return NULL;
136}