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