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 | |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 6 | #include <log.h> |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 7 | #include <malloc.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 9 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 10 | #include <image.h> |
Simon Glass | c5a68d2 | 2021-09-25 07:03:19 -0600 | [diff] [blame] | 11 | #include <relocate.h> |
Alexandru Gagniuc | ed6c9e0 | 2021-02-19 12:45:12 -0600 | [diff] [blame] | 12 | #include <u-boot/ecdsa.h> |
Jeroen Hofstee | 2b9912e | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 13 | #include <u-boot/rsa.h> |
Alexandru Gagniuc | 0bcb28d | 2021-02-19 12:45:10 -0600 | [diff] [blame] | 14 | #include <u-boot/hash-checksum.h> |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 15 | |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 16 | #define IMAGE_MAX_HASHED_NODES 100 |
| 17 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 18 | struct checksum_algo checksum_algos[] = { |
Sean Anderson | 14e110a | 2024-02-15 12:12:18 -0500 | [diff] [blame] | 19 | #if CONFIG_IS_ENABLED(SHA1) |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 20 | { |
Masahiro Yamada | 8ec87df | 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 | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 25 | .calculate = hash_calculate, |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 26 | }, |
Sean Anderson | 14e110a | 2024-02-15 12:12:18 -0500 | [diff] [blame] | 27 | #endif |
| 28 | #if CONFIG_IS_ENABLED(SHA256) |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 29 | { |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 30 | .name = "sha256", |
| 31 | .checksum_len = SHA256_SUM_LEN, |
| 32 | .der_len = SHA256_DER_LEN, |
| 33 | .der_prefix = sha256_der_prefix, |
Masahiro Yamada | 8ec87df | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 34 | .calculate = hash_calculate, |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 35 | }, |
Sean Anderson | 14e110a | 2024-02-15 12:12:18 -0500 | [diff] [blame] | 36 | #endif |
| 37 | #if CONFIG_IS_ENABLED(SHA384) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 38 | { |
| 39 | .name = "sha384", |
| 40 | .checksum_len = SHA384_SUM_LEN, |
| 41 | .der_len = SHA384_DER_LEN, |
| 42 | .der_prefix = sha384_der_prefix, |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 43 | .calculate = hash_calculate, |
| 44 | }, |
| 45 | #endif |
Sean Anderson | 14e110a | 2024-02-15 12:12:18 -0500 | [diff] [blame] | 46 | #if CONFIG_IS_ENABLED(SHA512) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 47 | { |
| 48 | .name = "sha512", |
| 49 | .checksum_len = SHA512_SUM_LEN, |
| 50 | .der_len = SHA512_DER_LEN, |
| 51 | .der_prefix = sha512_der_prefix, |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 52 | .calculate = hash_calculate, |
| 53 | }, |
| 54 | #endif |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 55 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 56 | }; |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 57 | |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 58 | struct checksum_algo *image_get_checksum_algo(const char *full_name) |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 59 | { |
| 60 | int i; |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 61 | const char *name; |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 62 | |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 63 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 64 | name = checksum_algos[i].name; |
| 65 | /* Make sure names match and next char is a comma */ |
| 66 | if (!strncmp(name, full_name, strlen(name)) && |
| 67 | full_name[strlen(name)] == ',') |
| 68 | return &checksum_algos[i]; |
| 69 | } |
| 70 | |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | struct crypto_algo *image_get_crypto_algo(const char *full_name) |
| 75 | { |
Alexandru Gagniuc | 0980164 | 2021-07-14 17:05:39 -0500 | [diff] [blame] | 76 | struct crypto_algo *crypto, *end; |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 77 | const char *name; |
| 78 | |
| 79 | /* Move name to after the comma */ |
| 80 | name = strchr(full_name, ','); |
| 81 | if (!name) |
| 82 | return NULL; |
| 83 | name += 1; |
| 84 | |
Alexandru Gagniuc | 0980164 | 2021-07-14 17:05:39 -0500 | [diff] [blame] | 85 | crypto = ll_entry_start(struct crypto_algo, cryptos); |
| 86 | end = ll_entry_end(struct crypto_algo, cryptos); |
| 87 | for (; crypto < end; crypto++) { |
| 88 | if (!strcmp(crypto->name, name)) |
| 89 | return crypto; |
| 90 | } |
| 91 | |
| 92 | /* Not found */ |
Simon Glass | 3e569a6 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 95 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 96 | struct padding_algo *image_get_padding_algo(const char *name) |
| 97 | { |
Alexandru Gagniuc | de41f0e | 2021-08-18 17:49:02 -0500 | [diff] [blame] | 98 | struct padding_algo *padding, *end; |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 99 | |
| 100 | if (!name) |
| 101 | return NULL; |
| 102 | |
Alexandru Gagniuc | de41f0e | 2021-08-18 17:49:02 -0500 | [diff] [blame] | 103 | padding = ll_entry_start(struct padding_algo, paddings); |
| 104 | end = ll_entry_end(struct padding_algo, paddings); |
| 105 | for (; padding < end; padding++) { |
| 106 | if (!strcmp(padding->name, name)) |
| 107 | return padding; |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return NULL; |
| 111 | } |