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