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