blob: a127b74bf6311e4003df43ef2f3bceb545500d56 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2016-2018 JUUL Labs
5 *
6 * Original license:
7 *
Christopher Collins92ea77f2016-12-12 15:59:26 -08008 * Licensed to the Apache Software Foundation (ASF) under one
9 * or more contributor license agreements. See the NOTICE file
10 * distributed with this work for additional information
11 * regarding copyright ownership. The ASF licenses this file
12 * to you under the Apache License, Version 2.0 (the
13 * "License"); you may not use this file except in compliance
14 * with the License. You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing,
19 * software distributed under the License is distributed on an
20 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 * KIND, either express or implied. See the License for the
22 * specific language governing permissions and limitations
23 * under the License.
24 */
25
Marko Kiiskilabd144682016-12-30 18:54:18 -080026#include <string.h>
27
Fabio Utzigba1fbe62017-07-21 14:01:20 -030028#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030029
Fabio Utzig19356bf2017-05-11 16:19:36 -030030#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include "bootutil/sign_key.h"
32
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include "mbedtls/ecdsa.h"
34#include "mbedtls/oid.h"
35#include "mbedtls/asn1.h"
36
37#include "bootutil_priv.h"
38
39/*
40 * Declaring these like this adds NULL termination.
41 */
42static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
43static const uint8_t ec_secp224r1_oid[] = MBEDTLS_OID_EC_GRP_SECP224R1;
44
45/*
Marko Kiiskila351a0b22016-12-29 17:31:00 -080046 * Parse the public key used for signing.
Christopher Collins92ea77f2016-12-12 15:59:26 -080047 */
48static int
49bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end)
50{
51 size_t len;
52 mbedtls_asn1_buf alg;
53 mbedtls_asn1_buf param;
54
55 if (mbedtls_asn1_get_tag(p, end, &len,
56 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
57 return -1;
58 }
59 end = *p + len;
60
61 if (mbedtls_asn1_get_alg(p, end, &alg, &param)) {
62 return -2;
63 }
64 if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
65 memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
66 return -3;
67 }
68 if (param.len != sizeof(ec_secp224r1_oid) - 1||
69 memcmp(param.p, ec_secp224r1_oid, sizeof(ec_secp224r1_oid) - 1)) {
70 return -4;
71 }
72
Fabio Utzig891f4fb2018-01-10 08:37:11 -020073 if (mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP224R1)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -080074 return -5;
75 }
76
77 if (mbedtls_asn1_get_bitstring_null(p, end, &len)) {
78 return -6;
79 }
80 if (*p + len != end) {
81 return -7;
82 }
83
84 if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) {
85 return -8;
86 }
87
88 if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) {
89 return -9;
90 }
91 return 0;
92}
93
94static int
95bootutil_cmp_sig(mbedtls_ecdsa_context *ctx, uint8_t *hash, uint32_t hlen,
Fabio Utzig1a927dd2017-12-05 10:30:26 -020096 uint8_t *sig, size_t slen)
Christopher Collins92ea77f2016-12-12 15:59:26 -080097{
98 return mbedtls_ecdsa_read_signature(ctx, hash, hlen, sig, slen);
99}
100
101int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200102bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103 uint8_t key_id)
104{
105 int rc;
106 uint8_t *cp;
107 uint8_t *end;
108 mbedtls_ecdsa_context ctx;
109
110 mbedtls_ecdsa_init(&ctx);
111
112 cp = (uint8_t *)bootutil_keys[key_id].key;
113 end = cp + *bootutil_keys[key_id].len;
114
115 rc = bootutil_parse_eckey(&ctx, &cp, end);
116 if (rc) {
117 return -1;
118 }
119
120 while (sig[slen - 1] == '\0') {
121 slen--;
122 }
123 rc = bootutil_cmp_sig(&ctx, hash, hlen, sig, slen);
124 mbedtls_ecdsa_free(&ctx);
125
126 return rc;
127}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300128#endif /* MCUBOOT_SIGN_EC */