Skip to content

Commit 4885bf7

Browse files
degjorvanordicjm
authored andcommitted
nrf_security: drivers: cracen: Add support for HKDF extract and expand
Adding support for PSA_ALG_HKDF_EXTRACT and PSA_ALG_HKDF_EXPAND for use with key derivation Signed-off-by: Dag Erik Gjørvad <[email protected]>
1 parent 65ca3c0 commit 4885bf7

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/key_derivation.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,22 @@ psa_status_t cracen_key_derivation_setup(cracen_key_derivation_operation_t *oper
193193
{
194194
operation->alg = alg;
195195

196-
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && PSA_ALG_IS_HKDF(operation->alg)) {
196+
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && (PSA_ALG_IS_HKDF(operation->alg) ||
197+
PSA_ALG_IS_HKDF_EXPAND(operation->alg))) {
198+
size_t hash_size = PSA_HASH_LENGTH(PSA_ALG_HKDF_GET_HASH(alg));
199+
200+
if (hash_size == 0) {
201+
return PSA_ERROR_NOT_SUPPORTED;
202+
}
203+
204+
operation->capacity =
205+
UINT8_MAX * hash_size; /* Max value of counter (1 byte) size of hash. */
206+
operation->state = CRACEN_KD_STATE_HKDF_INIT;
207+
208+
return PSA_SUCCESS;
209+
}
210+
211+
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && PSA_ALG_IS_HKDF_EXTRACT(operation->alg)) {
197212
size_t hash_size = PSA_HASH_LENGTH(PSA_ALG_HKDF_GET_HASH(alg));
198213

199214
if (hash_size == 0) {
@@ -625,7 +640,20 @@ psa_status_t cracen_key_derivation_input_bytes(cracen_key_derivation_operation_t
625640
psa_key_derivation_step_t step, const uint8_t *data,
626641
size_t data_length)
627642
{
628-
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && PSA_ALG_IS_HKDF(operation->alg)) {
643+
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && (PSA_ALG_IS_HKDF(operation->alg) ||
644+
PSA_ALG_IS_HKDF_EXTRACT(operation->alg))) {
645+
return cracen_key_derivation_input_bytes_hkdf(operation, step, data, data_length);
646+
}
647+
648+
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && PSA_ALG_IS_HKDF_EXPAND(operation->alg)) {
649+
if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
650+
if (data_length > sizeof(operation->hkdf.prk)) {
651+
return PSA_ERROR_INSUFFICIENT_MEMORY;
652+
}
653+
memcpy(operation->hkdf.prk, data, data_length);
654+
operation->state = CRACEN_KD_STATE_HKDF_KEYED;
655+
return PSA_SUCCESS;
656+
}
629657
return cracen_key_derivation_input_bytes_hkdf(operation, step, data, data_length);
630658
}
631659

@@ -1098,7 +1126,8 @@ psa_status_t cracen_key_derivation_output_bytes(cracen_key_derivation_operation_
10981126
{
10991127
psa_status_t (*generator)(cracen_key_derivation_operation_t *) = NULL;
11001128

1101-
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && PSA_ALG_IS_HKDF(operation->alg)) {
1129+
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && (PSA_ALG_IS_HKDF(operation->alg) ||
1130+
PSA_ALG_IS_HKDF_EXPAND(operation->alg))) {
11021131
if (operation->state < CRACEN_KD_STATE_HKDF_KEYED || !operation->hkdf.info_set) {
11031132
return PSA_ERROR_BAD_STATE;
11041133
}
@@ -1107,6 +1136,16 @@ psa_status_t cracen_key_derivation_output_bytes(cracen_key_derivation_operation_
11071136
generator = cracen_key_derivation_hkdf_generate_block;
11081137
}
11091138

1139+
if (IS_ENABLED(PSA_NEED_CRACEN_HKDF) && PSA_ALG_IS_HKDF_EXTRACT(operation->alg)) {
1140+
if (operation->state < CRACEN_KD_STATE_HKDF_KEYED) {
1141+
return PSA_ERROR_BAD_STATE;
1142+
}
1143+
1144+
operation->state = CRACEN_KD_STATE_HKDF_OUTPUT;
1145+
memcpy(output, operation->hkdf.prk, 32);
1146+
return PSA_SUCCESS;
1147+
}
1148+
11101149
if (IS_ENABLED(PSA_NEED_CRACEN_PBKDF2_HMAC) && PSA_ALG_IS_PBKDF2(operation->alg)) {
11111150
/* Salt, password and input cost must have been provided. */
11121151
if (!operation->pbkdf2.input_cost) {

0 commit comments

Comments
 (0)