2323
2424#include "cracen_psa_primitives.h"
2525
26- static psa_status_t cracen_cipher_crypt (cracen_cipher_operation_t * operation ,
27- const psa_key_attributes_t * attributes , psa_algorithm_t alg ,
28- const uint8_t * input , size_t input_length , uint8_t * output ,
29- size_t output_size , size_t * output_length )
26+ static bool is_alg_supported (psa_algorithm_t alg , const psa_key_attributes_t * attributes )
27+ {
28+ bool is_supported = false;
29+
30+ switch (alg ) {
31+ case PSA_ALG_STREAM_CIPHER :
32+ /* This is needed because in the PSA APIs the PSA_ALG_STREAM_CIPHER
33+ * relies on the key type to identify which algorithm to use. Here we
34+ * make sure that the key type is supported before we continue.
35+ */
36+ if (IS_ENABLED (PSA_NEED_CRACEN_STREAM_CIPHER_CHACHA20 )) {
37+ is_supported = (psa_get_key_type (attributes ) == PSA_KEY_TYPE_CHACHA20 )
38+ ? true
39+ : false;
40+ }
41+ break ;
42+ case PSA_ALG_CBC_NO_PADDING :
43+ IF_ENABLED (PSA_NEED_CRACEN_CBC_NO_PADDING_AES ,
44+ (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
45+ break ;
46+ case PSA_ALG_CBC_PKCS7 :
47+ IF_ENABLED (PSA_NEED_CRACEN_CBC_PKCS7_AES ,
48+ (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
49+ break ;
50+ case PSA_ALG_CTR :
51+ IF_ENABLED (PSA_NEED_CRACEN_CTR_AES ,
52+ (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
53+ break ;
54+ case PSA_ALG_ECB_NO_PADDING :
55+ IF_ENABLED (PSA_NEED_CRACEN_ECB_NO_PADDING_AES ,
56+ (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
57+ break ;
58+ default :
59+ is_supported = false;
60+ break ;
61+ }
62+
63+ return is_supported ;
64+ }
65+
66+ static psa_status_t setup (enum cipher_operation dir , cracen_cipher_operation_t * operation ,
67+ const psa_key_attributes_t * attributes ,
68+ const uint8_t * key_buffer , size_t key_buffer_size ,
69+ psa_algorithm_t alg )
70+ {
71+ if (!is_alg_supported (alg , attributes )) {
72+ return PSA_ERROR_NOT_SUPPORTED ;
73+ }
74+
75+ /*
76+ * Copy the key into the operation struct as it is not guaranteed
77+ * to be valid longer than the function call.
78+ */
79+
80+ if (key_buffer_size > sizeof (operation -> key_buffer )) {
81+ return PSA_ERROR_INVALID_ARGUMENT ;
82+ }
83+
84+ memcpy (operation -> key_buffer , key_buffer , key_buffer_size );
85+
86+ psa_status_t status = cracen_load_keyref (attributes , operation -> key_buffer , key_buffer_size ,
87+ & operation -> keyref );
88+ if (status != PSA_SUCCESS ) {
89+ return status ;
90+ }
91+
92+ operation -> alg = alg ;
93+ operation -> dir = dir ;
94+ operation -> blk_size =
95+ (alg == PSA_ALG_STREAM_CIPHER ) ? SX_BLKCIPHER_MAX_BLK_SZ : SX_BLKCIPHER_AES_BLK_SZ ;
96+
97+ return PSA_SUCCESS ;
98+ }
99+
100+ static psa_status_t crypt (cracen_cipher_operation_t * operation ,
101+ const psa_key_attributes_t * attributes , psa_algorithm_t alg ,
102+ const uint8_t * input , size_t input_length , uint8_t * output ,
103+ size_t output_size , size_t * output_length )
30104{
31105 size_t update_output_length = 0 ;
32106 size_t finish_output_length = 0 ;
@@ -51,9 +125,9 @@ static psa_status_t cracen_cipher_crypt(cracen_cipher_operation_t *operation,
51125 * the state between calls is not supported. This function is using the single part
52126 * APIs of Cracen to perform the AES ECB operations.
53127 */
54- psa_status_t cracen_cipher_crypt_ecb (const struct sxkeyref * key , const uint8_t * input ,
55- size_t input_length , uint8_t * output , size_t output_size ,
56- size_t * output_length , enum cipher_operation dir )
128+ static psa_status_t crypt_ecb (const struct sxkeyref * key , const uint8_t * input ,
129+ size_t input_length , uint8_t * output , size_t output_size ,
130+ size_t * output_length , enum cipher_operation dir )
57131{
58132 int sx_status ;
59133 struct sxblkcipher blkciph ;
@@ -122,13 +196,12 @@ psa_status_t cracen_cipher_encrypt(const psa_key_attributes_t *attributes,
122196 if (status != PSA_SUCCESS ) {
123197 return status ;
124198 }
125- return cracen_cipher_crypt_ecb (& key , input , input_length , output ,
126- output_size , output_length , CRACEN_ENCRYPT );
199+ return crypt_ecb (& key , input , input_length , output ,
200+ output_size , output_length , CRACEN_ENCRYPT );
127201 }
128202 }
129203
130- status = cracen_cipher_encrypt_setup (& operation , attributes , key_buffer , key_buffer_size ,
131- alg );
204+ status = setup (CRACEN_ENCRYPT , & operation , attributes , key_buffer , key_buffer_size , alg );
132205 if (status != PSA_SUCCESS ) {
133206 return status ;
134207 }
@@ -138,8 +211,8 @@ psa_status_t cracen_cipher_encrypt(const psa_key_attributes_t *attributes,
138211 return status ;
139212 }
140213
141- return cracen_cipher_crypt (& operation , attributes , alg , input , input_length , output ,
142- output_size , output_length );
214+ return crypt (& operation , attributes , alg , input , input_length , output ,
215+ output_size , output_length );
143216}
144217
145218psa_status_t cracen_cipher_decrypt (const psa_key_attributes_t * attributes ,
@@ -171,17 +244,16 @@ psa_status_t cracen_cipher_decrypt(const psa_key_attributes_t *attributes,
171244 if (status != PSA_SUCCESS ) {
172245 return status ;
173246 }
174- return cracen_cipher_crypt_ecb (& key , input , input_length , output ,
175- output_size , output_length , CRACEN_DECRYPT );
247+ return crypt_ecb (& key , input , input_length , output ,
248+ output_size , output_length , CRACEN_DECRYPT );
176249 }
177250 }
178251
179252 if (input_length < iv_size ) {
180253 return PSA_ERROR_INVALID_ARGUMENT ;
181254 }
182255
183- status = cracen_cipher_decrypt_setup (& operation , attributes , key_buffer , key_buffer_size ,
184- alg );
256+ status = setup (CRACEN_DECRYPT , & operation , attributes , key_buffer , key_buffer_size , alg );
185257 if (status != PSA_SUCCESS ) {
186258 return status ;
187259 }
@@ -191,49 +263,8 @@ psa_status_t cracen_cipher_decrypt(const psa_key_attributes_t *attributes,
191263 return status ;
192264 }
193265
194- return cracen_cipher_crypt (& operation , attributes , alg , input + iv_size ,
195- input_length - iv_size , output , output_size , output_length );
196- }
197-
198- static bool is_alg_supported (psa_algorithm_t alg , const psa_key_attributes_t * attributes )
199- {
200-
201- bool is_supported = false;
202-
203- switch (alg ) {
204- case PSA_ALG_STREAM_CIPHER :
205- /* This is needed because in the PSA APIs the PSA_ALG_STREAM_CIPHER
206- * relies on the key type to identify which algorithm to use. Here we
207- * make sure that the key type is supported before we continue.
208- */
209- if (IS_ENABLED (PSA_NEED_CRACEN_STREAM_CIPHER_CHACHA20 )) {
210- is_supported = (psa_get_key_type (attributes ) == PSA_KEY_TYPE_CHACHA20 )
211- ? true
212- : false;
213- }
214- break ;
215- case PSA_ALG_CBC_NO_PADDING :
216- IF_ENABLED (PSA_NEED_CRACEN_CBC_NO_PADDING_AES ,
217- (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
218- break ;
219- case PSA_ALG_CBC_PKCS7 :
220- IF_ENABLED (PSA_NEED_CRACEN_CBC_PKCS7_AES ,
221- (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
222- break ;
223- case PSA_ALG_CTR :
224- IF_ENABLED (PSA_NEED_CRACEN_CTR_AES ,
225- (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
226- break ;
227- case PSA_ALG_ECB_NO_PADDING :
228- IF_ENABLED (PSA_NEED_CRACEN_ECB_NO_PADDING_AES ,
229- (is_supported = psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ));
230- break ;
231- default :
232- is_supported = false;
233- break ;
234- }
235-
236- return is_supported ;
266+ return crypt (& operation , attributes , alg , input + iv_size ,
267+ input_length - iv_size , output , output_size , output_length );
237268}
238269
239270static psa_status_t initialize_cipher (cracen_cipher_operation_t * operation )
@@ -294,56 +325,40 @@ static psa_status_t initialize_cipher(cracen_cipher_operation_t *operation)
294325 return silex_statuscodes_to_psa (sx_status );
295326}
296327
297- static psa_status_t operation_setup (enum cipher_operation dir , cracen_cipher_operation_t * operation ,
298- const psa_key_attributes_t * attributes ,
299- const uint8_t * key_buffer , size_t key_buffer_size ,
300- psa_algorithm_t alg )
328+ static bool is_multi_part_supported (psa_algorithm_t alg )
301329{
302- if (!is_alg_supported (alg , attributes )) {
303- return PSA_ERROR_NOT_SUPPORTED ;
304- }
305-
306- /*
307- * Copy the key into the operation struct as it is not guaranteed
308- * to be valid longer than the function call.
309- */
310-
311- if (key_buffer_size > sizeof (operation -> key_buffer )) {
312- return PSA_ERROR_INVALID_ARGUMENT ;
313- }
314-
315- memcpy (operation -> key_buffer , key_buffer , key_buffer_size );
316-
317- psa_status_t status = cracen_load_keyref (attributes , operation -> key_buffer , key_buffer_size ,
318- & operation -> keyref );
319- if (status != PSA_SUCCESS ) {
320- return status ;
330+ if (IS_ENABLED (CONFIG_SOC_NRF54L20 )) {
331+ switch (alg ) {
332+ case PSA_ALG_ECB_NO_PADDING :
333+ return IS_ENABLED (PSA_NEED_CRACEN_ECB_NO_PADDING_AES );
334+ default :
335+ return false;
336+ }
337+ } else {
338+ return true;
321339 }
322-
323- operation -> alg = alg ;
324- operation -> dir = dir ;
325- operation -> blk_size =
326- (alg == PSA_ALG_STREAM_CIPHER ) ? SX_BLKCIPHER_MAX_BLK_SZ : SX_BLKCIPHER_AES_BLK_SZ ;
327-
328- return PSA_SUCCESS ;
329340}
330341
331342psa_status_t cracen_cipher_encrypt_setup (cracen_cipher_operation_t * operation ,
332343 const psa_key_attributes_t * attributes ,
333344 const uint8_t * key_buffer , size_t key_buffer_size ,
334345 psa_algorithm_t alg )
335346{
336- return operation_setup (CRACEN_ENCRYPT , operation , attributes , key_buffer , key_buffer_size ,
337- alg );
347+ if (!is_multi_part_supported (alg )) {
348+ return PSA_ERROR_NOT_SUPPORTED ;
349+ }
350+ return setup (CRACEN_ENCRYPT , operation , attributes , key_buffer , key_buffer_size , alg );
338351}
339352
340353psa_status_t cracen_cipher_decrypt_setup (cracen_cipher_operation_t * operation ,
341354 const psa_key_attributes_t * attributes ,
342355 const uint8_t * key_buffer , size_t key_buffer_size ,
343356 psa_algorithm_t alg )
344357{
345- return operation_setup (CRACEN_DECRYPT , operation , attributes , key_buffer , key_buffer_size ,
346- alg );
358+ if (!is_multi_part_supported (alg )) {
359+ return PSA_ERROR_NOT_SUPPORTED ;
360+ }
361+ return setup (CRACEN_DECRYPT , operation , attributes , key_buffer , key_buffer_size , alg );
347362}
348363
349364psa_status_t cracen_cipher_set_iv (cracen_cipher_operation_t * operation , const uint8_t * iv ,
@@ -444,7 +459,7 @@ psa_status_t cracen_cipher_update(cracen_cipher_operation_t *operation, const ui
444459 if (operation -> unprocessed_input_bytes ) {
445460 __ASSERT_NO_MSG (operation -> unprocessed_input_bytes ==
446461 operation -> blk_size );
447- status = cracen_cipher_crypt_ecb (
462+ status = crypt_ecb (
448463 & operation -> keyref , operation -> unprocessed_input ,
449464 operation -> unprocessed_input_bytes , output ,
450465 output_size , output_length , operation -> dir );
@@ -456,7 +471,7 @@ psa_status_t cracen_cipher_update(cracen_cipher_operation_t *operation, const ui
456471 }
457472
458473 if (block_bytes ) {
459- status = cracen_cipher_crypt_ecb (
474+ status = crypt_ecb (
460475 & operation -> keyref , input , block_bytes , output ,
461476 output_size , output_length , operation -> dir );
462477 if (status != PSA_SUCCESS ) {
@@ -553,10 +568,9 @@ psa_status_t cracen_cipher_finish(cracen_cipher_operation_t *operation, uint8_t
553568 */
554569 if (IS_ENABLED (PSA_NEED_CRACEN_ECB_NO_PADDING_AES )) {
555570 if (operation -> alg == PSA_ALG_ECB_NO_PADDING ) {
556- return cracen_cipher_crypt_ecb (& operation -> keyref ,
557- operation -> unprocessed_input ,
558- operation -> unprocessed_input_bytes , output ,
559- output_size , output_length , operation -> dir );
571+ return crypt_ecb (& operation -> keyref , operation -> unprocessed_input ,
572+ operation -> unprocessed_input_bytes , output ,
573+ output_size , output_length , operation -> dir );
560574 }
561575 }
562576
0 commit comments