2020-07-16 20:28:59 +02:00
|
|
|
/* BEGIN_HEADER */
|
2021-04-30 17:00:34 +02:00
|
|
|
#include "test/drivers/test_driver.h"
|
2022-03-07 21:13:29 +01:00
|
|
|
|
2022-11-22 14:35:44 +01:00
|
|
|
/* Auxiliary variables for pake tests.
|
|
|
|
Global to silent the compiler when unused. */
|
2022-12-14 08:27:46 +01:00
|
|
|
size_t pake_expected_hit_count = 0;
|
|
|
|
int pake_in_driver = 0;
|
2023-03-06 15:40:35 +01:00
|
|
|
|
2023-03-13 16:06:09 +01:00
|
|
|
/* The only two JPAKE user/peer identifiers supported for the time being. */
|
|
|
|
static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
|
|
|
|
static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
|
2023-03-06 15:40:35 +01:00
|
|
|
|
2023-03-08 09:56:29 +01:00
|
|
|
#if defined(PSA_WANT_ALG_JPAKE) && defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) && \
|
|
|
|
defined(PSA_WANT_ECC_SECP_R1_256) && defined(PSA_WANT_ALG_SHA_256)
|
2022-11-22 14:35:44 +01:00
|
|
|
static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
|
|
|
|
psa_pake_operation_t *server,
|
|
|
|
psa_pake_operation_t *client,
|
|
|
|
int client_input_first,
|
|
|
|
int round)
|
|
|
|
{
|
|
|
|
unsigned char *buffer0 = NULL, *buffer1 = NULL;
|
|
|
|
size_t buffer_length = (
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
|
|
|
|
/* The output should be exactly this size according to the spec */
|
|
|
|
const size_t expected_size_key_share =
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
|
|
|
|
/* The output should be exactly this size according to the spec */
|
|
|
|
const size_t expected_size_zk_public =
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
|
|
|
|
/* The output can be smaller: the spec allows stripping leading zeroes */
|
|
|
|
const size_t max_expected_size_zk_proof =
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
|
|
|
|
size_t buffer0_off = 0;
|
|
|
|
size_t buffer1_off = 0;
|
|
|
|
size_t s_g1_len, s_g2_len, s_a_len;
|
|
|
|
size_t s_g1_off, s_g2_off, s_a_off;
|
|
|
|
size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
|
|
|
|
size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
|
|
|
|
size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
|
|
|
|
size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
|
|
|
|
size_t c_g1_len, c_g2_len, c_a_len;
|
|
|
|
size_t c_g1_off, c_g2_off, c_a_off;
|
|
|
|
size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
|
|
|
|
size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
|
|
|
|
size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
|
|
|
|
size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
ASSERT_ALLOC(buffer0, buffer_length);
|
|
|
|
ASSERT_ALLOC(buffer1, buffer_length);
|
|
|
|
|
|
|
|
switch (round) {
|
|
|
|
case 1:
|
|
|
|
/* Server first round Output */
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_g1_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(s_g1_len, expected_size_key_share);
|
|
|
|
s_g1_off = buffer0_off;
|
|
|
|
buffer0_off += s_g1_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x1_pk_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
|
|
|
|
s_x1_pk_off = buffer0_off;
|
|
|
|
buffer0_off += s_x1_pk_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x1_pr_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
|
|
|
|
s_x1_pr_off = buffer0_off;
|
|
|
|
buffer0_off += s_x1_pr_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_g2_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(s_g2_len, expected_size_key_share);
|
|
|
|
s_g2_off = buffer0_off;
|
|
|
|
buffer0_off += s_g2_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2_pk_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
|
|
|
|
s_x2_pk_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2_pk_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2_pr_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
|
|
|
|
s_x2_pr_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2_pr_len;
|
|
|
|
|
|
|
|
if (client_input_first == 1) {
|
|
|
|
/* Client first round Input */
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g1_off, s_g1_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x1_pk_off,
|
|
|
|
s_x1_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x1_pr_off,
|
|
|
|
s_x1_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g2_off,
|
|
|
|
s_g2_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2_pk_off,
|
|
|
|
s_x2_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2_pr_off,
|
|
|
|
s_x2_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
/* Adjust for indirect client driver setup in first pake_output call. */
|
|
|
|
pake_expected_hit_count++;
|
|
|
|
|
2022-11-22 14:35:44 +01:00
|
|
|
/* Client first round Output */
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_g1_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(c_g1_len, expected_size_key_share);
|
|
|
|
c_g1_off = buffer1_off;
|
|
|
|
buffer1_off += c_g1_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x1_pk_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
|
|
|
|
c_x1_pk_off = buffer1_off;
|
|
|
|
buffer1_off += c_x1_pk_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x1_pr_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
|
|
|
|
c_x1_pr_off = buffer1_off;
|
|
|
|
buffer1_off += c_x1_pr_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_g2_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(c_g2_len, expected_size_key_share);
|
|
|
|
c_g2_off = buffer1_off;
|
|
|
|
buffer1_off += c_g2_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2_pk_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
|
|
|
|
c_x2_pk_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2_pk_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2_pr_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
|
|
|
|
c_x2_pr_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2_pr_len;
|
|
|
|
|
|
|
|
if (client_input_first == 0) {
|
|
|
|
/* Client first round Input */
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g1_off, s_g1_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x1_pk_off,
|
|
|
|
s_x1_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x1_pr_off,
|
|
|
|
s_x1_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g2_off,
|
|
|
|
s_g2_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2_pk_off,
|
|
|
|
s_x2_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2_pr_off,
|
|
|
|
s_x2_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Server first round Input */
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + c_g1_off, c_g1_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + c_x1_pk_off, c_x1_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + c_x1_pr_off, c_x1_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + c_g2_off, c_g2_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + c_x2_pk_off, c_x2_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + c_x2_pr_off, c_x2_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Server second round Output */
|
|
|
|
buffer0_off = 0;
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_a_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(s_a_len, expected_size_key_share);
|
|
|
|
s_a_off = buffer0_off;
|
|
|
|
buffer0_off += s_a_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2s_pk_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
|
|
|
|
s_x2s_pk_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2s_pk_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2s_pr_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
|
|
|
|
s_x2s_pr_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2s_pr_len;
|
|
|
|
|
|
|
|
if (client_input_first == 1) {
|
|
|
|
/* Client second round Input */
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_a_off, s_a_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2s_pk_off,
|
|
|
|
s_x2s_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2s_pr_off,
|
|
|
|
s_x2s_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Client second round Output */
|
|
|
|
buffer1_off = 0;
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_a_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(c_a_len, expected_size_key_share);
|
|
|
|
c_a_off = buffer1_off;
|
|
|
|
buffer1_off += c_a_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2s_pk_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
|
|
|
|
c_x2s_pk_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2s_pk_len;
|
|
|
|
PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2s_pr_len));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
|
|
|
|
c_x2s_pr_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2s_pr_len;
|
|
|
|
|
|
|
|
if (client_input_first == 0) {
|
|
|
|
/* Client second round Input */
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_a_off, s_a_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2s_pk_off,
|
|
|
|
s_x2s_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2s_pr_off,
|
|
|
|
s_x2s_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Server second round Input */
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + c_a_off, c_a_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + c_x2s_pk_off, c_x2s_pk_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + c_x2s_pr_off, c_x2s_pr_len);
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(buffer0);
|
|
|
|
mbedtls_free(buffer1);
|
|
|
|
}
|
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
|
|
|
|
2022-03-07 21:13:29 +01:00
|
|
|
#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
|
|
|
|
/* Sanity checks on the output of RSA encryption.
|
|
|
|
*
|
|
|
|
* \param modulus Key modulus. Must not have leading zeros.
|
|
|
|
* \param private_exponent Key private exponent.
|
|
|
|
* \param alg An RSA algorithm.
|
|
|
|
* \param input_data The input plaintext.
|
|
|
|
* \param buf The ciphertext produced by the driver.
|
|
|
|
* \param length Length of \p buf in bytes.
|
|
|
|
*/
|
|
|
|
static int sanity_check_rsa_encryption_result(
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const data_t *modulus, const data_t *private_exponent,
|
|
|
|
const data_t *input_data,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *buf, size_t length)
|
2022-03-07 21:13:29 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
mbedtls_mpi N, D, C, X;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&D);
|
|
|
|
mbedtls_mpi_init(&C);
|
|
|
|
mbedtls_mpi_init(&X);
|
2022-03-07 21:13:29 +01:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
|
|
|
|
|
|
|
int ok = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(length == modulus->len);
|
2022-03-07 21:13:29 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
/* Perform the private key operation */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_read_binary(&N, modulus->x, modulus->len) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_read_binary(&D,
|
|
|
|
private_exponent->x,
|
|
|
|
private_exponent->len) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_read_binary(&C, buf, length) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_exp_mod(&X, &C, &D, &N, NULL) == 0);
|
2022-03-07 21:13:29 +01:00
|
|
|
|
|
|
|
/* Sanity checks on the padded plaintext */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_write_binary(&X, buf, length) == 0);
|
|
|
|
|
|
|
|
if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
|
|
|
|
TEST_ASSERT(length > input_data->len + 2);
|
|
|
|
TEST_EQUAL(buf[0], 0x00);
|
|
|
|
TEST_EQUAL(buf[1], 0x02);
|
|
|
|
TEST_EQUAL(buf[length - input_data->len - 1], 0x00);
|
|
|
|
ASSERT_COMPARE(buf + length - input_data->len, input_data->len,
|
|
|
|
input_data->x, input_data->len);
|
|
|
|
} else if (PSA_ALG_IS_RSA_OAEP(alg)) {
|
|
|
|
TEST_EQUAL(buf[0], 0x00);
|
2022-03-07 21:13:29 +01:00
|
|
|
/* The rest is too hard to check */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
TEST_ASSERT(!"Encryption result sanity check not implemented for RSA algorithm");
|
2022-03-07 21:13:29 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N);
|
|
|
|
mbedtls_mpi_free(&D);
|
|
|
|
mbedtls_mpi_free(&C);
|
|
|
|
mbedtls_mpi_free(&X);
|
2022-03-07 21:13:29 +01:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
return ok;
|
2022-03-07 21:13:29 +01:00
|
|
|
}
|
|
|
|
#endif
|
2020-07-16 20:28:59 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2020-09-07 12:58:16 +02:00
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_CRYPTO_DRIVERS:PSA_CRYPTO_DRIVER_TEST
|
2020-07-16 20:28:59 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2021-12-20 13:23:57 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sign_hash(int key_type_arg,
|
|
|
|
int alg_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *expected_output,
|
|
|
|
int fake_output,
|
|
|
|
int expected_status_arg)
|
2020-07-16 20:28:59 +02:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-16 20:28:59 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-12-20 13:23:57 +01:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_size;
|
2020-07-16 20:28:59 +02:00
|
|
|
size_t signature_length = 0xdeadbeef;
|
|
|
|
psa_status_t actual_status;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-16 20:28:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
psa_set_key_type(&attributes,
|
|
|
|
key_type);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_import_key(&attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key);
|
2020-07-16 20:28:59 +02:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_status = force_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output == 1) {
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output =
|
|
|
|
expected_output->x;
|
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output_length =
|
|
|
|
expected_output->len;
|
2020-07-16 20:28:59 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 13:23:57 +01:00
|
|
|
/* Allocate a buffer which has the size advertized by the
|
|
|
|
* library. */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
key_bits = psa_get_key_bits(&attributes);
|
|
|
|
signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
|
|
|
|
|
|
|
|
TEST_ASSERT(signature_size != 0);
|
|
|
|
TEST_ASSERT(signature_size <= PSA_SIGNATURE_MAX_SIZE);
|
|
|
|
ASSERT_ALLOC(signature, signature_size);
|
|
|
|
|
|
|
|
actual_status = psa_sign_hash(key, alg,
|
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length);
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(signature, signature_length,
|
|
|
|
expected_output->x, expected_output->len);
|
2020-07-16 20:28:59 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_signature_sign_hooks.hits, 1);
|
2020-07-16 20:28:59 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_free(signature);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-16 20:28:59 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-07-17 19:46:15 +02:00
|
|
|
|
2021-12-20 17:35:48 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void verify_hash(int key_type_arg,
|
|
|
|
int key_type_public_arg,
|
|
|
|
int alg_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
int register_public_key,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *signature_input,
|
|
|
|
int expected_status_arg)
|
2020-07-17 19:46:15 +02:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-12-20 17:35:48 +01:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_key_type_t key_type_public = key_type_public_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-17 19:46:15 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t actual_status;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-17 19:46:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
if (register_public_key) {
|
|
|
|
psa_set_key_type(&attributes, key_type_public);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_import_key(&attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key);
|
|
|
|
} else {
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_import_key(&attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key);
|
2020-07-17 19:46:15 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_signature_verify_hooks.forced_status = force_status;
|
2020-07-17 19:46:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
actual_status = psa_verify_hash(key, alg,
|
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature_input->x, signature_input->len);
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_signature_verify_hooks.hits, 1);
|
2020-07-17 19:46:15 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
2020-07-17 19:46:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-07-20 15:33:08 +02:00
|
|
|
|
2021-12-21 10:31:59 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sign_message(int key_type_arg,
|
|
|
|
int alg_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *expected_output,
|
|
|
|
int fake_output,
|
|
|
|
int expected_status_arg)
|
2021-04-23 12:38:33 +02:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-12-21 10:31:59 +01:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_size;
|
2021-04-23 12:38:33 +02:00
|
|
|
size_t signature_length = 0xdeadbeef;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_import_key(&attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key);
|
2021-04-23 12:38:33 +02:00
|
|
|
|
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_status = force_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output == 1) {
|
2021-04-23 12:38:33 +02:00
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output =
|
|
|
|
expected_output->x;
|
|
|
|
mbedtls_test_driver_signature_sign_hooks.forced_output_length =
|
|
|
|
expected_output->len;
|
|
|
|
}
|
|
|
|
|
2021-12-21 10:31:59 +01:00
|
|
|
/* Allocate a buffer which has the size advertized by the
|
|
|
|
* library. */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
key_bits = psa_get_key_bits(&attributes);
|
|
|
|
signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
|
|
|
|
|
|
|
|
TEST_ASSERT(signature_size != 0);
|
|
|
|
TEST_ASSERT(signature_size <= PSA_SIGNATURE_MAX_SIZE);
|
|
|
|
ASSERT_ALLOC(signature, signature_size);
|
|
|
|
|
|
|
|
actual_status = psa_sign_message(key, alg,
|
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length);
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(signature, signature_length,
|
|
|
|
expected_output->x, expected_output->len);
|
2021-04-23 12:38:33 +02:00
|
|
|
}
|
2021-05-03 16:29:54 +02:00
|
|
|
/* In the builtin algorithm the driver is called twice. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_signature_sign_hooks.hits,
|
|
|
|
force_status == PSA_ERROR_NOT_SUPPORTED ? 2 : 1);
|
2021-04-23 12:38:33 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_free(signature);
|
|
|
|
PSA_DONE();
|
2021-04-23 12:38:33 +02:00
|
|
|
mbedtls_test_driver_signature_sign_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-12-21 14:53:45 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void verify_message(int key_type_arg,
|
|
|
|
int key_type_public_arg,
|
|
|
|
int alg_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
int register_public_key,
|
|
|
|
data_t *key_input,
|
|
|
|
data_t *data_input,
|
|
|
|
data_t *signature_input,
|
|
|
|
int expected_status_arg)
|
2021-04-23 12:38:33 +02:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-12-21 14:53:45 +01:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_key_type_t key_type_public = key_type_public_arg;
|
2021-04-23 12:38:33 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
if (register_public_key) {
|
|
|
|
psa_set_key_type(&attributes, key_type_public);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_import_key(&attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key);
|
|
|
|
} else {
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_import_key(&attributes,
|
|
|
|
key_input->x, key_input->len,
|
|
|
|
&key);
|
2021-04-23 12:38:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_test_driver_signature_verify_hooks.forced_status = force_status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
actual_status = psa_verify_message(key, alg,
|
|
|
|
data_input->x, data_input->len,
|
|
|
|
signature_input->x, signature_input->len);
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
2021-05-03 16:29:54 +02:00
|
|
|
/* In the builtin algorithm the driver is called twice. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_signature_verify_hooks.hits,
|
|
|
|
force_status == PSA_ERROR_NOT_SUPPORTED ? 2 : 1);
|
2021-04-23 12:38:33 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-23 12:38:33 +02:00
|
|
|
mbedtls_test_driver_signature_verify_hooks =
|
|
|
|
mbedtls_test_driver_signature_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-06 11:56:20 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_ECC_SECP_R1_256 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void generate_key(int force_status_arg,
|
|
|
|
data_t *fake_output,
|
|
|
|
int expected_status_arg)
|
2020-07-20 15:33:08 +02:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-20 15:33:08 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
|
2020-09-07 12:58:16 +02:00
|
|
|
const uint8_t *expected_output = NULL;
|
|
|
|
size_t expected_output_length = 0;
|
2020-07-20 15:33:08 +02:00
|
|
|
psa_status_t actual_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t actual_output[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(256)] = { 0 };
|
2020-07-20 15:33:08 +02:00
|
|
|
size_t actual_output_length;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-07-20 15:33:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_type(&attributes,
|
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
|
|
|
|
psa_set_key_bits(&attributes, 256);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
2020-07-20 15:33:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output->len > 0) {
|
2021-04-13 12:41:34 +02:00
|
|
|
expected_output =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output =
|
2023-01-11 14:50:10 +01:00
|
|
|
fake_output->x;
|
2021-04-13 12:41:34 +02:00
|
|
|
|
|
|
|
expected_output_length =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output_length =
|
2023-01-11 14:50:10 +01:00
|
|
|
fake_output->len;
|
2020-07-20 15:33:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks.hits = 0;
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_status = force_status;
|
2020-07-20 15:33:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-07-20 15:33:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
actual_status = psa_generate_key(&attributes, &key);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_key_management_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
2020-07-20 15:33:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (actual_status == PSA_SUCCESS) {
|
|
|
|
psa_export_key(key, actual_output, sizeof(actual_output), &actual_output_length);
|
2020-07-20 15:33:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output->len > 0) {
|
|
|
|
ASSERT_COMPARE(actual_output, actual_output_length,
|
|
|
|
expected_output, expected_output_length);
|
|
|
|
} else {
|
2020-07-20 15:33:08 +02:00
|
|
|
size_t zeroes = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < sizeof(actual_output); i++) {
|
|
|
|
if (actual_output[i] == 0) {
|
2020-07-20 15:33:08 +02:00
|
|
|
zeroes++;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-07-20 15:33:08 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(zeroes != sizeof(actual_output));
|
2020-07-20 15:33:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-07-20 15:33:08 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2021-03-30 14:57:04 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void validate_key(int force_status_arg,
|
|
|
|
int location,
|
|
|
|
int owner_id_arg,
|
|
|
|
int id_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_input,
|
|
|
|
int expected_status_arg)
|
2020-10-13 17:43:44 +02:00
|
|
|
{
|
2022-02-03 15:42:47 +01:00
|
|
|
psa_key_lifetime_t lifetime =
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( \
|
|
|
|
PSA_KEY_PERSISTENCE_DEFAULT, location);
|
|
|
|
mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make(owner_id_arg, id_arg);
|
2020-10-13 17:43:44 +02:00
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-10-13 17:43:44 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t actual_status;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-13 17:43:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_id(&attributes, id);
|
|
|
|
psa_set_key_type(&attributes,
|
|
|
|
key_type);
|
|
|
|
psa_set_key_lifetime(&attributes, lifetime);
|
|
|
|
psa_set_key_bits(&attributes, 0);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
|
2020-10-13 17:43:44 +02:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks.forced_status = force_status;
|
2020-10-13 17:43:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-10-13 17:43:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
actual_status = psa_import_key(&attributes, key_input->x, key_input->len, &key);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_key_management_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_key_management_hooks.location, location);
|
2020-10-13 17:43:44 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-13 17:43:44 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-30 14:57:04 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void export_key(int force_status_arg,
|
|
|
|
data_t *fake_output,
|
|
|
|
int key_in_type_arg,
|
|
|
|
data_t *key_in,
|
|
|
|
int key_out_type_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg)
|
2020-10-14 14:39:20 +02:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_handle_t handle = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_type_t input_key_type = key_in_type_arg;
|
|
|
|
psa_key_type_t output_key_type = key_out_type_arg;
|
|
|
|
const uint8_t *expected_output_ptr = NULL;
|
|
|
|
size_t expected_output_length = 0;
|
|
|
|
psa_status_t actual_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t actual_output[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(256)] = { 0 };
|
2020-10-14 14:39:20 +02:00
|
|
|
size_t actual_output_length;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-14 14:39:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_type(&attributes, input_key_type);
|
|
|
|
psa_set_key_bits(&attributes, 256);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
|
2020-10-14 14:39:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_in->x, key_in->len, &handle));
|
2020-10-14 14:39:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output->len > 0) {
|
2021-04-13 12:41:34 +02:00
|
|
|
expected_output_ptr =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output =
|
2023-01-11 14:50:10 +01:00
|
|
|
fake_output->x;
|
2021-04-13 12:41:34 +02:00
|
|
|
|
|
|
|
expected_output_length =
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_output_length =
|
2023-01-11 14:50:10 +01:00
|
|
|
fake_output->len;
|
|
|
|
} else {
|
2020-10-14 14:39:20 +02:00
|
|
|
expected_output_ptr = expected_output->x;
|
|
|
|
expected_output_length = expected_output->len;
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks.hits = 0;
|
|
|
|
mbedtls_test_driver_key_management_hooks.forced_status = force_status;
|
2020-10-14 14:39:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(output_key_type)) {
|
|
|
|
actual_status = psa_export_public_key(handle,
|
|
|
|
actual_output,
|
|
|
|
sizeof(actual_output),
|
|
|
|
&actual_output_length);
|
|
|
|
} else {
|
|
|
|
actual_status = psa_export_key(handle,
|
|
|
|
actual_output,
|
|
|
|
sizeof(actual_output),
|
|
|
|
&actual_output_length);
|
|
|
|
}
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
2020-10-14 14:39:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(output_key_type) &&
|
|
|
|
!PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(input_key_type)) {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_key_management_hooks.hits, 1);
|
|
|
|
}
|
2020-10-14 14:39:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (actual_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(actual_output, actual_output_length,
|
|
|
|
expected_output_ptr, expected_output_length);
|
2020-10-14 14:39:20 +02:00
|
|
|
}
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(handle);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_key_management_hooks =
|
|
|
|
mbedtls_test_driver_key_management_hooks_init();
|
2020-10-14 14:39:20 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-11-16 18:08:53 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void key_agreement(int alg_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
int our_key_type_arg,
|
|
|
|
data_t *our_key_data,
|
|
|
|
data_t *peer_key_data,
|
|
|
|
data_t *expected_output,
|
|
|
|
data_t *fake_output,
|
|
|
|
int expected_status_arg)
|
2022-11-16 18:08:53 +01:00
|
|
|
{
|
|
|
|
psa_status_t force_status = force_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t our_key_type = our_key_type_arg;
|
|
|
|
mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
const uint8_t *expected_output_ptr = NULL;
|
|
|
|
size_t expected_output_length = 0;
|
|
|
|
unsigned char *actual_output = NULL;
|
|
|
|
size_t actual_output_length = ~0;
|
|
|
|
size_t key_bits;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
mbedtls_test_driver_key_agreement_hooks =
|
|
|
|
mbedtls_test_driver_key_agreement_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2022-11-16 18:08:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, our_key_type);
|
|
|
|
PSA_ASSERT(psa_import_key(&attributes,
|
|
|
|
our_key_data->x, our_key_data->len,
|
|
|
|
&our_key));
|
2022-11-16 18:08:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
|
|
|
|
key_bits = psa_get_key_bits(&attributes);
|
2022-11-16 18:08:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(expected_output->len,
|
|
|
|
PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
|
|
|
|
TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
|
|
|
|
PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
|
2022-11-16 18:08:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output->len > 0) {
|
2022-11-16 18:08:53 +01:00
|
|
|
expected_output_ptr =
|
|
|
|
mbedtls_test_driver_key_agreement_hooks.forced_output =
|
2023-01-11 14:50:10 +01:00
|
|
|
fake_output->x;
|
2022-11-16 18:08:53 +01:00
|
|
|
|
|
|
|
expected_output_length =
|
|
|
|
mbedtls_test_driver_key_agreement_hooks.forced_output_length =
|
2023-01-11 14:50:10 +01:00
|
|
|
fake_output->len;
|
|
|
|
} else {
|
2022-11-16 18:08:53 +01:00
|
|
|
expected_output_ptr = expected_output->x;
|
|
|
|
expected_output_length = expected_output->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_test_driver_key_agreement_hooks.hits = 0;
|
|
|
|
mbedtls_test_driver_key_agreement_hooks.forced_status = force_status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(actual_output, expected_output->len);
|
|
|
|
actual_status = psa_raw_key_agreement(alg, our_key,
|
|
|
|
peer_key_data->x, peer_key_data->len,
|
|
|
|
actual_output, expected_output->len,
|
|
|
|
&actual_output_length);
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_key_agreement_hooks.hits, 1);
|
|
|
|
|
|
|
|
if (actual_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(actual_output, actual_output_length,
|
|
|
|
expected_output_ptr, expected_output_length);
|
2022-11-16 18:08:53 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(actual_output);
|
2022-11-16 18:08:53 +01:00
|
|
|
actual_output = NULL;
|
|
|
|
actual_output_length = ~0;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(our_key);
|
|
|
|
PSA_DONE();
|
2022-11-16 18:08:53 +01:00
|
|
|
mbedtls_test_driver_key_agreement_hooks =
|
|
|
|
mbedtls_test_driver_key_agreement_hooks_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-09-03 15:31:04 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void cipher_encrypt_validation(int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *input)
|
2020-09-03 15:31:04 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 15:31:04 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
|
2021-04-12 15:46:09 +02:00
|
|
|
unsigned char *output1 = NULL;
|
|
|
|
size_t output1_buffer_size = 0;
|
|
|
|
size_t output1_length = 0;
|
|
|
|
unsigned char *output2 = NULL;
|
|
|
|
size_t output2_buffer_size = 0;
|
|
|
|
size_t output2_length = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
size_t function_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
|
|
|
|
output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
|
|
|
|
ASSERT_ALLOC(output1, output1_buffer_size);
|
|
|
|
ASSERT_ALLOC(output2, output2_buffer_size);
|
2021-04-12 15:46:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
|
|
|
|
output1_buffer_size, &output1_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-04-12 15:46:09 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_update(&operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output2, output2_buffer_size,
|
|
|
|
&function_output_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2021-04-12 15:46:09 +02:00
|
|
|
output2_length += function_output_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_finish(&operation,
|
|
|
|
output2 + output2_length,
|
|
|
|
output2_buffer_size - output2_length,
|
|
|
|
&function_output_length));
|
2020-09-09 11:51:45 +02:00
|
|
|
/* Finish will have called abort as well, so expecting two hits here */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 2);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2021-04-12 15:46:09 +02:00
|
|
|
output2_length += function_output_length;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_abort(&operation));
|
2021-04-12 15:46:09 +02:00
|
|
|
// driver function should've been called as part of the finish() core routine
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
|
|
|
ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
|
|
|
|
output2, output2_length);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_cipher_abort(&operation);
|
|
|
|
mbedtls_free(output1);
|
|
|
|
mbedtls_free(output2);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 15:31:04 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void cipher_encrypt_multipart(int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *iv,
|
|
|
|
data_t *input,
|
|
|
|
int first_part_size_arg,
|
|
|
|
int output1_length_arg,
|
|
|
|
int output2_length_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int mock_output_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
int expected_status_arg)
|
2020-09-03 15:31:04 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 15:31:04 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-25 18:21:33 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_status_t force_status = force_status_arg;
|
2020-09-03 15:31:04 +02:00
|
|
|
size_t first_part_size = first_part_size_arg;
|
|
|
|
size_t output1_length = output1_length_arg;
|
|
|
|
size_t output2_length = output2_length_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
|
|
|
size_t total_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2021-06-25 18:21:33 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = force_status;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2021-04-12 17:45:01 +02:00
|
|
|
/* Test operation initialization */
|
|
|
|
mbedtls_psa_cipher_operation_t mbedtls_operation =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PSA_CIPHER_OPERATION_INIT;
|
2021-04-12 17:45:01 +02:00
|
|
|
|
2022-12-04 18:19:59 +01:00
|
|
|
mbedtls_transparent_test_driver_cipher_operation_t transparent_operation =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_TRANSPARENT_TEST_DRIVER_CIPHER_OPERATION_INIT;
|
2021-04-12 17:45:01 +02:00
|
|
|
|
|
|
|
mbedtls_opaque_test_driver_cipher_operation_t opaque_operation =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_OPAQUE_TEST_DRIVER_CIPHER_OPERATION_INIT;
|
2021-04-12 17:45:01 +02:00
|
|
|
|
|
|
|
operation.ctx.mbedtls_ctx = mbedtls_operation;
|
2022-12-04 18:19:59 +01:00
|
|
|
operation.ctx.transparent_test_driver_ctx = transparent_operation;
|
2021-04-12 17:45:01 +02:00
|
|
|
operation.ctx.opaque_test_driver_ctx = opaque_operation;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, (force_status == PSA_SUCCESS ? 1 : 0));
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
output_buffer_size = ((size_t) input->len +
|
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type));
|
|
|
|
ASSERT_ALLOC(output, output_buffer_size);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mock_output_arg) {
|
2021-06-25 18:21:33 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(first_part_size <= input->len);
|
|
|
|
PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, (force_status == PSA_SUCCESS ? 1 : 0));
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(function_output_length == output1_length);
|
2020-09-03 15:31:04 +02:00
|
|
|
total_output_length += function_output_length;
|
2021-06-25 18:21:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (first_part_size < input->len) {
|
|
|
|
PSA_ASSERT(psa_cipher_update(&operation,
|
|
|
|
input->x + first_part_size,
|
|
|
|
input->len - first_part_size,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-06-25 18:21:33 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(function_output_length == output2_length);
|
2021-06-25 18:21:33 +02:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mock_output_arg) {
|
2021-06-25 18:21:33 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = NULL;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_finish(&operation,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length);
|
2020-09-09 11:51:45 +02:00
|
|
|
/* Finish will have called abort as well, so expecting two hits here */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, (force_status == PSA_SUCCESS ? 2 : 0));
|
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
total_output_length += function_output_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(status, expected_status);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
PSA_ASSERT(psa_cipher_abort(&operation));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length);
|
2021-06-25 18:21:33 +02:00
|
|
|
}
|
2020-09-03 15:31:04 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_cipher_abort(&operation);
|
|
|
|
mbedtls_free(output);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 15:31:04 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void cipher_decrypt_multipart(int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *iv,
|
|
|
|
data_t *input,
|
|
|
|
int first_part_size_arg,
|
|
|
|
int output1_length_arg,
|
|
|
|
int output2_length_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int mock_output_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
int expected_status_arg)
|
2020-09-03 15:31:04 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 15:31:04 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-09-10 14:32:26 +02:00
|
|
|
psa_status_t force_status = force_status_arg;
|
2020-09-03 15:31:04 +02:00
|
|
|
size_t first_part_size = first_part_size_arg;
|
|
|
|
size_t output1_length = output1_length_arg;
|
|
|
|
size_t output2_length = output2_length_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
|
|
|
size_t total_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = force_status;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2021-04-12 17:45:01 +02:00
|
|
|
/* Test operation initialization */
|
|
|
|
mbedtls_psa_cipher_operation_t mbedtls_operation =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PSA_CIPHER_OPERATION_INIT;
|
2021-04-12 17:45:01 +02:00
|
|
|
|
2022-12-04 18:19:59 +01:00
|
|
|
mbedtls_transparent_test_driver_cipher_operation_t transparent_operation =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_TRANSPARENT_TEST_DRIVER_CIPHER_OPERATION_INIT;
|
2021-04-12 17:45:01 +02:00
|
|
|
|
|
|
|
mbedtls_opaque_test_driver_cipher_operation_t opaque_operation =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_OPAQUE_TEST_DRIVER_CIPHER_OPERATION_INIT;
|
2021-04-12 17:45:01 +02:00
|
|
|
|
|
|
|
operation.ctx.mbedtls_ctx = mbedtls_operation;
|
2022-12-04 18:19:59 +01:00
|
|
|
operation.ctx.transparent_test_driver_ctx = transparent_operation;
|
2021-04-12 17:45:01 +02:00
|
|
|
operation.ctx.opaque_test_driver_ctx = opaque_operation;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, (force_status == PSA_SUCCESS ? 1 : 0));
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
output_buffer_size = ((size_t) input->len +
|
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type));
|
|
|
|
ASSERT_ALLOC(output, output_buffer_size);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mock_output_arg) {
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len;
|
2020-09-10 18:07:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(first_part_size <= input->len);
|
|
|
|
PSA_ASSERT(psa_cipher_update(&operation,
|
|
|
|
input->x, first_part_size,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, (force_status == PSA_SUCCESS ? 1 : 0));
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(function_output_length == output1_length);
|
2020-09-03 15:31:04 +02:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (first_part_size < input->len) {
|
|
|
|
PSA_ASSERT(psa_cipher_update(&operation,
|
|
|
|
input->x + first_part_size,
|
|
|
|
input->len - first_part_size,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, (force_status == PSA_SUCCESS ? 1 : 0));
|
2021-06-25 18:21:33 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(function_output_length == output2_length);
|
2021-06-25 18:21:33 +02:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mock_output_arg) {
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = NULL;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_finish(&operation,
|
|
|
|
output + total_output_length,
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length);
|
2020-09-09 11:51:45 +02:00
|
|
|
/* Finish will have called abort as well, so expecting two hits here */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, (force_status == PSA_SUCCESS ? 2 : 0));
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
total_output_length += function_output_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(status, expected_status);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
PSA_ASSERT(psa_cipher_abort(&operation));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length);
|
2020-09-03 15:31:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_cipher_abort(&operation);
|
|
|
|
mbedtls_free(output);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 15:31:04 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void cipher_decrypt(int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *iv,
|
|
|
|
data_t *input_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int mock_output_arg,
|
|
|
|
int force_status_arg,
|
|
|
|
int expected_status_arg)
|
2020-09-03 15:31:04 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-03 15:31:04 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-09-10 14:32:26 +02:00
|
|
|
psa_status_t force_status = force_status_arg;
|
2021-04-12 15:46:09 +02:00
|
|
|
unsigned char *input = NULL;
|
|
|
|
size_t input_buffer_size = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
2021-04-12 15:46:09 +02:00
|
|
|
size_t output_length = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = force_status;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2021-04-12 15:46:09 +02:00
|
|
|
/* Allocate input buffer and copy the iv and the plaintext */
|
2023-01-11 14:50:10 +01:00
|
|
|
input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
|
|
|
|
if (input_buffer_size > 0) {
|
|
|
|
ASSERT_ALLOC(input, input_buffer_size);
|
|
|
|
memcpy(input, iv->x, iv->len);
|
|
|
|
memcpy(input + iv->len, input_arg->x, input_arg->len);
|
2021-04-12 15:46:09 +02:00
|
|
|
}
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
|
|
|
|
ASSERT_ALLOC(output, output_buffer_size);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2021-04-12 15:46:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mock_output_arg) {
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x;
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len;
|
2020-09-10 18:07:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
|
|
|
|
output_buffer_size, &output_length);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(status, expected_status);
|
2020-09-03 15:31:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(expected_output->x, expected_output->len,
|
|
|
|
output, output_length);
|
2020-09-03 15:31:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(input);
|
|
|
|
mbedtls_free(output);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-03 15:31:04 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-09-10 18:07:57 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void cipher_entry_points(int alg_arg, int key_type_arg,
|
|
|
|
data_t *key_data, data_t *iv,
|
|
|
|
data_t *input)
|
2020-09-10 18:07:57 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-09-10 18:07:57 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output, input->len + 16);
|
2020-09-11 11:44:50 +02:00
|
|
|
output_buffer_size = input->len + 16;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2021-07-09 16:55:02 +02:00
|
|
|
/*
|
|
|
|
* Test encrypt failure
|
|
|
|
* First test that if we don't force a driver error, encryption is
|
2021-12-21 06:14:10 +01:00
|
|
|
* successful, then force driver error.
|
2021-07-09 16:55:02 +02:00
|
|
|
*/
|
|
|
|
status = psa_cipher_encrypt(
|
|
|
|
key, alg, input->x, input->len,
|
2023-01-11 14:50:10 +01:00
|
|
|
output, output_buffer_size, &function_output_length);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
2021-07-09 16:55:02 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
|
|
|
|
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2021-07-09 18:20:46 +02:00
|
|
|
/* Set the output buffer in a given state. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < output_buffer_size; i++) {
|
2021-07-09 18:20:46 +02:00
|
|
|
output[i] = 0xa5;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-09 18:20:46 +02:00
|
|
|
|
2021-07-09 16:55:02 +02:00
|
|
|
status = psa_cipher_encrypt(
|
|
|
|
key, alg, input->x, input->len,
|
2023-01-11 14:50:10 +01:00
|
|
|
output, output_buffer_size, &function_output_length);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_GENERIC_ERROR);
|
2021-07-09 18:20:46 +02:00
|
|
|
/*
|
|
|
|
* Check that the output buffer is still in the same state.
|
|
|
|
* This will fail if the output buffer is used by the core to pass the IV
|
|
|
|
* it generated to the driver (and is not restored).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < output_buffer_size; i++) {
|
|
|
|
TEST_EQUAL(output[i], 0xa5);
|
2021-07-09 18:20:46 +02:00
|
|
|
}
|
2021-07-09 16:55:02 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
|
|
|
|
2020-09-10 18:07:57 +02:00
|
|
|
/* Test setup call, encrypt */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_encrypt_setup(&operation, key, alg);
|
2020-09-10 18:07:57 +02:00
|
|
|
/* When setup fails, it shouldn't call any further entry points */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&operation, iv->x, iv->len);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
2020-09-10 18:07:57 +02:00
|
|
|
|
|
|
|
/* Test setup call failure, decrypt */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_decrypt_setup(&operation, key, alg);
|
2020-09-10 18:07:57 +02:00
|
|
|
/* When setup fails, it shouldn't call any further entry points */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&operation, iv->x, iv->len);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
2020-09-10 18:07:57 +02:00
|
|
|
|
|
|
|
/* Test IV setting failure */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_encrypt_setup(&operation, key, alg);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&operation, iv->x, iv->len);
|
2020-09-10 18:07:57 +02:00
|
|
|
/* When setting the IV fails, it should call abort too */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 2);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2020-09-11 11:44:50 +02:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
|
|
|
psa_cipher_abort(&operation);
|
2020-09-10 18:07:57 +02:00
|
|
|
|
|
|
|
/* Test IV generation failure */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_encrypt_setup(&operation, key, alg);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2021-07-09 14:43:26 +02:00
|
|
|
/* Set the output buffer in a given state. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < 16; i++) {
|
2021-07-09 14:43:26 +02:00
|
|
|
output[i] = 0xa5;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-09 14:43:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_generate_iv(&operation, output, 16, &function_output_length);
|
2020-09-11 11:44:50 +02:00
|
|
|
/* When generating the IV fails, it should call abort too */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 2);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-07-09 14:43:26 +02:00
|
|
|
/*
|
|
|
|
* Check that the output buffer is still in the same state.
|
|
|
|
* This will fail if the output buffer is used by the core to pass the IV
|
|
|
|
* it generated to the driver (and is not restored).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < 16; i++) {
|
|
|
|
TEST_EQUAL(output[i], 0xa5);
|
2021-07-09 14:43:26 +02:00
|
|
|
}
|
2020-09-11 11:44:50 +02:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
|
|
|
psa_cipher_abort(&operation);
|
2020-09-10 18:07:57 +02:00
|
|
|
|
|
|
|
/* Test update failure */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_encrypt_setup(&operation, key, alg);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&operation, iv->x, iv->len);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length);
|
2020-09-10 18:07:57 +02:00
|
|
|
/* When the update call fails, it should call abort too */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 2);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2020-09-11 11:44:50 +02:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
|
|
|
psa_cipher_abort(&operation);
|
2020-09-10 18:07:57 +02:00
|
|
|
|
|
|
|
/* Test finish failure */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_encrypt_setup(&operation, key, alg);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&operation, iv->x, iv->len);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2020-09-10 18:07:57 +02:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_finish(&operation,
|
|
|
|
output + function_output_length,
|
|
|
|
output_buffer_size - function_output_length,
|
|
|
|
&function_output_length);
|
2020-09-10 18:07:57 +02:00
|
|
|
/* When the finish call fails, it should call abort too */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 2);
|
|
|
|
TEST_EQUAL(status, mbedtls_test_driver_cipher_hooks.forced_status);
|
2020-09-11 11:44:50 +02:00
|
|
|
/* Failure should prevent further operations from executing on the driver */
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks.hits = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&operation,
|
|
|
|
input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits, 0);
|
|
|
|
psa_cipher_abort(&operation);
|
2020-09-10 18:07:57 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_cipher_abort(&operation);
|
|
|
|
mbedtls_free(output);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
|
2020-09-10 18:07:57 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-03-23 09:33:39 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void aead_encrypt(int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *expected_result,
|
|
|
|
int forced_status_arg)
|
2021-03-23 09:33:39 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-03-19 18:46:15 +01:00
|
|
|
size_t key_bits;
|
2021-03-23 09:33:39 +01:00
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
key_bits = psa_get_key_bits(&attributes);
|
2021-03-19 18:46:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
|
|
|
|
alg);
|
2021-03-19 18:46:15 +01:00
|
|
|
/* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
|
|
|
|
* should be exact. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(output_size,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
|
|
|
|
TEST_ASSERT(output_size <=
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
|
|
|
|
ASSERT_ALLOC(output_data, output_size);
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_aead_hooks.forced_status = forced_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_aead_encrypt(key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x, additional_data->len,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
output_data, output_size,
|
|
|
|
&output_length);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_encrypt, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.driver_status, forced_status);
|
|
|
|
|
|
|
|
TEST_EQUAL(status, (forced_status == PSA_ERROR_NOT_SUPPORTED) ?
|
|
|
|
PSA_SUCCESS : forced_status);
|
|
|
|
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(expected_result->x, expected_result->len,
|
|
|
|
output_data, output_length);
|
2021-03-23 09:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_free(output_data);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 09:33:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void aead_decrypt(int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *expected_data,
|
|
|
|
int forced_status_arg)
|
2021-03-23 09:33:39 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-03-19 18:46:15 +01:00
|
|
|
size_t key_bits;
|
2021-03-23 09:33:39 +01:00
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
key_bits = psa_get_key_bits(&attributes);
|
2021-03-19 18:46:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
|
|
|
|
alg);
|
|
|
|
ASSERT_ALLOC(output_data, output_size);
|
2021-03-23 09:33:39 +01:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_aead_hooks.forced_status = forced_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_aead_decrypt(key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x,
|
|
|
|
additional_data->len,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
output_data, output_size,
|
|
|
|
&output_length);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_decrypt, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.driver_status, forced_status);
|
|
|
|
|
|
|
|
TEST_EQUAL(status, (forced_status == PSA_ERROR_NOT_SUPPORTED) ?
|
|
|
|
PSA_SUCCESS : forced_status);
|
|
|
|
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(expected_data->x, expected_data->len,
|
|
|
|
output_data, output_length);
|
2021-03-23 09:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_free(output_data);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
2021-03-23 09:33:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-02-22 12:44:15 +01:00
|
|
|
|
2021-04-29 21:10:11 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mac_sign(int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac,
|
|
|
|
int forced_status_arg)
|
2021-04-29 21:10:11 +02:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
uint8_t *actual_mac = NULL;
|
|
|
|
size_t mac_buffer_size =
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
|
2021-04-29 21:10:11 +02:00
|
|
|
size_t mac_length = 0;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
2021-05-10 11:18:20 +02:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mac_buffer_size <= PSA_MAC_MAX_SIZE);
|
2021-04-29 21:10:11 +02:00
|
|
|
/* We expect PSA_MAC_LENGTH to be exact. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(expected_mac->len == mac_buffer_size);
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2021-04-29 21:10:11 +02: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, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(actual_mac, mac_buffer_size);
|
2021-05-10 11:18:20 +02:00
|
|
|
mbedtls_test_driver_mac_hooks.forced_status = forced_status;
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2021-06-18 14:01:50 +02:00
|
|
|
/*
|
|
|
|
* Calculate the MAC, one-shot case.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_compute(key, alg,
|
|
|
|
input->x, input->len,
|
|
|
|
actual_mac, mac_buffer_size,
|
|
|
|
&mac_length);
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(forced_status, status);
|
2021-06-18 14:01:50 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_mac_abort(&operation));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(expected_mac->x, expected_mac->len,
|
|
|
|
actual_mac, mac_length);
|
2021-04-29 21:10:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(actual_mac);
|
2021-04-29 21:10:11 +02:00
|
|
|
actual_mac = NULL;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_mac_abort(&operation);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
|
|
|
mbedtls_free(actual_mac);
|
2021-05-10 11:18:20 +02:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-12-06 14:19:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mac_sign_multipart(int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac,
|
|
|
|
int fragments_count,
|
|
|
|
int forced_status_arg)
|
2021-12-06 14:19:37 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
uint8_t *actual_mac = NULL;
|
|
|
|
size_t mac_buffer_size =
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
|
2021-12-06 14:19:37 +01:00
|
|
|
size_t mac_length = 0;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
uint8_t *input_x = input->x;
|
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mac_buffer_size <= PSA_MAC_MAX_SIZE);
|
2021-12-06 14:19:37 +01:00
|
|
|
/* We expect PSA_MAC_LENGTH to be exact. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(expected_mac->len == mac_buffer_size);
|
2021-12-06 14:19:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2021-12-06 14:19:37 +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, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2021-12-06 14:19:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2021-12-06 14:19:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(actual_mac, mac_buffer_size);
|
2021-12-06 14:19:37 +01:00
|
|
|
mbedtls_test_driver_mac_hooks.forced_status = forced_status;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the MAC, multipart case.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_sign_setup(&operation, key, alg);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
2021-12-06 14:19:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(forced_status, status);
|
2021-12-06 14:19:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fragments_count) {
|
|
|
|
TEST_ASSERT((input->len / fragments_count) > 0);
|
2021-12-06 14:19:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (int i = 0; i < fragments_count; i++) {
|
2021-12-06 14:19:37 +01:00
|
|
|
int fragment_size = input->len / fragments_count;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i == fragments_count - 1) {
|
|
|
|
fragment_size += (input->len % fragments_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_mac_update(&operation,
|
|
|
|
input_x, fragment_size);
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 2 + i);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
}
|
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(PSA_ERROR_BAD_STATE, status);
|
2021-12-06 14:19:37 +01:00
|
|
|
}
|
|
|
|
input_x += fragment_size;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_sign_finish(&operation,
|
|
|
|
actual_mac, mac_buffer_size,
|
|
|
|
&mac_length);
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 3 + fragments_count);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(PSA_ERROR_BAD_STATE, status);
|
2021-12-06 14:19:37 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
PSA_ASSERT(psa_mac_abort(&operation));
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 3 + fragments_count);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(expected_mac->x, expected_mac->len,
|
|
|
|
actual_mac, mac_length);
|
2021-12-06 14:19:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(actual_mac);
|
2021-12-06 14:19:37 +01:00
|
|
|
actual_mac = NULL;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_mac_abort(&operation);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
|
|
|
mbedtls_free(actual_mac);
|
2021-12-06 14:19:37 +01:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 21:10:11 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mac_verify(int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac,
|
|
|
|
int forced_status_arg)
|
2021-04-29 21:10:11 +02:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
2021-05-10 11:18:20 +02:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(expected_mac->len <= PSA_MAC_MAX_SIZE);
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2021-05-10 11:18:20 +02:00
|
|
|
mbedtls_test_driver_mac_hooks.forced_status = forced_status;
|
2021-04-29 21:10:11 +02:00
|
|
|
|
2021-06-18 15:05:36 +02:00
|
|
|
/*
|
|
|
|
* Verify the MAC, one-shot case.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_verify(key, alg,
|
|
|
|
input->x, input->len,
|
|
|
|
expected_mac->x, expected_mac->len);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(forced_status, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_mac_abort(&operation));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
2021-04-29 21:10:11 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_mac_abort(&operation);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-05-10 11:18:20 +02:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
2021-04-29 21:10:11 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-12-06 14:52:12 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mac_verify_multipart(int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac,
|
|
|
|
int fragments_count,
|
|
|
|
int forced_status_arg)
|
2021-12-06 14:52:12 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
uint8_t *input_x = input->x;
|
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(expected_mac->len <= PSA_MAC_MAX_SIZE);
|
2021-12-06 14:52:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2021-12-06 14:52:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2021-12-06 14:52:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2021-12-06 14:52:12 +01:00
|
|
|
|
|
|
|
mbedtls_test_driver_mac_hooks.forced_status = forced_status;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the MAC, multi-part case.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_verify_setup(&operation, key, alg);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
2021-12-06 14:52:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(forced_status, status);
|
2021-12-06 14:52:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fragments_count) {
|
|
|
|
TEST_ASSERT((input->len / fragments_count) > 0);
|
2021-12-06 14:52:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (int i = 0; i < fragments_count; i++) {
|
2021-12-06 14:52:12 +01:00
|
|
|
int fragment_size = input->len / fragments_count;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i == fragments_count - 1) {
|
|
|
|
fragment_size += (input->len % fragments_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_mac_update(&operation,
|
|
|
|
input_x, fragment_size);
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 2 + i);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(PSA_ERROR_BAD_STATE, status);
|
2021-12-06 14:52:12 +01:00
|
|
|
}
|
|
|
|
input_x += fragment_size;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_verify_finish(&operation,
|
|
|
|
expected_mac->x,
|
|
|
|
expected_mac->len);
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 3 + fragments_count);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forced_status == PSA_SUCCESS ||
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED) {
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(PSA_ERROR_BAD_STATE, status);
|
2021-12-06 14:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_mac_abort(&operation));
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 3 + fragments_count);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_mac_hooks.hits, 1);
|
|
|
|
}
|
2021-12-06 14:52:12 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_mac_abort(&operation);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-12-06 14:52:12 +01:00
|
|
|
mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-06 13:17:36 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2023-01-11 14:50:10 +01:00
|
|
|
void builtin_key_export(int builtin_key_id_arg,
|
|
|
|
int builtin_key_type_arg,
|
|
|
|
int builtin_key_bits_arg,
|
|
|
|
int builtin_key_algorithm_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg)
|
2021-02-22 12:44:15 +01:00
|
|
|
{
|
|
|
|
psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg;
|
|
|
|
psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg;
|
|
|
|
psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg;
|
|
|
|
size_t builtin_key_bits = (size_t) builtin_key_bits_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make(0, builtin_key_id);
|
|
|
|
uint8_t *output_buffer = NULL;
|
2021-02-22 12:44:15 +01:00
|
|
|
size_t output_size = 0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
ASSERT_ALLOC(output_buffer, expected_output->len);
|
2021-02-22 12:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
actual_status = psa_export_key(key, output_buffer, expected_output->len, &output_size);
|
2021-02-22 12:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
PSA_ASSERT(actual_status);
|
|
|
|
TEST_EQUAL(output_size, expected_output->len);
|
|
|
|
ASSERT_COMPARE(output_buffer, output_size,
|
|
|
|
expected_output->x, expected_output->len);
|
2021-02-22 12:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
TEST_EQUAL(psa_get_key_bits(&attributes), builtin_key_bits);
|
|
|
|
TEST_EQUAL(psa_get_key_type(&attributes), builtin_key_type);
|
|
|
|
TEST_EQUAL(psa_get_key_algorithm(&attributes), builtin_key_alg);
|
|
|
|
} else {
|
|
|
|
if (actual_status != expected_status) {
|
|
|
|
fprintf(stderr, "Expected %d but got %d\n", expected_status, actual_status);
|
|
|
|
}
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
|
|
|
TEST_EQUAL(output_size, 0);
|
2021-02-22 12:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(output_buffer);
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-02-22 12:44:15 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-18 20:25:53 +01:00
|
|
|
/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2023-01-11 14:50:10 +01:00
|
|
|
void builtin_pubkey_export(int builtin_key_id_arg,
|
|
|
|
int builtin_key_type_arg,
|
|
|
|
int builtin_key_bits_arg,
|
|
|
|
int builtin_key_algorithm_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg)
|
2021-02-22 12:44:15 +01:00
|
|
|
{
|
|
|
|
psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg;
|
|
|
|
psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg;
|
|
|
|
psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg;
|
|
|
|
size_t builtin_key_bits = (size_t) builtin_key_bits_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make(0, builtin_key_id);
|
|
|
|
uint8_t *output_buffer = NULL;
|
2021-02-22 12:44:15 +01:00
|
|
|
size_t output_size = 0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
ASSERT_ALLOC(output_buffer, expected_output->len);
|
2021-02-22 12:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
actual_status = psa_export_public_key(key, output_buffer, expected_output->len, &output_size);
|
2021-02-22 12:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
PSA_ASSERT(actual_status);
|
|
|
|
TEST_EQUAL(output_size, expected_output->len);
|
|
|
|
ASSERT_COMPARE(output_buffer, output_size,
|
|
|
|
expected_output->x, expected_output->len);
|
2021-02-22 12:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
TEST_EQUAL(psa_get_key_bits(&attributes), builtin_key_bits);
|
|
|
|
TEST_EQUAL(psa_get_key_type(&attributes), builtin_key_type);
|
|
|
|
TEST_EQUAL(psa_get_key_algorithm(&attributes), builtin_key_alg);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(actual_status, expected_status);
|
|
|
|
TEST_EQUAL(output_size, 0);
|
2021-02-22 12:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(output_buffer);
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2021-02-22 12:44:15 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-04-06 18:20:29 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_compute(int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg,
|
|
|
|
int expected_status_arg)
|
2021-04-06 18:20:29 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_length;
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
ASSERT_ALLOC(output, PSA_HASH_LENGTH(alg));
|
2021-04-06 18:20:29 +02:00
|
|
|
|
2023-02-08 13:05:59 +01:00
|
|
|
/* Do this after psa_crypto_init() which may call hash drivers */
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_compute(alg, input->x, input->len,
|
|
|
|
output, PSA_HASH_LENGTH(alg),
|
|
|
|
&output_length), expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, forced_status);
|
2021-04-06 18:20:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(output, output_length, hash->x, hash->len);
|
2021-04-06 18:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 18:20:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_multipart_setup(int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg,
|
|
|
|
int expected_status_arg)
|
2021-04-06 18:20:29 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2023-02-08 13:05:59 +01:00
|
|
|
ASSERT_ALLOC(output, PSA_HASH_LENGTH(alg));
|
2021-04-06 18:20:29 +02:00
|
|
|
|
2023-02-08 13:05:59 +01:00
|
|
|
/* Do this after psa_crypto_init() which may call hash drivers */
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
2023-02-08 13:05:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_setup(&operation, alg), expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, forced_status);
|
|
|
|
|
|
|
|
if (expected_status == PSA_SUCCESS) {
|
|
|
|
PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits,
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 2);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, forced_status);
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_hash_finish(&operation,
|
|
|
|
output, PSA_HASH_LENGTH(alg),
|
|
|
|
&output_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits,
|
|
|
|
forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 4);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, forced_status);
|
|
|
|
|
|
|
|
ASSERT_COMPARE(output, output_length, hash->x, hash->len);
|
2021-04-06 18:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&operation);
|
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 18:20:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-01 14:52:54 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_multipart_update(int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg)
|
2021-05-01 14:52:54 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2023-02-08 13:05:59 +01:00
|
|
|
ASSERT_ALLOC(output, PSA_HASH_LENGTH(alg));
|
|
|
|
|
|
|
|
/* Do this after psa_crypto_init() which may call hash drivers */
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-05-01 14:52:54 +02:00
|
|
|
|
|
|
|
/*
|
2021-05-04 16:11:06 +02:00
|
|
|
* Update inactive operation, the driver shouldn't be called.
|
2021-05-01 14:52:54 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_update(&operation, input->x, input->len),
|
|
|
|
PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 0);
|
2021-05-01 14:52:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_setup(&operation, alg));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS);
|
2021-05-01 14:52:54 +02:00
|
|
|
|
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_update(&operation, input->x, input->len),
|
|
|
|
forced_status);
|
2021-05-01 14:52:54 +02:00
|
|
|
/* One or two more calls to the driver interface: update or update + abort */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits,
|
|
|
|
forced_status == PSA_SUCCESS ? 2 : 3);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, forced_status);
|
2021-05-01 14:52:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (forced_status == PSA_SUCCESS) {
|
2021-05-01 14:52:54 +02:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_finish(&operation,
|
|
|
|
output, PSA_HASH_LENGTH(alg),
|
|
|
|
&output_length));
|
2021-05-01 14:52:54 +02:00
|
|
|
/* Two calls to the driver interface: update + abort */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 2);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS);
|
2021-05-01 14:52:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(output, output_length, hash->x, hash->len);
|
2021-05-01 14:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&operation);
|
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2021-05-01 14:52:54 +02:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-01 15:02:51 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_multipart_finish(int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg)
|
2021-05-01 15:02:51 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
2023-02-08 13:05:59 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output, PSA_HASH_LENGTH(alg));
|
2021-05-01 15:02:51 +02:00
|
|
|
|
2023-02-08 13:05:59 +01:00
|
|
|
/* Do this after psa_crypto_init() which may call hash drivers */
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-05-01 15:02:51 +02:00
|
|
|
|
|
|
|
/*
|
2021-05-04 16:11:06 +02:00
|
|
|
* Finish inactive operation, the driver shouldn't be called.
|
2021-05-01 15:02:51 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_finish(&operation, output, PSA_HASH_LENGTH(alg),
|
|
|
|
&output_length),
|
|
|
|
PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 0);
|
2021-05-01 15:02:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_setup(&operation, alg));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS);
|
2021-05-01 15:02:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 2);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS);
|
2021-05-01 15:02:51 +02:00
|
|
|
|
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_finish(&operation,
|
|
|
|
output, PSA_HASH_LENGTH(alg),
|
|
|
|
&output_length),
|
|
|
|
forced_status);
|
2021-05-01 15:02:51 +02:00
|
|
|
/* Two more calls to the driver interface: finish + abort */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 4);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, forced_status);
|
2021-05-01 15:02:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (forced_status == PSA_SUCCESS) {
|
|
|
|
ASSERT_COMPARE(output, output_length, hash->x, hash->len);
|
|
|
|
}
|
2021-05-01 15:02:51 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&operation);
|
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2021-05-01 15:02:51 +02:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-06 18:20:29 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_clone(int alg_arg,
|
|
|
|
data_t *input, data_t *hash,
|
|
|
|
int forced_status_arg)
|
2021-04-06 18:20:29 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
psa_hash_operation_t source_operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t target_operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t output_length;
|
|
|
|
|
2023-02-08 13:05:59 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output, PSA_HASH_LENGTH(alg));
|
2021-04-06 18:20:29 +02:00
|
|
|
|
2023-02-08 13:05:59 +01:00
|
|
|
/* Do this after psa_crypto_init() which may call hash drivers */
|
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 18:20:29 +02:00
|
|
|
|
|
|
|
/*
|
2021-05-04 16:11:06 +02:00
|
|
|
* Clone inactive operation, the driver shouldn't be called.
|
2021-04-06 18:20:29 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_clone(&source_operation, &target_operation),
|
|
|
|
PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 0);
|
2021-04-06 18:20:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_setup(&source_operation, alg));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS);
|
2021-04-06 18:20:29 +02:00
|
|
|
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_hash_hooks.forced_status = forced_status;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_hash_clone(&source_operation, &target_operation),
|
|
|
|
forced_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits,
|
|
|
|
forced_status == PSA_SUCCESS ? 2 : 3);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, forced_status);
|
|
|
|
|
|
|
|
if (forced_status == PSA_SUCCESS) {
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_update(&target_operation,
|
|
|
|
input->x, input->len));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_hash_finish(&target_operation,
|
|
|
|
output, PSA_HASH_LENGTH(alg),
|
|
|
|
&output_length));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.hits, 3);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS);
|
|
|
|
|
|
|
|
ASSERT_COMPARE(output, output_length, hash->x, hash->len);
|
2021-04-06 18:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&source_operation);
|
|
|
|
psa_hash_abort(&target_operation);
|
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2021-04-13 12:41:34 +02:00
|
|
|
mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
|
2021-04-06 18:20:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-12-14 16:14:33 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void asymmetric_encrypt_decrypt(int alg_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *label,
|
|
|
|
data_t *fake_output_encrypt,
|
|
|
|
data_t *fake_output_decrypt,
|
|
|
|
int forced_status_encrypt_arg,
|
|
|
|
int forced_status_decrypt_arg,
|
|
|
|
int expected_status_encrypt_arg,
|
|
|
|
int expected_status_decrypt_arg)
|
2021-12-14 16:14:33 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = PSA_KEY_TYPE_RSA_KEY_PAIR;
|
2021-12-21 16:00:19 +01:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-12-14 16:14:33 +01:00
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_size;
|
|
|
|
size_t output_length = ~0;
|
|
|
|
unsigned char *output2 = NULL;
|
|
|
|
size_t output2_size;
|
|
|
|
size_t output2_length = ~0;
|
|
|
|
psa_status_t forced_status_encrypt = forced_status_encrypt_arg;
|
|
|
|
psa_status_t forced_status_decrypt = forced_status_decrypt_arg;
|
|
|
|
psa_status_t expected_status_encrypt = expected_status_encrypt_arg;
|
|
|
|
psa_status_t expected_status_decrypt = expected_status_decrypt_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks =
|
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks_init();
|
2021-12-14 16:14:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2021-12-14 16:14:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2021-12-14 16:14:33 +01:00
|
|
|
|
|
|
|
/* Determine the maximum ciphertext length */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
key_bits = psa_get_key_bits(&attributes);
|
2021-12-14 16:14:33 +01:00
|
|
|
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_status =
|
|
|
|
forced_status_encrypt;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output_encrypt->len > 0) {
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output =
|
|
|
|
fake_output_encrypt->x;
|
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length =
|
|
|
|
fake_output_encrypt->len;
|
2021-12-14 16:14:33 +01:00
|
|
|
output_size = fake_output_encrypt->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output, output_size);
|
|
|
|
} else {
|
|
|
|
output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
|
|
|
|
TEST_ASSERT(output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
|
|
|
|
ASSERT_ALLOC(output, output_size);
|
2021-12-14 16:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We test encryption by checking that encrypt-then-decrypt gives back
|
|
|
|
* the original plaintext because of the non-optional random
|
|
|
|
* part of encryption process which prevents using fixed vectors. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_asymmetric_encrypt(key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
label->x, label->len,
|
|
|
|
output, output_size,
|
|
|
|
&output_length), expected_status_encrypt);
|
2021-12-14 16:14:33 +01:00
|
|
|
/* We don't know what ciphertext length to expect, but check that
|
|
|
|
* it looks sensible. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(output_length <= output_size);
|
|
|
|
|
|
|
|
if (expected_status_encrypt == PSA_SUCCESS) {
|
|
|
|
if (fake_output_encrypt->len > 0) {
|
|
|
|
ASSERT_COMPARE(fake_output_encrypt->x, fake_output_encrypt->len,
|
|
|
|
output, output_length);
|
|
|
|
} else {
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_status =
|
|
|
|
forced_status_decrypt;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output_decrypt->len > 0) {
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output =
|
|
|
|
fake_output_decrypt->x;
|
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length =
|
|
|
|
fake_output_decrypt->len;
|
2021-12-14 16:14:33 +01:00
|
|
|
output2_size = fake_output_decrypt->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output2, output2_size);
|
|
|
|
} else {
|
2021-12-14 16:14:33 +01:00
|
|
|
output2_size = input_data->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(output2_size <=
|
|
|
|
PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
|
|
|
|
TEST_ASSERT(output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
|
|
|
|
ASSERT_ALLOC(output2, output2_size);
|
2021-12-14 16:14:33 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_asymmetric_decrypt(key, alg,
|
|
|
|
output, output_length,
|
|
|
|
label->x, label->len,
|
|
|
|
output2, output2_size,
|
|
|
|
&output2_length), expected_status_decrypt);
|
|
|
|
if (expected_status_decrypt == PSA_SUCCESS) {
|
|
|
|
if (fake_output_decrypt->len > 0) {
|
|
|
|
ASSERT_COMPARE(fake_output_decrypt->x, fake_output_decrypt->len,
|
|
|
|
output2, output2_length);
|
|
|
|
} else {
|
|
|
|
ASSERT_COMPARE(input_data->x, input_data->len,
|
|
|
|
output2, output2_length);
|
|
|
|
}
|
2021-12-14 16:14:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/*
|
|
|
|
* 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);
|
2021-12-14 16:14:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_free(output);
|
|
|
|
mbedtls_free(output2);
|
|
|
|
PSA_DONE();
|
2021-12-14 16:14:33 +01:00
|
|
|
}
|
2021-12-20 08:33:00 +01:00
|
|
|
/* END_CASE */
|
2022-02-17 12:23:35 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void asymmetric_decrypt(int alg_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *label,
|
|
|
|
data_t *expected_output_data,
|
|
|
|
data_t *fake_output_decrypt,
|
|
|
|
int forced_status_decrypt_arg,
|
|
|
|
int expected_status_decrypt_arg)
|
2022-02-17 12:23:35 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = PSA_KEY_TYPE_RSA_KEY_PAIR;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_size;
|
|
|
|
size_t output_length = ~0;
|
|
|
|
psa_status_t forced_status_decrypt = forced_status_decrypt_arg;
|
|
|
|
psa_status_t expected_status_decrypt = expected_status_decrypt_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks =
|
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks_init();
|
2022-02-17 12:23:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2022-02-17 12:23:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2022-02-17 12:23:35 +01:00
|
|
|
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_status =
|
|
|
|
forced_status_decrypt;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output_decrypt->len > 0) {
|
2022-03-07 10:14:07 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output =
|
|
|
|
fake_output_decrypt->x;
|
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length =
|
|
|
|
fake_output_decrypt->len;
|
2022-02-17 12:23:35 +01:00
|
|
|
output_size = fake_output_decrypt->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output, output_size);
|
|
|
|
} else {
|
2022-02-17 12:23:35 +01:00
|
|
|
output_size = expected_output_data->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output, expected_output_data->len);
|
2022-02-17 12:23:35 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_asymmetric_decrypt(key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
label->x, label->len,
|
|
|
|
output, output_size,
|
|
|
|
&output_length), expected_status_decrypt);
|
|
|
|
if (expected_status_decrypt == PSA_SUCCESS) {
|
|
|
|
TEST_EQUAL(output_length, expected_output_data->len);
|
|
|
|
ASSERT_COMPARE(expected_output_data->x, expected_output_data->len,
|
|
|
|
output, output_length);
|
2022-02-17 12:23:35 +01:00
|
|
|
}
|
|
|
|
exit:
|
|
|
|
/*
|
|
|
|
* 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);
|
2022-02-17 12:23:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2022-02-17 12:23:35 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-03-07 16:43:28 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void asymmetric_encrypt(int alg_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *modulus,
|
|
|
|
data_t *private_exponent,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *label,
|
|
|
|
data_t *fake_output_encrypt,
|
|
|
|
int forced_status_encrypt_arg,
|
|
|
|
int expected_status_encrypt_arg)
|
2022-03-07 16:43:28 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = PSA_KEY_TYPE_RSA_PUBLIC_KEY;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_size;
|
|
|
|
size_t output_length = ~0;
|
|
|
|
psa_status_t forced_status_encrypt = forced_status_encrypt_arg;
|
|
|
|
psa_status_t expected_status_encrypt = expected_status_encrypt_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2022-03-07 16:43:28 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks =
|
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2022-03-07 16:43:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2022-03-07 16:43:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
size_t key_bits = psa_get_key_bits(&attributes);
|
2022-03-07 21:13:29 +01:00
|
|
|
|
2022-03-07 16:43:28 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_status =
|
|
|
|
forced_status_encrypt;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fake_output_encrypt->len > 0) {
|
2022-03-07 16:43:28 +01:00
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output =
|
|
|
|
fake_output_encrypt->x;
|
|
|
|
mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length =
|
|
|
|
fake_output_encrypt->len;
|
|
|
|
output_size = fake_output_encrypt->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output, output_size);
|
|
|
|
} else {
|
|
|
|
output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
|
|
|
|
ASSERT_ALLOC(output, output_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_EQUAL(psa_asymmetric_encrypt(key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
label->x, label->len,
|
|
|
|
output, output_size,
|
|
|
|
&output_length), expected_status_encrypt);
|
|
|
|
if (expected_status_encrypt == PSA_SUCCESS) {
|
|
|
|
if (fake_output_encrypt->len > 0) {
|
|
|
|
TEST_EQUAL(fake_output_encrypt->len, output_length);
|
|
|
|
ASSERT_COMPARE(fake_output_encrypt->x, fake_output_encrypt->len,
|
|
|
|
output, output_length);
|
|
|
|
} else {
|
2022-03-08 10:32:18 +01:00
|
|
|
/* Perform sanity checks on the output */
|
|
|
|
#if PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_RSA(key_type)) {
|
|
|
|
if (!sanity_check_rsa_encryption_result(
|
2022-03-08 10:32:18 +01:00
|
|
|
alg, modulus, private_exponent,
|
|
|
|
input_data,
|
2023-01-11 14:50:10 +01:00
|
|
|
output, output_length)) {
|
2022-03-08 10:32:18 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} else
|
2022-03-08 10:32:18 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
(void) modulus;
|
|
|
|
(void) private_exponent;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(!"Encryption sanity checks not implemented for this key type");
|
2022-03-08 10:32:18 +01:00
|
|
|
}
|
2022-03-07 16:43:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
/*
|
|
|
|
* 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);
|
2022-03-07 16:43:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2022-03-07 16:43:28 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void aead_encrypt_setup(int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *expected_ciphertext,
|
|
|
|
data_t *expected_tag,
|
|
|
|
int forced_status_arg,
|
|
|
|
int expected_status_arg)
|
2022-01-21 16:07:22 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
uint8_t *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
size_t finish_output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
size_t tag_length = 0;
|
|
|
|
uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
|
|
|
|
|
|
|
|
psa_aead_operation_t operation = psa_aead_operation_init();
|
|
|
|
|
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_INIT();
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
mbedtls_test_driver_aead_hooks.forced_status = forced_status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
|
|
|
PSA_ASSERT(psa_get_key_attributes(key, &attributes));
|
|
|
|
key_bits = psa_get_key_bits(&attributes);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
|
|
|
|
alg);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
|
|
|
|
* should be exact. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(output_size,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
|
|
|
|
TEST_ASSERT(output_size <=
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
|
|
|
|
ASSERT_ALLOC(output_data, output_size);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_aead_encrypt_setup(&operation, key, alg);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(status, expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_encrypt_setup, 1);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
2022-01-21 16:07:22 +01:00
|
|
|
/* Set the nonce. */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_set_nonce,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* Check hooks hits and
|
|
|
|
* set length (additional data and data to encrypt) */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
|
|
|
|
input_data->len));
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_set_lengths,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* Pass the additional data */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
|
|
|
|
additional_data->len));
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_update_ad,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* Pass the data to encrypt */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
|
|
|
|
output_data, output_size, &output_length));
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_update,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* Finish the encryption operation */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_finish(&operation, output_data + output_length,
|
|
|
|
output_size - output_length,
|
|
|
|
&finish_output_length, tag_buffer,
|
|
|
|
PSA_AEAD_TAG_MAX_SIZE, &tag_length));
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_finish,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_abort,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* Compare output_data and expected_ciphertext */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(expected_ciphertext->x, expected_ciphertext->len,
|
|
|
|
output_data, output_length + finish_output_length);
|
2022-01-21 16:07:22 +01:00
|
|
|
|
|
|
|
/* Compare tag and expected_tag */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(expected_tag->x, expected_tag->len, tag_buffer, tag_length);
|
2022-01-21 16:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* Cleanup */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_destroy_key(key));
|
|
|
|
mbedtls_free(output_data);
|
|
|
|
PSA_DONE();
|
2022-01-21 16:07:22 +01:00
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-02-17 00:55:37 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void aead_decrypt_setup(int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_ciphertext,
|
|
|
|
data_t *input_tag,
|
|
|
|
data_t *expected_result,
|
|
|
|
int forced_status_arg,
|
|
|
|
int expected_status_arg)
|
2022-02-17 00:55:37 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
2022-04-06 15:42:10 +02:00
|
|
|
size_t verify_output_length = 0;
|
2022-02-17 00:55:37 +01:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
|
|
|
|
psa_aead_operation_t operation = psa_aead_operation_init();
|
|
|
|
mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_INIT();
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
2022-02-17 00:55:37 +01:00
|
|
|
|
|
|
|
output_size = input_ciphertext->len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(output_data, output_size);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
|
|
|
mbedtls_test_driver_aead_hooks.forced_status = forced_status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_aead_decrypt_setup(&operation, key, alg);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(status, (forced_status == PSA_ERROR_NOT_SUPPORTED) ?
|
|
|
|
PSA_SUCCESS : forced_status);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(status, expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_decrypt_setup, 1);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_set_nonce,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
|
|
|
|
input_ciphertext->len));
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_set_lengths,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
|
|
|
|
additional_data->len));
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_update_ad,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_update(&operation, input_ciphertext->x,
|
|
|
|
input_ciphertext->len, output_data,
|
|
|
|
output_size, &output_length));
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_update,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2022-04-22 12:36:07 +02:00
|
|
|
/* Offset applied to output_data in order to handle cases where verify()
|
|
|
|
* outputs further data */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_aead_verify(&operation, output_data + output_length,
|
|
|
|
output_size - output_length,
|
|
|
|
&verify_output_length, input_tag->x,
|
|
|
|
input_tag->len));
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_verify,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2022-04-22 12:36:07 +02:00
|
|
|
/* Since this is a decryption operation,
|
|
|
|
* finish should never be hit */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_finish, 0);
|
2022-02-17 00:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_aead_hooks.hits_abort,
|
|
|
|
forced_status == PSA_SUCCESS ? 1 : 0);
|
2022-04-06 16:29:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(expected_result->x, expected_result->len,
|
|
|
|
output_data, output_length + verify_output_length);
|
2022-02-17 00:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_destroy_key(key));
|
|
|
|
mbedtls_free(output_data);
|
|
|
|
PSA_DONE();
|
2022-02-17 00:55:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2023-02-27 13:00:57 +01:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
|
2022-12-14 08:27:46 +01:00
|
|
|
void pake_operations(data_t *pw_data, int forced_status_setup_arg, int forced_status_arg,
|
2023-02-22 11:02:40 +01:00
|
|
|
data_t *forced_output, int expected_status_arg,
|
2022-12-14 08:27:46 +01:00
|
|
|
int fut)
|
2022-11-22 14:35:44 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_status_t forced_status = forced_status_arg;
|
|
|
|
psa_status_t forced_status_setup = forced_status_setup_arg;
|
2023-02-22 11:02:40 +01:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2022-11-22 14:35:44 +01:00
|
|
|
psa_pake_operation_t operation = psa_pake_operation_init();
|
|
|
|
psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
|
|
|
|
psa_key_derivation_operation_t implicit_key =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_pake_primitive_t primitive = PSA_PAKE_PRIMITIVE(
|
|
|
|
PSA_PAKE_PRIMITIVE_TYPE_ECC,
|
|
|
|
PSA_ECC_FAMILY_SECP_R1, 256);
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
unsigned char *input_buffer = NULL;
|
2023-02-22 11:02:40 +01:00
|
|
|
const size_t size_key_share = PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, primitive,
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_PAKE_STEP_KEY_SHARE);
|
|
|
|
unsigned char *output_buffer = NULL;
|
|
|
|
size_t output_len = 0;
|
2023-02-22 11:02:40 +01:00
|
|
|
size_t output_size = PSA_PAKE_OUTPUT_SIZE(PSA_ALG_JPAKE, primitive,
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_PAKE_STEP_KEY_SHARE);
|
2023-02-22 11:02:40 +01:00
|
|
|
int in_driver = (forced_status_setup_arg == PSA_SUCCESS);
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
ASSERT_ALLOC(input_buffer,
|
2023-02-22 11:02:40 +01:00
|
|
|
PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, primitive,
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_PAKE_STEP_KEY_SHARE));
|
|
|
|
memset(input_buffer, 0xAA, size_key_share);
|
|
|
|
|
|
|
|
ASSERT_ALLOC(output_buffer,
|
2023-02-22 11:02:40 +01:00
|
|
|
PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, primitive,
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_PAKE_STEP_KEY_SHARE));
|
|
|
|
memset(output_buffer, 0x55, output_size);
|
|
|
|
|
|
|
|
PSA_INIT();
|
|
|
|
|
2023-02-22 11:02:40 +01:00
|
|
|
mbedtls_test_driver_pake_hooks = mbedtls_test_driver_pake_hooks_init();
|
|
|
|
|
2022-11-22 14:35:44 +01:00
|
|
|
if (pw_data->len > 0) {
|
2023-02-22 11:02:40 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
|
|
|
|
psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
|
|
|
|
&key));
|
|
|
|
}
|
|
|
|
|
2023-02-22 11:02:40 +01:00
|
|
|
psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
|
2022-11-22 14:35:44 +01:00
|
|
|
psa_pake_cs_set_primitive(&cipher_suite, primitive);
|
2023-02-22 11:02:40 +01:00
|
|
|
psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = forced_status_setup;
|
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
/* Collecting input stage (no driver entry points) */
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
|
|
|
|
PSA_SUCCESS);
|
|
|
|
|
2023-03-13 16:06:09 +01:00
|
|
|
PSA_ASSERT(psa_pake_set_user(&operation, jpake_server_id, sizeof(jpake_server_id)));
|
|
|
|
PSA_ASSERT(psa_pake_set_peer(&operation, jpake_client_id, sizeof(jpake_client_id)));
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(psa_pake_set_password_key(&operation, key),
|
|
|
|
PSA_SUCCESS);
|
|
|
|
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 0);
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
/* Computation stage (driver entry points) */
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
switch (fut) {
|
|
|
|
case 0: /* setup (via input) */
|
|
|
|
/* --- psa_pake_input (driver: setup, input) --- */
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_setup_status = forced_status_setup;
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = forced_status;
|
|
|
|
TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
input_buffer, size_key_share),
|
2023-02-22 11:02:40 +01:00
|
|
|
expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.setup, 1);
|
2022-12-14 08:27:46 +01:00
|
|
|
break;
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
case 1: /* setup (via output) */
|
2023-02-22 11:02:40 +01:00
|
|
|
/* --- psa_pake_output (driver: setup, output) --- */
|
2022-12-14 08:27:46 +01:00
|
|
|
mbedtls_test_driver_pake_hooks.forced_setup_status = forced_status_setup;
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = forced_status;
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
output_buffer, output_size, &output_len),
|
|
|
|
expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.setup, 1);
|
2022-12-14 08:27:46 +01:00
|
|
|
break;
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
case 2: /* input */
|
2023-02-24 08:41:39 +01:00
|
|
|
/* --- psa_pake_input (driver: setup, input, abort) --- */
|
2022-12-14 08:27:46 +01:00
|
|
|
mbedtls_test_driver_pake_hooks.forced_setup_status = forced_status_setup;
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = forced_status;
|
|
|
|
TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
input_buffer, size_key_share),
|
2023-02-22 11:02:40 +01:00
|
|
|
expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, in_driver ? 3 : 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.setup, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.input, in_driver ? 1 : 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.abort, in_driver ? 1 : 0);
|
2022-12-14 08:27:46 +01:00
|
|
|
break;
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
case 3: /* output */
|
2023-02-22 11:02:40 +01:00
|
|
|
/* --- psa_pake_output (driver: setup, output, (abort)) --- */
|
2022-12-14 08:27:46 +01:00
|
|
|
mbedtls_test_driver_pake_hooks.forced_setup_status = forced_status_setup;
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = forced_status;
|
|
|
|
if (forced_output->len > 0) {
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_output = forced_output->x;
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_output_length = forced_output->len;
|
|
|
|
}
|
|
|
|
TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
output_buffer, output_size, &output_len),
|
2023-02-22 11:02:40 +01:00
|
|
|
expected_status);
|
2022-12-14 08:27:46 +01:00
|
|
|
|
|
|
|
if (forced_output->len > 0) {
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, in_driver ? 2 : 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.setup, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.output, in_driver ? 1 : 0);
|
2022-12-14 08:27:46 +01:00
|
|
|
TEST_EQUAL(output_len, forced_output->len);
|
|
|
|
TEST_EQUAL(memcmp(output_buffer, forced_output->x, output_len), 0);
|
2023-02-08 09:12:42 +01:00
|
|
|
} else {
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, in_driver ? 3 : 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.setup, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.output, in_driver ? 1 : 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.abort, in_driver ? 1 : 0);
|
2022-12-14 08:27:46 +01:00
|
|
|
}
|
|
|
|
break;
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
case 4: /* get_implicit_key */
|
|
|
|
/* Call driver setup indirectly */
|
|
|
|
TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
input_buffer, size_key_share),
|
|
|
|
PSA_SUCCESS);
|
2022-11-22 14:35:44 +01:00
|
|
|
|
2022-12-22 11:22:45 +01:00
|
|
|
/* Simulate that we are ready to get implicit key. */
|
2023-01-26 08:46:37 +01:00
|
|
|
operation.computation_stage.jpake.input_step = PSA_PAKE_STEP_DERIVE;
|
|
|
|
operation.computation_stage.jpake.output_step = PSA_PAKE_STEP_DERIVE;
|
2022-12-22 11:22:45 +01:00
|
|
|
|
2022-12-14 08:27:46 +01:00
|
|
|
/* --- psa_pake_get_implicit_key --- */
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = forced_status;
|
2023-02-23 17:28:23 +01:00
|
|
|
memset(&mbedtls_test_driver_pake_hooks.hits, 0,
|
|
|
|
sizeof(mbedtls_test_driver_pake_hooks.hits));
|
2022-12-14 08:27:46 +01:00
|
|
|
TEST_EQUAL(psa_pake_get_implicit_key(&operation, &implicit_key),
|
2023-02-22 11:02:40 +01:00
|
|
|
expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 2);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.implicit_key, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.abort, 1);
|
2022-12-14 08:27:46 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5: /* abort */
|
|
|
|
/* Call driver setup indirectly */
|
|
|
|
TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
input_buffer, size_key_share),
|
|
|
|
PSA_SUCCESS);
|
|
|
|
|
|
|
|
/* --- psa_pake_abort --- */
|
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = forced_status;
|
2023-02-23 17:28:23 +01:00
|
|
|
memset(&mbedtls_test_driver_pake_hooks.hits, 0,
|
|
|
|
sizeof(mbedtls_test_driver_pake_hooks.hits));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(psa_pake_abort(&operation), expected_status);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 1);
|
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.abort, 1);
|
2022-12-14 08:27:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
/* Clean up */
|
2022-12-14 08:27:46 +01:00
|
|
|
mbedtls_test_driver_pake_hooks.forced_setup_status = PSA_SUCCESS;
|
2022-11-22 14:35:44 +01:00
|
|
|
mbedtls_test_driver_pake_hooks.forced_status = PSA_SUCCESS;
|
|
|
|
TEST_EQUAL(psa_pake_abort(&operation), PSA_SUCCESS);
|
|
|
|
exit:
|
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes(&attributes);
|
|
|
|
mbedtls_free(input_buffer);
|
|
|
|
mbedtls_free(output_buffer);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_test_driver_pake_hooks =
|
|
|
|
mbedtls_test_driver_pake_hooks_init();
|
|
|
|
PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-03-08 09:56:29 +01:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */
|
2022-11-22 14:35:44 +01:00
|
|
|
void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
|
|
|
|
int derive_alg_arg, data_t *pw_data,
|
|
|
|
int client_input_first, int in_driver)
|
|
|
|
{
|
|
|
|
psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
|
|
|
|
psa_pake_operation_t server = psa_pake_operation_init();
|
|
|
|
psa_pake_operation_t client = psa_pake_operation_init();
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_algorithm_t hash_alg = hash_arg;
|
|
|
|
psa_algorithm_t derive_alg = derive_alg_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_derivation_operation_t server_derive =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_key_derivation_operation_t client_derive =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
pake_in_driver = in_driver;
|
2022-12-14 08:27:46 +01:00
|
|
|
/* driver setup is called indirectly through pake_output/pake_input */
|
|
|
|
if (pake_in_driver) {
|
|
|
|
pake_expected_hit_count = 2;
|
|
|
|
} else {
|
|
|
|
pake_expected_hit_count = 1;
|
|
|
|
}
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
PSA_INIT();
|
|
|
|
|
2023-02-22 11:02:40 +01:00
|
|
|
mbedtls_test_driver_pake_hooks = mbedtls_test_driver_pake_hooks_init();
|
|
|
|
|
2022-11-22 14:35:44 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
|
|
|
|
PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
|
|
|
|
&key));
|
|
|
|
|
|
|
|
psa_pake_cs_set_algorithm(&cipher_suite, alg);
|
|
|
|
psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
|
|
|
|
psa_pake_cs_set_hash(&cipher_suite, hash_alg);
|
|
|
|
|
|
|
|
/* Get shared key */
|
|
|
|
PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
|
|
|
|
PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
|
|
|
|
|
|
|
|
if (PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
|
|
|
|
PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SEED,
|
|
|
|
(const uint8_t *) "", 0));
|
|
|
|
PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SEED,
|
|
|
|
(const uint8_t *) "", 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pake_in_driver) {
|
2022-12-14 08:27:46 +01:00
|
|
|
mbedtls_test_driver_pake_hooks.forced_setup_status = PSA_ERROR_NOT_SUPPORTED;
|
2022-11-22 14:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 0);
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 0);
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
|
2023-03-13 16:06:09 +01:00
|
|
|
PSA_ASSERT(psa_pake_set_user(&server, jpake_server_id, sizeof(jpake_server_id)));
|
|
|
|
PSA_ASSERT(psa_pake_set_peer(&server, jpake_client_id, sizeof(jpake_client_id)));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 0);
|
2023-03-13 16:06:09 +01:00
|
|
|
PSA_ASSERT(psa_pake_set_user(&client, jpake_client_id, sizeof(jpake_client_id)));
|
|
|
|
PSA_ASSERT(psa_pake_set_peer(&client, jpake_server_id, sizeof(jpake_server_id)));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 0);
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_ASSERT(psa_pake_set_password_key(&server, key));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 0);
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_ASSERT(psa_pake_set_password_key(&client, key));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total, 0);
|
2022-11-22 14:35:44 +01:00
|
|
|
|
|
|
|
/* First round */
|
|
|
|
ecjpake_do_round(alg, primitive_arg, &server, &client,
|
|
|
|
client_input_first, 1);
|
|
|
|
|
|
|
|
/* Second round */
|
|
|
|
ecjpake_do_round(alg, primitive_arg, &server, &client,
|
|
|
|
client_input_first, 2);
|
|
|
|
|
2022-12-22 13:34:47 +01:00
|
|
|
/* After the key is obtained operation is aborted.
|
2022-12-22 11:22:45 +01:00
|
|
|
Adapt counter of expected hits. */
|
|
|
|
if (pake_in_driver) {
|
|
|
|
pake_expected_hit_count++;
|
|
|
|
}
|
|
|
|
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
2022-12-22 11:22:45 +01:00
|
|
|
|
2022-12-22 13:34:47 +01:00
|
|
|
/* After the key is obtained operation is aborted.
|
2022-12-22 11:22:45 +01:00
|
|
|
Adapt counter of expected hits. */
|
|
|
|
if (pake_in_driver) {
|
|
|
|
pake_expected_hit_count++;
|
|
|
|
}
|
|
|
|
|
2022-11-22 14:35:44 +01:00
|
|
|
PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
|
2023-02-22 11:02:40 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_driver_pake_hooks.hits.total,
|
2022-11-22 14:35:44 +01:00
|
|
|
pake_in_driver ? pake_expected_hit_count++ : pake_expected_hit_count);
|
|
|
|
exit:
|
|
|
|
psa_key_derivation_abort(&server_derive);
|
|
|
|
psa_key_derivation_abort(&client_derive);
|
|
|
|
psa_destroy_key(key);
|
|
|
|
psa_pake_abort(&server);
|
|
|
|
psa_pake_abort(&client);
|
|
|
|
PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|