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> |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 36 | #include <mbedtls/ecp.h> |
| 37 | #include <mbedtls/error.h> |
| 38 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 39 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 40 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 41 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 42 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 43 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 44 | psa_status_t mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 45 | psa_key_type_t type, size_t curve_bits, |
| 46 | const uint8_t *data, size_t data_length, |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 47 | mbedtls_ecp_keypair **p_ecp ) |
| 48 | { |
| 49 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 50 | psa_status_t status; |
| 51 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 52 | size_t curve_bytes = data_length; |
| 53 | int explicit_bits = ( curve_bits != 0 ); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 54 | |
| 55 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 56 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
| 57 | { |
| 58 | /* A Weierstrass public key is represented as: |
| 59 | * - The byte 0x04; |
| 60 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 61 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 62 | * So its data length is 2m+1 where m is the curve size in bits. |
| 63 | */ |
| 64 | if( ( data_length & 1 ) == 0 ) |
| 65 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 66 | curve_bytes = data_length / 2; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 67 | |
| 68 | /* Montgomery public keys are represented in compressed format, meaning |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 69 | * their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 70 | |
| 71 | /* Private keys are represented in uncompressed private random integer |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 72 | * format, meaning their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 75 | if( explicit_bits ) |
| 76 | { |
| 77 | /* With an explicit bit-size, the data must have the matching length. */ |
| 78 | if( curve_bytes != PSA_BITS_TO_BYTES( curve_bits ) ) |
| 79 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | /* We need to infer the bit-size from the data. Since the only |
| 84 | * information we have is the length in bytes, the value of curve_bits |
| 85 | * at this stage is rounded up to the nearest multiple of 8. */ |
| 86 | curve_bits = PSA_BYTES_TO_BITS( curve_bytes ); |
| 87 | } |
| 88 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 89 | /* Allocate and initialize a key representation. */ |
| 90 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 91 | if( ecp == NULL ) |
| 92 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 93 | mbedtls_ecp_keypair_init( ecp ); |
| 94 | |
| 95 | /* Load the group. */ |
| 96 | 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] | 97 | curve_bits, !explicit_bits ); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 98 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 99 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 100 | /* We can't distinguish between a nonsensical family/size combination |
| 101 | * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a |
| 102 | * well-regarded curve that Mbed TLS just doesn't know about (which |
| 103 | * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how |
| 104 | * curves that Mbed TLS knows about but for which support is disabled |
| 105 | * at build time, return NOT_SUPPORTED. */ |
| 106 | status = PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 107 | goto exit; |
| 108 | } |
| 109 | |
| 110 | status = mbedtls_to_psa_error( |
| 111 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 112 | if( status != PSA_SUCCESS ) |
| 113 | goto exit; |
| 114 | |
| 115 | /* Load the key material. */ |
| 116 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 117 | { |
| 118 | /* Load the public value. */ |
| 119 | status = mbedtls_to_psa_error( |
| 120 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 121 | data, |
| 122 | data_length ) ); |
| 123 | if( status != PSA_SUCCESS ) |
| 124 | goto exit; |
| 125 | |
| 126 | /* Check that the point is on the curve. */ |
| 127 | status = mbedtls_to_psa_error( |
| 128 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 129 | if( status != PSA_SUCCESS ) |
| 130 | goto exit; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | /* Load and validate the secret value. */ |
| 135 | status = mbedtls_to_psa_error( |
| 136 | mbedtls_ecp_read_key( ecp->grp.id, |
| 137 | ecp, |
| 138 | data, |
| 139 | data_length ) ); |
| 140 | if( status != PSA_SUCCESS ) |
| 141 | goto exit; |
| 142 | } |
| 143 | |
| 144 | *p_ecp = ecp; |
| 145 | exit: |
| 146 | if( status != PSA_SUCCESS ) |
| 147 | { |
| 148 | mbedtls_ecp_keypair_free( ecp ); |
| 149 | mbedtls_free( ecp ); |
| 150 | } |
| 151 | |
| 152 | return( status ); |
| 153 | } |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 154 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 155 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 156 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 157 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 158 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 159 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 160 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 161 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 162 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 163 | psa_status_t mbedtls_psa_ecp_import_key( |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 164 | const psa_key_attributes_t *attributes, |
| 165 | const uint8_t *data, size_t data_length, |
| 166 | uint8_t *key_buffer, size_t key_buffer_size, |
| 167 | size_t *key_buffer_length, size_t *bits ) |
| 168 | { |
| 169 | psa_status_t status; |
| 170 | mbedtls_ecp_keypair *ecp = NULL; |
| 171 | |
| 172 | /* Parse input */ |
| 173 | status = mbedtls_psa_ecp_load_representation( attributes->core.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 174 | attributes->core.bits, |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 175 | data, |
| 176 | data_length, |
| 177 | &ecp ); |
| 178 | if( status != PSA_SUCCESS ) |
| 179 | goto exit; |
| 180 | |
| 181 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) == |
| 182 | PSA_ECC_FAMILY_MONTGOMERY ) |
| 183 | *bits = ecp->grp.nbits + 1; |
| 184 | else |
| 185 | *bits = ecp->grp.nbits; |
| 186 | |
| 187 | /* Re-export the data to PSA export format. There is currently no support |
| 188 | * for other input formats then the export format, so this is a 1-1 |
| 189 | * copy operation. */ |
| 190 | status = mbedtls_psa_ecp_export_key( attributes->core.type, |
| 191 | ecp, |
| 192 | key_buffer, |
| 193 | key_buffer_size, |
| 194 | key_buffer_length ); |
| 195 | exit: |
| 196 | /* Always free the PK object (will also free contained ECP context) */ |
| 197 | mbedtls_ecp_keypair_free( ecp ); |
| 198 | mbedtls_free( ecp ); |
| 199 | |
| 200 | return( status ); |
| 201 | } |
| 202 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 203 | psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type, |
| 204 | mbedtls_ecp_keypair *ecp, |
| 205 | uint8_t *data, |
| 206 | size_t data_size, |
| 207 | size_t *data_length ) |
| 208 | { |
| 209 | psa_status_t status; |
| 210 | |
| 211 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 212 | { |
| 213 | /* Check whether the public part is loaded */ |
| 214 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 215 | { |
| 216 | /* Calculate the public key */ |
| 217 | status = mbedtls_to_psa_error( |
| 218 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 219 | mbedtls_psa_get_random, |
| 220 | MBEDTLS_PSA_RANDOM_STATE ) ); |
| 221 | if( status != PSA_SUCCESS ) |
| 222 | return( status ); |
| 223 | } |
| 224 | |
| 225 | status = mbedtls_to_psa_error( |
| 226 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 227 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 228 | data_length, |
| 229 | data, |
| 230 | data_size ) ); |
| 231 | if( status != PSA_SUCCESS ) |
| 232 | memset( data, 0, data_size ); |
| 233 | |
| 234 | return( status ); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
| 239 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 240 | |
| 241 | status = mbedtls_to_psa_error( |
| 242 | mbedtls_ecp_write_key( ecp, |
| 243 | data, |
| 244 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
| 245 | if( status == PSA_SUCCESS ) |
| 246 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
| 247 | else |
| 248 | memset( data, 0, data_size ); |
| 249 | |
| 250 | return( status ); |
| 251 | } |
| 252 | } |
| 253 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 254 | psa_status_t mbedtls_psa_ecp_export_public_key( |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 255 | const psa_key_attributes_t *attributes, |
| 256 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 257 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 258 | { |
| 259 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 260 | mbedtls_ecp_keypair *ecp = NULL; |
| 261 | |
| 262 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 263 | attributes->core.type, attributes->core.bits, |
| 264 | key_buffer, key_buffer_size, &ecp ); |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 265 | if( status != PSA_SUCCESS ) |
| 266 | return( status ); |
| 267 | |
| 268 | status = mbedtls_psa_ecp_export_key( |
| 269 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 270 | PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ), |
| 271 | ecp, data, data_size, data_length ); |
| 272 | |
| 273 | mbedtls_ecp_keypair_free( ecp ); |
| 274 | mbedtls_free( ecp ); |
| 275 | |
| 276 | return( status ); |
| 277 | } |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 278 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 279 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 280 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 281 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
| 282 | psa_status_t mbedtls_psa_ecp_generate_key( |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 283 | const psa_key_attributes_t *attributes, |
| 284 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 285 | { |
| 286 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 287 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 288 | |
| 289 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 290 | attributes->core.type ); |
| 291 | mbedtls_ecp_group_id grp_id = |
| 292 | mbedtls_ecc_group_of_psa( curve, attributes->core.bits, 0 ); |
| 293 | |
| 294 | const mbedtls_ecp_curve_info *curve_info = |
| 295 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 296 | mbedtls_ecp_keypair ecp; |
| 297 | |
| 298 | if( attributes->domain_parameters_size != 0 ) |
| 299 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 300 | |
| 301 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 302 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 303 | |
| 304 | mbedtls_ecp_keypair_init( &ecp ); |
| 305 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
| 306 | mbedtls_psa_get_random, |
| 307 | MBEDTLS_PSA_RANDOM_STATE ); |
| 308 | if( ret != 0 ) |
| 309 | { |
| 310 | mbedtls_ecp_keypair_free( &ecp ); |
| 311 | return( mbedtls_to_psa_error( ret ) ); |
| 312 | } |
| 313 | |
| 314 | status = mbedtls_to_psa_error( |
| 315 | mbedtls_ecp_write_key( &ecp, key_buffer, key_buffer_size ) ); |
| 316 | |
| 317 | mbedtls_ecp_keypair_free( &ecp ); |
| 318 | |
| 319 | if( status == PSA_SUCCESS ) |
| 320 | *key_buffer_length = key_buffer_size; |
| 321 | |
| 322 | return( status ); |
| 323 | } |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 324 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 325 | |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 326 | /****************************************************************/ |
| 327 | /* ECDSA sign/verify */ |
| 328 | /****************************************************************/ |
| 329 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 330 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 331 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 332 | psa_status_t mbedtls_psa_ecdsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 333 | const psa_key_attributes_t *attributes, |
| 334 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 335 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 336 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 337 | { |
| 338 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 339 | mbedtls_ecp_keypair *ecp = NULL; |
| 340 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 341 | size_t curve_bytes; |
| 342 | mbedtls_mpi r, s; |
| 343 | |
| 344 | status = mbedtls_psa_ecp_load_representation( attributes->core.type, |
| 345 | attributes->core.bits, |
| 346 | key_buffer, |
| 347 | key_buffer_size, |
| 348 | &ecp ); |
| 349 | if( status != PSA_SUCCESS ) |
| 350 | return( status ); |
| 351 | |
| 352 | curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 353 | mbedtls_mpi_init( &r ); |
| 354 | mbedtls_mpi_init( &s ); |
| 355 | |
| 356 | if( signature_size < 2 * curve_bytes ) |
| 357 | { |
| 358 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 359 | goto cleanup; |
| 360 | } |
| 361 | |
Ronald Cron | 9103d49 | 2021-03-04 11:26:03 +0100 | [diff] [blame] | 362 | if( PSA_ALG_ECDSA_IS_DETERMINISTIC( alg ) ) |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 363 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 364 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 365 | 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] | 366 | 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] | 367 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( |
| 368 | &ecp->grp, &r, &s, |
| 369 | &ecp->d, hash, |
| 370 | hash_length, md_alg, |
| 371 | mbedtls_psa_get_random, |
| 372 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Ronald Cron | 9103d49 | 2021-03-04 11:26:03 +0100 | [diff] [blame] | 373 | #else |
Ronald Cron | bb9cbc7 | 2021-03-04 17:09:00 +0100 | [diff] [blame] | 374 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Ronald Cron | 9103d49 | 2021-03-04 11:26:03 +0100 | [diff] [blame] | 375 | goto cleanup; |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 376 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 377 | } |
| 378 | else |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 379 | { |
| 380 | (void) alg; |
| 381 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 382 | hash, hash_length, |
| 383 | mbedtls_psa_get_random, |
| 384 | MBEDTLS_PSA_RANDOM_STATE ) ); |
| 385 | } |
| 386 | |
| 387 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 388 | signature, |
| 389 | curve_bytes ) ); |
| 390 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 391 | signature + curve_bytes, |
| 392 | curve_bytes ) ); |
| 393 | cleanup: |
| 394 | mbedtls_mpi_free( &r ); |
| 395 | mbedtls_mpi_free( &s ); |
| 396 | if( ret == 0 ) |
| 397 | *signature_length = 2 * curve_bytes; |
| 398 | |
| 399 | mbedtls_ecp_keypair_free( ecp ); |
| 400 | mbedtls_free( ecp ); |
| 401 | |
| 402 | return( mbedtls_to_psa_error( ret ) ); |
| 403 | } |
| 404 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 405 | psa_status_t mbedtls_psa_ecdsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 406 | const psa_key_attributes_t *attributes, |
| 407 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 408 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 409 | const uint8_t *signature, size_t signature_length ) |
| 410 | { |
| 411 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 412 | mbedtls_ecp_keypair *ecp = NULL; |
| 413 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 414 | size_t curve_bytes; |
| 415 | mbedtls_mpi r, s; |
| 416 | |
| 417 | (void)alg; |
| 418 | |
| 419 | status = mbedtls_psa_ecp_load_representation( attributes->core.type, |
| 420 | attributes->core.bits, |
| 421 | key_buffer, |
| 422 | key_buffer_size, |
| 423 | &ecp ); |
| 424 | if( status != PSA_SUCCESS ) |
| 425 | return( status ); |
| 426 | |
| 427 | curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 428 | mbedtls_mpi_init( &r ); |
| 429 | mbedtls_mpi_init( &s ); |
| 430 | |
| 431 | if( signature_length != 2 * curve_bytes ) |
| 432 | { |
| 433 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
| 434 | goto cleanup; |
| 435 | } |
| 436 | |
| 437 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 438 | signature, |
| 439 | curve_bytes ) ); |
| 440 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 441 | signature + curve_bytes, |
| 442 | curve_bytes ) ); |
| 443 | |
| 444 | /* Check whether the public part is loaded. If not, load it. */ |
| 445 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 446 | { |
| 447 | MBEDTLS_MPI_CHK( |
| 448 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 449 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
| 450 | } |
| 451 | |
| 452 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 453 | &ecp->Q, &r, &s ); |
| 454 | |
| 455 | cleanup: |
| 456 | mbedtls_mpi_free( &r ); |
| 457 | mbedtls_mpi_free( &s ); |
| 458 | mbedtls_ecp_keypair_free( ecp ); |
| 459 | mbedtls_free( ecp ); |
| 460 | |
| 461 | return( mbedtls_to_psa_error( ret ) ); |
| 462 | } |
| 463 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 464 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 465 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 466 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 467 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |