Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifdef USE_HOSTCC |
| 7 | #include "mkimage.h" |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 8 | #include <fdt_support.h> |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 9 | #include <time.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <linux/libfdt.h> |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 11 | #else |
| 12 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 17 | #endif /* !USE_HOSTCC*/ |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 18 | #include <image.h> |
Alexandru Gagniuc | ed6c9e0 | 2021-02-19 12:45:12 -0600 | [diff] [blame] | 19 | #include <u-boot/ecdsa.h> |
Jeroen Hofstee | 2b9912e | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 20 | #include <u-boot/rsa.h> |
Alexandru Gagniuc | 0bcb28d | 2021-02-19 12:45:10 -0600 | [diff] [blame] | 21 | #include <u-boot/hash-checksum.h> |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 22 | |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 23 | #define IMAGE_MAX_HASHED_NODES 100 |
| 24 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 25 | struct checksum_algo checksum_algos[] = { |
| 26 | { |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 27 | .name = "sha1", |
| 28 | .checksum_len = SHA1_SUM_LEN, |
| 29 | .der_len = SHA1_DER_LEN, |
| 30 | .der_prefix = sha1_der_prefix, |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 31 | #if IMAGE_ENABLE_SIGN |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 32 | .calculate_sign = EVP_sha1, |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 33 | #endif |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 34 | .calculate = hash_calculate, |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 35 | }, |
| 36 | { |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 37 | .name = "sha256", |
| 38 | .checksum_len = SHA256_SUM_LEN, |
| 39 | .der_len = SHA256_DER_LEN, |
| 40 | .der_prefix = sha256_der_prefix, |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 41 | #if IMAGE_ENABLE_SIGN |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 42 | .calculate_sign = EVP_sha256, |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 43 | #endif |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 44 | .calculate = hash_calculate, |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 45 | }, |
| 46 | #ifdef CONFIG_SHA384 |
| 47 | { |
| 48 | .name = "sha384", |
| 49 | .checksum_len = SHA384_SUM_LEN, |
| 50 | .der_len = SHA384_DER_LEN, |
| 51 | .der_prefix = sha384_der_prefix, |
| 52 | #if IMAGE_ENABLE_SIGN |
| 53 | .calculate_sign = EVP_sha384, |
| 54 | #endif |
| 55 | .calculate = hash_calculate, |
| 56 | }, |
| 57 | #endif |
| 58 | #ifdef CONFIG_SHA512 |
| 59 | { |
| 60 | .name = "sha512", |
| 61 | .checksum_len = SHA512_SUM_LEN, |
| 62 | .der_len = SHA512_DER_LEN, |
| 63 | .der_prefix = sha512_der_prefix, |
| 64 | #if IMAGE_ENABLE_SIGN |
| 65 | .calculate_sign = EVP_sha512, |
| 66 | #endif |
| 67 | .calculate = hash_calculate, |
| 68 | }, |
| 69 | #endif |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 70 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 71 | }; |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 72 | |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 73 | struct crypto_algo crypto_algos[] = { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 74 | { |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 75 | .name = "rsa2048", |
| 76 | .key_len = RSA2048_BYTES, |
| 77 | .sign = rsa_sign, |
| 78 | .add_verify_data = rsa_add_verify_data, |
| 79 | .verify = rsa_verify, |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 80 | }, |
| 81 | { |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 82 | .name = "rsa4096", |
| 83 | .key_len = RSA4096_BYTES, |
| 84 | .sign = rsa_sign, |
| 85 | .add_verify_data = rsa_add_verify_data, |
| 86 | .verify = rsa_verify, |
Alexandru Gagniuc | ed6c9e0 | 2021-02-19 12:45:12 -0600 | [diff] [blame] | 87 | }, |
| 88 | { |
| 89 | .name = "ecdsa256", |
| 90 | .key_len = ECDSA256_BYTES, |
| 91 | .sign = ecdsa_sign, |
| 92 | .add_verify_data = ecdsa_add_verify_data, |
| 93 | .verify = ecdsa_verify, |
| 94 | }, |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 97 | struct padding_algo padding_algos[] = { |
| 98 | { |
| 99 | .name = "pkcs-1.5", |
| 100 | .verify = padding_pkcs_15_verify, |
| 101 | }, |
Philippe Reynes | 061daa0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 102 | #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT |
| 103 | { |
| 104 | .name = "pss", |
| 105 | .verify = padding_pss_verify, |
| 106 | } |
| 107 | #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */ |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 108 | }; |
| 109 | |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 110 | struct checksum_algo *image_get_checksum_algo(const char *full_name) |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 111 | { |
| 112 | int i; |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 113 | const char *name; |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 114 | |
T Karthik Reddy | 1ed8c13 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 115 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 116 | static bool done; |
| 117 | |
| 118 | if (!done) { |
| 119 | done = true; |
| 120 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 121 | checksum_algos[i].name += gd->reloc_off; |
| 122 | #if IMAGE_ENABLE_SIGN |
| 123 | checksum_algos[i].calculate_sign += gd->reloc_off; |
| 124 | #endif |
| 125 | checksum_algos[i].calculate += gd->reloc_off; |
| 126 | } |
| 127 | } |
| 128 | #endif |
| 129 | |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 130 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 131 | name = checksum_algos[i].name; |
| 132 | /* Make sure names match and next char is a comma */ |
| 133 | if (!strncmp(name, full_name, strlen(name)) && |
| 134 | full_name[strlen(name)] == ',') |
| 135 | return &checksum_algos[i]; |
| 136 | } |
| 137 | |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | struct crypto_algo *image_get_crypto_algo(const char *full_name) |
| 142 | { |
| 143 | int i; |
| 144 | const char *name; |
| 145 | |
T Karthik Reddy | 1ed8c13 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 146 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 147 | static bool done; |
| 148 | |
| 149 | if (!done) { |
| 150 | done = true; |
| 151 | for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { |
| 152 | crypto_algos[i].name += gd->reloc_off; |
| 153 | crypto_algos[i].sign += gd->reloc_off; |
| 154 | crypto_algos[i].add_verify_data += gd->reloc_off; |
| 155 | crypto_algos[i].verify += gd->reloc_off; |
| 156 | } |
| 157 | } |
| 158 | #endif |
| 159 | |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 160 | /* Move name to after the comma */ |
| 161 | name = strchr(full_name, ','); |
| 162 | if (!name) |
| 163 | return NULL; |
| 164 | name += 1; |
| 165 | |
| 166 | for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { |
| 167 | if (!strcmp(crypto_algos[i].name, name)) |
| 168 | return &crypto_algos[i]; |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return NULL; |
| 172 | } |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 173 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 174 | struct padding_algo *image_get_padding_algo(const char *name) |
| 175 | { |
| 176 | int i; |
| 177 | |
| 178 | if (!name) |
| 179 | return NULL; |
| 180 | |
| 181 | for (i = 0; i < ARRAY_SIZE(padding_algos); i++) { |
| 182 | if (!strcmp(padding_algos[i].name, name)) |
| 183 | return &padding_algos[i]; |
| 184 | } |
| 185 | |
| 186 | return NULL; |
| 187 | } |