blob: fa9407bb300d2262e71ad91a911188a9ea2458c7 [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>
Alexandru Gagniuc3ebd2002021-02-19 12:45:12 -060012#include <u-boot/ecdsa.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020013#include <u-boot/rsa.h>
Alexandru Gagniucdb182c42021-02-19 12:45:10 -060014#include <u-boot/hash-checksum.h>
Simon Glass58fe7e52013-06-13 15:10:00 -070015
Simon Glass56ab8d62013-06-13 15:10:09 -070016#define IMAGE_MAX_HASHED_NODES 100
17
Heiko Schocheredaf9b12014-03-03 12:19:26 +010018struct checksum_algo checksum_algos[] = {
19 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090020 .name = "sha1",
21 .checksum_len = SHA1_SUM_LEN,
22 .der_len = SHA1_DER_LEN,
23 .der_prefix = sha1_der_prefix,
Masahiro Yamada79f3c592017-10-23 10:03:40 +090024 .calculate = hash_calculate,
Heiko Schocheredaf9b12014-03-03 12:19:26 +010025 },
26 {
Masahiro Yamada79f3c592017-10-23 10:03:40 +090027 .name = "sha256",
28 .checksum_len = SHA256_SUM_LEN,
29 .der_len = SHA256_DER_LEN,
30 .der_prefix = sha256_der_prefix,
Masahiro Yamada79f3c592017-10-23 10:03:40 +090031 .calculate = hash_calculate,
Reuben Dowle1908fd92020-04-16 17:36:52 +120032 },
33#ifdef CONFIG_SHA384
34 {
35 .name = "sha384",
36 .checksum_len = SHA384_SUM_LEN,
37 .der_len = SHA384_DER_LEN,
38 .der_prefix = sha384_der_prefix,
Reuben Dowle1908fd92020-04-16 17:36:52 +120039 .calculate = hash_calculate,
40 },
41#endif
42#ifdef CONFIG_SHA512
43 {
44 .name = "sha512",
45 .checksum_len = SHA512_SUM_LEN,
46 .der_len = SHA512_DER_LEN,
47 .der_prefix = sha512_der_prefix,
Reuben Dowle1908fd92020-04-16 17:36:52 +120048 .calculate = hash_calculate,
49 },
50#endif
Andrew Duda06ca6d62016-11-08 18:53:41 +000051
52};
53
Andrew Duda6616c822016-11-08 18:53:41 +000054struct checksum_algo *image_get_checksum_algo(const char *full_name)
55{
56 int i;
57 const char *name;
58
Alexandru Gagniucafb1bca2021-07-14 17:05:38 -050059#if defined(CONFIG_NEEDS_MANUAL_RELOC)
T Karthik Reddy0164dc82019-03-16 15:23:03 +053060 static bool done;
61
62 if (!done) {
63 done = true;
64 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
65 checksum_algos[i].name += gd->reloc_off;
T Karthik Reddy0164dc82019-03-16 15:23:03 +053066 checksum_algos[i].calculate += gd->reloc_off;
67 }
68 }
69#endif
70
Andrew Duda6616c822016-11-08 18:53:41 +000071 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
72 name = checksum_algos[i].name;
73 /* Make sure names match and next char is a comma */
74 if (!strncmp(name, full_name, strlen(name)) &&
75 full_name[strlen(name)] == ',')
76 return &checksum_algos[i];
Simon Glass35191a32013-06-13 15:10:02 -070077 }
Heiko Schocher4b817562014-03-03 12:19:27 +010078
Andrew Duda6616c822016-11-08 18:53:41 +000079 return NULL;
80}
Simon Glass58fe7e52013-06-13 15:10:00 -070081
Andrew Duda6616c822016-11-08 18:53:41 +000082struct crypto_algo *image_get_crypto_algo(const char *full_name)
Simon Glass58fe7e52013-06-13 15:10:00 -070083{
Alexandru Gagniucb04e9182021-07-14 17:05:39 -050084 struct crypto_algo *crypto, *end;
Andrew Duda6616c822016-11-08 18:53:41 +000085 const char *name;
86
Alexandru Gagniucb2c5f9b2021-07-14 17:05:46 -050087#if defined(CONFIG_NEEDS_MANUAL_RELOC)
88 static bool done;
89
90 if (!done) {
91 crypto = ll_entry_start(struct crypto_algo, cryptos);
92 end = ll_entry_end(struct crypto_algo, cryptos);
93 for (; crypto < end; crypto++) {
94 crypto->name += gd->reloc_off;
95 crypto->verify += gd->reloc_off;
96 }
97 }
98#endif
99
Andrew Duda6616c822016-11-08 18:53:41 +0000100 /* Move name to after the comma */
101 name = strchr(full_name, ',');
102 if (!name)
103 return NULL;
104 name += 1;
Simon Glass58fe7e52013-06-13 15:10:00 -0700105
Alexandru Gagniucb04e9182021-07-14 17:05:39 -0500106 crypto = ll_entry_start(struct crypto_algo, cryptos);
107 end = ll_entry_end(struct crypto_algo, cryptos);
108 for (; crypto < end; crypto++) {
109 if (!strcmp(crypto->name, name))
110 return crypto;
111 }
112
113 /* Not found */
Simon Glass58fe7e52013-06-13 15:10:00 -0700114 return NULL;
115}
Simon Glassfbabc0f2013-06-13 15:10:01 -0700116
Philippe Reynes12468352018-11-14 13:51:00 +0100117struct padding_algo *image_get_padding_algo(const char *name)
118{
Alexandru Gagniucf3b5e582021-08-18 17:49:02 -0500119 struct padding_algo *padding, *end;
Philippe Reynes12468352018-11-14 13:51:00 +0100120
121 if (!name)
122 return NULL;
123
Alexandru Gagniucf3b5e582021-08-18 17:49:02 -0500124 padding = ll_entry_start(struct padding_algo, paddings);
125 end = ll_entry_end(struct padding_algo, paddings);
126 for (; padding < end; padding++) {
127 if (!strcmp(padding->name, name))
128 return padding;
Philippe Reynes12468352018-11-14 13:51:00 +0100129 }
130
131 return NULL;
132}