Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #include "mcuboot_config/mcuboot_config.h" |
| 21 | |
| 22 | #if defined(MCUBOOT_ENC_IMAGES) |
| 23 | #include <assert.h> |
| 24 | #include <stddef.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <string.h> |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 27 | #include <stdio.h> |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 28 | |
| 29 | #include "hal/hal_flash.h" |
| 30 | |
| 31 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 32 | #include "mbedtls/rsa.h" |
| 33 | #include "mbedtls/asn1.h" |
| 34 | #endif |
| 35 | |
| 36 | #if defined(MCUBOOT_ENCRYPT_KW) |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 37 | # if defined(MCUBOOT_USE_MBED_TLS) |
| 38 | # include "mbedtls/nist_kw.h" |
| 39 | # include "mbedtls/aes.h" |
| 40 | # else |
| 41 | # include "tinycrypt/aes.h" |
| 42 | # endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 43 | #endif |
| 44 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 45 | #include "bootutil/image.h" |
| 46 | #include "bootutil/enc_key.h" |
| 47 | #include "bootutil/sign_key.h" |
| 48 | |
| 49 | #include "bootutil_priv.h" |
| 50 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 51 | #define TLV_ENC_RSA_SZ 256 |
| 52 | #define TLV_ENC_KW_SZ 24 |
| 53 | |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 54 | #if defined(MCUBOOT_ENCRYPT_KW) |
| 55 | #if defined(MCUBOOT_USE_MBED_TLS) |
| 56 | int |
| 57 | key_unwrap(uint8_t *wrapped, uint8_t *enckey) |
| 58 | { |
| 59 | mbedtls_nist_kw_context kw; |
| 60 | int rc; |
| 61 | size_t olen; |
| 62 | |
| 63 | mbedtls_nist_kw_init(&kw); |
| 64 | |
| 65 | rc = mbedtls_nist_kw_setkey(&kw, MBEDTLS_CIPHER_ID_AES, |
| 66 | bootutil_enc_key.key, *bootutil_enc_key.len * 8, 0); |
| 67 | if (rc) { |
| 68 | goto done; |
| 69 | } |
| 70 | |
| 71 | rc = mbedtls_nist_kw_unwrap(&kw, MBEDTLS_KW_MODE_KW, wrapped, TLV_ENC_KW_SZ, |
| 72 | enckey, &olen, BOOT_ENC_KEY_SIZE); |
| 73 | |
| 74 | done: |
| 75 | mbedtls_nist_kw_free(&kw); |
| 76 | return rc; |
| 77 | } |
| 78 | #else /* !MCUBOOT_USE_MBED_TLS */ |
| 79 | /* |
| 80 | * Implements AES key unwrapping following RFC-3394 section 2.2.2, using |
| 81 | * tinycrypt for AES-128 decryption. |
| 82 | */ |
| 83 | int |
| 84 | key_unwrap(uint8_t *wrapped, uint8_t *enckey) |
| 85 | { |
| 86 | struct tc_aes_key_sched_struct aes; |
| 87 | uint8_t A[8]; |
| 88 | uint8_t B[16]; |
| 89 | int8_t i, j, k; |
| 90 | |
| 91 | if (tc_aes128_set_decrypt_key(&aes, bootutil_enc_key.key) == 0) { |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | for (k = 0; k < 8; k++) { |
| 96 | A[k] = wrapped[k]; |
| 97 | enckey[k] = wrapped[8 + k]; |
| 98 | enckey[8 + k] = wrapped[16 + k]; |
| 99 | } |
| 100 | |
| 101 | for (j = 5; j >= 0; j--) { |
| 102 | for (i = 2; i > 0; i--) { |
| 103 | for (k = 0; k < 8; k++) { |
| 104 | B[k] = A[k]; |
| 105 | B[8 + k] = enckey[((i-1) * 8) + k]; |
| 106 | } |
| 107 | B[7] ^= 2 * j + i; |
| 108 | if (tc_aes_decrypt((uint8_t *)&B, (uint8_t *)&B, &aes) == 0) { |
| 109 | return -1; |
| 110 | } |
| 111 | for (k = 0; k < 8; k++) { |
| 112 | A[k] = B[k]; |
| 113 | enckey[((i-1) * 8) + k] = B[8 + k]; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | for (i = 0, k = 0; i < 8; i++) { |
| 119 | k |= A[i] ^ 0xa6; |
| 120 | } |
| 121 | if (k) { |
| 122 | return -1; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | #endif /* MCUBOOT_USE_MBED_TLS */ |
| 127 | #endif /* MCUBOOT_ENCRYPT_KW */ |
| 128 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 129 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 130 | static int |
| 131 | parse_enckey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end) |
| 132 | { |
| 133 | int rc; |
| 134 | size_t len; |
| 135 | |
| 136 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 137 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | if (*p + len != end) { |
| 142 | return -2; |
| 143 | } |
| 144 | |
| 145 | if ( /* version */ |
| 146 | mbedtls_asn1_get_int(p, end, &ctx->ver) != 0 || |
| 147 | /* public modulus */ |
| 148 | mbedtls_asn1_get_mpi(p, end, &ctx->N) != 0 || |
| 149 | /* public exponent */ |
| 150 | mbedtls_asn1_get_mpi(p, end, &ctx->E) != 0 || |
| 151 | /* private exponent */ |
| 152 | mbedtls_asn1_get_mpi(p, end, &ctx->D) != 0 || |
| 153 | /* primes */ |
| 154 | mbedtls_asn1_get_mpi(p, end, &ctx->P) != 0 || |
| 155 | mbedtls_asn1_get_mpi(p, end, &ctx->Q) != 0 || |
| 156 | /* d mod (p-1) and d mod (q-1) */ |
| 157 | mbedtls_asn1_get_mpi(p, end, &ctx->DP) != 0 || |
| 158 | mbedtls_asn1_get_mpi(p, end, &ctx->DQ) != 0 || |
| 159 | /* q ^ (-1) mod p */ |
| 160 | mbedtls_asn1_get_mpi(p, end, &ctx->QP) != 0) { |
| 161 | return -3; |
| 162 | } |
| 163 | |
| 164 | ctx->len = mbedtls_mpi_size(&ctx->N); |
| 165 | |
| 166 | if (*p != end) { |
| 167 | return -4; |
| 168 | } |
| 169 | |
| 170 | if (mbedtls_rsa_check_pubkey(ctx) != 0 || |
| 171 | mbedtls_rsa_check_privkey(ctx) != 0) { |
| 172 | return -5; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame^] | 180 | boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot, uint8_t *enckey) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 181 | { |
| 182 | int rc; |
| 183 | |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 184 | #if defined(MCUBOOT_USE_MBED_TLS) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 185 | mbedtls_aes_init(&enc_state[slot].aes); |
| 186 | rc = mbedtls_aes_setkey_enc(&enc_state[slot].aes, enckey, BOOT_ENC_KEY_SIZE_BITS); |
| 187 | if (rc) { |
| 188 | mbedtls_aes_free(&enc_state[slot].aes); |
| 189 | return -1; |
| 190 | } |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 191 | #else |
| 192 | (void)rc; |
| 193 | |
| 194 | /* set_encrypt and set_decrypt do the same thing in tinycrypt */ |
| 195 | tc_aes128_set_encrypt_key(&enc_state[slot].aes, enckey); |
| 196 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 197 | |
| 198 | enc_state[slot].valid = 1; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 204 | # define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048 |
| 205 | # define EXPECTED_ENC_LEN TLV_ENC_RSA_SZ |
| 206 | #elif defined(MCUBOOT_ENCRYPT_KW) |
| 207 | # define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW128 |
| 208 | # define EXPECTED_ENC_LEN TLV_ENC_KW_SZ |
| 209 | #endif |
| 210 | |
| 211 | /* |
| 212 | * Load encryption key. |
| 213 | */ |
| 214 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame^] | 215 | boot_enc_load(struct enc_key_data *enc_state, int image_index, |
| 216 | const struct image_header *hdr, const struct flash_area *fap, |
| 217 | uint8_t *enckey) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 218 | { |
| 219 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 220 | mbedtls_rsa_context rsa; |
| 221 | uint8_t *cp; |
| 222 | uint8_t *cpend; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 223 | size_t olen; |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 224 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 225 | uint32_t off; |
| 226 | uint32_t end; |
| 227 | struct image_tlv_info info; |
| 228 | struct image_tlv tlv; |
| 229 | uint8_t buf[TLV_ENC_RSA_SZ]; |
| 230 | uint8_t slot; |
| 231 | uint8_t enckey_type; |
| 232 | int rc; |
| 233 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 234 | 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] | 235 | if (rc < 0) { |
| 236 | return rc; |
| 237 | } |
| 238 | slot = rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 239 | |
| 240 | /* Already loaded... */ |
| 241 | if (enc_state[slot].valid) { |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | off = hdr->ih_img_size + hdr->ih_hdr_size; |
| 246 | |
| 247 | rc = flash_area_read(fap, off, &info, sizeof(info)); |
| 248 | if (rc) { |
| 249 | return rc; |
| 250 | } |
| 251 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 252 | return -1; |
| 253 | } |
| 254 | end = off + info.it_tlv_tot; |
| 255 | off += sizeof(info); |
| 256 | |
| 257 | enckey_type = 0; |
| 258 | for (; off < end; off += sizeof(tlv) + tlv.it_len) { |
| 259 | rc = flash_area_read(fap, off, &tlv, sizeof tlv); |
| 260 | if (rc) { |
| 261 | return rc; |
| 262 | } |
| 263 | |
| 264 | if (tlv.it_type == EXPECTED_ENC_TLV) { |
| 265 | if (tlv.it_len != EXPECTED_ENC_LEN) { |
| 266 | return -1; |
| 267 | } |
| 268 | rc = flash_area_read(fap, off + sizeof(tlv), buf, EXPECTED_ENC_LEN); |
| 269 | if (rc) { |
| 270 | return rc; |
| 271 | } |
| 272 | enckey_type = EXPECTED_ENC_TLV; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (enckey_type == 0) { |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | if (enckey_type == EXPECTED_ENC_TLV) { |
| 282 | #if defined(MCUBOOT_ENCRYPT_RSA) |
| 283 | mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256); |
| 284 | |
| 285 | cp = (uint8_t *)bootutil_enc_key.key; |
| 286 | cpend = cp + *bootutil_enc_key.len; |
| 287 | |
| 288 | rc = parse_enckey(&rsa, &cp, cpend); |
| 289 | if (rc) { |
| 290 | mbedtls_rsa_free(&rsa); |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 291 | return rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | rc = mbedtls_rsa_rsaes_oaep_decrypt(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, |
| 295 | NULL, 0, &olen, buf, enckey, BOOT_ENC_KEY_SIZE); |
| 296 | mbedtls_rsa_free(&rsa); |
| 297 | |
| 298 | #elif defined(MCUBOOT_ENCRYPT_KW) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 299 | assert(*bootutil_enc_key.len == 16); |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 300 | rc = key_unwrap(buf, enckey); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 301 | #endif |
| 302 | } |
| 303 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 304 | return rc; |
| 305 | } |
| 306 | |
Andrzej Puzdrowski | cf97dd0 | 2019-03-14 15:10:10 +0100 | [diff] [blame] | 307 | bool |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame^] | 308 | boot_enc_valid(struct enc_key_data *enc_state, int image_index, |
| 309 | const struct flash_area *fap) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 310 | { |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 311 | int rc; |
| 312 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 313 | 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] | 314 | if (rc < 0) { |
| 315 | /* can't get proper slot number - skip encryption, */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 316 | /* postpone the error for a upper layer */ |
Andrzej Puzdrowski | cf97dd0 | 2019-03-14 15:10:10 +0100 | [diff] [blame] | 317 | return false; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return enc_state[rc].valid; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame^] | 324 | boot_enc_mark_keys_invalid(struct enc_key_data *enc_state) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 325 | { |
| 326 | size_t slot; |
| 327 | |
| 328 | for(slot = 0; slot < BOOT_NUM_SLOTS; ++slot) { |
| 329 | enc_state[slot].valid = 0; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame^] | 334 | boot_encrypt(struct enc_key_data *enc_state, int image_index, |
| 335 | const struct flash_area *fap, uint32_t off, uint32_t sz, |
| 336 | uint32_t blk_off, uint8_t *buf) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 337 | { |
| 338 | struct enc_key_data *enc; |
| 339 | uint32_t i, j; |
| 340 | uint8_t u8; |
| 341 | uint8_t nonce[16]; |
| 342 | uint8_t blk[16]; |
Andrzej Puzdrowski | e575fe9 | 2019-03-14 12:20:19 +0100 | [diff] [blame] | 343 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 344 | |
| 345 | memset(nonce, 0, 12); |
| 346 | off >>= 4; |
| 347 | nonce[12] = (uint8_t)(off >> 24); |
| 348 | nonce[13] = (uint8_t)(off >> 16); |
| 349 | nonce[14] = (uint8_t)(off >> 8); |
| 350 | nonce[15] = (uint8_t)off; |
| 351 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 352 | 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] | 353 | if (rc < 0) { |
| 354 | assert(0); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | enc = &enc_state[rc]; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 359 | assert(enc->valid == 1); |
| 360 | for (i = 0; i < sz; i++) { |
| 361 | if (i == 0 || blk_off == 0) { |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 362 | #if defined(MCUBOOT_USE_MBED_TLS) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 363 | mbedtls_aes_crypt_ecb(&enc->aes, MBEDTLS_AES_ENCRYPT, nonce, blk); |
Fabio Utzig | 38f5ffe | 2018-12-27 16:12:58 -0200 | [diff] [blame] | 364 | #else |
| 365 | tc_aes_encrypt(blk, nonce, &enc->aes); |
| 366 | #endif |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 367 | |
| 368 | for (j = 16; j > 0; --j) { |
| 369 | if (++nonce[j - 1] != 0) { |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | u8 = *buf; |
| 376 | *buf++ = u8 ^ blk[blk_off]; |
| 377 | blk_off = (blk_off + 1) & 0x0f; |
| 378 | } |
| 379 | } |
| 380 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame^] | 381 | /** |
| 382 | * Clears encrypted state after use. |
| 383 | */ |
| 384 | void boot_enc_zeroize(struct enc_key_data *enc_state) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 385 | { |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame^] | 386 | memset(enc_state, 0, sizeof(struct enc_key_data) * BOOT_NUM_SLOTS); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | #endif /* MCUBOOT_ENC_IMAGES */ |