Merge tag 'tpm-master-28012025' of https://source.denx.de/u-boot/custodians/u-boot-tpm
[u-boot.git] / boot / image-sig.c
blob6bc74866eaedca3f17e78136ebad2c7c74178928
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2013, Google Inc.
4 */
6 #include <log.h>
7 #include <malloc.h>
8 #include <asm/global_data.h>
9 DECLARE_GLOBAL_DATA_PTR;
10 #include <image.h>
11 #include <relocate.h>
12 #include <u-boot/ecdsa.h>
13 #include <u-boot/rsa.h>
14 #include <u-boot/hash-checksum.h>
16 #define IMAGE_MAX_HASHED_NODES 100
18 struct checksum_algo checksum_algos[] = {
19 #if CONFIG_IS_ENABLED(SHA1)
21 .name = "sha1",
22 .checksum_len = SHA1_SUM_LEN,
23 .der_len = SHA1_DER_LEN,
24 .der_prefix = sha1_der_prefix,
25 .calculate = hash_calculate,
27 #endif
28 #if CONFIG_IS_ENABLED(SHA256)
30 .name = "sha256",
31 .checksum_len = SHA256_SUM_LEN,
32 .der_len = SHA256_DER_LEN,
33 .der_prefix = sha256_der_prefix,
34 .calculate = hash_calculate,
36 #endif
37 #if CONFIG_IS_ENABLED(SHA384)
39 .name = "sha384",
40 .checksum_len = SHA384_SUM_LEN,
41 .der_len = SHA384_DER_LEN,
42 .der_prefix = sha384_der_prefix,
43 .calculate = hash_calculate,
45 #endif
46 #if CONFIG_IS_ENABLED(SHA512)
48 .name = "sha512",
49 .checksum_len = SHA512_SUM_LEN,
50 .der_len = SHA512_DER_LEN,
51 .der_prefix = sha512_der_prefix,
52 .calculate = hash_calculate,
54 #endif
58 struct checksum_algo *image_get_checksum_algo(const char *full_name)
60 int i;
61 const char *name;
63 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];
71 return NULL;
74 struct crypto_algo *image_get_crypto_algo(const char *full_name)
76 struct crypto_algo *crypto, *end;
77 const char *name;
79 /* Move name to after the comma */
80 name = strchr(full_name, ',');
81 if (!name)
82 return NULL;
83 name += 1;
85 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;
92 /* Not found */
93 return NULL;
96 struct padding_algo *image_get_padding_algo(const char *name)
98 struct padding_algo *padding, *end;
100 if (!name)
101 return NULL;
103 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;
110 return NULL;