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" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include "mbedtls/platform.h" |
| 33 | #if !defined(MBEDTLS_PLATFORM_C) |
| 34 | #define mbedtls_calloc calloc |
| 35 | #define mbedtls_free free |
| 36 | #endif |
| 37 | |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 38 | #include <mbedtls/ecdsa.h> |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 39 | #include <mbedtls/ecp.h> |
| 40 | #include <mbedtls/error.h> |
| 41 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 42 | #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 43 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 44 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ) ) |
| 45 | #define BUILTIN_KEY_TYPE_ECC_KEY_PAIR 1 |
| 46 | #endif |
| 47 | |
| 48 | #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 49 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 50 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) ) ) |
| 51 | #define BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY 1 |
| 52 | #endif |
| 53 | |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 54 | #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 55 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 56 | defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) && \ |
| 57 | defined(MBEDTLS_ECDSA_C) ) ) |
| 58 | #define BUILTIN_ALG_ECDSA 1 |
| 59 | #endif |
| 60 | |
| 61 | #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 62 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 63 | defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) && \ |
| 64 | defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) ) ) |
| 65 | #define BUILTIN_ALG_DETERMINISTIC_ECDSA 1 |
| 66 | #endif |
| 67 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 68 | #if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 69 | defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 70 | defined(BUILTIN_ALG_ECDSA) || \ |
| 71 | defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 72 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 73 | psa_status_t mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 74 | psa_key_type_t type, size_t curve_bits, |
| 75 | const uint8_t *data, size_t data_length, |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 76 | mbedtls_ecp_keypair **p_ecp ) |
| 77 | { |
| 78 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 79 | psa_status_t status; |
| 80 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 81 | size_t curve_bytes = data_length; |
| 82 | int explicit_bits = ( curve_bits != 0 ); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 83 | |
| 84 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 85 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
| 86 | { |
| 87 | /* A Weierstrass public key is represented as: |
| 88 | * - The byte 0x04; |
| 89 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 90 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 91 | * So its data length is 2m+1 where m is the curve size in bits. |
| 92 | */ |
| 93 | if( ( data_length & 1 ) == 0 ) |
| 94 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 95 | curve_bytes = data_length / 2; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 96 | |
| 97 | /* Montgomery public keys are represented in compressed format, meaning |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 98 | * their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 99 | |
| 100 | /* Private keys are represented in uncompressed private random integer |
Gilles Peskine | d88ccae | 2021-02-08 18:39:18 +0100 | [diff] [blame] | 101 | * format, meaning their curve_bytes is equal to the amount of input. */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 104 | if( explicit_bits ) |
| 105 | { |
| 106 | /* With an explicit bit-size, the data must have the matching length. */ |
| 107 | if( curve_bytes != PSA_BITS_TO_BYTES( curve_bits ) ) |
| 108 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | /* We need to infer the bit-size from the data. Since the only |
| 113 | * information we have is the length in bytes, the value of curve_bits |
| 114 | * at this stage is rounded up to the nearest multiple of 8. */ |
| 115 | curve_bits = PSA_BYTES_TO_BITS( curve_bytes ); |
| 116 | } |
| 117 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 118 | /* Allocate and initialize a key representation. */ |
| 119 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 120 | if( ecp == NULL ) |
| 121 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 122 | mbedtls_ecp_keypair_init( ecp ); |
| 123 | |
| 124 | /* Load the group. */ |
| 125 | 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] | 126 | curve_bits, !explicit_bits ); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 127 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 128 | { |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 129 | /* We can't distinguish between a nonsensical family/size combination |
| 130 | * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a |
| 131 | * well-regarded curve that Mbed TLS just doesn't know about (which |
| 132 | * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how |
| 133 | * curves that Mbed TLS knows about but for which support is disabled |
| 134 | * at build time, return NOT_SUPPORTED. */ |
| 135 | status = PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 136 | goto exit; |
| 137 | } |
| 138 | |
| 139 | status = mbedtls_to_psa_error( |
| 140 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 141 | if( status != PSA_SUCCESS ) |
| 142 | goto exit; |
| 143 | |
| 144 | /* Load the key material. */ |
| 145 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 146 | { |
| 147 | /* Load the public value. */ |
| 148 | status = mbedtls_to_psa_error( |
| 149 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 150 | data, |
| 151 | data_length ) ); |
| 152 | if( status != PSA_SUCCESS ) |
| 153 | goto exit; |
| 154 | |
| 155 | /* Check that the point is on the curve. */ |
| 156 | status = mbedtls_to_psa_error( |
| 157 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 158 | if( status != PSA_SUCCESS ) |
| 159 | goto exit; |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | /* Load and validate the secret value. */ |
| 164 | status = mbedtls_to_psa_error( |
| 165 | mbedtls_ecp_read_key( ecp->grp.id, |
| 166 | ecp, |
| 167 | data, |
| 168 | data_length ) ); |
| 169 | if( status != PSA_SUCCESS ) |
| 170 | goto exit; |
| 171 | } |
| 172 | |
| 173 | *p_ecp = ecp; |
| 174 | exit: |
| 175 | if( status != PSA_SUCCESS ) |
| 176 | { |
| 177 | mbedtls_ecp_keypair_free( ecp ); |
| 178 | mbedtls_free( ecp ); |
| 179 | } |
| 180 | |
| 181 | return( status ); |
| 182 | } |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 183 | #endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 184 | * defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 185 | * defined(BUILTIN_ALG_ECDSA) || |
| 186 | * defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 187 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 188 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 189 | #if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 190 | defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 191 | |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 192 | static psa_status_t ecp_import_key( |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 193 | const psa_key_attributes_t *attributes, |
| 194 | const uint8_t *data, size_t data_length, |
| 195 | uint8_t *key_buffer, size_t key_buffer_size, |
| 196 | size_t *key_buffer_length, size_t *bits ) |
| 197 | { |
| 198 | psa_status_t status; |
| 199 | mbedtls_ecp_keypair *ecp = NULL; |
| 200 | |
| 201 | /* Parse input */ |
| 202 | status = mbedtls_psa_ecp_load_representation( attributes->core.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 203 | attributes->core.bits, |
Ronald Cron | d6ec303 | 2020-11-27 18:54:57 +0100 | [diff] [blame] | 204 | data, |
| 205 | data_length, |
| 206 | &ecp ); |
| 207 | if( status != PSA_SUCCESS ) |
| 208 | goto exit; |
| 209 | |
| 210 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) == |
| 211 | PSA_ECC_FAMILY_MONTGOMERY ) |
| 212 | *bits = ecp->grp.nbits + 1; |
| 213 | else |
| 214 | *bits = ecp->grp.nbits; |
| 215 | |
| 216 | /* Re-export the data to PSA export format. There is currently no support |
| 217 | * for other input formats then the export format, so this is a 1-1 |
| 218 | * copy operation. */ |
| 219 | status = mbedtls_psa_ecp_export_key( attributes->core.type, |
| 220 | ecp, |
| 221 | key_buffer, |
| 222 | key_buffer_size, |
| 223 | key_buffer_length ); |
| 224 | exit: |
| 225 | /* Always free the PK object (will also free contained ECP context) */ |
| 226 | mbedtls_ecp_keypair_free( ecp ); |
| 227 | mbedtls_free( ecp ); |
| 228 | |
| 229 | return( status ); |
| 230 | } |
| 231 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 232 | psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type, |
| 233 | mbedtls_ecp_keypair *ecp, |
| 234 | uint8_t *data, |
| 235 | size_t data_size, |
| 236 | size_t *data_length ) |
| 237 | { |
| 238 | psa_status_t status; |
| 239 | |
| 240 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 241 | { |
| 242 | /* Check whether the public part is loaded */ |
| 243 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 244 | { |
| 245 | /* Calculate the public key */ |
| 246 | status = mbedtls_to_psa_error( |
| 247 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 248 | mbedtls_psa_get_random, |
| 249 | MBEDTLS_PSA_RANDOM_STATE ) ); |
| 250 | if( status != PSA_SUCCESS ) |
| 251 | return( status ); |
| 252 | } |
| 253 | |
| 254 | status = mbedtls_to_psa_error( |
| 255 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 256 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 257 | data_length, |
| 258 | data, |
| 259 | data_size ) ); |
| 260 | if( status != PSA_SUCCESS ) |
| 261 | memset( data, 0, data_size ); |
| 262 | |
| 263 | return( status ); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
| 268 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 269 | |
| 270 | status = mbedtls_to_psa_error( |
| 271 | mbedtls_ecp_write_key( ecp, |
| 272 | data, |
| 273 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
| 274 | if( status == PSA_SUCCESS ) |
| 275 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
| 276 | else |
| 277 | memset( data, 0, data_size ); |
| 278 | |
| 279 | return( status ); |
| 280 | } |
| 281 | } |
| 282 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 283 | static psa_status_t ecp_export_public_key( |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 284 | const psa_key_attributes_t *attributes, |
| 285 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 286 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 287 | { |
| 288 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 289 | mbedtls_ecp_keypair *ecp = NULL; |
| 290 | |
| 291 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 292 | attributes->core.type, attributes->core.bits, |
| 293 | key_buffer, key_buffer_size, &ecp ); |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 294 | if( status != PSA_SUCCESS ) |
| 295 | return( status ); |
| 296 | |
| 297 | status = mbedtls_psa_ecp_export_key( |
| 298 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 299 | PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ), |
| 300 | ecp, data, data_size, data_length ); |
| 301 | |
| 302 | mbedtls_ecp_keypair_free( ecp ); |
| 303 | mbedtls_free( ecp ); |
| 304 | |
| 305 | return( status ); |
| 306 | } |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 307 | #endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 308 | * defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 309 | |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 310 | #if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | bbe5cbb | 2020-11-20 19:42:24 +0100 | [diff] [blame] | 311 | static psa_status_t ecp_generate_key( |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 312 | const psa_key_attributes_t *attributes, |
| 313 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 314 | { |
| 315 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 316 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 317 | |
| 318 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 319 | attributes->core.type ); |
| 320 | mbedtls_ecp_group_id grp_id = |
| 321 | mbedtls_ecc_group_of_psa( curve, attributes->core.bits, 0 ); |
| 322 | |
| 323 | const mbedtls_ecp_curve_info *curve_info = |
| 324 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 325 | mbedtls_ecp_keypair ecp; |
| 326 | |
| 327 | if( attributes->domain_parameters_size != 0 ) |
| 328 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 329 | |
| 330 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 331 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 332 | |
| 333 | mbedtls_ecp_keypair_init( &ecp ); |
| 334 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
| 335 | mbedtls_psa_get_random, |
| 336 | MBEDTLS_PSA_RANDOM_STATE ); |
| 337 | if( ret != 0 ) |
| 338 | { |
| 339 | mbedtls_ecp_keypair_free( &ecp ); |
| 340 | return( mbedtls_to_psa_error( ret ) ); |
| 341 | } |
| 342 | |
| 343 | status = mbedtls_to_psa_error( |
| 344 | mbedtls_ecp_write_key( &ecp, key_buffer, key_buffer_size ) ); |
| 345 | |
| 346 | mbedtls_ecp_keypair_free( &ecp ); |
| 347 | |
| 348 | if( status == PSA_SUCCESS ) |
| 349 | *key_buffer_length = key_buffer_size; |
| 350 | |
| 351 | return( status ); |
| 352 | } |
| 353 | #endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
| 354 | |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 355 | /****************************************************************/ |
| 356 | /* ECDSA sign/verify */ |
| 357 | /****************************************************************/ |
| 358 | |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 359 | #if defined(BUILTIN_ALG_ECDSA) || \ |
| 360 | defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 361 | static psa_status_t ecdsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 362 | const psa_key_attributes_t *attributes, |
| 363 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 364 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 365 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 366 | { |
| 367 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 368 | mbedtls_ecp_keypair *ecp = NULL; |
| 369 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 370 | size_t curve_bytes; |
| 371 | mbedtls_mpi r, s; |
| 372 | |
| 373 | status = mbedtls_psa_ecp_load_representation( attributes->core.type, |
| 374 | attributes->core.bits, |
| 375 | key_buffer, |
| 376 | key_buffer_size, |
| 377 | &ecp ); |
| 378 | if( status != PSA_SUCCESS ) |
| 379 | return( status ); |
| 380 | |
| 381 | curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 382 | mbedtls_mpi_init( &r ); |
| 383 | mbedtls_mpi_init( &s ); |
| 384 | |
| 385 | if( signature_size < 2 * curve_bytes ) |
| 386 | { |
| 387 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 388 | goto cleanup; |
| 389 | } |
| 390 | |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 391 | #if defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 392 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 393 | { |
| 394 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 395 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 396 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 397 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( |
| 398 | &ecp->grp, &r, &s, |
| 399 | &ecp->d, hash, |
| 400 | hash_length, md_alg, |
| 401 | mbedtls_psa_get_random, |
| 402 | MBEDTLS_PSA_RANDOM_STATE ) ); |
| 403 | } |
| 404 | else |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 405 | #endif /* defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 406 | { |
| 407 | (void) alg; |
| 408 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 409 | hash, hash_length, |
| 410 | mbedtls_psa_get_random, |
| 411 | MBEDTLS_PSA_RANDOM_STATE ) ); |
| 412 | } |
| 413 | |
| 414 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 415 | signature, |
| 416 | curve_bytes ) ); |
| 417 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 418 | signature + curve_bytes, |
| 419 | curve_bytes ) ); |
| 420 | cleanup: |
| 421 | mbedtls_mpi_free( &r ); |
| 422 | mbedtls_mpi_free( &s ); |
| 423 | if( ret == 0 ) |
| 424 | *signature_length = 2 * curve_bytes; |
| 425 | |
| 426 | mbedtls_ecp_keypair_free( ecp ); |
| 427 | mbedtls_free( ecp ); |
| 428 | |
| 429 | return( mbedtls_to_psa_error( ret ) ); |
| 430 | } |
| 431 | |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 432 | static psa_status_t ecdsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 433 | const psa_key_attributes_t *attributes, |
| 434 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 435 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 436 | const uint8_t *signature, size_t signature_length ) |
| 437 | { |
| 438 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 439 | mbedtls_ecp_keypair *ecp = NULL; |
| 440 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 441 | size_t curve_bytes; |
| 442 | mbedtls_mpi r, s; |
| 443 | |
| 444 | (void)alg; |
| 445 | |
| 446 | status = mbedtls_psa_ecp_load_representation( attributes->core.type, |
| 447 | attributes->core.bits, |
| 448 | key_buffer, |
| 449 | key_buffer_size, |
| 450 | &ecp ); |
| 451 | if( status != PSA_SUCCESS ) |
| 452 | return( status ); |
| 453 | |
| 454 | curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 455 | mbedtls_mpi_init( &r ); |
| 456 | mbedtls_mpi_init( &s ); |
| 457 | |
| 458 | if( signature_length != 2 * curve_bytes ) |
| 459 | { |
| 460 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
| 461 | goto cleanup; |
| 462 | } |
| 463 | |
| 464 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 465 | signature, |
| 466 | curve_bytes ) ); |
| 467 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 468 | signature + curve_bytes, |
| 469 | curve_bytes ) ); |
| 470 | |
| 471 | /* Check whether the public part is loaded. If not, load it. */ |
| 472 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 473 | { |
| 474 | MBEDTLS_MPI_CHK( |
| 475 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 476 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
| 477 | } |
| 478 | |
| 479 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 480 | &ecp->Q, &r, &s ); |
| 481 | |
| 482 | cleanup: |
| 483 | mbedtls_mpi_free( &r ); |
| 484 | mbedtls_mpi_free( &s ); |
| 485 | mbedtls_ecp_keypair_free( ecp ); |
| 486 | mbedtls_free( ecp ); |
| 487 | |
| 488 | return( mbedtls_to_psa_error( ret ) ); |
| 489 | } |
| 490 | |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 491 | #endif /* defined(BUILTIN_ALG_ECDSA) || \ |
| 492 | * defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 493 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 494 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 495 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 496 | |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 497 | psa_status_t mbedtls_psa_ecp_import_key( |
| 498 | const psa_key_attributes_t *attributes, |
| 499 | const uint8_t *data, size_t data_length, |
| 500 | uint8_t *key_buffer, size_t key_buffer_size, |
| 501 | size_t *key_buffer_length, size_t *bits ) |
| 502 | { |
| 503 | return( ecp_import_key( attributes, data, data_length, |
| 504 | key_buffer, key_buffer_size, |
| 505 | key_buffer_length, bits ) ); |
| 506 | } |
| 507 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 508 | psa_status_t mbedtls_psa_ecp_export_public_key( |
| 509 | const psa_key_attributes_t *attributes, |
| 510 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 511 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 512 | { |
| 513 | return( ecp_export_public_key( attributes, key_buffer, key_buffer_size, |
| 514 | data, data_size, data_length ) ); |
| 515 | } |
| 516 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 517 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 518 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 519 | |
Ronald Cron | bbe5cbb | 2020-11-20 19:42:24 +0100 | [diff] [blame] | 520 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
| 521 | psa_status_t mbedtls_psa_ecp_generate_key( |
| 522 | const psa_key_attributes_t *attributes, |
| 523 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 524 | { |
| 525 | return( ecp_generate_key( attributes, key_buffer, key_buffer_size, |
| 526 | key_buffer_length ) ); |
| 527 | } |
| 528 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
| 529 | |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 530 | |
| 531 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 532 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 533 | |
| 534 | psa_status_t mbedtls_psa_ecdsa_sign_hash( |
| 535 | const psa_key_attributes_t *attributes, |
| 536 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 537 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 538 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 539 | { |
| 540 | |
| 541 | return( ecdsa_sign_hash( attributes, |
| 542 | key_buffer, key_buffer_size, |
| 543 | alg, hash, hash_length, |
| 544 | signature, signature_size, signature_length ) ); |
| 545 | } |
| 546 | |
| 547 | psa_status_t mbedtls_psa_ecdsa_verify_hash( |
| 548 | const psa_key_attributes_t *attributes, |
| 549 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 550 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 551 | const uint8_t *signature, size_t signature_length ) |
| 552 | { |
| 553 | return( ecdsa_verify_hash( attributes, |
| 554 | key_buffer, key_buffer_size, |
| 555 | alg, hash, hash_length, |
| 556 | signature, signature_length ) ); |
| 557 | } |
| 558 | |
| 559 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 560 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 561 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 562 | /* |
| 563 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 564 | */ |
| 565 | |
| 566 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 567 | |
| 568 | #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 569 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 570 | |
| 571 | psa_status_t mbedtls_transparent_test_driver_ecp_import_key( |
| 572 | const psa_key_attributes_t *attributes, |
| 573 | const uint8_t *data, size_t data_length, |
| 574 | uint8_t *key_buffer, size_t key_buffer_size, |
| 575 | size_t *key_buffer_length, size_t *bits ) |
| 576 | { |
| 577 | return( ecp_import_key( attributes, data, data_length, |
| 578 | key_buffer, key_buffer_size, |
| 579 | key_buffer_length, bits ) ); |
| 580 | } |
| 581 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 582 | psa_status_t mbedtls_transparent_test_driver_ecp_export_public_key( |
| 583 | const psa_key_attributes_t *attributes, |
| 584 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 585 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 586 | { |
| 587 | return( ecp_export_public_key( attributes, key_buffer, key_buffer_size, |
| 588 | data, data_size, data_length ) ); |
| 589 | } |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 590 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 591 | #endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 592 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 593 | |
Ronald Cron | 9539126 | 2021-02-08 09:54:03 +0100 | [diff] [blame] | 594 | #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) && \ |
| 595 | defined(MBEDTLS_GENPRIME) |
Ronald Cron | bbe5cbb | 2020-11-20 19:42:24 +0100 | [diff] [blame] | 596 | psa_status_t mbedtls_transparent_test_driver_ecp_generate_key( |
| 597 | const psa_key_attributes_t *attributes, |
| 598 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 599 | { |
| 600 | return( ecp_generate_key( attributes, key_buffer, key_buffer_size, |
| 601 | key_buffer_length ) ); |
| 602 | } |
Ronald Cron | 9539126 | 2021-02-08 09:54:03 +0100 | [diff] [blame] | 603 | #endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) && |
Ronald Cron | bbe5cbb | 2020-11-20 19:42:24 +0100 | [diff] [blame] | 604 | defined(MBEDTLS_GENPRIME) */ |
| 605 | |
Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame^] | 606 | #if defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) || \ |
| 607 | defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) |
| 608 | |
| 609 | psa_status_t mbedtls_transparent_test_driver_ecdsa_sign_hash( |
| 610 | const psa_key_attributes_t *attributes, |
| 611 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 612 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 613 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 614 | { |
| 615 | |
| 616 | #if defined(MBEDTLS_ECDSA_C) |
| 617 | return( ecdsa_sign_hash( attributes, |
| 618 | key_buffer, key_buffer_size, |
| 619 | alg, hash, hash_length, |
| 620 | signature, signature_size, signature_length ) ); |
| 621 | #else |
| 622 | (void)attributes; |
| 623 | (void)key_buffer; |
| 624 | (void)key_buffer_size; |
| 625 | (void)alg; |
| 626 | (void)hash; |
| 627 | (void)hash_length; |
| 628 | (void)signature; |
| 629 | (void)signature_size; |
| 630 | (void)signature_length; |
| 631 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 632 | #endif |
| 633 | } |
| 634 | |
| 635 | psa_status_t mbedtls_transparent_test_driver_ecdsa_verify_hash( |
| 636 | const psa_key_attributes_t *attributes, |
| 637 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 638 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 639 | const uint8_t *signature, size_t signature_length ) |
| 640 | { |
| 641 | #if defined(MBEDTLS_ECDSA_C) |
| 642 | return( ecdsa_verify_hash( attributes, |
| 643 | key_buffer, key_buffer_size, |
| 644 | alg, hash, hash_length, |
| 645 | signature, signature_length ) ); |
| 646 | #else |
| 647 | (void)attributes; |
| 648 | (void)key_buffer; |
| 649 | (void)key_buffer_size; |
| 650 | (void)alg; |
| 651 | (void)hash; |
| 652 | (void)hash_length; |
| 653 | (void)signature; |
| 654 | (void)signature_length; |
| 655 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 656 | #endif |
| 657 | } |
| 658 | |
| 659 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) || |
| 660 | * defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) */ |
| 661 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 662 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 663 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 664 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |