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 | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 22 | #ifdef MCUBOOT_SIGN_RSA |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 23 | #include "bootutil/sign_key.h" |
| 24 | |
| 25 | #include "mbedtls/rsa.h" |
| 26 | #include "mbedtls/asn1.h" |
| 27 | |
| 28 | #include "bootutil_priv.h" |
| 29 | |
| 30 | static const uint8_t sha256_oid[] = { |
| 31 | 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, |
| 32 | 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, |
| 33 | 0x00, 0x04, 0x20 |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * Parse the public key used for signing. Simple RSA format. |
| 38 | */ |
| 39 | static int |
| 40 | bootutil_parse_rsakey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end) |
| 41 | { |
| 42 | int rc; |
| 43 | size_t len; |
| 44 | |
| 45 | if ((rc = mbedtls_asn1_get_tag(p, end, &len, |
| 46 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | if (*p + len != end) { |
| 51 | return -2; |
| 52 | } |
| 53 | |
| 54 | if ((rc = mbedtls_asn1_get_mpi(p, end, &ctx->N)) != 0 || |
| 55 | (rc = mbedtls_asn1_get_mpi(p, end, &ctx->E)) != 0) { |
| 56 | return -3; |
| 57 | } |
| 58 | |
| 59 | if (*p != end) { |
| 60 | return -4; |
| 61 | } |
| 62 | |
| 63 | if ((rc = mbedtls_rsa_check_pubkey(ctx)) != 0) { |
| 64 | return -5; |
| 65 | } |
| 66 | |
| 67 | ctx->len = mbedtls_mpi_size(&ctx->N); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * PKCS1.5 using RSA2048 computed over SHA256. |
| 74 | */ |
| 75 | static int |
| 76 | bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen, |
| 77 | uint8_t *sig) |
| 78 | { |
| 79 | uint8_t buf[MBEDTLS_MPI_MAX_SIZE]; |
| 80 | uint8_t *p; |
| 81 | |
| 82 | if (ctx->len != 256) { |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | if (mbedtls_rsa_public(ctx, sig, buf)) { |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | p = buf; |
| 91 | |
| 92 | if (*p++ != 0 || *p++ != MBEDTLS_RSA_SIGN) { |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | while (*p != 0) { |
| 97 | if (p >= buf + ctx->len - 1 || *p != 0xFF) { |
| 98 | return -1; |
| 99 | } |
| 100 | p++; |
| 101 | } |
| 102 | p++; |
| 103 | |
| 104 | if ((p - buf) + sizeof(sha256_oid) + hlen != ctx->len) { |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | if (memcmp(p, sha256_oid, sizeof(sha256_oid))) { |
| 109 | return -1; |
| 110 | } |
| 111 | p += sizeof(sha256_oid); |
| 112 | |
| 113 | if (memcmp(p, hash, hlen)) { |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int |
| 121 | bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen, |
| 122 | uint8_t key_id) |
| 123 | { |
| 124 | mbedtls_rsa_context ctx; |
| 125 | int rc; |
| 126 | uint8_t *cp; |
| 127 | uint8_t *end; |
| 128 | |
| 129 | mbedtls_rsa_init(&ctx, 0, 0); |
| 130 | |
| 131 | cp = (uint8_t *)bootutil_keys[key_id].key; |
| 132 | end = cp + *bootutil_keys[key_id].len; |
| 133 | |
| 134 | rc = bootutil_parse_rsakey(&ctx, &cp, end); |
| 135 | if (rc || slen != ctx.len) { |
| 136 | mbedtls_rsa_free(&ctx); |
| 137 | return rc; |
| 138 | } |
| 139 | rc = bootutil_cmp_rsasig(&ctx, hash, hlen, sig); |
| 140 | mbedtls_rsa_free(&ctx); |
| 141 | |
| 142 | return rc; |
| 143 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 144 | #endif /* MCUBOOT_SIGN_RSA */ |