Skip to content

Commit 196d0eb

Browse files
Vge0rgerlubos
authored andcommitted
nrf_security: Refactor blkcipher in Cracen
In the Cracen driver the PSA context cracen_cipher_operation_t already contains a struct sxkeyref and also a struct sxblkcipher which also contains a sxkeyref struct. Instead of keeping two instanses of the sxkeyref keep the one in the PSA operation and convert the other one to pointer and refactor the code of blkcipher accordingly. Signed-off-by: Georgios Vasilakis <[email protected]>
1 parent 77da492 commit 196d0eb

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -292,24 +292,24 @@ psa_status_t cracen_cipher_encrypt(const psa_key_attributes_t *attributes,
292292
* error and thus we don't need to write an else here.
293293
*/
294294
if (IS_ENABLED(PSA_NEED_CRACEN_ECB_NO_PADDING_AES) && alg == PSA_ALG_ECB_NO_PADDING) {
295-
struct sxkeyref key;
296295

297-
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size, &key);
296+
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size,
297+
&operation.keyref);
298298
if (status != PSA_SUCCESS) {
299299
return status;
300300
}
301-
return crypt_ecb(&operation.cipher, &key, input, input_length, output, output_size,
302-
output_length, CRACEN_ENCRYPT);
301+
302+
return crypt_ecb(&operation.cipher, &operation.keyref, input, input_length, output,
303+
output_size, output_length, CRACEN_ENCRYPT);
303304
}
304305
if (IS_ENABLED(PSA_NEED_CRACEN_CBC_PKCS7_AES) && alg == PSA_ALG_CBC_PKCS7) {
305-
struct sxkeyref key;
306-
307-
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size, &key);
306+
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size,
307+
&operation.keyref);
308308
if (status != PSA_SUCCESS) {
309309
return status;
310310
}
311-
return encrypt_cbc(&key, input, input_length, output, output_size, output_length,
312-
iv);
311+
return encrypt_cbc(&operation.keyref, input, input_length, output, output_size,
312+
output_length, iv);
313313
}
314314

315315
status = setup(CRACEN_ENCRYPT, &operation, attributes, key_buffer, key_buffer_size, alg);
@@ -339,7 +339,6 @@ psa_status_t cracen_cipher_decrypt(const psa_key_attributes_t *attributes,
339339
psa_status_t status;
340340
/* ChaCha20 only supports 12 bytes IV in the single part decryption function */
341341
const size_t iv_size = (alg == PSA_ALG_STREAM_CIPHER) ? 12 : SX_BLKCIPHER_IV_SZ;
342-
struct sxkeyref key;
343342
*output_length = 0;
344343

345344
if (input_length == 0) {
@@ -350,20 +349,22 @@ psa_status_t cracen_cipher_decrypt(const psa_key_attributes_t *attributes,
350349
* error and thus we don't need to write an else here.
351350
*/
352351
if (IS_ENABLED(PSA_NEED_CRACEN_ECB_NO_PADDING_AES) && alg == PSA_ALG_ECB_NO_PADDING) {
353-
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size, &key);
352+
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size,
353+
&operation.keyref);
354354
if (status != PSA_SUCCESS) {
355355
return status;
356356
}
357-
return crypt_ecb(&operation.cipher, &key, input, input_length, output, output_size,
358-
output_length, CRACEN_DECRYPT);
357+
return crypt_ecb(&operation.cipher, &operation.keyref, input, input_length, output,
358+
output_size, output_length, CRACEN_DECRYPT);
359359
}
360360
if (IS_ENABLED(PSA_NEED_CRACEN_CBC_PKCS7_AES) && alg == PSA_ALG_CBC_PKCS7) {
361-
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size, &key);
361+
status = cracen_load_keyref(attributes, key_buffer, key_buffer_size,
362+
&operation.keyref);
362363
if (status != PSA_SUCCESS) {
363364
return status;
364365
}
365-
return decrypt_cbc(&key, input + iv_size, input_length - iv_size, output,
366-
output_size, output_length, input);
366+
return decrypt_cbc(&operation.keyref, input + iv_size, input_length - iv_size,
367+
output, output_size, output_length, input);
367368
}
368369

369370
if (input_length < iv_size) {

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/include/sxsymcrypt/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct sxaead {
111111
*/
112112
struct sxblkcipher {
113113
const struct sx_blkcipher_cmdma_cfg *cfg;
114-
struct sxkeyref key;
114+
const struct sxkeyref *key;
115115
uint32_t textsz;
116116
struct sx_dmactl dma;
117117
struct sxdesc descs[5];

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/blkcipher.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ static const struct sx_blkcipher_cmdma_cfg ba411xtscfg = {
8181
int sx_blkcipher_free(struct sxblkcipher *cipher_ctx)
8282
{
8383
int sx_err = SX_OK;
84-
if (cipher_ctx->key.clean_key) {
85-
sx_err = cipher_ctx->key.clean_key(cipher_ctx->key.user_data);
84+
if (cipher_ctx->key && cipher_ctx->key->clean_key) {
85+
sx_err = cipher_ctx->key->clean_key(cipher_ctx->key->user_data);
8686
}
8787
sx_cmdma_release_hw(&cipher_ctx->dma);
8888
return sx_err;
@@ -106,8 +106,8 @@ static int sx_blkcipher_hw_reserve(struct sxblkcipher *cipher_ctx)
106106
goto exit;
107107
}
108108

109-
if (cipher_ctx->key.prepare_key) {
110-
err = cipher_ctx->key.prepare_key(cipher_ctx->key.user_data);
109+
if (cipher_ctx->key && cipher_ctx->key->prepare_key) {
110+
err = cipher_ctx->key->prepare_key(cipher_ctx->key->user_data);
111111
}
112112

113113
exit:
@@ -140,7 +140,7 @@ static int sx_blkcipher_create_aesxts(struct sxblkcipher *cipher_ctx, const stru
140140
}
141141
}
142142

143-
memcpy(&cipher_ctx->key, key1, sizeof(cipher_ctx->key));
143+
cipher_ctx->key = key1;
144144
err = sx_blkcipher_hw_reserve(cipher_ctx);
145145
if (err != SX_OK) {
146146
return err;
@@ -207,7 +207,7 @@ static int sx_blkcipher_create_aes_ba411(struct sxblkcipher *cipher_ctx, const s
207207
}
208208
}
209209

210-
memcpy(&cipher_ctx->key, key, sizeof(cipher_ctx->key));
210+
cipher_ctx->key = key;
211211
cipher_ctx->cfg = cfg;
212212
cipher_ctx->textsz = 0;
213213

@@ -324,8 +324,9 @@ int sx_blkcipher_resume_state(struct sxblkcipher *cipher_ctx)
324324
cipher_ctx->dma.dmamem.cfg &= ~(cipher_ctx->cfg->ctxsave);
325325
sx_cmdma_newcmd(&cipher_ctx->dma, cipher_ctx->descs, cipher_ctx->dma.dmamem.cfg,
326326
cipher_ctx->cfg->dmatags->cfg);
327-
if (KEYREF_IS_USR(&cipher_ctx->key)) {
328-
ADD_CFGDESC(cipher_ctx->dma, cipher_ctx->key.key, cipher_ctx->key.sz,
327+
328+
if (cipher_ctx->key && KEYREF_IS_USR(cipher_ctx->key)) {
329+
ADD_CFGDESC(cipher_ctx->dma, cipher_ctx->key->key, cipher_ctx->key->sz,
329330
cipher_ctx->cfg->dmatags->key);
330331
}
331332
/* Context will be transferred in the same place as the IV. However,

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/chachapoly.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ static int sx_blkcipher_create_chacha20(struct sxblkcipher *cipher_ctx, struct s
151151
return SX_ERR_INVALID_KEY_SZ;
152152
}
153153

154-
memcpy(&cipher_ctx->key, key, sizeof(cipher_ctx->key));
155154
sx_hw_reserve(&cipher_ctx->dma);
156155
cipher_ctx->cfg = &ba417chacha20cfg;
156+
cipher_ctx->key = key;
157157

158158
sx_cmdma_newcmd(&cipher_ctx->dma, cipher_ctx->descs, BA417_MODE_CHACHA20 | dir,
159159
cipher_ctx->cfg->dmatags->cfg);

0 commit comments

Comments
 (0)