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

Driver-only hashes: PKCS5 #6244

Merged
merged 15 commits into from
Sep 5, 2022
Merged

Conversation

AndrzejKurek
Copy link
Contributor

@AndrzejKurek AndrzejKurek commented Aug 30, 2022

This PR adds all of the required changes to have the PKCS5 module working without MD.
Fixes #6145.

Test coverage comparison:

*** Comparing before-default -> after-default ***
          pk: total 168; skipped  92 ->  92
     pkparse: total 276; skipped  20 ->  20
     pkwrite: total  12; skipped   0 ->   0
       pkcs5: total  54; skipped   0 ->   0

*** Comparing before-full -> after-full ***
          pk: total 168; skipped   8 ->   8
     pkparse: total 276; skipped  20 ->  20
     pkwrite: total  12; skipped   0 ->   0
       pkcs5: total  54; skipped   0 ->   0

*** Comparing reference -> drivers ***
          pk: total 168; skipped  30 ->   8
     pkparse: total 276; skipped  20 ->  20
     pkwrite: total  12; skipped   0 ->   0
       pkcs5: total  54; skipped   0 ->   0

@AndrzejKurek AndrzejKurek added needs-work needs-ci Needs to pass CI tests component-psa PSA keystore/dispatch layer (storage, drivers, …) component-test Test framework and CI scripts labels Aug 30, 2022
@mpg mpg self-requested a review August 30, 2022 11:01
library/pkcs5.c Outdated Show resolved Hide resolved
library/pkcs5.c Outdated Show resolved Hide resolved
library/pkcs5.c Outdated Show resolved Hide resolved
@AndrzejKurek AndrzejKurek force-pushed the pkcs5-no-md branch 4 times, most recently from e682975 to c235ad3 Compare August 31, 2022 23:12
@AndrzejKurek AndrzejKurek added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review priority-high High priority - will be reviewed soon and removed needs-work labels Aug 31, 2022
@AndrzejKurek AndrzejKurek marked this pull request as ready for review August 31, 2022 23:14
@AndrzejKurek AndrzejKurek changed the title [DRAFT] PKCS5 without MD PKCS5 without MD Aug 31, 2022
@AndrzejKurek AndrzejKurek changed the title PKCS5 without MD Driver-only hashes: PKCS5 Aug 31, 2022
@mprse mprse self-requested a review September 1, 2022 09:21
@mprse mprse removed the needs-reviewer This PR needs someone to pick it up for review label Sep 1, 2022
mpg
mpg previously approved these changes Sep 1, 2022
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

Looks good to me! Just left one suggestion for improvement. Let's hope the CI agrees :)

@@ -14,12 +14,14 @@ void pbkdf2_hmac( int hash, data_t * pw_str, data_t * salt_str,
{
unsigned char key[100];

PSA_INIT();
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 OK but I think we could be even more precise: define a new macro PSA_INIT_IF_NO_MD() that only calls psa_crypto_init() if MD_C is not defined. That way, we can check that things still work without psa_crypto_init() when MD_C is defined, as the documentation say. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that's a good thing to test. Do you think that it will be useful in other .function files, or just for pkcs5?

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems likely to be used in other files. Actually, even in this PR, it's already used in pkparse too IIRC.

Copy link
Contributor

Choose a reason for hiding this comment

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

Code that only uses hashes should not require psa_crypto_init, which among other things requires entropy.

Copy link
Contributor

Choose a reason for hiding this comment

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

Code that only uses hashes should not require psa_crypto_init, which among other things requires entropy.

PBKDF2 uses HMAC, which in PSA requires the key store to be initialized unless I missed something.

Even it was just hashing, I don't think there's any point in the documentation right now that says you can use psa_hash_xxx() functions without calling psa_crypto_init() first, and I'd rather not rely on undocumented properties. (Also, isn't there some kind of driver initialization step that needs to happen when hashes are provided by drivers?)

So, if you're talking about the future, with split init and the parts that can be done transparently happening automatically under the hood, then yes, sure, but as far as this PR is concerned, I think right now we do need to call psa_crypto_init().

Copy link
Contributor

@mprse mprse left a comment

Choose a reason for hiding this comment

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

I did the first round of review and found few places for minor improvements.

#define MBEDTLS_PSA_BUILTIN_ALG_HMAC 1
#define PSA_WANT_ALG_HMAC 1
#define PSA_WANT_KEY_TYPE_HMAC

#if defined(MBEDTLS_MD_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 this ok that MBEDTLS_PSA_BUILTIN_ALG_HMAC, PSA_WANT_ALG_HMAC, PSA_WANT_KEY_TYPE_HMAC are always defined?

library/pkcs5.c Outdated
{
#if defined(MBEDTLS_MD_C)
mbedtls_md_context_t md_ctx;
const mbedtls_md_info_t *md_info;
Copy link
Contributor

Choose a reason for hiding this comment

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

For consistency I suggest to init md_info to NULL and ret to MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED.


md_info = mbedtls_md_info_from_type( md_alg );
if( md_info == NULL )
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Copy link
Contributor

Choose a reason for hiding this comment

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

md_ctx is already initalized, so we need to set ret to MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE and go to exit in order to free md_ctx or move this block before mbedtls_md_init().

Copy link
Contributor

Choose a reason for hiding this comment

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

Technically, we only need to call md_free() after md_setup() has been called, but we might still want to apply your suggestion for the sake of uniformity.

library/pkcs5.c Outdated
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;


memset( counter, 0, 4 );
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to use sizeof( counter ) instead 4.

library/pkcs5.c Outdated
size_t use_len, out_len, out_size;
unsigned char *out_p = output;
unsigned char counter[4];
mbedtls_svc_key_id_t psa_hmac_key;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to init key to MBEDTLS_SVC_KEY_ID_INIT.

library/pkcs5.c Outdated
if( ( status = psa_mac_update( &operation, salt, slen ) ) != PSA_SUCCESS )
goto cleanup;

if( ( status = psa_mac_update( &operation, counter, 4 ) ) != 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.

Suggest to use sizeof( counter ) instead 4.

library/pkcs5.c Outdated
memset( counter, 0, 4 );
counter[3] = 1;
psa_algorithm_t alg = PSA_ALG_HMAC( mbedtls_hash_info_psa_from_md( md_alg ) );
out_size = PSA_MAC_LENGTH( PSA_KEY_TYPE_HMAC, 0, alg );
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to declare out_size as const size_t.

library/pkcs5.c Outdated
unsigned int i;
unsigned char md1[PSA_HASH_MAX_SIZE];
unsigned char work[PSA_HASH_MAX_SIZE];
unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to declare md_size as const unsigned char.

library/pkcs5.c Outdated
!= PSA_SUCCESS )
goto cleanup;

memcpy( md1, work, md_size );
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that md_size should be equal to out_len, but shouldn't we use out_len as this is the real size of work (returned by psa_mac_sign_finish)?

library/pkcs5.c Outdated
/* Zeroise buffers to clear sensitive data from memory. */
mbedtls_platform_zeroize( work, PSA_HASH_MAX_SIZE );
mbedtls_platform_zeroize( md1, PSA_HASH_MAX_SIZE );
psa_destroy_key( psa_hmac_key );
Copy link
Contributor

Choose a reason for hiding this comment

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

Status of key destruction is skipped.

@mpg
Copy link
Contributor

mpg commented Sep 2, 2022

(Also, this needs rebasing to resolve trivial conflicts now that the PKCS12 work has been merged.)

Andrzej Kurek added 13 commits September 2, 2022 04:03
Use the new implementation locally
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Hashing via PSA is now supported 
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
It's possible to build PKCS5 with PSA instead of MD
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
If PSA is defined and there is no MD - an initialization
is required.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
The shared part has now been extracted and will
be used regardless of the deprecation define.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Add consts, more elegant size calculation and 
variable initialization.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
@AndrzejKurek
Copy link
Contributor Author

AndrzejKurek commented Sep 2, 2022

(Also, this needs rebasing to resolve trivial conflicts now that the PKCS12 work has been merged.)

Conflicts resolved (commits 37a17e8 and 11265d7, removing PKCS12 and PKCS5 was next to each other) and error handling improved in 216baca.

@AndrzejKurek
Copy link
Contributor Author

There's also one regression in test coverage - I'll investigate it and update the PR.

Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
@AndrzejKurek
Copy link
Contributor Author

Right, I missed cherry-picking one commit when I was restructuring the PR - added.
Current results:

*** Comparing before-default -> after-default ***
          pk: total 168; skipped  92 ->  92
     pkparse: total 276; skipped  20 ->  20
     pkwrite: total  12; skipped   0 ->   0
       pkcs5: total  54; skipped   0 ->   0

*** Comparing before-full -> after-full ***
          pk: total 168; skipped   8 ->   8
     pkparse: total 276; skipped  20 ->  20
     pkwrite: total  12; skipped   0 ->   0
       pkcs5: total  54; skipped   0 ->   0

*** Comparing reference -> drivers ***
          pk: total 168; skipped  30 ->   8
     pkparse: total 276; skipped  20 ->  20
     pkwrite: total  12; skipped   0 ->   0
       pkcs5: total  54; skipped   0 ->   0

@AndrzejKurek AndrzejKurek removed the needs-ci Needs to pass CI tests label Sep 3, 2022
Copy link
Contributor

@mprse mprse left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my comments.
LGTM

Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

LGTM. Rebase is good, and thanks for improving error handling.

@mpg mpg added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Sep 5, 2022
@mpg mpg merged commit 52f83dc into Mbed-TLS:development Sep 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports component-psa PSA keystore/dispatch layer (storage, drivers, …) component-test Test framework and CI scripts priority-high High priority - will be reviewed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Driver-only hashes: PKCS5
4 participants