blob: 6ddac59ed9d85060ae1f4b48e5bdee0c037fa83c [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -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_EC)
23#include "bootutil/sign_key.h"
24
25#include "mbedtls/sha256.h"
26#include "mbedtls/ecdsa.h"
27#include "mbedtls/oid.h"
28#include "mbedtls/asn1.h"
29
30#include "bootutil_priv.h"
31
32/*
33 * Declaring these like this adds NULL termination.
34 */
35static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
36static const uint8_t ec_secp224r1_oid[] = MBEDTLS_OID_EC_GRP_SECP224R1;
37
38/*
39 * Parse the public key used for signing. Simple RSA format.
40 */
41static int
42bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end)
43{
44 size_t len;
45 mbedtls_asn1_buf alg;
46 mbedtls_asn1_buf param;
47
48 if (mbedtls_asn1_get_tag(p, end, &len,
49 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
50 return -1;
51 }
52 end = *p + len;
53
54 if (mbedtls_asn1_get_alg(p, end, &alg, &param)) {
55 return -2;
56 }
57 if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
58 memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
59 return -3;
60 }
61 if (param.len != sizeof(ec_secp224r1_oid) - 1||
62 memcmp(param.p, ec_secp224r1_oid, sizeof(ec_secp224r1_oid) - 1)) {
63 return -4;
64 }
65
66 if (mbedtls_ecp_group_load_secp224r1(&ctx->grp)) {
67 return -5;
68 }
69
70 if (mbedtls_asn1_get_bitstring_null(p, end, &len)) {
71 return -6;
72 }
73 if (*p + len != end) {
74 return -7;
75 }
76
77 if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) {
78 return -8;
79 }
80
81 if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) {
82 return -9;
83 }
84 return 0;
85}
86
87static int
88bootutil_cmp_sig(mbedtls_ecdsa_context *ctx, uint8_t *hash, uint32_t hlen,
89 uint8_t *sig, int slen)
90{
91 return mbedtls_ecdsa_read_signature(ctx, hash, hlen, sig, slen);
92}
93
94int
95bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
96 uint8_t key_id)
97{
98 int rc;
99 uint8_t *cp;
100 uint8_t *end;
101 mbedtls_ecdsa_context ctx;
102
103 mbedtls_ecdsa_init(&ctx);
104
105 cp = (uint8_t *)bootutil_keys[key_id].key;
106 end = cp + *bootutil_keys[key_id].len;
107
108 rc = bootutil_parse_eckey(&ctx, &cp, end);
109 if (rc) {
110 return -1;
111 }
112
113 while (sig[slen - 1] == '\0') {
114 slen--;
115 }
116 rc = bootutil_cmp_sig(&ctx, hash, hlen, sig, slen);
117 mbedtls_ecdsa_free(&ctx);
118
119 return rc;
120}
121#endif /* MYNEWT_VAL(BOOTUTIL_SIGN_EC) */