blob: 3c6fd7dfc6f255ab8eceb6371f0d2f0191414ca4 [file] [log] [blame]
Marko Kiiskilabf943392016-12-29 17:29:48 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Marko Kiiskila755daed2016-12-30 18:53:13 -080020#include <string.h>
21
Fabio Utzigba1fbe62017-07-21 14:01:20 -030022#ifdef MCUBOOT_MYNEWT
23#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030024#endif
25
Fabio Utzig19356bf2017-05-11 16:19:36 -030026#ifdef MCUBOOT_SIGN_EC256
Marko Kiiskilabf943392016-12-29 17:29:48 -080027#include "bootutil/sign_key.h"
28
29#include "mbedtls/oid.h"
30#include "mbedtls/asn1.h"
31
32#include "tinycrypt/ecc_dsa.h"
33#include "bootutil_priv.h"
34
35/*
36 * Declaring these like this adds NULL termination.
37 */
38static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
39static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
40
41/*
42 * Parse the public key used for signing.
43 */
44static int
Szymon Janc16184882017-09-22 18:32:11 -030045tinycrypt_import_key(uint8_t *pubkey, uint8_t *cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -080046{
47 size_t len;
48 mbedtls_asn1_buf alg;
49 mbedtls_asn1_buf param;
50
51 if (mbedtls_asn1_get_tag(&cp, end, &len,
52 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
53 return -1;
54 }
55 end = cp + len;
56
57 if (mbedtls_asn1_get_alg(&cp, end, &alg, &param)) {
58 return -2;
59 }
60 if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
61 memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
62 return -3;
63 }
64 if (param.len != sizeof(ec_secp256r1_oid) - 1 ||
65 memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) {
66 return -4;
67 }
68 if (mbedtls_asn1_get_bitstring_null(&cp, end, &len)) {
69 return -6;
70 }
71 if (cp + len != end) {
72 return -7;
73 }
74
75 if (len != 2 * NUM_ECC_BYTES + 1) {
76 return -8;
77 }
78 if (cp[0] != 0x04) {
79 return -9;
80 }
81
Szymon Janc16184882017-09-22 18:32:11 -030082 /* TODO avoid this copy? */
83 memcpy(pubkey, cp + 1, 2 * NUM_ECC_BYTES);
Marko Kiiskilabf943392016-12-29 17:29:48 -080084
85 return 0;
86}
87
88/*
89 * cp points to ASN1 string containing an integer.
90 * Verify the tag, and that the length is 32 bytes.
91 */
92static int
Szymon Janc16184882017-09-22 18:32:11 -030093tinycrypt_read_bigint(uint8_t i[NUM_ECC_BYTES], uint8_t **cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -080094{
95 size_t len;
96
97 if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) {
98 return -3;
99 }
Szymon Janc16184882017-09-22 18:32:11 -0300100
Marko Kiiskila755daed2016-12-30 18:53:13 -0800101 if (len > NUM_ECC_BYTES) {
Szymon Janc16184882017-09-22 18:32:11 -0300102 memcpy(i, *cp + len - NUM_ECC_BYTES, NUM_ECC_BYTES);
Marko Kiiskila755daed2016-12-30 18:53:13 -0800103 } else {
Szymon Janc16184882017-09-22 18:32:11 -0300104 memset(i + NUM_ECC_BYTES, 0, NUM_ECC_BYTES - len);
105 memcpy(i + NUM_ECC_BYTES - len, *cp, len);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800106 }
Marko Kiiskilabf943392016-12-29 17:29:48 -0800107 *cp += len;
108 return 0;
109}
110
111/*
112 * Read in signature. Signature has r and s encoded as integers.
113 */
114static int
Szymon Janc16184882017-09-22 18:32:11 -0300115tinycrypt_decode_sig(uint8_t signature[NUM_ECC_BYTES * 2], uint8_t *cp, uint8_t *end)
Marko Kiiskilabf943392016-12-29 17:29:48 -0800116{
117 int rc;
118 size_t len;
119
120 rc = mbedtls_asn1_get_tag(&cp, end, &len,
121 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
122 if (rc) {
123 return -1;
124 }
David Brownbaff96f2017-01-27 17:35:14 -0700125 if (cp + len > end) {
Marko Kiiskilabf943392016-12-29 17:29:48 -0800126 return -2;
127 }
Szymon Janc16184882017-09-22 18:32:11 -0300128
129 rc = tinycrypt_read_bigint(signature, &cp, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800130 if (rc) {
131 return -3;
132 }
Szymon Janc16184882017-09-22 18:32:11 -0300133 rc = tinycrypt_read_bigint(signature + NUM_ECC_BYTES, &cp, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800134 if (rc) {
135 return -4;
136 }
137 return 0;
138}
139
140int
141bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
142 uint8_t key_id)
143{
144 int rc;
145 uint8_t *cp;
146 uint8_t *end;
Szymon Janc16184882017-09-22 18:32:11 -0300147
148 uint8_t public_key[2 * NUM_ECC_BYTES];
149 uint8_t signature[2 * NUM_ECC_BYTES];
Marko Kiiskilabf943392016-12-29 17:29:48 -0800150
151 cp = (uint8_t *)bootutil_keys[key_id].key;
152 end = cp + *bootutil_keys[key_id].len;
153
Szymon Janc16184882017-09-22 18:32:11 -0300154 rc = tinycrypt_import_key(public_key, cp, end);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800155 if (rc) {
156 return -1;
157 }
158
Szymon Janc16184882017-09-22 18:32:11 -0300159 rc = tinycrypt_decode_sig(signature, sig, sig + slen);
Marko Kiiskilabf943392016-12-29 17:29:48 -0800160 if (rc) {
161 return -1;
162 }
163
164 /*
165 * This is simplified, as the hash length is also 32 bytes.
166 */
167 if (hlen != NUM_ECC_BYTES) {
168 return -1;
169 }
170
Szymon Janc16184882017-09-22 18:32:11 -0300171 rc = uECC_verify(public_key, hash, NUM_ECC_BYTES, signature, uECC_secp256r1());
Marko Kiiskilabf943392016-12-29 17:29:48 -0800172 if (rc == 1) {
173 return 0;
174 } else {
175 return -2;
176 }
177}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300178#endif /* MCUBOOT_SIGN_EC256 */