blob: 6bc74866eaedca3f17e78136ebad2c7c74178928 [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 Glassf7ae49f2020-05-10 11:40:05 -06006#include <log.h>
Simon Glass56518e72013-06-13 15:10:01 -07007#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -06008#include <asm/global_data.h>
Simon Glass56518e72013-06-13 15:10:01 -07009DECLARE_GLOBAL_DATA_PTR;
Simon Glass3e569a62013-06-13 15:10:00 -070010#include <image.h>
Simon Glassc5a68d22021-09-25 07:03:19 -060011#include <relocate.h>
Alexandru Gagniuced6c9e02021-02-19 12:45:12 -060012#include <u-boot/ecdsa.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020013#include <u-boot/rsa.h>
Alexandru Gagniuc0bcb28d2021-02-19 12:45:10 -060014#include <u-boot/hash-checksum.h>
Simon Glass3e569a62013-06-13 15:10:00 -070015
Simon Glass4d098522013-06-13 15:10:09 -070016#define IMAGE_MAX_HASHED_NODES 100
17
Heiko Schocher646257d2014-03-03 12:19:26 +010018struct checksum_algo checksum_algos[] = {
Sean Anderson14e110a2024-02-15 12:12:18 -050019#if CONFIG_IS_ENABLED(SHA1)
Heiko Schocher646257d2014-03-03 12:19:26 +010020 {
Masahiro Yamada8ec87df2017-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 Yamada8ec87df2017-10-23 10:03:40 +090025 .calculate = hash_calculate,
Heiko Schocher646257d2014-03-03 12:19:26 +010026 },
Sean Anderson14e110a2024-02-15 12:12:18 -050027#endif
28#if CONFIG_IS_ENABLED(SHA256)
Heiko Schocher646257d2014-03-03 12:19:26 +010029 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090030 .name = "sha256",
31 .checksum_len = SHA256_SUM_LEN,
32 .der_len = SHA256_DER_LEN,
33 .der_prefix = sha256_der_prefix,
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090034 .calculate = hash_calculate,
Reuben Dowled16b38f2020-04-16 17:36:52 +120035 },
Sean Anderson14e110a2024-02-15 12:12:18 -050036#endif
37#if CONFIG_IS_ENABLED(SHA384)
Reuben Dowled16b38f2020-04-16 17:36:52 +120038 {
39 .name = "sha384",
40 .checksum_len = SHA384_SUM_LEN,
41 .der_len = SHA384_DER_LEN,
42 .der_prefix = sha384_der_prefix,
Reuben Dowled16b38f2020-04-16 17:36:52 +120043 .calculate = hash_calculate,
44 },
45#endif
Sean Anderson14e110a2024-02-15 12:12:18 -050046#if CONFIG_IS_ENABLED(SHA512)
Reuben Dowled16b38f2020-04-16 17:36:52 +120047 {
48 .name = "sha512",
49 .checksum_len = SHA512_SUM_LEN,
50 .der_len = SHA512_DER_LEN,
51 .der_prefix = sha512_der_prefix,
Reuben Dowled16b38f2020-04-16 17:36:52 +120052 .calculate = hash_calculate,
53 },
54#endif
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010055
Heiko Schocher646257d2014-03-03 12:19:26 +010056};
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010057
Andrew Duda83dd98e2016-11-08 18:53:41 +000058struct checksum_algo *image_get_checksum_algo(const char *full_name)
Simon Glass3e569a62013-06-13 15:10:00 -070059{
60 int i;
Andrew Duda83dd98e2016-11-08 18:53:41 +000061 const char *name;
Simon Glass3e569a62013-06-13 15:10:00 -070062
Andrew Duda83dd98e2016-11-08 18:53:41 +000063 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
64 name = checksum_algos[i].name;
65 /* Make sure names match and next char is a comma */
66 if (!strncmp(name, full_name, strlen(name)) &&
67 full_name[strlen(name)] == ',')
68 return &checksum_algos[i];
69 }
70
71 return NULL;
72}
73
74struct crypto_algo *image_get_crypto_algo(const char *full_name)
75{
Alexandru Gagniuc09801642021-07-14 17:05:39 -050076 struct crypto_algo *crypto, *end;
Andrew Duda83dd98e2016-11-08 18:53:41 +000077 const char *name;
78
79 /* Move name to after the comma */
80 name = strchr(full_name, ',');
81 if (!name)
82 return NULL;
83 name += 1;
84
Alexandru Gagniuc09801642021-07-14 17:05:39 -050085 crypto = ll_entry_start(struct crypto_algo, cryptos);
86 end = ll_entry_end(struct crypto_algo, cryptos);
87 for (; crypto < end; crypto++) {
88 if (!strcmp(crypto->name, name))
89 return crypto;
90 }
91
92 /* Not found */
Simon Glass3e569a62013-06-13 15:10:00 -070093 return NULL;
94}
Simon Glass56518e72013-06-13 15:10:01 -070095
Philippe Reynes20031562018-11-14 13:51:00 +010096struct padding_algo *image_get_padding_algo(const char *name)
97{
Alexandru Gagniucde41f0e2021-08-18 17:49:02 -050098 struct padding_algo *padding, *end;
Philippe Reynes20031562018-11-14 13:51:00 +010099
100 if (!name)
101 return NULL;
102
Alexandru Gagniucde41f0e2021-08-18 17:49:02 -0500103 padding = ll_entry_start(struct padding_algo, paddings);
104 end = ll_entry_end(struct padding_algo, paddings);
105 for (; padding < end; padding++) {
106 if (!strcmp(padding->name, name))
107 return padding;
Philippe Reynes20031562018-11-14 13:51:00 +0100108 }
109
110 return NULL;
111}