Skip to content

Commit b22182f

Browse files
committed
Merge branch 'feature/mbedtls_using_util_algorithm_v3.3' into 'release/v3.3'
mbedtls: mbedtls using SHA/AES of util component instead of its raw algorithm (backport v3.3) See merge request sdk/ESP8266_RTOS_SDK!1240
2 parents 6222b1c + ffaaf07 commit b22182f

File tree

7 files changed

+61
-141
lines changed

7 files changed

+61
-141
lines changed

components/libsodium/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ menu "libsodium"
33
config LIBSODIUM_USE_MBEDTLS_SHA
44
bool "Use mbedTLS SHA256 & SHA512 implementations"
55
default y
6-
depends on !MBEDTLS_HARDWARE_SHA
76
help
87
If this option is enabled, libsodium will use thin wrappers
98
around mbedTLS for SHA256 & SHA512 operations.

components/libsodium/port/crypto_hash_mbedtls/crypto_hash_sha256_mbedtls.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@
1616
#include "mbedtls/sha256.h"
1717
#include <string.h>
1818

19-
#ifdef MBEDTLS_SHA256_ALT
20-
/* Wrapper only works if the libsodium context structure can be mapped
21-
directly to the mbedTLS context structure.
22-
23-
See extended comments in crypto_hash_sha512_mbedtls.c
24-
*/
25-
#error "This wrapper only support standard software mbedTLS SHA"
26-
#endif
27-
2819
/* Sanity check that all the context fields have identical sizes
2920
(this should be more or less given from the SHA256 algorithm)
3021
@@ -55,7 +46,6 @@ static void sha256_libsodium_to_mbedtls(mbedtls_sha256_context *mb_ctx, crypto_h
5546
memcpy(mb_ctx->total, &ls_state->count, sizeof(mb_ctx->total));
5647
memcpy(mb_ctx->state, ls_state->state, sizeof(mb_ctx->state));
5748
memcpy(mb_ctx->buffer, ls_state->buf, sizeof(mb_ctx->buffer));
58-
mb_ctx->is224 = 0;
5949
}
6050

6151
int

components/libsodium/port/crypto_hash_mbedtls/crypto_hash_sha512_mbedtls.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@
1616
#include "mbedtls/sha512.h"
1717
#include <string.h>
1818

19-
#ifdef MBEDTLS_SHA512_ALT
20-
/* Wrapper only works if the libsodium context structure can be mapped
21-
directly to the mbedTLS context structure.
22-
23-
For ESP8266 hardware SHA, the problems are fitting all the data in
24-
the libsodium state structure, and also that libsodium doesn't
25-
have mbedtls_sha512_free() or mbedtls_sha512_clone() so we can't
26-
manage the hardware state in a clean way.
27-
*/
28-
#error "This wrapper only support standard software mbedTLS SHA"
29-
#endif
30-
3119
/* Sanity check that all the context fields have identical sizes
3220
(this should be more or less given from the SHA512 algorithm)
3321
@@ -59,7 +47,6 @@ static void sha512_libsodium_to_mbedtls(mbedtls_sha512_context *mb_ctx, crypto_h
5947
memcpy(mb_ctx->total, ls_state->count, sizeof(mb_ctx->total));
6048
memcpy(mb_ctx->state, ls_state->state, sizeof(mb_ctx->state));
6149
memcpy(mb_ctx->buffer, ls_state->buf, sizeof(mb_ctx->buffer));
62-
mb_ctx->is384 = 0;
6350
}
6451

6552
int

components/mbedtls/Kconfig

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -116,51 +116,6 @@ menu "mbedTLS"
116116
default 3 if MBEDTLS_DEBUG_LEVEL_DEBUG
117117
default 4 if MBEDTLS_DEBUG_LEVEL_VERBOSE
118118

119-
config MBEDTLS_HARDWARE_AES
120-
bool "Enable hardware AES acceleration"
121-
default y
122-
help
123-
Enable hardware accelerated AES encryption & decryption.
124-
125-
Note that if the ESP32 CPU is running at 240MHz, hardware AES does not
126-
offer any speed boost over software AES.
127-
128-
config MBEDTLS_HARDWARE_MPI
129-
bool "Enable hardware MPI (bignum) acceleration"
130-
default n
131-
help
132-
Enable hardware accelerated multiple precision integer operations.
133-
134-
Hardware accelerated multiplication, modulo multiplication,
135-
and modular exponentiation for up to 4096 bit results.
136-
137-
These operations are used by RSA.
138-
139-
config MBEDTLS_MPI_USE_INTERRUPT
140-
bool "Use interrupt for MPI operations"
141-
depends on MBEDTLS_HARDWARE_MPI
142-
default n
143-
help
144-
Use an interrupt to coordinate MPI operations.
145-
146-
This allows other code to run on the CPU while an MPI operation is pending.
147-
Otherwise the CPU busy-waits.
148-
149-
config MBEDTLS_HARDWARE_SHA
150-
bool "Enable hardware SHA acceleration"
151-
default y
152-
help
153-
Enable hardware accelerated SHA1, SHA256, SHA384 & SHA512 in mbedTLS.
154-
155-
Due to a hardware limitation, hardware acceleration is only
156-
guaranteed if SHA digests are calculated one at a time. If more
157-
than one SHA digest is calculated at the same time, one will
158-
be calculated fully in hardware and the rest will be calculated
159-
(at least partially calculated) in software. This happens automatically.
160-
161-
SHA hardware acceleration is faster than software in some situations but
162-
slower in others. You should benchmark to find the best setting for you.
163-
164119
config MBEDTLS_HAVE_TIME
165120
bool "Enable mbedtls time"
166121
depends on !ESP32_TIME_SYSCALL_USE_NONE

components/mbedtls/port/include/mbedtls/esp_config.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,40 +108,21 @@
108108
* within the modules that are enabled.
109109
* \{
110110
*/
111-
112-
/* The following units have ESP32 hardware support,
113-
uncommenting each _ALT macro will use the
114-
hardware-accelerated implementation. */
115-
#ifdef CONFIG_MBEDTLS_HARDWARE_AES
116111
#define MBEDTLS_AES_ALT
117-
#else
118-
#undef MBEDTLS_AES_ALT
119-
#endif
120112

121113
/* MBEDTLS_SHAxx_ALT to enable hardware SHA support
122114
with software fallback.
123115
*/
124-
#ifdef CONFIG_MBEDTLS_HARDWARE_SHA
125116
#define MBEDTLS_SHA1_ALT
126117
#define MBEDTLS_SHA256_ALT
127118
#define MBEDTLS_SHA512_ALT
128-
#else
129-
#undef MBEDTLS_SHA1_ALT
130-
#undef MBEDTLS_SHA256_ALT
131-
#undef MBEDTLS_SHA512_ALT
132-
#endif
133119

134120
/* The following MPI (bignum) functions have ESP32 hardware support,
135121
Uncommenting these macros will use the hardware-accelerated
136122
implementations.
137123
*/
138-
#ifdef CONFIG_MBEDTLS_HARDWARE_MPI
139-
#define MBEDTLS_MPI_EXP_MOD_ALT
140-
#define MBEDTLS_MPI_MUL_MPI_ALT
141-
#else
142124
#undef MBEDTLS_MPI_EXP_MOD_ALT
143125
#undef MBEDTLS_MPI_MUL_MPI_ALT
144-
#endif
145126

146127
/**
147128
* \def MBEDTLS_ENTROPY_HARDWARE_ALT

components/util/include/esp_sha.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ typedef struct {
2828
} esp_sha1_t;
2929

3030
typedef struct {
31-
uint64_t length;
32-
uint32_t curlen;
31+
uint32_t total[2];
3332
uint32_t state[8];
34-
uint8_t buf[64];
33+
uint8_t buffer[64];
3534
} esp_sha256_t;
3635

3736
typedef struct {

components/util/src/sha256.c

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static const uint32_t K[64] = {
7979
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
8080
};
8181

82-
static void esp_sha256_transform(esp_sha256_t *ctx, uint8_t *buf)
82+
static void esp_sha256_transform(esp_sha256_t *ctx, const uint8_t *buf)
8383
{
8484
uint32_t S[8], W[64], t0, t1;
8585
uint32_t t;
@@ -108,8 +108,8 @@ int esp_sha256_init(esp_sha256_t *ctx)
108108
{
109109
util_assert(ctx);
110110

111-
ctx->curlen = 0;
112-
ctx->length = 0;
111+
ctx->total[0] = 0;
112+
ctx->total[1] = 0;
113113

114114
ctx->state[0] = 0x6A09E667UL;
115115
ctx->state[1] = 0xBB67AE85UL;
@@ -127,8 +127,8 @@ int esp_sha224_init(esp_sha224_t *ctx)
127127
{
128128
util_assert(ctx);
129129

130-
ctx->curlen = 0;
131-
ctx->length = 0;
130+
ctx->total[0] = 0;
131+
ctx->total[1] = 0;
132132

133133
ctx->state[0] = 0xC1059ED8;
134134
ctx->state[1] = 0x367CD507;
@@ -144,39 +144,43 @@ int esp_sha224_init(esp_sha224_t *ctx)
144144

145145
int esp_sha256_update(esp_sha256_t *ctx, const void *src, size_t size)
146146
{
147-
const uint8_t *in = (const uint8_t *)src;
148-
uint32_t n;
147+
size_t fill;
148+
uint32_t left;
149+
const uint8_t *input = (const uint8_t *)src;
149150

150151
util_assert(ctx);
151152
util_assert(src);
152153
util_assert(size);
153154

154-
if (ctx->curlen >= sizeof(ctx->buf))
155-
return -1;
156-
157-
while (size > 0) {
158-
if (ctx->curlen == 0 && size >= 64) {
159-
esp_sha256_transform(ctx, (uint8_t *) in);
160-
161-
ctx->length += 64 * 8;
162-
in += 64;
163-
size -= 64;
164-
} else {
165-
n = MIN(size, (64 - ctx->curlen));
166-
memcpy(ctx->buf + ctx->curlen, in, n);
167-
ctx->curlen += n;
168-
in += n;
169-
size -= n;
170-
171-
if (ctx->curlen == 64) {
172-
esp_sha256_transform(ctx, ctx->buf);
173-
174-
ctx->length += 8 * 64;
175-
ctx->curlen = 0;
176-
}
177-
}
155+
left = ctx->total[0] & 0x3F;
156+
fill = 64 - left;
157+
158+
ctx->total[0] += size;
159+
ctx->total[0] &= 0xFFFFFFFF;
160+
161+
if (ctx->total[0] < size)
162+
ctx->total[1]++;
163+
164+
if (left && size >= fill) {
165+
memcpy(ctx->buffer + left, input, fill);
166+
167+
esp_sha256_transform(ctx, ctx->buffer);
168+
169+
input += fill;
170+
size -= fill;
171+
left = 0;
172+
}
173+
174+
while (size >= 64) {
175+
esp_sha256_transform(ctx, input);
176+
177+
input += 64;
178+
size -= 64;
178179
}
179180

181+
if (size > 0)
182+
memcpy(ctx->buffer + left, input, size);
183+
180184
return 0;
181185
}
182186

@@ -191,37 +195,42 @@ int esp_sha224_update(esp_sha224_t *ctx, const void *src, size_t size)
191195

192196
int esp_sha224_finish(esp_sha224_t *ctx, void *dest)
193197
{
194-
int i;
198+
uint32_t used;
199+
uint32_t high, low;
195200
uint8_t *out = (uint8_t *)dest;
196201

197202
util_assert(ctx);
198203
util_assert(dest);
199204

200-
if (ctx->curlen >= sizeof(ctx->buf))
201-
return -1;
202-
203-
ctx->length += ctx->curlen * 8;
205+
used = ctx->total[0] & 0x3F;
204206

205-
ctx->buf[ctx->curlen++] = 0x80;
207+
ctx->buffer[used++] = 0x80;
206208

207-
if (ctx->curlen > 56) {
208-
while (ctx->curlen < 64)
209-
ctx->buf[ctx->curlen++] = 0;
210-
211-
esp_sha256_transform(ctx, ctx->buf);
212-
ctx->curlen = 0;
209+
if (used <= 56) {
210+
memset(ctx->buffer + used, 0, 56 - used);
211+
} else {
212+
memset(ctx->buffer + used, 0, 64 - used);
213+
esp_sha256_transform(ctx, ctx->buffer);
214+
memset(ctx->buffer, 0, 56);
213215
}
214216

215-
while (ctx->curlen < 56)
216-
ctx->buf[ctx->curlen++] = 0;
217+
high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
218+
low = (ctx->total[0] << 3);
217219

218-
ESP_PUT_BE64(ctx->buf + 56, ctx->length);
219-
esp_sha256_transform(ctx, ctx->buf);
220+
ESP_PUT_BE32(ctx->buffer + 56, high);
221+
ESP_PUT_BE32(ctx->buffer + 60, low);
220222

221-
for (i = 0; i < 7; i++)
222-
ESP_PUT_BE32(out + (4 * i), ctx->state[i]);
223+
esp_sha256_transform(ctx, ctx->buffer);
223224

224-
return 0;
225+
ESP_PUT_BE32(out + 0, ctx->state[0]);
226+
ESP_PUT_BE32(out + 4, ctx->state[1]);
227+
ESP_PUT_BE32(out + 8, ctx->state[2]);
228+
ESP_PUT_BE32(out + 12, ctx->state[3]);
229+
ESP_PUT_BE32(out + 16, ctx->state[4]);
230+
ESP_PUT_BE32(out + 20, ctx->state[5]);
231+
ESP_PUT_BE32(out + 24, ctx->state[6]);
232+
233+
return( 0 );
225234
}
226235

227236
int esp_sha256_finish(esp_sha256_t *ctx, void *dest)

0 commit comments

Comments
 (0)