blob: 185624c7109f73250cc0a2f2050cbe886643e3be [file] [log] [blame]
Marko Kiiskilabf943392016-12-29 17:29:48 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * 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 Kiiskilabf943392016-12-29 17:29:48 -08009 * 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 Kiiskila755daed2016-12-30 18:53:13 -080027#include <string.h>
28
Fabio Utzigba1fbe62017-07-21 14:01:20 -030029#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030030
Fabio Utzig19356bf2017-05-11 16:19:36 -030031#ifdef MCUBOOT_SIGN_EC256
Roman Okhrimenko1dcc73b2020-10-01 19:22:14 +030032/*TODO: remove this after cypress port mbedtls to abstract crypto api */
33#if defined (MCUBOOT_USE_TINYCRYPT) || defined (MCUBOOT_USE_CC310)
Marko Kiiskilabf943392016-12-29 17:29:48 -080034#include "bootutil/sign_key.h"
35
36#include "mbedtls/oid.h"
37#include "mbedtls/asn1.h"
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090038#include "bootutil/crypto/ecdsa_p256.h"
Marko Kiiskilabf943392016-12-29 17:29:48 -080039#include "bootutil_priv.h"
40
41/*
42 * Declaring these like this adds NULL termination.
43 */
44static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
45static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
46
47/*
48 * Parse the public key used for signing.
49 */
50static int
Sigvart Hovland795cd0d2019-03-04 13:06:12 +010051bootutil_import_key(uint8_t **cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -080052{
53 size_t len;
54 mbedtls_asn1_buf alg;
55 mbedtls_asn1_buf param;
56
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030057 if (mbedtls_asn1_get_tag(cp, end, &len,
Marko Kiiskilabf943392016-12-29 17:29:48 -080058 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
59 return -1;
60 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030061 end = *cp + len;
Marko Kiiskilabf943392016-12-29 17:29:48 -080062
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030063 /* ECParameters (RFC5480) */
64 if (mbedtls_asn1_get_alg(cp, end, &alg, &param)) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080065 return -2;
66 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030067 /* id-ecPublicKey (RFC5480) */
Marko Kiiskilabf943392016-12-29 17:29:48 -080068 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 Utzig33fa8ad2017-10-05 18:21:30 -030072 /* namedCurve (RFC5480) */
Marko Kiiskilabf943392016-12-29 17:29:48 -080073 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 Utzig33fa8ad2017-10-05 18:21:30 -030077 /* ECPoint (RFC5480) */
78 if (mbedtls_asn1_get_bitstring_null(cp, end, &len)) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080079 return -6;
80 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030081 if (*cp + len != end) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080082 return -7;
83 }
84
85 if (len != 2 * NUM_ECC_BYTES + 1) {
86 return -8;
87 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030088 /* Is uncompressed? */
89 if (*cp[0] != 0x04) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080090 return -9;
91 }
92
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030093 (*cp)++;
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090094
Marko Kiiskilabf943392016-12-29 17:29:48 -080095 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 */
102static int
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100103bootutil_read_bigint(uint8_t i[NUM_ECC_BYTES], uint8_t **cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -0800104{
105 size_t len;
106
107 if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) {
108 return -3;
109 }
Szymon Janc16184882017-09-22 18:32:11 -0300110
Fabio Utzig006994b2019-01-16 16:33:20 -0800111 if (len >= NUM_ECC_BYTES) {
Szymon Janc16184882017-09-22 18:32:11 -0300112 memcpy(i, *cp + len - NUM_ECC_BYTES, NUM_ECC_BYTES);
Marko Kiiskila755daed2016-12-30 18:53:13 -0800113 } else {
Fabio Utzig006994b2019-01-16 16:33:20 -0800114 memset(i, 0, NUM_ECC_BYTES - len);
Szymon Janc16184882017-09-22 18:32:11 -0300115 memcpy(i + NUM_ECC_BYTES - len, *cp, len);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800116 }
Marko Kiiskilabf943392016-12-29 17:29:48 -0800117 *cp += len;
118 return 0;
119}
120
121/*
122 * Read in signature. Signature has r and s encoded as integers.
123 */
124static int
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100125bootutil_decode_sig(uint8_t signature[NUM_ECC_BYTES * 2], uint8_t *cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -0800126{
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 Brownbaff96f2017-01-27 17:35:14 -0700135 if (cp + len > end) {
Marko Kiiskilabf943392016-12-29 17:29:48 -0800136 return -2;
137 }
Szymon Janc16184882017-09-22 18:32:11 -0300138
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100139 rc = bootutil_read_bigint(signature, &cp, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800140 if (rc) {
141 return -3;
142 }
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100143 rc = bootutil_read_bigint(signature + NUM_ECC_BYTES, &cp, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800144 if (rc) {
145 return -4;
146 }
147 return 0;
148}
149
150int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200151bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Marko Kiiskilabf943392016-12-29 17:29:48 -0800152 uint8_t key_id)
153{
154 int rc;
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900155 bootutil_ecdsa_p256_context ctx;
Fabio Utzig33fa8ad2017-10-05 18:21:30 -0300156 uint8_t *pubkey;
Marko Kiiskilabf943392016-12-29 17:29:48 -0800157 uint8_t *end;
Szymon Janc16184882017-09-22 18:32:11 -0300158
Szymon Janc16184882017-09-22 18:32:11 -0300159 uint8_t signature[2 * NUM_ECC_BYTES];
Marko Kiiskilabf943392016-12-29 17:29:48 -0800160
Fabio Utzig33fa8ad2017-10-05 18:21:30 -0300161 pubkey = (uint8_t *)bootutil_keys[key_id].key;
162 end = pubkey + *bootutil_keys[key_id].len;
Marko Kiiskilabf943392016-12-29 17:29:48 -0800163
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100164 rc = bootutil_import_key(&pubkey, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800165 if (rc) {
166 return -1;
167 }
168
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100169 rc = bootutil_decode_sig(signature, sig, sig + slen);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800170 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ž Hrastnik4f4833d2020-09-14 13:53:31 +0900181 bootutil_ecdsa_p256_init(&ctx);
182 rc = bootutil_ecdsa_p256_verify(&ctx, pubkey, hash, signature);
183 bootutil_ecdsa_p256_drop(&ctx);
Sigvart Hovland65a6ab22019-03-11 15:56:04 +0100184 return rc;
185}
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900186
Roman Okhrimenko1dcc73b2020-10-01 19:22:14 +0300187#endif /* MCUBOOT_USE_TINYCRYPT || defined MCUBOOT_USE_CC310 */
Fabio Utzig19356bf2017-05-11 16:19:36 -0300188#endif /* MCUBOOT_SIGN_EC256 */