Skip to content

Commit 80341e3

Browse files
committed
Merge branch 'bugfix/fix_uitl_sha_for_mbedtls_error' into 'master'
util: support SHA224/SHA384 calculation although call sha256/sha512 APIs See merge request sdk/ESP8266_RTOS_SDK!1255
2 parents b02ad14 + aa79ba1 commit 80341e3

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

components/libsodium/port/crypto_hash_mbedtls/crypto_hash_sha256_mbedtls.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static void sha256_libsodium_to_mbedtls(mbedtls_sha256_context *mb_ctx, crypto_h
4646
memcpy(mb_ctx->total, &ls_state->count, sizeof(mb_ctx->total));
4747
memcpy(mb_ctx->state, ls_state->state, sizeof(mb_ctx->state));
4848
memcpy(mb_ctx->buffer, ls_state->buf, sizeof(mb_ctx->buffer));
49+
mb_ctx->is224 = 0;
4950
}
5051

5152
int

components/libsodium/port/crypto_hash_mbedtls/crypto_hash_sha512_mbedtls.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static void sha512_libsodium_to_mbedtls(mbedtls_sha512_context *mb_ctx, crypto_h
4747
memcpy(mb_ctx->total, ls_state->count, sizeof(mb_ctx->total));
4848
memcpy(mb_ctx->state, ls_state->state, sizeof(mb_ctx->state));
4949
memcpy(mb_ctx->buffer, ls_state->buf, sizeof(mb_ctx->buffer));
50+
mb_ctx->is384 = 0;
5051
}
5152

5253
int

components/util/include/esp_sha.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ typedef struct {
3131
uint32_t total[2];
3232
uint32_t state[8];
3333
uint8_t buffer[64];
34+
35+
int is224;
3436
} esp_sha256_t;
3537

3638
typedef struct {
3739
uint64_t total[2];
3840
uint64_t state[8];
3941
uint8_t buffer[128];
42+
43+
int is384;
4044
} esp_sha512_t;
4145

4246
typedef esp_sha256_t esp_sha224_t;

components/util/src/sha256.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ int esp_sha256_init(esp_sha256_t *ctx)
108108
{
109109
util_assert(ctx);
110110

111+
ctx->is224 = 0;
112+
111113
ctx->total[0] = 0;
112114
ctx->total[1] = 0;
113115

@@ -127,6 +129,8 @@ int esp_sha224_init(esp_sha224_t *ctx)
127129
{
128130
util_assert(ctx);
129131

132+
ctx->is224 = 1;
133+
130134
ctx->total[0] = 0;
131135
ctx->total[1] = 0;
132136

@@ -230,19 +234,19 @@ int esp_sha224_finish(esp_sha224_t *ctx, void *dest)
230234
ESP_PUT_BE32(out + 20, ctx->state[5]);
231235
ESP_PUT_BE32(out + 24, ctx->state[6]);
232236

237+
if (!ctx->is224) {
238+
ESP_PUT_BE32(out + 28, ctx->state[7]);
239+
}
240+
233241
return( 0 );
234242
}
235243

236244
int esp_sha256_finish(esp_sha256_t *ctx, void *dest)
237245
{
238-
uint8_t *out = (uint8_t *)dest;
239-
240246
util_assert(ctx);
241247
util_assert(dest);
242248

243249
esp_sha224_finish(ctx, dest);
244250

245-
ESP_PUT_BE32(out + 28, ctx->state[7]);
246-
247251
return 0;
248252
}

components/util/src/sha512.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ int esp_sha512_init(esp_sha512_t *ctx)
159159
{
160160
util_assert(ctx);
161161

162+
ctx->is384 = 0;
163+
162164
ctx->total[0] = 0;
163165
ctx->total[1] = 0;
164166

@@ -178,6 +180,8 @@ int esp_sha384_init(esp_sha384_t *ctx)
178180
{
179181
util_assert(ctx);
180182

183+
ctx->is384 = 1;
184+
181185
ctx->total[0] = 0;
182186
ctx->total[1] = 0;
183187

@@ -279,20 +283,20 @@ int esp_sha384_finish(esp_sha384_t *ctx, void *dest)
279283
PUT_UINT64_BE(ctx->state[4], output, 32);
280284
PUT_UINT64_BE(ctx->state[5], output, 40);
281285

286+
if (!ctx->is384) {
287+
PUT_UINT64_BE(ctx->state[6], output, 48);
288+
PUT_UINT64_BE(ctx->state[7], output, 56);
289+
}
290+
282291
return 0;
283292
}
284293

285294
int esp_sha512_finish(esp_sha512_t *ctx, void *dest)
286295
{
287-
uint8_t *output = (uint8_t *)dest;
288-
289296
util_assert(ctx);
290297
util_assert(dest);
291298

292299
esp_sha384_finish(ctx, dest);
293300

294-
PUT_UINT64_BE(ctx->state[6], output, 48);
295-
PUT_UINT64_BE(ctx->state[7], output, 56);
296-
297301
return 0;
298302
}

0 commit comments

Comments
 (0)