Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -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 | |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 22 | #ifdef MCUBOOT_MYNEWT |
| 23 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 24 | #endif |
| 25 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 26 | #ifdef MCUBOOT_SIGN_EC256 |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 27 | #include "bootutil/sign_key.h" |
| 28 | |
| 29 | #include "mbedtls/oid.h" |
| 30 | #include "mbedtls/asn1.h" |
| 31 | |
| 32 | #include "tinycrypt/ecc_dsa.h" |
| 33 | #include "bootutil_priv.h" |
| 34 | |
| 35 | /* |
| 36 | * Declaring these like this adds NULL termination. |
| 37 | */ |
| 38 | static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 39 | static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1; |
| 40 | |
| 41 | /* |
| 42 | * Parse the public key used for signing. |
| 43 | */ |
| 44 | static int |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 45 | tinycrypt_import_key(uint8_t **cp, uint8_t *end) |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 46 | { |
| 47 | size_t len; |
| 48 | mbedtls_asn1_buf alg; |
| 49 | mbedtls_asn1_buf param; |
| 50 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 51 | if (mbedtls_asn1_get_tag(cp, end, &len, |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 52 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 53 | return -1; |
| 54 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 55 | end = *cp + len; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 56 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 57 | /* ECParameters (RFC5480) */ |
| 58 | if (mbedtls_asn1_get_alg(cp, end, &alg, ¶m)) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 59 | return -2; |
| 60 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 61 | /* id-ecPublicKey (RFC5480) */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 62 | if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| 63 | memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| 64 | return -3; |
| 65 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 66 | /* namedCurve (RFC5480) */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 67 | if (param.len != sizeof(ec_secp256r1_oid) - 1 || |
| 68 | memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) { |
| 69 | return -4; |
| 70 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 71 | /* ECPoint (RFC5480) */ |
| 72 | if (mbedtls_asn1_get_bitstring_null(cp, end, &len)) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 73 | return -6; |
| 74 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 75 | if (*cp + len != end) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 76 | return -7; |
| 77 | } |
| 78 | |
| 79 | if (len != 2 * NUM_ECC_BYTES + 1) { |
| 80 | return -8; |
| 81 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 82 | /* Is uncompressed? */ |
| 83 | if (*cp[0] != 0x04) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 84 | return -9; |
| 85 | } |
| 86 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 87 | (*cp)++; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * cp points to ASN1 string containing an integer. |
| 93 | * Verify the tag, and that the length is 32 bytes. |
| 94 | */ |
| 95 | static int |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 96 | tinycrypt_read_bigint(uint8_t i[NUM_ECC_BYTES], uint8_t **cp, uint8_t *end) |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 97 | { |
| 98 | size_t len; |
| 99 | |
| 100 | if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) { |
| 101 | return -3; |
| 102 | } |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 103 | |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 104 | if (len > NUM_ECC_BYTES) { |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 105 | memcpy(i, *cp + len - NUM_ECC_BYTES, NUM_ECC_BYTES); |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 106 | } else { |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 107 | memset(i + NUM_ECC_BYTES, 0, NUM_ECC_BYTES - len); |
| 108 | memcpy(i + NUM_ECC_BYTES - len, *cp, len); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 109 | } |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 110 | *cp += len; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Read in signature. Signature has r and s encoded as integers. |
| 116 | */ |
| 117 | static int |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 118 | tinycrypt_decode_sig(uint8_t signature[NUM_ECC_BYTES * 2], uint8_t *cp, uint8_t *end) |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 119 | { |
| 120 | int rc; |
| 121 | size_t len; |
| 122 | |
| 123 | rc = mbedtls_asn1_get_tag(&cp, end, &len, |
| 124 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
| 125 | if (rc) { |
| 126 | return -1; |
| 127 | } |
David Brown | baff96f | 2017-01-27 17:35:14 -0700 | [diff] [blame] | 128 | if (cp + len > end) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 129 | return -2; |
| 130 | } |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 131 | |
| 132 | rc = tinycrypt_read_bigint(signature, &cp, end); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 133 | if (rc) { |
| 134 | return -3; |
| 135 | } |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 136 | rc = tinycrypt_read_bigint(signature + NUM_ECC_BYTES, &cp, end); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 137 | if (rc) { |
| 138 | return -4; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int |
| 144 | bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen, |
| 145 | uint8_t key_id) |
| 146 | { |
| 147 | int rc; |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 148 | uint8_t *pubkey; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 149 | uint8_t *end; |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 150 | |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 151 | uint8_t signature[2 * NUM_ECC_BYTES]; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 152 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 153 | pubkey = (uint8_t *)bootutil_keys[key_id].key; |
| 154 | end = pubkey + *bootutil_keys[key_id].len; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 155 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 156 | rc = tinycrypt_import_key(&pubkey, end); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 157 | if (rc) { |
| 158 | return -1; |
| 159 | } |
| 160 | |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 161 | rc = tinycrypt_decode_sig(signature, sig, sig + slen); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 162 | if (rc) { |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * This is simplified, as the hash length is also 32 bytes. |
| 168 | */ |
| 169 | if (hlen != NUM_ECC_BYTES) { |
| 170 | return -1; |
| 171 | } |
| 172 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame^] | 173 | rc = uECC_verify(pubkey, hash, NUM_ECC_BYTES, signature, uECC_secp256r1()); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 174 | if (rc == 1) { |
| 175 | return 0; |
| 176 | } else { |
| 177 | return -2; |
| 178 | } |
| 179 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 180 | #endif /* MCUBOOT_SIGN_EC256 */ |