| 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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, | 
|  | 220 | attributes->core.bits, | 
|  | 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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type) == | 
|  | 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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | status = mbedtls_psa_ecp_export_key(attributes->core.type, | 
|  | 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 { | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 284 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 84b9f1b | 2024-02-19 16:44:29 +0100 | [diff] [blame] | 285 | mbedtls_ecp_write_key_ext(ecp, data_length, data, data_size)); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | return status; | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 287 | } | 
|  | 288 | } | 
|  | 289 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 290 | psa_status_t mbedtls_psa_ecp_export_public_key( | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 291 | const psa_key_attributes_t *attributes, | 
|  | 292 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | uint8_t *data, size_t data_size, size_t *data_length) | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 294 | { | 
|  | 295 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 296 | mbedtls_ecp_keypair *ecp = NULL; | 
|  | 297 |  | 
|  | 298 | status = mbedtls_psa_ecp_load_representation( | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 299 | attributes->core.type, attributes->core.bits, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | key_buffer, key_buffer_size, &ecp); | 
|  | 301 | if (status != PSA_SUCCESS) { | 
|  | 302 | return status; | 
|  | 303 | } | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 304 |  | 
|  | 305 | status = mbedtls_psa_ecp_export_key( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( | 
|  | 307 | PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type)), | 
|  | 308 | ecp, data, data_size, data_length); | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 309 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | mbedtls_ecp_keypair_free(ecp); | 
|  | 311 | mbedtls_free(ecp); | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 312 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | return status; | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 314 | } | 
| Valerio Setti | 27c501a | 2023-06-27 16:58:52 +0200 | [diff] [blame] | 315 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || | 
|  | 316 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 317 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ | 
| Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 318 |  | 
| Valerio Setti | 4c0174d | 2023-06-26 10:05:50 +0200 | [diff] [blame] | 319 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 320 | psa_status_t mbedtls_psa_ecp_generate_key( | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 321 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | 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] | 323 | { | 
|  | 324 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 325 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 326 |  | 
|  | 327 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | attributes->core.type); | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 329 | mbedtls_ecp_group_id grp_id = | 
| Valerio Setti | d36c313 | 2023-12-21 14:03:51 +0100 | [diff] [blame] | 330 | mbedtls_ecc_group_from_psa(curve, attributes->core.bits); | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 331 |  | 
|  | 332 | const mbedtls_ecp_curve_info *curve_info = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | mbedtls_ecp_curve_info_from_grp_id(grp_id); | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 334 | mbedtls_ecp_keypair ecp; | 
|  | 335 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | if (grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL) { | 
|  | 337 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 338 | } | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 339 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | mbedtls_ecp_keypair_init(&ecp); | 
|  | 341 | ret = mbedtls_ecp_gen_key(grp_id, &ecp, | 
|  | 342 | mbedtls_psa_get_random, | 
|  | 343 | MBEDTLS_PSA_RANDOM_STATE); | 
|  | 344 | if (ret != 0) { | 
|  | 345 | mbedtls_ecp_keypair_free(&ecp); | 
|  | 346 | return mbedtls_to_psa_error(ret); | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
|  | 349 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 84b9f1b | 2024-02-19 16:44:29 +0100 | [diff] [blame] | 350 | mbedtls_ecp_write_key_ext(&ecp, key_buffer_length, | 
|  | 351 | key_buffer, key_buffer_size)); | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 352 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | mbedtls_ecp_keypair_free(&ecp); | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 354 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | return status; | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 356 | } | 
| Valerio Setti | 4c0174d | 2023-06-26 10:05:50 +0200 | [diff] [blame] | 357 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE */ | 
| Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 358 |  | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 359 | /****************************************************************/ | 
|  | 360 | /* ECDSA sign/verify */ | 
|  | 361 | /****************************************************************/ | 
|  | 362 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 363 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ | 
|  | 364 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) | 
|  | 365 | psa_status_t mbedtls_psa_ecdsa_sign_hash( | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 366 | const psa_key_attributes_t *attributes, | 
|  | 367 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 368 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | uint8_t *signature, size_t signature_size, size_t *signature_length) | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 370 | { | 
|  | 371 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 372 | mbedtls_ecp_keypair *ecp = NULL; | 
|  | 373 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 374 | size_t curve_bytes; | 
|  | 375 | mbedtls_mpi r, s; | 
|  | 376 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, | 
|  | 378 | attributes->core.bits, | 
|  | 379 | key_buffer, | 
|  | 380 | key_buffer_size, | 
|  | 381 | &ecp); | 
|  | 382 | if (status != PSA_SUCCESS) { | 
|  | 383 | return status; | 
|  | 384 | } | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 385 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); | 
|  | 387 | mbedtls_mpi_init(&r); | 
|  | 388 | mbedtls_mpi_init(&s); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 389 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | if (signature_size < 2 * curve_bytes) { | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 391 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; | 
|  | 392 | goto cleanup; | 
|  | 393 | } | 
|  | 394 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) { | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 396 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | 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] | 398 | 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] | 399 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext( | 
|  | 400 | &ecp->grp, &r, &s, | 
|  | 401 | &ecp->d, hash, | 
|  | 402 | hash_length, md_alg, | 
|  | 403 | mbedtls_psa_get_random, | 
|  | 404 | MBEDTLS_PSA_RANDOM_STATE)); | 
| Ronald Cron | 9103d49 | 2021-03-04 11:26:03 +0100 | [diff] [blame] | 405 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; | 
|  | 407 | goto cleanup; | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 408 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | } else { | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 410 | (void) alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d, | 
|  | 412 | hash, hash_length, | 
|  | 413 | mbedtls_psa_get_random, | 
|  | 414 | MBEDTLS_PSA_RANDOM_STATE)); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r, | 
|  | 418 | signature, | 
|  | 419 | curve_bytes)); | 
|  | 420 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s, | 
|  | 421 | signature + curve_bytes, | 
|  | 422 | curve_bytes)); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 423 | cleanup: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | mbedtls_mpi_free(&r); | 
|  | 425 | mbedtls_mpi_free(&s); | 
|  | 426 | if (ret == 0) { | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 427 | *signature_length = 2 * curve_bytes; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | } | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 429 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | mbedtls_ecp_keypair_free(ecp); | 
|  | 431 | mbedtls_free(ecp); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 432 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | return mbedtls_to_psa_error(ret); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 436 | 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] | 437 | { | 
|  | 438 | int ret = 0; | 
|  | 439 |  | 
|  | 440 | /* Check whether the public part is loaded. If not, load it. */ | 
|  | 441 | if (mbedtls_ecp_is_zero(&ecp->Q)) { | 
|  | 442 | ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q, | 
|  | 443 | &ecp->d, &ecp->grp.G, | 
|  | 444 | mbedtls_psa_get_random, | 
|  | 445 | MBEDTLS_PSA_RANDOM_STATE); | 
|  | 446 | } | 
|  | 447 |  | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 448 | return mbedtls_to_psa_error(ret); | 
| Paul Elliott | eefe472 | 2023-02-06 15:59:09 +0000 | [diff] [blame] | 449 | } | 
|  | 450 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 451 | psa_status_t mbedtls_psa_ecdsa_verify_hash( | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 452 | const psa_key_attributes_t *attributes, | 
|  | 453 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 454 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | const uint8_t *signature, size_t signature_length) | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 456 | { | 
|  | 457 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 458 | mbedtls_ecp_keypair *ecp = NULL; | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 459 | size_t curve_bytes; | 
|  | 460 | mbedtls_mpi r, s; | 
|  | 461 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | (void) alg; | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 463 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, | 
|  | 465 | attributes->core.bits, | 
|  | 466 | key_buffer, | 
|  | 467 | key_buffer_size, | 
|  | 468 | &ecp); | 
|  | 469 | if (status != PSA_SUCCESS) { | 
|  | 470 | return status; | 
|  | 471 | } | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 472 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); | 
|  | 474 | mbedtls_mpi_init(&r); | 
|  | 475 | mbedtls_mpi_init(&s); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 476 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | if (signature_length != 2 * curve_bytes) { | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 478 | status = PSA_ERROR_INVALID_SIGNATURE; | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 479 | goto cleanup; | 
|  | 480 | } | 
|  | 481 |  | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 482 | status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r, | 
|  | 483 | signature, | 
|  | 484 | curve_bytes)); | 
|  | 485 | if (status != PSA_SUCCESS) { | 
|  | 486 | goto cleanup; | 
|  | 487 | } | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 488 |  | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 489 | status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s, | 
|  | 490 | signature + curve_bytes, | 
|  | 491 | curve_bytes)); | 
|  | 492 | if (status != PSA_SUCCESS) { | 
|  | 493 | goto cleanup; | 
|  | 494 | } | 
| Paul Elliott | eefe472 | 2023-02-06 15:59:09 +0000 | [diff] [blame] | 495 |  | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 496 | status = mbedtls_psa_ecp_load_public_part(ecp); | 
|  | 497 | if (status != PSA_SUCCESS) { | 
|  | 498 | goto cleanup; | 
|  | 499 | } | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 500 |  | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 501 | status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash, | 
|  | 502 | hash_length, &ecp->Q, | 
|  | 503 | &r, &s)); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 504 | cleanup: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | mbedtls_mpi_free(&r); | 
|  | 506 | mbedtls_mpi_free(&s); | 
|  | 507 | mbedtls_ecp_keypair_free(ecp); | 
|  | 508 | mbedtls_free(ecp); | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 509 |  | 
| Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 510 | return status; | 
| Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 511 | } | 
|  | 512 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 513 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 514 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ | 
|  | 515 |  | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 516 | /****************************************************************/ | 
|  | 517 | /* ECDH Key Agreement */ | 
|  | 518 | /****************************************************************/ | 
| Aditya Deshpande | 5567c66 | 2022-11-07 10:43:29 +0000 | [diff] [blame] | 519 |  | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 520 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) | 
| Aditya Deshpande | 5567c66 | 2022-11-07 10:43:29 +0000 | [diff] [blame] | 521 | psa_status_t mbedtls_psa_key_agreement_ecdh( | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 522 | const psa_key_attributes_t *attributes, | 
|  | 523 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 524 | psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length, | 
|  | 525 | uint8_t *shared_secret, size_t shared_secret_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 526 | size_t *shared_secret_length) | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 527 | { | 
| Aditya Deshpande | b6bc752 | 2022-11-29 16:53:29 +0000 | [diff] [blame] | 528 | psa_status_t status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->core.type) || | 
|  | 530 | !PSA_ALG_IS_ECDH(alg)) { | 
|  | 531 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 532 | } | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 533 | mbedtls_ecp_keypair *ecp = NULL; | 
| Aditya Deshpande | b6bc752 | 2022-11-29 16:53:29 +0000 | [diff] [blame] | 534 | status = mbedtls_psa_ecp_load_representation( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | attributes->core.type, | 
|  | 536 | attributes->core.bits, | 
|  | 537 | key_buffer, | 
|  | 538 | key_buffer_size, | 
|  | 539 | &ecp); | 
|  | 540 | if (status != PSA_SUCCESS) { | 
|  | 541 | return status; | 
|  | 542 | } | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 543 | mbedtls_ecp_keypair *their_key = NULL; | 
|  | 544 | mbedtls_ecdh_context ecdh; | 
|  | 545 | size_t bits = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ecp->grp.id, &bits); | 
|  | 547 | mbedtls_ecdh_init(&ecdh); | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 548 |  | 
|  | 549 | status = mbedtls_psa_ecp_load_representation( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), | 
|  | 551 | bits, | 
|  | 552 | peer_key, | 
|  | 553 | peer_key_length, | 
|  | 554 | &their_key); | 
|  | 555 | if (status != PSA_SUCCESS) { | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 556 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | } | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 558 |  | 
|  | 559 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS)); | 
|  | 561 | if (status != PSA_SUCCESS) { | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 562 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | } | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 564 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | mbedtls_ecdh_get_params(&ecdh, ecp, MBEDTLS_ECDH_OURS)); | 
|  | 566 | if (status != PSA_SUCCESS) { | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 567 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | } | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 569 |  | 
|  | 570 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | mbedtls_ecdh_calc_secret(&ecdh, | 
|  | 572 | shared_secret_length, | 
|  | 573 | shared_secret, shared_secret_size, | 
|  | 574 | mbedtls_psa_get_random, | 
|  | 575 | MBEDTLS_PSA_RANDOM_STATE)); | 
|  | 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 | } | 
|  | 579 | if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) { | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 580 | status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | } | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 582 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | if (status != PSA_SUCCESS) { | 
|  | 584 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); | 
|  | 585 | } | 
|  | 586 | mbedtls_ecdh_free(&ecdh); | 
|  | 587 | mbedtls_ecp_keypair_free(their_key); | 
|  | 588 | mbedtls_free(their_key); | 
|  | 589 | mbedtls_ecp_keypair_free(ecp); | 
|  | 590 | mbedtls_free(ecp); | 
|  | 591 | return status; | 
| Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 592 | } | 
|  | 593 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ | 
|  | 594 |  | 
|  | 595 |  | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 596 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |