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