Skip to content

Commit 5117f08

Browse files
[nrf fromtree] net: openthread: Add PSA implementation for PBDKF2 genkey
Add implementation of openthread pbkdf2 generate key using PSA functions. Co-authored-by: Joakim Andersson <[email protected]> Signed-off-by: Maciej Baczmanski <[email protected]> (cherry picked from commit 85bc24e)
1 parent 962eb73 commit 5117f08

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

modules/openthread/platform/crypto_psa.c

+62
Original file line numberDiff line numberDiff line change
@@ -603,4 +603,66 @@ otError otPlatCryptoEcdsaGenerateAndImportKey(otCryptoKeyRef aKeyRef)
603603
return psaToOtError(status);
604604
}
605605

606+
otError otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword,
607+
uint16_t aPasswordLen,
608+
const uint8_t *aSalt,
609+
uint16_t aSaltLen,
610+
uint32_t aIterationCounter,
611+
uint16_t aKeyLen,
612+
uint8_t *aKey)
613+
{
614+
psa_status_t status = PSA_SUCCESS;
615+
psa_key_id_t key_id = PSA_KEY_ID_NULL;
616+
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
617+
psa_algorithm_t algorithm = PSA_ALG_PBKDF2_AES_CMAC_PRF_128;
618+
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
619+
620+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
621+
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_VOLATILE);
622+
psa_set_key_algorithm(&attributes, algorithm);
623+
psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
624+
psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(aPasswordLen));
625+
626+
status = psa_import_key(&attributes, aPassword, aPasswordLen, &key_id);
627+
if (status != PSA_SUCCESS) {
628+
goto out;
629+
}
630+
631+
status = psa_key_derivation_setup(&operation, algorithm);
632+
if (status != PSA_SUCCESS) {
633+
goto out;
634+
}
635+
636+
status = psa_key_derivation_input_integer(&operation, PSA_KEY_DERIVATION_INPUT_COST,
637+
aIterationCounter);
638+
if (status != PSA_SUCCESS) {
639+
goto out;
640+
}
641+
642+
status = psa_key_derivation_input_bytes(&operation, PSA_KEY_DERIVATION_INPUT_SALT,
643+
aSalt, aSaltLen);
644+
if (status != PSA_SUCCESS) {
645+
goto out;
646+
}
647+
648+
status = psa_key_derivation_input_key(&operation, PSA_KEY_DERIVATION_INPUT_PASSWORD,
649+
key_id);
650+
if (status != PSA_SUCCESS) {
651+
goto out;
652+
}
653+
654+
status = psa_key_derivation_output_bytes(&operation, aKey, aKeyLen);
655+
if (status != PSA_SUCCESS) {
656+
goto out;
657+
}
658+
659+
out:
660+
psa_reset_key_attributes(&attributes);
661+
psa_key_derivation_abort(&operation);
662+
psa_destroy_key(key_id);
663+
664+
__ASSERT_NO_MSG(status == PSA_SUCCESS);
665+
return psaToOtError(status);
666+
}
667+
606668
#endif /* #if CONFIG_OPENTHREAD_ECDSA */

0 commit comments

Comments
 (0)