blob: de469af0825fe1ed40a14725f57e169dee884420 [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
Roman Okhrimenko977b3752022-03-31 14:40:48 +03005 * Copyright (C) 2021 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07006 *
7 * Original license:
8 *
Christopher Collins92ea77f2016-12-12 15:59:26 -08009 * Licensed to the Apache Software Foundation (ASF) under one
10 * or more contributor license agreements. See the NOTICE file
11 * distributed with this work for additional information
12 * regarding copyright ownership. The ASF licenses this file
13 * to you under the Apache License, Version 2.0 (the
14 * "License"); you may not use this file except in compliance
15 * with the License. You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing,
20 * software distributed under the License is distributed on an
21 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22 * KIND, either express or implied. See the License for the
23 * specific language governing permissions and limitations
24 * under the License.
25 */
26
Marko Kiiskilabd144682016-12-30 18:54:18 -080027#include <string.h>
28
Fabio Utzigba1fbe62017-07-21 14:01:20 -030029#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030030
Fabio Utzig19356bf2017-05-11 16:19:36 -030031#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "bootutil/sign_key.h"
33
Christopher Collins92ea77f2016-12-12 15:59:26 -080034#include "mbedtls/ecdsa.h"
35#include "mbedtls/oid.h"
36#include "mbedtls/asn1.h"
37
Roman Okhrimenko977b3752022-03-31 14:40:48 +030038#include "bootutil/crypto/common.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "bootutil_priv.h"
40
41/*
42 * Declaring these like this adds NULL termination.
43 */
44static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
45static const uint8_t ec_secp224r1_oid[] = MBEDTLS_OID_EC_GRP_SECP224R1;
46
47/*
Marko Kiiskila351a0b22016-12-29 17:31:00 -080048 * Parse the public key used for signing.
Christopher Collins92ea77f2016-12-12 15:59:26 -080049 */
50static int
51bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end)
52{
53 size_t len;
54 mbedtls_asn1_buf alg;
55 mbedtls_asn1_buf param;
56
57 if (mbedtls_asn1_get_tag(p, end, &len,
58 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
59 return -1;
60 }
61 end = *p + len;
62
63 if (mbedtls_asn1_get_alg(p, end, &alg, &param)) {
64 return -2;
65 }
66 if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
67 memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
68 return -3;
69 }
70 if (param.len != sizeof(ec_secp224r1_oid) - 1||
71 memcmp(param.p, ec_secp224r1_oid, sizeof(ec_secp224r1_oid) - 1)) {
72 return -4;
73 }
74
Roman Okhrimenko977b3752022-03-31 14:40:48 +030075 if (mbedtls_ecp_group_load(&ctx->MBEDTLS_CONTEXT_MEMBER(grp), MBEDTLS_ECP_DP_SECP224R1)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -080076 return -5;
77 }
78
79 if (mbedtls_asn1_get_bitstring_null(p, end, &len)) {
80 return -6;
81 }
82 if (*p + len != end) {
83 return -7;
84 }
85
Roman Okhrimenko977b3752022-03-31 14:40:48 +030086 if (mbedtls_ecp_point_read_binary(&ctx->MBEDTLS_CONTEXT_MEMBER(grp), &ctx->MBEDTLS_CONTEXT_MEMBER(Q), *p, end - *p)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 return -8;
88 }
89
Roman Okhrimenko977b3752022-03-31 14:40:48 +030090 if (mbedtls_ecp_check_pubkey(&ctx->MBEDTLS_CONTEXT_MEMBER(grp), &ctx->MBEDTLS_CONTEXT_MEMBER(Q))) {
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 return -9;
92 }
93 return 0;
94}
95
96static int
97bootutil_cmp_sig(mbedtls_ecdsa_context *ctx, uint8_t *hash, uint32_t hlen,
Fabio Utzig1a927dd2017-12-05 10:30:26 -020098 uint8_t *sig, size_t slen)
Christopher Collins92ea77f2016-12-12 15:59:26 -080099{
100 return mbedtls_ecdsa_read_signature(ctx, hash, hlen, sig, slen);
101}
102
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300103fih_int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200104bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 uint8_t key_id)
106{
107 int rc;
108 uint8_t *cp;
109 uint8_t *end;
110 mbedtls_ecdsa_context ctx;
111
112 mbedtls_ecdsa_init(&ctx);
113
114 cp = (uint8_t *)bootutil_keys[key_id].key;
115 end = cp + *bootutil_keys[key_id].len;
116
117 rc = bootutil_parse_eckey(&ctx, &cp, end);
118 if (rc) {
119 return -1;
120 }
121
122 while (sig[slen - 1] == '\0') {
123 slen--;
124 }
125 rc = bootutil_cmp_sig(&ctx, hash, hlen, sig, slen);
126 mbedtls_ecdsa_free(&ctx);
127
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300128 FIH_RET(fih_int_encode_zero_equality(rc));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300130#endif /* MCUBOOT_SIGN_EC */