Skip to content

Commit 2038851

Browse files
committed
Merge branch 'fix/sha_port_formatting_and_local_val_types' into 'master'
fix(mbedtls/sha): Fix formatting and change local variable's types Closes IDF-12217 and IDF-12218 See merge request espressif/esp-idf!36792
2 parents 68b79fc + d403005 commit 2038851

File tree

4 files changed

+134
-132
lines changed

4 files changed

+134
-132
lines changed

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

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
#include "sha/sha_core.h"
3737

3838
/* Implementation that should never be optimized out by the compiler */
39-
static void mbedtls_zeroize( void *v, size_t n )
39+
static void mbedtls_zeroize(void *v, size_t n)
4040
{
4141
volatile unsigned char *p = (unsigned char *)v;
42-
while ( n-- ) {
42+
while (n--) {
4343
*p++ = 0;
4444
}
4545
}
@@ -51,40 +51,40 @@ static void mbedtls_zeroize( void *v, size_t n )
5151
#ifndef PUT_UINT32_BE
5252
#define PUT_UINT32_BE(n,b,i) \
5353
{ \
54-
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
55-
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
56-
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
57-
(b)[(i) + 3] = (unsigned char) ( (n) ); \
54+
(b)[(i) ] = (unsigned char) ((n) >> 24); \
55+
(b)[(i) + 1] = (unsigned char) ((n) >> 16); \
56+
(b)[(i) + 2] = (unsigned char) ((n) >> 8); \
57+
(b)[(i) + 3] = (unsigned char) ((n) ); \
5858
}
5959
#endif
6060

61-
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
61+
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
6262
{
63-
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
63+
memset(ctx, 0, sizeof(mbedtls_sha1_context));
6464
}
6565

66-
void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
66+
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
6767
{
68-
if ( ctx == NULL ) {
68+
if (ctx == NULL) {
6969
return;
7070
}
71-
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
71+
mbedtls_zeroize(ctx, sizeof(mbedtls_sha1_context));
7272
}
7373

74-
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
75-
const mbedtls_sha1_context *src )
74+
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
75+
const mbedtls_sha1_context *src)
7676
{
7777
memcpy(dst, src, sizeof(mbedtls_sha1_context));
7878
}
7979

8080
/*
8181
* SHA-1 context setup
8282
*/
83-
int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
83+
int mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
8484
{
8585
ctx->total[0] = 0;
8686
ctx->total[1] = 0;
87-
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
87+
memset(ctx, 0, sizeof(mbedtls_sha1_context));
8888
ctx->mode = SHA1;
8989

9090
return 0;
@@ -110,7 +110,7 @@ static void esp_internal_sha1_block_process(mbedtls_sha1_context *ctx, const uin
110110
}
111111
}
112112

113-
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
113+
int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
114114
{
115115
esp_sha_acquire_hardware();
116116
esp_internal_sha_update_state(ctx);
@@ -133,12 +133,12 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned cha
133133
return 0;
134134
}
135135

136-
int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
136+
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

141-
if ( !ilen || (input == NULL)) {
141+
if (!ilen || (input == NULL)) {
142142
return 0;
143143
}
144144

@@ -148,21 +148,21 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
148148
ctx->total[0] += (uint32_t) ilen;
149149
ctx->total[0] &= 0xFFFFFFFF;
150150

151-
if ( ctx->total[0] < (uint32_t) ilen ) {
151+
if (ctx->total[0] < (uint32_t) ilen) {
152152
ctx->total[1]++;
153153
}
154154

155-
if ( left && ilen >= fill ) {
156-
memcpy( (void *) (ctx->buffer + left), input, fill );
155+
if (left && ilen >= fill) {
156+
memcpy((void *) (ctx->buffer + left), input, fill);
157157
input += fill;
158158
ilen -= fill;
159159
left = 0;
160160
local_len = 64;
161161
}
162162

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

165-
if ( len || local_len) {
165+
if (len || local_len) {
166166

167167
esp_sha_acquire_hardware();
168168

@@ -179,12 +179,12 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
179179
#endif /* SOC_SHA_SUPPORT_DMA */
180180
{
181181
/* First process buffered block, if any */
182-
if ( local_len ) {
182+
if (local_len) {
183183
esp_internal_sha1_block_process(ctx, ctx->buffer);
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
}
@@ -196,8 +196,8 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
196196

197197
}
198198

199-
if ( ilen > 0 ) {
200-
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
199+
if (ilen > 0) {
200+
memcpy((void *) (ctx->buffer + left), input + len, ilen - len);
201201
}
202202
return 0;
203203
}
@@ -212,28 +212,27 @@ static const unsigned char sha1_padding[64] = {
212212
/*
213213
* SHA-1 final digest
214214
*/
215-
int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
215+
int mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20])
216216
{
217217
int ret = -1;
218218
uint32_t last, padn;
219219
uint32_t high, low;
220220
unsigned char msglen[8];
221221

222-
high = ( ctx->total[0] >> 29 )
223-
| ( ctx->total[1] << 3 );
224-
low = ( ctx->total[0] << 3 );
222+
high = (ctx->total[0] >> 29)
223+
| (ctx->total[1] << 3);
224+
low = (ctx->total[0] << 3);
225225

226-
PUT_UINT32_BE( high, msglen, 0 );
227-
PUT_UINT32_BE( low, msglen, 4 );
226+
PUT_UINT32_BE(high, msglen, 0);
227+
PUT_UINT32_BE(low, msglen, 4);
228228

229229
last = ctx->total[0] & 0x3F;
230-
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
230+
padn = (last < 56) ? (56 - last) : (120 - last);
231231

232-
233-
if ( ( ret = mbedtls_sha1_update( ctx, sha1_padding, padn ) ) != 0 ) {
232+
if ((ret = mbedtls_sha1_update(ctx, sha1_padding, padn)) != 0) {
234233
return ret;
235234
}
236-
if ( ( ret = mbedtls_sha1_update( ctx, msglen, 8 ) ) != 0 ) {
235+
if ((ret = mbedtls_sha1_update(ctx, msglen, 8)) != 0) {
237236
return ret;
238237
}
239238

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

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
#include "sha/sha_core.h"
3737

3838
/* Implementation that should never be optimized out by the compiler */
39-
static void mbedtls_zeroize( void *v, size_t n )
39+
static void mbedtls_zeroize(void *v, size_t n)
4040
{
4141
volatile unsigned char *p = v;
42-
while ( n-- ) {
42+
while (n--) {
4343
*p++ = 0;
4444
}
4545
}
@@ -50,51 +50,51 @@ static void mbedtls_zeroize( void *v, size_t n )
5050
#ifndef GET_UINT32_BE
5151
#define GET_UINT32_BE(n,b,i) \
5252
do { \
53-
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
54-
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
55-
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
56-
| ( (uint32_t) (b)[(i) + 3] ); \
57-
} while( 0 )
53+
(n) = ((uint32_t) (b)[(i) ] << 24) \
54+
| ((uint32_t) (b)[(i) + 1] << 16) \
55+
| ((uint32_t) (b)[(i) + 2] << 8) \
56+
| ((uint32_t) (b)[(i) + 3] ); \
57+
} while(0)
5858
#endif
5959

6060
#ifndef PUT_UINT32_BE
6161
#define PUT_UINT32_BE(n,b,i) \
6262
do { \
63-
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
64-
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
65-
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
66-
(b)[(i) + 3] = (unsigned char) ( (n) ); \
67-
} while( 0 )
63+
(b)[(i) ] = (unsigned char) ((n) >> 24); \
64+
(b)[(i) + 1] = (unsigned char) ((n) >> 16); \
65+
(b)[(i) + 2] = (unsigned char) ((n) >> 8); \
66+
(b)[(i) + 3] = (unsigned char) ((n) ); \
67+
} while(0)
6868
#endif
6969

70-
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
70+
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
7171
{
72-
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
72+
memset(ctx, 0, sizeof(mbedtls_sha256_context));
7373
}
7474

75-
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
75+
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
7676
{
77-
if ( ctx == NULL ) {
77+
if (ctx == NULL) {
7878
return;
7979
}
8080

81-
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
81+
mbedtls_zeroize(ctx, sizeof(mbedtls_sha256_context));
8282
}
8383

84-
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
85-
const mbedtls_sha256_context *src )
84+
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
85+
const mbedtls_sha256_context *src)
8686
{
8787
*dst = *src;
8888
}
8989

9090
/*
9191
* SHA-256 context setup
9292
*/
93-
int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
93+
int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
9494
{
95-
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
95+
memset(ctx, 0, sizeof(mbedtls_sha256_context));
9696

97-
if ( is224 ) {
97+
if (is224) {
9898
ctx->mode = SHA2_224;
9999
} else {
100100
ctx->mode = SHA2_256;
@@ -123,7 +123,7 @@ static void esp_internal_sha256_block_process(mbedtls_sha256_context *ctx, const
123123
}
124124
}
125125

126-
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
126+
int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64])
127127
{
128128
esp_sha_acquire_hardware();
129129
esp_internal_sha_update_state(ctx);
@@ -149,13 +149,13 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned
149149
/*
150150
* SHA-256 process buffer
151151
*/
152-
int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
153-
size_t ilen )
152+
int mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input,
153+
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

158-
if ( ilen == 0 ) {
158+
if (ilen == 0) {
159159
return 0;
160160
}
161161

@@ -165,22 +165,23 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
165165
ctx->total[0] += (uint32_t) ilen;
166166
ctx->total[0] &= 0xFFFFFFFF;
167167

168-
if ( ctx->total[0] < (uint32_t) ilen ) {
168+
if (ctx->total[0] < (uint32_t) ilen) {
169169
ctx->total[1]++;
170170
}
171171

172172
/* Check if any data pending from previous call to this API */
173-
if ( left && ilen >= fill ) {
174-
memcpy( (void *) (ctx->buffer + left), input, fill );
173+
if (left && ilen >= fill) {
174+
memcpy((void *) (ctx->buffer + left), input, fill);
175175

176176
input += fill;
177177
ilen -= fill;
178178
left = 0;
179179
local_len = 64;
180180
}
181181

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

185186
esp_sha_acquire_hardware();
186187

@@ -197,12 +198,12 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
197198
#endif /* SOC_SHA_SUPPORT_DMA */
198199
{
199200
/* First process buffered block, if any */
200-
if ( local_len ) {
201+
if (local_len) {
201202
esp_internal_sha256_block_process(ctx, ctx->buffer);
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
}
@@ -213,8 +214,8 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
213214
esp_sha_release_hardware();
214215
}
215216

216-
if ( ilen > 0 ) {
217-
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
217+
if (ilen > 0) {
218+
memcpy((void *) (ctx->buffer + left), input + len, ilen - len);
218219
}
219220

220221
return 0;
@@ -230,28 +231,28 @@ static const unsigned char sha256_padding[64] = {
230231
/*
231232
* SHA-256 final digest
232233
*/
233-
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
234+
int mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char *output)
234235
{
235236
int ret = -1;
236237
uint32_t last, padn;
237238
uint32_t high, low;
238239
unsigned char msglen[8];
239240

240-
high = ( ctx->total[0] >> 29 )
241-
| ( ctx->total[1] << 3 );
242-
low = ( ctx->total[0] << 3 );
241+
high = (ctx->total[0] >> 29)
242+
| (ctx->total[1] << 3);
243+
low = (ctx->total[0] << 3);
243244

244-
PUT_UINT32_BE( high, msglen, 0 );
245-
PUT_UINT32_BE( low, msglen, 4 );
245+
PUT_UINT32_BE(high, msglen, 0);
246+
PUT_UINT32_BE(low, msglen, 4);
246247

247248
last = ctx->total[0] & 0x3F;
248-
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
249+
padn = (last < 56) ? (56 - last) : (120 - last);
249250

250-
if ( ( ret = mbedtls_sha256_update( ctx, sha256_padding, padn ) ) != 0 ) {
251+
if ((ret = mbedtls_sha256_update(ctx, sha256_padding, padn)) != 0) {
251252
return ret;
252253
}
253254

254-
if ( ( ret = mbedtls_sha256_update( ctx, msglen, 8 ) ) != 0 ) {
255+
if ((ret = mbedtls_sha256_update(ctx, msglen, 8)) != 0) {
255256
return ret;
256257
}
257258

0 commit comments

Comments
 (0)