1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  *
4  * Copyright (c) 2016-2019 JUUL Labs
5  * Copyright (c) 2017 Linaro LTD
6  * Copyright (C) 2021-2023 Arm Limited
7  *
8  * Original license:
9  *
10  * Licensed to the Apache Software Foundation (ASF) under one
11  * or more contributor license agreements.  See the NOTICE file
12  * distributed with this work for additional information
13  * regarding copyright ownership.  The ASF licenses this file
14  * to you under the Apache License, Version 2.0 (the
15  * "License"); you may not use this file except in compliance
16  * with the License.  You may obtain a copy of the License at
17  *
18  *  http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing,
21  * software distributed under the License is distributed on an
22  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23  * KIND, either express or implied.  See the License for the
24  * specific language governing permissions and limitations
25  * under the License.
26  */
27 
28 #include <string.h>
29 
30 #include "mcuboot_config/mcuboot_config.h"
31 
32 #if defined(MCUBOOT_SIGN_EC256) || defined(MCUBOOT_SIGN_EC384)
33 
34 #include "bootutil_priv.h"
35 #include "bootutil/fault_injection_hardening.h"
36 #include "bootutil/crypto/ecdsa.h"
37 
38 fih_ret
bootutil_verify_sig(uint8_t * hash,uint32_t hlen,uint8_t * sig,size_t slen,uint8_t key_id)39 bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
40                     uint8_t key_id)
41 {
42     int rc;
43     bootutil_ecdsa_context ctx;
44     FIH_DECLARE(fih_rc, FIH_FAILURE);
45     uint8_t *pubkey;
46     uint8_t *end;
47 
48     pubkey = (uint8_t *)bootutil_keys[key_id].key;
49     end = pubkey + *bootutil_keys[key_id].len;
50     bootutil_ecdsa_init(&ctx);
51 
52     rc = bootutil_ecdsa_parse_public_key(&ctx, &pubkey, end);
53     if (rc) {
54         goto out;
55     }
56 
57     rc = bootutil_ecdsa_verify(&ctx, pubkey, end-pubkey, hash, hlen, sig, slen);
58     fih_rc = fih_ret_encode_zero_equality(rc);
59     if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
60         FIH_SET(fih_rc, FIH_FAILURE);
61     }
62 
63 out:
64     bootutil_ecdsa_drop(&ctx);
65 
66     FIH_RET(fih_rc);
67 }
68 
69 #endif /* MCUBOOT_SIGN_EC256 || MCUBOOT_SIGN_EC384 */
70