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