Skip to content

Commit 2717e5b

Browse files
committed
fix(mbedtls/sha): Fix some local variable's types to avoid any substraction overflow error
- Though such a case would not occur given the way it is used the driver layer
1 parent 4f9f6bb commit 2717e5b

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

components/mbedtls/port/sha/core/esp_sha1.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned cha
135135

136136
int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
137137
{
138-
size_t fill;
139-
uint32_t left, len, local_len = 0;
138+
size_t fill, left, len;
139+
uint32_t local_len = 0;
140140

141141
if ( !ilen || (input == NULL)) {
142142
return 0;
@@ -160,7 +160,7 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
160160
local_len = 64;
161161
}
162162

163-
len = (ilen / 64) * 64;
163+
len = SHA_ALIGN_DOWN(ilen , 64);
164164

165165
if ( len || local_len) {
166166

@@ -184,7 +184,7 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
184184
}
185185

186186
uint32_t length_processed = 0;
187-
while ( len - length_processed > 0 ) {
187+
while ( len - length_processed != 0 ) {
188188
esp_internal_sha1_block_process(ctx, input + length_processed);
189189
length_processed += 64;
190190
}

components/mbedtls/port/sha/core/esp_sha256.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned
152152
int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
153153
size_t ilen )
154154
{
155-
size_t fill;
156-
uint32_t left, len, local_len = 0;
155+
size_t fill, left, len;
156+
uint32_t local_len = 0;
157157

158158
if ( ilen == 0 ) {
159159
return 0;
@@ -179,7 +179,8 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
179179
local_len = 64;
180180
}
181181

182-
len = (ilen / 64) * 64;
182+
len = SHA_ALIGN_DOWN(ilen , 64);
183+
183184
if ( len || local_len) {
184185

185186
esp_sha_acquire_hardware();
@@ -202,7 +203,7 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
202203
}
203204

204205
uint32_t length_processed = 0;
205-
while ( len - length_processed > 0 ) {
206+
while ( len - length_processed != 0 ) {
206207
esp_internal_sha256_block_process(ctx, input + length_processed);
207208
length_processed += 64;
208209
}

components/mbedtls/port/sha/core/esp_sha512.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,14 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned
190190
int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
191191
size_t ilen )
192192
{
193-
size_t fill;
194-
unsigned int left, len, local_len = 0;
193+
size_t fill, left, len;
194+
uint32_t local_len = 0;
195195

196196
if ( ilen == 0 ) {
197197
return 0;
198198
}
199199

200-
left = (unsigned int) (ctx->total[0] & 0x7F);
200+
left = (size_t) (ctx->total[0] & 0x7F);
201201
fill = 128 - left;
202202

203203
ctx->total[0] += (uint64_t) ilen;
@@ -215,7 +215,8 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
215215
local_len = 128;
216216
}
217217

218-
len = (ilen / 128) * 128;
218+
len = SHA_ALIGN_DOWN(ilen , 128);
219+
219220
if ( len || local_len) {
220221

221222
esp_sha_acquire_hardware();
@@ -243,7 +244,7 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
243244
}
244245

245246
uint32_t length_processed = 0;
246-
while ( len - length_processed > 0 ) {
247+
while ( len - length_processed != 0 ) {
247248
esp_internal_sha512_block_process(ctx, input + length_processed);
248249
length_processed += 128;
249250
}

components/mbedtls/port/sha/core/include/esp_sha_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern "C" {
3737
#endif
3838
#endif /* SOC_SHA_SUPPORT_DMA */
3939

40+
#define SHA_ALIGN_DOWN(num, align) ((num) & ~((align) - 1))
41+
4042
typedef enum {
4143
SHA_BLOCK_MODE,
4244
#if SOC_SHA_SUPPORT_DMA

0 commit comments

Comments
 (0)