Skip to content

Commit a5aac1e

Browse files
Ron EldorRon Eldor
authored andcommitted
Fix functionality issues
FIx some functionality issues for better visibility: 1. Allocate the context only for 128 bit key 2. Change oreder of freeing the resources.
1 parent f5956ce commit a5aac1e

File tree

1 file changed

+13
-21
lines changed
  • features/cryptocell/FEATURE_CRYPTOCELL310

1 file changed

+13
-21
lines changed

features/cryptocell/FEATURE_CRYPTOCELL310/cmac_alt.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,28 @@ int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
5151
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
5252
}
5353

54-
/* Allocated and initialise in the cipher context memory for the CMAC
55-
* context
56-
*/
57-
cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) );
58-
if( cmac_ctx == NULL )
59-
return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
60-
6154

6255
switch( keybits )
6356
{
6457
case 128:
65-
{
58+
/* Allocated and initialise in the cipher context memory for the CMAC
59+
* context
60+
*/
61+
cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) );
62+
if( cmac_ctx == NULL )
63+
return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
6664
cmac_ctx->CC_keySizeInBytes = ( keybits / 8 );
6765
memcpy( cmac_ctx->CC_Key, key, cmac_ctx->CC_keySizeInBytes );
68-
}
6966
break;
7067
case 192:
7168
case 256:
72-
{
73-
mbedtls_free( cmac_ctx );
7469
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
75-
}
7670
default:
77-
{
78-
mbedtls_free( cmac_ctx );
7971
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
80-
}
8172
}
8273

8374
ctx->cmac_ctx = cmac_ctx;
84-
return 0;
75+
return( 0 );
8576
}
8677

8778
static int init_cc( mbedtls_cmac_context_t *cmac_ctx )
@@ -190,9 +181,9 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
190181
exit:
191182
if( ret != 0 )
192183
{
184+
SaSi_AesFree( &cmac_ctx->CC_Context );
193185
mbedtls_platform_zeroize( cmac_ctx, sizeof( *cmac_ctx ) );
194186
mbedtls_free( cmac_ctx );
195-
SaSi_AesFree( &cmac_ctx->CC_Context );
196187
}
197188
return( ret );
198189
}
@@ -275,22 +266,23 @@ int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
275266

276267
if( ( ret = init_cc( ctx.cmac_ctx ) ) != 0 )
277268
{
278-
goto exit;
269+
goto clear_cc;
279270
}
280271

281272
if( SaSi_AesFinish( &ctx.cmac_ctx->CC_Context, ilen, ( uint8_t * ) input,
282273
ilen, output, &olen ) != 0 )
283274
{
284275
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
285-
goto exit;
276+
goto clear_cc;
286277
}
287278

288-
289-
exit:
279+
clear_cc:
290280
if( SaSi_AesFree( &ctx.cmac_ctx->CC_Context ) != 0 && ret == 0 )
291281
{
292282
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
293283
}
284+
285+
exit:
294286
mbedtls_cipher_free( &ctx );
295287

296288
return( ret );

0 commit comments

Comments
 (0)