2020-11-25 15:25:26 +01:00
|
|
|
/*
|
|
|
|
* PSA ECP layer on top of Mbed TLS crypto
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2020-11-25 15:25:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
|
|
|
#include <psa/crypto.h>
|
|
|
|
#include "psa_crypto_core.h"
|
|
|
|
#include "psa_crypto_ecp.h"
|
2020-11-26 16:36:16 +01:00
|
|
|
#include "psa_crypto_random_impl.h"
|
2024-01-02 13:26:40 +01:00
|
|
|
#include "mbedtls/psa_util.h"
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2020-12-09 16:36:19 +01:00
|
|
|
#include <mbedtls/ecdsa.h>
|
2022-11-04 17:55:57 +01:00
|
|
|
#include <mbedtls/ecdh.h>
|
2020-11-25 15:25:26 +01:00
|
|
|
#include <mbedtls/ecp.h>
|
|
|
|
#include <mbedtls/error.h>
|
|
|
|
|
2023-06-27 16:58:52 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
|
2021-03-13 18:50:11 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
|
2020-12-10 09:35:33 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2023-12-29 11:46:44 +01:00
|
|
|
/* Helper function to verify if the provided EC's family and key bit size are valid.
|
|
|
|
*
|
|
|
|
* Note: "bits" parameter is used both as input and output and it might be updated
|
|
|
|
* in case provided input value is not multiple of 8 ("sloppy" bits).
|
|
|
|
*/
|
|
|
|
static int check_ecc_parameters(psa_ecc_family_t family, size_t *bits)
|
2023-12-21 14:48:31 +01:00
|
|
|
{
|
|
|
|
switch (family) {
|
|
|
|
case PSA_ECC_FAMILY_SECP_R1:
|
2023-12-29 11:46:44 +01:00
|
|
|
switch (*bits) {
|
2023-12-21 14:48:31 +01:00
|
|
|
case 192:
|
|
|
|
case 224:
|
|
|
|
case 256:
|
|
|
|
case 384:
|
|
|
|
case 521:
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
case 528:
|
2023-12-29 11:46:44 +01:00
|
|
|
*bits = 521;
|
|
|
|
return PSA_SUCCESS;
|
2023-12-21 14:48:31 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
|
2023-12-29 11:46:44 +01:00
|
|
|
switch (*bits) {
|
2023-12-21 14:48:31 +01:00
|
|
|
case 256:
|
|
|
|
case 384:
|
|
|
|
case 512:
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PSA_ECC_FAMILY_MONTGOMERY:
|
2023-12-29 11:46:44 +01:00
|
|
|
switch (*bits) {
|
2023-12-21 14:48:31 +01:00
|
|
|
case 448:
|
|
|
|
case 255:
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
case 256:
|
2023-12-29 11:46:44 +01:00
|
|
|
*bits = 255;
|
|
|
|
return PSA_SUCCESS;
|
2023-12-21 14:48:31 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PSA_ECC_FAMILY_SECP_K1:
|
2023-12-29 11:46:44 +01:00
|
|
|
switch (*bits) {
|
2023-12-21 14:48:31 +01:00
|
|
|
case 192:
|
2024-01-09 13:45:05 +01:00
|
|
|
/* secp224k1 is not and will not be supported in PSA (#3541). */
|
2023-12-21 14:48:31 +01:00
|
|
|
case 256:
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:25:26 +01:00
|
|
|
psa_status_t mbedtls_psa_ecp_load_representation(
|
2021-01-27 15:44:45 +01:00
|
|
|
psa_key_type_t type, size_t curve_bits,
|
|
|
|
const uint8_t *data, size_t data_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair **p_ecp)
|
2020-11-25 15:25:26 +01:00
|
|
|
{
|
|
|
|
mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
|
|
|
|
psa_status_t status;
|
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
2021-01-27 15:44:45 +01:00
|
|
|
size_t curve_bytes = data_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
int explicit_bits = (curve_bits != 0);
|
2020-11-25 15:25:26 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
|
|
|
|
PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) {
|
2020-11-25 15:25:26 +01:00
|
|
|
/* A Weierstrass public key is represented as:
|
|
|
|
* - The byte 0x04;
|
|
|
|
* - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
|
|
|
|
* - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
|
|
|
|
* So its data length is 2m+1 where m is the curve size in bits.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((data_length & 1) == 0) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
2021-01-27 15:44:45 +01:00
|
|
|
curve_bytes = data_length / 2;
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* Montgomery public keys are represented in compressed format, meaning
|
2021-02-08 18:39:18 +01:00
|
|
|
* their curve_bytes is equal to the amount of input. */
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* Private keys are represented in uncompressed private random integer
|
2021-02-08 18:39:18 +01:00
|
|
|
* format, meaning their curve_bytes is equal to the amount of input. */
|
2020-11-25 15:25:26 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (explicit_bits) {
|
2021-01-27 15:44:45 +01:00
|
|
|
/* With an explicit bit-size, the data must have the matching length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (curve_bytes != PSA_BITS_TO_BYTES(curve_bits)) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-27 15:44:45 +01:00
|
|
|
/* We need to infer the bit-size from the data. Since the only
|
|
|
|
* information we have is the length in bytes, the value of curve_bits
|
|
|
|
* at this stage is rounded up to the nearest multiple of 8. */
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_bits = PSA_BYTES_TO_BITS(curve_bytes);
|
2021-01-27 15:44:45 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:25:26 +01:00
|
|
|
/* Allocate and initialize a key representation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
|
|
|
|
if (ecp == NULL) {
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
}
|
|
|
|
mbedtls_ecp_keypair_init(ecp);
|
2020-11-25 15:25:26 +01:00
|
|
|
|
2023-12-29 11:46:44 +01:00
|
|
|
status = check_ecc_parameters(PSA_KEY_TYPE_ECC_GET_FAMILY(type), &curve_bits);
|
2023-12-21 14:48:31 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:25:26 +01:00
|
|
|
/* Load the group. */
|
2023-12-21 10:16:33 +01:00
|
|
|
grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(type),
|
2023-12-21 14:03:51 +01:00
|
|
|
curve_bits);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (grp_id == MBEDTLS_ECP_DP_NONE) {
|
2023-12-21 14:03:51 +01:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:25:26 +01:00
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_load(&ecp->grp, grp_id));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-25 15:25:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* Load the key material. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
|
2020-11-25 15:25:26 +01:00
|
|
|
/* Load the public value. */
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q,
|
|
|
|
data,
|
|
|
|
data_length));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-25 15:25:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* Check that the point is on the curve. */
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_check_pubkey(&ecp->grp, &ecp->Q));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-25 15:25:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-25 15:25:26 +01:00
|
|
|
/* Load and validate the secret value. */
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_read_key(ecp->grp.id,
|
|
|
|
ecp,
|
|
|
|
data,
|
|
|
|
data_length));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-25 15:25:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-25 15:25:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*p_ecp = ecp;
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
mbedtls_ecp_keypair_free(ecp);
|
|
|
|
mbedtls_free(ecp);
|
2020-11-25 15:25:26 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-11-25 15:25:26 +01:00
|
|
|
}
|
2023-06-27 16:58:52 +02:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
|
2021-03-13 18:50:11 +01:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
|
2020-12-10 09:35:33 +01:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
|
2020-11-25 15:25:26 +01:00
|
|
|
|
2023-06-27 16:58:52 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
|
2021-03-13 18:50:11 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
|
2020-11-27 18:54:57 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_ecp_import_key(
|
2020-11-27 18:54:57 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *data, size_t data_length,
|
|
|
|
uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *key_buffer_length, size_t *bits)
|
2020-11-27 18:54:57 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
|
|
|
|
|
|
|
/* Parse input */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_ecp_load_representation(attributes->core.type,
|
|
|
|
attributes->core.bits,
|
|
|
|
data,
|
|
|
|
data_length,
|
|
|
|
&ecp);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-27 18:54:57 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-27 18:54:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type) ==
|
|
|
|
PSA_ECC_FAMILY_MONTGOMERY) {
|
2020-11-27 18:54:57 +01:00
|
|
|
*bits = ecp->grp.nbits + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-11-27 18:54:57 +01:00
|
|
|
*bits = ecp->grp.nbits;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-27 18:54:57 +01:00
|
|
|
|
|
|
|
/* Re-export the data to PSA export format. There is currently no support
|
|
|
|
* for other input formats then the export format, so this is a 1-1
|
|
|
|
* copy operation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_ecp_export_key(attributes->core.type,
|
|
|
|
ecp,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
key_buffer_length);
|
2020-11-27 18:54:57 +01:00
|
|
|
exit:
|
|
|
|
/* Always free the PK object (will also free contained ECP context) */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair_free(ecp);
|
|
|
|
mbedtls_free(ecp);
|
2020-11-27 18:54:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-11-27 18:54:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type,
|
|
|
|
mbedtls_ecp_keypair *ecp,
|
|
|
|
uint8_t *data,
|
|
|
|
size_t data_size,
|
|
|
|
size_t *data_length)
|
2020-11-26 16:36:16 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
|
2020-11-26 16:36:16 +01:00
|
|
|
/* Check whether the public part is loaded */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ecp_is_zero(&ecp->Q)) {
|
2020-11-26 16:36:16 +01:00
|
|
|
/* Calculate the public key */
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE));
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-11-26 16:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_point_write_binary(&ecp->grp, &ecp->Q,
|
|
|
|
MBEDTLS_ECP_PF_UNCOMPRESSED,
|
|
|
|
data_length,
|
|
|
|
data,
|
|
|
|
data_size));
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
memset(data, 0, data_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
} else {
|
2020-11-26 16:36:16 +01:00
|
|
|
status = mbedtls_to_psa_error(
|
2024-02-19 16:44:29 +01:00
|
|
|
mbedtls_ecp_write_key_ext(ecp, data_length, data, data_size));
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-11-26 16:36:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_ecp_export_public_key(
|
2020-11-26 16:36:16 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *data, size_t data_size, size_t *data_length)
|
2020-11-26 16:36:16 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
|
|
|
|
|
|
|
status = mbedtls_psa_ecp_load_representation(
|
2021-01-27 15:44:45 +01:00
|
|
|
attributes->core.type, attributes->core.bits,
|
2023-01-11 14:50:10 +01:00
|
|
|
key_buffer, key_buffer_size, &ecp);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-11-26 16:36:16 +01:00
|
|
|
|
|
|
|
status = mbedtls_psa_ecp_export_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_KEY_TYPE_ECC_PUBLIC_KEY(
|
|
|
|
PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type)),
|
|
|
|
ecp, data, data_size, data_length);
|
2020-11-26 16:36:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair_free(ecp);
|
|
|
|
mbedtls_free(ecp);
|
2020-11-26 16:36:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-11-26 16:36:16 +01:00
|
|
|
}
|
2023-06-27 16:58:52 +02:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
|
2021-03-13 18:50:11 +01:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
|
2020-11-26 19:19:10 +01:00
|
|
|
|
2023-06-26 10:05:50 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_ecp_generate_key(
|
2020-11-20 18:17:42 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
|
2020-11-20 18:17:42 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
|
2023-01-11 14:50:10 +01:00
|
|
|
attributes->core.type);
|
2020-11-20 18:17:42 +01:00
|
|
|
mbedtls_ecp_group_id grp_id =
|
2023-12-21 14:03:51 +01:00
|
|
|
mbedtls_ecc_group_from_psa(curve, attributes->core.bits);
|
2020-11-20 18:17:42 +01:00
|
|
|
|
|
|
|
const mbedtls_ecp_curve_info *curve_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_curve_info_from_grp_id(grp_id);
|
2020-11-20 18:17:42 +01:00
|
|
|
mbedtls_ecp_keypair ecp;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2020-11-20 18:17:42 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair_init(&ecp);
|
|
|
|
ret = mbedtls_ecp_gen_key(grp_id, &ecp,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ecp_keypair_free(&ecp);
|
|
|
|
return mbedtls_to_psa_error(ret);
|
2020-11-20 18:17:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2024-02-19 16:44:29 +01:00
|
|
|
mbedtls_ecp_write_key_ext(&ecp, key_buffer_length,
|
|
|
|
key_buffer, key_buffer_size));
|
2020-11-20 18:17:42 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair_free(&ecp);
|
2020-11-20 18:17:42 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-11-20 18:17:42 +01:00
|
|
|
}
|
2023-06-26 10:05:50 +02:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE */
|
2020-11-20 18:17:42 +01:00
|
|
|
|
2020-12-09 16:36:19 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* ECDSA sign/verify */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
|
|
|
psa_status_t mbedtls_psa_ecdsa_sign_hash(
|
2020-12-09 16:36:19 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *signature, size_t signature_size, size_t *signature_length)
|
2020-12-09 16:36:19 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t curve_bytes;
|
|
|
|
mbedtls_mpi r, s;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_ecp_load_representation(attributes->core.type,
|
|
|
|
attributes->core.bits,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
&ecp);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
|
|
|
|
mbedtls_mpi_init(&r);
|
|
|
|
mbedtls_mpi_init(&s);
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (signature_size < 2 * curve_bytes) {
|
2020-12-09 16:36:19 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) {
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
|
2023-03-28 11:38:08 +02:00
|
|
|
mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext(
|
|
|
|
&ecp->grp, &r, &s,
|
|
|
|
&ecp->d, hash,
|
|
|
|
hash_length, md_alg,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE));
|
2021-03-04 11:26:03 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
|
|
|
goto cleanup;
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-12-09 16:36:19 +01:00
|
|
|
(void) alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d,
|
|
|
|
hash, hash_length,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE));
|
2020-12-09 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r,
|
|
|
|
signature,
|
|
|
|
curve_bytes));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s,
|
|
|
|
signature + curve_bytes,
|
|
|
|
curve_bytes));
|
2020-12-09 16:36:19 +01:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&r);
|
|
|
|
mbedtls_mpi_free(&s);
|
|
|
|
if (ret == 0) {
|
2020-12-09 16:36:19 +01:00
|
|
|
*signature_length = 2 * curve_bytes;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair_free(ecp);
|
|
|
|
mbedtls_free(ecp);
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_to_psa_error(ret);
|
2020-12-09 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp)
|
2023-02-06 16:59:09 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* Check whether the public part is loaded. If not, load it. */
|
|
|
|
if (mbedtls_ecp_is_zero(&ecp->Q)) {
|
|
|
|
ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q,
|
|
|
|
&ecp->d, &ecp->grp.G,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE);
|
|
|
|
}
|
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
return mbedtls_to_psa_error(ret);
|
2023-02-06 16:59:09 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_ecdsa_verify_hash(
|
2020-12-09 16:36:19 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint8_t *signature, size_t signature_length)
|
2020-12-09 16:36:19 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
|
|
|
size_t curve_bytes;
|
|
|
|
mbedtls_mpi r, s;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) alg;
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_ecp_load_representation(attributes->core.type,
|
|
|
|
attributes->core.bits,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
&ecp);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
|
|
|
|
mbedtls_mpi_init(&r);
|
|
|
|
mbedtls_mpi_init(&s);
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (signature_length != 2 * curve_bytes) {
|
2023-02-15 18:32:42 +01:00
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
2020-12-09 16:36:19 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r,
|
|
|
|
signature,
|
|
|
|
curve_bytes));
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2023-02-06 16:59:09 +01:00
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s,
|
|
|
|
signature + curve_bytes,
|
|
|
|
curve_bytes));
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
status = mbedtls_psa_ecp_load_public_part(ecp);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash,
|
|
|
|
hash_length, &ecp->Q,
|
|
|
|
&r, &s));
|
2020-12-09 16:36:19 +01:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&r);
|
|
|
|
mbedtls_mpi_free(&s);
|
|
|
|
mbedtls_ecp_keypair_free(ecp);
|
|
|
|
mbedtls_free(ecp);
|
2020-12-09 16:36:19 +01:00
|
|
|
|
2023-02-15 18:32:42 +01:00
|
|
|
return status;
|
2020-12-09 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
2020-12-10 09:35:33 +01:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
|
|
|
|
2022-11-04 17:55:57 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* ECDH Key Agreement */
|
|
|
|
/****************************************************************/
|
2022-11-07 11:43:29 +01:00
|
|
|
|
2022-11-04 17:55:57 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
|
2022-11-07 11:43:29 +01:00
|
|
|
psa_status_t mbedtls_psa_key_agreement_ecdh(
|
2022-11-04 17:55:57 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length,
|
|
|
|
uint8_t *shared_secret, size_t shared_secret_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *shared_secret_length)
|
2022-11-04 17:55:57 +01:00
|
|
|
{
|
2022-11-29 17:53:29 +01:00
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->core.type) ||
|
|
|
|
!PSA_ALG_IS_ECDH(alg)) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
2022-11-04 17:55:57 +01:00
|
|
|
mbedtls_ecp_keypair *ecp = NULL;
|
2022-11-29 17:53:29 +01:00
|
|
|
status = mbedtls_psa_ecp_load_representation(
|
2023-01-11 14:50:10 +01:00
|
|
|
attributes->core.type,
|
|
|
|
attributes->core.bits,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
&ecp);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2022-11-04 17:55:57 +01:00
|
|
|
mbedtls_ecp_keypair *their_key = NULL;
|
|
|
|
mbedtls_ecdh_context ecdh;
|
|
|
|
size_t bits = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ecp->grp.id, &bits);
|
|
|
|
mbedtls_ecdh_init(&ecdh);
|
2022-11-04 17:55:57 +01:00
|
|
|
|
|
|
|
status = mbedtls_psa_ecp_load_representation(
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
|
|
|
|
bits,
|
|
|
|
peer_key,
|
|
|
|
peer_key_length,
|
|
|
|
&their_key);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-11-04 17:55:57 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-04 17:55:57 +01:00
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-11-04 17:55:57 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-04 17:55:57 +01:00
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_get_params(&ecdh, ecp, MBEDTLS_ECDH_OURS));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-11-04 17:55:57 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-04 17:55:57 +01:00
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_calc_secret(&ecdh,
|
|
|
|
shared_secret_length,
|
|
|
|
shared_secret, shared_secret_size,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-11-04 17:55:57 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
|
2022-11-04 17:55:57 +01:00
|
|
|
status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-04 17:55:57 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
mbedtls_platform_zeroize(shared_secret, shared_secret_size);
|
|
|
|
}
|
|
|
|
mbedtls_ecdh_free(&ecdh);
|
|
|
|
mbedtls_ecp_keypair_free(their_key);
|
|
|
|
mbedtls_free(their_key);
|
|
|
|
mbedtls_ecp_keypair_free(ecp);
|
|
|
|
mbedtls_free(ecp);
|
|
|
|
return status;
|
2022-11-04 17:55:57 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
|
|
|
|
|
|
|
|
|
2020-11-25 15:25:26 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|