blob: 0421a61b0406640b158f49ba06acec213a1ae60b [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
Simon Glass3e569a62013-06-13 15:10:00 -07006#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06007#include <log.h>
Simon Glass56518e72013-06-13 15:10:01 -07008#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass56518e72013-06-13 15:10:01 -070010DECLARE_GLOBAL_DATA_PTR;
Simon Glass3e569a62013-06-13 15:10:00 -070011#include <image.h>
Simon Glassc5a68d22021-09-25 07:03:19 -060012#include <relocate.h>
Alexandru Gagniuced6c9e02021-02-19 12:45:12 -060013#include <u-boot/ecdsa.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020014#include <u-boot/rsa.h>
Alexandru Gagniuc0bcb28d2021-02-19 12:45:10 -060015#include <u-boot/hash-checksum.h>
Simon Glass3e569a62013-06-13 15:10:00 -070016
Simon Glass4d098522013-06-13 15:10:09 -070017#define IMAGE_MAX_HASHED_NODES 100
18
Heiko Schocher646257d2014-03-03 12:19:26 +010019struct checksum_algo checksum_algos[] = {
Sean Anderson14e110a2024-02-15 12:12:18 -050020#if CONFIG_IS_ENABLED(SHA1)
Heiko Schocher646257d2014-03-03 12:19:26 +010021 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090022 .name = "sha1",
23 .checksum_len = SHA1_SUM_LEN,
24 .der_len = SHA1_DER_LEN,
25 .der_prefix = sha1_der_prefix,
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090026 .calculate = hash_calculate,
Heiko Schocher646257d2014-03-03 12:19:26 +010027 },
Sean Anderson14e110a2024-02-15 12:12:18 -050028#endif
29#if CONFIG_IS_ENABLED(SHA256)
Heiko Schocher646257d2014-03-03 12:19:26 +010030 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090031 .name = "sha256",
32 .checksum_len = SHA256_SUM_LEN,
33 .der_len = SHA256_DER_LEN,
34 .der_prefix = sha256_der_prefix,
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090035 .calculate = hash_calculate,
Reuben Dowled16b38f2020-04-16 17:36:52 +120036 },
Sean Anderson14e110a2024-02-15 12:12:18 -050037#endif
38#if CONFIG_IS_ENABLED(SHA384)
Reuben Dowled16b38f2020-04-16 17:36:52 +120039 {
40 .name = "sha384",
41 .checksum_len = SHA384_SUM_LEN,
42 .der_len = SHA384_DER_LEN,
43 .der_prefix = sha384_der_prefix,
Reuben Dowled16b38f2020-04-16 17:36:52 +120044 .calculate = hash_calculate,
45 },
46#endif
Sean Anderson14e110a2024-02-15 12:12:18 -050047#if CONFIG_IS_ENABLED(SHA512)
Reuben Dowled16b38f2020-04-16 17:36:52 +120048 {
49 .name = "sha512",
50 .checksum_len = SHA512_SUM_LEN,
51 .der_len = SHA512_DER_LEN,
52 .der_prefix = sha512_der_prefix,
Reuben Dowled16b38f2020-04-16 17:36:52 +120053 .calculate = hash_calculate,
54 },
55#endif
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010056
Heiko Schocher646257d2014-03-03 12:19:26 +010057};
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010058
Andrew Duda83dd98e2016-11-08 18:53:41 +000059struct checksum_algo *image_get_checksum_algo(const char *full_name)
Simon Glass3e569a62013-06-13 15:10:00 -070060{
61 int i;
Andrew Duda83dd98e2016-11-08 18:53:41 +000062 const char *name;
Simon Glass3e569a62013-06-13 15:10:00 -070063
Andrew Duda83dd98e2016-11-08 18:53:41 +000064 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
65 name = checksum_algos[i].name;
66 /* Make sure names match and next char is a comma */
67 if (!strncmp(name, full_name, strlen(name)) &&
68 full_name[strlen(name)] == ',')
69 return &checksum_algos[i];
70 }
71
72 return NULL;
73}
74
75struct crypto_algo *image_get_crypto_algo(const char *full_name)
76{
Alexandru Gagniuc09801642021-07-14 17:05:39 -050077 struct crypto_algo *crypto, *end;
Andrew Duda83dd98e2016-11-08 18:53:41 +000078 const char *name;
79
80 /* Move name to after the comma */
81 name = strchr(full_name, ',');
82 if (!name)
83 return NULL;
84 name += 1;
85
Alexandru Gagniuc09801642021-07-14 17:05:39 -050086 crypto = ll_entry_start(struct crypto_algo, cryptos);
87 end = ll_entry_end(struct crypto_algo, cryptos);
88 for (; crypto < end; crypto++) {
89 if (!strcmp(crypto->name, name))
90 return crypto;
91 }
92
93 /* Not found */
Simon Glass3e569a62013-06-13 15:10:00 -070094 return NULL;
95}
Simon Glass56518e72013-06-13 15:10:01 -070096
Philippe Reynes20031562018-11-14 13:51:00 +010097struct padding_algo *image_get_padding_algo(const char *name)
98{
Alexandru Gagniucde41f0e2021-08-18 17:49:02 -050099 struct padding_algo *padding, *end;
Philippe Reynes20031562018-11-14 13:51:00 +0100100
101 if (!name)
102 return NULL;
103
Alexandru Gagniucde41f0e2021-08-18 17:49:02 -0500104 padding = ll_entry_start(struct padding_algo, paddings);
105 end = ll_entry_end(struct padding_algo, paddings);
106 for (; padding < end; padding++) {
107 if (!strcmp(padding->name, name))
108 return padding;
Philippe Reynes20031562018-11-14 13:51:00 +0100109 }
110
111 return NULL;
112}