Skip to content

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

Closed
wants to merge 12 commits into from
Closed

Mbed Crypto Tests #1

wants to merge 12 commits into from

Conversation

itayzafrir
Copy link
Owner

Description

Add mbed-crypto basic functionality tests to mbed-os/TESTS directory.

Pull request type

[ ] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[x] Test update
[ ] Breaking change

@@ -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)
Copy link

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).

Copy link
Owner Author

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.

Copy link
Owner Author

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.

{
psa_algorithm_t alg = PSA_ALG_SHA_256;
psa_hash_operation_t operation;
unsigned char hash[] = {
Copy link

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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK will do.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link

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));
Copy link

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.

Copy link
Owner Author

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.

Copy link

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.

Copy link
Owner Author

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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to deterministic.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_CRYPT;
size_t key_bits = 512;
psa_key_policy_t policy;
unsigned char input[100];
Copy link

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.

Copy link
Owner Author

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.

Copy link

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.

Copy link
Owner Author

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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to deterministic.

Copy link
Owner Author

@itayzafrir itayzafrir left a 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.

@@ -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)
Copy link
Owner Author

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.

{
psa_algorithm_t alg = PSA_ALG_SHA_256;
psa_hash_operation_t operation;
unsigned char hash[] = {
Copy link
Owner Author

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));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to deterministic.

psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_CRYPT;
size_t key_bits = 512;
psa_key_policy_t policy;
unsigned char input[100];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to deterministic.

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)];

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?

Copy link
Owner Author

@itayzafrir itayzafrir Nov 13, 2018

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.

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)];

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.

Copy link
Owner Author

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,
Copy link

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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

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.

{
psa_algorithm_t alg = PSA_ALG_SHA_256;
psa_hash_operation_t operation;
unsigned char hash[] = {
Copy link

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));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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[] = {
Copy link

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.

Copy link
Owner Author

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?

Copy link

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?

Copy link
Owner Author

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link

@Patater Patater left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@itayzafrir itayzafrir closed this Nov 15, 2018
@itayzafrir
Copy link
Owner Author

New PR @ ARMmbed#8754

NirSonnenschein pushed a commit that referenced this pull request Nov 26, 2018
itayzafrir pushed a commit that referenced this pull request Dec 4, 2018
itayzafrir pushed a commit that referenced this pull request Mar 18, 2019
Finish memory protection and add static assert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants