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