2018-10-24 11:45:18 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2023-07-26 18:58:48 +02:00
|
|
|
#include "psa/crypto.h"
|
2018-10-24 11:45:18 +02:00
|
|
|
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2018-12-17 23:35:42 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_finish(int alg_arg, data_t *input, data_t *expected_hash)
|
2018-10-24 11:45:18 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
|
|
|
|
size_t actual_hash_length;
|
2019-01-04 12:47:44 +01:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2018-10-24 11:45:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2018-12-18 00:18:46 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_setup(&operation, alg));
|
|
|
|
PSA_ASSERT(psa_hash_update(&operation,
|
|
|
|
input->x, input->len));
|
|
|
|
PSA_ASSERT(psa_hash_finish(&operation,
|
|
|
|
actual_hash, sizeof(actual_hash),
|
|
|
|
&actual_hash_length));
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(expected_hash->x, expected_hash->len,
|
2023-07-27 15:17:27 +02:00
|
|
|
actual_hash, actual_hash_length);
|
2018-10-24 11:45:18 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&operation);
|
|
|
|
PSA_DONE();
|
2018-10-24 11:45:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-07-26 18:58:48 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hmac(int alg_arg, char *input, data_t *expected_mac)
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = PSA_ALG_HMAC(alg_arg);
|
|
|
|
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
|
|
|
|
const uint8_t key_data[] = { // 32 bytes of 0xaa
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
|
|
|
|
};
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
|
2023-07-27 13:54:09 +02:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE);
|
2023-07-26 18:58:48 +02:00
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), &key));
|
|
|
|
|
|
|
|
uint8_t mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
|
|
|
|
size_t mac_length = 0;
|
|
|
|
|
2023-07-27 13:54:09 +02:00
|
|
|
size_t input_len = strlen(input);
|
2023-07-27 14:28:41 +02:00
|
|
|
PSA_ASSERT(psa_mac_compute(key, alg, (uint8_t const *) input, input_len, mac, sizeof(mac),
|
|
|
|
&mac_length));
|
2023-07-26 18:58:48 +02:00
|
|
|
|
2023-07-27 14:00:02 +02:00
|
|
|
// manual comparison against expected MAC
|
2023-07-26 18:58:48 +02:00
|
|
|
ASSERT_COMPARE(expected_mac->x, expected_mac->len, mac, mac_length);
|
|
|
|
|
2023-07-27 14:00:02 +02:00
|
|
|
// use psa_mac_verify to compare to expected MAC
|
2023-07-27 14:28:41 +02:00
|
|
|
PSA_ASSERT(psa_mac_verify(key, alg, (uint8_t const *) input, input_len, expected_mac->x,
|
|
|
|
expected_mac->len));
|
2023-07-27 13:54:09 +02:00
|
|
|
|
2023-07-27 14:00:02 +02:00
|
|
|
// corrupt the MAC and check that psa_mac_verify fails
|
|
|
|
expected_mac->x[0] ^= 0x7f;
|
2023-07-27 14:28:41 +02:00
|
|
|
TEST_EQUAL(psa_mac_verify(key, alg, (uint8_t const *) input, input_len, expected_mac->x,
|
|
|
|
expected_mac->len), PSA_ERROR_INVALID_SIGNATURE);
|
2023-07-27 14:00:02 +02:00
|
|
|
|
2023-07-26 18:58:48 +02:00
|
|
|
PSA_ASSERT(psa_destroy_key(key));
|
|
|
|
exit:
|
|
|
|
PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-10-24 11:45:18 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_verify(int alg_arg, data_t *input, data_t *expected_hash)
|
2018-10-24 11:45:18 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2019-01-04 12:47:44 +01:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2018-10-24 11:45:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2018-10-24 11:45:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_hash_setup(&operation, alg));
|
|
|
|
PSA_ASSERT(psa_hash_update(&operation,
|
|
|
|
input->x,
|
|
|
|
input->len));
|
|
|
|
PSA_ASSERT(psa_hash_verify(&operation,
|
|
|
|
expected_hash->x,
|
|
|
|
expected_hash->len));
|
2018-10-24 11:45:18 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&operation);
|
|
|
|
PSA_DONE();
|
2018-10-24 11:45:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void hash_multi_part(int alg_arg, data_t *input, data_t *expected_hash)
|
2018-10-24 11:45:18 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-10-29 12:34:37 +01:00
|
|
|
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
|
2018-10-24 11:45:18 +02:00
|
|
|
size_t actual_hash_length;
|
2019-01-04 12:47:44 +01:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2019-01-19 12:03:41 +01:00
|
|
|
psa_hash_operation_t operation2 = PSA_HASH_OPERATION_INIT;
|
2018-10-29 12:34:37 +01:00
|
|
|
uint32_t len = 0;
|
2018-10-24 11:45:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
|
|
|
|
do {
|
|
|
|
memset(actual_hash, 0, sizeof(actual_hash));
|
|
|
|
PSA_ASSERT(psa_hash_setup(&operation, alg));
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_hash_update(&operation,
|
|
|
|
input->x, len));
|
|
|
|
PSA_ASSERT(psa_hash_clone(&operation, &operation2));
|
|
|
|
PSA_ASSERT(psa_hash_update(&operation,
|
|
|
|
input->x + len, input->len - len));
|
|
|
|
PSA_ASSERT(psa_hash_update(&operation2,
|
|
|
|
input->x + len, input->len - len));
|
|
|
|
|
|
|
|
PSA_ASSERT(psa_hash_finish(&operation,
|
|
|
|
actual_hash, sizeof(actual_hash),
|
|
|
|
&actual_hash_length));
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(expected_hash->x, expected_hash->len,
|
2023-07-27 15:17:27 +02:00
|
|
|
actual_hash, actual_hash_length);
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
PSA_ASSERT(psa_hash_finish(&operation2,
|
|
|
|
actual_hash, sizeof(actual_hash),
|
|
|
|
&actual_hash_length));
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(expected_hash->x, expected_hash->len,
|
2023-07-27 15:17:27 +02:00
|
|
|
actual_hash, actual_hash_length);
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (len++ != input->len);
|
2018-10-24 11:45:18 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&operation);
|
|
|
|
psa_hash_abort(&operation2);
|
|
|
|
PSA_DONE();
|
2018-10-24 11:45:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|