-
Notifications
You must be signed in to change notification settings - Fork 0
Mbed Crypto Tests #1
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
Conversation
Add tests setup code and random generator test.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
@@ -51,7 +82,8 @@ utest::v1::status_t test_setup(const size_t number_of_cases) | |||
} | |||
|
|||
Case cases[] = { | |||
Case("mbed-crypto random", test_crypto_random, case_failure_handler) | |||
Case("mbed-crypto random", test_crypto_random, case_failure_handler), | |||
Case("mbed-crypto asymmetric encrypt/decrypt", test_crypto_asymmetric_encrypt_decrypt, case_failure_handler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it break anything to always include the comma at the end of the line here?
If we can always include commas at the end, the diffs will be more clean (won't look like we are adding new tests that were already added in a previous commit).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I'll check if it breaks. I can also add a line break before the closing }
of the cases array instead, then it will prevent the false appearance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this once I raise an actual PR to mbed-os. I'll either rebase this branch or create a completely new branch.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
{ | ||
psa_algorithm_t alg = PSA_ALG_SHA_256; | ||
psa_hash_operation_t operation; | ||
unsigned char hash[] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this a hash of? Include a comment in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK will do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
encrypted, sizeof(encrypted), &output_len)); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_finish(&operation, encrypted + output_len, | ||
sizeof(encrypted) - output_len, &output_len)); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_abort(&operation)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add another TEST_ASSERT_EQUAL_UINT8_ARRAY here, to check that the encrypted value matches what we expect the data to get encrypted to.
Otherwise, this test would pass if the buffer was never encrypted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, but since the key is generated rather than imported I don't see how this can be done. The jira tasks states that the keys should be generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a separate key generation test. Not all tests must use generated keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change this to use a fixed imported key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to deterministic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
TESTS/mbed-crypto/sanity/main.cpp
Outdated
psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_CRYPT; | ||
size_t key_bits = 512; | ||
psa_key_policy_t policy; | ||
unsigned char input[100]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we test with a fixed input so we can check the result of the signature matches what we expect? Note that this would only work for deterministic signature algorithms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as before, the key is generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The signature test doesn't need to use a generated key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I'll change this to use a fixed imported key then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to deterministic.
Add the test to the test cases array, this was missed previously. Fix the buffer sizes in the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Patater I've updated the following tests to be deterministic:
test_crypto_symmetric_cipher_encrypt_decrypt
test_crypto_asymmetric_sign_verify
The test test_crypto_asymmetric_encrypt_decrypt
remains as is for now, i.e. uses a generated key and is non-deterministic. Please let me know if you want this one to be deterministic as well.
I've also addressed the rest of the comments in your review.
I haven't rebased the branch to keep the review process easier, once approved I'll either rebase and get rid of some of the commits or create an new branch altogether.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
@@ -51,7 +82,8 @@ utest::v1::status_t test_setup(const size_t number_of_cases) | |||
} | |||
|
|||
Case cases[] = { | |||
Case("mbed-crypto random", test_crypto_random, case_failure_handler) | |||
Case("mbed-crypto random", test_crypto_random, case_failure_handler), | |||
Case("mbed-crypto asymmetric encrypt/decrypt", test_crypto_asymmetric_encrypt_decrypt, case_failure_handler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this once I raise an actual PR to mbed-os. I'll either rebase this branch or create a completely new branch.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
{ | ||
psa_algorithm_t alg = PSA_ALG_SHA_256; | ||
psa_hash_operation_t operation; | ||
unsigned char hash[] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
encrypted, sizeof(encrypted), &output_len)); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_finish(&operation, encrypted + output_len, | ||
sizeof(encrypted) - output_len, &output_len)); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_abort(&operation)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to deterministic.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_CRYPT; | ||
size_t key_bits = 512; | ||
psa_key_policy_t policy; | ||
unsigned char input[100]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to deterministic.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
0x28, 0x8d, 0x76, 0xc0, 0xa7, 0x09, 0x50, 0x3f, | ||
0x87, 0x96, 0x1e, 0x96, 0x05, 0xcb, 0xb9, 0x6d | ||
}; | ||
unsigned char encrypted[sizeof(input)], decrypted[sizeof(input)], iv[sizeof(input)]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@itayzafrir is decrypted buffer size sizeof(input) or can be size of output, which is bigger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The input buffer is 16 bytes (one block as this is AES), and since we're using PSA_ALG_CBC_NO_PADDING
they should all be the same size.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
0x28, 0x8d, 0x76, 0xc0, 0xa7, 0x09, 0x50, 0x3f, | ||
0x87, 0x96, 0x1e, 0x96, 0x05, 0xcb, 0xb9, 0x6d | ||
}; | ||
unsigned char encrypted[sizeof(input)], decrypted[sizeof(input)], iv[sizeof(input)]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case the input
buffer will be changed in the future to larger one, this will cause to invalid IV
size.
I think it should be hard-coded to 16.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_generate_key(slot, key_type, key_bits, NULL, 0)); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_get_key_information(slot, NULL, &got_bits)); | ||
TEST_ASSERT_EQUAL(key_bits, got_bits); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_asymmetric_encrypt(slot, alg, input, sizeof(input), NULL, 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we've talked about this before, but "encrypt and then decrypt" is a bad example, as nobody will do that in practice. Encrypt should be an example. Decrypt should be a separate example.
This applies to both asymmetric and symmetric operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree for an example, but this is a test. Our test suite also has tests which encrypt and decrypt:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, fine to make our tests better later.
TESTS/mbed-crypto/sanity/main.cpp
Outdated
{ | ||
psa_algorithm_t alg = PSA_ALG_SHA_256; | ||
psa_hash_operation_t operation; | ||
unsigned char hash[] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
encrypted, sizeof(encrypted), &output_len)); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_finish(&operation, encrypted + output_len, | ||
sizeof(encrypted) - output_len, &output_len)); | ||
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_abort(&operation)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
TESTS/mbed-crypto/sanity/main.cpp
Outdated
psa_key_type_t key_type = PSA_KEY_TYPE_RSA_KEYPAIR; | ||
psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; | ||
psa_key_policy_t policy; | ||
unsigned char key[] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making this, and other large constant arrays, static const
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you consider a large array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you consider to be excessive stack usage for an embedded target?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed all constant arrays to static const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
New PR @ ARMmbed#8754 |
Finish memory protection and add static assert
Description
Add mbed-crypto basic functionality tests to mbed-os/TESTS directory.
Pull request type