Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [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 | |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame^] | 22 | #ifdef APP_mynewt |
| 23 | #include "mynewt/config.h" |
| 24 | #endif |
| 25 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 26 | #ifdef MCUBOOT_SIGN_EC256 |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 27 | #include "bootutil/sign_key.h" |
| 28 | |
| 29 | #include "mbedtls/oid.h" |
| 30 | #include "mbedtls/asn1.h" |
| 31 | |
| 32 | #include "tinycrypt/ecc_dsa.h" |
| 33 | #include "bootutil_priv.h" |
| 34 | |
| 35 | /* |
| 36 | * Declaring these like this adds NULL termination. |
| 37 | */ |
| 38 | static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 39 | static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1; |
| 40 | |
| 41 | /* |
| 42 | * Parse the public key used for signing. |
| 43 | */ |
| 44 | static int |
| 45 | tinycrypt_import_key(EccPoint *pubkey, uint8_t *cp, uint8_t *end) |
| 46 | { |
| 47 | size_t len; |
| 48 | mbedtls_asn1_buf alg; |
| 49 | mbedtls_asn1_buf param; |
| 50 | |
| 51 | if (mbedtls_asn1_get_tag(&cp, end, &len, |
| 52 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 53 | return -1; |
| 54 | } |
| 55 | end = cp + len; |
| 56 | |
| 57 | if (mbedtls_asn1_get_alg(&cp, end, &alg, ¶m)) { |
| 58 | return -2; |
| 59 | } |
| 60 | if (alg.len != sizeof(ec_pubkey_oid) - 1 || |
| 61 | memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) { |
| 62 | return -3; |
| 63 | } |
| 64 | if (param.len != sizeof(ec_secp256r1_oid) - 1 || |
| 65 | memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) { |
| 66 | return -4; |
| 67 | } |
| 68 | if (mbedtls_asn1_get_bitstring_null(&cp, end, &len)) { |
| 69 | return -6; |
| 70 | } |
| 71 | if (cp + len != end) { |
| 72 | return -7; |
| 73 | } |
| 74 | |
| 75 | if (len != 2 * NUM_ECC_BYTES + 1) { |
| 76 | return -8; |
| 77 | } |
| 78 | if (cp[0] != 0x04) { |
| 79 | return -9; |
| 80 | } |
| 81 | |
| 82 | ecc_bytes2native(pubkey->x, cp + 1); |
| 83 | ecc_bytes2native(pubkey->y, cp + 1 + NUM_ECC_BYTES); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * cp points to ASN1 string containing an integer. |
| 90 | * Verify the tag, and that the length is 32 bytes. |
| 91 | */ |
| 92 | static int |
| 93 | tinycrypt_read_bigint(uint32_t i[NUM_ECC_DIGITS], uint8_t **cp, uint8_t *end) |
| 94 | { |
| 95 | size_t len; |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 96 | uint8_t bigint[NUM_ECC_BYTES]; |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 97 | |
| 98 | if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) { |
| 99 | return -3; |
| 100 | } |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 101 | if (len > NUM_ECC_BYTES) { |
| 102 | memcpy(bigint, *cp + len - NUM_ECC_BYTES, NUM_ECC_BYTES); |
| 103 | } else { |
| 104 | memset(bigint, 0, NUM_ECC_BYTES - len); |
| 105 | memcpy(bigint + NUM_ECC_BYTES - len, *cp, len); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 106 | } |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 107 | *cp += len; |
Marko Kiiskila | 755daed | 2016-12-30 18:53:13 -0800 | [diff] [blame] | 108 | ecc_bytes2native(i, bigint); |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Read in signature. Signature has r and s encoded as integers. |
| 114 | */ |
| 115 | static int |
| 116 | tinycrypt_decode_sig(uint32_t r[NUM_ECC_DIGITS], uint32_t s[NUM_ECC_DIGITS], |
| 117 | uint8_t *cp, uint8_t *end) |
| 118 | { |
| 119 | int rc; |
| 120 | size_t len; |
| 121 | |
| 122 | rc = mbedtls_asn1_get_tag(&cp, end, &len, |
| 123 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
| 124 | if (rc) { |
| 125 | return -1; |
| 126 | } |
David Brown | baff96f | 2017-01-27 17:35:14 -0700 | [diff] [blame] | 127 | if (cp + len > end) { |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 128 | return -2; |
| 129 | } |
| 130 | rc = tinycrypt_read_bigint(r, &cp, end); |
| 131 | if (rc) { |
| 132 | return -3; |
| 133 | } |
| 134 | rc = tinycrypt_read_bigint(s, &cp, end); |
| 135 | if (rc) { |
| 136 | return -4; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int |
| 142 | bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen, |
| 143 | uint8_t key_id) |
| 144 | { |
| 145 | int rc; |
| 146 | uint8_t *cp; |
| 147 | uint8_t *end; |
| 148 | EccPoint ctx; |
| 149 | uint32_t r[NUM_ECC_DIGITS]; |
| 150 | uint32_t s[NUM_ECC_DIGITS]; |
| 151 | uint32_t hash_t[NUM_ECC_DIGITS]; |
| 152 | |
| 153 | cp = (uint8_t *)bootutil_keys[key_id].key; |
| 154 | end = cp + *bootutil_keys[key_id].len; |
| 155 | |
| 156 | rc = tinycrypt_import_key(&ctx, cp, end); |
| 157 | if (rc) { |
| 158 | return -1; |
| 159 | } |
| 160 | |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 161 | rc = tinycrypt_decode_sig(r, s, sig, sig + slen); |
| 162 | if (rc) { |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * This is simplified, as the hash length is also 32 bytes. |
| 168 | */ |
| 169 | if (hlen != NUM_ECC_BYTES) { |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | ecc_bytes2native(hash_t, hash); |
| 174 | rc = ecdsa_verify(&ctx, hash_t, r, s); |
| 175 | if (rc == 1) { |
| 176 | return 0; |
| 177 | } else { |
| 178 | return -2; |
| 179 | } |
| 180 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 181 | #endif /* MCUBOOT_SIGN_EC256 */ |