2013-09-21 12:31:05 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pk.h"
|
2023-05-17 15:37:29 +02:00
|
|
|
#include "pk_internal.h"
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-11-12 00:01:34 +01:00
|
|
|
/* For error codes */
|
2019-01-31 14:20:20 +01:00
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#include "mbedtls/base64.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
#include "mbedtls/rsa.h"
|
2023-05-15 11:18:46 +02:00
|
|
|
#include "pk_internal.h"
|
2014-11-12 00:01:34 +01:00
|
|
|
|
2017-05-08 12:15:49 +02:00
|
|
|
#include <limits.h>
|
2017-06-09 15:26:59 +02:00
|
|
|
#include <stdint.h>
|
2017-02-15 11:52:32 +01:00
|
|
|
|
2019-12-19 00:09:47 +01:00
|
|
|
/* Needed only for test case data under #if defined(MBEDTLS_USE_PSA_CRYPTO),
|
|
|
|
* but the test code generator requires test case data to be valid C code
|
2022-03-31 15:07:01 +02:00
|
|
|
* unconditionally (https://github.com/Mbed-TLS/mbedtls/issues/2023). */
|
2019-12-19 00:09:47 +01:00
|
|
|
#include "psa/crypto.h"
|
2023-03-28 12:33:20 +02:00
|
|
|
#include "md_psa.h"
|
2019-12-19 00:09:47 +01:00
|
|
|
|
2023-04-04 11:47:55 +02:00
|
|
|
/* Used for properly sizing the key buffer in pk_genkey_ec() */
|
2023-06-07 13:07:21 +02:00
|
|
|
#include "psa_util_internal.h"
|
2023-04-03 16:01:47 +02:00
|
|
|
|
2023-07-20 18:26:58 +02:00
|
|
|
#define RSA_KEY_SIZE MBEDTLS_RSA_GEN_KEY_MIN_BITS
|
|
|
|
#define RSA_KEY_LEN (MBEDTLS_RSA_GEN_KEY_MIN_BITS/8)
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2023-09-25 08:13:22 +02:00
|
|
|
/* MBEDTLS_TEST_PK_PSA_SIGN is enabled when:
|
|
|
|
* - The build has PK_[PARSE/WRITE]_C for RSA or ECDSA signature.
|
|
|
|
* - The build has built-in ECC and ECDSA signature.
|
|
|
|
*/
|
|
|
|
#if (defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C) && \
|
|
|
|
((defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)) || \
|
|
|
|
defined(MBEDTLS_PK_CAN_ECDSA_SIGN))) || \
|
|
|
|
(defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_CAN_ECDSA_SIGN))
|
|
|
|
#define MBEDTLS_TEST_PK_PSA_SIGN
|
|
|
|
#endif
|
|
|
|
|
2023-05-17 15:37:29 +02:00
|
|
|
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
|
|
|
|
static int pk_genkey_ec(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
|
2023-04-03 16:01:47 +02:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
size_t curve_bits;
|
2023-05-17 15:37:29 +02:00
|
|
|
psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(grp_id, &curve_bits);
|
2023-04-03 16:01:47 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-05-22 18:39:43 +02:00
|
|
|
if (curve == 0) {
|
|
|
|
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:01:47 +02:00
|
|
|
psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
|
|
|
|
psa_set_key_bits(&key_attr, curve_bits);
|
2023-05-18 19:00:22 +02:00
|
|
|
psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT |
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH |
|
|
|
|
PSA_KEY_USAGE_SIGN_MESSAGE);
|
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
psa_set_key_algorithm(&key_attr, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
|
|
|
|
#else
|
|
|
|
psa_set_key_algorithm(&key_attr, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
|
|
|
|
#endif
|
2023-04-03 16:01:47 +02:00
|
|
|
|
2023-05-18 19:00:22 +02:00
|
|
|
status = psa_generate_key(&key_attr, &pk->priv_id);
|
2023-04-03 16:01:47 +02:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2023-05-18 19:00:22 +02:00
|
|
|
status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
|
2023-05-17 15:37:29 +02:00
|
|
|
&pk->pub_raw_len);
|
2023-04-03 16:01:47 +02:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-04-04 11:19:33 +02:00
|
|
|
ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
|
|
|
goto exit;
|
2023-04-03 16:01:47 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 15:37:29 +02:00
|
|
|
pk->ec_family = curve;
|
|
|
|
pk->ec_bits = curve_bits;
|
2023-04-03 16:01:47 +02:00
|
|
|
|
2023-05-17 15:37:29 +02:00
|
|
|
return 0;
|
2023-04-03 16:01:47 +02:00
|
|
|
|
2023-05-17 15:37:29 +02:00
|
|
|
exit:
|
2023-05-18 19:00:22 +02:00
|
|
|
status = psa_destroy_key(pk->priv_id);
|
2023-05-17 15:37:29 +02:00
|
|
|
return (ret != 0) ? ret : psa_pk_status_to_mbedtls(status);
|
2023-04-03 16:01:47 +02:00
|
|
|
}
|
2023-05-17 15:37:29 +02:00
|
|
|
#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
|
2023-04-03 16:01:47 +02:00
|
|
|
|
2019-11-05 16:42:13 +01:00
|
|
|
/** Generate a key of the desired type.
|
|
|
|
*
|
2023-12-14 20:40:54 +01:00
|
|
|
* \param pk The PK object to fill. It must have been initialized
|
|
|
|
* with mbedtls_pk_setup().
|
|
|
|
* \param curve_or_keybits - For RSA keys, the key size in bits.
|
|
|
|
* - For EC keys, the curve (\c MBEDTLS_ECP_DP_xxx).
|
2019-11-05 16:42:13 +01:00
|
|
|
*
|
2023-12-14 20:40:54 +01:00
|
|
|
* \return The status from the underlying type-specific key
|
|
|
|
* generation function.
|
|
|
|
* \return -1 if the key type is not recognized.
|
2019-11-05 16:42:13 +01:00
|
|
|
*/
|
2023-12-14 20:40:54 +01:00
|
|
|
static int pk_genkey(mbedtls_pk_context *pk, int curve_or_keybits)
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2023-12-14 20:40:54 +01:00
|
|
|
(void) pk;
|
|
|
|
(void) curve_or_keybits;
|
2013-12-19 17:29:52 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
|
|
|
|
return mbedtls_rsa_gen_key(mbedtls_pk_rsa(*pk),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
2023-12-14 20:40:54 +01:00
|
|
|
curve_or_keybits, 3);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-21 12:31:05 +02:00
|
|
|
#endif
|
2023-06-14 14:56:48 +02:00
|
|
|
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY ||
|
|
|
|
mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY_DH ||
|
|
|
|
mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECDSA) {
|
2013-09-21 12:31:05 +02:00
|
|
|
int ret;
|
2023-05-17 15:37:29 +02:00
|
|
|
|
2024-01-23 11:05:34 +01:00
|
|
|
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
|
|
|
|
ret = pk_genkey_ec(pk, curve_or_keybits);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
2023-12-14 20:40:54 +01:00
|
|
|
ret = mbedtls_ecp_group_load(&mbedtls_pk_ec_rw(*pk)->grp, curve_or_keybits);
|
2023-05-17 15:37:29 +02:00
|
|
|
if (ret != 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2023-05-18 19:00:22 +02:00
|
|
|
return mbedtls_ecp_gen_keypair(&mbedtls_pk_ec_rw(*pk)->grp,
|
|
|
|
&mbedtls_pk_ec_rw(*pk)->d,
|
|
|
|
&mbedtls_pk_ec_rw(*pk)->Q,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL);
|
2024-01-23 11:05:34 +01:00
|
|
|
#endif /* MBEDTLS_ECP_C && !MBEDTLS_PK_USE_PSA_EC_DATA */
|
2023-05-17 15:37:29 +02:00
|
|
|
|
2023-04-05 18:21:48 +02:00
|
|
|
}
|
2023-06-14 14:56:48 +02:00
|
|
|
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_rsa_decrypt_func(void *ctx, size_t *olen,
|
|
|
|
const unsigned char *input, unsigned char *output,
|
|
|
|
size_t output_max_len)
|
2014-04-03 22:09:18 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
olen, input, output, output_max_len);
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_rsa_sign_func(void *ctx,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
|
|
|
mbedtls_md_type_t md_alg, unsigned int hashlen,
|
|
|
|
const unsigned char *hash, unsigned char *sig)
|
2014-04-03 22:09:18 +02:00
|
|
|
{
|
2017-05-03 17:43:15 +02:00
|
|
|
((void) f_rng);
|
|
|
|
((void) p_rng);
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
md_alg, hashlen, hash, sig);
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_rsa_key_len_func(void *ctx)
|
2014-04-03 22:09:18 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ((const mbedtls_rsa_context *) ctx)->len;
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
2018-10-31 10:28:01 +01:00
|
|
|
|
2024-01-18 14:11:26 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
static int pk_setup_for_type(mbedtls_pk_type_t pk_type, int want_pair,
|
|
|
|
mbedtls_pk_context *pk, psa_key_type_t *psa_type)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
if (pk_type == MBEDTLS_PK_NONE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
TEST_EQUAL(mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(pk_type)), 0);
|
|
|
|
|
|
|
|
switch (pk_type) {
|
2024-01-18 14:16:27 +01:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
case MBEDTLS_PK_RSA:
|
|
|
|
{
|
|
|
|
*psa_type = PSA_KEY_TYPE_RSA_KEY_PAIR;
|
|
|
|
mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
|
|
|
|
if (want_pair) {
|
|
|
|
TEST_EQUAL(mbedtls_rsa_gen_key(
|
|
|
|
rsa,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
MBEDTLS_RSA_GEN_KEY_MIN_BITS, 65537), 0);
|
|
|
|
} else {
|
|
|
|
unsigned char N[PSA_BITS_TO_BYTES(MBEDTLS_RSA_GEN_KEY_MIN_BITS)] = { 0xff };
|
|
|
|
N[sizeof(N) - 1] = 0x03;
|
2024-01-18 17:47:54 +01:00
|
|
|
const unsigned char E[1] = { 0x03 };
|
2024-01-18 14:16:27 +01:00
|
|
|
TEST_EQUAL(mbedtls_rsa_import_raw(rsa,
|
|
|
|
N, sizeof(N),
|
|
|
|
NULL, 0, NULL, 0, NULL, 0,
|
|
|
|
E, sizeof(E)), 0);
|
|
|
|
TEST_EQUAL(mbedtls_rsa_complete(rsa), 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_RSA_C */
|
|
|
|
|
2024-01-18 17:47:54 +01:00
|
|
|
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
|
|
|
case MBEDTLS_PK_ECKEY:
|
|
|
|
case MBEDTLS_PK_ECKEY_DH:
|
|
|
|
case MBEDTLS_PK_ECDSA:
|
|
|
|
{
|
|
|
|
mbedtls_ecp_group_id grp_id = mbedtls_ecp_grp_id_list()[0];
|
|
|
|
size_t bits;
|
|
|
|
*psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(mbedtls_ecc_group_to_psa(grp_id, &bits));
|
|
|
|
TEST_EQUAL(pk_genkey(pk, grp_id), 0);
|
|
|
|
if (!want_pair) {
|
2024-01-23 11:05:34 +01:00
|
|
|
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
|
|
|
|
psa_key_attributes_t pub_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_set_key_type(&pub_attributes,
|
|
|
|
PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(*psa_type));
|
|
|
|
psa_set_key_usage_flags(&pub_attributes,
|
|
|
|
PSA_KEY_USAGE_EXPORT |
|
|
|
|
PSA_KEY_USAGE_COPY |
|
|
|
|
PSA_KEY_USAGE_VERIFY_MESSAGE |
|
|
|
|
PSA_KEY_USAGE_VERIFY_HASH);
|
|
|
|
psa_set_key_algorithm(&pub_attributes, PSA_ALG_ECDSA_ANY);
|
|
|
|
PSA_ASSERT(psa_destroy_key(pk->priv_id));
|
|
|
|
PSA_ASSERT(psa_import_key(&pub_attributes,
|
|
|
|
pk->pub_raw, pk->pub_raw_len,
|
|
|
|
&pk->priv_id));
|
|
|
|
#else
|
2024-01-18 17:47:54 +01:00
|
|
|
mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
|
|
|
|
mbedtls_mpi_free(&ec->d);
|
2024-01-23 11:05:34 +01:00
|
|
|
#endif
|
2024-01-18 17:47:54 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
|
|
|
|
2024-01-18 14:11:26 +01:00
|
|
|
default:
|
|
|
|
TEST_FAIL("Unknown PK type in test data");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!want_pair) {
|
|
|
|
*psa_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(*psa_type);
|
|
|
|
}
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-31 10:28:01 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
|
|
|
|
/*
|
2022-03-15 14:20:20 +01:00
|
|
|
* Generate an ECC key using PSA and return the key identifier of that key,
|
2019-05-27 14:53:13 +02:00
|
|
|
* or 0 if the key generation failed.
|
2018-10-31 16:22:49 +01:00
|
|
|
* The key uses NIST P-256 and is usable for signing with SHA-256.
|
2018-10-31 10:28:01 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_svc_key_id_t pk_psa_genkey_ecc(void)
|
2018-10-31 10:28:01 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key;
|
2019-05-27 14:53:13 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-12-03 21:19:09 +01:00
|
|
|
const psa_key_type_t type =
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1);
|
2018-10-31 10:28:01 +01:00
|
|
|
const size_t bits = 256;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
|
|
|
|
psa_set_key_type(&attributes, type);
|
|
|
|
psa_set_key_bits(&attributes, bits);
|
|
|
|
PSA_ASSERT(psa_generate_key(&attributes, &key));
|
2018-10-31 10:28:01 +01:00
|
|
|
|
2022-03-15 14:20:20 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return key;
|
2022-03-15 14:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate an RSA key using PSA and return the key identifier of that key,
|
|
|
|
* or 0 if the key generation failed.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_svc_key_id_t pk_psa_genkey_rsa(void)
|
2022-03-15 14:20:20 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
const psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
|
2022-04-08 15:14:40 +02:00
|
|
|
const size_t bits = 1024;
|
2022-03-15 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
|
|
|
|
psa_set_key_type(&attributes, type);
|
|
|
|
psa_set_key_bits(&attributes, bits);
|
|
|
|
PSA_ASSERT(psa_generate_key(&attributes, &key));
|
2022-03-15 14:20:20 +01:00
|
|
|
|
2019-05-27 14:53:13 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return key;
|
2018-10-31 10:28:01 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2013-09-21 12:31:05 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_PK_C
|
2013-09-21 12:31:05 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2022-03-15 14:20:20 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_psa_utils(int key_is_rsa)
|
2018-10-31 09:57:45 +01:00
|
|
|
{
|
2018-10-31 11:14:36 +01:00
|
|
|
mbedtls_pk_context pk, pk2;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key;
|
2019-05-27 14:53:13 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2018-11-06 09:34:30 +01:00
|
|
|
const char * const name = "Opaque";
|
2022-04-08 15:14:40 +02:00
|
|
|
size_t bitlen;
|
2018-10-31 10:36:51 +01:00
|
|
|
|
2018-10-31 11:14:36 +01:00
|
|
|
mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
|
|
|
|
unsigned char b1[1], b2[1];
|
|
|
|
size_t len;
|
|
|
|
mbedtls_pk_debug_item dbg;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
mbedtls_pk_init(&pk2);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup_opaque(&pk, MBEDTLS_SVC_KEY_ID_INIT) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2018-10-31 10:57:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
mbedtls_pk_init(&pk);
|
2018-10-31 10:57:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_is_rsa) {
|
2022-04-13 20:28:52 +02:00
|
|
|
bitlen = 1024; /* hardcoded in genkey() */
|
2022-03-15 14:20:20 +01:00
|
|
|
key = pk_psa_genkey_rsa();
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-04-13 20:28:52 +02:00
|
|
|
bitlen = 256; /* hardcoded in genkey() */
|
2022-03-15 14:20:20 +01:00
|
|
|
key = pk_psa_genkey_ecc();
|
2022-04-08 15:14:40 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_svc_key_id_is_null(key)) {
|
2019-05-27 14:53:13 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-10-31 10:28:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup_opaque(&pk, key) == 0);
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_get_type(&pk) == MBEDTLS_PK_OPAQUE);
|
|
|
|
TEST_ASSERT(strcmp(mbedtls_pk_get_name(&pk), name) == 0);
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk) == bitlen);
|
|
|
|
TEST_ASSERT(mbedtls_pk_get_len(&pk) == bitlen / 8);
|
2018-10-31 10:36:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_is_rsa) {
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, MBEDTLS_PK_ECKEY) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, MBEDTLS_PK_ECDSA) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, MBEDTLS_PK_RSA) == 1);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, MBEDTLS_PK_ECKEY) == 1);
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, MBEDTLS_PK_ECDSA) == 1);
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, MBEDTLS_PK_RSA) == 0);
|
2022-03-15 14:20:20 +01:00
|
|
|
}
|
2018-10-31 10:57:29 +01:00
|
|
|
|
2018-10-31 11:14:36 +01:00
|
|
|
/* unsupported operations: verify, decrypt, encrypt */
|
2023-06-21 16:58:40 +02:00
|
|
|
if (key_is_rsa == 1) {
|
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, md_alg,
|
|
|
|
b1, sizeof(b1), b2, sizeof(b2))
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
|
|
|
} else {
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_decrypt(&pk, b1, sizeof(b1),
|
|
|
|
b2, &len, sizeof(b2),
|
|
|
|
NULL, NULL)
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
2022-04-22 15:02:47 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_encrypt(&pk, b1, sizeof(b1),
|
|
|
|
b2, &len, sizeof(b2),
|
|
|
|
NULL, NULL)
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
2018-10-31 11:14:36 +01:00
|
|
|
|
|
|
|
/* unsupported functions: check_pair, debug */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_is_rsa) {
|
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk2,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk2,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) == 0);
|
|
|
|
}
|
|
|
|
TEST_ASSERT(mbedtls_pk_check_pair(&pk, &pk2,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL)
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
|
|
|
TEST_ASSERT(mbedtls_pk_debug(&pk, &dbg)
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
2018-10-31 11:14:36 +01:00
|
|
|
|
2018-10-31 10:28:01 +01:00
|
|
|
/* test that freeing the context does not destroy the key */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
TEST_ASSERT(PSA_SUCCESS == psa_get_key_attributes(key, &attributes));
|
|
|
|
TEST_ASSERT(PSA_SUCCESS == psa_destroy_key(key));
|
2018-10-31 10:28:01 +01:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk); /* redundant except upon error */
|
|
|
|
mbedtls_pk_free(&pk2);
|
|
|
|
USE_PSA_DONE();
|
2018-10-31 09:57:45 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-05-11 14:11:46 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_can_do_ext(int opaque_key, int key_type, int key_usage, int key_alg,
|
2023-12-14 20:40:54 +01:00
|
|
|
int key_alg2, int curve_or_keybits, int alg_check, int usage_check,
|
2023-01-11 14:50:10 +01:00
|
|
|
int result)
|
2022-05-11 14:11:46 +02:00
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2022-05-11 14:11:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (opaque_key == 1) {
|
|
|
|
psa_set_key_usage_flags(&attributes, key_usage);
|
|
|
|
psa_set_key_algorithm(&attributes, key_alg);
|
|
|
|
if (key_alg2 != 0) {
|
|
|
|
psa_set_key_enrollment_algorithm(&attributes, key_alg2);
|
|
|
|
}
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2023-12-14 20:40:54 +01:00
|
|
|
psa_set_key_bits(&attributes, curve_or_keybits);
|
2022-05-11 14:11:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_generate_key(&attributes, &key));
|
2022-05-11 14:11:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_svc_key_id_is_null(key)) {
|
2022-05-19 18:07:53 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-05-11 14:11:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_setup_opaque(&pk, key), 0);
|
2022-05-11 14:11:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_get_type(&pk), MBEDTLS_PK_OPAQUE);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(key_type)), 0);
|
2023-12-14 20:40:54 +01:00
|
|
|
TEST_EQUAL(pk_genkey(&pk, curve_or_keybits), 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_get_type(&pk), key_type);
|
2022-05-19 18:07:53 +02:00
|
|
|
}
|
2022-05-11 14:11:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_can_do_ext(&pk, alg_check, usage_check), result);
|
2022-05-11 14:11:46 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
PSA_ASSERT(psa_destroy_key(key));
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
USE_PSA_DONE();
|
2022-05-11 14:11:46 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2022-07-29 16:06:52 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pk_invalid_param()
|
|
|
|
{
|
|
|
|
mbedtls_pk_context ctx;
|
|
|
|
mbedtls_pk_type_t pk_type = 0;
|
2022-08-02 15:02:28 +02:00
|
|
|
unsigned char buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t buf_size = sizeof(buf);
|
|
|
|
|
|
|
|
mbedtls_pk_init(&ctx);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_restartable(&ctx, MBEDTLS_MD_NONE,
|
2022-07-29 16:06:52 +02:00
|
|
|
NULL, buf_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
buf, buf_size,
|
|
|
|
NULL));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_restartable(&ctx, MBEDTLS_MD_SHA256,
|
2022-08-30 14:15:55 +02:00
|
|
|
NULL, 0,
|
2023-01-11 14:50:10 +01:00
|
|
|
buf, buf_size,
|
|
|
|
NULL));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_ext(pk_type, NULL,
|
|
|
|
&ctx, MBEDTLS_MD_NONE,
|
|
|
|
NULL, buf_size,
|
|
|
|
buf, buf_size));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_ext(pk_type, NULL,
|
|
|
|
&ctx, MBEDTLS_MD_SHA256,
|
|
|
|
NULL, 0,
|
|
|
|
buf, buf_size));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_sign_restartable(&ctx, MBEDTLS_MD_NONE,
|
|
|
|
NULL, buf_size,
|
|
|
|
buf, buf_size, &buf_size,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_sign_restartable(&ctx, MBEDTLS_MD_SHA256,
|
|
|
|
NULL, 0,
|
|
|
|
buf, buf_size, &buf_size,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL));
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_sign_ext(pk_type, &ctx, MBEDTLS_MD_NONE,
|
|
|
|
NULL, buf_size,
|
|
|
|
buf, buf_size, &buf_size,
|
|
|
|
NULL, NULL));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_sign_ext(pk_type, &ctx, MBEDTLS_MD_SHA256,
|
|
|
|
NULL, 0,
|
|
|
|
buf, buf_size, &buf_size,
|
|
|
|
NULL, NULL));
|
2022-07-29 16:06:52 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&ctx);
|
2023-04-19 14:25:52 +02:00
|
|
|
USE_PSA_DONE();
|
2022-07-29 16:06:52 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void valid_parameters()
|
2019-01-31 14:20:20 +01:00
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
unsigned char buf[1];
|
|
|
|
size_t len;
|
|
|
|
void *options = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
/* In informational functions, we accept NULL where a context pointer
|
|
|
|
* is expected because that's what the library has done forever.
|
|
|
|
* We do not document that NULL is accepted, so we may wish to change
|
|
|
|
* the behavior in a future version. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_get_bitlen(NULL) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_pk_get_len(NULL) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(NULL, MBEDTLS_PK_NONE) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_pk_sign_restartable(&pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof(buf), &len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_pk_sign(&pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof(buf), &len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
|
|
|
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_ASSERT(mbedtls_pk_sign_ext(MBEDTLS_PK_NONE, &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof(buf), &len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify_restartable(&pk,
|
2019-01-31 14:20:20 +01:00
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
2023-01-11 14:50:10 +01:00
|
|
|
buf, sizeof(buf),
|
|
|
|
NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk,
|
2019-01-31 14:20:20 +01:00
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
2023-01-11 14:50:10 +01:00
|
|
|
buf, sizeof(buf)) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_pk_verify_ext(MBEDTLS_PK_NONE, options,
|
|
|
|
&pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof(buf)) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_pk_encrypt(&pk,
|
|
|
|
NULL, 0,
|
|
|
|
NULL, &len, 0,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_pk_decrypt(&pk,
|
|
|
|
NULL, 0,
|
|
|
|
NULL, &len, 0,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PK_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_parse_key(&pk, NULL, 0, NULL, 1,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL) ==
|
|
|
|
MBEDTLS_ERR_PK_KEY_INVALID_FORMAT);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_parse_public_key(&pk, NULL, 0) ==
|
|
|
|
MBEDTLS_ERR_PK_KEY_INVALID_FORMAT);
|
2019-01-31 14:20:20 +01:00
|
|
|
#endif /* MBEDTLS_PK_PARSE_C */
|
2023-04-19 14:25:52 +02:00
|
|
|
USE_PSA_DONE();
|
2019-01-31 14:20:20 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PK_WRITE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void valid_parameters_pkwrite(data_t *key_data)
|
2019-01-31 14:20:20 +01:00
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
|
|
|
|
/* For the write tests to be effective, we need a valid key pair. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_parse_key(&pk,
|
|
|
|
key_data->x, key_data->len, NULL, 0,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL) == 0);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_write_key_der(&pk, NULL, 0) ==
|
|
|
|
MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_write_pubkey_der(&pk, NULL, 0) ==
|
|
|
|
MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_write_key_pem(&pk, NULL, 0) ==
|
|
|
|
MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_write_pubkey_pem(&pk, NULL, 0) ==
|
|
|
|
MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
|
2019-01-31 14:20:20 +01:00
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-04-19 14:25:52 +02:00
|
|
|
USE_PSA_DONE();
|
2019-01-31 14:20:20 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-09-21 12:31:05 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-12-14 20:40:54 +01:00
|
|
|
void pk_utils(int type, int curve_or_keybits, int bitlen, int len, char *name)
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(type)) == 0);
|
2023-12-14 20:40:54 +01:00
|
|
|
TEST_ASSERT(pk_genkey(&pk, curve_or_keybits) == 0);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT((int) mbedtls_pk_get_type(&pk) == type);
|
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, type));
|
|
|
|
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk) == (unsigned) bitlen);
|
|
|
|
TEST_ASSERT(mbedtls_pk_get_len(&pk) == (unsigned) len);
|
|
|
|
TEST_ASSERT(strcmp(mbedtls_pk_get_name(&pk), name) == 0);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-04-04 11:13:06 +02:00
|
|
|
USE_PSA_DONE();
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_pk_check_pair(char *pub_file, char *prv_file, int ret)
|
2014-11-06 16:51:20 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pub, prv, alt;
|
2023-06-01 10:59:42 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
mbedtls_svc_key_id_t opaque_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pub);
|
|
|
|
mbedtls_pk_init(&prv);
|
|
|
|
mbedtls_pk_init(&alt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2023-04-03 14:42:22 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
/* mbedtls_pk_check_pair() returns either PK or ECP error codes depending
|
|
|
|
on MBEDTLS_USE_PSA_CRYPTO so here we dynamically translate between the
|
|
|
|
two */
|
|
|
|
if (ret == MBEDTLS_ERR_ECP_BAD_INPUT_DATA) {
|
|
|
|
ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
|
|
|
}
|
2023-06-01 10:59:42 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-04-03 14:42:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_parse_public_keyfile(&pub, pub_file) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_pk_parse_keyfile(&prv, prv_file, NULL,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL)
|
|
|
|
== 0);
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_check_pair(&pub, &prv,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL)
|
|
|
|
== ret);
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_get_type(&prv) == MBEDTLS_PK_RSA) {
|
|
|
|
TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&alt, mbedtls_pk_rsa(prv),
|
|
|
|
mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func,
|
|
|
|
mbedtls_rsa_key_len_func) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_pk_check_pair(&pub, &alt,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL)
|
|
|
|
== ret);
|
2014-11-08 17:08:08 +01:00
|
|
|
}
|
2014-11-12 00:01:34 +01:00
|
|
|
#endif
|
2023-06-01 10:59:42 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
if (mbedtls_pk_get_type(&prv) == MBEDTLS_PK_ECKEY) {
|
|
|
|
TEST_EQUAL(mbedtls_pk_wrap_as_opaque(&prv, &opaque_key_id,
|
|
|
|
PSA_ALG_ANY_HASH,
|
|
|
|
PSA_KEY_USAGE_EXPORT, 0), 0);
|
|
|
|
TEST_EQUAL(mbedtls_pk_check_pair(&pub, &prv, mbedtls_test_rnd_std_rand,
|
|
|
|
NULL), ret);
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-08 17:08:08 +01:00
|
|
|
|
2023-06-01 10:59:42 +02:00
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_destroy_key(opaque_key_id);
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pub);
|
|
|
|
mbedtls_pk_free(&prv);
|
|
|
|
mbedtls_pk_free(&alt);
|
2022-02-22 15:29:05 +01:00
|
|
|
USE_PSA_DONE();
|
2014-11-06 16:51:20 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-07-08 10:48:18 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_rsa_verify_test_vec(data_t *message_str, int digest, int mod,
|
|
|
|
char *input_N, char *input_E,
|
|
|
|
data_t *result_str, int result)
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx *rs_ctx = NULL;
|
2017-05-08 10:09:36 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx ctx;
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
rs_ctx = &ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_restart_init(rs_ctx);
|
2018-07-02 12:23:23 +02:00
|
|
|
// this setting would ensure restart would happen if ECC was used
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_set_max_ops(1);
|
2017-05-08 10:09:36 +02:00
|
|
|
#endif
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
rsa = mbedtls_pk_rsa(pk);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-01-27 11:45:49 +01:00
|
|
|
rsa->len = mod / 8;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&rsa->N, input_N) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&rsa->E, input_E) == 0);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, digest, message_str->x, 0,
|
|
|
|
result_str->x, mbedtls_pk_get_len(&pk)) == result);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify_restartable(&pk, digest, message_str->x, 0,
|
|
|
|
result_str->x, mbedtls_pk_get_len(
|
|
|
|
&pk), rs_ctx) == result);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2017-05-08 10:09:36 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_restart_free(rs_ctx);
|
2017-05-08 10:09:36 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2022-02-22 15:29:05 +01:00
|
|
|
USE_PSA_DONE();
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-07-08 10:48:18 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_rsa_verify_ext_test_vec(data_t *message_str, int digest,
|
|
|
|
int mod, char *input_N,
|
|
|
|
char *input_E, data_t *result_str,
|
|
|
|
int pk_type, int mgf1_hash_id,
|
|
|
|
int salt_len, int sig_len,
|
|
|
|
int result)
|
2014-06-05 13:41:44 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_pk_rsassa_pss_options pss_opts;
|
2014-06-05 13:41:44 +02:00
|
|
|
void *options;
|
2022-04-15 12:00:16 +02:00
|
|
|
int ret;
|
2014-06-05 13:41:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
2014-06-05 13:41:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
rsa = mbedtls_pk_rsa(pk);
|
2014-06-05 13:41:44 +02:00
|
|
|
|
|
|
|
rsa->len = mod / 8;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&rsa->N, input_N) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&rsa->E, input_E) == 0);
|
2014-06-05 13:41:44 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mgf1_hash_id < 0) {
|
2014-06-05 13:41:44 +02:00
|
|
|
options = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2014-06-05 13:41:44 +02:00
|
|
|
options = &pss_opts;
|
|
|
|
|
|
|
|
pss_opts.mgf1_hash_id = mgf1_hash_id;
|
|
|
|
pss_opts.expected_salt_len = salt_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_verify_ext(pk_type, options, &pk,
|
|
|
|
digest, message_str->x, message_str->len,
|
|
|
|
result_str->x, sig_len);
|
2022-04-15 12:00:16 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (result == MBEDTLS_ERR_RSA_INVALID_PADDING) {
|
2022-04-25 14:38:18 +02:00
|
|
|
/* Mbed TLS distinguishes "invalid padding" from "valid padding but
|
2022-04-22 11:32:18 +02:00
|
|
|
* the rest of the signature is invalid". This has little use in
|
|
|
|
* practice and PSA doesn't report this distinction.
|
|
|
|
* In this case, PSA returns PSA_ERROR_INVALID_SIGNATURE translated
|
2022-04-25 14:38:18 +02:00
|
|
|
* to MBEDTLS_ERR_RSA_VERIFY_FAILED.
|
|
|
|
* However, currently `mbedtls_pk_verify_ext()` may use either the
|
|
|
|
* PSA or the Mbed TLS API, depending on the PSS options used.
|
|
|
|
* So, it may return either INVALID_PADDING or INVALID_SIGNATURE.
|
2022-04-22 11:32:18 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_RSA_VERIFY_FAILED);
|
|
|
|
} else
|
2022-04-15 12:00:16 +02:00
|
|
|
#endif
|
2022-04-22 11:32:18 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(ret, result);
|
2022-04-22 11:32:18 +02:00
|
|
|
}
|
2014-06-05 13:41:44 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-03-17 11:59:12 +01:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2014-06-05 13:41:44 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-01-26 16:53:01 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PK_CAN_ECDSA_VERIFY */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_ec_test_vec(int type, int id, data_t *key, data_t *hash,
|
|
|
|
data_t *sig, int ret)
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-04-19 14:25:52 +02:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(type)) == 0);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&pk, MBEDTLS_PK_ECDSA));
|
2023-05-17 15:37:29 +02:00
|
|
|
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
|
2023-06-14 14:57:46 +02:00
|
|
|
TEST_ASSERT(key->len <= MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
|
|
|
|
memcpy(pk.pub_raw, key->x, key->len);
|
|
|
|
pk.ec_family = mbedtls_ecc_group_to_psa(id, &(pk.ec_bits));
|
|
|
|
pk.pub_raw_len = key->len;
|
2023-05-17 15:37:29 +02:00
|
|
|
#else
|
|
|
|
mbedtls_ecp_keypair *eckey = (mbedtls_ecp_keypair *) mbedtls_pk_ec(pk);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&eckey->grp, id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_point_read_binary(&eckey->grp, &eckey->Q,
|
|
|
|
key->x, key->len) == 0);
|
2023-05-17 15:37:29 +02:00
|
|
|
#endif
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2020-09-08 14:58:28 +02:00
|
|
|
// MBEDTLS_MD_NONE is used since it will be ignored.
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, MBEDTLS_MD_NONE,
|
|
|
|
hash->x, hash->len, sig->x, sig->len) == ret);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
USE_PSA_DONE();
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-05-08 10:09:36 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C:MBEDTLS_ECDSA_DETERMINISTIC */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_sign_verify_restart(int pk_type, int grp_id, char *d_str,
|
|
|
|
char *QX_str, char *QY_str,
|
|
|
|
int md_alg, data_t *hash, data_t *sig_check,
|
|
|
|
int max_ops, int min_restart, int max_restart)
|
2017-05-08 10:09:36 +02:00
|
|
|
{
|
|
|
|
int ret, cnt_restart;
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx rs_ctx;
|
2017-05-08 10:09:36 +02:00
|
|
|
mbedtls_pk_context prv, pub;
|
|
|
|
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
Rm useless use of MD in PK test functions
Same rationale as previous "Rm useless use of MD" commits.
Here the first two test functions were already not depending on MD_C,
but the new version is much simpler, as it avoids having two versions of
the code depending on the value of USE_PSA.
Changes to the data file generated with the following Python script:
import hashlib
suite = 'pk'
functions = {
'pk_rsa_verify_test_vec': (2, 1, True),
'pk_rsa_verify_ext_test_vec': (2, 1, True),
'pk_sign_verify_restart': (6, 7, False),
}
def hash_ctx(s):
if s == 'MBEDTLS_MD_MD5':
return hashlib.md5()
if s == 'MBEDTLS_MD_SHA1':
return hashlib.sha1()
if s == 'MBEDTLS_MD_SHA224':
return hashlib.sha224()
if s == 'MBEDTLS_MD_SHA256':
return hashlib.sha256()
if s == 'MBEDTLS_MD_SHA384':
return hashlib.sha384()
if s == 'MBEDTLS_MD_SHA512':
return hashlib.sha512()
if s == 'MBEDTLS_MD_RIPEMD160':
return hashlib.new("ripemd160")
def fix(l):
parts = l.rstrip().split(":")
fun = parts[0]
if fun not in functions:
return l
(alg_idx, msg_idx, is_hex) = functions[fun]
alg_str = parts[alg_idx]
if alg_str == "MBEDTLS_MD_NONE" or alg_str == "255":
return l
h = hash_ctx(alg_str)
msg_str = parts[msg_idx][1:-1]
msg = bytes.fromhex(msg_str) if is_hex else bytes(msg_str, 'ascii')
h.update(msg)
msg_hash = h.hexdigest()
msg_hash_str = '"' + msg_hash + '"'
parts[msg_idx] = msg_hash_str
return ":".join(parts) + '\n'
filename = 'tests/suites/test_suite_' + suite + '.data'
with open(filename) as f:
lines = f.readlines()
lines = [fix(l) for l in lines]
with open(filename, 'w') as f:
f.writelines(lines)
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2022-07-18 10:55:56 +02:00
|
|
|
size_t slen;
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_restart_init(&rs_ctx);
|
|
|
|
mbedtls_pk_init(&prv);
|
|
|
|
mbedtls_pk_init(&pub);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(sig, 0, sizeof(sig));
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&prv, mbedtls_pk_info_from_type(pk_type)) == 0);
|
2023-05-15 11:18:46 +02:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&mbedtls_pk_ec_rw(prv)->grp, grp_id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&mbedtls_pk_ec_rw(prv)->d, d_str) == 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pub, mbedtls_pk_info_from_type(pk_type)) == 0);
|
2023-05-15 11:18:46 +02:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&mbedtls_pk_ec_rw(pub)->grp, grp_id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_point_read_string(&mbedtls_pk_ec_rw(pub)->Q, 16, QX_str, QY_str) == 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_set_max_ops(max_ops);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
slen = sizeof(sig);
|
2017-05-08 10:09:36 +02:00
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_sign_restartable(&prv, md_alg, hash->x, hash->len,
|
|
|
|
sig, sizeof(sig), &slen,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
&rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(slen == sig_check->len);
|
|
|
|
TEST_ASSERT(memcmp(sig, sig_check->x, slen) == 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_verify_restartable(&pub, md_alg,
|
|
|
|
hash->x, hash->len, sig, slen, &rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
sig[0]++;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_verify_restartable(&pub, md_alg,
|
|
|
|
hash->x, hash->len, sig, slen, &rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
|
|
|
TEST_ASSERT(ret != 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
sig[0]--;
|
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
/* Do we leak memory when aborting? try verify then sign
|
|
|
|
* This test only makes sense when we actually restart */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (min_restart > 0) {
|
|
|
|
ret = mbedtls_pk_verify_restartable(&pub, md_alg,
|
|
|
|
hash->x, hash->len, sig, slen, &rs_ctx);
|
|
|
|
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
|
|
|
mbedtls_pk_restart_free(&rs_ctx);
|
|
|
|
|
|
|
|
slen = sizeof(sig);
|
|
|
|
ret = mbedtls_pk_sign_restartable(&prv, md_alg, hash->x, hash->len,
|
2023-02-02 13:40:50 +01:00
|
|
|
sig, sizeof(sig), &slen,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
&rs_ctx);
|
|
|
|
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
2017-08-28 12:20:39 +02:00
|
|
|
}
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_restart_free(&rs_ctx);
|
|
|
|
mbedtls_pk_free(&prv);
|
|
|
|
mbedtls_pk_free(&pub);
|
2022-02-22 15:29:05 +01:00
|
|
|
USE_PSA_DONE();
|
2017-05-08 10:09:36 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-03-17 11:42:03 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_MD_CAN_SHA256 */
|
2023-12-14 20:40:54 +01:00
|
|
|
void pk_sign_verify(int type, int curve_or_keybits, int sign_ret, int verify_ret)
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-21 12:31:05 +02:00
|
|
|
size_t sig_len;
|
2021-06-22 18:39:53 +02:00
|
|
|
unsigned char hash[32]; // Hard-coded for SHA256
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t hash_len = sizeof(hash);
|
2019-11-05 16:32:32 +01:00
|
|
|
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
2017-05-08 10:09:36 +02:00
|
|
|
void *rs_ctx = NULL;
|
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx ctx;
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
rs_ctx = &ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_restart_init(rs_ctx);
|
2018-07-02 12:23:23 +02:00
|
|
|
/* This value is large enough that the operation will complete in one run.
|
|
|
|
* See comments at the top of ecp_test_vect_restart in
|
|
|
|
* test_suite_ecp.function for estimates of operation counts. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_set_max_ops(42000);
|
2017-05-08 10:09:36 +02:00
|
|
|
#endif
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-04-19 14:25:52 +02:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-02-02 13:40:50 +01:00
|
|
|
memset(hash, 0x2a, sizeof(hash));
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(type)) == 0);
|
2023-12-14 20:40:54 +01:00
|
|
|
TEST_ASSERT(pk_genkey(&pk, curve_or_keybits) == 0);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_sign_restartable(&pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, hash_len,
|
2023-02-02 13:40:50 +01:00
|
|
|
sig, sizeof(sig), &sig_len,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
rs_ctx) == sign_ret);
|
|
|
|
if (sign_ret == 0) {
|
|
|
|
TEST_ASSERT(sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE);
|
|
|
|
} else {
|
2019-11-12 11:08:23 +01:00
|
|
|
sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, hash_len, sig, sig_len) == verify_ret);
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verify_ret == 0) {
|
2017-05-08 10:09:36 +02:00
|
|
|
hash[0]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, hash_len, sig, sig_len) != 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
hash[0]--;
|
|
|
|
|
|
|
|
sig[0]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, hash_len, sig, sig_len) != 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
sig[0]--;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, hash_len,
|
2023-02-02 13:40:50 +01:00
|
|
|
sig, sizeof(sig), &sig_len,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL) == sign_ret);
|
|
|
|
if (sign_ret == 0) {
|
|
|
|
TEST_ASSERT(sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE);
|
|
|
|
} else {
|
2019-11-12 11:08:23 +01:00
|
|
|
sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify_restartable(&pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, hash_len, sig, sig_len, rs_ctx) == verify_ret);
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verify_ret == 0) {
|
2017-05-08 10:09:36 +02:00
|
|
|
hash[0]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify_restartable(&pk, MBEDTLS_MD_SHA256,
|
2023-02-02 13:40:50 +01:00
|
|
|
hash, sizeof(hash), sig, sig_len, rs_ctx) != 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
hash[0]--;
|
|
|
|
|
|
|
|
sig[0]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify_restartable(&pk, MBEDTLS_MD_SHA256,
|
2023-02-02 13:40:50 +01:00
|
|
|
hash, sizeof(hash), sig, sig_len, rs_ctx) != 0);
|
2017-05-08 10:09:36 +02:00
|
|
|
sig[0]--;
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2017-05-08 10:09:36 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_restart_free(rs_ctx);
|
2017-05-08 10:09:36 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-03-22 00:32:04 +01:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_rsa_encrypt_decrypt_test(data_t *message, int mod,
|
|
|
|
char *input_P, char *input_Q,
|
|
|
|
char *input_N, char *input_E,
|
|
|
|
int ret)
|
2013-10-17 12:34:16 +02:00
|
|
|
{
|
2022-02-22 14:26:49 +01:00
|
|
|
unsigned char output[300], result[300];
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2022-02-22 14:26:49 +01:00
|
|
|
mbedtls_mpi N, P, Q, E;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2022-02-22 14:26:49 +01:00
|
|
|
size_t olen, rlen;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
|
|
|
|
mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
memset(output, 0, sizeof(output));
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2022-02-22 14:26:49 +01:00
|
|
|
/* encryption test */
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2022-02-22 14:26:49 +01:00
|
|
|
/* init pk-rsa context */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
rsa = mbedtls_pk_rsa(pk);
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2022-02-22 14:26:49 +01:00
|
|
|
/* load public key */
|
2013-10-17 12:34:16 +02:00
|
|
|
rsa->len = mod / 8;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&rsa->N, input_N) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&rsa->E, input_E) == 0);
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_encrypt(&pk, message->x, message->len,
|
|
|
|
output, &olen, sizeof(output),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info) == ret);
|
2022-02-22 14:26:49 +01:00
|
|
|
|
|
|
|
/* decryption test */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
|
|
|
|
mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
|
2022-02-22 14:26:49 +01:00
|
|
|
|
|
|
|
/* init pk-rsa context */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
rsa = mbedtls_pk_rsa(pk);
|
2022-02-22 14:26:49 +01:00
|
|
|
|
|
|
|
/* load public key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
|
2022-02-22 14:26:49 +01:00
|
|
|
|
|
|
|
/* load private key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_rsa_import(rsa, &N, &P, &Q, NULL, &E) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_rsa_get_len(rsa) == (size_t) (mod / 8));
|
|
|
|
TEST_ASSERT(mbedtls_rsa_complete(rsa) == 0);
|
2022-02-22 14:26:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(result, 0, sizeof(result));
|
2022-02-22 14:26:49 +01:00
|
|
|
rlen = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_decrypt(&pk, output, olen,
|
|
|
|
result, &rlen, sizeof(result),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info) == ret);
|
|
|
|
if (ret == 0) {
|
|
|
|
TEST_ASSERT(rlen == message->len);
|
|
|
|
TEST_ASSERT(memcmp(result, message->x, rlen) == 0);
|
2022-02-22 14:26:49 +01:00
|
|
|
}
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
|
|
|
|
mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
USE_PSA_DONE();
|
2013-10-17 12:34:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_rsa_decrypt_test_vec(data_t *cipher, int mod,
|
|
|
|
char *input_P, char *input_Q,
|
|
|
|
char *input_N, char *input_E,
|
|
|
|
data_t *clear, int ret)
|
2013-10-17 12:34:16 +02:00
|
|
|
{
|
2018-11-21 12:44:31 +01:00
|
|
|
unsigned char output[256];
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2017-08-23 07:32:42 +02:00
|
|
|
mbedtls_mpi N, P, Q, E;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2017-05-30 15:23:15 +02:00
|
|
|
size_t olen;
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
|
|
|
|
mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* init pk-rsa context */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
rsa = mbedtls_pk_rsa(pk);
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* load public key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* load private key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_rsa_import(rsa, &N, &P, &Q, NULL, &E) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_rsa_get_len(rsa) == (size_t) (mod / 8));
|
|
|
|
TEST_ASSERT(mbedtls_rsa_complete(rsa) == 0);
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* decryption test */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, sizeof(output));
|
2013-10-17 12:34:16 +02:00
|
|
|
olen = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_decrypt(&pk, cipher->x, cipher->len,
|
|
|
|
output, &olen, sizeof(output),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info) == ret);
|
|
|
|
if (ret == 0) {
|
|
|
|
TEST_ASSERT(olen == clear->len);
|
|
|
|
TEST_ASSERT(memcmp(output, clear->x, olen) == 0);
|
2013-10-17 12:34:16 +02:00
|
|
|
}
|
|
|
|
|
2022-04-22 15:02:47 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
|
|
|
|
mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
USE_PSA_DONE();
|
2022-04-22 15:02:47 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_wrap_rsa_decrypt_test_vec(data_t *cipher, int mod,
|
|
|
|
char *input_P, char *input_Q,
|
|
|
|
char *input_N, char *input_E,
|
|
|
|
data_t *clear, int ret)
|
2022-04-22 15:02:47 +02:00
|
|
|
{
|
|
|
|
unsigned char output[256];
|
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
|
|
|
mbedtls_mpi N, P, Q, E;
|
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_svc_key_id_t key_id;
|
|
|
|
size_t olen;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
|
|
|
|
mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2022-04-22 15:02:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
|
2022-04-22 15:02:47 +02:00
|
|
|
|
|
|
|
/* init pk-rsa context */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)), 0);
|
|
|
|
rsa = mbedtls_pk_rsa(pk);
|
2022-04-22 15:02:47 +02:00
|
|
|
|
|
|
|
/* load public key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&N, input_N), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&E, input_E), 0);
|
2022-04-22 15:02:47 +02:00
|
|
|
|
|
|
|
/* load private key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&P, input_P), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&Q, input_Q), 0);
|
|
|
|
TEST_EQUAL(mbedtls_rsa_import(rsa, &N, &P, &Q, NULL, &E), 0);
|
|
|
|
TEST_EQUAL(mbedtls_rsa_get_len(rsa), (size_t) (mod / 8));
|
|
|
|
TEST_EQUAL(mbedtls_rsa_complete(rsa), 0);
|
2022-04-22 15:02:47 +02:00
|
|
|
|
|
|
|
/* Turn PK context into an opaque one. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_wrap_as_opaque(&pk, &key_id,
|
|
|
|
PSA_ALG_RSA_PKCS1V15_CRYPT,
|
|
|
|
PSA_KEY_USAGE_DECRYPT,
|
|
|
|
PSA_ALG_NONE), 0);
|
2022-04-22 15:02:47 +02:00
|
|
|
|
|
|
|
/* decryption test */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, sizeof(output));
|
2022-04-22 15:02:47 +02:00
|
|
|
olen = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_decrypt(&pk, cipher->x, cipher->len,
|
|
|
|
output, &olen, sizeof(output),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info), ret);
|
|
|
|
if (ret == 0) {
|
|
|
|
TEST_EQUAL(olen, clear->len);
|
|
|
|
TEST_EQUAL(memcmp(output, clear->x, olen), 0);
|
2022-04-22 15:02:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(PSA_SUCCESS, psa_destroy_key(key_id));
|
2022-04-22 15:02:47 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
|
|
|
|
mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
USE_PSA_DONE();
|
2013-10-17 12:34:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2013-10-17 12:46:39 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_ec_nocrypt(int type)
|
2013-10-17 12:46:39 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-10-17 12:46:39 +02:00
|
|
|
unsigned char output[100];
|
|
|
|
unsigned char input[100];
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2013-10-17 12:46:39 +02:00
|
|
|
size_t olen = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
memset(output, 0, sizeof(output));
|
|
|
|
memset(input, 0, sizeof(input));
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(type)) == 0);
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_encrypt(&pk, input, sizeof(input),
|
|
|
|
output, &olen, sizeof(output),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info) == ret);
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_decrypt(&pk, input, sizeof(input),
|
|
|
|
output, &olen, sizeof(output),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info) == ret);
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-04-19 14:25:52 +02:00
|
|
|
USE_PSA_DONE();
|
2013-10-17 12:46:39 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2017-05-08 12:15:49 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_rsa_overflow()
|
2017-02-15 11:52:32 +01:00
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
2023-04-24 09:19:45 +02:00
|
|
|
size_t hash_len = UINT_MAX + 1, sig_len = UINT_MAX + 1;
|
2018-02-26 11:02:10 +01:00
|
|
|
unsigned char hash[50], sig[100];
|
2017-05-08 12:15:49 +02:00
|
|
|
|
2023-04-20 11:59:52 +02:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-19 14:25:52 +02:00
|
|
|
USE_PSA_INIT();
|
2023-04-20 11:59:52 +02:00
|
|
|
|
2023-02-02 13:40:50 +01:00
|
|
|
memset(hash, 0x2a, sizeof(hash));
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2017-02-15 11:52:32 +01:00
|
|
|
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)), 0);
|
2017-02-15 11:52:32 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PKCS1_V21)
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_verify_ext(MBEDTLS_PK_RSASSA_PSS, NULL, &pk,
|
|
|
|
MBEDTLS_MD_NONE, hash, hash_len, sig, sig_len),
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2017-02-15 11:52:32 +01:00
|
|
|
#endif /* MBEDTLS_PKCS1_V21 */
|
|
|
|
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_verify(&pk, MBEDTLS_MD_NONE, hash, hash_len,
|
|
|
|
sig, sig_len),
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2017-02-15 11:52:32 +01:00
|
|
|
|
2023-07-16 12:06:06 +02:00
|
|
|
#if defined(MBEDTLS_PKCS1_V21)
|
|
|
|
TEST_EQUAL(mbedtls_pk_sign_ext(MBEDTLS_PK_RSASSA_PSS, &pk,
|
|
|
|
MBEDTLS_MD_NONE, hash, hash_len,
|
|
|
|
sig, sizeof(sig), &sig_len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL),
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
|
|
|
#endif /* MBEDTLS_PKCS1_V21 */
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_pk_sign(&pk, MBEDTLS_MD_NONE, hash, hash_len,
|
|
|
|
sig, sizeof(sig), &sig_len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL),
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2017-02-15 11:52:32 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-04-19 14:25:52 +02:00
|
|
|
USE_PSA_DONE();
|
2017-02-15 11:52:32 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_PK_RSA_ALT_SUPPORT */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pk_rsa_alt()
|
2014-04-03 22:09:18 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* An rsa_alt context can only do private operations (decrypt, sign).
|
|
|
|
* Test it against the public operations (encrypt, verify) of a
|
|
|
|
* corresponding rsa context.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context raw;
|
|
|
|
mbedtls_pk_context rsa, alt;
|
|
|
|
mbedtls_pk_debug_item dbg_items[10];
|
2023-06-30 17:48:19 +02:00
|
|
|
unsigned char hash[50], sig[RSA_KEY_LEN];
|
|
|
|
unsigned char msg[50], ciph[RSA_KEY_LEN], test[50];
|
2014-04-03 22:09:18 +02:00
|
|
|
size_t sig_len, ciph_len, test_len;
|
2015-04-08 12:49:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_init(&raw);
|
2023-04-20 11:59:52 +02:00
|
|
|
mbedtls_pk_init(&rsa);
|
|
|
|
mbedtls_pk_init(&alt);
|
|
|
|
USE_PSA_INIT();
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2023-02-02 13:40:50 +01:00
|
|
|
memset(hash, 0x2a, sizeof(hash));
|
|
|
|
memset(sig, 0, sizeof(sig));
|
|
|
|
memset(msg, 0x2a, sizeof(msg));
|
|
|
|
memset(ciph, 0, sizeof(ciph));
|
|
|
|
memset(test, 0, sizeof(test));
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2021-12-21 06:14:10 +01:00
|
|
|
/* Initialize PK RSA context with random key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&rsa,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
TEST_ASSERT(pk_genkey(&rsa, RSA_KEY_SIZE) == 0);
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Extract key to the raw rsa context */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_rsa_copy(&raw, mbedtls_pk_rsa(rsa)) == 0);
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Initialize PK RSA_ALT context */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&alt, (void *) &raw,
|
|
|
|
mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func,
|
|
|
|
mbedtls_rsa_key_len_func) == 0);
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Test administrative functions */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_can_do(&alt, MBEDTLS_PK_RSA));
|
|
|
|
TEST_ASSERT(mbedtls_pk_get_bitlen(&alt) == RSA_KEY_SIZE);
|
|
|
|
TEST_ASSERT(mbedtls_pk_get_len(&alt) == RSA_KEY_LEN);
|
|
|
|
TEST_ASSERT(mbedtls_pk_get_type(&alt) == MBEDTLS_PK_RSA_ALT);
|
|
|
|
TEST_ASSERT(strcmp(mbedtls_pk_get_name(&alt), "RSA-alt") == 0);
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2024-01-18 14:11:26 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&alt,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT,
|
|
|
|
&attributes),
|
|
|
|
MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE);
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
|
|
|
|
2014-04-03 22:09:18 +02:00
|
|
|
/* Test signature */
|
2023-02-11 18:14:54 +01:00
|
|
|
#if SIZE_MAX > UINT_MAX
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_sign(&alt, MBEDTLS_MD_NONE, hash, SIZE_MAX,
|
2023-02-02 13:40:50 +01:00
|
|
|
sig, sizeof(sig), &sig_len,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand, NULL)
|
|
|
|
== MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
2023-02-11 18:14:54 +01:00
|
|
|
#endif /* SIZE_MAX > UINT_MAX */
|
2023-02-02 13:40:50 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_sign(&alt, MBEDTLS_MD_NONE, hash, sizeof(hash),
|
|
|
|
sig, sizeof(sig), &sig_len,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand, NULL)
|
|
|
|
== 0);
|
|
|
|
TEST_ASSERT(sig_len == RSA_KEY_LEN);
|
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&rsa, MBEDTLS_MD_NONE,
|
2023-02-02 13:40:50 +01:00
|
|
|
hash, sizeof(hash), sig, sig_len) == 0);
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Test decrypt */
|
2023-02-02 13:40:50 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_encrypt(&rsa, msg, sizeof(msg),
|
|
|
|
ciph, &ciph_len, sizeof(ciph),
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand, NULL) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_pk_decrypt(&alt, ciph, ciph_len,
|
2023-02-02 13:40:50 +01:00
|
|
|
test, &test_len, sizeof(test),
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand, NULL) == 0);
|
2023-02-02 13:40:50 +01:00
|
|
|
TEST_ASSERT(test_len == sizeof(msg));
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(test, msg, test_len) == 0);
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Test forbidden operations */
|
2023-02-02 13:40:50 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_encrypt(&alt, msg, sizeof(msg),
|
|
|
|
ciph, &ciph_len, sizeof(ciph),
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_std_rand, NULL) == ret);
|
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&alt, MBEDTLS_MD_NONE,
|
2023-02-02 13:40:50 +01:00
|
|
|
hash, sizeof(hash), sig, sig_len) == ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_debug(&alt, dbg_items) == ret);
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(&raw);
|
|
|
|
mbedtls_pk_free(&rsa); mbedtls_pk_free(&alt);
|
|
|
|
USE_PSA_DONE();
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2023-09-25 08:13:22 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_MD_CAN_SHA256:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_TEST_PK_PSA_SIGN */
|
2023-12-14 20:40:54 +01:00
|
|
|
void pk_psa_sign(int curve_or_keybits, int psa_type, int expected_bits)
|
2018-10-31 16:22:49 +01:00
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
2019-12-19 00:09:47 +01:00
|
|
|
unsigned char hash[32];
|
2022-03-15 14:48:52 +01:00
|
|
|
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
2019-12-19 00:09:47 +01:00
|
|
|
unsigned char pkey_legacy[200];
|
|
|
|
unsigned char pkey_psa[200];
|
2019-02-01 12:48:19 +01:00
|
|
|
unsigned char *pkey_legacy_start, *pkey_psa_start;
|
2022-04-22 13:57:44 +02:00
|
|
|
psa_algorithm_t alg_psa;
|
2019-02-01 12:48:19 +01:00
|
|
|
size_t sig_len, klen_legacy, klen_psa;
|
|
|
|
int ret;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key_id;
|
2019-12-18 20:44:15 +01:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-10-31 16:22:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This tests making signatures with a wrapped PSA key:
|
2022-03-15 14:48:52 +01:00
|
|
|
* - generate a fresh ECP/RSA legacy PK context
|
2018-10-31 16:22:49 +01:00
|
|
|
* - wrap it in a PK context and make a signature this way
|
|
|
|
* - extract the public key
|
|
|
|
* - parse it to a PK context and verify the signature this way
|
|
|
|
*/
|
|
|
|
|
2023-04-20 11:59:52 +02:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-19 14:25:52 +02:00
|
|
|
USE_PSA_INIT();
|
2019-08-01 12:47:40 +02:00
|
|
|
|
2022-03-15 14:48:52 +01:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
|
2023-12-14 20:40:54 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_RSA(psa_type)) {
|
2022-03-15 14:48:52 +01:00
|
|
|
/* Create legacy RSA public/private key in PK context. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_rsa_gen_key(mbedtls_pk_rsa(pk),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
2023-12-14 20:40:54 +01:00
|
|
|
curve_or_keybits, 3) == 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
|
|
|
|
} else
|
2022-03-15 14:48:52 +01:00
|
|
|
#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
|
2023-01-26 16:53:01 +01:00
|
|
|
#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
|
2023-12-14 20:40:54 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(psa_type)) {
|
|
|
|
mbedtls_ecp_group_id grpid = curve_or_keybits;
|
2022-03-15 14:48:52 +01:00
|
|
|
|
|
|
|
/* Create legacy EC public/private key in PK context. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) == 0);
|
2023-04-03 16:01:47 +02:00
|
|
|
TEST_ASSERT(pk_genkey(&pk, grpid) == 0);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
alg_psa = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
|
|
|
|
} else
|
2023-01-26 16:53:01 +01:00
|
|
|
#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
|
2022-03-15 14:48:52 +01:00
|
|
|
{
|
2023-12-14 20:40:54 +01:00
|
|
|
(void) curve_or_keybits;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSUME(!"Opaque PK key not supported in this configuration");
|
2022-03-15 14:48:52 +01:00
|
|
|
}
|
2019-02-01 12:48:19 +01:00
|
|
|
|
|
|
|
/* Export underlying public key for re-importing in a legacy context. */
|
2023-02-02 15:21:48 +01:00
|
|
|
#if defined(MBEDTLS_PK_WRITE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_write_pubkey_der(&pk, pkey_legacy,
|
|
|
|
sizeof(pkey_legacy));
|
|
|
|
TEST_ASSERT(ret >= 0);
|
2019-02-01 12:48:19 +01:00
|
|
|
klen_legacy = (size_t) ret;
|
|
|
|
/* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
pkey_legacy_start = pkey_legacy + sizeof(pkey_legacy) - klen_legacy;
|
2023-02-02 15:21:48 +01:00
|
|
|
#else
|
2023-05-15 11:18:46 +02:00
|
|
|
ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(pk)->grp),
|
|
|
|
&(mbedtls_pk_ec_ro(pk)->Q),
|
2023-02-08 09:52:40 +01:00
|
|
|
MBEDTLS_ECP_PF_UNCOMPRESSED,
|
|
|
|
&klen_legacy, pkey_legacy,
|
|
|
|
sizeof(pkey_legacy));
|
2023-02-02 15:21:48 +01:00
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
pkey_legacy_start = pkey_legacy;
|
|
|
|
#endif /* MBEDTLS_PK_WRITE_C */
|
2019-02-01 12:48:19 +01:00
|
|
|
|
|
|
|
/* Turn PK context into an opaque one. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&pk, &key_id, alg_psa,
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH,
|
|
|
|
PSA_ALG_NONE) == 0);
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(key_id, &attributes));
|
2023-12-14 20:40:54 +01:00
|
|
|
TEST_EQUAL(psa_get_key_type(&attributes), (psa_key_type_t) psa_type);
|
|
|
|
TEST_EQUAL(psa_get_key_bits(&attributes), (size_t) expected_bits);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_get_key_lifetime(&attributes),
|
|
|
|
PSA_KEY_LIFETIME_VOLATILE);
|
2019-12-18 20:44:15 +01:00
|
|
|
|
2023-02-02 13:40:50 +01:00
|
|
|
memset(hash, 0x2a, sizeof(hash));
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256,
|
2023-02-02 13:40:50 +01:00
|
|
|
hash, sizeof(hash), sig, sizeof(sig), &sig_len,
|
2023-01-11 14:50:10 +01:00
|
|
|
NULL, NULL) == 0);
|
2023-06-21 16:58:40 +02:00
|
|
|
/* Only opaque EC keys support verification. */
|
2023-12-14 20:40:54 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(psa_type)) {
|
2023-06-21 16:58:40 +02:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof(hash), sig, sig_len) == 0);
|
|
|
|
}
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2019-02-01 12:48:19 +01:00
|
|
|
/* Export underlying public key for re-importing in a psa context. */
|
2023-02-02 15:21:48 +01:00
|
|
|
#if defined(MBEDTLS_PK_WRITE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_write_pubkey_der(&pk, pkey_psa,
|
|
|
|
sizeof(pkey_psa));
|
|
|
|
TEST_ASSERT(ret >= 0);
|
2019-02-01 12:48:19 +01:00
|
|
|
klen_psa = (size_t) ret;
|
|
|
|
/* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
pkey_psa_start = pkey_psa + sizeof(pkey_psa) - klen_psa;
|
2023-02-02 15:21:48 +01:00
|
|
|
#else
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
status = psa_export_public_key(key_id, pkey_psa, sizeof(pkey_psa),
|
2023-02-08 09:52:40 +01:00
|
|
|
&klen_psa);
|
2023-02-02 15:21:48 +01:00
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
pkey_psa_start = pkey_psa;
|
|
|
|
#endif /* MBEDTLS_PK_WRITE_C */
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(klen_psa == klen_legacy);
|
|
|
|
TEST_ASSERT(memcmp(pkey_psa_start, pkey_legacy_start, klen_psa) == 0);
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
TEST_ASSERT(PSA_SUCCESS == psa_destroy_key(key_id));
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-02-02 15:21:48 +01:00
|
|
|
|
|
|
|
/* If we used "pk_write" previously, then we go for a "pk_parse" here;
|
|
|
|
* otherwise if we went for "ecp_point_write_binary" then we'll go
|
|
|
|
* for a "ecp_point_read_binary" here. This allows to drop dependencies
|
|
|
|
* on "PK_WRITE" and "PK_PARSE" if required */
|
|
|
|
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_PK_PARSE_C)
|
|
|
|
TEST_EQUAL(mbedtls_pk_parse_public_key(&pk, pkey_legacy_start,
|
2023-02-08 09:52:40 +01:00
|
|
|
klen_legacy), 0);
|
2023-02-02 15:21:48 +01:00
|
|
|
#else
|
|
|
|
TEST_EQUAL(mbedtls_pk_setup(&pk,
|
2023-02-08 09:52:40 +01:00
|
|
|
mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)), 0);
|
2023-02-02 15:21:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecp_group_load(
|
2023-05-15 11:18:46 +02:00
|
|
|
&(mbedtls_pk_ec_rw(pk)->grp),
|
2023-12-14 20:40:54 +01:00
|
|
|
(mbedtls_ecp_group_id) curve_or_keybits), 0);
|
2023-05-15 11:18:46 +02:00
|
|
|
TEST_EQUAL(mbedtls_ecp_point_read_binary(&(mbedtls_pk_ec_ro(pk)->grp),
|
|
|
|
&(mbedtls_pk_ec_rw(pk)->Q),
|
2023-02-08 09:52:40 +01:00
|
|
|
pkey_legacy_start, klen_legacy), 0);
|
2023-02-02 15:21:48 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256,
|
2023-02-02 13:40:50 +01:00
|
|
|
hash, sizeof(hash), sig, sig_len) == 0);
|
2018-10-31 16:22:49 +01:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
USE_PSA_DONE();
|
2018-10-31 16:22:49 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-03-12 12:24:50 +01:00
|
|
|
|
2023-07-16 12:06:06 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2023-12-14 20:40:54 +01:00
|
|
|
void pk_sign_ext(int pk_type, int curve_or_keybits, int key_pk_type, int md_alg)
|
2022-03-12 12:24:50 +01:00
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
size_t sig_len;
|
|
|
|
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
2023-07-16 12:06:06 +02:00
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
2023-03-28 11:14:24 +02:00
|
|
|
size_t hash_len = mbedtls_md_get_size_from_type(md_alg);
|
2022-03-22 06:20:39 +01:00
|
|
|
void const *options = NULL;
|
|
|
|
mbedtls_pk_rsassa_pss_options rsassa_pss_options;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(hash, 0x2a, sizeof(hash));
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2022-03-12 12:24:50 +01:00
|
|
|
|
2023-04-19 14:25:52 +02:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-07-16 12:06:06 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
2022-03-12 12:24:50 +01:00
|
|
|
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(pk_type)), 0);
|
2023-12-14 20:40:54 +01:00
|
|
|
TEST_EQUAL(pk_genkey(&pk, curve_or_keybits), 0);
|
2022-03-12 12:24:50 +01:00
|
|
|
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_sign_ext(key_pk_type, &pk, md_alg, hash, hash_len,
|
|
|
|
sig, sizeof(sig), &sig_len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL), 0);
|
2022-03-22 16:00:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_pk_type == MBEDTLS_PK_RSASSA_PSS) {
|
2022-03-22 06:20:39 +01:00
|
|
|
rsassa_pss_options.mgf1_hash_id = md_alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(hash_len != 0);
|
2022-07-05 12:09:04 +02:00
|
|
|
rsassa_pss_options.expected_salt_len = hash_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
options = (const void *) &rsassa_pss_options;
|
2022-03-22 06:20:39 +01:00
|
|
|
}
|
2023-07-16 12:06:06 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_verify_ext(key_pk_type, options, &pk, md_alg,
|
|
|
|
hash, hash_len, sig, sig_len), 0);
|
2022-03-12 12:24:50 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-07-16 12:06:06 +02:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2022-03-12 12:24:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-04-12 15:12:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_USE_PSA_CRYPTO */
|
2023-12-14 20:40:54 +01:00
|
|
|
void pk_psa_wrap_sign_ext(int pk_type, int key_bits, int key_pk_type, int md_alg)
|
2022-04-12 15:12:43 +02:00
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
size_t sig_len, pkey_len;
|
|
|
|
mbedtls_svc_key_id_t key_id;
|
|
|
|
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
2022-04-21 12:23:28 +02:00
|
|
|
unsigned char pkey[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
|
2022-04-12 15:12:43 +02:00
|
|
|
unsigned char *pkey_start;
|
2022-07-08 10:33:44 +02:00
|
|
|
unsigned char hash[PSA_HASH_MAX_SIZE];
|
2023-03-28 11:38:08 +02:00
|
|
|
psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
|
2022-04-22 13:57:44 +02:00
|
|
|
psa_algorithm_t psa_alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t hash_len = PSA_HASH_LENGTH(psa_md_alg);
|
2022-04-12 15:12:43 +02:00
|
|
|
void const *options = NULL;
|
|
|
|
mbedtls_pk_rsassa_pss_options rsassa_pss_options;
|
|
|
|
int ret;
|
|
|
|
|
2023-04-19 14:25:52 +02:00
|
|
|
mbedtls_pk_init(&pk);
|
2023-04-20 11:59:52 +02:00
|
|
|
PSA_INIT();
|
2022-04-12 15:12:43 +02:00
|
|
|
|
|
|
|
/* Create legacy RSA public/private key in PK context. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
TEST_EQUAL(mbedtls_pk_setup(&pk,
|
|
|
|
mbedtls_pk_info_from_type(pk_type)), 0);
|
|
|
|
TEST_EQUAL(mbedtls_rsa_gen_key(mbedtls_pk_rsa(pk),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
2023-12-14 20:40:54 +01:00
|
|
|
key_bits, 3), 0);
|
2022-04-12 15:12:43 +02:00
|
|
|
|
|
|
|
/* Export underlying public key for re-importing in a legacy context. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_write_pubkey_der(&pk, pkey, sizeof(pkey));
|
|
|
|
TEST_ASSERT(ret >= 0);
|
2022-04-12 15:12:43 +02:00
|
|
|
|
|
|
|
pkey_len = (size_t) ret;
|
|
|
|
/* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
pkey_start = pkey + sizeof(pkey) - pkey_len;
|
|
|
|
|
|
|
|
if (key_pk_type == MBEDTLS_PK_RSA) {
|
|
|
|
psa_alg = PSA_ALG_RSA_PKCS1V15_SIGN(psa_md_alg);
|
|
|
|
} else if (key_pk_type == MBEDTLS_PK_RSASSA_PSS) {
|
|
|
|
psa_alg = PSA_ALG_RSA_PSS(psa_md_alg);
|
|
|
|
} else {
|
|
|
|
TEST_ASSUME(!"PK key type not supported in this configuration");
|
|
|
|
}
|
2022-04-22 13:57:44 +02:00
|
|
|
|
2022-04-12 15:12:43 +02:00
|
|
|
/* Turn PK context into an opaque one. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_wrap_as_opaque(&pk, &key_id, psa_alg,
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH,
|
|
|
|
PSA_ALG_NONE), 0);
|
2022-04-12 15:12:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(hash, 0x2a, sizeof(hash));
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2022-04-12 15:12:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_sign_ext(key_pk_type, &pk, md_alg, hash, hash_len,
|
|
|
|
sig, sizeof(sig), &sig_len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL), 0);
|
2022-04-12 15:12:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
TEST_EQUAL(PSA_SUCCESS, psa_destroy_key(key_id));
|
2022-04-12 15:12:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
TEST_EQUAL(mbedtls_pk_parse_public_key(&pk, pkey_start, pkey_len), 0);
|
2022-04-12 15:12:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_pk_type == MBEDTLS_PK_RSASSA_PSS) {
|
2022-04-12 15:12:43 +02:00
|
|
|
rsassa_pss_options.mgf1_hash_id = md_alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(hash_len != 0);
|
2022-07-05 13:11:13 +02:00
|
|
|
rsassa_pss_options.expected_salt_len = hash_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
options = (const void *) &rsassa_pss_options;
|
2022-04-12 15:12:43 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_verify_ext(key_pk_type, options, &pk, md_alg,
|
|
|
|
hash, hash_len, sig, sig_len), 0);
|
2022-04-12 15:12:43 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
PSA_DONE();
|
2022-04-12 15:12:43 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2024-01-18 14:11:26 +01:00
|
|
|
|
2024-01-18 14:16:27 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */
|
|
|
|
void pk_get_psa_attributes(int pk_type, int from_pair,
|
|
|
|
int usage_arg,
|
|
|
|
int to_pair, int expected_alg)
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_usage_t usage = usage_arg;
|
|
|
|
|
|
|
|
MD_OR_USE_PSA_INIT();
|
|
|
|
|
|
|
|
psa_key_type_t expected_psa_type = 0;
|
|
|
|
if (!pk_setup_for_type(pk_type, from_pair, &pk, &expected_psa_type)) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (!to_pair) {
|
|
|
|
expected_psa_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(expected_psa_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE; //TODO: diversity
|
|
|
|
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; //TODO: diversity
|
|
|
|
psa_set_key_id(&attributes, key_id);
|
|
|
|
psa_set_key_lifetime(&attributes, lifetime);
|
|
|
|
|
|
|
|
psa_key_usage_t expected_usage = usage;
|
|
|
|
/* Usage implied universally */
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_SIGN_HASH) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
|
|
|
|
}
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
|
|
|
|
}
|
|
|
|
/* Usage implied by mbedtls_pk_get_psa_attributes() */
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_SIGN_HASH) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_VERIFY_HASH;
|
|
|
|
}
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
|
|
|
|
}
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_DECRYPT) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_ENCRYPT;
|
|
|
|
}
|
|
|
|
expected_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
|
|
|
|
|
2024-01-18 17:47:54 +01:00
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
/* When the resulting algorithm is ECDSA, the compile-time configuration
|
|
|
|
* can cause it to be either deterministic or randomized ECDSA.
|
|
|
|
* Rather than have two near-identical sets of test data depending on
|
|
|
|
* the configuration, always use randomized in the test data and
|
|
|
|
* tweak the expected result here. */
|
|
|
|
if (expected_alg == PSA_ALG_ECDSA(PSA_ALG_ANY_HASH)) {
|
|
|
|
expected_alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-01-18 14:16:27 +01:00
|
|
|
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, usage, &attributes), 0);
|
|
|
|
|
|
|
|
TEST_EQUAL(psa_get_key_lifetime(&attributes), lifetime);
|
|
|
|
TEST_ASSERT(mbedtls_svc_key_id_equal(psa_get_key_id(&attributes),
|
|
|
|
key_id));
|
|
|
|
TEST_EQUAL(psa_get_key_type(&attributes), expected_psa_type);
|
|
|
|
TEST_EQUAL(psa_get_key_bits(&attributes),
|
|
|
|
mbedtls_pk_get_bitlen(&pk));
|
|
|
|
TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
|
|
|
|
TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
|
|
|
|
TEST_EQUAL(psa_get_key_enrollment_algorithm(&attributes), PSA_ALG_NONE);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
MD_OR_USE_PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_RSA_C:MBEDTLS_PKCS1_V21 */
|
|
|
|
void pk_rsa_v21_get_psa_attributes(int md_type, int from_pair,
|
|
|
|
int usage_arg,
|
|
|
|
int to_pair, int expected_alg)
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
psa_key_usage_t usage = usage_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
MD_OR_USE_PSA_INIT();
|
|
|
|
|
|
|
|
psa_key_type_t expected_psa_type = 0;
|
|
|
|
if (!pk_setup_for_type(MBEDTLS_PK_RSA, from_pair, &pk, &expected_psa_type)) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
|
|
|
|
TEST_EQUAL(mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_type), 0);
|
|
|
|
if (!to_pair) {
|
|
|
|
expected_psa_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(expected_psa_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_key_usage_t expected_usage = usage;
|
|
|
|
/* Usage implied universally */
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_SIGN_HASH) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
|
|
|
|
}
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
|
|
|
|
}
|
|
|
|
/* Usage implied by mbedtls_pk_get_psa_attributes() */
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_SIGN_HASH) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_VERIFY_HASH;
|
|
|
|
}
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
|
|
|
|
}
|
|
|
|
if (expected_usage & PSA_KEY_USAGE_DECRYPT) {
|
|
|
|
expected_usage |= PSA_KEY_USAGE_ENCRYPT;
|
|
|
|
}
|
|
|
|
expected_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, usage, &attributes), 0);
|
|
|
|
|
|
|
|
TEST_EQUAL(psa_get_key_lifetime(&attributes), PSA_KEY_LIFETIME_VOLATILE);
|
|
|
|
TEST_ASSERT(mbedtls_svc_key_id_equal(psa_get_key_id(&attributes),
|
|
|
|
MBEDTLS_SVC_KEY_ID_INIT));
|
|
|
|
TEST_EQUAL(psa_get_key_type(&attributes), expected_psa_type);
|
|
|
|
TEST_EQUAL(psa_get_key_bits(&attributes),
|
|
|
|
mbedtls_pk_get_bitlen(&pk));
|
|
|
|
TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
|
|
|
|
TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
|
|
|
|
TEST_EQUAL(psa_get_key_enrollment_algorithm(&attributes), PSA_ALG_NONE);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
MD_OR_USE_PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2024-01-18 14:11:26 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */
|
|
|
|
void pk_get_psa_attributes_fail(int pk_type, int from_pair,
|
|
|
|
int usage_arg,
|
|
|
|
int expected_ret)
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_usage_t usage = usage_arg;
|
|
|
|
|
|
|
|
MD_OR_USE_PSA_INIT();
|
|
|
|
|
|
|
|
psa_key_type_t expected_psa_type;
|
|
|
|
if (!pk_setup_for_type(pk_type, from_pair, &pk, &expected_psa_type)) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, usage, &attributes),
|
|
|
|
expected_ret);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
MD_OR_USE_PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2024-01-22 20:53:21 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
void pk_get_psa_attributes_opaque(int o_type_arg, int o_bits_arg,
|
|
|
|
int o_usage_arg, int o_alg_arg,
|
|
|
|
int usage_arg,
|
|
|
|
int expected_ret,
|
|
|
|
int to_pair, int expected_usage_arg)
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t o_type = o_type_arg;
|
|
|
|
size_t bits = o_bits_arg;
|
|
|
|
psa_key_usage_t o_usage = o_usage_arg;
|
|
|
|
psa_algorithm_t alg = o_alg_arg;
|
|
|
|
psa_key_usage_t usage = usage_arg;
|
|
|
|
psa_key_usage_t expected_usage = expected_usage_arg;
|
|
|
|
|
|
|
|
USE_PSA_INIT();
|
|
|
|
|
|
|
|
psa_set_key_type(&attributes, o_type);
|
|
|
|
psa_set_key_bits(&attributes, bits);
|
|
|
|
psa_set_key_usage_flags(&attributes, o_usage);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_enrollment_algorithm(&attributes, 42);
|
|
|
|
//TODO: test with persistent key
|
|
|
|
PSA_ASSERT(psa_generate_key(&attributes, &key_id));
|
|
|
|
TEST_EQUAL(mbedtls_pk_setup_opaque(&pk, key_id), 0);
|
|
|
|
|
|
|
|
psa_key_type_t expected_psa_type =
|
|
|
|
to_pair ? o_type : PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(o_type);
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, usage, &attributes),
|
|
|
|
expected_ret);
|
|
|
|
|
|
|
|
if (expected_ret == 0) {
|
|
|
|
TEST_EQUAL(psa_get_key_lifetime(&attributes), PSA_KEY_LIFETIME_VOLATILE);
|
|
|
|
TEST_ASSERT(mbedtls_svc_key_id_equal(psa_get_key_id(&attributes),
|
|
|
|
MBEDTLS_SVC_KEY_ID_INIT));
|
|
|
|
TEST_EQUAL(psa_get_key_type(&attributes), expected_psa_type);
|
|
|
|
TEST_EQUAL(psa_get_key_bits(&attributes), bits);
|
|
|
|
TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
|
|
|
|
TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
|
|
|
|
TEST_EQUAL(psa_get_key_enrollment_algorithm(&attributes), PSA_ALG_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free(&pk);
|
|
|
|
psa_destroy_key(key_id);
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
USE_PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|