Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [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 | |
Ricardo Salveti | a2d5b1a | 2017-01-18 11:41:39 -0200 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 22 | #ifdef APP_mynewt |
| 23 | #include "mynewt/config.h" |
| 24 | #endif |
| 25 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 26 | #ifdef MCUBOOT_SIGN_RSA |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 27 | #include "bootutil/sign_key.h" |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 28 | #include "bootutil/sha256.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 29 | |
| 30 | #include "mbedtls/rsa.h" |
| 31 | #include "mbedtls/asn1.h" |
| 32 | |
| 33 | #include "bootutil_priv.h" |
| 34 | |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 35 | #if MCUBOOT_RSA_PKCS1_15 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 36 | static const uint8_t sha256_oid[] = { |
| 37 | 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, |
| 38 | 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, |
| 39 | 0x00, 0x04, 0x20 |
| 40 | }; |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #ifndef MCUBOOT_RSA_PKCS1_15 |
| 44 | /* |
| 45 | * Constants for this particular constrained implementation of |
| 46 | * RSA-PSS. In particular, we support RSA 2048, with a SHA256 hash, |
| 47 | * and a 32-byte salt. A signature with different parameters will be |
| 48 | * rejected as invalid. |
| 49 | */ |
| 50 | |
| 51 | /* The size, in octets, of the message. */ |
| 52 | #define PSS_EMLEN 256 |
| 53 | |
| 54 | /* The size of the hash function. For SHA256, this is 32 bytes. */ |
| 55 | #define PSS_HLEN 32 |
| 56 | |
| 57 | /* Size of the salt, should be fixed. */ |
| 58 | #define PSS_SLEN 32 |
| 59 | |
| 60 | /* The length of the mask: emLen - hLen - 1. */ |
| 61 | #define PSS_MASK_LEN (256 - PSS_HLEN - 1) |
| 62 | |
| 63 | #define PSS_HASH_OFFSET PSS_MASK_LEN |
| 64 | |
| 65 | /* For the mask itself, how many bytes should be all zeros. */ |
| 66 | #define PSS_MASK_ZERO_COUNT (PSS_MASK_LEN - PSS_SLEN - 1) |
| 67 | #define PSS_MASK_ONE_POS PSS_MASK_ZERO_COUNT |
| 68 | |
| 69 | /* Where the salt starts. */ |
| 70 | #define PSS_MASK_SALT_POS (PSS_MASK_ONE_POS + 1) |
| 71 | |
| 72 | static const uint8_t pss_zeros[8] = {0}; |
| 73 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Parse the public key used for signing. Simple RSA format. |
| 77 | */ |
| 78 | static int |
| 79 | bootutil_parse_rsakey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end) |
| 80 | { |
| 81 | int rc; |
| 82 | size_t len; |
| 83 | |
| 84 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 85 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | if (*p + len != end) { |
| 90 | return -2; |
| 91 | } |
| 92 | |
| 93 | if ((rc = mbedtls_asn1_get_mpi(p, end, &ctx->N)) != 0 || |
| 94 | (rc = mbedtls_asn1_get_mpi(p, end, &ctx->E)) != 0) { |
| 95 | return -3; |
| 96 | } |
| 97 | |
| 98 | if (*p != end) { |
| 99 | return -4; |
| 100 | } |
| 101 | |
| 102 | if ((rc = mbedtls_rsa_check_pubkey(ctx)) != 0) { |
| 103 | return -5; |
| 104 | } |
| 105 | |
| 106 | ctx->len = mbedtls_mpi_size(&ctx->N); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 111 | #ifndef MCUBOOT_RSA_PKCS1_15 |
| 112 | /* |
| 113 | * Compute the RSA-PSS mask-generation function, MGF1. Assumptions |
| 114 | * are that the mask length will be less than 256 * PSS_HLEN, and |
| 115 | * therefore we never need to increment anything other than the low |
| 116 | * byte of the counter. |
| 117 | * |
| 118 | * This is described in PKCS#1, B.2.1. |
| 119 | */ |
| 120 | static void |
| 121 | pss_mgf1(uint8_t *mask, const uint8_t *hash) |
| 122 | { |
| 123 | bootutil_sha256_context ctx; |
| 124 | uint8_t counter[4] = { 0, 0, 0, 0 }; |
| 125 | uint8_t htmp[PSS_HLEN]; |
| 126 | int count = PSS_MASK_LEN; |
| 127 | int bytes; |
| 128 | |
| 129 | while (count > 0) { |
| 130 | bootutil_sha256_init(&ctx); |
| 131 | bootutil_sha256_update(&ctx, hash, PSS_HLEN); |
| 132 | bootutil_sha256_update(&ctx, counter, 4); |
| 133 | bootutil_sha256_finish(&ctx, htmp); |
| 134 | |
| 135 | counter[3]++; |
| 136 | |
| 137 | bytes = PSS_HLEN; |
| 138 | if (bytes > count) |
| 139 | bytes = count; |
| 140 | |
| 141 | memcpy(mask, htmp, bytes); |
| 142 | mask += bytes; |
| 143 | count -= bytes; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Validate an RSA signature, using RSA-PSS, as described in PKCS #1 |
| 149 | * v2.2, section 9.1.2, with many parameters required to have fixed |
| 150 | * values. |
| 151 | */ |
| 152 | static int |
| 153 | bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen, |
| 154 | uint8_t *sig) |
| 155 | { |
| 156 | bootutil_sha256_context shactx; |
| 157 | uint8_t em[MBEDTLS_MPI_MAX_SIZE]; |
| 158 | uint8_t db_mask[PSS_MASK_LEN]; |
| 159 | uint8_t h2[PSS_HLEN]; |
| 160 | int i; |
| 161 | |
David Brown | cdb968f | 2017-06-05 12:57:26 -0600 | [diff] [blame^] | 162 | if (ctx->len != PSS_EMLEN || PSS_EMLEN > MBEDTLS_MPI_MAX_SIZE) { |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | if (hlen != PSS_HLEN) { |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | if (mbedtls_rsa_public(ctx, sig, em)) { |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * PKCS #1 v2.2, 9.1.2 EMSA-PSS-Verify |
| 176 | * |
| 177 | * emBits is 2048 |
| 178 | * emLen = ceil(emBits/8) = 256 |
| 179 | * |
| 180 | * The salt length is not known at the beginning. |
| 181 | */ |
| 182 | |
| 183 | /* Step 1. The message is constrained by the address space of a |
| 184 | * 32-bit processor, which is far less than the 2^61-1 limit of |
| 185 | * SHA-256. |
| 186 | */ |
| 187 | |
| 188 | /* Step 2. mHash is passed in as 'hash', with hLen the hlen |
| 189 | * argument. */ |
| 190 | |
| 191 | /* Step 3. if emLen < hLen + sLen + 2, inconsistent and stop. |
| 192 | * The salt length is not known at this point. |
| 193 | */ |
| 194 | |
| 195 | /* Step 4. If the rightmost octect of EM does have the value |
| 196 | * 0xbc, output inconsistent and stop. |
| 197 | */ |
| 198 | if (em[PSS_EMLEN - 1] != 0xbc) { |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | /* Step 5. Let maskedDB be the leftmost emLen - hLen - 1 octets |
| 203 | * of EM, and H be the next hLen octets. |
| 204 | * |
| 205 | * maskedDB is then the first 256 - 32 - 1 = 0-222 |
| 206 | * H is 32 bytes 223-254 |
| 207 | */ |
| 208 | |
| 209 | /* Step 6. If the leftmost 8emLen - emBits bits of the leftmost |
| 210 | * octet in maskedDB are not all equal to zero, output |
| 211 | * inconsistent and stop. |
| 212 | * |
| 213 | * 8emLen - emBits is zero, so there is nothing to test here. |
| 214 | */ |
| 215 | |
| 216 | /* Step 7. let dbMask = MGF(H, emLen - hLen - 1). */ |
| 217 | pss_mgf1(db_mask, &em[PSS_HASH_OFFSET]); |
| 218 | |
| 219 | /* Step 8. let DB = maskedDB xor dbMask. |
| 220 | * To avoid needing an additional buffer, store the 'db' in the |
| 221 | * same buffer as db_mask. From now, to the end of this function, |
| 222 | * db_mask refers to the unmasked 'db'. */ |
| 223 | for (i = 0; i < PSS_MASK_LEN; i++) { |
| 224 | db_mask[i] ^= em[i]; |
| 225 | } |
| 226 | |
| 227 | /* Step 9. Set the leftmost 8emLen - emBits bits of the leftmost |
| 228 | * octet in DB to zero. |
| 229 | * pycrypto seems to always make the emBits 2047, so we need to |
| 230 | * clear the top bit. */ |
| 231 | db_mask[0] &= 0x7F; |
| 232 | |
| 233 | /* Step 10. If the emLen - hLen - sLen - 2 leftmost octets of DB |
| 234 | * are not zero or if the octet at position emLen - hLen - sLen - |
| 235 | * 1 (the leftmost position is "position 1") does not have |
| 236 | * hexadecimal value 0x01, output "inconsistent" and stop. */ |
| 237 | for (i = 0; i < PSS_MASK_ZERO_COUNT; i++) { |
| 238 | if (db_mask[i] != 0) { |
| 239 | return -1; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (db_mask[PSS_MASK_ONE_POS] != 1) { |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | /* Step 11. Let salt be the last sLen octets of DB */ |
| 248 | |
| 249 | /* Step 12. Let M' = 0x00 00 00 00 00 00 00 00 || mHash || salt; */ |
| 250 | |
| 251 | /* Step 13. Let H' = Hash(M') */ |
| 252 | bootutil_sha256_init(&shactx); |
| 253 | bootutil_sha256_update(&shactx, pss_zeros, 8); |
| 254 | bootutil_sha256_update(&shactx, hash, PSS_HLEN); |
| 255 | bootutil_sha256_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN); |
| 256 | bootutil_sha256_finish(&shactx, h2); |
| 257 | |
| 258 | /* Step 14. If H = H', output "consistent". Otherwise, output |
| 259 | * "inconsistent". */ |
| 260 | if (memcmp(h2, &em[PSS_HASH_OFFSET], PSS_HLEN) != 0) { |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | #else /* BOOTUTIL_RSA_PKCS1_15 */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 267 | /* |
| 268 | * PKCS1.5 using RSA2048 computed over SHA256. |
| 269 | */ |
| 270 | static int |
| 271 | bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen, |
| 272 | uint8_t *sig) |
| 273 | { |
| 274 | uint8_t buf[MBEDTLS_MPI_MAX_SIZE]; |
| 275 | uint8_t *p; |
| 276 | |
| 277 | if (ctx->len != 256) { |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | if (mbedtls_rsa_public(ctx, sig, buf)) { |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | p = buf; |
| 286 | |
| 287 | if (*p++ != 0 || *p++ != MBEDTLS_RSA_SIGN) { |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | while (*p != 0) { |
| 292 | if (p >= buf + ctx->len - 1 || *p != 0xFF) { |
| 293 | return -1; |
| 294 | } |
| 295 | p++; |
| 296 | } |
| 297 | p++; |
| 298 | |
| 299 | if ((p - buf) + sizeof(sha256_oid) + hlen != ctx->len) { |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | if (memcmp(p, sha256_oid, sizeof(sha256_oid))) { |
| 304 | return -1; |
| 305 | } |
| 306 | p += sizeof(sha256_oid); |
| 307 | |
| 308 | if (memcmp(p, hash, hlen)) { |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 314 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 315 | |
| 316 | int |
| 317 | bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen, |
| 318 | uint8_t key_id) |
| 319 | { |
| 320 | mbedtls_rsa_context ctx; |
| 321 | int rc; |
| 322 | uint8_t *cp; |
| 323 | uint8_t *end; |
| 324 | |
| 325 | mbedtls_rsa_init(&ctx, 0, 0); |
| 326 | |
| 327 | cp = (uint8_t *)bootutil_keys[key_id].key; |
| 328 | end = cp + *bootutil_keys[key_id].len; |
| 329 | |
| 330 | rc = bootutil_parse_rsakey(&ctx, &cp, end); |
| 331 | if (rc || slen != ctx.len) { |
| 332 | mbedtls_rsa_free(&ctx); |
| 333 | return rc; |
| 334 | } |
| 335 | rc = bootutil_cmp_rsasig(&ctx, hash, hlen, sig); |
| 336 | mbedtls_rsa_free(&ctx); |
| 337 | |
| 338 | return rc; |
| 339 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 340 | #endif /* MCUBOOT_SIGN_RSA */ |