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" |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 29 | #include "md_psa.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> |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 36 | #include <mbedtls/ecdh.h> |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 37 | #include <mbedtls/ecp.h> |
| 38 | #include <mbedtls/error.h> |
| 39 | |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) || \ |
| 41 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
| 42 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 43 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 44 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 45 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 46 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 47 | psa_status_t mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 48 | psa_key_type_t type, size_t curve_bits, |
| 49 | const uint8_t *data, size_t data_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | mbedtls_ecp_keypair **p_ecp) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 51 | { |
| 52 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 53 | psa_status_t status; |
| 54 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 55 | size_t curve_bytes = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | int explicit_bits = (curve_bits != 0); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 57 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) && |
| 59 | PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 60 | /* A Weierstrass public key is represented as: |
| 61 | * - The byte 0x04; |
| 62 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 63 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 64 | * So its data length is 2m+1 where m is the curve size in bits. |
| 65 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | if ((data_length & 1) == 0) { |
| 67 | return PSA_ERROR_INVALID_ARGUMENT; |
| 68 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 69 | curve_bytes = data_length / 2; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 70 | |
| 71 | /* Montgomery public keys are represented in compressed format, meaning |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 72 | * their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 73 | |
| 74 | /* Private keys are represented in uncompressed private random integer |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 75 | * format, meaning their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | if (explicit_bits) { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 79 | /* With an explicit bit-size, the data must have the matching length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | if (curve_bytes != PSA_BITS_TO_BYTES(curve_bits)) { |
| 81 | return PSA_ERROR_INVALID_ARGUMENT; |
| 82 | } |
| 83 | } else { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 84 | /* We need to infer the bit-size from the data. Since the only |
| 85 | * information we have is the length in bytes, the value of curve_bits |
| 86 | * at this stage is rounded up to the nearest multiple of 8. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | curve_bits = PSA_BYTES_TO_BITS(curve_bytes); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 90 | /* Allocate and initialize a key representation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
| 92 | if (ecp == NULL) { |
| 93 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 94 | } |
| 95 | mbedtls_ecp_keypair_init(ecp); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 96 | |
| 97 | /* Load the group. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | grp_id = mbedtls_ecc_group_of_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(type), |
| 99 | curve_bits, !explicit_bits); |
| 100 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 101 | /* We can't distinguish between a nonsensical family/size combination |
| 102 | * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a |
| 103 | * well-regarded curve that Mbed TLS just doesn't know about (which |
| 104 | * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how |
| 105 | * curves that Mbed TLS knows about but for which support is disabled |
| 106 | * at build time, return NOT_SUPPORTED. */ |
| 107 | status = PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 108 | goto exit; |
| 109 | } |
| 110 | |
| 111 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | mbedtls_ecp_group_load(&ecp->grp, grp_id)); |
| 113 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 114 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 116 | |
| 117 | /* Load the key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 119 | /* Load the public value. */ |
| 120 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q, |
| 122 | data, |
| 123 | data_length)); |
| 124 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 125 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 127 | |
| 128 | /* Check that the point is on the curve. */ |
| 129 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | mbedtls_ecp_check_pubkey(&ecp->grp, &ecp->Q)); |
| 131 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 132 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | } |
| 134 | } else { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 135 | /* Load and validate the secret value. */ |
| 136 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | mbedtls_ecp_read_key(ecp->grp.id, |
| 138 | ecp, |
| 139 | data, |
| 140 | data_length)); |
| 141 | if (status != PSA_SUCCESS) { |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 142 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | *p_ecp = ecp; |
| 147 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | if (status != PSA_SUCCESS) { |
| 149 | mbedtls_ecp_keypair_free(ecp); |
| 150 | mbedtls_free(ecp); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | return status; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 154 | } |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 155 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) || |
| 156 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
| 157 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 158 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 159 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 160 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 161 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 162 | |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 163 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
| 164 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 165 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 166 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 167 | psa_status_t mbedtls_psa_ecp_import_key( |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 168 | const psa_key_attributes_t *attributes, |
| 169 | const uint8_t *data, size_t data_length, |
| 170 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | size_t *key_buffer_length, size_t *bits) |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 172 | { |
| 173 | psa_status_t status; |
| 174 | mbedtls_ecp_keypair *ecp = NULL; |
| 175 | |
| 176 | /* Parse input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 178 | attributes->core.bits, |
| 179 | data, |
| 180 | data_length, |
| 181 | &ecp); |
| 182 | if (status != PSA_SUCCESS) { |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 183 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | } |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type) == |
| 187 | PSA_ECC_FAMILY_MONTGOMERY) { |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 188 | *bits = ecp->grp.nbits + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | } else { |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 190 | *bits = ecp->grp.nbits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | } |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 192 | |
| 193 | /* Re-export the data to PSA export format. There is currently no support |
| 194 | * for other input formats then the export format, so this is a 1-1 |
| 195 | * copy operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | status = mbedtls_psa_ecp_export_key(attributes->core.type, |
| 197 | ecp, |
| 198 | key_buffer, |
| 199 | key_buffer_size, |
| 200 | key_buffer_length); |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 201 | exit: |
| 202 | /* Always free the PK object (will also free contained ECP context) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | mbedtls_ecp_keypair_free(ecp); |
| 204 | mbedtls_free(ecp); |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | return status; |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type, |
| 210 | mbedtls_ecp_keypair *ecp, |
| 211 | uint8_t *data, |
| 212 | size_t data_size, |
| 213 | size_t *data_length) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 214 | { |
| 215 | psa_status_t status; |
| 216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 218 | /* Check whether the public part is loaded */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | if (mbedtls_ecp_is_zero(&ecp->Q)) { |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 220 | /* Calculate the public key */ |
| 221 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 223 | mbedtls_psa_get_random, |
| 224 | MBEDTLS_PSA_RANDOM_STATE)); |
| 225 | if (status != PSA_SUCCESS) { |
| 226 | return status; |
| 227 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | mbedtls_ecp_point_write_binary(&ecp->grp, &ecp->Q, |
| 232 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 233 | data_length, |
| 234 | data, |
| 235 | data_size)); |
| 236 | if (status != PSA_SUCCESS) { |
| 237 | memset(data, 0, data_size); |
| 238 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | return status; |
| 241 | } else { |
| 242 | if (data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits)) { |
| 243 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 244 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 245 | |
| 246 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | mbedtls_ecp_write_key(ecp, |
| 248 | data, |
| 249 | PSA_BITS_TO_BYTES(ecp->grp.nbits))); |
| 250 | if (status == PSA_SUCCESS) { |
| 251 | *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits); |
| 252 | } else { |
| 253 | memset(data, 0, data_size); |
| 254 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | return status; |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 260 | psa_status_t mbedtls_psa_ecp_export_public_key( |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 261 | const psa_key_attributes_t *attributes, |
| 262 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | uint8_t *data, size_t data_size, size_t *data_length) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 264 | { |
| 265 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 266 | mbedtls_ecp_keypair *ecp = NULL; |
| 267 | |
| 268 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 269 | attributes->core.type, attributes->core.bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | key_buffer, key_buffer_size, &ecp); |
| 271 | if (status != PSA_SUCCESS) { |
| 272 | return status; |
| 273 | } |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 274 | |
| 275 | status = mbedtls_psa_ecp_export_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 277 | PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type)), |
| 278 | ecp, data, data_size, data_length); |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | mbedtls_ecp_keypair_free(ecp); |
| 281 | mbedtls_free(ecp); |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | return status; |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 284 | } |
Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 285 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
| 286 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 287 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 288 | |
Valerio Setti | 4c0174d | 2023-06-26 10:05:50 +0200 | [diff] [blame] | 289 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 290 | psa_status_t mbedtls_psa_ecp_generate_key( |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 291 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | 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] | 293 | { |
| 294 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 295 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 296 | |
| 297 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | attributes->core.type); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 299 | mbedtls_ecp_group_id grp_id = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | mbedtls_ecc_group_of_psa(curve, attributes->core.bits, 0); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 301 | |
| 302 | const mbedtls_ecp_curve_info *curve_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | mbedtls_ecp_curve_info_from_grp_id(grp_id); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 304 | mbedtls_ecp_keypair ecp; |
| 305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | if (attributes->domain_parameters_size != 0) { |
| 307 | return PSA_ERROR_NOT_SUPPORTED; |
| 308 | } |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | if (grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL) { |
| 311 | return PSA_ERROR_NOT_SUPPORTED; |
| 312 | } |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | mbedtls_ecp_keypair_init(&ecp); |
| 315 | ret = mbedtls_ecp_gen_key(grp_id, &ecp, |
| 316 | mbedtls_psa_get_random, |
| 317 | MBEDTLS_PSA_RANDOM_STATE); |
| 318 | if (ret != 0) { |
| 319 | mbedtls_ecp_keypair_free(&ecp); |
| 320 | return mbedtls_to_psa_error(ret); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | mbedtls_ecp_write_key(&ecp, key_buffer, key_buffer_size)); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | mbedtls_ecp_keypair_free(&ecp); |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | if (status == PSA_SUCCESS) { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 329 | *key_buffer_length = key_buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | } |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | return status; |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 333 | } |
Valerio Setti | 4c0174d | 2023-06-26 10:05:50 +0200 | [diff] [blame] | 334 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE */ |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 335 | |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 336 | /****************************************************************/ |
| 337 | /* ECDSA sign/verify */ |
| 338 | /****************************************************************/ |
| 339 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 340 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 341 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 342 | psa_status_t mbedtls_psa_ecdsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 343 | const psa_key_attributes_t *attributes, |
| 344 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 345 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 347 | { |
| 348 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 349 | mbedtls_ecp_keypair *ecp = NULL; |
| 350 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 351 | size_t curve_bytes; |
| 352 | mbedtls_mpi r, s; |
| 353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 355 | attributes->core.bits, |
| 356 | key_buffer, |
| 357 | key_buffer_size, |
| 358 | &ecp); |
| 359 | if (status != PSA_SUCCESS) { |
| 360 | return status; |
| 361 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); |
| 364 | mbedtls_mpi_init(&r); |
| 365 | mbedtls_mpi_init(&s); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | if (signature_size < 2 * curve_bytes) { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 368 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 369 | goto cleanup; |
| 370 | } |
| 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 373 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 375 | mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext( |
| 377 | &ecp->grp, &r, &s, |
| 378 | &ecp->d, hash, |
| 379 | hash_length, md_alg, |
| 380 | mbedtls_psa_get_random, |
| 381 | MBEDTLS_PSA_RANDOM_STATE)); |
Ronald Cron | 9103d49 | 2021-03-04 11:26:03 +0100 | [diff] [blame] | 382 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 384 | goto cleanup; |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 385 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | } else { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 387 | (void) alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d, |
| 389 | hash, hash_length, |
| 390 | mbedtls_psa_get_random, |
| 391 | MBEDTLS_PSA_RANDOM_STATE)); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r, |
| 395 | signature, |
| 396 | curve_bytes)); |
| 397 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s, |
| 398 | signature + curve_bytes, |
| 399 | curve_bytes)); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 400 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | mbedtls_mpi_free(&r); |
| 402 | mbedtls_mpi_free(&s); |
| 403 | if (ret == 0) { |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 404 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | mbedtls_ecp_keypair_free(ecp); |
| 408 | mbedtls_free(ecp); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | return mbedtls_to_psa_error(ret); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 413 | psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp) |
Paul Elliott | eefe472 | 2023-02-06 15:59:09 +0000 | [diff] [blame] | 414 | { |
| 415 | int ret = 0; |
| 416 | |
| 417 | /* Check whether the public part is loaded. If not, load it. */ |
| 418 | if (mbedtls_ecp_is_zero(&ecp->Q)) { |
| 419 | ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q, |
| 420 | &ecp->d, &ecp->grp.G, |
| 421 | mbedtls_psa_get_random, |
| 422 | MBEDTLS_PSA_RANDOM_STATE); |
| 423 | } |
| 424 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 425 | return mbedtls_to_psa_error(ret); |
Paul Elliott | eefe472 | 2023-02-06 15:59:09 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 428 | psa_status_t mbedtls_psa_ecdsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 429 | const psa_key_attributes_t *attributes, |
| 430 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 431 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | const uint8_t *signature, size_t signature_length) |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 433 | { |
| 434 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 435 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 436 | size_t curve_bytes; |
| 437 | mbedtls_mpi r, s; |
| 438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | (void) alg; |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 442 | attributes->core.bits, |
| 443 | key_buffer, |
| 444 | key_buffer_size, |
| 445 | &ecp); |
| 446 | if (status != PSA_SUCCESS) { |
| 447 | return status; |
| 448 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); |
| 451 | mbedtls_mpi_init(&r); |
| 452 | mbedtls_mpi_init(&s); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | if (signature_length != 2 * curve_bytes) { |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 455 | status = PSA_ERROR_INVALID_SIGNATURE; |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 456 | goto cleanup; |
| 457 | } |
| 458 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 459 | status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r, |
| 460 | signature, |
| 461 | curve_bytes)); |
| 462 | if (status != PSA_SUCCESS) { |
| 463 | goto cleanup; |
| 464 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 465 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 466 | status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s, |
| 467 | signature + curve_bytes, |
| 468 | curve_bytes)); |
| 469 | if (status != PSA_SUCCESS) { |
| 470 | goto cleanup; |
| 471 | } |
Paul Elliott | eefe472 | 2023-02-06 15:59:09 +0000 | [diff] [blame] | 472 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 473 | status = mbedtls_psa_ecp_load_public_part(ecp); |
| 474 | if (status != PSA_SUCCESS) { |
| 475 | goto cleanup; |
| 476 | } |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 477 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 478 | status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash, |
| 479 | hash_length, &ecp->Q, |
| 480 | &r, &s)); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 481 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | mbedtls_mpi_free(&r); |
| 483 | mbedtls_mpi_free(&s); |
| 484 | mbedtls_ecp_keypair_free(ecp); |
| 485 | mbedtls_free(ecp); |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 486 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 487 | return status; |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 490 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 491 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 492 | |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 493 | /****************************************************************/ |
| 494 | /* ECDH Key Agreement */ |
| 495 | /****************************************************************/ |
Aditya Deshpande | 5567c66 | 2022-11-07 10:43:29 +0000 | [diff] [blame] | 496 | |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 497 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Aditya Deshpande | 5567c66 | 2022-11-07 10:43:29 +0000 | [diff] [blame] | 498 | psa_status_t mbedtls_psa_key_agreement_ecdh( |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 499 | const psa_key_attributes_t *attributes, |
| 500 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 501 | psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length, |
| 502 | uint8_t *shared_secret, size_t shared_secret_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | size_t *shared_secret_length) |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 504 | { |
Aditya Deshpande | b6bc752 | 2022-11-29 16:53:29 +0000 | [diff] [blame] | 505 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->core.type) || |
| 507 | !PSA_ALG_IS_ECDH(alg)) { |
| 508 | return PSA_ERROR_INVALID_ARGUMENT; |
| 509 | } |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 510 | mbedtls_ecp_keypair *ecp = NULL; |
Aditya Deshpande | b6bc752 | 2022-11-29 16:53:29 +0000 | [diff] [blame] | 511 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | attributes->core.type, |
| 513 | attributes->core.bits, |
| 514 | key_buffer, |
| 515 | key_buffer_size, |
| 516 | &ecp); |
| 517 | if (status != PSA_SUCCESS) { |
| 518 | return status; |
| 519 | } |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 520 | mbedtls_ecp_keypair *their_key = NULL; |
| 521 | mbedtls_ecdh_context ecdh; |
| 522 | size_t bits = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ecp->grp.id, &bits); |
| 524 | mbedtls_ecdh_init(&ecdh); |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 525 | |
| 526 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 528 | bits, |
| 529 | peer_key, |
| 530 | peer_key_length, |
| 531 | &their_key); |
| 532 | if (status != PSA_SUCCESS) { |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 533 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | } |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 535 | |
| 536 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS)); |
| 538 | if (status != PSA_SUCCESS) { |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 539 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | } |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 541 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | mbedtls_ecdh_get_params(&ecdh, ecp, MBEDTLS_ECDH_OURS)); |
| 543 | if (status != PSA_SUCCESS) { |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 544 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | } |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 546 | |
| 547 | status = mbedtls_to_psa_error( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | mbedtls_ecdh_calc_secret(&ecdh, |
| 549 | shared_secret_length, |
| 550 | shared_secret, shared_secret_size, |
| 551 | mbedtls_psa_get_random, |
| 552 | MBEDTLS_PSA_RANDOM_STATE)); |
| 553 | if (status != PSA_SUCCESS) { |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 554 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | } |
| 556 | if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) { |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 557 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | } |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 559 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | if (status != PSA_SUCCESS) { |
| 561 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); |
| 562 | } |
| 563 | mbedtls_ecdh_free(&ecdh); |
| 564 | mbedtls_ecp_keypair_free(their_key); |
| 565 | mbedtls_free(their_key); |
| 566 | mbedtls_ecp_keypair_free(ecp); |
| 567 | mbedtls_free(ecp); |
| 568 | return status; |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 569 | } |
| 570 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
| 571 | |
| 572 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 573 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |