92
92
}
93
93
#endif /* PUT_UINT64_BE */
94
94
95
+ #if defined(MBEDTLS_SHA512_SMALLER )
96
+ static void sha512_put_uint64_be ( uint64_t n , unsigned char * b , uint8_t i )
97
+ {
98
+ PUT_UINT64_BE (n , b , i );
99
+ }
100
+ #else
101
+ #define sha512_put_uint64_be PUT_UINT64_BE
102
+ #endif /* MBEDTLS_SHA512_SMALLER */
103
+
95
104
void mbedtls_sha512_init ( mbedtls_sha512_context * ctx )
96
105
{
97
106
SHA512_VALIDATE ( ctx != NULL );
@@ -219,7 +228,7 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
219
228
{
220
229
int i ;
221
230
uint64_t temp1 , temp2 , W [80 ];
222
- uint64_t A , B , C , D , E , F , G , H ;
231
+ uint64_t A [ 8 ] ;
223
232
224
233
SHA512_VALIDATE_RET ( ctx != NULL );
225
234
SHA512_VALIDATE_RET ( (const unsigned char * )data != NULL );
@@ -244,6 +253,28 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
244
253
(d) += temp1; (h) = temp1 + temp2; \
245
254
} while( 0 )
246
255
256
+ for ( i = 0 ; i < 8 ; i ++ )
257
+ A [i ] = ctx -> state [i ];
258
+
259
+ #if defined(MBEDTLS_SHA512_SMALLER )
260
+ for ( i = 0 ; i < 80 ; i ++ )
261
+ {
262
+ if ( i < 16 )
263
+ {
264
+ GET_UINT64_BE ( W [i ], data , i << 3 );
265
+ }
266
+ else
267
+ {
268
+ W [i ] = S1 (W [i - 2 ]) + W [i - 7 ] +
269
+ S0 (W [i - 15 ]) + W [i - 16 ];
270
+ }
271
+
272
+ P ( A [0 ], A [1 ], A [2 ], A [3 ], A [4 ], A [5 ], A [6 ], A [7 ], W [i ], K [i ] );
273
+
274
+ temp1 = A [7 ]; A [7 ] = A [6 ]; A [6 ] = A [5 ]; A [5 ] = A [4 ]; A [4 ] = A [3 ];
275
+ A [3 ] = A [2 ]; A [2 ] = A [1 ]; A [1 ] = A [0 ]; A [0 ] = temp1 ;
276
+ }
277
+ #else /* MBEDTLS_SHA512_SMALLER */
247
278
for ( i = 0 ; i < 16 ; i ++ )
248
279
{
249
280
GET_UINT64_BE ( W [i ], data , i << 3 );
@@ -255,37 +286,23 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
255
286
S0 (W [i - 15 ]) + W [i - 16 ];
256
287
}
257
288
258
- A = ctx -> state [0 ];
259
- B = ctx -> state [1 ];
260
- C = ctx -> state [2 ];
261
- D = ctx -> state [3 ];
262
- E = ctx -> state [4 ];
263
- F = ctx -> state [5 ];
264
- G = ctx -> state [6 ];
265
- H = ctx -> state [7 ];
266
289
i = 0 ;
267
-
268
290
do
269
291
{
270
- P ( A , B , C , D , E , F , G , H , W [i ], K [i ] ); i ++ ;
271
- P ( H , A , B , C , D , E , F , G , W [i ], K [i ] ); i ++ ;
272
- P ( G , H , A , B , C , D , E , F , W [i ], K [i ] ); i ++ ;
273
- P ( F , G , H , A , B , C , D , E , W [i ], K [i ] ); i ++ ;
274
- P ( E , F , G , H , A , B , C , D , W [i ], K [i ] ); i ++ ;
275
- P ( D , E , F , G , H , A , B , C , W [i ], K [i ] ); i ++ ;
276
- P ( C , D , E , F , G , H , A , B , W [i ], K [i ] ); i ++ ;
277
- P ( B , C , D , E , F , G , H , A , W [i ], K [i ] ); i ++ ;
292
+ P ( A [ 0 ], A [ 1 ], A [ 2 ], A [ 3 ], A [ 4 ], A [ 5 ], A [ 6 ], A [ 7 ] , W [i ], K [i ] ); i ++ ;
293
+ P ( A [ 7 ] , A [ 0 ], A [ 1 ], A [ 2 ], A [ 3 ], A [ 4 ], A [ 5 ], A [ 6 ] , W [i ], K [i ] ); i ++ ;
294
+ P ( A [ 6 ], A [ 7 ] , A [ 0 ], A [ 1 ], A [ 2 ], A [ 3 ], A [ 4 ], A [ 5 ] , W [i ], K [i ] ); i ++ ;
295
+ P ( A [ 5 ], A [ 6 ], A [ 7 ] , A [ 0 ], A [ 1 ], A [ 2 ], A [ 3 ], A [ 4 ] , W [i ], K [i ] ); i ++ ;
296
+ P ( A [ 4 ], A [ 5 ], A [ 6 ], A [ 7 ] , A [ 0 ], A [ 1 ], A [ 2 ], A [ 3 ] , W [i ], K [i ] ); i ++ ;
297
+ P ( A [ 3 ], A [ 4 ], A [ 5 ], A [ 6 ], A [ 7 ] , A [ 0 ], A [ 1 ], A [ 2 ] , W [i ], K [i ] ); i ++ ;
298
+ P ( A [ 2 ], A [ 3 ], A [ 4 ], A [ 5 ], A [ 6 ], A [ 7 ] , A [ 0 ], A [ 1 ] , W [i ], K [i ] ); i ++ ;
299
+ P ( A [ 1 ], A [ 2 ], A [ 3 ], A [ 4 ], A [ 5 ], A [ 6 ], A [ 7 ] , A [ 0 ] , W [i ], K [i ] ); i ++ ;
278
300
}
279
301
while ( i < 80 );
302
+ #endif /* MBEDTLS_SHA512_SMALLER */
280
303
281
- ctx -> state [0 ] += A ;
282
- ctx -> state [1 ] += B ;
283
- ctx -> state [2 ] += C ;
284
- ctx -> state [3 ] += D ;
285
- ctx -> state [4 ] += E ;
286
- ctx -> state [5 ] += F ;
287
- ctx -> state [6 ] += G ;
288
- ctx -> state [7 ] += H ;
304
+ for ( i = 0 ; i < 8 ; i ++ )
305
+ ctx -> state [i ] += A [i ];
289
306
290
307
return ( 0 );
291
308
}
@@ -403,26 +420,26 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
403
420
| ( ctx -> total [1 ] << 3 );
404
421
low = ( ctx -> total [0 ] << 3 );
405
422
406
- PUT_UINT64_BE ( high , ctx -> buffer , 112 );
407
- PUT_UINT64_BE ( low , ctx -> buffer , 120 );
423
+ sha512_put_uint64_be ( high , ctx -> buffer , 112 );
424
+ sha512_put_uint64_be ( low , ctx -> buffer , 120 );
408
425
409
426
if ( ( ret = mbedtls_internal_sha512_process ( ctx , ctx -> buffer ) ) != 0 )
410
427
return ( ret );
411
428
412
429
/*
413
430
* Output final state
414
431
*/
415
- PUT_UINT64_BE ( ctx -> state [0 ], output , 0 );
416
- PUT_UINT64_BE ( ctx -> state [1 ], output , 8 );
417
- PUT_UINT64_BE ( ctx -> state [2 ], output , 16 );
418
- PUT_UINT64_BE ( ctx -> state [3 ], output , 24 );
419
- PUT_UINT64_BE ( ctx -> state [4 ], output , 32 );
420
- PUT_UINT64_BE ( ctx -> state [5 ], output , 40 );
432
+ sha512_put_uint64_be ( ctx -> state [0 ], output , 0 );
433
+ sha512_put_uint64_be ( ctx -> state [1 ], output , 8 );
434
+ sha512_put_uint64_be ( ctx -> state [2 ], output , 16 );
435
+ sha512_put_uint64_be ( ctx -> state [3 ], output , 24 );
436
+ sha512_put_uint64_be ( ctx -> state [4 ], output , 32 );
437
+ sha512_put_uint64_be ( ctx -> state [5 ], output , 40 );
421
438
422
439
if ( ctx -> is384 == 0 )
423
440
{
424
- PUT_UINT64_BE ( ctx -> state [6 ], output , 48 );
425
- PUT_UINT64_BE ( ctx -> state [7 ], output , 56 );
441
+ sha512_put_uint64_be ( ctx -> state [6 ], output , 48 );
442
+ sha512_put_uint64_be ( ctx -> state [7 ], output , 56 );
426
443
}
427
444
428
445
return ( 0 );
0 commit comments