-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Conversation
e682975
to
c235ad3
Compare
82efdcd
to
ca95a74
Compare
ca95a74
to
dceb12b
Compare
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.
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(); |
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.
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?
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.
Right, that's a good thing to test. Do you think that it will be useful in other .function files, or just for pkcs5?
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.
It seems likely to be used in other files. Actually, even in this PR, it's already used in pkparse too IIRC.
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.
Code that only uses hashes should not require psa_crypto_init
, which among other things requires entropy.
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.
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()
.
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 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) |
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.
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; |
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.
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 ); |
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.
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()
.
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.
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 ); |
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.
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; |
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.
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 ) |
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.
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 ); |
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.
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 ); |
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.
Suggest to declare md_size
as const unsigned char
.
library/pkcs5.c
Outdated
!= PSA_SUCCESS ) | ||
goto cleanup; | ||
|
||
memcpy( md1, work, md_size ); |
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 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 ); |
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.
Status of key destruction is skipped.
(Also, this needs rebasing to resolve trivial conflicts now that the PKCS12 work has been merged.) |
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>
e7a622e
to
6c2dd56
Compare
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
6c2dd56
to
216baca
Compare
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>
Right, I missed cherry-picking one commit when I was restructuring the PR - added.
|
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.
Thanks for addressing my comments.
LGTM
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.
LGTM. Rebase is good, and thanks for improving error handling.
This PR adds all of the required changes to have the PKCS5 module working without MD.
Fixes #6145.
Test coverage comparison: