Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 3 | * |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 4 | * Copyright (c) 2018-2019 JUUL Labs |
| 5 | * Copyright (c) 2019 Arm Limited |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "mcuboot_config/mcuboot_config.h" |
| 9 | |
| 10 | #if defined(MCUBOOT_ENC_IMAGES) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | #include <inttypes.h> |
| 13 | #include <string.h> |
| 14 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 15 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 16 | #include "mbedtls/rsa.h" |
Fabio Utzig | 6f4d8f8 | 2019-12-12 18:46:06 -0300 | [diff] [blame] | 17 | #include "mbedtls/rsa_internal.h" |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 18 | #include "mbedtls/asn1.h" |
| 19 | #endif |
| 20 | |
| 21 | #if defined(MCUBOOT_ENCRYPT_KW) |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 22 | #include "bootutil/crypto/aes_kw.h" |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 23 | #endif |
| 24 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 25 | #if defined(MCUBOOT_ENCRYPT_EC256) |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 26 | #include "bootutil/crypto/ecdh_p256.h" |
| 27 | #endif |
| 28 | |
| 29 | #if defined(MCUBOOT_ENCRYPT_X25519) |
| 30 | #include "bootutil/crypto/ecdh_x25519.h" |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | #if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 34 | #include "bootutil/crypto/sha256.h" |
| 35 | #include "bootutil/crypto/hmac_sha256.h" |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 36 | #include "mbedtls/oid.h" |
| 37 | #include "mbedtls/asn1.h" |
| 38 | #endif |
| 39 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 40 | #include "bootutil/image.h" |
| 41 | #include "bootutil/enc_key.h" |
| 42 | #include "bootutil/sign_key.h" |
| 43 | |
| 44 | #include "bootutil_priv.h" |
| 45 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 46 | #if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) |
| 47 | #if defined(_compare) |
| 48 | static inline int bootutil_constant_time_compare(const uint8_t *a, const uint8_t *b, size_t size) |
| 49 | { |
| 50 | return _compare(a, b, size); |
| 51 | } |
| 52 | #else |
| 53 | static int bootutil_constant_time_compare(const uint8_t *a, const uint8_t *b, size_t size) |
| 54 | { |
| 55 | const uint8_t *tempa = a; |
| 56 | const uint8_t *tempb = b; |
| 57 | uint8_t result = 0; |
| 58 | unsigned int i; |
| 59 | |
| 60 | for (i = 0; i < size; i++) { |
| 61 | result |= tempa[i] ^ tempb[i]; |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | #endif |
| 66 | #endif |
| 67 | |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 68 | #if defined(MCUBOOT_ENCRYPT_KW) |
Fabio Utzig | 6ace9ee | 2019-08-19 11:11:07 -0300 | [diff] [blame] | 69 | static int |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 70 | key_unwrap(const uint8_t *wrapped, uint8_t *enckey) |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 71 | { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 72 | bootutil_aes_kw_context aes_kw; |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 73 | int rc; |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 74 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 75 | bootutil_aes_kw_init(&aes_kw); |
| 76 | rc = bootutil_aes_kw_set_unwrap_key(&aes_kw, bootutil_enc_key.key, *bootutil_enc_key.len); |
| 77 | if (rc != 0) { |
| 78 | goto done; |
| 79 | } |
| 80 | rc = bootutil_aes_kw_unwrap(&aes_kw, wrapped, TLV_ENC_KW_SZ, enckey, BOOT_ENC_KEY_SIZE); |
| 81 | if (rc != 0) { |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 82 | goto done; |
| 83 | } |
| 84 | |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 85 | done: |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 86 | bootutil_aes_kw_drop(&aes_kw); |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 87 | return rc; |
| 88 | } |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 89 | #endif /* MCUBOOT_ENCRYPT_KW */ |
| 90 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 91 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 92 | static int |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 93 | parse_rsa_enckey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 94 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 95 | size_t len; |
| 96 | |
Fabio Utzig | 6f4d8f8 | 2019-12-12 18:46:06 -0300 | [diff] [blame] | 97 | if (mbedtls_asn1_get_tag(p, end, &len, |
| 98 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if (*p + len != end) { |
| 103 | return -2; |
| 104 | } |
| 105 | |
Fabio Utzig | 6f4d8f8 | 2019-12-12 18:46:06 -0300 | [diff] [blame] | 106 | /* Non-optional fields. */ |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 107 | if ( /* version */ |
| 108 | mbedtls_asn1_get_int(p, end, &ctx->ver) != 0 || |
| 109 | /* public modulus */ |
| 110 | mbedtls_asn1_get_mpi(p, end, &ctx->N) != 0 || |
| 111 | /* public exponent */ |
| 112 | mbedtls_asn1_get_mpi(p, end, &ctx->E) != 0 || |
| 113 | /* private exponent */ |
| 114 | mbedtls_asn1_get_mpi(p, end, &ctx->D) != 0 || |
| 115 | /* primes */ |
| 116 | mbedtls_asn1_get_mpi(p, end, &ctx->P) != 0 || |
Fabio Utzig | 6f4d8f8 | 2019-12-12 18:46:06 -0300 | [diff] [blame] | 117 | mbedtls_asn1_get_mpi(p, end, &ctx->Q) != 0) { |
| 118 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 119 | return -3; |
| 120 | } |
| 121 | |
Fabio Utzig | 6f4d8f8 | 2019-12-12 18:46:06 -0300 | [diff] [blame] | 122 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 123 | /* |
| 124 | * DP/DQ/QP are only used inside mbedTLS if it was built with the |
| 125 | * Chinese Remainder Theorem enabled (default). In case it is disabled |
| 126 | * we parse, or if not available, we calculate those values. |
| 127 | */ |
| 128 | if (*p < end) { |
| 129 | if ( /* d mod (p-1) and d mod (q-1) */ |
| 130 | mbedtls_asn1_get_mpi(p, end, &ctx->DP) != 0 || |
| 131 | mbedtls_asn1_get_mpi(p, end, &ctx->DQ) != 0 || |
| 132 | /* q ^ (-1) mod p */ |
| 133 | mbedtls_asn1_get_mpi(p, end, &ctx->QP) != 0) { |
| 134 | |
| 135 | return -4; |
| 136 | } |
| 137 | } else { |
| 138 | if (mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D, |
| 139 | &ctx->DP, &ctx->DQ, &ctx->QP) != 0) { |
| 140 | return -5; |
| 141 | } |
| 142 | } |
| 143 | #endif |
| 144 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 145 | ctx->len = mbedtls_mpi_size(&ctx->N); |
| 146 | |
Fabio Utzig | 6f4d8f8 | 2019-12-12 18:46:06 -0300 | [diff] [blame] | 147 | if (mbedtls_rsa_check_privkey(ctx) != 0) { |
| 148 | return -6; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | #endif |
| 154 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 155 | #if defined(MCUBOOT_ENCRYPT_EC256) |
| 156 | static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 157 | static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1; |
| 158 | |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 159 | #define SHARED_KEY_LEN NUM_ECC_BYTES |
| 160 | #define PRIV_KEY_LEN NUM_ECC_BYTES |
| 161 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 162 | /* |
| 163 | * Parses the output of `imgtool keygen`, which produces a PKCS#8 elliptic |
| 164 | * curve keypair. See RFC5208 and RFC5915. |
| 165 | */ |
| 166 | static int |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 167 | parse_ec256_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key) |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 168 | { |
| 169 | int rc; |
| 170 | size_t len; |
| 171 | int version; |
| 172 | mbedtls_asn1_buf alg; |
| 173 | mbedtls_asn1_buf param; |
| 174 | |
| 175 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 176 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | if (*p + len != end) { |
| 181 | return -2; |
| 182 | } |
| 183 | |
| 184 | version = 0; |
| 185 | if (mbedtls_asn1_get_int(p, end, &version) || version != 0) { |
| 186 | return -3; |
| 187 | } |
| 188 | |
| 189 | if ((rc = mbedtls_asn1_get_alg(p, end, &alg, ¶m)) != 0) { |
| 190 | return -5; |
| 191 | } |
| 192 | |
| 193 | if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| 194 | memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| 195 | return -6; |
| 196 | } |
| 197 | if (param.len != sizeof(ec_secp256r1_oid) - 1 || |
| 198 | memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) { |
| 199 | return -7; |
| 200 | } |
| 201 | |
| 202 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 203 | return -8; |
| 204 | } |
| 205 | |
| 206 | /* RFC5915 - ECPrivateKey */ |
| 207 | |
| 208 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 209 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 210 | return -9; |
| 211 | } |
| 212 | |
| 213 | version = 0; |
| 214 | if (mbedtls_asn1_get_int(p, end, &version) || version != 1) { |
| 215 | return -10; |
| 216 | } |
| 217 | |
| 218 | /* privateKey */ |
| 219 | |
| 220 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 221 | return -11; |
| 222 | } |
| 223 | |
| 224 | if (len != NUM_ECC_BYTES) { |
| 225 | return -12; |
| 226 | } |
| 227 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 228 | memcpy(private_key, *p, len); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 229 | |
| 230 | /* publicKey usually follows but is not parsed here */ |
| 231 | |
| 232 | return 0; |
| 233 | } |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 234 | #endif /* defined(MCUBOOT_ENCRYPT_EC256) */ |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 235 | |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 236 | #if defined(MCUBOOT_ENCRYPT_X25519) |
| 237 | #define X25519_OID "\x6e" |
| 238 | static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \ |
| 239 | MBEDTLS_OID_ORG_GOV X25519_OID; |
| 240 | |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 241 | #define SHARED_KEY_LEN 32 |
| 242 | #define PRIV_KEY_LEN 32 |
| 243 | |
| 244 | static int |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 245 | parse_x25519_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key) |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 246 | { |
| 247 | size_t len; |
| 248 | int version; |
| 249 | mbedtls_asn1_buf alg; |
| 250 | mbedtls_asn1_buf param; |
| 251 | |
| 252 | if (mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 253 | MBEDTLS_ASN1_SEQUENCE) != 0) { |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | if (*p + len != end) { |
| 258 | return -2; |
| 259 | } |
| 260 | |
| 261 | version = 0; |
| 262 | if (mbedtls_asn1_get_int(p, end, &version) || version != 0) { |
| 263 | return -3; |
| 264 | } |
| 265 | |
| 266 | if (mbedtls_asn1_get_alg(p, end, &alg, ¶m) != 0) { |
| 267 | return -4; |
| 268 | } |
| 269 | |
| 270 | if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| 271 | memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| 272 | return -5; |
| 273 | } |
| 274 | |
| 275 | if (mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING) != 0) { |
| 276 | return -6; |
| 277 | } |
| 278 | |
| 279 | if (mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING) != 0) { |
| 280 | return -7; |
| 281 | } |
| 282 | |
| 283 | if (len != PRIV_KEY_LEN) { |
| 284 | return -8; |
| 285 | } |
| 286 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 287 | memcpy(private_key, *p, PRIV_KEY_LEN); |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | #endif /* defined(MCUBOOT_ENCRYPT_X25519) */ |
| 291 | |
| 292 | #if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 293 | /* |
| 294 | * HKDF as described by RFC5869. |
| 295 | * |
| 296 | * @param ikm The input data to be derived. |
| 297 | * @param ikm_len Length of the input data. |
| 298 | * @param info An information tag. |
| 299 | * @param info_len Length of the information tag. |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 300 | * @param salt An salt tag. |
| 301 | * @param salt_len Length of the salt tag. |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 302 | * @param okm Output of the KDF computation. |
| 303 | * @param okm_len On input the requested length; on output the generated length |
| 304 | */ |
| 305 | static int |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 306 | hkdf(const uint8_t *ikm, uint16_t ikm_len, const uint8_t *info, uint16_t info_len, |
| 307 | const uint8_t *salt, uint16_t salt_len, uint8_t *okm, uint16_t *okm_len) |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 308 | { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 309 | bootutil_hmac_sha256_context hmac; |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 310 | uint8_t prk[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
| 311 | uint8_t T[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 312 | uint16_t off; |
| 313 | uint16_t len; |
| 314 | uint8_t counter; |
| 315 | bool first; |
| 316 | int rc; |
| 317 | |
| 318 | /* |
| 319 | * Extract |
| 320 | */ |
| 321 | |
| 322 | if (ikm == NULL || okm == NULL || ikm_len == 0) { |
| 323 | return -1; |
| 324 | } |
| 325 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 326 | bootutil_hmac_sha256_init(&hmac); |
| 327 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 328 | rc = bootutil_hmac_sha256_set_key(&hmac, salt, salt_len); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 329 | if (rc != 0) { |
| 330 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 331 | } |
| 332 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 333 | rc = bootutil_hmac_sha256_update(&hmac, ikm, ikm_len); |
| 334 | if (rc != 0) { |
| 335 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 336 | } |
| 337 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 338 | rc = bootutil_hmac_sha256_finish(&hmac, prk, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
| 339 | if (rc != 0) { |
| 340 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Expand |
| 345 | */ |
| 346 | |
| 347 | len = *okm_len; |
| 348 | counter = 1; |
| 349 | first = true; |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 350 | for (off = 0; len > 0; off += BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE, ++counter) { |
| 351 | bootutil_hmac_sha256_init(&hmac); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 352 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 353 | rc = bootutil_hmac_sha256_set_key(&hmac, prk, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
| 354 | if (rc != 0) { |
| 355 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | if (first) { |
| 359 | first = false; |
| 360 | } else { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 361 | rc = bootutil_hmac_sha256_update(&hmac, T, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
| 362 | if (rc != 0) { |
| 363 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 367 | rc = bootutil_hmac_sha256_update(&hmac, info, info_len); |
| 368 | if (rc != 0) { |
| 369 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 370 | } |
| 371 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 372 | rc = bootutil_hmac_sha256_update(&hmac, &counter, 1); |
| 373 | if (rc != 0) { |
| 374 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 375 | } |
| 376 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 377 | rc = bootutil_hmac_sha256_finish(&hmac, T, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
| 378 | if (rc != 0) { |
| 379 | goto error; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 380 | } |
| 381 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 382 | if (len > BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) { |
| 383 | memcpy(&okm[off], T, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
| 384 | len -= BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 385 | } else { |
| 386 | memcpy(&okm[off], T, len); |
| 387 | len = 0; |
| 388 | } |
| 389 | } |
| 390 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 391 | bootutil_hmac_sha256_drop(&hmac); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 392 | return 0; |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 393 | |
| 394 | error: |
| 395 | bootutil_hmac_sha256_drop(&hmac); |
| 396 | return -1; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 397 | } |
| 398 | #endif |
| 399 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 400 | int |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 401 | boot_enc_init(struct enc_key_data *enc_state, uint8_t slot) |
| 402 | { |
| 403 | bootutil_aes_ctr_init(&enc_state[slot].aes_ctr); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | int |
| 408 | boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot) |
| 409 | { |
| 410 | bootutil_aes_ctr_drop(&enc_state[slot].aes_ctr); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | int |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 415 | boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot, |
| 416 | const struct boot_status *bs) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 417 | { |
| 418 | int rc; |
| 419 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 420 | rc = bootutil_aes_ctr_set_key(&enc_state[slot].aes_ctr, bs->enckey[slot]); |
| 421 | if (rc != 0) { |
| 422 | boot_enc_drop(enc_state, slot); |
| 423 | enc_state[slot].valid = 0; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | enc_state[slot].valid = 1; |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 432 | #define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 433 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 434 | #if defined(MCUBOOT_ENCRYPT_RSA) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 435 | # define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048 |
| 436 | # define EXPECTED_ENC_EXT_LEN EXPECTED_ENC_TLV |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 437 | #elif defined(MCUBOOT_ENCRYPT_KW) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 438 | # define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW128 |
| 439 | # define EXPECTED_ENC_EXT_LEN EXPECTED_ENC_TLV |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 440 | #elif defined(MCUBOOT_ENCRYPT_EC256) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 441 | # define EXPECTED_ENC_TLV IMAGE_TLV_ENC_EC256 |
| 442 | # define EXPECTED_ENC_EXT_LEN (EXPECTED_ENC_LEN + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) |
Bohdan Kovalchuk | 25c7a0f | 2020-11-30 21:32:14 +0200 | [diff] [blame] | 443 | # define EC_PUBK_INDEX (0) |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 444 | # define EC_TAG_INDEX (65) |
| 445 | # define EC_CIPHERKEY_INDEX (65 + 32) |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 446 | _Static_assert(EC_CIPHERKEY_INDEX + 16 == EXPECTED_ENC_LEN, |
| 447 | "Please fix ECIES-P256 component indexes"); |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 448 | #elif defined(MCUBOOT_ENCRYPT_X25519) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 449 | # define EXPECTED_ENC_TLV IMAGE_TLV_ENC_X25519 |
| 450 | # define EXPECTED_ENC_EXT_LEN EXPECTED_ENC_TLV |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 451 | # define EC_PUBK_INDEX (0) |
| 452 | # define EC_TAG_INDEX (32) |
| 453 | # define EC_CIPHERKEY_INDEX (32 + 32) |
| 454 | _Static_assert(EC_CIPHERKEY_INDEX + 16 == EXPECTED_ENC_LEN, |
| 455 | "Please fix ECIES-X25519 component indexes"); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 456 | #endif |
| 457 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 458 | #ifndef EXPECTED_ENC_EXT_LEN |
| 459 | #define EXPECTED_ENC_EXT_LEN EXPECTED_ENC_LEN |
| 460 | #endif |
| 461 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 462 | /* |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 463 | * Decrypt an encryption key TLV. |
| 464 | * |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 465 | * @param buf An encryption TLV buffer red from flash |
| 466 | * @param sz An encryption TLV buffer data size |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 467 | * @param enckey An AES-128 key sized buffer to store to plain key. |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 468 | */ |
| 469 | int |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 470 | boot_enc_decrypt(const uint8_t *buf, uint8_t *enckey, uint32_t sz, uint8_t *enciv) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 471 | { |
| 472 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 473 | mbedtls_rsa_context rsa; |
| 474 | uint8_t *cp; |
| 475 | uint8_t *cpend; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 476 | size_t olen; |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 477 | #endif |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 478 | #if defined(MCUBOOT_ENCRYPT_EC256) |
| 479 | bootutil_ecdh_p256_context ecdh_p256; |
| 480 | #endif |
| 481 | #if defined(MCUBOOT_ENCRYPT_X25519) |
| 482 | bootutil_ecdh_x25519_context ecdh_x25519; |
| 483 | #endif |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 484 | #if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 485 | bootutil_hmac_sha256_context hmac; |
| 486 | bootutil_aes_ctr_context aes_ctr; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 487 | uint8_t salt[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 488 | uint8_t tag[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 489 | uint8_t shared[SHARED_KEY_LEN]; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 490 | uint8_t derived_key[BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE + BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE * 2]; |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 491 | uint8_t *cp; |
| 492 | uint8_t *cpend; |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 493 | uint8_t private_key[PRIV_KEY_LEN]; |
| 494 | uint8_t counter[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE]; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 495 | uint16_t len; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 496 | uint16_t out_len; |
| 497 | const uint8_t *my_salt = salt; |
| 498 | uint8_t *my_key_iv = counter; |
| 499 | uint8_t *my_counter = counter; |
| 500 | #else |
| 501 | (void)sz; |
| 502 | (void)enciv; |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 503 | #endif |
| 504 | int rc = -1; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 505 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 506 | #if defined(MCUBOOT_ENCRYPT_RSA) |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 507 | |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 508 | mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 509 | |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 510 | cp = (uint8_t *)bootutil_enc_key.key; |
| 511 | cpend = cp + *bootutil_enc_key.len; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 512 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 513 | rc = parse_rsa_enckey(&rsa, &cp, cpend); |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 514 | if (rc) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 515 | mbedtls_rsa_free(&rsa); |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 516 | return rc; |
| 517 | } |
| 518 | |
| 519 | rc = mbedtls_rsa_rsaes_oaep_decrypt(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, |
| 520 | NULL, 0, &olen, buf, enckey, BOOT_ENC_KEY_SIZE); |
| 521 | mbedtls_rsa_free(&rsa); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 522 | |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 523 | #endif /* defined(MCUBOOT_ENCRYPT_RSA) */ |
| 524 | |
| 525 | #if defined(MCUBOOT_ENCRYPT_KW) |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 526 | |
Fabio Utzig | 233af7d | 2019-08-26 12:06:16 -0300 | [diff] [blame] | 527 | assert(*bootutil_enc_key.len == 16); |
| 528 | rc = key_unwrap(buf, enckey); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 529 | |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 530 | #endif /* defined(MCUBOOT_ENCRYPT_KW) */ |
| 531 | |
| 532 | #if defined(MCUBOOT_ENCRYPT_EC256) |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 533 | |
| 534 | cp = (uint8_t *)bootutil_enc_key.key; |
| 535 | cpend = cp + *bootutil_enc_key.len; |
| 536 | |
| 537 | /* |
| 538 | * Load the stored EC256 decryption private key |
| 539 | */ |
| 540 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 541 | rc = parse_ec256_enckey(&cp, cpend, private_key); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 542 | if (rc) { |
| 543 | return rc; |
| 544 | } |
| 545 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 546 | /* |
| 547 | * First "element" in the TLV is the curve point (public key) |
| 548 | */ |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 549 | bootutil_ecdh_p256_init(&ecdh_p256); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 550 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 551 | rc = bootutil_ecdh_p256_shared_secret(&ecdh_p256, &buf[EC_PUBK_INDEX], private_key, shared); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 552 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 553 | bootutil_ecdh_p256_drop(&ecdh_p256); |
| 554 | if (rc != 0) { |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 555 | return -1; |
| 556 | } |
| 557 | |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 558 | #endif /* defined(MCUBOOT_ENCRYPT_EC256) */ |
| 559 | |
| 560 | #if defined(MCUBOOT_ENCRYPT_X25519) |
| 561 | |
| 562 | cp = (uint8_t *)bootutil_enc_key.key; |
| 563 | cpend = cp + *bootutil_enc_key.len; |
| 564 | |
| 565 | /* |
| 566 | * Load the stored X25519 decryption private key |
| 567 | */ |
| 568 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 569 | rc = parse_x25519_enckey(&cp, cpend, private_key); |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 570 | if (rc) { |
| 571 | return rc; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * First "element" in the TLV is the curve point (public key) |
| 576 | */ |
| 577 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 578 | bootutil_ecdh_x25519_init(&ecdh_x25519); |
| 579 | |
| 580 | rc = bootutil_ecdh_x25519_shared_secret(&ecdh_x25519, &buf[EC_PUBK_INDEX], private_key, shared); |
| 581 | bootutil_ecdh_x25519_drop(&ecdh_x25519); |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 582 | if (!rc) { |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | #endif /* defined(MCUBOOT_ENCRYPT_X25519) */ |
| 587 | |
| 588 | #if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) |
| 589 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 590 | /* |
| 591 | * Expand shared secret to create keys for AES-128-CTR + HMAC-SHA256 |
| 592 | */ |
| 593 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 594 | /* Prepare for default encryption scheme with zero salt and AES IVs */ |
| 595 | memset(counter, 0, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE); |
| 596 | memset(salt, 0, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
| 597 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 598 | len = BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 599 | |
| 600 | if (sz > EXPECTED_ENC_LEN) { |
| 601 | /* Use new enhanced encryption scheme with randomly generated salt and AES IVs */ |
| 602 | len += BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE * 2; |
| 603 | |
| 604 | my_salt = &buf[EXPECTED_ENC_LEN]; |
| 605 | |
| 606 | my_key_iv = &derived_key[BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + |
| 607 | BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
| 608 | |
| 609 | my_counter = &derived_key[BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + |
| 610 | BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE + |
| 611 | BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE]; |
| 612 | } |
| 613 | |
| 614 | out_len = len; |
| 615 | rc = hkdf(shared, SHARED_KEY_LEN, (uint8_t *)"MCUBoot_ECIES_v1", BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE, |
| 616 | my_salt, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE, derived_key, &out_len); |
| 617 | |
| 618 | if (rc != 0 || len != out_len) { |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * HMAC the key and check that our received MAC matches the generated tag |
| 624 | */ |
| 625 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 626 | bootutil_hmac_sha256_init(&hmac); |
| 627 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 628 | rc = bootutil_hmac_sha256_set_key(&hmac, &derived_key[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE], BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 629 | if (rc != 0) { |
| 630 | (void)bootutil_hmac_sha256_drop(&hmac); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 631 | return -1; |
| 632 | } |
| 633 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 634 | rc = bootutil_hmac_sha256_update(&hmac, &buf[EC_CIPHERKEY_INDEX], BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 635 | if (rc != 0) { |
| 636 | (void)bootutil_hmac_sha256_drop(&hmac); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 637 | return -1; |
| 638 | } |
| 639 | |
Bohdan Kovalchuk | 25c7a0f | 2020-11-30 21:32:14 +0200 | [diff] [blame] | 640 | /* Assumes the tag buffer is at least sizeof(hmac_tag_size(state)) bytes */ |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 641 | rc = bootutil_hmac_sha256_finish(&hmac, tag, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
| 642 | if (rc != 0) { |
| 643 | (void)bootutil_hmac_sha256_drop(&hmac); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 644 | return -1; |
| 645 | } |
| 646 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 647 | if (bootutil_constant_time_compare(tag, &buf[EC_TAG_INDEX], BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) != 0) { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 648 | (void)bootutil_hmac_sha256_drop(&hmac); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 649 | return -1; |
| 650 | } |
| 651 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 652 | bootutil_hmac_sha256_drop(&hmac); |
| 653 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 654 | (void)memcpy(enciv, my_counter, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE); |
| 655 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 656 | /* |
| 657 | * Finally decrypt the received ciphered key |
| 658 | */ |
| 659 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 660 | bootutil_aes_ctr_init(&aes_ctr); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 661 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 662 | rc = bootutil_aes_ctr_set_key(&aes_ctr, derived_key); |
| 663 | if (rc != 0) { |
| 664 | bootutil_aes_ctr_drop(&aes_ctr); |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 665 | return -1; |
| 666 | } |
| 667 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 668 | rc = bootutil_aes_ctr_decrypt(&aes_ctr, my_key_iv, &buf[EC_CIPHERKEY_INDEX], BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE, 0, enckey); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 669 | if (rc != 0) { |
| 670 | bootutil_aes_ctr_drop(&aes_ctr); |
| 671 | return -1; |
| 672 | } |
| 673 | |
| 674 | bootutil_aes_ctr_drop(&aes_ctr); |
| 675 | |
Fabio Utzig | 5fde832 | 2019-10-23 12:23:08 -0300 | [diff] [blame] | 676 | rc = 0; |
| 677 | |
Fabio Utzig | 6aec6ae | 2020-04-02 11:06:07 -0300 | [diff] [blame] | 678 | #endif /* defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) */ |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 679 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 680 | return rc; |
| 681 | } |
| 682 | |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 683 | /* |
| 684 | * Load encryption key. |
| 685 | */ |
| 686 | int |
| 687 | boot_enc_load(struct enc_key_data *enc_state, int image_index, |
| 688 | const struct image_header *hdr, const struct flash_area *fap, |
| 689 | struct boot_status *bs) |
| 690 | { |
| 691 | uint32_t off; |
| 692 | uint16_t len; |
| 693 | struct image_tlv_iter it; |
| 694 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 695 | uint8_t *buf; |
| 696 | #else |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 697 | uint8_t buf[EXPECTED_ENC_EXT_LEN]; |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 698 | #endif |
| 699 | uint8_t slot; |
| 700 | int rc; |
| 701 | |
| 702 | rc = flash_area_id_to_multi_image_slot(image_index, fap->fa_id); |
| 703 | if (rc < 0) { |
| 704 | return rc; |
| 705 | } |
| 706 | slot = rc; |
| 707 | |
| 708 | /* Already loaded... */ |
| 709 | if (enc_state[slot].valid) { |
| 710 | return 1; |
| 711 | } |
| 712 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 713 | /* Initialize the AES context */ |
| 714 | boot_enc_init(enc_state, slot); |
| 715 | |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 716 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, EXPECTED_ENC_TLV, false); |
| 717 | if (rc) { |
| 718 | return -1; |
| 719 | } |
| 720 | |
| 721 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 722 | if (rc != 0) { |
| 723 | return rc; |
| 724 | } |
| 725 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 726 | if ((len < EXPECTED_ENC_LEN) || (len > EXPECTED_ENC_EXT_LEN)) { |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 727 | return -1; |
| 728 | } |
| 729 | |
| 730 | #if MCUBOOT_SWAP_SAVE_ENCTLV |
| 731 | buf = bs->enctlv[slot]; |
| 732 | memset(buf, 0xff, BOOT_ENC_TLV_ALIGN_SIZE); |
| 733 | #endif |
| 734 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 735 | rc = flash_area_read(fap, off, buf, len); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 736 | if (rc) { |
| 737 | return -1; |
| 738 | } |
| 739 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 740 | return (boot_enc_decrypt(buf, bs->enckey[slot], len, enc_state[slot].aes_iv)); |
Fabio Utzig | 4741c45 | 2019-12-19 15:32:41 -0300 | [diff] [blame] | 741 | } |
| 742 | |
Andrzej Puzdrowski | cf97dd0 | 2019-03-14 15:10:10 +0100 | [diff] [blame] | 743 | bool |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 744 | boot_enc_valid(struct enc_key_data *enc_state, int image_index, |
| 745 | const struct flash_area *fap) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 746 | { |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 747 | int rc; |
| 748 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 749 | rc = flash_area_id_to_multi_image_slot(image_index, fap->fa_id); |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 750 | if (rc < 0) { |
| 751 | /* can't get proper slot number - skip encryption, */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 752 | /* postpone the error for a upper layer */ |
Andrzej Puzdrowski | cf97dd0 | 2019-03-14 15:10:10 +0100 | [diff] [blame] | 753 | return false; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | return enc_state[rc].valid; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 760 | boot_encrypt(struct enc_key_data *enc_state, int image_index, |
| 761 | const struct flash_area *fap, uint32_t off, uint32_t sz, |
| 762 | uint32_t blk_off, uint8_t *buf) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 763 | { |
| 764 | struct enc_key_data *enc; |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 765 | uint8_t *nonce; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 766 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 767 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 768 | /* boot_copy_region will call boot_encrypt with sz = 0 when skipping over |
| 769 | the TLVs. */ |
| 770 | if (sz == 0) { |
| 771 | return; |
| 772 | } |
| 773 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 774 | rc = flash_area_id_to_multi_image_slot(image_index, fap->fa_id); |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 775 | if (rc < 0) { |
| 776 | assert(0); |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | enc = &enc_state[rc]; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 781 | assert(enc->valid == 1); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 782 | |
| 783 | nonce = enc->aes_iv; |
| 784 | |
| 785 | off >>= 4; |
| 786 | nonce[12] = (uint8_t)(off >> 24); |
| 787 | nonce[13] = (uint8_t)(off >> 16); |
| 788 | nonce[14] = (uint8_t)(off >> 8); |
| 789 | nonce[15] = (uint8_t)off; |
| 790 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 791 | bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 792 | } |
| 793 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 794 | /** |
| 795 | * Clears encrypted state after use. |
| 796 | */ |
Fabio Utzig | b63d995 | 2019-08-19 14:20:52 -0300 | [diff] [blame] | 797 | void |
| 798 | boot_enc_zeroize(struct enc_key_data *enc_state) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 799 | { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 800 | uint8_t slot; |
| 801 | for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { |
| 802 | (void)boot_enc_drop(enc_state, slot); |
| 803 | } |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 804 | memset(enc_state, 0, sizeof(struct enc_key_data) * BOOT_NUM_SLOTS); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | #endif /* MCUBOOT_ENC_IMAGES */ |