Skip to content

Commit 479cf68

Browse files
committed
[NUC472/M487] Fix multiple calls to SHA free in SHA alter.
1 parent 7d92550 commit 479cf68

File tree

10 files changed

+85
-85
lines changed

10 files changed

+85
-85
lines changed

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
static void mbedtls_sha1_init_internal(mbedtls_sha1_context *ctx, int try_hw)
3333
{
3434
if (try_hw && crypto_sha_acquire()) {
35-
ctx->ishw = 1;
35+
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha1_hw_init(&ctx->hw_ctx);
3737
} else {
38-
ctx->ishw = 0;
38+
ctx->active_ctx = &ctx->sw_ctx;
3939
mbedtls_sha1_sw_init(&ctx->sw_ctx);
4040
}
4141
}
@@ -51,27 +51,27 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
5151
return;
5252
}
5353

54-
if (ctx->ishw) {
54+
if (ctx->active_ctx == &ctx->hw_ctx) {
5555
mbedtls_sha1_hw_free(&ctx->hw_ctx);
5656
crypto_sha_release();
57-
} else {
57+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
5858
mbedtls_sha1_sw_free(&ctx->sw_ctx);
5959
}
60+
ctx->active_ctx = NULL;
6061
}
6162

6263
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
6364
const mbedtls_sha1_context *src)
6465
{
6566
// If dst is H/W context, we need to change it to S/W context first before cloning to.
66-
if (dst->ishw) {
67+
if (dst->active_ctx == &dst->hw_ctx) {
6768
mbedtls_sha1_free(dst);
6869
// Force S/W context
6970
mbedtls_sha1_init_internal(dst, 0);
7071
}
7172

72-
if (src->ishw) {
73+
if (src->active_ctx == &src->hw_ctx) {
7374
// Clone S/W ctx from H/W ctx
74-
dst->ishw = 0;
7575
dst->sw_ctx.total[0] = src->hw_ctx.total;
7676
dst->sw_ctx.total[1] = 0;
7777
{
@@ -89,7 +89,7 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
8989
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
9090
mbedtls_sha1_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
9191
}
92-
} else {
92+
} else if (src->active_ctx == &src->sw_ctx) {
9393
// Clone S/W ctx from S/W ctx
9494
dst->sw_ctx = src->sw_ctx;
9595
}
@@ -100,9 +100,9 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
100100
*/
101101
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
102102
{
103-
if (ctx->ishw) {
103+
if (ctx->active_ctx == &ctx->hw_ctx) {
104104
mbedtls_sha1_hw_starts(&ctx->hw_ctx);
105-
} else {
105+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
106106
mbedtls_sha1_sw_starts(&ctx->sw_ctx);
107107
}
108108
}
@@ -112,9 +112,9 @@ void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
112112
*/
113113
void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen)
114114
{
115-
if (ctx->ishw) {
115+
if (ctx->active_ctx == &ctx->hw_ctx) {
116116
mbedtls_sha1_hw_update(&ctx->hw_ctx, input, ilen);
117-
} else {
117+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
118118
mbedtls_sha1_sw_update(&ctx->sw_ctx, input, ilen);
119119
}
120120
}
@@ -124,18 +124,18 @@ void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input,
124124
*/
125125
void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20])
126126
{
127-
if (ctx->ishw) {
127+
if (ctx->active_ctx == &ctx->hw_ctx) {
128128
mbedtls_sha1_hw_finish(&ctx->hw_ctx, output);
129-
} else {
129+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
130130
mbedtls_sha1_sw_finish(&ctx->sw_ctx, output);
131131
}
132132
}
133133

134134
void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
135135
{
136-
if (ctx->ishw) {
136+
if (ctx->active_ctx == &ctx->hw_ctx) {
137137
mbedtls_sha1_hw_process(&ctx->hw_ctx, data);
138-
} else {
138+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
139139
mbedtls_sha1_sw_process(&ctx->sw_ctx, data);
140140
}
141141
}

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct mbedtls_sha1_context_s;
3333
* \brief SHA-1 context structure
3434
*/
3535
typedef struct mbedtls_sha1_context_s {
36-
int ishw;
36+
void *active_ctx;
3737
crypto_sha_context hw_ctx;
3838
mbedtls_sha1_sw_context sw_ctx;
3939
}

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
static void mbedtls_sha256_init_internal(mbedtls_sha256_context *ctx, int try_hw)
3333
{
3434
if (try_hw && crypto_sha_acquire()) {
35-
ctx->ishw = 1;
35+
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha256_hw_init(&ctx->hw_ctx);
3737
} else {
38-
ctx->ishw = 0;
38+
ctx->active_ctx = &ctx->sw_ctx;
3939
mbedtls_sha256_sw_init(&ctx->sw_ctx);
4040
}
4141
}
@@ -51,27 +51,27 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
5151
return;
5252
}
5353

54-
if (ctx->ishw) {
54+
if (ctx->active_ctx == &ctx->hw_ctx) {
5555
mbedtls_sha256_hw_free(&ctx->hw_ctx);
5656
crypto_sha_release();
57-
} else {
57+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
5858
mbedtls_sha256_sw_free(&ctx->sw_ctx);
5959
}
60+
ctx->active_ctx = NULL;
6061
}
6162

6263
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
6364
const mbedtls_sha256_context *src)
6465
{
6566
// If dst is H/W context, we need to change it to S/W context first before cloning to.
66-
if (dst->ishw) {
67+
if (dst->active_ctx == &dst->hw_ctx) {
6768
mbedtls_sha256_free(dst);
6869
// Force S/W context
6970
mbedtls_sha256_init_internal(dst, 0);
7071
}
7172

72-
if (src->ishw) {
73+
if (src->active_ctx == &src->hw_ctx) {
7374
// Clone S/W ctx from H/W ctx
74-
dst->ishw = 0;
7575
dst->sw_ctx.total[0] = src->hw_ctx.total;
7676
dst->sw_ctx.total[1] = 0;
7777
{
@@ -90,7 +90,7 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
9090
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
9191
mbedtls_sha256_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
9292
}
93-
} else {
93+
} else if (src->active_ctx == &src->sw_ctx) {
9494
// Clone S/W ctx from S/W ctx
9595
dst->sw_ctx = src->sw_ctx;
9696
}
@@ -101,9 +101,9 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
101101
*/
102102
void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
103103
{
104-
if (ctx->ishw) {
104+
if (ctx->active_ctx == &ctx->hw_ctx) {
105105
mbedtls_sha256_hw_starts(&ctx->hw_ctx, is224);
106-
} else {
106+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
107107
mbedtls_sha256_sw_starts(&ctx->sw_ctx, is224);
108108
}
109109
}
@@ -113,9 +113,9 @@ void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
113113
*/
114114
void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen)
115115
{
116-
if (ctx->ishw) {
116+
if (ctx->active_ctx == &ctx->hw_ctx) {
117117
mbedtls_sha256_hw_update(&ctx->hw_ctx, input, ilen);
118-
} else {
118+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
119119
mbedtls_sha256_sw_update(&ctx->sw_ctx, input, ilen);
120120
}
121121
}
@@ -125,18 +125,18 @@ void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *inp
125125
*/
126126
void mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char output[32])
127127
{
128-
if (ctx->ishw) {
128+
if (ctx->active_ctx == &ctx->hw_ctx) {
129129
mbedtls_sha256_hw_finish(&ctx->hw_ctx, output);
130-
} else {
130+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
131131
mbedtls_sha256_sw_finish(&ctx->sw_ctx, output);
132132
}
133133
}
134134

135135
void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64])
136136
{
137-
if (ctx->ishw) {
137+
if (ctx->active_ctx == &ctx->hw_ctx) {
138138
mbedtls_sha256_hw_process(&ctx->hw_ctx, data);
139-
} else {
139+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
140140
mbedtls_sha256_sw_process(&ctx->sw_ctx, data);
141141
}
142142
}

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct mbedtls_sha256_context_s;
3333
* \brief SHA-256 context structure
3434
*/
3535
typedef struct mbedtls_sha256_context_s {
36-
int ishw;
36+
void *active_ctx;
3737
crypto_sha_context hw_ctx;
3838
mbedtls_sha256_sw_context sw_ctx;
3939
}

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
static void mbedtls_sha512_init_internal(mbedtls_sha512_context *ctx, int try_hw)
3333
{
3434
if (try_hw && crypto_sha_acquire()) {
35-
ctx->ishw = 1;
35+
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha512_hw_init(&ctx->hw_ctx);
3737
} else {
38-
ctx->ishw = 0;
38+
ctx->active_ctx = &ctx->sw_ctx;
3939
mbedtls_sha512_sw_init(&ctx->sw_ctx);
4040
}
4141
}
@@ -51,27 +51,27 @@ void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
5151
return;
5252
}
5353

54-
if (ctx->ishw) {
54+
if (ctx->active_ctx == &ctx->hw_ctx) {
5555
mbedtls_sha512_hw_free(&ctx->hw_ctx);
5656
crypto_sha_release();
57-
} else {
57+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
5858
mbedtls_sha512_sw_free(&ctx->sw_ctx);
5959
}
60+
ctx->active_ctx = NULL;
6061
}
6162

6263
void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
6364
const mbedtls_sha512_context *src)
6465
{
6566
// If dst is H/W context, we need to change it to S/W context first before cloning to.
66-
if (dst->ishw) {
67+
if (dst->active_ctx == &dst->hw_ctx) {
6768
mbedtls_sha512_free(dst);
6869
// Force S/W context
6970
mbedtls_sha512_init_internal(dst, 0);
7071
}
7172

72-
if (src->ishw) {
73+
if (src->active_ctx == &src->hw_ctx) {
7374
// Clone S/W ctx from H/W ctx
74-
dst->ishw = 0;
7575
dst->sw_ctx.total[0] = src->hw_ctx.total;
7676
dst->sw_ctx.total[1] = 0;
7777
{
@@ -91,7 +91,7 @@ void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
9191
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {
9292
mbedtls_sha512_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer);
9393
}
94-
} else {
94+
} else if (src->active_ctx == &src->sw_ctx) {
9595
// Clone S/W ctx from S/W ctx
9696
dst->sw_ctx = src->sw_ctx;
9797
}
@@ -102,9 +102,9 @@ void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
102102
*/
103103
void mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384)
104104
{
105-
if (ctx->ishw) {
105+
if (ctx->active_ctx == &ctx->hw_ctx) {
106106
mbedtls_sha512_hw_starts(&ctx->hw_ctx, is384);
107-
} else {
107+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
108108
mbedtls_sha512_sw_starts(&ctx->sw_ctx, is384);
109109
}
110110
}
@@ -114,9 +114,9 @@ void mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384)
114114
*/
115115
void mbedtls_sha512_update(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
116116
{
117-
if (ctx->ishw) {
117+
if (ctx->active_ctx == &ctx->hw_ctx) {
118118
mbedtls_sha512_hw_update(&ctx->hw_ctx, input, ilen);
119-
} else {
119+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
120120
mbedtls_sha512_sw_update(&ctx->sw_ctx, input, ilen);
121121
}
122122
}
@@ -126,18 +126,18 @@ void mbedtls_sha512_update(mbedtls_sha512_context *ctx, const unsigned char *inp
126126
*/
127127
void mbedtls_sha512_finish(mbedtls_sha512_context *ctx, unsigned char output[64])
128128
{
129-
if (ctx->ishw) {
129+
if (ctx->active_ctx == &ctx->hw_ctx) {
130130
mbedtls_sha512_hw_finish(&ctx->hw_ctx, output);
131-
} else {
131+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
132132
mbedtls_sha512_sw_finish(&ctx->sw_ctx, output);
133133
}
134134
}
135135

136136
void mbedtls_sha512_process(mbedtls_sha512_context *ctx, const unsigned char data[128])
137137
{
138-
if (ctx->ishw) {
138+
if (ctx->active_ctx == &ctx->hw_ctx) {
139139
mbedtls_sha512_hw_process(&ctx->hw_ctx, data);
140-
} else {
140+
} else if (ctx->active_ctx == &ctx->sw_ctx) {
141141
mbedtls_sha512_sw_process(&ctx->sw_ctx, data);
142142
}
143143
}

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct mbedtls_sha512_context_s;
3333
* \brief SHA-512 context structure
3434
*/
3535
typedef struct mbedtls_sha512_context_s {
36-
int ishw;
36+
void *active_ctx;
3737
crypto_sha_context hw_ctx;
3838
mbedtls_sha512_sw_context sw_ctx;
3939
}

0 commit comments

Comments
 (0)