Skip to content

Commit a1c5fcd

Browse files
Ron EldorRon Eldor
authored andcommitted
Add a deinit function
Add a deinit function that will be called and check inside whether context is initialized. This function is called for freeing the CC context, instead of every time check that it's initizliaed and free it.
1 parent 4e29c8f commit a1c5fcd

File tree

1 file changed

+12
-10
lines changed
  • features/cryptocell/FEATURE_CRYPTOCELL310

1 file changed

+12
-10
lines changed

features/cryptocell/FEATURE_CRYPTOCELL310/cmac_alt.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ static int init_cc( mbedtls_cmac_context_t *cmac_ctx )
5252

5353
exit:
5454
return( ret );
55+
}
56+
57+
static int deinit_cc( mbedtls_cmac_context_t *cmac_ctx )
58+
{
59+
if( cmac_ctx->is_cc_initiated == 1 &&
60+
SaSi_AesFree( &cmac_ctx->CC_Context ) != 0 )
61+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
5562

63+
return( 0 );
5664
}
5765

5866
int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
@@ -116,7 +124,6 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
116124
if( ctx == NULL || ctx->cipher_info == NULL )
117125
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
118126

119-
120127
block_size = ctx->cipher_info->block_size;
121128
if( block_size != SASI_AES_BLOCK_SIZE_IN_BYTES )
122129
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
@@ -174,7 +181,7 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
174181
exit:
175182
if( ret != 0 )
176183
{
177-
SaSi_AesFree( &cmac_ctx->CC_Context );
184+
deinit_cc( cmac_ctx );
178185
mbedtls_platform_zeroize( cmac_ctx, sizeof( *cmac_ctx ) );
179186
mbedtls_free( cmac_ctx );
180187
}
@@ -203,8 +210,7 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
203210
}
204211

205212
exit:
206-
if( cmac_ctx->is_cc_initiated == 1 &&
207-
SaSi_AesFree( &cmac_ctx->CC_Context ) != 0 && ret == 0 )
213+
if( deinit_cc( cmac_ctx ) && ret == 0 )
208214
{
209215
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
210216
}
@@ -214,7 +220,6 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
214220

215221
int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
216222
{
217-
int ret = 0;
218223
mbedtls_cmac_context_t *cmac_ctx;
219224

220225
if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL )
@@ -227,8 +232,7 @@ int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
227232
mbedtls_platform_zeroize( cmac_ctx->unprocessed_block,
228233
sizeof( cmac_ctx->unprocessed_block ) );
229234

230-
if( cmac_ctx->is_cc_initiated == 1 &&
231-
SaSi_AesFree( &cmac_ctx->CC_Context ) != 0 )
235+
if( deinit_cc( cmac_ctx ) != 0 )
232236
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
233237

234238
return( init_cc( cmac_ctx ) );
@@ -256,7 +260,6 @@ int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
256260
if( ret != 0 )
257261
goto exit;
258262

259-
260263
if( SaSi_AesFinish( &ctx.cmac_ctx->CC_Context, ilen, ( uint8_t * ) input,
261264
ilen, output, &olen ) != 0 )
262265
{
@@ -265,8 +268,7 @@ int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
265268
}
266269

267270
clear_cc:
268-
if( ctx.cmac_ctx->is_cc_initiated == 1 &&
269-
SaSi_AesFree( &ctx.cmac_ctx->CC_Context ) != 0 && ret == 0 )
271+
if( deinit_cc( ctx.cmac_ctx ) != 0 && ret == 0 )
270272
{
271273
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
272274
}

0 commit comments

Comments
 (0)