@@ -4301,7 +4301,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4301
4301
goto exit ;
4302
4302
}
4303
4303
4304
- if (!PSA_ALG_IS_CIPHER (alg )) {
4304
+ if (!PSA_ALG_IS_CIPHER (alg ) && ! PSA_ALG_IS_CMAC ( alg ) ) {
4305
4305
status = PSA_ERROR_INVALID_ARGUMENT ;
4306
4306
goto exit ;
4307
4307
}
@@ -4316,7 +4316,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4316
4316
* so we only set it (in the driver wrapper) after resources have been
4317
4317
* allocated/initialized. */
4318
4318
operation -> iv_set = 0 ;
4319
- if (alg == PSA_ALG_ECB_NO_PADDING ) {
4319
+ if (alg == PSA_ALG_ECB_NO_PADDING || PSA_ALG_FULL_LENGTH_MAC ( alg ) == PSA_ALG_CMAC ) {
4320
4320
operation -> iv_required = 0 ;
4321
4321
} else {
4322
4322
operation -> iv_required = 1 ;
@@ -5913,6 +5913,7 @@ static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5913
5913
psa_key_attributes_t * attributes )
5914
5914
{
5915
5915
psa_status_t status ;
5916
+ psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED ;
5916
5917
psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT ;
5917
5918
size_t mac_output_length ;
5918
5919
uint8_t U_i [PSA_MAC_MAX_SIZE ];
@@ -5924,10 +5925,26 @@ static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5924
5925
mac_operation .mac_size = prf_output_length ;
5925
5926
MBEDTLS_PUT_UINT32_BE (pbkdf2 -> block_number , block_counter , 0 );
5926
5927
5928
+ psa_key_id_t key = 0 ;
5929
+ status = psa_import_key (attributes , pbkdf2 -> password , pbkdf2 -> password_length , & key );
5930
+ if (status != PSA_SUCCESS ) {
5931
+ return status ;
5932
+ }
5933
+
5934
+ psa_key_slot_t * slot ;
5935
+ status = psa_get_and_lock_key_slot_with_policy (
5936
+ key ,
5937
+ & slot ,
5938
+ PSA_KEY_USAGE_SIGN_MESSAGE ,
5939
+ prf_alg );
5940
+ if (status != PSA_SUCCESS ) {
5941
+ goto cleanup ;
5942
+ }
5943
+
5927
5944
status = psa_driver_wrapper_mac_sign_setup (& mac_operation ,
5928
- attributes ,
5929
- pbkdf2 -> password ,
5930
- pbkdf2 -> password_length ,
5945
+ & slot -> attr ,
5946
+ slot -> key . data ,
5947
+ slot -> key . bytes ,
5931
5948
prf_alg );
5932
5949
if (status != PSA_SUCCESS ) {
5933
5950
goto cleanup ;
@@ -5957,9 +5974,9 @@ static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5957
5974
/* We are passing prf_output_length as mac_size because the driver
5958
5975
* function directly sets mac_output_length as mac_size upon success.
5959
5976
* See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
5960
- status = psa_driver_wrapper_mac_compute (attributes ,
5961
- pbkdf2 -> password ,
5962
- pbkdf2 -> password_length ,
5977
+ status = psa_driver_wrapper_mac_compute (& slot -> attr ,
5978
+ slot -> key . data ,
5979
+ slot -> key . bytes ,
5963
5980
prf_alg , U_i , prf_output_length ,
5964
5981
U_i , prf_output_length ,
5965
5982
& mac_output_length );
@@ -5971,9 +5988,11 @@ static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5971
5988
}
5972
5989
5973
5990
cleanup :
5991
+ psa_destroy_key (key );
5992
+ unlock_status = psa_unregister_read_under_mutex (slot );
5974
5993
/* Zeroise buffers to clear sensitive data from memory. */
5975
5994
mbedtls_platform_zeroize (U_i , PSA_MAC_MAX_SIZE );
5976
- return status ;
5995
+ return ( status == PSA_SUCCESS ) ? unlock_status : status ;
5977
5996
}
5978
5997
5979
5998
static psa_status_t psa_key_derivation_pbkdf2_read (
@@ -5987,19 +6006,21 @@ static psa_status_t psa_key_derivation_pbkdf2_read(
5987
6006
uint8_t prf_output_length ;
5988
6007
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT ;
5989
6008
psa_set_key_bits (& attributes , PSA_BYTES_TO_BITS (pbkdf2 -> password_length ));
5990
- psa_set_key_usage_flags (& attributes , PSA_KEY_USAGE_SIGN_MESSAGE );
5991
6009
5992
6010
if (PSA_ALG_IS_PBKDF2_HMAC (kdf_alg )) {
5993
6011
prf_alg = PSA_ALG_HMAC (PSA_ALG_PBKDF2_HMAC_GET_HASH (kdf_alg ));
5994
6012
prf_output_length = PSA_HASH_LENGTH (prf_alg );
5995
6013
psa_set_key_type (& attributes , PSA_KEY_TYPE_HMAC );
6014
+ psa_set_key_usage_flags (& attributes , PSA_KEY_USAGE_SIGN_MESSAGE );
5996
6015
} else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ) {
5997
6016
prf_alg = PSA_ALG_CMAC ;
5998
6017
prf_output_length = PSA_MAC_LENGTH (PSA_KEY_TYPE_AES , 128U , PSA_ALG_CMAC );
5999
6018
psa_set_key_type (& attributes , PSA_KEY_TYPE_AES );
6019
+ psa_set_key_usage_flags (& attributes , PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_ENCRYPT );
6000
6020
} else {
6001
6021
return PSA_ERROR_INVALID_ARGUMENT ;
6002
6022
}
6023
+ psa_set_key_algorithm (& attributes , prf_alg );
6003
6024
6004
6025
switch (pbkdf2 -> state ) {
6005
6026
case PSA_PBKDF2_STATE_PASSWORD_SET :
@@ -7198,23 +7219,47 @@ static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7198
7219
size_t * output_len )
7199
7220
{
7200
7221
psa_status_t status = PSA_SUCCESS ;
7222
+ psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED ;
7223
+
7201
7224
if (input_len != PSA_MAC_LENGTH (PSA_KEY_TYPE_AES , 128U , PSA_ALG_CMAC )) {
7202
7225
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT ;
7203
7226
uint8_t zeros [16 ] = { 0 };
7204
7227
psa_set_key_type (& attributes , PSA_KEY_TYPE_AES );
7205
7228
psa_set_key_bits (& attributes , PSA_BYTES_TO_BITS (sizeof (zeros )));
7206
- psa_set_key_usage_flags (& attributes , PSA_KEY_USAGE_SIGN_MESSAGE );
7229
+ psa_set_key_algorithm (& attributes , PSA_ALG_CMAC );
7230
+ psa_set_key_usage_flags (& attributes , PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_ENCRYPT );
7231
+
7232
+ psa_key_id_t key = 0 ;
7233
+ status = psa_import_key (& attributes , zeros , sizeof (zeros ), & key );
7234
+ if (status != PSA_SUCCESS ) {
7235
+ return status ;
7236
+ }
7237
+
7238
+ psa_key_slot_t * slot ;
7239
+ status = psa_get_and_lock_key_slot_with_policy (
7240
+ key ,
7241
+ & slot ,
7242
+ PSA_KEY_USAGE_SIGN_MESSAGE ,
7243
+ PSA_ALG_CMAC );
7244
+ if (status != PSA_SUCCESS ) {
7245
+ return status ;
7246
+ }
7247
+
7207
7248
/* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
7208
7249
* mac_size as the driver function sets mac_output_length = mac_size
7209
7250
* on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
7210
- status = psa_driver_wrapper_mac_compute (& attributes ,
7211
- zeros , sizeof ( zeros ) ,
7251
+ status = psa_driver_wrapper_mac_compute (& slot -> attr ,
7252
+ slot -> key . data , slot -> key . bytes ,
7212
7253
PSA_ALG_CMAC , input , input_len ,
7213
7254
output ,
7214
7255
PSA_MAC_LENGTH (PSA_KEY_TYPE_AES ,
7215
7256
128U ,
7216
7257
PSA_ALG_CMAC ),
7217
7258
output_len );
7259
+
7260
+ psa_destroy_key (key );
7261
+ unlock_status = psa_unregister_read_under_mutex (slot );
7262
+ return (status == PSA_SUCCESS ) ? unlock_status : status ;
7218
7263
} else {
7219
7264
memcpy (output , input , input_len );
7220
7265
* output_len = PSA_MAC_LENGTH (PSA_KEY_TYPE_AES , 128U , PSA_ALG_CMAC );
0 commit comments