Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2017-2018 Linaro LTD |
| 5 | * Copyright (c) 2017-2019 JUUL Labs |
| 6 | * |
| 7 | * Original license: |
| 8 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 9 | * Licensed to the Apache Software Foundation (ASF) under one |
| 10 | * or more contributor license agreements. See the NOTICE file |
| 11 | * distributed with this work for additional information |
| 12 | * regarding copyright ownership. The ASF licenses this file |
| 13 | * to you under the Apache License, Version 2.0 (the |
| 14 | * "License"); you may not use this file except in compliance |
| 15 | * with the License. You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, |
| 20 | * software distributed under the License is distributed on an |
| 21 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 22 | * KIND, either express or implied. See the License for the |
| 23 | * specific language governing permissions and limitations |
| 24 | * under the License. |
| 25 | */ |
| 26 | |
Ricardo Salveti | a2d5b1a | 2017-01-18 11:41:39 -0200 | [diff] [blame] | 27 | #include <string.h> |
| 28 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 29 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 30 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 31 | #ifdef MCUBOOT_SIGN_RSA |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | #include "bootutil/sign_key.h" |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 33 | #include "bootutil/sha256.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 34 | |
| 35 | #include "mbedtls/rsa.h" |
| 36 | #include "mbedtls/asn1.h" |
Yiping Peng | 3393992 | 2018-09-30 15:06:53 +0800 | [diff] [blame] | 37 | #include "mbedtls/version.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 38 | |
| 39 | #include "bootutil_priv.h" |
| 40 | |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 41 | /* |
| 42 | * Constants for this particular constrained implementation of |
| 43 | * RSA-PSS. In particular, we support RSA 2048, with a SHA256 hash, |
| 44 | * and a 32-byte salt. A signature with different parameters will be |
| 45 | * rejected as invalid. |
| 46 | */ |
| 47 | |
| 48 | /* The size, in octets, of the message. */ |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 49 | #define PSS_EMLEN (MCUBOOT_SIGN_RSA_LEN / 8) |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 50 | |
| 51 | /* The size of the hash function. For SHA256, this is 32 bytes. */ |
| 52 | #define PSS_HLEN 32 |
| 53 | |
| 54 | /* Size of the salt, should be fixed. */ |
| 55 | #define PSS_SLEN 32 |
| 56 | |
| 57 | /* The length of the mask: emLen - hLen - 1. */ |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 58 | #define PSS_MASK_LEN (PSS_EMLEN - PSS_HLEN - 1) |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 59 | |
| 60 | #define PSS_HASH_OFFSET PSS_MASK_LEN |
| 61 | |
| 62 | /* For the mask itself, how many bytes should be all zeros. */ |
| 63 | #define PSS_MASK_ZERO_COUNT (PSS_MASK_LEN - PSS_SLEN - 1) |
| 64 | #define PSS_MASK_ONE_POS PSS_MASK_ZERO_COUNT |
| 65 | |
| 66 | /* Where the salt starts. */ |
| 67 | #define PSS_MASK_SALT_POS (PSS_MASK_ONE_POS + 1) |
| 68 | |
| 69 | static const uint8_t pss_zeros[8] = {0}; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Parse the public key used for signing. Simple RSA format. |
| 73 | */ |
| 74 | static int |
| 75 | bootutil_parse_rsakey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end) |
| 76 | { |
| 77 | int rc; |
| 78 | size_t len; |
| 79 | |
| 80 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 81 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | if (*p + len != end) { |
| 86 | return -2; |
| 87 | } |
| 88 | |
| 89 | if ((rc = mbedtls_asn1_get_mpi(p, end, &ctx->N)) != 0 || |
| 90 | (rc = mbedtls_asn1_get_mpi(p, end, &ctx->E)) != 0) { |
| 91 | return -3; |
| 92 | } |
| 93 | |
David Brown | 785dc4c | 2018-02-13 14:31:24 -0700 | [diff] [blame] | 94 | ctx->len = mbedtls_mpi_size(&ctx->N); |
| 95 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 96 | if (*p != end) { |
| 97 | return -4; |
| 98 | } |
| 99 | |
Yiping Peng | 3393992 | 2018-09-30 15:06:53 +0800 | [diff] [blame] | 100 | /* The mbedtls version is more than 2.6.1 */ |
| 101 | #if MBEDTLS_VERSION_NUMBER > 0x02060100 |
| 102 | rc = mbedtls_rsa_import(ctx, &ctx->N, NULL, NULL, NULL, &ctx->E); |
| 103 | if (rc != 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 104 | return -5; |
| 105 | } |
Yiping Peng | 3393992 | 2018-09-30 15:06:53 +0800 | [diff] [blame] | 106 | #endif |
| 107 | |
| 108 | rc = mbedtls_rsa_check_pubkey(ctx); |
| 109 | if (rc != 0) { |
| 110 | return -6; |
| 111 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 112 | |
| 113 | ctx->len = mbedtls_mpi_size(&ctx->N); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 118 | /* |
| 119 | * Compute the RSA-PSS mask-generation function, MGF1. Assumptions |
| 120 | * are that the mask length will be less than 256 * PSS_HLEN, and |
| 121 | * therefore we never need to increment anything other than the low |
| 122 | * byte of the counter. |
| 123 | * |
| 124 | * This is described in PKCS#1, B.2.1. |
| 125 | */ |
| 126 | static void |
| 127 | pss_mgf1(uint8_t *mask, const uint8_t *hash) |
| 128 | { |
| 129 | bootutil_sha256_context ctx; |
| 130 | uint8_t counter[4] = { 0, 0, 0, 0 }; |
| 131 | uint8_t htmp[PSS_HLEN]; |
| 132 | int count = PSS_MASK_LEN; |
| 133 | int bytes; |
| 134 | |
| 135 | while (count > 0) { |
| 136 | bootutil_sha256_init(&ctx); |
| 137 | bootutil_sha256_update(&ctx, hash, PSS_HLEN); |
| 138 | bootutil_sha256_update(&ctx, counter, 4); |
| 139 | bootutil_sha256_finish(&ctx, htmp); |
| 140 | |
| 141 | counter[3]++; |
| 142 | |
| 143 | bytes = PSS_HLEN; |
| 144 | if (bytes > count) |
| 145 | bytes = count; |
| 146 | |
| 147 | memcpy(mask, htmp, bytes); |
| 148 | mask += bytes; |
| 149 | count -= bytes; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Validate an RSA signature, using RSA-PSS, as described in PKCS #1 |
| 155 | * v2.2, section 9.1.2, with many parameters required to have fixed |
| 156 | * values. |
| 157 | */ |
| 158 | static int |
| 159 | bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen, |
| 160 | uint8_t *sig) |
| 161 | { |
| 162 | bootutil_sha256_context shactx; |
| 163 | uint8_t em[MBEDTLS_MPI_MAX_SIZE]; |
| 164 | uint8_t db_mask[PSS_MASK_LEN]; |
| 165 | uint8_t h2[PSS_HLEN]; |
| 166 | int i; |
| 167 | |
David Brown | cdb968f | 2017-06-05 12:57:26 -0600 | [diff] [blame] | 168 | if (ctx->len != PSS_EMLEN || PSS_EMLEN > MBEDTLS_MPI_MAX_SIZE) { |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | if (hlen != PSS_HLEN) { |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | if (mbedtls_rsa_public(ctx, sig, em)) { |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * PKCS #1 v2.2, 9.1.2 EMSA-PSS-Verify |
| 182 | * |
| 183 | * emBits is 2048 |
| 184 | * emLen = ceil(emBits/8) = 256 |
| 185 | * |
| 186 | * The salt length is not known at the beginning. |
| 187 | */ |
| 188 | |
| 189 | /* Step 1. The message is constrained by the address space of a |
| 190 | * 32-bit processor, which is far less than the 2^61-1 limit of |
| 191 | * SHA-256. |
| 192 | */ |
| 193 | |
| 194 | /* Step 2. mHash is passed in as 'hash', with hLen the hlen |
| 195 | * argument. */ |
| 196 | |
| 197 | /* Step 3. if emLen < hLen + sLen + 2, inconsistent and stop. |
| 198 | * The salt length is not known at this point. |
| 199 | */ |
| 200 | |
Sam Bristow | d0ca0ff | 2019-10-30 20:51:35 +1300 | [diff] [blame] | 201 | /* Step 4. If the rightmost octet of EM does have the value |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 202 | * 0xbc, output inconsistent and stop. |
| 203 | */ |
| 204 | if (em[PSS_EMLEN - 1] != 0xbc) { |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | /* Step 5. Let maskedDB be the leftmost emLen - hLen - 1 octets |
| 209 | * of EM, and H be the next hLen octets. |
| 210 | * |
| 211 | * maskedDB is then the first 256 - 32 - 1 = 0-222 |
| 212 | * H is 32 bytes 223-254 |
| 213 | */ |
| 214 | |
| 215 | /* Step 6. If the leftmost 8emLen - emBits bits of the leftmost |
| 216 | * octet in maskedDB are not all equal to zero, output |
| 217 | * inconsistent and stop. |
| 218 | * |
| 219 | * 8emLen - emBits is zero, so there is nothing to test here. |
| 220 | */ |
| 221 | |
| 222 | /* Step 7. let dbMask = MGF(H, emLen - hLen - 1). */ |
| 223 | pss_mgf1(db_mask, &em[PSS_HASH_OFFSET]); |
| 224 | |
| 225 | /* Step 8. let DB = maskedDB xor dbMask. |
| 226 | * To avoid needing an additional buffer, store the 'db' in the |
| 227 | * same buffer as db_mask. From now, to the end of this function, |
| 228 | * db_mask refers to the unmasked 'db'. */ |
| 229 | for (i = 0; i < PSS_MASK_LEN; i++) { |
| 230 | db_mask[i] ^= em[i]; |
| 231 | } |
| 232 | |
| 233 | /* Step 9. Set the leftmost 8emLen - emBits bits of the leftmost |
| 234 | * octet in DB to zero. |
| 235 | * pycrypto seems to always make the emBits 2047, so we need to |
| 236 | * clear the top bit. */ |
| 237 | db_mask[0] &= 0x7F; |
| 238 | |
| 239 | /* Step 10. If the emLen - hLen - sLen - 2 leftmost octets of DB |
| 240 | * are not zero or if the octet at position emLen - hLen - sLen - |
| 241 | * 1 (the leftmost position is "position 1") does not have |
| 242 | * hexadecimal value 0x01, output "inconsistent" and stop. */ |
| 243 | for (i = 0; i < PSS_MASK_ZERO_COUNT; i++) { |
| 244 | if (db_mask[i] != 0) { |
| 245 | return -1; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (db_mask[PSS_MASK_ONE_POS] != 1) { |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | /* Step 11. Let salt be the last sLen octets of DB */ |
| 254 | |
| 255 | /* Step 12. Let M' = 0x00 00 00 00 00 00 00 00 || mHash || salt; */ |
| 256 | |
| 257 | /* Step 13. Let H' = Hash(M') */ |
| 258 | bootutil_sha256_init(&shactx); |
| 259 | bootutil_sha256_update(&shactx, pss_zeros, 8); |
| 260 | bootutil_sha256_update(&shactx, hash, PSS_HLEN); |
| 261 | bootutil_sha256_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN); |
| 262 | bootutil_sha256_finish(&shactx, h2); |
| 263 | |
| 264 | /* Step 14. If H = H', output "consistent". Otherwise, output |
| 265 | * "inconsistent". */ |
| 266 | if (memcmp(h2, &em[PSS_HASH_OFFSET], PSS_HLEN) != 0) { |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 272 | |
| 273 | int |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 274 | bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 275 | uint8_t key_id) |
| 276 | { |
| 277 | mbedtls_rsa_context ctx; |
| 278 | int rc; |
| 279 | uint8_t *cp; |
| 280 | uint8_t *end; |
| 281 | |
| 282 | mbedtls_rsa_init(&ctx, 0, 0); |
| 283 | |
| 284 | cp = (uint8_t *)bootutil_keys[key_id].key; |
| 285 | end = cp + *bootutil_keys[key_id].len; |
| 286 | |
| 287 | rc = bootutil_parse_rsakey(&ctx, &cp, end); |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 288 | if (rc || slen != ctx.len) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 289 | mbedtls_rsa_free(&ctx); |
| 290 | return rc; |
| 291 | } |
| 292 | rc = bootutil_cmp_rsasig(&ctx, hash, hlen, sig); |
| 293 | mbedtls_rsa_free(&ctx); |
| 294 | |
| 295 | return rc; |
| 296 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 297 | #endif /* MCUBOOT_SIGN_RSA */ |