Skip to content
New issue

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

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

Already on GitHub? # to your account

Validate values of PSA Crypto macros against spec #3936

Draft
wants to merge 6 commits into
base: development
Choose a base branch
from

Conversation

bensze01
Copy link
Contributor

@bensze01 bensze01 commented Dec 3, 2020

Description

This PR adds a test case which validates the values of the PSA macros in our headers (except _IS_ and _GET_ macros already covered elsewhere) against the PSA crypto API spec.

It also warns about macros present in the spec, but not defined by us.

Closes #3922

Status

IN DEVELOPMENT

Requires Backporting

No, PSA only

Migrations

Still need to update the PSA macros in our headers to match the spec.

Additional comments

I'll push a new commit to update PSA macros after verifying the tests fail correctly.

Todos

  • Update PSA macros to their 1.0.0 values

@bensze01 bensze01 added mbed TLS team needs-ci Needs to pass CI tests needs-review Every commit must be reviewed by at least two team members, labels Dec 3, 2020
Revision: 76df6622ac6508d477733f5f45f5c0bed472e192

include/psa/crypto.h
sphinx-source/appendix/specdef_values.rst

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
@bensze01
Copy link
Contributor Author

bensze01 commented Dec 4, 2020

Force-pushed PR to fix a permissions issue. Content unchanged.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

I gave this a quick overview and I think the test achieves its intended purpose, but the code should be refactored to avoid duplication and the integration isn't quite right. So I propose to use the script manually for now to validate that we make the right changes to crypto*.h to conform to the PSA specification 1.0.1, and take our time to finish the script and include it in our tests.

Based on the MacroCollector in generate_psa_constants.py,
but *just* different enough not to warrant inheriting anything from it.

Should probably create a shared base class for the two of them at some point.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. Let's use this script to validate that we get the values right, but not merge it until we've cleaned it up and got rid of code duplication.

#endif
'''

class MacroCollector():
Copy link
Contributor

Choose a reason for hiding this comment

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

For information, I'm working on a script that generates storage test cases, and it also uses the MacroCollector class. So far I've only made a tiny patch and then I derive from it. As discussed internally, I'm going to create a directory scripts/mbedtls_dev and start putting non-executable Python modules there.

Copy link
Contributor

Choose a reason for hiding this comment

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

#3953 creates scripts/mbedtls_dev. MacroCollector could be moved to a new module there. (import generate_psa_constants.MacroCollector would work, but creating a new module is cleaner.)

component_check_verify_psa_macros () {
msg "Check: verify PSA Crypto macros against spec" # ~ 2s
make -C programs test/verify_psa_macros
if_build_succeeded programs/test/verify_psa_macros
Copy link
Contributor

Choose a reason for hiding this comment

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

This is independent of the library configuration, so I think it should be part of an existing component, like tests/scripts/test_psa_constant_names.py.

os.chdir('..')
generate_verify_psa_macros(('scripts/data_files/psa_spec/crypto.h',
'scripts/data_files/psa_spec/specdef_values.rst'),
'programs/test/verify_psa_macros.c')
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it useful to have verify_psa_macros.c as a test program checked into the repository? It seems to me like this program has no value except to compile it, run it and see its output as part of a test, so it could be generated and run on the fly like test_psa_constant_names.py does.

@@ -0,0 +1,562 @@
typedef /* implementation-defined type */ psa_aead_operation_t;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is generated from a commit which is slightly anterior to the psa-crypto-api-1.0.1 tag, and slightly different (1.0.1 has additional lifetime-related macros). Let's base our copy on the official released version.

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
Tag: psa-crypto-api-1.0.1
Revision: 9ce27ef1c6b196184cf7b1665b62c0371d1b81b6

include/psa/crypto.h
sphinx-source/appendix/specdef_values.rst

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
To PSA Crypto API version 1.0.1

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
For PSA Crypto API 1.0.1

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
@bensze01 bensze01 added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Dec 7, 2020
@bensze01 bensze01 removed the needs-ci Needs to pass CI tests label Dec 7, 2020
{header}

#define hash_alg PSA_ALG_SHA_256
#define mac_alg PSA_ALG_CMAC
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing: aead_alg (for PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG and PSA_ALG_AEAD_WITH_SHORTENED_TAG).

#define mac_alg PSA_ALG_CMAC
#define ka_alg PSA_ALG_ECDH
#define kdf_alg PSA_ALG_HKDF(PSA_ALG_SHA_256)
#define mac_length 4
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing: tag_length (for PSA_ALG_AEAD_WITH_SHORTENED_TAG).

{header}

#define hash_alg PSA_ALG_SHA_256
#define mac_alg PSA_ALG_CMAC
Copy link
Contributor

Choose a reason for hiding this comment

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

Using a single value isn't enough to test the shortened/default-length macros. There needs to be at least one shortened value as well.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm Dec 7, 2020

Choose a reason for hiding this comment

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

More generally, this program should test several parameter values for each type of parameter. Possibly have the Python code enumerate all possible arguments by reusing code from test_psa_constant_names.py.

@yanesca
Copy link
Contributor

yanesca commented Sep 7, 2021

This is a PR, moved the corresponding issue (#3922) into the epic and backlog boards instead. I have added it to the unified board as well.

@daverodgman daverodgman added the historical-reviewed Reviewed & agreed to keep legacy PR/issue label May 6, 2022
@daverodgman daverodgman added component-psa PSA keystore/dispatch layer (storage, drivers, …) priority-medium Medium priority - this can be reviewed as time permits and removed mbed TLS team labels May 13, 2022
@tom-daubney-arm
Copy link
Contributor

We are now converting older PRs to draft PRs where the following conditions are met: They have not been updated in the last 3 months, and they need more than non-trivial work to complete.

@tom-daubney-arm tom-daubney-arm marked this pull request as draft June 2, 2023 11:15
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
component-psa PSA keystore/dispatch layer (storage, drivers, …) historical-reviewed Reviewed & agreed to keep legacy PR/issue needs-work priority-medium Medium priority - this can be reviewed as time permits
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSA: review incompatibilities with PSA Crypto 1.0.0
5 participants