Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 10 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 11 | #include <image.h> |
Alexandru Gagniuc | 3ebd200 | 2021-02-19 12:45:12 -0600 | [diff] [blame] | 12 | #include <u-boot/ecdsa.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 13 | #include <u-boot/rsa.h> |
Alexandru Gagniuc | db182c4 | 2021-02-19 12:45:10 -0600 | [diff] [blame] | 14 | #include <u-boot/hash-checksum.h> |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 15 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 16 | #define IMAGE_MAX_HASHED_NODES 100 |
| 17 | |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 18 | struct checksum_algo checksum_algos[] = { |
| 19 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 20 | .name = "sha1", |
| 21 | .checksum_len = SHA1_SUM_LEN, |
| 22 | .der_len = SHA1_DER_LEN, |
| 23 | .der_prefix = sha1_der_prefix, |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 24 | .calculate = hash_calculate, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 25 | }, |
| 26 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 27 | .name = "sha256", |
| 28 | .checksum_len = SHA256_SUM_LEN, |
| 29 | .der_len = SHA256_DER_LEN, |
| 30 | .der_prefix = sha256_der_prefix, |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 31 | .calculate = hash_calculate, |
Reuben Dowle | 1908fd9 | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 32 | }, |
| 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 Dowle | 1908fd9 | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 39 | .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 Dowle | 1908fd9 | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 48 | .calculate = hash_calculate, |
| 49 | }, |
| 50 | #endif |
Andrew Duda | 06ca6d6 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 51 | |
| 52 | }; |
| 53 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 54 | struct padding_algo padding_algos[] = { |
| 55 | { |
| 56 | .name = "pkcs-1.5", |
| 57 | .verify = padding_pkcs_15_verify, |
| 58 | }, |
Simon Glass | 66b00c8 | 2021-07-14 17:05:31 -0500 | [diff] [blame] | 59 | #ifdef CONFIG_FIT_RSASSA_PSS |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 60 | { |
| 61 | .name = "pss", |
| 62 | .verify = padding_pss_verify, |
| 63 | } |
Simon Glass | 66b00c8 | 2021-07-14 17:05:31 -0500 | [diff] [blame] | 64 | #endif /* CONFIG_FIT_RSASSA_PSS */ |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 67 | struct checksum_algo *image_get_checksum_algo(const char *full_name) |
| 68 | { |
| 69 | int i; |
| 70 | const char *name; |
| 71 | |
Alexandru Gagniuc | afb1bca | 2021-07-14 17:05:38 -0500 | [diff] [blame] | 72 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
T Karthik Reddy | 0164dc8 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 73 | static bool done; |
| 74 | |
| 75 | if (!done) { |
| 76 | done = true; |
| 77 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 78 | checksum_algos[i].name += gd->reloc_off; |
T Karthik Reddy | 0164dc8 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 79 | checksum_algos[i].calculate += gd->reloc_off; |
| 80 | } |
| 81 | } |
| 82 | #endif |
| 83 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 84 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 85 | name = checksum_algos[i].name; |
| 86 | /* Make sure names match and next char is a comma */ |
| 87 | if (!strncmp(name, full_name, strlen(name)) && |
| 88 | full_name[strlen(name)] == ',') |
| 89 | return &checksum_algos[i]; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 90 | } |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 91 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 94 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 95 | struct crypto_algo *image_get_crypto_algo(const char *full_name) |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 96 | { |
Alexandru Gagniuc | b04e918 | 2021-07-14 17:05:39 -0500 | [diff] [blame] | 97 | struct crypto_algo *crypto, *end; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 98 | const char *name; |
| 99 | |
Alexandru Gagniuc | b2c5f9b | 2021-07-14 17:05:46 -0500 | [diff] [blame] | 100 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 101 | static bool done; |
| 102 | |
| 103 | if (!done) { |
| 104 | crypto = ll_entry_start(struct crypto_algo, cryptos); |
| 105 | end = ll_entry_end(struct crypto_algo, cryptos); |
| 106 | for (; crypto < end; crypto++) { |
| 107 | crypto->name += gd->reloc_off; |
| 108 | crypto->verify += gd->reloc_off; |
| 109 | } |
| 110 | } |
| 111 | #endif |
| 112 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 113 | /* Move name to after the comma */ |
| 114 | name = strchr(full_name, ','); |
| 115 | if (!name) |
| 116 | return NULL; |
| 117 | name += 1; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 118 | |
Alexandru Gagniuc | b04e918 | 2021-07-14 17:05:39 -0500 | [diff] [blame] | 119 | crypto = ll_entry_start(struct crypto_algo, cryptos); |
| 120 | end = ll_entry_end(struct crypto_algo, cryptos); |
| 121 | for (; crypto < end; crypto++) { |
| 122 | if (!strcmp(crypto->name, name)) |
| 123 | return crypto; |
| 124 | } |
| 125 | |
| 126 | /* Not found */ |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 127 | return NULL; |
| 128 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 129 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 130 | struct padding_algo *image_get_padding_algo(const char *name) |
| 131 | { |
| 132 | int i; |
| 133 | |
| 134 | if (!name) |
| 135 | return NULL; |
| 136 | |
| 137 | for (i = 0; i < ARRAY_SIZE(padding_algos); i++) { |
| 138 | if (!strcmp(padding_algos[i].name, name)) |
| 139 | return &padding_algos[i]; |
| 140 | } |
| 141 | |
| 142 | return NULL; |
| 143 | } |