Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA ECP layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa/crypto.h> |
| 26 | #include "psa_crypto_core.h" |
| 27 | #include "psa_crypto_ecp.h" |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 28 | #include "psa_crypto_random_impl.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 29 | #include "psa_crypto_hash.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 30 | |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include "mbedtls/platform.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 34 | |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 35 | #include <mbedtls/ecdsa.h> |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 36 | #include <mbedtls/ecp.h> |
| 37 | #include <mbedtls/error.h> |
| 38 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 39 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 40 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 41 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 42 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 43 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 44 | psa_status_t mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 45 | psa_key_type_t type, size_t curve_bits, |
| 46 | const uint8_t *data, size_t data_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | mbedtls_ecp_keypair **p_ecp) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 48 | { |
| 49 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 50 | psa_status_t status; |
| 51 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 52 | size_t curve_bytes = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 53 | int explicit_bits = (curve_bits != 0); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 54 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 55 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) && |
| 56 | PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 57 | /* A Weierstrass public key is represented as: |
| 58 | * - The byte 0x04; |
| 59 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 60 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 61 | * So its data length is 2m+1 where m is the curve size in bits. |
| 62 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 63 | if ((data_length & 1) == 0) { |
| 64 | return PSA_ERROR_INVALID_ARGUMENT; |
| 65 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 66 | curve_bytes = data_length / 2; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 67 | |
| 68 | /* Montgomery public keys are represented in compressed format, meaning |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 69 | * their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 70 | |
| 71 | /* Private keys are represented in uncompressed private random integer |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 72 | * format, meaning their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 75 | if (explicit_bits) { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 76 | /* With an explicit bit-size, the data must have the matching length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 77 | if (curve_bytes != PSA_BITS_TO_BYTES(curve_bits)) { |
| 78 | return PSA_ERROR_INVALID_ARGUMENT; |
| 79 | } |
| 80 | } else { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 81 | /* We need to infer the bit-size from the data. Since the only |
| 82 | * information we have is the length in bytes, the value of curve_bits |
| 83 | * at this stage is rounded up to the nearest multiple of 8. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | curve_bits = PSA_BYTES_TO_BITS(curve_bytes); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 87 | /* Allocate and initialize a key representation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 88 | ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
| 89 | if (ecp == NULL) { |
| 90 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 91 | } |
| 92 | mbedtls_ecp_keypair_init(ecp); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 93 | |
| 94 | /* Load the group. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | grp_id = mbedtls_ecc_group_of_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(type), |
| 96 | curve_bits, !explicit_bits); |
| 97 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 98 | /* We can't distinguish between a nonsensical family/size combination |
| 99 | * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a |
| 100 | * well-regarded curve that Mbed TLS just doesn't know about (which |
| 101 | * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how |
| 102 | * curves that Mbed TLS knows about but for which support is disabled |
| 103 | * at build time, return NOT_SUPPORTED. */ |
| 104 | status = PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 105 | goto exit; |
| 106 | } |
| 107 | |
| 108 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | mbedtls_ecp_group_load(&ecp->grp, grp_id)); |
| 110 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 111 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 113 | |
| 114 | /* Load the key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 116 | /* Load the public value. */ |
| 117 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 118 | mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q, |
| 119 | data, |
| 120 | data_length)); |
| 121 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 122 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 123 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 124 | |
| 125 | /* Check that the point is on the curve. */ |
| 126 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | mbedtls_ecp_check_pubkey(&ecp->grp, &ecp->Q)); |
| 128 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 129 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 130 | } |
| 131 | } else { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 132 | /* Load and validate the secret value. */ |
| 133 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 134 | mbedtls_ecp_read_key(ecp->grp.id, |
| 135 | ecp, |
| 136 | data, |
| 137 | data_length)); |
| 138 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 139 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | *p_ecp = ecp; |
| 144 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | if (status != PSA_SUCCESS) { |
| 146 | mbedtls_ecp_keypair_free(ecp); |
| 147 | mbedtls_free(ecp); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 150 | return status; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 151 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 152 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 153 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 154 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 155 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 156 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 157 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 158 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 159 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 160 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 161 | psa_status_t mbedtls_psa_ecp_import_key( |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 162 | const psa_key_attributes_t *attributes, |
| 163 | const uint8_t *data, size_t data_length, |
| 164 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 165 | size_t *key_buffer_length, size_t *bits) |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 166 | { |
| 167 | psa_status_t status; |
| 168 | mbedtls_ecp_keypair *ecp = NULL; |
| 169 | |
| 170 | /* Parse input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 171 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 172 | attributes->core.bits, |
| 173 | data, |
| 174 | data_length, |
| 175 | &ecp); |
| 176 | if (status != PSA_SUCCESS) { |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 177 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 178 | } |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type) == |
| 181 | PSA_ECC_FAMILY_MONTGOMERY) { |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 182 | *bits = ecp->grp.nbits + 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | } else { |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 184 | *bits = ecp->grp.nbits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | } |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 186 | |
| 187 | /* Re-export the data to PSA export format. There is currently no support |
| 188 | * for other input formats then the export format, so this is a 1-1 |
| 189 | * copy operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | status = mbedtls_psa_ecp_export_key(attributes->core.type, |
| 191 | ecp, |
| 192 | key_buffer, |
| 193 | key_buffer_size, |
| 194 | key_buffer_length); |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 195 | exit: |
| 196 | /* Always free the PK object (will also free contained ECP context) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 197 | mbedtls_ecp_keypair_free(ecp); |
| 198 | mbedtls_free(ecp); |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | return status; |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type, |
| 204 | mbedtls_ecp_keypair *ecp, |
| 205 | uint8_t *data, |
| 206 | size_t data_size, |
| 207 | size_t *data_length) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 208 | { |
| 209 | psa_status_t status; |
| 210 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 211 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 212 | /* Check whether the public part is loaded */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | if (mbedtls_ecp_is_zero(&ecp->Q)) { |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 214 | /* Calculate the public key */ |
| 215 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 217 | mbedtls_psa_get_random, |
| 218 | MBEDTLS_PSA_RANDOM_STATE)); |
| 219 | if (status != PSA_SUCCESS) { |
| 220 | return status; |
| 221 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | mbedtls_ecp_point_write_binary(&ecp->grp, &ecp->Q, |
| 226 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 227 | data_length, |
| 228 | data, |
| 229 | data_size)); |
| 230 | if (status != PSA_SUCCESS) { |
| 231 | memset(data, 0, data_size); |
| 232 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | return status; |
| 235 | } else { |
| 236 | if (data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits)) { |
| 237 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 238 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 239 | |
| 240 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | mbedtls_ecp_write_key(ecp, |
| 242 | data, |
| 243 | PSA_BITS_TO_BYTES(ecp->grp.nbits))); |
| 244 | if (status == PSA_SUCCESS) { |
| 245 | *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits); |
| 246 | } else { |
| 247 | memset(data, 0, data_size); |
| 248 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 250 | return status; |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 254 | psa_status_t mbedtls_psa_ecp_export_public_key( |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 255 | const psa_key_attributes_t *attributes, |
| 256 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | uint8_t *data, size_t data_size, size_t *data_length) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 258 | { |
| 259 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 260 | mbedtls_ecp_keypair *ecp = NULL; |
| 261 | |
| 262 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 263 | attributes->core.type, attributes->core.bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | key_buffer, key_buffer_size, &ecp); |
| 265 | if (status != PSA_SUCCESS) { |
| 266 | return status; |
| 267 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 268 | |
| 269 | status = mbedtls_psa_ecp_export_key( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 271 | PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type)), |
| 272 | ecp, data, data_size, data_length); |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 273 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 274 | mbedtls_ecp_keypair_free(ecp); |
| 275 | mbedtls_free(ecp); |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | return status; |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 278 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 279 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 280 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 281 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 282 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
| 283 | psa_status_t mbedtls_psa_ecp_generate_key( |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 284 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 285 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 286 | { |
| 287 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 288 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 289 | |
| 290 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | attributes->core.type); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 292 | mbedtls_ecp_group_id grp_id = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | mbedtls_ecc_group_of_psa(curve, attributes->core.bits, 0); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 294 | |
| 295 | const mbedtls_ecp_curve_info *curve_info = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 296 | mbedtls_ecp_curve_info_from_grp_id(grp_id); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 297 | mbedtls_ecp_keypair ecp; |
| 298 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | if (attributes->domain_parameters_size != 0) { |
| 300 | return PSA_ERROR_NOT_SUPPORTED; |
| 301 | } |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 302 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | if (grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL) { |
| 304 | return PSA_ERROR_NOT_SUPPORTED; |
| 305 | } |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 307 | mbedtls_ecp_keypair_init(&ecp); |
| 308 | ret = mbedtls_ecp_gen_key(grp_id, &ecp, |
| 309 | mbedtls_psa_get_random, |
| 310 | MBEDTLS_PSA_RANDOM_STATE); |
| 311 | if (ret != 0) { |
| 312 | mbedtls_ecp_keypair_free(&ecp); |
| 313 | return mbedtls_to_psa_error(ret); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 317 | mbedtls_ecp_write_key(&ecp, key_buffer, key_buffer_size)); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 318 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 319 | mbedtls_ecp_keypair_free(&ecp); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | if (status == PSA_SUCCESS) { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 322 | *key_buffer_length = key_buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 323 | } |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | return status; |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 326 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 327 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 328 | |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 329 | /****************************************************************/ |
| 330 | /* ECDSA sign/verify */ |
| 331 | /****************************************************************/ |
| 332 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 333 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 334 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 335 | psa_status_t mbedtls_psa_ecdsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 336 | const psa_key_attributes_t *attributes, |
| 337 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 338 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 339 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 340 | { |
| 341 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 342 | mbedtls_ecp_keypair *ecp = NULL; |
| 343 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 344 | size_t curve_bytes; |
| 345 | mbedtls_mpi r, s; |
| 346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 347 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 348 | attributes->core.bits, |
| 349 | key_buffer, |
| 350 | key_buffer_size, |
| 351 | &ecp); |
| 352 | if (status != PSA_SUCCESS) { |
| 353 | return status; |
| 354 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 355 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 356 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); |
| 357 | mbedtls_mpi_init(&r); |
| 358 | mbedtls_mpi_init(&s); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 359 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | if (signature_size < 2 * curve_bytes) { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 361 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 362 | goto cleanup; |
| 363 | } |
| 364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 366 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 367 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
| 368 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg); |
| 369 | mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info); |
| 370 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext( |
| 371 | &ecp->grp, &r, &s, |
| 372 | &ecp->d, hash, |
| 373 | hash_length, md_alg, |
| 374 | mbedtls_psa_get_random, |
| 375 | MBEDTLS_PSA_RANDOM_STATE)); |
Ronald Cron | 9103d49 | 2021-03-04 11:26:03 +0100 | [diff] [blame] | 376 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 377 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 378 | goto cleanup; |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 379 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | } else { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 381 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 382 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d, |
| 383 | hash, hash_length, |
| 384 | mbedtls_psa_get_random, |
| 385 | MBEDTLS_PSA_RANDOM_STATE)); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 388 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r, |
| 389 | signature, |
| 390 | curve_bytes)); |
| 391 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s, |
| 392 | signature + curve_bytes, |
| 393 | curve_bytes)); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 394 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | mbedtls_mpi_free(&r); |
| 396 | mbedtls_mpi_free(&s); |
| 397 | if (ret == 0) { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 398 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 399 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 400 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 401 | mbedtls_ecp_keypair_free(ecp); |
| 402 | mbedtls_free(ecp); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 403 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | return mbedtls_to_psa_error(ret); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 407 | psa_status_t mbedtls_psa_ecdsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 408 | const psa_key_attributes_t *attributes, |
| 409 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 410 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | const uint8_t *signature, size_t signature_length) |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 412 | { |
| 413 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 414 | mbedtls_ecp_keypair *ecp = NULL; |
| 415 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 416 | size_t curve_bytes; |
| 417 | mbedtls_mpi r, s; |
| 418 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 419 | (void) alg; |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 420 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 421 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 422 | attributes->core.bits, |
| 423 | key_buffer, |
| 424 | key_buffer_size, |
| 425 | &ecp); |
| 426 | if (status != PSA_SUCCESS) { |
| 427 | return status; |
| 428 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 430 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); |
| 431 | mbedtls_mpi_init(&r); |
| 432 | mbedtls_mpi_init(&s); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 433 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 434 | if (signature_length != 2 * curve_bytes) { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 435 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
| 436 | goto cleanup; |
| 437 | } |
| 438 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 439 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&r, |
| 440 | signature, |
| 441 | curve_bytes)); |
| 442 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&s, |
| 443 | signature + curve_bytes, |
| 444 | curve_bytes)); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 445 | |
| 446 | /* Check whether the public part is loaded. If not, load it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 447 | if (mbedtls_ecp_is_zero(&ecp->Q)) { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 448 | MBEDTLS_MPI_CHK( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 449 | mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 450 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE)); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 453 | ret = mbedtls_ecdsa_verify(&ecp->grp, hash, hash_length, |
| 454 | &ecp->Q, &r, &s); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 455 | |
| 456 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 457 | mbedtls_mpi_free(&r); |
| 458 | mbedtls_mpi_free(&s); |
| 459 | mbedtls_ecp_keypair_free(ecp); |
| 460 | mbedtls_free(ecp); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 462 | return mbedtls_to_psa_error(ret); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 465 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 466 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 467 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 468 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |