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 | |
| 6 | #ifdef USE_HOSTCC |
| 7 | #include "mkimage.h" |
| 8 | #include <time.h> |
| 9 | #else |
| 10 | #include <common.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 13 | #endif /* !USE_HOSTCC*/ |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 14 | #include <image.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 15 | #include <u-boot/rsa.h> |
| 16 | #include <u-boot/rsa-checksum.h> |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 17 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 18 | #define IMAGE_MAX_HASHED_NODES 100 |
| 19 | |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 20 | struct checksum_algo checksum_algos[] = { |
| 21 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 22 | .name = "sha1", |
| 23 | .checksum_len = SHA1_SUM_LEN, |
| 24 | .der_len = SHA1_DER_LEN, |
| 25 | .der_prefix = sha1_der_prefix, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 26 | #if IMAGE_ENABLE_SIGN |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 27 | .calculate_sign = EVP_sha1, |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 28 | #endif |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 29 | .calculate = hash_calculate, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 30 | }, |
| 31 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 32 | .name = "sha256", |
| 33 | .checksum_len = SHA256_SUM_LEN, |
| 34 | .der_len = SHA256_DER_LEN, |
| 35 | .der_prefix = sha256_der_prefix, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 36 | #if IMAGE_ENABLE_SIGN |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 37 | .calculate_sign = EVP_sha256, |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 38 | #endif |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 39 | .calculate = hash_calculate, |
Andrew Duda | 06ca6d6 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | }; |
| 43 | |
| 44 | struct crypto_algo crypto_algos[] = { |
| 45 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 46 | .name = "rsa2048", |
| 47 | .key_len = RSA2048_BYTES, |
| 48 | .sign = rsa_sign, |
| 49 | .add_verify_data = rsa_add_verify_data, |
| 50 | .verify = rsa_verify, |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 51 | }, |
| 52 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 53 | .name = "rsa4096", |
| 54 | .key_len = RSA4096_BYTES, |
| 55 | .sign = rsa_sign, |
| 56 | .add_verify_data = rsa_add_verify_data, |
| 57 | .verify = rsa_verify, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 58 | } |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 59 | |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 60 | }; |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 61 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 62 | struct padding_algo padding_algos[] = { |
| 63 | { |
| 64 | .name = "pkcs-1.5", |
| 65 | .verify = padding_pkcs_15_verify, |
| 66 | }, |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 67 | #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT |
| 68 | { |
| 69 | .name = "pss", |
| 70 | .verify = padding_pss_verify, |
| 71 | } |
| 72 | #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */ |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 75 | struct checksum_algo *image_get_checksum_algo(const char *full_name) |
| 76 | { |
| 77 | int i; |
| 78 | const char *name; |
| 79 | |
T Karthik Reddy | 0164dc8 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 80 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 81 | static bool done; |
| 82 | |
| 83 | if (!done) { |
| 84 | done = true; |
| 85 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 86 | checksum_algos[i].name += gd->reloc_off; |
| 87 | #if IMAGE_ENABLE_SIGN |
| 88 | checksum_algos[i].calculate_sign += gd->reloc_off; |
| 89 | #endif |
| 90 | checksum_algos[i].calculate += gd->reloc_off; |
| 91 | } |
| 92 | } |
| 93 | #endif |
| 94 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 95 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 96 | name = checksum_algos[i].name; |
| 97 | /* Make sure names match and next char is a comma */ |
| 98 | if (!strncmp(name, full_name, strlen(name)) && |
| 99 | full_name[strlen(name)] == ',') |
| 100 | return &checksum_algos[i]; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 101 | } |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 102 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 105 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 106 | struct crypto_algo *image_get_crypto_algo(const char *full_name) |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 107 | { |
| 108 | int i; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 109 | const char *name; |
| 110 | |
T Karthik Reddy | 0164dc8 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 111 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 112 | static bool done; |
| 113 | |
| 114 | if (!done) { |
| 115 | done = true; |
| 116 | for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { |
| 117 | crypto_algos[i].name += gd->reloc_off; |
| 118 | crypto_algos[i].sign += gd->reloc_off; |
| 119 | crypto_algos[i].add_verify_data += gd->reloc_off; |
| 120 | crypto_algos[i].verify += gd->reloc_off; |
| 121 | } |
| 122 | } |
| 123 | #endif |
| 124 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 125 | /* Move name to after the comma */ |
| 126 | name = strchr(full_name, ','); |
| 127 | if (!name) |
| 128 | return NULL; |
| 129 | name += 1; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 130 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 131 | for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { |
| 132 | if (!strcmp(crypto_algos[i].name, name)) |
| 133 | return &crypto_algos[i]; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | return NULL; |
| 137 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 138 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 139 | struct padding_algo *image_get_padding_algo(const char *name) |
| 140 | { |
| 141 | int i; |
| 142 | |
| 143 | if (!name) |
| 144 | return NULL; |
| 145 | |
| 146 | for (i = 0; i < ARRAY_SIZE(padding_algos); i++) { |
| 147 | if (!strcmp(padding_algos[i].name, name)) |
| 148 | return &padding_algos[i]; |
| 149 | } |
| 150 | |
| 151 | return NULL; |
| 152 | } |