Merge tag 'u-boot-stm32-20231004' of https://source.denx.de/u-boot/custodians/u-boot-stm
[pandora-u-boot.git] / boot / image-sig.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013, Google Inc.
4  */
5
6 #include <common.h>
7 #include <log.h>
8 #include <malloc.h>
9 #include <asm/global_data.h>
10 DECLARE_GLOBAL_DATA_PTR;
11 #include <image.h>
12 #include <relocate.h>
13 #include <u-boot/ecdsa.h>
14 #include <u-boot/rsa.h>
15 #include <u-boot/hash-checksum.h>
16
17 #define IMAGE_MAX_HASHED_NODES          100
18
19 struct checksum_algo checksum_algos[] = {
20         {
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,
26         },
27         {
28                 .name = "sha256",
29                 .checksum_len = SHA256_SUM_LEN,
30                 .der_len = SHA256_DER_LEN,
31                 .der_prefix = sha256_der_prefix,
32                 .calculate = hash_calculate,
33         },
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,
40                 .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,
49                 .calculate = hash_calculate,
50         },
51 #endif
52
53 };
54
55 struct checksum_algo *image_get_checksum_algo(const char *full_name)
56 {
57         int i;
58         const char *name;
59
60         for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
61                 name = checksum_algos[i].name;
62                 /* Make sure names match and next char is a comma */
63                 if (!strncmp(name, full_name, strlen(name)) &&
64                     full_name[strlen(name)] == ',')
65                         return &checksum_algos[i];
66         }
67
68         return NULL;
69 }
70
71 struct crypto_algo *image_get_crypto_algo(const char *full_name)
72 {
73         struct crypto_algo *crypto, *end;
74         const char *name;
75
76         /* Move name to after the comma */
77         name = strchr(full_name, ',');
78         if (!name)
79                 return NULL;
80         name += 1;
81
82         crypto = ll_entry_start(struct crypto_algo, cryptos);
83         end = ll_entry_end(struct crypto_algo, cryptos);
84         for (; crypto < end; crypto++) {
85                 if (!strcmp(crypto->name, name))
86                         return crypto;
87         }
88
89         /* Not found */
90         return NULL;
91 }
92
93 struct padding_algo *image_get_padding_algo(const char *name)
94 {
95         struct padding_algo *padding, *end;
96
97         if (!name)
98                 return NULL;
99
100         padding = ll_entry_start(struct padding_algo, paddings);
101         end = ll_entry_end(struct padding_algo, paddings);
102         for (; padding < end; padding++) {
103                 if (!strcmp(padding->name, name))
104                         return padding;
105         }
106
107         return NULL;
108 }