blob: b27a70b51771c8bdad8418350e86f84bd45fe835 [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
20#include "syscfg/syscfg.h"
21
22#if MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
23#include "bootutil/sign_key.h"
24
25#include "mbedtls/oid.h"
26#include "mbedtls/asn1.h"
27
28#include "tinycrypt/ecc_dsa.h"
29#include "bootutil_priv.h"
30
31/*
32 * Declaring these like this adds NULL termination.
33 */
34static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
35static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
36
37/*
38 * Parse the public key used for signing.
39 */
40static int
41tinycrypt_import_key(EccPoint *pubkey, uint8_t *cp, uint8_t *end)
42{
43 size_t len;
44 mbedtls_asn1_buf alg;
45 mbedtls_asn1_buf param;
46
47 if (mbedtls_asn1_get_tag(&cp, end, &len,
48 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
49 return -1;
50 }
51 end = cp + len;
52
53 if (mbedtls_asn1_get_alg(&cp, end, &alg, &param)) {
54 return -2;
55 }
56 if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
57 memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
58 return -3;
59 }
60 if (param.len != sizeof(ec_secp256r1_oid) - 1 ||
61 memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) {
62 return -4;
63 }
64 if (mbedtls_asn1_get_bitstring_null(&cp, end, &len)) {
65 return -6;
66 }
67 if (cp + len != end) {
68 return -7;
69 }
70
71 if (len != 2 * NUM_ECC_BYTES + 1) {
72 return -8;
73 }
74 if (cp[0] != 0x04) {
75 return -9;
76 }
77
78 ecc_bytes2native(pubkey->x, cp + 1);
79 ecc_bytes2native(pubkey->y, cp + 1 + NUM_ECC_BYTES);
80
81 return 0;
82}
83
84/*
85 * cp points to ASN1 string containing an integer.
86 * Verify the tag, and that the length is 32 bytes.
87 */
88static int
89tinycrypt_read_bigint(uint32_t i[NUM_ECC_DIGITS], uint8_t **cp, uint8_t *end)
90{
91 size_t len;
92
93 if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) {
94 return -3;
95 }
96
97 for (; *cp < end; *cp = *cp + 1, len--) {
98 if (**cp != 0) {
99 break;
100 }
101 }
102 if (len != NUM_ECC_BYTES) {
103 return -1;
104 }
105 ecc_bytes2native(i, *cp);
106 *cp += len;
107 return 0;
108}
109
110/*
111 * Read in signature. Signature has r and s encoded as integers.
112 */
113static int
114tinycrypt_decode_sig(uint32_t r[NUM_ECC_DIGITS], uint32_t s[NUM_ECC_DIGITS],
115 uint8_t *cp, uint8_t *end)
116{
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 }
125 if (cp + len != end) {
126 return -2;
127 }
128 rc = tinycrypt_read_bigint(r, &cp, end);
129 if (rc) {
130 return -3;
131 }
132 rc = tinycrypt_read_bigint(s, &cp, end);
133 if (rc) {
134 return -4;
135 }
136 return 0;
137}
138
139int
140bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
141 uint8_t key_id)
142{
143 int rc;
144 uint8_t *cp;
145 uint8_t *end;
146 EccPoint ctx;
147 uint32_t r[NUM_ECC_DIGITS];
148 uint32_t s[NUM_ECC_DIGITS];
149 uint32_t hash_t[NUM_ECC_DIGITS];
150
151 cp = (uint8_t *)bootutil_keys[key_id].key;
152 end = cp + *bootutil_keys[key_id].len;
153
154 rc = tinycrypt_import_key(&ctx, cp, end);
155 if (rc) {
156 return -1;
157 }
158
159 while (sig[slen - 1] == '\0') {
160 slen--;
161 }
162
163 rc = tinycrypt_decode_sig(r, s, sig, sig + slen);
164 if (rc) {
165 return -1;
166 }
167
168 /*
169 * This is simplified, as the hash length is also 32 bytes.
170 */
171 if (hlen != NUM_ECC_BYTES) {
172 return -1;
173 }
174
175 ecc_bytes2native(hash_t, hash);
176 rc = ecdsa_verify(&ctx, hash_t, r, s);
177 if (rc == 1) {
178 return 0;
179 } else {
180 return -2;
181 }
182}
183#endif /* MYNEWT_VAL(BOOTUTIL_SIGN_EC256) */