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