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