Skip to content

Commit db119c7

Browse files
ccli8adbridge
authored andcommitted
Fix compile error for SHA-256 alternative on some condition
Also include non-issue refinement for SHA-1/SHA-256 alternatives.
1 parent 4fcb505 commit db119c7

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
6666
{
6767
unsigned char output[20];
6868
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);
69+
unsigned char *output_pos = output;
70+
unsigned char *output_end = output + (sizeof (output) / sizeof (output[0]));
71+
uint32_t *state_pos = (uint32_t *) &(dst->sw_ctx.state[0]);
72+
while (output_pos != output_end) {
73+
*state_pos ++ = nu_get32_be(output_pos);
74+
output_pos += 4;
75+
}
7476
}
7577
memcpy(dst->sw_ctx.buffer, src->hw_ctx.buffer, src->hw_ctx.buffer_left);
7678
if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#if defined(MBEDTLS_SHA1_C)
2626
#if defined(MBEDTLS_SHA1_ALT)
2727

28-
#include "sha1.h"
2928
#include "sha_alt_hw.h"
3029
#include "sha1_alt_sw.h"
3130

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
#include "mbedtls/sha1.h"
3737

3838
#include <string.h>
39+
#if defined(MBEDTLS_SELF_TEST)
40+
#if defined(MBEDTLS_PLATFORM_C)
41+
#include "mbedtls/platform.h"
42+
#else
43+
#include <stdio.h>
44+
#define mbedtls_printf printf
45+
#endif /* MBEDTLS_PLATFORM_C */
46+
#endif /* MBEDTLS_SELF_TEST */
3947

4048
/* Implementation that should never be optimized out by the compiler */
4149
static void mbedtls_zeroize( void *v, size_t n ) {

targets/TARGET_NUVOTON/TARGET_NUC472/crypto/sha/sha256_alt.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
6666
{
6767
unsigned char output[32];
6868
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-
dst->sw_ctx.state[5] = nu_get32_be(output + 20);
75-
dst->sw_ctx.state[6] = nu_get32_be(output + 24);
76-
dst->sw_ctx.state[7] = nu_get32_be(output + 28);
69+
unsigned char *output_pos = output;
70+
unsigned char *output_end = output + (sizeof (output) / sizeof (output[0]));
71+
uint32_t *state_pos = (uint32_t *) &(dst->sw_ctx.state[0]);
72+
while (output_pos != output_end) {
73+
*state_pos ++ = nu_get32_be(output_pos);
74+
output_pos += 4;
75+
}
7776
}
7877
memcpy(dst->sw_ctx.buffer, src->hw_ctx.buffer, src->hw_ctx.buffer_left);
7978
dst->sw_ctx.is224 = src->hw_ctx.is224;

targets/TARGET_NUVOTON/TARGET_NUC472/crypto/sha/sha256_alt.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
#include MBEDTLS_CONFIG_FILE
2323
#endif
2424

25-
#if defined(MBEDTLS_SHA1_C)
25+
#if defined(MBEDTLS_SHA256_C)
2626
#if defined(MBEDTLS_SHA256_ALT)
2727

28-
#include "sha256.h"
2928
#include "sha_alt_hw.h"
3029
#include "sha256_alt_sw.h"
3130

@@ -103,6 +102,6 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char da
103102
#endif
104103

105104
#endif /* MBEDTLS_SHA256_ALT */
106-
#endif /* MBEDTLS_SHA1_C */
105+
#endif /* MBEDTLS_SHA256_C */
107106

108107
#endif /* sha256_alt.h */

targets/TARGET_NUVOTON/TARGET_NUC472/crypto/sha/sha256_alt_sw.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
#include "mbedtls/sha256.h"
3737

3838
#include <string.h>
39+
#if defined(MBEDTLS_SELF_TEST)
40+
#if defined(MBEDTLS_PLATFORM_C)
41+
#include "mbedtls/platform.h"
42+
#else
43+
#include <stdio.h>
44+
#include <stdlib.h>
45+
#define mbedtls_printf printf
46+
#define mbedtls_calloc calloc
47+
#define mbedtls_free free
48+
#endif /* MBEDTLS_PLATFORM_C */
49+
#endif /* MBEDTLS_SELF_TEST */
3950

4051
/* Implementation that should never be optimized out by the compiler */
4152
static void mbedtls_zeroize( void *v, size_t n ) {
@@ -303,6 +314,6 @@ void mbedtls_sha256_sw_finish( mbedtls_sha256_sw_context *ctx, unsigned char out
303314
PUT_UINT32_BE( ctx->state[7], output, 28 );
304315
}
305316

306-
#endif /* MBEDTLS_SHA1_ALT */
317+
#endif /* MBEDTLS_SHA256_ALT */
307318

308319
#endif /* MBEDTLS_SHA256_C */

0 commit comments

Comments
 (0)