Skip to content

Commit 4ebd812

Browse files
Ron EldorRon Eldor
authored andcommitted
Fix issues in CC310 shax_alt discovered by OTT
Initialize the Cryptocell context in the `mbedtls_shax_process()` function, in case it wasn't initialized. The Process function can be called without calling to the starts function. Discovered by On Target Testing on the NRF52840_DK platform.
1 parent 0404701 commit 4ebd812

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

features/cryptocell/FEATURE_CRYPTOCELL310/sha1_alt.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,23 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
4444
memcpy( dst, src, sizeof( mbedtls_sha1_context ) );
4545
}
4646

47-
int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
47+
static int init_cc( mbedtls_sha1_context *ctx )
4848
{
49+
int ret = 0;
4950
if( CRYS_HASH_Init( &ctx->crys_hash_ctx, CRYS_HASH_SHA1_mode ) != CRYS_OK )
50-
return ( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
51-
return ( 0 );
51+
{
52+
ret = MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED ;
53+
goto exit;
54+
}
55+
56+
ctx->is_cc_initiated = 1;
57+
exit:
58+
return ( ret );
59+
}
60+
61+
int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
62+
{
63+
return ( init_cc( ctx ) );
5264
}
5365

5466

@@ -79,8 +91,21 @@ int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
7991
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
8092
const unsigned char data[64] )
8193
{
94+
int ret = 0;
95+
if( ctx->is_cc_initiated == 0 )
96+
{
97+
ret = init_cc( ctx );
98+
if( ret != 0 )
99+
goto exit;
100+
}
101+
82102
if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)data, 64 ) != CRYS_OK )
83-
return ( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
84-
return ( 0 );
103+
{
104+
ret = MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED;
105+
goto exit;
106+
}
107+
108+
exit:
109+
return ( ret );
85110
}
86111
#endif //MBEDTLS_SHA1_ALT

features/cryptocell/FEATURE_CRYPTOCELL310/sha1_alt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern "C" {
3333
typedef struct
3434
{
3535
CRYS_HASHUserContext_t crys_hash_ctx;
36+
int is_cc_initiated;
3637
} mbedtls_sha1_context;
3738

3839
/**

features/cryptocell/FEATURE_CRYPTOCELL310/sha256_alt.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,46 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
4242
memcpy( dst, src, sizeof( mbedtls_sha256_context ) );
4343
}
4444

45+
static int init_cc( mbedtls_sha256_context *ctx, int is224 )
46+
{
47+
int ret = 0;
48+
if( CRYS_HASH_Init( &ctx->crys_hash_ctx, is224 ?
49+
CRYS_HASH_SHA224_mode : CRYS_HASH_SHA256_mode ) != CRYS_OK )
50+
{
51+
ret = MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED ;
52+
goto exit;
53+
}
54+
55+
ctx->is_cc_initiated = 1;
56+
exit:
57+
return ( ret );
58+
59+
}
4560

4661
int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
4762
{
48-
if(CRYS_HASH_Init( &ctx->crys_hash_ctx, is224 ?
49-
CRYS_HASH_SHA224_mode : CRYS_HASH_SHA256_mode ) != CRYS_OK )
50-
return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
51-
return ( 0 );
63+
return ( init_cc( ctx, is224 ) );
5264
}
5365

5466
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
5567
const unsigned char data[64] )
5668
{
69+
int ret = 0;
70+
if( ctx->is_cc_initiated == 0 )
71+
{
72+
ret = init_cc( ctx, 0 );
73+
if( ret != 0 )
74+
goto exit;
75+
}
76+
5777
if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)data, 64 ) != CRYS_OK )
58-
return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
59-
return ( 0 );
78+
{
79+
ret = MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED;
80+
goto exit;
81+
}
82+
83+
exit:
84+
return ( ret );
6085
}
6186

6287
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,

features/cryptocell/FEATURE_CRYPTOCELL310/sha256_alt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern "C" {
3535
typedef struct
3636
{
3737
CRYS_HASHUserContext_t crys_hash_ctx;
38+
int is_cc_initiated;
3839
} mbedtls_sha256_context;
3940

4041

0 commit comments

Comments
 (0)