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