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