Skip to content

Add mock tests for hooks for secure element drivers #174

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

Merged
merged 9 commits into from
Aug 22, 2019

Conversation

dgreen-arm
Copy link
Contributor

This PR add hooks for generating keys, signing and verification using secure element driver code.

@dgreen-arm dgreen-arm requested a review from AndrzejKurek July 15, 2019 14:21
uint8_t *p_pubkey_out,
size_t pubkey_out_size,
size_t *p_pubkey_length);
size_t bits);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you drop support for pubkey exporting during key generation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've fixed this based on the discussion we had.

status = drv->asymmetric->p_sign(
slot->data.se.slot_number, alg, hash, hash_length, signature,
signature_size, signature_length );
goto exit;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this goto?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it goes to exit at this point anyway, I'll remove it.

drv->asymmetric->p_sign == NULL )
{
status = PSA_ERROR_NOT_SUPPORTED;
goto exit;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this goto?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe we need this one, as there isn't anything more we can do at this point.

mock_import_data.usage = usage;
mock_import_data.data_length = data_length;

if( mock_import_data.return_value == PSA_SUCCESS )
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be set regardlesss of the result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we want to set all these values even on failure? The test seems to indicate that type at least should remain unset if mock_import_return_value isn't PSA_SUCCESS

Copy link
Contributor

Choose a reason for hiding this comment

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

The mock structures only indicate with what arguments was the function called, regardless of any error.

@gilles-peskine-arm gilles-peskine-arm added the feature branch This issue or PR is for a short-term feature branch which doesn't have its own label. label Jul 22, 2019
@dgreen-arm dgreen-arm force-pushed the psa-se-driver-hooks branch from bde7708 to 284eca2 Compare July 23, 2019 14:33
Copy link
Contributor

@AndrzejKurek AndrzejKurek left a comment

Choose a reason for hiding this comment

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

I would only suggest always filling the mock in the tests, since it should only indicate with what arguments was the function called. If necessary, the tests should also be adjusted.

@Patater
Copy link
Contributor

Patater commented Jul 31, 2019

@dgreen-arm I've rebased this branch on the latest API branch. Available at https://github.com/Patater/mbed-crypto/tree/psa-se-driver-hooks. Would recommend updating this PR with that branch, changing the target branch of this PR, and continuing the review from there.

@dgreen-arm dgreen-arm changed the base branch from dev/shared/secure-element to psa-api-1.0-beta August 2, 2019 08:47
@dgreen-arm dgreen-arm force-pushed the psa-se-driver-hooks branch from 284eca2 to 37a41ec Compare August 2, 2019 08:48
@dgreen-arm
Copy link
Contributor Author

There are currently two issues to note with this PR. Firstly, the tests aren't currently being run as MBEDTLS_PSA_CRYPTO_SE_C isn't enabled by default. Would adding a test case in all.sh be the best approach for this?
Second, the mock import failure test currently doesn't work with how the mocks are constructed. Either the test or the mock needs updating.

@gilles-peskine-arm gilles-peskine-arm added api-spec Issue or PR about the PSA specifications enhancement New feature or request needs: work The pull request needs rework before it can be merged. and removed feature branch This issue or PR is for a short-term feature branch which doesn't have its own label. labels Aug 2, 2019
@gilles-peskine-arm
Copy link
Collaborator

gilles-peskine-arm commented Aug 2, 2019

There's an all.sh job with SE support enabled since #157 which is merged so rebasing on top of the target branch would give you that.

@dgreen-arm
Copy link
Contributor Author

Oh, I hadn't realized that. This is based on #157, so that issue is already solved.

@gilles-peskine-arm
Copy link
Collaborator

I broke off the hooks for generate/sign/verify in #211, without the mock tests, to get a basis to make the whole stack work on real hardware. This PR should be rebased on top of a merge of #211, to add the tests.

@dgreen-arm dgreen-arm force-pushed the psa-se-driver-hooks branch from 37a41ec to 917ab8a Compare August 19, 2019 08:04
@@ -1835,7 +1835,7 @@ psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
if( driver != NULL )
{
const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
size_t bits;
size_t bits = PSA_MAX_KEY_BITS + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this change needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please leave a comment in the code explaining the reason, to improve the code's readability. Others will have the same question.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see this change was added in commit "Parametrize key bits in import mock test", which doesn't seem right if all one intends to do is updating a test as the title would imply. If the core has to be updated together with the test change, revise the commit message. If the core has a bug, explain it in the commit message and make a change to fix the bug separate from the mock test updating.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've split this out into a separate commit and added an explanation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment in the code about why you chose the value you did.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a code comment.

Copy link
Contributor

@AndrzejKurek AndrzejKurek left a comment

Choose a reason for hiding this comment

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

Just for the sake of another reviewer - I would suggest renaming this PR and describing it accordingly.

@@ -1835,7 +1835,7 @@ psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
if( driver != NULL )
{
const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
size_t bits;
size_t bits = PSA_MAX_KEY_BITS + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I see this change was added in commit "Parametrize key bits in import mock test", which doesn't seem right if all one intends to do is updating a test as the title would imply. If the core has to be updated together with the test change, revise the commit message. If the core has a bug, explain it in the commit message and make a change to fix the bug separate from the mock test updating.

@dgreen-arm dgreen-arm force-pushed the psa-se-driver-hooks branch from 917ab8a to af793a8 Compare August 20, 2019 12:11
@dgreen-arm
Copy link
Contributor Author

The ABI check is failing as the fix hasn't been merged into the psa-api-1.0-beta branch yet, however I've run it locally and there are no changes, as expected.

@dgreen-arm dgreen-arm force-pushed the psa-se-driver-hooks branch from af793a8 to dd55ea5 Compare August 20, 2019 13:57
@dgreen-arm dgreen-arm changed the base branch from psa-api-1.0-beta to development August 21, 2019 10:52
@dgreen-arm dgreen-arm changed the title Add hooks for secure element drivers Add mock tests for hooks for secure element drivers Aug 21, 2019
AndrzejKurek and others added 9 commits August 21, 2019 16:56
Mock key importing and exporting
In psa_import_key, the key bits value was uninitialized before
calling the secure element driver import function. There is a
potential issue if the driver returns PSA_SUCCESS without setting
the key bits. This shouldn't happen, but shouldn't be discounted
either, so we initialize the key bits to an invalid issue.
@dgreen-arm dgreen-arm force-pushed the psa-se-driver-hooks branch from dd55ea5 to 0892d0f Compare August 21, 2019 16:03
@dgreen-arm
Copy link
Contributor Author

I've reworked this to save and check all the attributes in the import and generate mock tests. I've also rebased it onto development to (hopefully) get the PR-head tests passing.

Previous version available here: https://github.com/dgreen-arm/mbed-crypto/tree/psa-se-driver-hooks-3
Updated attributes here, but before rebase here: https://github.com/dgreen-arm/mbed-crypto/tree/psa-se-driver-hooks-4

@dgreen-arm dgreen-arm added needs: review The pull request is ready for review. This generally means that it has no known issues. and removed needs: work The pull request needs rework before it can be merged. labels Aug 21, 2019
Copy link
Contributor

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

LGTM

@Patater Patater requested a review from AndrzejKurek August 21, 2019 19:08
@k-stachowiak k-stachowiak self-requested a review August 22, 2019 11:40
@Patater Patater dismissed AndrzejKurek’s stale review August 22, 2019 13:10

Review comments addressed

@Patater Patater removed the needs: review The pull request is ready for review. This generally means that it has no known issues. label Aug 22, 2019
@Patater Patater merged commit de4453d into ARMmbed:development Aug 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-spec Issue or PR about the PSA specifications enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants