1
+ // ============================================================================
2
+ // The code in this file is based off of that in:
3
+ // https://github.com/awslabs/aws-lc/
4
+ // (commit: d84d2f329dccbc7f3866eab54951bd012e317041)
5
+ // ============================================================================
6
+
1
7
#include <stdlib.h>
2
8
#include <stdint.h>
3
9
#include <string.h>
4
10
11
+ // ============================================================================
12
+ // Helper functions from crypto/internal.h
13
+ // ============================================================================
14
+
5
15
static inline void * OPENSSL_memcpy (void * dst , const void * src , size_t n ) {
6
16
if (n == 0 ) {
7
17
return dst ;
@@ -26,6 +36,14 @@ static inline uint64_t CRYPTO_load_u64_be(const void *ptr) {
26
36
return CRYPTO_bswap8 (ret );
27
37
}
28
38
39
+ // ============================================================================
40
+ // The defintion of sha512_block_data_order from crypto/fipsmodule/sha/sha512.c
41
+ // with only one addition (return_state), needed for Heapster typechecking
42
+ // ============================================================================
43
+
44
+ // Used in sha512_block_data_order below, needed for Heapster typechecking
45
+ void return_state (uint64_t * state ) { }
46
+
29
47
static const uint64_t K512 [80 ] = {
30
48
UINT64_C (0x428a2f98d728ae22 ), UINT64_C (0x7137449123ef65cd ),
31
49
UINT64_C (0xb5c0fbcfec4d3b2f ), UINT64_C (0xe9b5dba58189dbbc ),
@@ -69,9 +87,7 @@ static const uint64_t K512[80] = {
69
87
UINT64_C (0x5fcb6fab3ad6faec ), UINT64_C (0x6c44198c4a475817 ),
70
88
};
71
89
72
- #ifndef ROTR
73
90
#define ROTR (x , s ) (((x) >> s) | (x) << (64 - s))
74
- #endif
75
91
76
92
#define Sigma0 (x ) (ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
77
93
#define Sigma1 (x ) (ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
@@ -99,8 +115,6 @@ static const uint64_t K512[80] = {
99
115
ROUND_00_15(i + j, a, b, c, d, e, f, g, h); \
100
116
} while (0)
101
117
102
- void return_state (uint64_t * state ) { }
103
-
104
118
static void sha512_block_data_order (uint64_t * state , const uint8_t * in ,
105
119
size_t num ) {
106
120
uint64_t a , b , c , d , e , f , g , h , s0 , s1 , T1 ;
@@ -184,7 +198,132 @@ static void sha512_block_data_order(uint64_t *state, const uint8_t *in,
184
198
}
185
199
}
186
200
187
- // needed for Heapster to be able to see the static function above
201
+
202
+ // ============================================================================
203
+ // A definition equivalent to sha512_block_data_order which uses multiple
204
+ // functions, for use with Mr. Solver
205
+ // ============================================================================
206
+
207
+ static void round_00_15 (uint64_t i ,
208
+ uint64_t * a , uint64_t * b , uint64_t * c , uint64_t * d ,
209
+ uint64_t * e , uint64_t * f , uint64_t * g , uint64_t * h ,
210
+ uint64_t * T1 ) {
211
+ * T1 += * h + Sigma1 (* e ) + Ch (* e , * f , * g ) + K512 [i ];
212
+ * h = Sigma0 (* a ) + Maj (* a , * b , * c );
213
+ * d += * T1 ;
214
+ * h += * T1 ;
215
+ }
216
+
217
+ static void round_16_80 (uint64_t i , uint64_t j ,
218
+ uint64_t * a , uint64_t * b , uint64_t * c , uint64_t * d ,
219
+ uint64_t * e , uint64_t * f , uint64_t * g , uint64_t * h ,
220
+ uint64_t * X ,
221
+ uint64_t * s0 , uint64_t * s1 , uint64_t * T1 ) {
222
+ * s0 = X [(j + 1 ) & 0x0f ];
223
+ * s0 = sigma0 (* s0 );
224
+ * s1 = X [(j + 14 ) & 0x0f ];
225
+ * s1 = sigma1 (* s1 );
226
+ * T1 = X [(j ) & 0x0f ] += * s0 + * s1 + X [(j + 9 ) & 0x0f ];
227
+ round_00_15 (i + j , a , b , c , d , e , f , g , h , T1 );
228
+ }
229
+
230
+ // Used in processBlock below, needed for Heapster typechecking
231
+ void return_X (uint64_t * X ) { }
232
+
233
+ static void processBlock (uint64_t * a , uint64_t * b , uint64_t * c , uint64_t * d ,
234
+ uint64_t * e , uint64_t * f , uint64_t * g , uint64_t * h ,
235
+ const uint8_t * in ) {
236
+ uint64_t s0 , s1 , T1 ;
237
+ uint64_t X [16 ];
238
+ int i ;
239
+
240
+ T1 = X [0 ] = CRYPTO_load_u64_be (in );
241
+ round_00_15 (0 , a , b , c , d , e , f , g , h , & T1 );
242
+ T1 = X [1 ] = CRYPTO_load_u64_be (in + 8 );
243
+ round_00_15 (1 , h , a , b , c , d , e , f , g , & T1 );
244
+ T1 = X [2 ] = CRYPTO_load_u64_be (in + 2 * 8 );
245
+ round_00_15 (2 , g , h , a , b , c , d , e , f , & T1 );
246
+ T1 = X [3 ] = CRYPTO_load_u64_be (in + 3 * 8 );
247
+ round_00_15 (3 , f , g , h , a , b , c , d , e , & T1 );
248
+ T1 = X [4 ] = CRYPTO_load_u64_be (in + 4 * 8 );
249
+ round_00_15 (4 , e , f , g , h , a , b , c , d , & T1 );
250
+ T1 = X [5 ] = CRYPTO_load_u64_be (in + 5 * 8 );
251
+ round_00_15 (5 , d , e , f , g , h , a , b , c , & T1 );
252
+ T1 = X [6 ] = CRYPTO_load_u64_be (in + 6 * 8 );
253
+ round_00_15 (6 , c , d , e , f , g , h , a , b , & T1 );
254
+ T1 = X [7 ] = CRYPTO_load_u64_be (in + 7 * 8 );
255
+ round_00_15 (7 , b , c , d , e , f , g , h , a , & T1 );
256
+ T1 = X [8 ] = CRYPTO_load_u64_be (in + 8 * 8 );
257
+ round_00_15 (8 , a , b , c , d , e , f , g , h , & T1 );
258
+ T1 = X [9 ] = CRYPTO_load_u64_be (in + 9 * 8 );
259
+ round_00_15 (9 , h , a , b , c , d , e , f , g , & T1 );
260
+ T1 = X [10 ] = CRYPTO_load_u64_be (in + 10 * 8 );
261
+ round_00_15 (10 , g , h , a , b , c , d , e , f , & T1 );
262
+ T1 = X [11 ] = CRYPTO_load_u64_be (in + 11 * 8 );
263
+ round_00_15 (11 , f , g , h , a , b , c , d , e , & T1 );
264
+ T1 = X [12 ] = CRYPTO_load_u64_be (in + 12 * 8 );
265
+ round_00_15 (12 , e , f , g , h , a , b , c , d , & T1 );
266
+ T1 = X [13 ] = CRYPTO_load_u64_be (in + 13 * 8 );
267
+ round_00_15 (13 , d , e , f , g , h , a , b , c , & T1 );
268
+ T1 = X [14 ] = CRYPTO_load_u64_be (in + 14 * 8 );
269
+ round_00_15 (14 , c , d , e , f , g , h , a , b , & T1 );
270
+ T1 = X [15 ] = CRYPTO_load_u64_be (in + 15 * 8 );
271
+ round_00_15 (15 , b , c , d , e , f , g , h , a , & T1 );
272
+
273
+ return_X (X ); // for Heapster
274
+
275
+ for (i = 16 ; i < 80 ; i += 16 ) {
276
+ round_16_80 (i , 0 , a , b , c , d , e , f , g , h , X , & s0 , & s1 , & T1 );
277
+ round_16_80 (i , 1 , h , a , b , c , d , e , f , g , X , & s0 , & s1 , & T1 );
278
+ round_16_80 (i , 2 , g , h , a , b , c , d , e , f , X , & s0 , & s1 , & T1 );
279
+ round_16_80 (i , 3 , f , g , h , a , b , c , d , e , X , & s0 , & s1 , & T1 );
280
+ round_16_80 (i , 4 , e , f , g , h , a , b , c , d , X , & s0 , & s1 , & T1 );
281
+ round_16_80 (i , 5 , d , e , f , g , h , a , b , c , X , & s0 , & s1 , & T1 );
282
+ round_16_80 (i , 6 , c , d , e , f , g , h , a , b , X , & s0 , & s1 , & T1 );
283
+ round_16_80 (i , 7 , b , c , d , e , f , g , h , a , X , & s0 , & s1 , & T1 );
284
+ round_16_80 (i , 8 , a , b , c , d , e , f , g , h , X , & s0 , & s1 , & T1 );
285
+ round_16_80 (i , 9 , h , a , b , c , d , e , f , g , X , & s0 , & s1 , & T1 );
286
+ round_16_80 (i , 10 , g , h , a , b , c , d , e , f , X , & s0 , & s1 , & T1 );
287
+ round_16_80 (i , 11 , f , g , h , a , b , c , d , e , X , & s0 , & s1 , & T1 );
288
+ round_16_80 (i , 12 , e , f , g , h , a , b , c , d , X , & s0 , & s1 , & T1 );
289
+ round_16_80 (i , 13 , d , e , f , g , h , a , b , c , X , & s0 , & s1 , & T1 );
290
+ round_16_80 (i , 14 , c , d , e , f , g , h , a , b , X , & s0 , & s1 , & T1 );
291
+ round_16_80 (i , 15 , b , c , d , e , f , g , h , a , X , & s0 , & s1 , & T1 );
292
+ }
293
+ }
294
+
295
+ static void processBlocks (uint64_t * state , const uint8_t * in , size_t num ) {
296
+ uint64_t a , b , c , d , e , f , g , h ;
297
+
298
+ while (num -- ) {
299
+
300
+ a = state [0 ];
301
+ b = state [1 ];
302
+ c = state [2 ];
303
+ d = state [3 ];
304
+ e = state [4 ];
305
+ f = state [5 ];
306
+ g = state [6 ];
307
+ h = state [7 ];
308
+
309
+ processBlock (& a , & b , & c , & d , & e , & f , & g , & h , in );
310
+
311
+ state [0 ] += a ;
312
+ state [1 ] += b ;
313
+ state [2 ] += c ;
314
+ state [3 ] += d ;
315
+ state [4 ] += e ;
316
+ state [5 ] += f ;
317
+ state [6 ] += g ;
318
+ state [7 ] += h ;
319
+
320
+ in += 16 * 8 ;
321
+ }
322
+ }
323
+
324
+
325
+ // Needed for Heapster to be able to see the static functions above
188
326
void dummy (uint64_t * state , const uint8_t * in , size_t num ) {
189
327
sha512_block_data_order (state , in , num );
328
+ processBlocks (state , in , num );
190
329
}
0 commit comments