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