blob: ef99381e327774da0f1e7cd628d7ca134d72ae56 [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
Marko Kiiskilabf943392016-12-29 17:29:48 -080032#include "bootutil/sign_key.h"
33
34#include "mbedtls/oid.h"
35#include "mbedtls/asn1.h"
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090036#include "bootutil/crypto/ecdsa_p256.h"
Marko Kiiskilabf943392016-12-29 17:29:48 -080037#include "bootutil_priv.h"
38
39/*
40 * Declaring these like this adds NULL termination.
41 */
42static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
43static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
44
45/*
46 * Parse the public key used for signing.
47 */
48static int
Sigvart Hovland795cd0d2019-03-04 13:06:12 +010049bootutil_import_key(uint8_t **cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -080050{
51 size_t len;
52 mbedtls_asn1_buf alg;
53 mbedtls_asn1_buf param;
54
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030055 if (mbedtls_asn1_get_tag(cp, end, &len,
Marko Kiiskilabf943392016-12-29 17:29:48 -080056 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
57 return -1;
58 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030059 end = *cp + len;
Marko Kiiskilabf943392016-12-29 17:29:48 -080060
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030061 /* ECParameters (RFC5480) */
62 if (mbedtls_asn1_get_alg(cp, end, &alg, &param)) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080063 return -2;
64 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030065 /* id-ecPublicKey (RFC5480) */
Marko Kiiskilabf943392016-12-29 17:29:48 -080066 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 Utzig33fa8ad2017-10-05 18:21:30 -030070 /* namedCurve (RFC5480) */
Marko Kiiskilabf943392016-12-29 17:29:48 -080071 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 Utzig33fa8ad2017-10-05 18:21:30 -030075 /* ECPoint (RFC5480) */
76 if (mbedtls_asn1_get_bitstring_null(cp, end, &len)) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080077 return -6;
78 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030079 if (*cp + len != end) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080080 return -7;
81 }
82
83 if (len != 2 * NUM_ECC_BYTES + 1) {
84 return -8;
85 }
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030086 /* Is uncompressed? */
87 if (*cp[0] != 0x04) {
Marko Kiiskilabf943392016-12-29 17:29:48 -080088 return -9;
89 }
90
Fabio Utzig33fa8ad2017-10-05 18:21:30 -030091 (*cp)++;
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090092
Marko Kiiskilabf943392016-12-29 17:29:48 -080093 return 0;
94}
95
96/*
97 * cp points to ASN1 string containing an integer.
98 * Verify the tag, and that the length is 32 bytes.
99 */
100static int
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100101bootutil_read_bigint(uint8_t i[NUM_ECC_BYTES], uint8_t **cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -0800102{
103 size_t len;
104
105 if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) {
106 return -3;
107 }
Szymon Janc16184882017-09-22 18:32:11 -0300108
Fabio Utzig006994b2019-01-16 16:33:20 -0800109 if (len >= NUM_ECC_BYTES) {
Szymon Janc16184882017-09-22 18:32:11 -0300110 memcpy(i, *cp + len - NUM_ECC_BYTES, NUM_ECC_BYTES);
Marko Kiiskila755daed2016-12-30 18:53:13 -0800111 } else {
Fabio Utzig006994b2019-01-16 16:33:20 -0800112 memset(i, 0, NUM_ECC_BYTES - len);
Szymon Janc16184882017-09-22 18:32:11 -0300113 memcpy(i + NUM_ECC_BYTES - len, *cp, len);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800114 }
Marko Kiiskilabf943392016-12-29 17:29:48 -0800115 *cp += len;
116 return 0;
117}
118
119/*
120 * Read in signature. Signature has r and s encoded as integers.
121 */
122static int
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100123bootutil_decode_sig(uint8_t signature[NUM_ECC_BYTES * 2], uint8_t *cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -0800124{
125 int rc;
126 size_t len;
127
128 rc = mbedtls_asn1_get_tag(&cp, end, &len,
129 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
130 if (rc) {
131 return -1;
132 }
David Brownbaff96f2017-01-27 17:35:14 -0700133 if (cp + len > end) {
Marko Kiiskilabf943392016-12-29 17:29:48 -0800134 return -2;
135 }
Szymon Janc16184882017-09-22 18:32:11 -0300136
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100137 rc = bootutil_read_bigint(signature, &cp, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800138 if (rc) {
139 return -3;
140 }
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100141 rc = bootutil_read_bigint(signature + NUM_ECC_BYTES, &cp, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800142 if (rc) {
143 return -4;
144 }
145 return 0;
146}
147
148int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200149bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Marko Kiiskilabf943392016-12-29 17:29:48 -0800150 uint8_t key_id)
151{
152 int rc;
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900153 bootutil_ecdsa_p256_context ctx;
Fabio Utzig33fa8ad2017-10-05 18:21:30 -0300154 uint8_t *pubkey;
Marko Kiiskilabf943392016-12-29 17:29:48 -0800155 uint8_t *end;
Szymon Janc16184882017-09-22 18:32:11 -0300156
Szymon Janc16184882017-09-22 18:32:11 -0300157 uint8_t signature[2 * NUM_ECC_BYTES];
Marko Kiiskilabf943392016-12-29 17:29:48 -0800158
Fabio Utzig33fa8ad2017-10-05 18:21:30 -0300159 pubkey = (uint8_t *)bootutil_keys[key_id].key;
160 end = pubkey + *bootutil_keys[key_id].len;
Marko Kiiskilabf943392016-12-29 17:29:48 -0800161
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100162 rc = bootutil_import_key(&pubkey, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800163 if (rc) {
164 return -1;
165 }
166
Sigvart Hovland795cd0d2019-03-04 13:06:12 +0100167 rc = bootutil_decode_sig(signature, sig, sig + slen);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800168 if (rc) {
169 return -1;
170 }
171
172 /*
173 * This is simplified, as the hash length is also 32 bytes.
174 */
175 if (hlen != NUM_ECC_BYTES) {
176 return -1;
177 }
178
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900179 bootutil_ecdsa_p256_init(&ctx);
180 rc = bootutil_ecdsa_p256_verify(&ctx, pubkey, hash, signature);
181 bootutil_ecdsa_p256_drop(&ctx);
Sigvart Hovland65a6ab22019-03-11 15:56:04 +0100182 return rc;
183}
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900184
Fabio Utzig19356bf2017-05-11 16:19:36 -0300185#endif /* MCUBOOT_SIGN_EC256 */