Skip to content

Commit cf3d6e0

Browse files
committed
[NUC472] Fix SHA accelerator errors
1. Fix clone SHA context error. Convert SHA H/W context to SHA S/W context due to just one instance of SHA H/W. 2. Fix partial update error.
1 parent f332ef7 commit cf3d6e0

File tree

10 files changed

+318
-242
lines changed

10 files changed

+318
-242
lines changed

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto/sha/sha1_alt.c

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,19 @@
2525

2626
#include "sha1_alt.h"
2727
#include "crypto-misc.h"
28+
#include "nu_bitutil.h"
29+
#include "string.h"
2830

2931
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
3032
{
3133
if (crypto_sha_acquire()) {
32-
ctx->mbedtls_sha1_init = mbedtls_sha1_hw_init;
33-
ctx->mbedtls_sha1_free = mbedtls_sha1_hw_free;
34-
ctx->mbedtls_sha1_clone = mbedtls_sha1_hw_clone;
35-
ctx->mbedtls_sha1_starts = mbedtls_sha1_hw_starts;
36-
ctx->mbedtls_sha1_update = mbedtls_sha1_hw_update;
37-
ctx->mbedtls_sha1_finish = mbedtls_sha1_hw_finish;
38-
ctx->mbedtls_sha1_process = mbedtls_sha1_hw_process;
34+
ctx->ishw = 1;
35+
mbedtls_sha1_hw_init(&ctx->hw_ctx);
3936
}
4037
else {
41-
ctx->mbedtls_sha1_init = mbedtls_sha1_sw_init;
42-
ctx->mbedtls_sha1_free = mbedtls_sha1_sw_free;
43-
ctx->mbedtls_sha1_clone = mbedtls_sha1_sw_clone;
44-
ctx->mbedtls_sha1_starts = mbedtls_sha1_sw_starts;
45-
ctx->mbedtls_sha1_update = mbedtls_sha1_sw_update;
46-
ctx->mbedtls_sha1_finish = mbedtls_sha1_sw_finish;
47-
ctx->mbedtls_sha1_process = mbedtls_sha1_sw_process;
38+
ctx->ishw = 0;
39+
mbedtls_sha1_sw_init(&ctx->sw_ctx);
4840
}
49-
50-
ctx->mbedtls_sha1_init(ctx);
5141
}
5242

5343
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
@@ -56,48 +46,90 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
5646
return;
5747
}
5848

59-
ctx->mbedtls_sha1_free(ctx);
60-
61-
if (ctx->mbedtls_sha1_init == mbedtls_sha1_hw_init) {
49+
if (ctx->ishw) {
50+
mbedtls_sha1_hw_free(&ctx->hw_ctx);
6251
crypto_sha_release();
6352
}
53+
else {
54+
mbedtls_sha1_sw_free(&ctx->sw_ctx);
55+
}
6456
}
6557

6658
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
6759
const mbedtls_sha1_context *src)
6860
{
69-
*dst = *src;
61+
if (src->ishw) {
62+
// Clone S/W ctx from H/W ctx
63+
dst->ishw = 0;
64+
dst->sw_ctx.total[0] = src->hw_ctx.total;
65+
dst->sw_ctx.total[1] = 0;
66+
{
67+
unsigned char output[20];
68+
crypto_sha_getinternstate(output, sizeof (output));
69+
dst->sw_ctx.state[0] = nu_get32_be(output);
70+
dst->sw_ctx.state[1] = nu_get32_be(output + 4);
71+
dst->sw_ctx.state[2] = nu_get32_be(output + 8);
72+
dst->sw_ctx.state[3] = nu_get32_be(output + 12);
73+
dst->sw_ctx.state[4] = nu_get32_be(output + 16);
74+
}
75+
memcpy(dst->sw_ctx.buffer, src->hw_ctx.buffer, src->hw_ctx.buffer_left);
76+
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
77+
mbedtls_sha1_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
78+
}
79+
}
80+
else {
81+
// Clone S/W ctx from S/W ctx
82+
dst->sw_ctx = src->sw_ctx;
83+
}
7084
}
7185

7286
/*
7387
* SHA-1 context setup
7488
*/
7589
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
7690
{
77-
ctx->mbedtls_sha1_starts(ctx);
78-
79-
return;
91+
if (ctx->ishw) {
92+
mbedtls_sha1_hw_starts(&ctx->hw_ctx);
93+
}
94+
else {
95+
mbedtls_sha1_sw_starts(&ctx->sw_ctx);
96+
}
8097
}
8198

8299
/*
83100
* SHA-1 process buffer
84101
*/
85102
void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen)
86103
{
87-
ctx->mbedtls_sha1_update(ctx, input, ilen);
104+
if (ctx->ishw) {
105+
mbedtls_sha1_hw_update(&ctx->hw_ctx, input, ilen);
106+
}
107+
else {
108+
mbedtls_sha1_sw_update(&ctx->sw_ctx, input, ilen);
109+
}
88110
}
89111

90112
/*
91113
* SHA-1 final digest
92114
*/
93115
void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20])
94116
{
95-
ctx->mbedtls_sha1_finish(ctx, output);
117+
if (ctx->ishw) {
118+
mbedtls_sha1_hw_finish(&ctx->hw_ctx, output);
119+
}
120+
else {
121+
mbedtls_sha1_sw_finish(&ctx->sw_ctx, output);
122+
}
96123
}
97124

98125
void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
99126
{
100-
ctx->mbedtls_sha1_process(ctx, data);
127+
if (ctx->ishw) {
128+
mbedtls_sha1_hw_process(&ctx->hw_ctx, data);
129+
}
130+
else {
131+
mbedtls_sha1_sw_process(&ctx->sw_ctx, data);
132+
}
101133
}
102134

103135
#endif /* MBEDTLS_SHA1_ALT */

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto/sha/sha1_alt.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,9 @@ struct mbedtls_sha1_context_s;
4040
*/
4141
typedef struct mbedtls_sha1_context_s
4242
{
43-
union {
44-
crypto_sha_context hw_ctx;
45-
mbedtls_sha1_sw_context sw_ctx;
46-
};
47-
48-
void (*mbedtls_sha1_init)(struct mbedtls_sha1_context_s *ctx);
49-
void (*mbedtls_sha1_free)(struct mbedtls_sha1_context_s *ctx);
50-
void (*mbedtls_sha1_clone)(struct mbedtls_sha1_context_s *dst, const struct mbedtls_sha1_context_s *src);
51-
void (*mbedtls_sha1_starts)(struct mbedtls_sha1_context_s *ctx);
52-
void (*mbedtls_sha1_update)(struct mbedtls_sha1_context_s *ctx, const unsigned char *input, size_t ilen);
53-
void (*mbedtls_sha1_finish)(struct mbedtls_sha1_context_s *ctx, unsigned char output[20]);
54-
void (*mbedtls_sha1_process)(struct mbedtls_sha1_context_s *ctx, const unsigned char data[64]);
43+
int ishw;
44+
crypto_sha_context hw_ctx;
45+
mbedtls_sha1_sw_context sw_ctx;
5546
}
5647
mbedtls_sha1_context;
5748

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto/sha/sha1_alt_sw.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,30 @@ static void mbedtls_zeroize( void *v, size_t n ) {
6565
}
6666
#endif
6767

68-
void mbedtls_sha1_sw_init( mbedtls_sha1_context *ctx )
68+
void mbedtls_sha1_sw_init( mbedtls_sha1_sw_context *ctx )
6969
{
70-
memset( &ctx->sw_ctx, 0, sizeof( ctx->sw_ctx ) );
70+
memset( ctx, 0, sizeof( mbedtls_sha1_sw_context ) );
7171
}
7272

73-
void mbedtls_sha1_sw_free( mbedtls_sha1_context *ctx )
73+
void mbedtls_sha1_sw_free( mbedtls_sha1_sw_context *ctx )
7474
{
7575
if( ctx == NULL )
7676
return;
7777

78-
mbedtls_zeroize( &ctx->sw_ctx, sizeof( ctx->sw_ctx ) );
78+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_sw_context ) );
7979
}
8080

81-
void mbedtls_sha1_sw_clone( mbedtls_sha1_context *dst,
82-
const mbedtls_sha1_context *src )
81+
void mbedtls_sha1_sw_clone( mbedtls_sha1_sw_context *dst,
82+
const mbedtls_sha1_sw_context *src )
8383
{
84-
dst->sw_ctx = src->sw_ctx;
84+
*dst = *src;
8585
}
8686

8787
/*
8888
* SHA-1 context setup
8989
*/
90-
void mbedtls_sha1_sw_starts( mbedtls_sha1_context *ctx_ )
90+
void mbedtls_sha1_sw_starts( mbedtls_sha1_sw_context *ctx )
9191
{
92-
mbedtls_sha1_sw_context *ctx = &ctx_->sw_ctx;
93-
9492
ctx->total[0] = 0;
9593
ctx->total[1] = 0;
9694

@@ -101,10 +99,8 @@ void mbedtls_sha1_sw_starts( mbedtls_sha1_context *ctx_ )
10199
ctx->state[4] = 0xC3D2E1F0;
102100
}
103101

104-
void mbedtls_sha1_sw_process( mbedtls_sha1_context *ctx_, const unsigned char data[64] )
102+
void mbedtls_sha1_sw_process( mbedtls_sha1_sw_context *ctx, const unsigned char data[64] )
105103
{
106-
mbedtls_sha1_sw_context *ctx = &ctx_->sw_ctx;
107-
108104
uint32_t temp, W[16], A, B, C, D, E;
109105

110106
GET_UINT32_BE( W[ 0], data, 0 );
@@ -262,10 +258,8 @@ void mbedtls_sha1_sw_process( mbedtls_sha1_context *ctx_, const unsigned char da
262258
/*
263259
* SHA-1 process buffer
264260
*/
265-
void mbedtls_sha1_sw_update( mbedtls_sha1_context *ctx_, const unsigned char *input, size_t ilen )
261+
void mbedtls_sha1_sw_update( mbedtls_sha1_sw_context *ctx, const unsigned char *input, size_t ilen )
266262
{
267-
mbedtls_sha1_sw_context *ctx = &ctx_->sw_ctx;
268-
269263
size_t fill;
270264
uint32_t left;
271265

@@ -284,15 +278,15 @@ void mbedtls_sha1_sw_update( mbedtls_sha1_context *ctx_, const unsigned char *in
284278
if( left && ilen >= fill )
285279
{
286280
memcpy( (void *) (ctx->buffer + left), input, fill );
287-
mbedtls_sha1_sw_process( ctx_, ctx->buffer );
281+
mbedtls_sha1_sw_process( ctx, ctx->buffer );
288282
input += fill;
289283
ilen -= fill;
290284
left = 0;
291285
}
292286

293287
while( ilen >= 64 )
294288
{
295-
mbedtls_sha1_sw_process( ctx_, input );
289+
mbedtls_sha1_sw_process( ctx, input );
296290
input += 64;
297291
ilen -= 64;
298292
}
@@ -312,10 +306,8 @@ static const unsigned char sha1_padding[64] =
312306
/*
313307
* SHA-1 final digest
314308
*/
315-
void mbedtls_sha1_sw_finish( mbedtls_sha1_context *ctx_, unsigned char output[20] )
309+
void mbedtls_sha1_sw_finish( mbedtls_sha1_sw_context *ctx, unsigned char output[20] )
316310
{
317-
mbedtls_sha1_sw_context *ctx = &ctx_->sw_ctx;
318-
319311
uint32_t last, padn;
320312
uint32_t high, low;
321313
unsigned char msglen[8];
@@ -330,8 +322,8 @@ void mbedtls_sha1_sw_finish( mbedtls_sha1_context *ctx_, unsigned char output[20
330322
last = ctx->total[0] & 0x3F;
331323
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
332324

333-
mbedtls_sha1_sw_update( ctx_, sha1_padding, padn );
334-
mbedtls_sha1_sw_update( ctx_, msglen, 8 );
325+
mbedtls_sha1_sw_update( ctx, sha1_padding, padn );
326+
mbedtls_sha1_sw_update( ctx, msglen, 8 );
335327

336328
PUT_UINT32_BE( ctx->state[0], output, 0 );
337329
PUT_UINT32_BE( ctx->state[1], output, 4 );

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto/sha/sha1_alt_sw.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
extern "C" {
4040
#endif
4141

42-
struct mbedtls_sha1_context_s;
43-
4442
/**
4543
* \brief SHA-1 context structure
4644
*/
@@ -57,30 +55,30 @@ mbedtls_sha1_sw_context;
5755
*
5856
* \param ctx SHA-1 context to be initialized
5957
*/
60-
void mbedtls_sha1_sw_init( struct mbedtls_sha1_context_s *ctx );
58+
void mbedtls_sha1_sw_init( mbedtls_sha1_sw_context *ctx );
6159

6260
/**
6361
* \brief Clear SHA-1 context
6462
*
6563
* \param ctx SHA-1 context to be cleared
6664
*/
67-
void mbedtls_sha1_sw_free( struct mbedtls_sha1_context_s *ctx );
65+
void mbedtls_sha1_sw_free( mbedtls_sha1_sw_context *ctx );
6866

6967
/**
7068
* \brief Clone (the state of) a SHA-1 context
7169
*
7270
* \param dst The destination context
7371
* \param src The context to be cloned
7472
*/
75-
void mbedtls_sha1_sw_clone( struct mbedtls_sha1_context_s *dst,
76-
const struct mbedtls_sha1_context_s *src );
73+
void mbedtls_sha1_sw_clone( mbedtls_sha1_sw_context *dst,
74+
const mbedtls_sha1_sw_context *src );
7775

7876
/**
7977
* \brief SHA-1 context setup
8078
*
8179
* \param ctx context to be initialized
8280
*/
83-
void mbedtls_sha1_sw_starts( struct mbedtls_sha1_context_s *ctx );
81+
void mbedtls_sha1_sw_starts( mbedtls_sha1_sw_context *ctx );
8482

8583
/**
8684
* \brief SHA-1 process buffer
@@ -89,18 +87,18 @@ void mbedtls_sha1_sw_starts( struct mbedtls_sha1_context_s *ctx );
8987
* \param input buffer holding the data
9088
* \param ilen length of the input data
9189
*/
92-
void mbedtls_sha1_sw_update( struct mbedtls_sha1_context_s *ctx, const unsigned char *input, size_t ilen );
90+
void mbedtls_sha1_sw_update( mbedtls_sha1_sw_context *ctx, const unsigned char *input, size_t ilen );
9391

9492
/**
9593
* \brief SHA-1 final digest
9694
*
9795
* \param ctx SHA-1 context
9896
* \param output SHA-1 checksum result
9997
*/
100-
void mbedtls_sha1_sw_finish( struct mbedtls_sha1_context_s *ctx, unsigned char output[20] );
98+
void mbedtls_sha1_sw_finish( mbedtls_sha1_sw_context *ctx, unsigned char output[20] );
10199

102100
/* Internal use */
103-
void mbedtls_sha1_sw_process( struct mbedtls_sha1_context_s *ctx, const unsigned char data[64] );
101+
void mbedtls_sha1_sw_process( mbedtls_sha1_sw_context *ctx, const unsigned char data[64] );
104102

105103
#ifdef __cplusplus
106104
}

0 commit comments

Comments
 (0)