blob: ee2b594d6e0941c5f25a5f3e8885de8ad48486df [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
Marko Kiiskilabd144682016-12-30 18:54:18 -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_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -080027#include "bootutil/sign_key.h"
28
Christopher Collins92ea77f2016-12-12 15:59:26 -080029#include "mbedtls/ecdsa.h"
30#include "mbedtls/oid.h"
31#include "mbedtls/asn1.h"
32
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_secp224r1_oid[] = MBEDTLS_OID_EC_GRP_SECP224R1;
40
41/*
Marko Kiiskila351a0b22016-12-29 17:31:00 -080042 * Parse the public key used for signing.
Christopher Collins92ea77f2016-12-12 15:59:26 -080043 */
44static int
45bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end)
46{
47 size_t len;
48 mbedtls_asn1_buf alg;
49 mbedtls_asn1_buf param;
50
51 if (mbedtls_asn1_get_tag(p, end, &len,
52 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
53 return -1;
54 }
55 end = *p + len;
56
57 if (mbedtls_asn1_get_alg(p, 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_secp224r1_oid) - 1||
65 memcmp(param.p, ec_secp224r1_oid, sizeof(ec_secp224r1_oid) - 1)) {
66 return -4;
67 }
68
69 if (mbedtls_ecp_group_load_secp224r1(&ctx->grp)) {
70 return -5;
71 }
72
73 if (mbedtls_asn1_get_bitstring_null(p, end, &len)) {
74 return -6;
75 }
76 if (*p + len != end) {
77 return -7;
78 }
79
80 if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) {
81 return -8;
82 }
83
84 if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) {
85 return -9;
86 }
87 return 0;
88}
89
90static int
91bootutil_cmp_sig(mbedtls_ecdsa_context *ctx, uint8_t *hash, uint32_t hlen,
Fabio Utzig1a927dd2017-12-05 10:30:26 -020092 uint8_t *sig, size_t slen)
Christopher Collins92ea77f2016-12-12 15:59:26 -080093{
94 return mbedtls_ecdsa_read_signature(ctx, hash, hlen, sig, slen);
95}
96
97int
Fabio Utzig1a927dd2017-12-05 10:30:26 -020098bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 uint8_t key_id)
100{
101 int rc;
102 uint8_t *cp;
103 uint8_t *end;
104 mbedtls_ecdsa_context ctx;
105
106 mbedtls_ecdsa_init(&ctx);
107
108 cp = (uint8_t *)bootutil_keys[key_id].key;
109 end = cp + *bootutil_keys[key_id].len;
110
111 rc = bootutil_parse_eckey(&ctx, &cp, end);
112 if (rc) {
113 return -1;
114 }
115
116 while (sig[slen - 1] == '\0') {
117 slen--;
118 }
119 rc = bootutil_cmp_sig(&ctx, hash, hlen, sig, slen);
120 mbedtls_ecdsa_free(&ctx);
121
122 return rc;
123}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300124#endif /* MCUBOOT_SIGN_EC */