28
28
#include "ssi_aes_defs.h"
29
29
#include <string.h>
30
30
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
+
31
58
int mbedtls_cipher_cmac_starts ( mbedtls_cipher_context_t * ctx ,
32
59
const unsigned char * key , size_t keybits )
33
60
{
@@ -72,34 +99,7 @@ int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
72
99
}
73
100
74
101
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 ) );
103
103
}
104
104
105
105
int mbedtls_cipher_cmac_update ( mbedtls_cipher_context_t * ctx ,
@@ -123,13 +123,6 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
123
123
124
124
cmac_ctx = ctx -> cmac_ctx ;
125
125
126
- if ( cmac_ctx -> is_cc_initiated == 0 )
127
- {
128
- ret = init_cc ( cmac_ctx );
129
- if ( ret != 0 )
130
- goto exit ;
131
- }
132
-
133
126
/* Is there data still to process from the last call?
134
127
*/
135
128
if ( cmac_ctx -> unprocessed_len > 0 )
@@ -201,13 +194,6 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
201
194
202
195
cmac_ctx = ctx -> cmac_ctx ;
203
196
204
- if ( cmac_ctx -> is_cc_initiated == 0 )
205
- {
206
- ret = init_cc ( cmac_ctx );
207
- if ( ret != 0 )
208
- goto exit ;
209
- }
210
-
211
197
if ( ( ret = SaSi_AesFinish ( & cmac_ctx -> CC_Context , cmac_ctx -> unprocessed_len ,
212
198
cmac_ctx -> unprocessed_block ,
213
199
cmac_ctx -> unprocessed_len , output , & olen ) ) != 0 )
@@ -217,7 +203,8 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
217
203
}
218
204
219
205
exit :
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 )
221
208
{
222
209
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ;
223
210
}
@@ -227,6 +214,7 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
227
214
228
215
int mbedtls_cipher_cmac_reset ( mbedtls_cipher_context_t * ctx )
229
216
{
217
+ int ret = 0 ;
230
218
mbedtls_cmac_context_t * cmac_ctx ;
231
219
232
220
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 )
239
227
mbedtls_platform_zeroize ( cmac_ctx -> unprocessed_block ,
240
228
sizeof ( cmac_ctx -> unprocessed_block ) );
241
229
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 ) );
243
235
}
244
236
245
237
int 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,
264
256
if ( ret != 0 )
265
257
goto exit ;
266
258
267
- if ( ( ret = init_cc ( ctx .cmac_ctx ) ) != 0 )
268
- {
269
- goto clear_cc ;
270
- }
271
259
272
260
if ( SaSi_AesFinish ( & ctx .cmac_ctx -> CC_Context , ilen , ( uint8_t * ) input ,
273
261
ilen , output , & olen ) != 0 )
@@ -277,7 +265,8 @@ int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
277
265
}
278
266
279
267
clear_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 )
281
270
{
282
271
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ;
283
272
}
0 commit comments