Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/sys/psa_crypto: test interleaved hash operations #20606

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/sys/psa_crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ CFLAGS += -DCONFIG_PSA_SINGLE_KEY_COUNT=1
USEMODULE += psa_asymmetric
USEMODULE += psa_asymmetric_ecc_ed25519

USEMODULE += psa_hash
USEMODULE += psa_hash_sha_256

include $(RIOTBASE)/Makefile.include
1 change: 1 addition & 0 deletions tests/sys/psa_crypto/Makefile.ci
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
BOARD_INSUFFICIENT_MEMORY := \
arduino-duemilanove \
arduino-leonardo \
arduino-mega2560 \
arduino-nano \
arduino-uno \
atmega328p \
Expand Down
46 changes: 46 additions & 0 deletions tests/sys/psa_crypto/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
addFailure(msg, line, file);
}

#define TEST_ASSERT_PSA(func_, do_) { psa_status_t ret = func_; if (ret != PSA_SUCCESS) { addFailurePSA(#func_, ret, __LINE__, __FILE__); do_; } }

Check warning on line 34 in tests/sys/psa_crypto/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
#define TEST_ASSERT_PSA_CLEANUP(func_) TEST_ASSERT_PSA(func_, goto cleanup)
#define TEST_ASSERT_PSA_RETURN(func_) TEST_ASSERT_PSA(func_, return)

Check warning on line 36 in tests/sys/psa_crypto/main.c

View workflow job for this annotation

GitHub Actions / static-tests

keyword 'return' not immediately followed by a semicolon or a single space
#define TEST_ASSERT_PSA_CONTINUE(func_) TEST_ASSERT_PSA(func_, )

/*
Expand All @@ -58,16 +58,61 @@
TEST_ASSERT_PSA_CLEANUP(psa_crypto_init());
TEST_ASSERT_PSA_CLEANUP(psa_generate_key(&key_attr, &key_id));

TEST_ASSERT_PSA_CLEANUP(psa_export_public_key(key_id, key_data, sizeof(key_data), &key_data_len));

Check warning on line 61 in tests/sys/psa_crypto/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
TEST_ASSERT_PSA_CLEANUP(psa_crypto_init());
TEST_ASSERT_PSA_CLEANUP(psa_export_public_key(key_id, key_data, sizeof(key_data), &key_data_len));

Check warning on line 63 in tests/sys/psa_crypto/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

cleanup:
TEST_ASSERT_PSA_CONTINUE(psa_destroy_key(key_id));
}

/*
* Several interleaved hash operations shouldn't collide.
*/
static void test_hash_interleaved(void)
{
const psa_algorithm_t alg = PSA_ALG_SHA_256;

const uint8_t in1[1] = "a";
const uint8_t in2[1] = "b";

const uint8_t exp1[] = {
0xca, 0x97, 0x81, 0x12, 0xca, 0x1b, 0xbd, 0xca, 0xfa, 0xc2, 0x31, 0xb3,
0x9a, 0x23, 0xdc, 0x4d, 0xa7, 0x86, 0xef, 0xf8, 0x14, 0x7c, 0x4e, 0x72,
0xb9, 0x80, 0x77, 0x85, 0xaf, 0xee, 0x48, 0xbb
};
const uint8_t exp2[] = {
0x3e, 0x23, 0xe8, 0x16, 0x00, 0x39, 0x59, 0x4a, 0x33, 0x89, 0x4f, 0x65,
0x64, 0xe1, 0xb1, 0x34, 0x8b, 0xbd, 0x7a, 0x00, 0x88, 0xd4, 0x2c, 0x4a,
0xcb, 0x73, 0xee, 0xae, 0xd5, 0x9c, 0x00, 0x9d
};

uint8_t out[PSA_HASH_LENGTH(alg)];
size_t out_len;

psa_hash_operation_t op1 = PSA_HASH_OPERATION_INIT;
psa_hash_operation_t op2 = PSA_HASH_OPERATION_INIT;

TEST_ASSERT_PSA_CLEANUP(psa_hash_setup(&op1, alg));
TEST_ASSERT_PSA_CLEANUP(psa_hash_setup(&op2, alg));

TEST_ASSERT_PSA_CLEANUP(psa_hash_update(&op1, in1, sizeof(in1)));
TEST_ASSERT_PSA_CLEANUP(psa_hash_update(&op2, in2, sizeof(in2)));

TEST_ASSERT_PSA_CLEANUP(psa_hash_finish(&op1, out, sizeof(out), &out_len));
TEST_ASSERT_EQUAL_INT(sizeof(out), out_len);
TEST_ASSERT_EQUAL_INT(0, memcmp(out, exp1, sizeof(out)));
TEST_ASSERT_PSA_CLEANUP(psa_hash_finish(&op2, out, sizeof(out), &out_len));
TEST_ASSERT_EQUAL_INT(sizeof(out), out_len);
TEST_ASSERT_EQUAL_INT(0, memcmp(out, exp2, sizeof(out)));

cleanup:
TEST_ASSERT_PSA_CONTINUE(psa_hash_abort(&op1));
TEST_ASSERT_PSA_CONTINUE(psa_hash_abort(&op2));
}

/**
* Exporting and re-importing a private Ed25519 key should result in the same public key and signature.

Check warning on line 115 in tests/sys/psa_crypto/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
*/
static void test_exported_key_is_identical_when_imported_again_ed25519(void)
{
Expand Down Expand Up @@ -105,7 +150,7 @@
TEST_ASSERT_PSA_CLEANUP(psa_generate_key(&key_attr, &key_id));

// sign msg with generated keypair
TEST_ASSERT_PSA_CLEANUP(psa_sign_message(key_id, key_alg, msg, sizeof(msg), sig, sizeof(sig), &sig_len));

Check warning on line 153 in tests/sys/psa_crypto/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

// export public and private key, then free slot
TEST_ASSERT_PSA_CLEANUP(psa_export_public_key(key_id, pubkey, sizeof(pubkey), &pubkey_len));
Expand All @@ -119,7 +164,7 @@
TEST_ASSERT(pubkey_len == pubkey2_len && memcmp(pubkey, pubkey2, pubkey_len) == 0);

// sign msg with imported key and compare signatures
TEST_ASSERT_PSA_CLEANUP(psa_sign_message(key_id, key_alg, msg, sizeof(msg), sig2, sizeof(sig2), &sig2_len));

Check warning on line 167 in tests/sys/psa_crypto/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
TEST_ASSERT(sig_len == sig2_len && memcmp(sig, sig2, sig_len) == 0);

cleanup:
Expand Down Expand Up @@ -181,6 +226,7 @@
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_init_twice),
new_TestFixture(test_hash_interleaved),
new_TestFixture(test_exported_key_is_identical_when_imported_again_ed25519),
new_TestFixture(test_export_public_key_ed25519),
};
Expand Down
Loading