blob: 63050c7a17edb7f51df7cc4dc193b86ac70029fd [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#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030023
Fabio Utzig19356bf2017-05-11 16:19:36 -030024#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "bootutil/sign_key.h"
26
Christopher Collins92ea77f2016-12-12 15:59:26 -080027#include "mbedtls/ecdsa.h"
28#include "mbedtls/oid.h"
29#include "mbedtls/asn1.h"
30
31#include "bootutil_priv.h"
32
33/*
34 * Declaring these like this adds NULL termination.
35 */
36static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
37static const uint8_t ec_secp224r1_oid[] = MBEDTLS_OID_EC_GRP_SECP224R1;
38
39/*
Marko Kiiskila351a0b22016-12-29 17:31:00 -080040 * Parse the public key used for signing.
Christopher Collins92ea77f2016-12-12 15:59:26 -080041 */
42static int
43bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end)
44{
45 size_t len;
46 mbedtls_asn1_buf alg;
47 mbedtls_asn1_buf param;
48
49 if (mbedtls_asn1_get_tag(p, end, &len,
50 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
51 return -1;
52 }
53 end = *p + len;
54
55 if (mbedtls_asn1_get_alg(p, end, &alg, &param)) {
56 return -2;
57 }
58 if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
59 memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
60 return -3;
61 }
62 if (param.len != sizeof(ec_secp224r1_oid) - 1||
63 memcmp(param.p, ec_secp224r1_oid, sizeof(ec_secp224r1_oid) - 1)) {
64 return -4;
65 }
66
Fabio Utzig891f4fb2018-01-10 08:37:11 -020067 if (mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP224R1)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 return -5;
69 }
70
71 if (mbedtls_asn1_get_bitstring_null(p, end, &len)) {
72 return -6;
73 }
74 if (*p + len != end) {
75 return -7;
76 }
77
78 if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) {
79 return -8;
80 }
81
82 if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) {
83 return -9;
84 }
85 return 0;
86}
87
88static int
89bootutil_cmp_sig(mbedtls_ecdsa_context *ctx, uint8_t *hash, uint32_t hlen,
Fabio Utzig1a927dd2017-12-05 10:30:26 -020090 uint8_t *sig, size_t slen)
Christopher Collins92ea77f2016-12-12 15:59:26 -080091{
92 return mbedtls_ecdsa_read_signature(ctx, hash, hlen, sig, slen);
93}
94
95int
Fabio Utzig1a927dd2017-12-05 10:30:26 -020096bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -080097 uint8_t key_id)
98{
99 int rc;
100 uint8_t *cp;
101 uint8_t *end;
102 mbedtls_ecdsa_context ctx;
103
104 mbedtls_ecdsa_init(&ctx);
105
106 cp = (uint8_t *)bootutil_keys[key_id].key;
107 end = cp + *bootutil_keys[key_id].len;
108
109 rc = bootutil_parse_eckey(&ctx, &cp, end);
110 if (rc) {
111 return -1;
112 }
113
114 while (sig[slen - 1] == '\0') {
115 slen--;
116 }
117 rc = bootutil_cmp_sig(&ctx, hash, hlen, sig, slen);
118 mbedtls_ecdsa_free(&ctx);
119
120 return rc;
121}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300122#endif /* MCUBOOT_SIGN_EC */