| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| /******************************************************************************* |
| * \copyright |
| * |
| * (c) 2019, Cypress Semiconductor Corporation |
| * or a subsidiary of Cypress Semiconductor Corporation. All rights |
| * reserved. |
| * |
| * This software is a port of the open source MCUBoot project. |
| * |
| * This file was modified to fit PSoC6-based MCUBoot applications. |
| * |
| * Portions of this software, including source code, documentation and related |
| * materials ("Software"), are owned by Cypress Semiconductor |
| * Corporation or one of its subsidiaries ("Cypress") and is protected by |
| * and subject to worldwide patent protection (United States and foreign), |
| * United States copyright laws and international treaty provisions. |
| * Therefore, you may use this Software only as provided in the license |
| * agreement accompanying the software package from which you |
| * obtained this Software ("EULA"). |
| * |
| * If no EULA applies, Cypress hereby grants you a personal, non- |
| * exclusive, non-transferable license to copy, modify, and compile the |
| * Software source code solely for use in connection with Cypress's |
| * integrated circuit products. Any reproduction, modification, translation, |
| * compilation, or representation of this Software except as specified |
| * above is prohibited without the express written permission of Cypress. |
| * |
| * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO |
| * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING, |
| * BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED |
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| * PARTICULAR PURPOSE. Cypress reserves the right to make |
| * changes to the Software without notice. Cypress does not assume any |
| * liability arising out of the application or use of the Software or any |
| * product or circuit described in the Software. Cypress does not |
| * authorize its products for use in any products where a malfunction or |
| * failure of the Cypress product may reasonably be expected to result in |
| * significant property damage, injury or death ("High Risk Product"). By |
| * including Cypress's product in a High Risk Product, the manufacturer |
| * of such system or application assumes all risk of such use and in doing |
| * so agrees to indemnify Cypress against all liability. |
| * |
| ********************************************************************************/ |
| |
| #include "mcuboot_config/mcuboot_config.h" |
| |
| #ifdef MCUBOOT_SIGN_EC256 |
| |
| #include "bootutil/sign_key.h" |
| |
| #ifdef MCUBOOT_USE_MBED_TLS |
| |
| #include "mbedtls/oid.h" |
| #include "mbedtls/asn1.h" |
| #include "mbedtls/ecdsa.h" |
| |
| #include "bootutil_priv.h" |
| |
| #include <string.h> |
| |
| /* |
| * Declaring these like this adds NULL termination. |
| */ |
| static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1; |
| |
| /* |
| * Parse the public key used for signing. |
| */ |
| static int |
| bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end) |
| { |
| size_t len; |
| mbedtls_asn1_buf alg; |
| mbedtls_asn1_buf param; |
| |
| if (mbedtls_asn1_get_tag(p, end, &len, |
| MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| return -1; |
| } |
| end = *p + len; |
| |
| if (mbedtls_asn1_get_alg(p, end, &alg, ¶m)) { |
| return -2; |
| } |
| if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| return -3; |
| } |
| if (param.len != sizeof(ec_secp256r1_oid) - 1|| |
| memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) { |
| return -4; |
| } |
| |
| if (mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1)) { |
| return -5; |
| } |
| |
| if (mbedtls_asn1_get_bitstring_null(p, end, &len)) { |
| return -6; |
| } |
| if (*p + len != end) { |
| return -7; |
| } |
| |
| if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) { |
| return -8; |
| } |
| |
| if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) { |
| return -9; |
| } |
| return 0; |
| } |
| |
| int |
| bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen, uint8_t key_id) |
| { |
| int rc; |
| uint8_t *cp; |
| uint8_t *end; |
| mbedtls_ecdsa_context ctx; |
| |
| mbedtls_ecdsa_init(&ctx); |
| |
| cp = (uint8_t *)bootutil_keys[key_id].key; |
| end = cp + *bootutil_keys[key_id].len; |
| |
| rc = bootutil_parse_eckey(&ctx, &cp, end); |
| if (rc) { |
| return -1; |
| } |
| |
| while (sig[slen - 1] == '\0') { |
| slen--; |
| } |
| |
| rc = mbedtls_ecdsa_read_signature(&ctx, hash, hlen, sig, slen); |
| |
| mbedtls_ecdsa_free(&ctx); |
| |
| return rc; |
| } |
| |
| #endif /* MCUBOOT_USE_MBED_TLS */ |
| |
| #endif /* MCUBOOT_SIGN_EC256 */ |