0.2.0-dev Download ZIP
921 lines ·30.6 KB Raw
| 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 OR GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "tf_psa_crypto_common.h" |
| 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 12 | |
| 13 | #include <psa/crypto.h> |
| 14 | #include "psa_crypto_core.h" |
| 15 | #include "psa_crypto_ecp.h" |
| 16 | #include "psa_crypto_random_impl.h" |
| 17 | #include "psa_util_internal.h" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include "mbedtls/platform.h" |
| 22 | |
| 23 | #include <mbedtls/private/ecdsa.h> |
| 24 | #include <mbedtls/private/ecp.h> |
| 25 | #include <mbedtls/private/error_common.h> |
| 26 | |
| 27 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 28 | #include "tf-psa-crypto/private/everest/x25519.h" |
| 29 | #include "mbedtls/constant_time.h" |
| 30 | #endif |
| 31 | |
| 32 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) || \ |
| 33 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
| 34 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
| 35 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 36 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 37 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 38 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
| 39 | /* Helper function to verify if the provided EC's family and key bit size are valid. |
| 40 | * |
| 41 | * Note: "bits" parameter is used both as input and output and it might be updated |
| 42 | * in case provided input value is not multiple of 8 ("sloppy" bits). |
| 43 | */ |
| 44 | static int check_ecc_parameters(psa_ecc_family_t family, size_t *bits) |
| 45 | { |
| 46 | switch (family) { |
| 47 | case PSA_ECC_FAMILY_SECP_R1: |
| 48 | switch (*bits) { |
| 49 | case 192: |
| 50 | case 224: |
| 51 | case 256: |
| 52 | case 384: |
| 53 | case 521: |
| 54 | return PSA_SUCCESS; |
| 55 | case 528: |
| 56 | *bits = 521; |
| 57 | return PSA_SUCCESS; |
| 58 | } |
| 59 | break; |
| 60 | |
| 61 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
| 62 | switch (*bits) { |
| 63 | case 256: |
| 64 | case 384: |
| 65 | case 512: |
| 66 | return PSA_SUCCESS; |
| 67 | } |
| 68 | break; |
| 69 | |
| 70 | case PSA_ECC_FAMILY_MONTGOMERY: |
| 71 | switch (*bits) { |
| 72 | case 448: |
| 73 | case 255: |
| 74 | return PSA_SUCCESS; |
| 75 | case 256: |
| 76 | *bits = 255; |
| 77 | return PSA_SUCCESS; |
| 78 | } |
| 79 | break; |
| 80 | |
| 81 | case PSA_ECC_FAMILY_SECP_K1: |
| 82 | switch (*bits) { |
| 83 | case 192: |
| 84 | case 256: |
| 85 | return PSA_SUCCESS; |
| 86 | } |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | return PSA_ERROR_INVALID_ARGUMENT; |
| 91 | } |
| 92 | |
| 93 | psa_status_t mbedtls_psa_ecp_load_representation( |
| 94 | psa_key_type_t type, size_t curve_bits, |
| 95 | const uint8_t *data, size_t data_length, |
| 96 | mbedtls_ecp_keypair **p_ecp) |
| 97 | { |
| 98 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 99 | psa_status_t status; |
| 100 | mbedtls_ecp_keypair *ecp = NULL; |
| 101 | size_t curve_bytes = data_length; |
| 102 | int explicit_bits = (curve_bits != 0); |
| 103 | |
| 104 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) && |
| 105 | PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) { |
| 106 | /* A Weierstrass public key is represented as: |
| 107 | * - The byte 0x04; |
| 108 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 109 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 110 | * So its data length is 2m+1 where m is the curve size in bits. |
| 111 | */ |
| 112 | if ((data_length & 1) == 0) { |
| 113 | return PSA_ERROR_INVALID_ARGUMENT; |
| 114 | } |
| 115 | curve_bytes = data_length / 2; |
| 116 | |
| 117 | /* Montgomery public keys are represented in compressed format, meaning |
| 118 | * their curve_bytes is equal to the amount of input. */ |
| 119 | |
| 120 | /* Private keys are represented in uncompressed private random integer |
| 121 | * format, meaning their curve_bytes is equal to the amount of input. */ |
| 122 | } |
| 123 | |
| 124 | if (explicit_bits) { |
| 125 | /* With an explicit bit-size, the data must have the matching length. */ |
| 126 | if (curve_bytes != PSA_BITS_TO_BYTES(curve_bits)) { |
| 127 | return PSA_ERROR_INVALID_ARGUMENT; |
| 128 | } |
| 129 | } else { |
| 130 | /* We need to infer the bit-size from the data. Since the only |
| 131 | * information we have is the length in bytes, the value of curve_bits |
| 132 | * at this stage is rounded up to the nearest multiple of 8. */ |
| 133 | curve_bits = PSA_BYTES_TO_BITS(curve_bytes); |
| 134 | } |
| 135 | |
| 136 | /* Allocate and initialize a key representation. */ |
| 137 | ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
| 138 | if (ecp == NULL) { |
| 139 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 140 | } |
| 141 | mbedtls_ecp_keypair_init(ecp); |
| 142 | |
| 143 | status = check_ecc_parameters(PSA_KEY_TYPE_ECC_GET_FAMILY(type), &curve_bits); |
| 144 | if (status != PSA_SUCCESS) { |
| 145 | goto exit; |
| 146 | } |
| 147 | |
| 148 | /* Load the group. */ |
| 149 | grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(type), |
| 150 | curve_bits); |
| 151 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
| 152 | status = PSA_ERROR_NOT_SUPPORTED; |
| 153 | goto exit; |
| 154 | } |
| 155 | |
| 156 | status = mbedtls_to_psa_error( |
| 157 | mbedtls_ecp_group_load(&ecp->grp, grp_id)); |
| 158 | if (status != PSA_SUCCESS) { |
| 159 | goto exit; |
| 160 | } |
| 161 | |
| 162 | /* Load the key material. */ |
| 163 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 164 | /* Load the public value. */ |
| 165 | status = mbedtls_to_psa_error( |
| 166 | mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q, |
| 167 | data, |
| 168 | data_length)); |
| 169 | if (status != PSA_SUCCESS) { |
| 170 | goto exit; |
| 171 | } |
| 172 | |
| 173 | /* Check that the point is on the curve. */ |
| 174 | status = mbedtls_to_psa_error( |
| 175 | mbedtls_ecp_check_pubkey(&ecp->grp, &ecp->Q)); |
| 176 | if (status != PSA_SUCCESS) { |
| 177 | goto exit; |
| 178 | } |
| 179 | } else { |
| 180 | /* Load and validate the secret value. */ |
| 181 | status = mbedtls_to_psa_error( |
| 182 | mbedtls_ecp_read_key(ecp->grp.id, |
| 183 | ecp, |
| 184 | data, |
| 185 | data_length)); |
| 186 | if (status != PSA_SUCCESS) { |
| 187 | goto exit; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | *p_ecp = ecp; |
| 192 | exit: |
| 193 | if (status != PSA_SUCCESS) { |
| 194 | mbedtls_ecp_keypair_free(ecp); |
| 195 | mbedtls_free(ecp); |
| 196 | } |
| 197 | |
| 198 | return status; |
| 199 | } |
| 200 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) || |
| 201 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
| 202 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
| 203 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 204 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 205 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 206 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
| 207 | |
| 208 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
| 209 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
| 210 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 211 | |
| 212 | psa_status_t mbedtls_psa_ecp_import_key( |
| 213 | const psa_key_attributes_t *attributes, |
| 214 | const uint8_t *data, size_t data_length, |
| 215 | uint8_t *key_buffer, size_t key_buffer_size, |
| 216 | size_t *key_buffer_length, size_t *bits) |
| 217 | { |
| 218 | psa_status_t status; |
| 219 | mbedtls_ecp_keypair *ecp = NULL; |
| 220 | |
| 221 | /* Parse input */ |
| 222 | status = mbedtls_psa_ecp_load_representation(attributes->type, |
| 223 | attributes->bits, |
| 224 | data, |
| 225 | data_length, |
| 226 | &ecp); |
| 227 | if (status != PSA_SUCCESS) { |
| 228 | goto exit; |
| 229 | } |
| 230 | |
| 231 | if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->type) == |
| 232 | PSA_ECC_FAMILY_MONTGOMERY) { |
| 233 | *bits = ecp->grp.nbits + 1; |
| 234 | } else { |
| 235 | *bits = ecp->grp.nbits; |
| 236 | } |
| 237 | |
| 238 | /* Re-export the data to PSA export format. There is currently no support |
| 239 | * for other input formats then the export format, so this is a 1-1 |
| 240 | * copy operation. */ |
| 241 | status = mbedtls_psa_ecp_export_key(attributes->type, |
| 242 | ecp, |
| 243 | key_buffer, |
| 244 | key_buffer_size, |
| 245 | key_buffer_length); |
| 246 | exit: |
| 247 | /* Always free the PK object (will also free contained ECP context) */ |
| 248 | mbedtls_ecp_keypair_free(ecp); |
| 249 | mbedtls_free(ecp); |
| 250 | |
| 251 | return status; |
| 252 | } |
| 253 | |
| 254 | psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type, |
| 255 | mbedtls_ecp_keypair *ecp, |
| 256 | uint8_t *data, |
| 257 | size_t data_size, |
| 258 | size_t *data_length) |
| 259 | { |
| 260 | psa_status_t status; |
| 261 | |
| 262 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 263 | /* Check whether the public part is loaded */ |
| 264 | if (mbedtls_ecp_is_zero(&ecp->Q)) { |
| 265 | /* Calculate the public key */ |
| 266 | status = mbedtls_to_psa_error( |
| 267 | mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 268 | mbedtls_psa_get_random, |
| 269 | MBEDTLS_PSA_RANDOM_STATE)); |
| 270 | if (status != PSA_SUCCESS) { |
| 271 | return status; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | status = mbedtls_to_psa_error( |
| 276 | mbedtls_ecp_point_write_binary(&ecp->grp, &ecp->Q, |
| 277 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 278 | data_length, |
| 279 | data, |
| 280 | data_size)); |
| 281 | if (status != PSA_SUCCESS) { |
| 282 | memset(data, 0, data_size); |
| 283 | } |
| 284 | |
| 285 | return status; |
| 286 | } else { |
| 287 | status = mbedtls_to_psa_error( |
| 288 | mbedtls_ecp_write_key_ext(ecp, data_length, data, data_size)); |
| 289 | return status; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | psa_status_t mbedtls_psa_ecp_export_public_key( |
| 294 | const psa_key_attributes_t *attributes, |
| 295 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 296 | uint8_t *data, size_t data_size, size_t *data_length) |
| 297 | { |
| 298 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 299 | mbedtls_ecp_keypair *ecp = NULL; |
| 300 | |
| 301 | status = mbedtls_psa_ecp_load_representation( |
| 302 | attributes->type, attributes->bits, |
| 303 | key_buffer, key_buffer_size, &ecp); |
| 304 | if (status != PSA_SUCCESS) { |
| 305 | return status; |
| 306 | } |
| 307 | |
| 308 | status = mbedtls_psa_ecp_export_key( |
| 309 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 310 | PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->type)), |
| 311 | ecp, data, data_size, data_length); |
| 312 | |
| 313 | mbedtls_ecp_keypair_free(ecp); |
| 314 | mbedtls_free(ecp); |
| 315 | |
| 316 | return status; |
| 317 | } |
| 318 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
| 319 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
| 320 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 321 | |
| 322 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
| 323 | psa_status_t mbedtls_psa_ecp_generate_key( |
| 324 | const psa_key_attributes_t *attributes, |
| 325 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) |
| 326 | { |
| 327 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 328 | attributes->type); |
| 329 | mbedtls_ecp_group_id grp_id = |
| 330 | mbedtls_ecc_group_from_psa(curve, attributes->bits); |
| 331 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
| 332 | return PSA_ERROR_NOT_SUPPORTED; |
| 333 | } |
| 334 | |
| 335 | mbedtls_ecp_keypair ecp; |
| 336 | mbedtls_ecp_keypair_init(&ecp); |
| 337 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 338 | |
| 339 | ret = mbedtls_ecp_group_load(&ecp.grp, grp_id); |
| 340 | if (ret != 0) { |
| 341 | goto exit; |
| 342 | } |
| 343 | |
| 344 | ret = mbedtls_ecp_gen_privkey(&ecp.grp, &ecp.d, |
| 345 | mbedtls_psa_get_random, |
| 346 | MBEDTLS_PSA_RANDOM_STATE); |
| 347 | if (ret != 0) { |
| 348 | goto exit; |
| 349 | } |
| 350 | |
| 351 | ret = mbedtls_ecp_write_key_ext(&ecp, key_buffer_length, |
| 352 | key_buffer, key_buffer_size); |
| 353 | |
| 354 | exit: |
| 355 | mbedtls_ecp_keypair_free(&ecp); |
| 356 | return mbedtls_to_psa_error(ret); |
| 357 | } |
| 358 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE */ |
| 359 | |
| 360 | /****************************************************************/ |
| 361 | /* ECDSA sign/verify */ |
| 362 | /****************************************************************/ |
| 363 | |
| 364 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 365 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 366 | psa_status_t mbedtls_psa_ecdsa_sign_hash( |
| 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->type, |
| 379 | attributes->bits, |
| 380 | key_buffer, |
| 381 | key_buffer_size, |
| 382 | &ecp); |
| 383 | if (status != PSA_SUCCESS) { |
| 384 | return status; |
| 385 | } |
| 386 | |
| 387 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); |
| 388 | mbedtls_mpi_init(&r); |
| 389 | mbedtls_mpi_init(&s); |
| 390 | |
| 391 | if (signature_size < 2 * curve_bytes) { |
| 392 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 393 | goto cleanup; |
| 394 | } |
| 395 | |
| 396 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) { |
| 397 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 398 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
| 399 | mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg); |
| 400 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext( |
| 401 | &ecp->grp, &r, &s, |
| 402 | &ecp->d, hash, |
| 403 | hash_length, md_alg, |
| 404 | mbedtls_psa_get_random, |
| 405 | MBEDTLS_PSA_RANDOM_STATE)); |
| 406 | #else |
| 407 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 408 | goto cleanup; |
| 409 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 410 | } else { |
| 411 | (void) alg; |
| 412 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d, |
| 413 | hash, hash_length, |
| 414 | mbedtls_psa_get_random, |
| 415 | MBEDTLS_PSA_RANDOM_STATE)); |
| 416 | } |
| 417 | |
| 418 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r, |
| 419 | signature, |
| 420 | curve_bytes)); |
| 421 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s, |
| 422 | signature + curve_bytes, |
| 423 | curve_bytes)); |
| 424 | cleanup: |
| 425 | mbedtls_mpi_free(&r); |
| 426 | mbedtls_mpi_free(&s); |
| 427 | if (ret == 0) { |
| 428 | *signature_length = 2 * curve_bytes; |
| 429 | } |
| 430 | |
| 431 | mbedtls_ecp_keypair_free(ecp); |
| 432 | mbedtls_free(ecp); |
| 433 | |
| 434 | return mbedtls_to_psa_error(ret); |
| 435 | } |
| 436 | |
| 437 | psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp) |
| 438 | { |
| 439 | int ret = 0; |
| 440 | |
| 441 | /* Check whether the public part is loaded. If not, load it. */ |
| 442 | if (mbedtls_ecp_is_zero(&ecp->Q)) { |
| 443 | ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q, |
| 444 | &ecp->d, &ecp->grp.G, |
| 445 | mbedtls_psa_get_random, |
| 446 | MBEDTLS_PSA_RANDOM_STATE); |
| 447 | } |
| 448 | |
| 449 | return mbedtls_to_psa_error(ret); |
| 450 | } |
| 451 | |
| 452 | psa_status_t mbedtls_psa_ecdsa_verify_hash( |
| 453 | const psa_key_attributes_t *attributes, |
| 454 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 455 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 456 | const uint8_t *signature, size_t signature_length) |
| 457 | { |
| 458 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 459 | mbedtls_ecp_keypair *ecp = NULL; |
| 460 | size_t curve_bytes; |
| 461 | mbedtls_mpi r, s; |
| 462 | |
| 463 | (void) alg; |
| 464 | |
| 465 | status = mbedtls_psa_ecp_load_representation(attributes->type, |
| 466 | attributes->bits, |
| 467 | key_buffer, |
| 468 | key_buffer_size, |
| 469 | &ecp); |
| 470 | if (status != PSA_SUCCESS) { |
| 471 | return status; |
| 472 | } |
| 473 | |
| 474 | curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits); |
| 475 | mbedtls_mpi_init(&r); |
| 476 | mbedtls_mpi_init(&s); |
| 477 | |
| 478 | if (signature_length != 2 * curve_bytes) { |
| 479 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 480 | goto cleanup; |
| 481 | } |
| 482 | |
| 483 | status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r, |
| 484 | signature, |
| 485 | curve_bytes)); |
| 486 | if (status != PSA_SUCCESS) { |
| 487 | goto cleanup; |
| 488 | } |
| 489 | |
| 490 | status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s, |
| 491 | signature + curve_bytes, |
| 492 | curve_bytes)); |
| 493 | if (status != PSA_SUCCESS) { |
| 494 | goto cleanup; |
| 495 | } |
| 496 | |
| 497 | status = mbedtls_psa_ecp_load_public_part(ecp); |
| 498 | if (status != PSA_SUCCESS) { |
| 499 | goto cleanup; |
| 500 | } |
| 501 | |
| 502 | status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash, |
| 503 | hash_length, &ecp->Q, |
| 504 | &r, &s)); |
| 505 | cleanup: |
| 506 | mbedtls_mpi_free(&r); |
| 507 | mbedtls_mpi_free(&s); |
| 508 | mbedtls_ecp_keypair_free(ecp); |
| 509 | mbedtls_free(ecp); |
| 510 | |
| 511 | return status; |
| 512 | } |
| 513 | |
| 514 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 515 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 516 | |
| 517 | /****************************************************************/ |
| 518 | /* ECDH Key Agreement */ |
| 519 | /****************************************************************/ |
| 520 | |
| 521 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
| 522 | static psa_status_t ecdh_write_secret(const mbedtls_ecp_group *grp, |
| 523 | const mbedtls_ecp_point *secret, |
| 524 | uint8_t *shared_secret, size_t shared_secret_size, |
| 525 | size_t *shared_secret_length) |
| 526 | { |
| 527 | *shared_secret_length = PSA_BITS_TO_BYTES(grp->pbits); |
| 528 | if (shared_secret_size < *shared_secret_length) { |
| 529 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 530 | } |
| 531 | |
| 532 | return mbedtls_to_psa_error( |
| 533 | mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY ? |
| 534 | mbedtls_mpi_write_binary_le(&secret->X, shared_secret, *shared_secret_length) : |
| 535 | mbedtls_mpi_write_binary(&secret->X, shared_secret, *shared_secret_length)); |
| 536 | } |
| 537 | |
| 538 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 539 | static psa_status_t ecdh_everest_shared_secret( |
| 540 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 541 | const uint8_t *peer_key, size_t peer_key_length, |
| 542 | uint8_t *shared_secret, size_t shared_secret_size, |
| 543 | size_t *shared_secret_length) |
| 544 | { |
| 545 | /* This static function is only called when we know the curve is x25519, |
| 546 | * so we know key_buffer_size is correct unless the keystore is corrupted. |
| 547 | * However even in that case we don't want the consequence to be a memory |
| 548 | * error, so check anyway. This cannot be covered by tests though. */ |
| 549 | if (key_buffer_size != MBEDTLS_X25519_KEY_SIZE_BYTES) { |
| 550 | return PSA_ERROR_INVALID_ARGUMENT; |
| 551 | } |
| 552 | |
| 553 | /* peer_key_length comes from the outside and could be incorrect */ |
| 554 | if (peer_key_length != MBEDTLS_X25519_KEY_SIZE_BYTES) { |
| 555 | return PSA_ERROR_INVALID_ARGUMENT; |
| 556 | } |
| 557 | |
| 558 | *shared_secret_length = MBEDTLS_X25519_KEY_SIZE_BYTES; |
| 559 | if (shared_secret_size < *shared_secret_length) { |
| 560 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 561 | } |
| 562 | |
| 563 | mbedtls_x25519_scalarmult(shared_secret, key_buffer, peer_key); |
| 564 | |
| 565 | /* Check that the shared secret is not zero. This is not required by RFC |
| 566 | * 7748, but ecp.c does it (by checking if the input is a low-order point, |
| 567 | * see ecp_check_bad_points_mx()), and protocols that require contributory |
| 568 | * behaviour need it. Also, it will never happen in normal ECDH usage. */ |
| 569 | uint8_t zero[MBEDTLS_X25519_KEY_SIZE_BYTES] = { 0 }; |
| 570 | if (mbedtls_ct_memcmp(zero, shared_secret, *shared_secret_length) == 0) { |
| 571 | return PSA_ERROR_INVALID_ARGUMENT; |
| 572 | } |
| 573 | |
| 574 | return PSA_SUCCESS; |
| 575 | } |
| 576 | #endif /* MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */ |
| 577 | |
| 578 | psa_status_t mbedtls_psa_key_agreement_ecdh( |
| 579 | const psa_key_attributes_t *attributes, |
| 580 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 581 | psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length, |
| 582 | uint8_t *shared_secret, size_t shared_secret_size, |
| 583 | size_t *shared_secret_length) |
| 584 | { |
| 585 | mbedtls_ecp_keypair *our_key = NULL; |
| 586 | mbedtls_ecp_keypair *their_key = NULL; |
| 587 | mbedtls_ecp_point secret; |
| 588 | mbedtls_ecp_point_init(&secret); |
| 589 | |
| 590 | psa_status_t status; |
| 591 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->type) || |
| 592 | !PSA_ALG_IS_ECDH(alg)) { |
| 593 | return PSA_ERROR_INVALID_ARGUMENT; |
| 594 | } |
| 595 | |
| 596 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 597 | if (attributes->type == PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY) && |
| 598 | attributes->bits == 255) { |
| 599 | return ecdh_everest_shared_secret(key_buffer, key_buffer_size, |
| 600 | peer_key, peer_key_length, |
| 601 | shared_secret, shared_secret_size, |
| 602 | shared_secret_length); |
| 603 | } |
| 604 | #endif /* MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */ |
| 605 | |
| 606 | status = mbedtls_psa_ecp_load_representation( |
| 607 | attributes->type, |
| 608 | attributes->bits, |
| 609 | key_buffer, |
| 610 | key_buffer_size, |
| 611 | &our_key); |
| 612 | if (status != PSA_SUCCESS) { |
| 613 | return status; |
| 614 | } |
| 615 | |
| 616 | size_t bits = 0; |
| 617 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits); |
| 618 | |
| 619 | status = mbedtls_psa_ecp_load_representation( |
| 620 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 621 | bits, |
| 622 | peer_key, |
| 623 | peer_key_length, |
| 624 | &their_key); |
| 625 | if (status != PSA_SUCCESS) { |
| 626 | goto exit; |
| 627 | } |
| 628 | |
| 629 | status = mbedtls_to_psa_error( |
| 630 | mbedtls_ecp_mul(&our_key->grp, &secret, &our_key->d, &their_key->Q, |
| 631 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE)); |
| 632 | if (status != PSA_SUCCESS) { |
| 633 | goto exit; |
| 634 | } |
| 635 | |
| 636 | status = ecdh_write_secret(&our_key->grp, &secret, |
| 637 | shared_secret, shared_secret_size, shared_secret_length); |
| 638 | |
| 639 | exit: |
| 640 | if (status != PSA_SUCCESS) { |
| 641 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); |
| 642 | } |
| 643 | mbedtls_ecp_point_free(&secret); |
| 644 | mbedtls_ecp_keypair_free(their_key); |
| 645 | mbedtls_free(their_key); |
| 646 | mbedtls_ecp_keypair_free(our_key); |
| 647 | mbedtls_free(our_key); |
| 648 | return status; |
| 649 | } |
| 650 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
| 651 | |
| 652 | /****************************************************************/ |
| 653 | /* Interruptible ECC Key Generation */ |
| 654 | /****************************************************************/ |
| 655 | |
| 656 | #if defined(MBEDTLS_ECP_RESTARTABLE) && \ |
| 657 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) |
| 658 | |
| 659 | uint32_t mbedtls_psa_generate_key_iop_get_num_ops( |
| 660 | mbedtls_psa_generate_key_iop_t *operation) |
| 661 | { |
| 662 | return operation->num_ops; |
| 663 | } |
| 664 | |
| 665 | psa_status_t mbedtls_psa_ecp_generate_key_iop_setup( |
| 666 | mbedtls_psa_generate_key_iop_t *operation, |
| 667 | const psa_key_attributes_t *attributes) |
| 668 | { |
| 669 | int status = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 670 | |
| 671 | mbedtls_ecp_keypair_init(&operation->ecp); |
| 672 | |
| 673 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 674 | psa_get_key_type(attributes)); |
| 675 | mbedtls_ecp_group_id grp_id = |
| 676 | mbedtls_ecc_group_from_psa(curve, psa_get_key_bits(attributes)); |
| 677 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
| 678 | return PSA_ERROR_NOT_SUPPORTED; |
| 679 | } |
| 680 | |
| 681 | status = mbedtls_ecp_group_load(&operation->ecp.grp, grp_id); |
| 682 | |
| 683 | return mbedtls_to_psa_error(status); |
| 684 | } |
| 685 | |
| 686 | psa_status_t mbedtls_psa_ecp_generate_key_iop_complete( |
| 687 | mbedtls_psa_generate_key_iop_t *operation, |
| 688 | uint8_t *key_output, |
| 689 | size_t key_output_size, |
| 690 | size_t *key_len) |
| 691 | { |
| 692 | *key_len = 0; |
| 693 | int status = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 694 | |
| 695 | *key_len = PSA_BITS_TO_BYTES(operation->ecp.grp.nbits); |
| 696 | |
| 697 | if (*key_len > key_output_size) { |
| 698 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 699 | } |
| 700 | |
| 701 | status = mbedtls_ecp_gen_privkey(&operation->ecp.grp, &operation->ecp.d, |
| 702 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE); |
| 703 | |
| 704 | if (status != 0) { |
| 705 | return mbedtls_to_psa_error(status); |
| 706 | } |
| 707 | |
| 708 | /* Our implementation of key generation only generates the private key |
| 709 | which doesn't invlolve any ECC arithmetic operations so number of ops |
| 710 | is less than 1 but we round up to 1 to differentiate between num ops of |
| 711 | 0 which means no work has been done this facilitates testing. */ |
| 712 | operation->num_ops = 1; |
| 713 | |
| 714 | status = mbedtls_mpi_write_binary(&operation->ecp.d, key_output, key_output_size); |
| 715 | |
| 716 | return mbedtls_to_psa_error(status); |
| 717 | } |
| 718 | |
| 719 | psa_status_t mbedtls_psa_ecp_generate_key_iop_abort( |
| 720 | mbedtls_psa_generate_key_iop_t *operation) |
| 721 | { |
| 722 | mbedtls_ecp_keypair_free(&operation->ecp); |
| 723 | operation->num_ops = 0; |
| 724 | return PSA_SUCCESS; |
| 725 | } |
| 726 | |
| 727 | #endif /* MBEDTLS_ECP_RESTARTABLE && MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE */ |
| 728 | |
| 729 | #if defined(MBEDTLS_ECP_RESTARTABLE) && \ |
| 730 | (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
| 731 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
| 732 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)) |
| 733 | |
| 734 | uint32_t mbedtls_psa_ecp_export_public_key_iop_get_num_ops( |
| 735 | mbedtls_psa_export_public_key_iop_t *operation) |
| 736 | { |
| 737 | return operation->num_ops; |
| 738 | } |
| 739 | |
| 740 | psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( |
| 741 | mbedtls_psa_export_public_key_iop_t *operation, |
| 742 | uint8_t *key, |
| 743 | size_t key_len, |
| 744 | const psa_key_attributes_t *key_attributes) |
| 745 | { |
| 746 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 747 | |
| 748 | status = mbedtls_psa_ecp_load_representation( |
| 749 | psa_get_key_type(key_attributes), |
| 750 | psa_get_key_bits(key_attributes), |
| 751 | key, |
| 752 | key_len, |
| 753 | &operation->key); |
| 754 | if (status != PSA_SUCCESS) { |
| 755 | goto exit; |
| 756 | } |
| 757 | |
| 758 | mbedtls_ecp_restart_init(&operation->restart_ctx); |
| 759 | operation->num_ops = 0; |
| 760 | |
| 761 | exit: |
| 762 | return status; |
| 763 | } |
| 764 | |
| 765 | psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( |
| 766 | mbedtls_psa_export_public_key_iop_t *operation, |
| 767 | uint8_t *pub_key, |
| 768 | size_t pub_key_size, |
| 769 | size_t *pub_key_len) |
| 770 | { |
| 771 | int ret = 0; |
| 772 | |
| 773 | if (mbedtls_ecp_is_zero(&operation->key->Q)) { |
| 774 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 775 | |
| 776 | ret = mbedtls_ecp_mul_restartable(&operation->key->grp, &operation->key->Q, |
| 777 | &operation->key->d, &operation->key->grp.G, |
| 778 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, |
| 779 | &operation->restart_ctx); |
| 780 | operation->num_ops += operation->restart_ctx.ops_done; |
| 781 | } |
| 782 | |
| 783 | if (ret == 0) { |
| 784 | ret = mbedtls_ecp_write_public_key(operation->key, |
| 785 | MBEDTLS_ECP_PF_UNCOMPRESSED, pub_key_len, |
| 786 | pub_key, pub_key_size); |
| 787 | } |
| 788 | |
| 789 | return mbedtls_to_psa_error(ret); |
| 790 | } |
| 791 | |
| 792 | psa_status_t mbedtls_psa_ecp_export_public_key_iop_abort( |
| 793 | mbedtls_psa_export_public_key_iop_t *operation) |
| 794 | { |
| 795 | mbedtls_ecp_keypair_free(operation->key); |
| 796 | mbedtls_free(operation->key); |
| 797 | mbedtls_ecp_restart_free(&operation->restart_ctx); |
| 798 | operation->num_ops = 0; |
| 799 | return PSA_SUCCESS; |
| 800 | } |
| 801 | |
| 802 | #endif /* MBEDTLS_ECP_RESTARTABLE && \ |
| 803 | (MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT || |
| 804 | MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT || \ |
| 805 | MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 806 | |
| 807 | /****************************************************************/ |
| 808 | /* Interruptible ECC Key Agreement */ |
| 809 | /****************************************************************/ |
| 810 | |
| 811 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 812 | |
| 813 | uint32_t mbedtls_psa_key_agreement_iop_get_num_ops( |
| 814 | mbedtls_psa_key_agreement_interruptible_operation_t *operation) |
| 815 | { |
| 816 | return operation->num_ops; |
| 817 | } |
| 818 | |
| 819 | psa_status_t mbedtls_psa_key_agreement_iop_setup( |
| 820 | mbedtls_psa_key_agreement_interruptible_operation_t *operation, |
| 821 | const psa_key_attributes_t *private_key_attributes, |
| 822 | const uint8_t *private_key_buffer, |
| 823 | size_t private_key_buffer_len, |
| 824 | const uint8_t *peer_key, |
| 825 | size_t peer_key_length) |
| 826 | { |
| 827 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 828 | |
| 829 | /* We need to clear number of ops here in case there was a previous |
| 830 | complete operation which doesn't reset it after finsishing. */ |
| 831 | operation->num_ops = 0; |
| 832 | |
| 833 | psa_key_type_t private_key_type = psa_get_key_type(private_key_attributes); |
| 834 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key_type)) { |
| 835 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 836 | goto exit; |
| 837 | } |
| 838 | |
| 839 | status = mbedtls_psa_ecp_load_representation( |
| 840 | psa_get_key_type(private_key_attributes), |
| 841 | psa_get_key_bits(private_key_attributes), |
| 842 | private_key_buffer, |
| 843 | private_key_buffer_len, |
| 844 | &operation->our_key); |
| 845 | if (status != PSA_SUCCESS) { |
| 846 | goto exit; |
| 847 | } |
| 848 | |
| 849 | status = mbedtls_psa_ecp_load_representation( |
| 850 | PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type), |
| 851 | psa_get_key_bits(private_key_attributes), |
| 852 | peer_key, |
| 853 | peer_key_length, |
| 854 | &operation->their_key); |
| 855 | if (status != PSA_SUCCESS) { |
| 856 | goto exit; |
| 857 | } |
| 858 | |
| 859 | /* mbedtls_psa_ecp_load_representation() calls mbedtls_ecp_check_pubkey() which |
| 860 | takes MBEDTLS_ECP_OPS_CHK amount of ops. */ |
| 861 | operation->num_ops += MBEDTLS_ECP_OPS_CHK; |
| 862 | |
| 863 | exit: |
| 864 | return status; |
| 865 | } |
| 866 | |
| 867 | psa_status_t mbedtls_psa_key_agreement_iop_complete( |
| 868 | mbedtls_psa_key_agreement_interruptible_operation_t *operation, |
| 869 | uint8_t *shared_secret, |
| 870 | size_t shared_secret_size, |
| 871 | size_t *shared_secret_length) |
| 872 | { |
| 873 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 874 | mbedtls_ecp_point secret; |
| 875 | |
| 876 | mbedtls_ecp_point_init(&secret); |
| 877 | |
| 878 | mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); |
| 879 | |
| 880 | status = mbedtls_to_psa_error( |
| 881 | mbedtls_ecp_mul_restartable(&operation->our_key->grp, |
| 882 | &secret, |
| 883 | &operation->our_key->d, |
| 884 | &operation->their_key->Q, |
| 885 | mbedtls_psa_get_random, |
| 886 | MBEDTLS_PSA_RANDOM_STATE, |
| 887 | &operation->rs)); |
| 888 | operation->num_ops += operation->rs.ops_done; |
| 889 | if (status != PSA_SUCCESS) { |
| 890 | goto exit; |
| 891 | } |
| 892 | |
| 893 | status = ecdh_write_secret(&operation->our_key->grp, &secret, |
| 894 | shared_secret, shared_secret_size, shared_secret_length); |
| 895 | |
| 896 | exit: |
| 897 | mbedtls_ecp_point_free(&secret); |
| 898 | |
| 899 | return status; |
| 900 | } |
| 901 | |
| 902 | psa_status_t mbedtls_psa_key_agreement_iop_abort( |
| 903 | mbedtls_psa_key_agreement_interruptible_operation_t *operation) |
| 904 | { |
| 905 | mbedtls_ecp_keypair_free(operation->our_key); |
| 906 | mbedtls_free(operation->our_key); |
| 907 | operation->our_key = NULL; |
| 908 | |
| 909 | mbedtls_ecp_keypair_free(operation->their_key); |
| 910 | mbedtls_free(operation->their_key); |
| 911 | operation->their_key = NULL; |
| 912 | |
| 913 | mbedtls_ecp_restart_free(&operation->rs); |
| 914 | operation->num_ops = 0; |
| 915 | |
| 916 | return PSA_SUCCESS; |
| 917 | } |
| 918 | |
| 919 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
| 920 | |
| 921 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |