Skip to content

Commit 116b14a

Browse files
committed
[NUC472/M487] Refine code with SHA context selection in SHA alter.
1 parent 980cb6b commit 116b14a

File tree

5 files changed

+80
-65
lines changed

5 files changed

+80
-65
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
#include "nu_bitutil.h"
2424
#include "string.h"
2525

26-
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
26+
/* Choose SHA S/W or H/W context and initialize it
27+
*
28+
* try_hw:
29+
* 0: Initialize S/W context
30+
* 1: Try acquiring SHA H/W resource first and initialize its H/W context if successful. If failed, initialize S/W context.
31+
*/
32+
static void mbedtls_sha1_init_internal(mbedtls_sha1_context *ctx, int try_hw)
2733
{
28-
if (crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_acquire()) {
2935
ctx->ishw = 1;
3036
mbedtls_sha1_hw_init(&ctx->hw_ctx);
3137
} else {
@@ -34,6 +40,11 @@ void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
3440
}
3541
}
3642

43+
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
44+
{
45+
mbedtls_sha1_init_internal(ctx, 1);
46+
}
47+
3748
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
3849
{
3950
if (ctx == NULL) {
@@ -52,18 +63,10 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
5263
const mbedtls_sha1_context *src)
5364
{
5465
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55-
while (dst->ishw) {
66+
if (dst->ishw) {
5667
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.
68+
// Force S/W context
69+
mbedtls_sha1_init_internal(dst, 0);
6770
}
6871

6972
if (src->ishw) {

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
#include "nu_bitutil.h"
2424
#include "string.h"
2525

26-
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
26+
/* Choose SHA S/W or H/W context and initialize it
27+
*
28+
* try_hw:
29+
* 0: Initialize S/W context
30+
* 1: Try acquiring SHA H/W resource first and initialize its H/W context if successful. If failed, initialize S/W context.
31+
*/
32+
static void mbedtls_sha256_init_internal(mbedtls_sha256_context *ctx, int try_hw)
2733
{
28-
if (crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_acquire()) {
2935
ctx->ishw = 1;
3036
mbedtls_sha256_hw_init(&ctx->hw_ctx);
3137
} else {
@@ -34,6 +40,11 @@ void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
3440
}
3541
}
3642

43+
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
44+
{
45+
mbedtls_sha256_init_internal(ctx, 1);
46+
}
47+
3748
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
3849
{
3950
if (ctx == NULL) {
@@ -52,18 +63,10 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
5263
const mbedtls_sha256_context *src)
5364
{
5465
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55-
while (dst->ishw) {
66+
if (dst->ishw) {
5667
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.
68+
// Force S/W context
69+
mbedtls_sha256_init_internal(dst, 0);
6770
}
6871

6972
if (src->ishw) {

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
#include "nu_bitutil.h"
2424
#include "string.h"
2525

26-
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
26+
/* Choose SHA S/W or H/W context and initialize it
27+
*
28+
* try_hw:
29+
* 0: Initialize S/W context
30+
* 1: Try acquiring SHA H/W resource first and initialize its H/W context if successful. If failed, initialize S/W context.
31+
*/
32+
static void mbedtls_sha512_init_internal(mbedtls_sha512_context *ctx, int try_hw)
2733
{
28-
if (crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_acquire()) {
2935
ctx->ishw = 1;
3036
mbedtls_sha512_hw_init(&ctx->hw_ctx);
3137
} else {
@@ -34,6 +40,11 @@ void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
3440
}
3541
}
3642

43+
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
44+
{
45+
mbedtls_sha512_init_internal(ctx, 1);
46+
}
47+
3748
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
3849
{
3950
if (ctx == NULL) {
@@ -52,18 +63,10 @@ void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
5263
const mbedtls_sha512_context *src)
5364
{
5465
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55-
while (dst->ishw) {
66+
if (dst->ishw) {
5667
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.
68+
// Force S/W context
69+
mbedtls_sha512_init_internal(dst, 0);
6770
}
6871

6972
if (src->ishw) {

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
#include "nu_bitutil.h"
2424
#include "string.h"
2525

26-
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
26+
/* Choose SHA S/W or H/W context and initialize it
27+
*
28+
* try_hw:
29+
* 0: Initialize S/W context
30+
* 1: Try acquiring SHA H/W resource first and initialize its H/W context if successful. If failed, initialize S/W context.
31+
*/
32+
static void mbedtls_sha1_init_internal(mbedtls_sha1_context *ctx, int try_hw)
2733
{
28-
if (crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_acquire()) {
2935
ctx->ishw = 1;
3036
mbedtls_sha1_hw_init(&ctx->hw_ctx);
3137
} else {
@@ -34,6 +40,11 @@ void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
3440
}
3541
}
3642

43+
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
44+
{
45+
mbedtls_sha1_init_internal(ctx, 1);
46+
}
47+
3748
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
3849
{
3950
if (ctx == NULL) {
@@ -52,18 +63,10 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
5263
const mbedtls_sha1_context *src)
5364
{
5465
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55-
while (dst->ishw) {
66+
if (dst->ishw) {
5667
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.
68+
// Force S/W context
69+
mbedtls_sha1_init_internal(dst, 0);
6770
}
6871

6972
if (src->ishw) {

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
#include "nu_bitutil.h"
2424
#include "string.h"
2525

26-
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
26+
/* Choose SHA S/W or H/W context and initialize it
27+
*
28+
* try_hw:
29+
* 0: Initialize S/W context
30+
* 1: Try acquiring SHA H/W resource first and initialize its H/W context if successful. If failed, initialize S/W context.
31+
*/
32+
static void mbedtls_sha256_init_internal(mbedtls_sha256_context *ctx, int try_hw)
2733
{
28-
if (crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_acquire()) {
2935
ctx->ishw = 1;
3036
mbedtls_sha256_hw_init(&ctx->hw_ctx);
3137
} else {
@@ -34,6 +40,11 @@ void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
3440
}
3541
}
3642

43+
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
44+
{
45+
mbedtls_sha256_init_internal(ctx, 1);
46+
}
47+
3748
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
3849
{
3950
if (ctx == NULL) {
@@ -52,18 +63,10 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
5263
const mbedtls_sha256_context *src)
5364
{
5465
// If dst is H/W context, we need to change it to S/W context first before cloning to.
55-
while (dst->ishw) {
66+
if (dst->ishw) {
5667
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.
68+
// Force S/W context
69+
mbedtls_sha256_init_internal(dst, 0);
6770
}
6871

6972
if (src->ishw) {

0 commit comments

Comments
 (0)