Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2016-2018 JUUL Labs |
| 5 | * |
| 6 | * Original license: |
| 7 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 8 | * 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 Kiiskila | bd14468 | 2016-12-30 18:54:18 -0800 | [diff] [blame] | 26 | #include <string.h> |
| 27 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 28 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 29 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 30 | #ifdef MCUBOOT_SIGN_EC |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 31 | #include "bootutil/sign_key.h" |
| 32 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 33 | #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 | */ |
| 42 | static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 43 | static const uint8_t ec_secp224r1_oid[] = MBEDTLS_OID_EC_GRP_SECP224R1; |
| 44 | |
| 45 | /* |
Marko Kiiskila | 351a0b2 | 2016-12-29 17:31:00 -0800 | [diff] [blame] | 46 | * Parse the public key used for signing. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 47 | */ |
| 48 | static int |
| 49 | bootutil_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, ¶m)) { |
| 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 Utzig | 891f4fb | 2018-01-10 08:37:11 -0200 | [diff] [blame] | 73 | if (mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP224R1)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 74 | 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 | |
| 94 | static int |
| 95 | bootutil_cmp_sig(mbedtls_ecdsa_context *ctx, uint8_t *hash, uint32_t hlen, |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 96 | uint8_t *sig, size_t slen) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 97 | { |
| 98 | return mbedtls_ecdsa_read_signature(ctx, hash, hlen, sig, slen); |
| 99 | } |
| 100 | |
| 101 | int |
Fabio Utzig | 1a927dd | 2017-12-05 10:30:26 -0200 | [diff] [blame] | 102 | bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 103 | 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 Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 128 | #endif /* MCUBOOT_SIGN_EC */ |