2828#include "ssi_aes_defs.h"
2929#include <string.h>
3030
31+ static int init_cc ( mbedtls_cmac_context_t * cmac_ctx )
32+ {
33+ int ret = 0 ;
34+ SaSiAesUserKeyData_t CC_KeyData ;
35+ if ( SaSi_AesInit ( & cmac_ctx -> CC_Context , SASI_AES_ENCRYPT ,
36+ SASI_AES_MODE_CMAC , SASI_AES_PADDING_NONE ) != 0 )
37+ {
38+ return ( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
39+ }
40+
41+ CC_KeyData .pKey = cmac_ctx -> CC_Key ;
42+ CC_KeyData .keySize = cmac_ctx -> CC_keySizeInBytes ;
43+
44+ if ( SaSi_AesSetKey ( & cmac_ctx -> CC_Context , SASI_AES_USER_KEY ,
45+ & CC_KeyData , sizeof ( CC_KeyData ) ) != 0 )
46+ {
47+ ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ;
48+ goto exit ;
49+ }
50+
51+ cmac_ctx -> is_cc_initiated = 1 ;
52+
53+ exit :
54+ return ( ret );
55+
56+ }
57+
3158int mbedtls_cipher_cmac_starts ( mbedtls_cipher_context_t * ctx ,
3259 const unsigned char * key , size_t keybits )
3360{
@@ -72,34 +99,7 @@ int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
7299 }
73100
74101 ctx -> cmac_ctx = cmac_ctx ;
75- return ( 0 );
76- }
77-
78- static int init_cc ( mbedtls_cmac_context_t * cmac_ctx )
79- {
80- int ret = 0 ;
81- SaSiAesUserKeyData_t CC_KeyData ;
82- if ( SaSi_AesInit ( & cmac_ctx -> CC_Context , SASI_AES_ENCRYPT ,
83- SASI_AES_MODE_CMAC , SASI_AES_PADDING_NONE ) != 0 )
84- {
85- return ( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
86- }
87-
88- CC_KeyData .pKey = cmac_ctx -> CC_Key ;
89- CC_KeyData .keySize = cmac_ctx -> CC_keySizeInBytes ;
90-
91- if ( SaSi_AesSetKey ( & cmac_ctx -> CC_Context , SASI_AES_USER_KEY ,
92- & CC_KeyData , sizeof ( CC_KeyData ) ) != 0 )
93- {
94- ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ;
95- goto exit ;
96- }
97-
98- cmac_ctx -> is_cc_initiated = 1 ;
99-
100- exit :
101- return ( ret );
102-
102+ return ( init_cc ( cmac_ctx ) );
103103}
104104
105105int mbedtls_cipher_cmac_update ( mbedtls_cipher_context_t * ctx ,
@@ -123,13 +123,6 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
123123
124124 cmac_ctx = ctx -> cmac_ctx ;
125125
126- if ( cmac_ctx -> is_cc_initiated == 0 )
127- {
128- ret = init_cc ( cmac_ctx );
129- if ( ret != 0 )
130- goto exit ;
131- }
132-
133126 /* Is there data still to process from the last call?
134127 */
135128 if ( cmac_ctx -> unprocessed_len > 0 )
@@ -201,13 +194,6 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
201194
202195 cmac_ctx = ctx -> cmac_ctx ;
203196
204- if ( cmac_ctx -> is_cc_initiated == 0 )
205- {
206- ret = init_cc ( cmac_ctx );
207- if ( ret != 0 )
208- goto exit ;
209- }
210-
211197 if ( ( ret = SaSi_AesFinish ( & cmac_ctx -> CC_Context , cmac_ctx -> unprocessed_len ,
212198 cmac_ctx -> unprocessed_block ,
213199 cmac_ctx -> unprocessed_len , output , & olen ) ) != 0 )
@@ -217,7 +203,8 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
217203 }
218204
219205exit :
220- if ( SaSi_AesFree ( & cmac_ctx -> CC_Context ) != 0 && ret == 0 )
206+ if ( cmac_ctx -> is_cc_initiated == 1 &&
207+ SaSi_AesFree ( & cmac_ctx -> CC_Context ) != 0 && ret == 0 )
221208 {
222209 ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ;
223210 }
@@ -227,6 +214,7 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
227214
228215int mbedtls_cipher_cmac_reset ( mbedtls_cipher_context_t * ctx )
229216{
217+ int ret = 0 ;
230218 mbedtls_cmac_context_t * cmac_ctx ;
231219
232220 if ( ctx == NULL || ctx -> cipher_info == NULL || ctx -> cmac_ctx == NULL )
@@ -239,7 +227,11 @@ int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
239227 mbedtls_platform_zeroize ( cmac_ctx -> unprocessed_block ,
240228 sizeof ( cmac_ctx -> unprocessed_block ) );
241229
242- return ( 0 );
230+ if ( cmac_ctx -> is_cc_initiated == 1 &&
231+ SaSi_AesFree ( & cmac_ctx -> CC_Context ) != 0 )
232+ return ( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
233+
234+ return ( init_cc ( cmac_ctx ) );
243235}
244236
245237int mbedtls_cipher_cmac ( const mbedtls_cipher_info_t * cipher_info ,
@@ -264,10 +256,6 @@ int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
264256 if ( ret != 0 )
265257 goto exit ;
266258
267- if ( ( ret = init_cc ( ctx .cmac_ctx ) ) != 0 )
268- {
269- goto clear_cc ;
270- }
271259
272260 if ( SaSi_AesFinish ( & ctx .cmac_ctx -> CC_Context , ilen , ( uint8_t * ) input ,
273261 ilen , output , & olen ) != 0 )
@@ -277,7 +265,8 @@ int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
277265 }
278266
279267clear_cc :
280- if ( SaSi_AesFree ( & ctx .cmac_ctx -> CC_Context ) != 0 && ret == 0 )
268+ if ( ctx .cmac_ctx -> is_cc_initiated == 1 &&
269+ SaSi_AesFree ( & ctx .cmac_ctx -> CC_Context ) != 0 && ret == 0 )
281270 {
282271 ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ;
283272 }
0 commit comments