Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | /******************************************************************************* |
| 21 | * \copyright |
| 22 | * |
| 23 | * (c) 2019, Cypress Semiconductor Corporation |
| 24 | * or a subsidiary of Cypress Semiconductor Corporation. All rights |
| 25 | * reserved. |
| 26 | * |
| 27 | * This software is a port of the open source MCUBoot project. |
| 28 | * |
| 29 | * This file was modified to fit PSoC6-based MCUBoot applications. |
| 30 | * |
| 31 | * Portions of this software, including source code, documentation and related |
| 32 | * materials ("Software"), are owned by Cypress Semiconductor |
| 33 | * Corporation or one of its subsidiaries ("Cypress") and is protected by |
| 34 | * and subject to worldwide patent protection (United States and foreign), |
| 35 | * United States copyright laws and international treaty provisions. |
| 36 | * Therefore, you may use this Software only as provided in the license |
| 37 | * agreement accompanying the software package from which you |
| 38 | * obtained this Software ("EULA"). |
| 39 | * |
| 40 | * If no EULA applies, Cypress hereby grants you a personal, non- |
| 41 | * exclusive, non-transferable license to copy, modify, and compile the |
| 42 | * Software source code solely for use in connection with Cypress's |
| 43 | * integrated circuit products. Any reproduction, modification, translation, |
| 44 | * compilation, or representation of this Software except as specified |
| 45 | * above is prohibited without the express written permission of Cypress. |
| 46 | * |
| 47 | * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO |
| 48 | * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING, |
| 49 | * BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED |
| 50 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 51 | * PARTICULAR PURPOSE. Cypress reserves the right to make |
| 52 | * changes to the Software without notice. Cypress does not assume any |
| 53 | * liability arising out of the application or use of the Software or any |
| 54 | * product or circuit described in the Software. Cypress does not |
| 55 | * authorize its products for use in any products where a malfunction or |
| 56 | * failure of the Cypress product may reasonably be expected to result in |
| 57 | * significant property damage, injury or death ("High Risk Product"). By |
| 58 | * including Cypress's product in a High Risk Product, the manufacturer |
| 59 | * of such system or application assumes all risk of such use and in doing |
| 60 | * so agrees to indemnify Cypress against all liability. |
| 61 | * |
| 62 | ********************************************************************************/ |
| 63 | |
| 64 | #include "mcuboot_config/mcuboot_config.h" |
| 65 | |
| 66 | #ifdef MCUBOOT_SIGN_EC256 |
| 67 | |
| 68 | #include "bootutil/sign_key.h" |
| 69 | |
| 70 | #ifdef MCUBOOT_USE_MBED_TLS |
| 71 | |
| 72 | #include "mbedtls/oid.h" |
| 73 | #include "mbedtls/asn1.h" |
| 74 | #include "mbedtls/ecdsa.h" |
| 75 | |
| 76 | #include "bootutil_priv.h" |
| 77 | |
| 78 | #include <string.h> |
| 79 | |
| 80 | /* |
| 81 | * Declaring these like this adds NULL termination. |
| 82 | */ |
| 83 | static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 84 | static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1; |
| 85 | |
| 86 | /* |
| 87 | * Parse the public key used for signing. |
| 88 | */ |
| 89 | static int |
| 90 | bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end) |
| 91 | { |
| 92 | size_t len; |
| 93 | mbedtls_asn1_buf alg; |
| 94 | mbedtls_asn1_buf param; |
| 95 | |
| 96 | if (mbedtls_asn1_get_tag(p, end, &len, |
| 97 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 98 | return -1; |
| 99 | } |
| 100 | end = *p + len; |
| 101 | |
| 102 | if (mbedtls_asn1_get_alg(p, end, &alg, ¶m)) { |
| 103 | return -2; |
| 104 | } |
| 105 | if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| 106 | memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| 107 | return -3; |
| 108 | } |
| 109 | if (param.len != sizeof(ec_secp256r1_oid) - 1|| |
| 110 | memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) { |
| 111 | return -4; |
| 112 | } |
| 113 | |
| 114 | if (mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1)) { |
| 115 | return -5; |
| 116 | } |
| 117 | |
| 118 | if (mbedtls_asn1_get_bitstring_null(p, end, &len)) { |
| 119 | return -6; |
| 120 | } |
| 121 | if (*p + len != end) { |
| 122 | return -7; |
| 123 | } |
| 124 | |
| 125 | if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) { |
| 126 | return -8; |
| 127 | } |
| 128 | |
| 129 | if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) { |
| 130 | return -9; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int |
| 136 | bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen, uint8_t key_id) |
| 137 | { |
| 138 | int rc; |
| 139 | uint8_t *cp; |
| 140 | uint8_t *end; |
| 141 | mbedtls_ecdsa_context ctx; |
| 142 | |
| 143 | mbedtls_ecdsa_init(&ctx); |
| 144 | |
| 145 | cp = (uint8_t *)bootutil_keys[key_id].key; |
| 146 | end = cp + *bootutil_keys[key_id].len; |
| 147 | |
| 148 | rc = bootutil_parse_eckey(&ctx, &cp, end); |
| 149 | if (rc) { |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | while (sig[slen - 1] == '\0') { |
| 154 | slen--; |
| 155 | } |
| 156 | |
| 157 | rc = mbedtls_ecdsa_read_signature(&ctx, hash, hlen, sig, slen); |
| 158 | |
| 159 | mbedtls_ecdsa_free(&ctx); |
| 160 | |
| 161 | return rc; |
| 162 | } |
| 163 | |
| 164 | #endif /* MCUBOOT_USE_MBED_TLS */ |
| 165 | |
| 166 | #endif /* MCUBOOT_SIGN_EC256 */ |