Skip to content

Commit 8ba0781

Browse files
committed
[NUC472/M487] Fix SHA H/W resource leakage in context cloning
1 parent 83fb50c commit 8ba0781

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
5151
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
5252
const mbedtls_sha1_context *src)
5353
{
54+
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55+
while (dst->ishw) {
56+
mbedtls_sha1_free(dst);
57+
// We just want S/W context, so we lock SHA H/W resource if it is available.
58+
if (crypto_sha_acquire()) {
59+
mbedtls_sha1_init(dst);
60+
crypto_sha_release();
61+
}
62+
else {
63+
mbedtls_sha1_init(dst);
64+
}
65+
66+
// We still have potential to get H/W context due to race condition. Retry if need be.
67+
}
68+
5469
if (src->ishw) {
5570
// Clone S/W ctx from H/W ctx
5671
dst->ishw = 0;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
5151
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
5252
const mbedtls_sha256_context *src)
5353
{
54+
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55+
while (dst->ishw) {
56+
mbedtls_sha256_free(dst);
57+
// We just want S/W context, so we lock SHA H/W resource if it is available.
58+
if (crypto_sha_acquire()) {
59+
mbedtls_sha256_init(dst);
60+
crypto_sha_release();
61+
}
62+
else {
63+
mbedtls_sha256_init(dst);
64+
}
65+
66+
// We still have potential to get H/W context due to race condition. Retry if need be.
67+
}
68+
5469
if (src->ishw) {
5570
// Clone S/W ctx from H/W ctx
5671
dst->ishw = 0;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
5151
void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
5252
const mbedtls_sha512_context *src)
5353
{
54+
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55+
while (dst->ishw) {
56+
mbedtls_sha512_free(dst);
57+
// We just want S/W context, so we lock SHA H/W resource if it is available.
58+
if (crypto_sha_acquire()) {
59+
mbedtls_sha512_init(dst);
60+
crypto_sha_release();
61+
}
62+
else {
63+
mbedtls_sha512_init(dst);
64+
}
65+
66+
// We still have potential to get H/W context due to race condition. Retry if need be.
67+
}
68+
5469
if (src->ishw) {
5570
// Clone S/W ctx from H/W ctx
5671
dst->ishw = 0;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
5151
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
5252
const mbedtls_sha1_context *src)
5353
{
54+
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55+
while (dst->ishw) {
56+
mbedtls_sha1_free(dst);
57+
// We just want S/W context, so we lock SHA H/W resource if it is available.
58+
if (crypto_sha_acquire()) {
59+
mbedtls_sha1_init(dst);
60+
crypto_sha_release();
61+
}
62+
else {
63+
mbedtls_sha1_init(dst);
64+
}
65+
66+
// We still have potential to get H/W context due to race condition. Retry if need be.
67+
}
68+
5469
if (src->ishw) {
5570
// Clone S/W ctx from H/W ctx
5671
dst->ishw = 0;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
5151
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
5252
const mbedtls_sha256_context *src)
5353
{
54+
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55+
while (dst->ishw) {
56+
mbedtls_sha256_free(dst);
57+
// We just want S/W context, so we lock SHA H/W resource if it is available.
58+
if (crypto_sha_acquire()) {
59+
mbedtls_sha256_init(dst);
60+
crypto_sha_release();
61+
}
62+
else {
63+
mbedtls_sha256_init(dst);
64+
}
65+
66+
// We still have potential to get H/W context due to race condition. Retry if need be.
67+
}
68+
5469
if (src->ishw) {
5570
// Clone S/W ctx from H/W ctx
5671
dst->ishw = 0;

0 commit comments

Comments
 (0)