Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -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) 2016-2019 JUUL Labs |
| 5 | * Copyright (c) 2017 Linaro LTD |
| 6 | * |
| 7 | * Original license: |
| 8 | * |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -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 | |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [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_EC256 |
Roman Okhrimenko | 1dcc73b | 2020-10-01 19:22:14 +0300 | [diff] [blame] | 32 | /*TODO: remove this after cypress port mbedtls to abstract crypto api */ |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 33 | #if defined(MCUBOOT_USE_CC310) || defined(MCUBOOT_USE_MBED_TLS) |
Sigvart Hovland | 59f1d29 | 2020-10-28 14:40:09 +0100 | [diff] [blame] | 34 | #define NUM_ECC_BYTES (256 / 8) |
| 35 | #endif |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 36 | #if defined(MCUBOOT_USE_TINYCRYPT) || defined(MCUBOOT_USE_CC310) || \ |
| 37 | defined(MCUBOOT_USE_MBED_TLS) |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 38 | #include "bootutil/sign_key.h" |
| 39 | |
| 40 | #include "mbedtls/oid.h" |
| 41 | #include "mbedtls/asn1.h" |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 42 | #include "bootutil/crypto/ecdsa_p256.h" |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 43 | #include "bootutil_priv.h" |
| 44 | |
| 45 | /* |
| 46 | * Declaring these like this adds NULL termination. |
| 47 | */ |
| 48 | static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 49 | static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1; |
| 50 | |
| 51 | /* |
| 52 | * Parse the public key used for signing. |
| 53 | */ |
Roman Okhrimenko | 2f045a2 | 2021-03-05 14:06:33 +0200 | [diff] [blame] | 54 | #ifdef CY_MBEDTLS_HW_ACCELERATION |
| 55 | static int |
| 56 | bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end) |
| 57 | { |
| 58 | size_t len; |
| 59 | mbedtls_asn1_buf alg; |
| 60 | mbedtls_asn1_buf param; |
| 61 | |
| 62 | if (mbedtls_asn1_get_tag(p, end, &len, |
| 63 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 64 | return -1; |
| 65 | } |
| 66 | end = *p + len; |
| 67 | |
| 68 | if (mbedtls_asn1_get_alg(p, end, &alg, ¶m)) { |
| 69 | return -2; |
| 70 | } |
| 71 | if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| 72 | memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| 73 | return -3; |
| 74 | } |
| 75 | if (param.len != sizeof(ec_secp256r1_oid) - 1|| |
| 76 | memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) { |
| 77 | return -4; |
| 78 | } |
| 79 | |
| 80 | if (mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1)) { |
| 81 | return -5; |
| 82 | } |
| 83 | |
| 84 | if (mbedtls_asn1_get_bitstring_null(p, end, &len)) { |
| 85 | return -6; |
| 86 | } |
| 87 | if (*p + len != end) { |
| 88 | return -7; |
| 89 | } |
| 90 | |
| 91 | if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) { |
| 92 | return -8; |
| 93 | } |
| 94 | |
| 95 | if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) { |
| 96 | return -9; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 100 | #else /* !CY_MBEDTLS_HW_ACCELERATION */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 101 | static int |
Sigvart Hovland | 795cd0d | 2019-03-04 13:06:12 +0100 | [diff] [blame] | 102 | bootutil_import_key(uint8_t **cp, uint8_t *end) |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 103 | { |
| 104 | size_t len; |
| 105 | mbedtls_asn1_buf alg; |
| 106 | mbedtls_asn1_buf param; |
| 107 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 108 | if (mbedtls_asn1_get_tag(cp, end, &len, |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 109 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 110 | return -1; |
| 111 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 112 | end = *cp + len; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 113 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 114 | /* ECParameters (RFC5480) */ |
| 115 | if (mbedtls_asn1_get_alg(cp, end, &alg, ¶m)) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 116 | return -2; |
| 117 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 118 | /* id-ecPublicKey (RFC5480) */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 119 | if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| 120 | memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| 121 | return -3; |
| 122 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 123 | /* namedCurve (RFC5480) */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 124 | if (param.len != sizeof(ec_secp256r1_oid) - 1 || |
| 125 | memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) { |
| 126 | return -4; |
| 127 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 128 | /* ECPoint (RFC5480) */ |
| 129 | if (mbedtls_asn1_get_bitstring_null(cp, end, &len)) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 130 | return -6; |
| 131 | } |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 132 | if (*cp + len != end) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 133 | return -7; |
| 134 | } |
| 135 | |
| 136 | if (len != 2 * NUM_ECC_BYTES + 1) { |
| 137 | return -8; |
| 138 | } |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 139 | |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 140 | return 0; |
| 141 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 142 | #endif /* CY_MBEDTLS_HW_ACCELERATION */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 143 | |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 144 | #ifndef MCUBOOT_ECDSA_NEED_ASN1_SIG |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 145 | /* |
| 146 | * cp points to ASN1 string containing an integer. |
| 147 | * Verify the tag, and that the length is 32 bytes. |
| 148 | */ |
| 149 | static int |
Sigvart Hovland | 795cd0d | 2019-03-04 13:06:12 +0100 | [diff] [blame] | 150 | 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] | 151 | { |
| 152 | size_t len; |
| 153 | |
| 154 | if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) { |
| 155 | return -3; |
| 156 | } |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 157 | |
Fabio Utzig | 006994b | 2019-01-16 16:33:20 -0800 | [diff] [blame] | 158 | if (len >= NUM_ECC_BYTES) { |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 159 | memcpy(i, *cp + len - NUM_ECC_BYTES, NUM_ECC_BYTES); |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 160 | } else { |
Fabio Utzig | 006994b | 2019-01-16 16:33:20 -0800 | [diff] [blame] | 161 | memset(i, 0, NUM_ECC_BYTES - len); |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 162 | memcpy(i + NUM_ECC_BYTES - len, *cp, len); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 163 | } |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 164 | *cp += len; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Read in signature. Signature has r and s encoded as integers. |
| 170 | */ |
| 171 | static int |
Sigvart Hovland | 795cd0d | 2019-03-04 13:06:12 +0100 | [diff] [blame] | 172 | 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] | 173 | { |
| 174 | int rc; |
| 175 | size_t len; |
| 176 | |
| 177 | rc = mbedtls_asn1_get_tag(&cp, end, &len, |
| 178 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
| 179 | if (rc) { |
| 180 | return -1; |
| 181 | } |
David Brown | baff96f | 2017-01-27 17:35:14 -0700 | [diff] [blame] | 182 | if (cp + len > end) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 183 | return -2; |
| 184 | } |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 185 | |
Sigvart Hovland | 795cd0d | 2019-03-04 13:06:12 +0100 | [diff] [blame] | 186 | rc = bootutil_read_bigint(signature, &cp, end); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 187 | if (rc) { |
| 188 | return -3; |
| 189 | } |
Sigvart Hovland | 795cd0d | 2019-03-04 13:06:12 +0100 | [diff] [blame] | 190 | rc = bootutil_read_bigint(signature + NUM_ECC_BYTES, &cp, end); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 191 | if (rc) { |
| 192 | return -4; |
| 193 | } |
| 194 | return 0; |
| 195 | } |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 196 | #endif /* not MCUBOOT_ECDSA_NEED_ASN1_SIG */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 197 | |
| 198 | int |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 199 | 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] | 200 | uint8_t key_id) |
| 201 | { |
| 202 | int rc; |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 203 | bootutil_ecdsa_p256_context ctx; |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 204 | uint8_t *pubkey; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 205 | uint8_t *end; |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 206 | |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 207 | #ifndef MCUBOOT_ECDSA_NEED_ASN1_SIG |
Szymon Janc | 1618488 | 2017-09-22 18:32:11 -0300 | [diff] [blame] | 208 | uint8_t signature[2 * NUM_ECC_BYTES]; |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 209 | #endif |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 210 | |
Fabio Utzig | 33fa8ad | 2017-10-05 18:21:30 -0300 | [diff] [blame] | 211 | pubkey = (uint8_t *)bootutil_keys[key_id].key; |
| 212 | end = pubkey + *bootutil_keys[key_id].len; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 213 | |
Roman Okhrimenko | 2f045a2 | 2021-03-05 14:06:33 +0200 | [diff] [blame] | 214 | #ifdef CY_MBEDTLS_HW_ACCELERATION |
| 215 | mbedtls_ecdsa_init(&ctx); |
| 216 | rc = bootutil_parse_eckey(&ctx, &pubkey, end); |
| 217 | #else |
Sigvart Hovland | 795cd0d | 2019-03-04 13:06:12 +0100 | [diff] [blame] | 218 | rc = bootutil_import_key(&pubkey, end); |
Roman Okhrimenko | 2f045a2 | 2021-03-05 14:06:33 +0200 | [diff] [blame] | 219 | #endif |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 220 | if (rc) { |
| 221 | return -1; |
| 222 | } |
| 223 | |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 224 | #ifndef MCUBOOT_ECDSA_NEED_ASN1_SIG |
Sigvart Hovland | 795cd0d | 2019-03-04 13:06:12 +0100 | [diff] [blame] | 225 | rc = bootutil_decode_sig(signature, sig, sig + slen); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 226 | if (rc) { |
| 227 | return -1; |
| 228 | } |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 229 | #endif |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * This is simplified, as the hash length is also 32 bytes. |
| 233 | */ |
Roman Okhrimenko | 2f045a2 | 2021-03-05 14:06:33 +0200 | [diff] [blame] | 234 | #ifdef CY_MBEDTLS_HW_ACCELERATION |
| 235 | while (sig[slen - 1] == '\0') { |
| 236 | slen--; |
| 237 | } |
| 238 | rc = mbedtls_ecdsa_read_signature(&ctx, hash, hlen, sig, slen); |
| 239 | |
| 240 | #else /* CY_MBEDTLS_HW_ACCELERATION */ |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 241 | if (hlen != NUM_ECC_BYTES) { |
| 242 | return -1; |
| 243 | } |
| 244 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 245 | bootutil_ecdsa_p256_init(&ctx); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 246 | #ifdef MCUBOOT_ECDSA_NEED_ASN1_SIG |
| 247 | rc = bootutil_ecdsa_p256_verify(&ctx, pubkey, end - pubkey, hash, sig, slen); |
| 248 | #else |
| 249 | rc = bootutil_ecdsa_p256_verify(&ctx, pubkey, end - pubkey, hash, signature, |
| 250 | 2 * NUM_ECC_BYTES); |
| 251 | #endif |
Roman Okhrimenko | 2f045a2 | 2021-03-05 14:06:33 +0200 | [diff] [blame] | 252 | #endif /* CY_MBEDTLS_HW_ACCELERATION */ |
| 253 | |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 254 | bootutil_ecdsa_p256_drop(&ctx); |
Roman Okhrimenko | 2f045a2 | 2021-03-05 14:06:33 +0200 | [diff] [blame] | 255 | |
Sigvart Hovland | 65a6ab2 | 2019-03-11 15:56:04 +0100 | [diff] [blame] | 256 | return rc; |
| 257 | } |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 258 | |
Roman Okhrimenko | 1dcc73b | 2020-10-01 19:22:14 +0300 | [diff] [blame] | 259 | #endif /* MCUBOOT_USE_TINYCRYPT || defined MCUBOOT_USE_CC310 */ |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 260 | #endif /* MCUBOOT_SIGN_EC256 */ |