Debian Codegraph @

components/kascrypt/Hashes/dcblake2.pp

raw
1 {
2 BLAKE2 reference source code package - reference C implementations
3
4 Written in 2012 by Samuel Neves <[email protected]>
5
6 Pascal tranlastion in 2014-2020 by Alexander Koblov ([email protected])
7
8 To the extent possible under law, the author(s) have dedicated all copyright
9 and related and neighboring rights to this software to the public domain
10 worldwide. This software is distributed without any warranty.
11
12 You should have received a copy of the CC0 Public Domain Dedication along with
13 this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
14 }
15
16 unit DCblake2;
17
18 {$mode objfpc}{$H+}
19 {$macro on}{$R-}{$Q-}
20 {$define USE_MTPROCS}
21
22 interface
23
24 uses
25 SysUtils, CTypes;
26
27 const
28 BLAKE2S_BLOCKBYTES = 64;
29 BLAKE2S_OUTBYTES = 32;
30 BLAKE2S_KEYBYTES = 32;
31 BLAKE2S_SALTBYTES = 8;
32 BLAKE2S_PERSONALBYTES = 8;
33 BLAKE2S_PARALLELISM_DEGREE = 8;
34
35 const
36 BLAKE2B_BLOCKBYTES = 128;
37 BLAKE2B_OUTBYTES = 64;
38 BLAKE2B_KEYBYTES = 64;
39 BLAKE2B_SALTBYTES = 16;
40 BLAKE2B_PERSONALBYTES = 16;
41 BLAKE2B_PARALLELISM_DEGREE = 4;
42
43 type
44 {$packrecords 1}
45 Pblake2s_param = ^blake2s_param;
46 blake2s_param = record
47 digest_length: cuint8; // 1
48 key_length: cuint8; // 2
49 fanout: cuint8; // 3
50 depth: cuint8; // 4
51 leaf_length: cuint32; // 8
52 node_offset: array[0..5] of cuint8;// 14
53 node_depth: cuint8; // 15
54 inner_length: cuint8; // 16
55 // uint8_t reserved[0];
56 salt: array[0..Pred(BLAKE2S_SALTBYTES)] of cuint8; // 24
57 personal: array[0..Pred(BLAKE2S_PERSONALBYTES)] of cuint8; // 32
58 end;
59 {$packrecords 8}
60 Pblake2s_state = ^blake2s_state;
61 blake2s_state = record
62 h: array[0..7] of cuint32;
63 t: array[0..1] of cuint32;
64 f: array[0..1] of cuint32;
65 buf: array[0..Pred(2 * BLAKE2S_BLOCKBYTES)] of cuint8;
66 buflen: csize_t;
67 last_node: cuint8;
68 end;
69 {$packrecords 1}
70 Pblake2sp_state = ^blake2sp_state;
71 blake2sp_state = record
72 S: array[0..7] of blake2s_state;
73 R: blake2s_state;
74 buf: array[0..Pred(8 * BLAKE2S_BLOCKBYTES)] of cuint8;
75 buflen: csize_t;
76 inlen: csize_t;
77 inp: PByte;
78 end;
79 {$packrecords c}
80 Pblake2b_state = ^blake2b_state;
81 blake2b_state = record
82 h: array[0..7] of cuint64;
83 t: array[0..1] of cuint64;
84 f: array[0..1] of cuint64;
85 buf: array [0..Pred(BLAKE2B_BLOCKBYTES)] of cuint8;
86 buflen: csize_t;
87 outlen: csize_t;
88 last_node: cuint8;
89 end;
90 {$packrecords 1}
91 Pblake2b_param = ^blake2b_param;
92 blake2b_param = record
93 digest_length: uint8; // 1
94 key_length: cuint8; // 2
95 fanout: cuint8; // 3
96 depth: cuint8; // 4
97 leaf_length: cuint32; // 8
98 node_offset: cuint32; // 12
99 xof_length: cuint32; // 16
100 node_depth: cuint8; // 17
101 inner_length: cuint8; // 18
102 reserved: array[0..13] of cuint8; // 32
103 salt: array [0..Pred(BLAKE2B_SALTBYTES)] of cuint8; // 48
104 personal: array[0..Pred(BLAKE2B_PERSONALBYTES)] of cuint8; // 64
105 end;
106 {$packrecords default}
107 Pblake2bp_state = ^blake2bp_state;
108 blake2bp_state = record
109 S: array[0..3] of blake2b_state;
110 R: blake2b_state;
111 buf: array[0..Pred(4 * BLAKE2B_BLOCKBYTES)] of cuint8;
112 buflen: csize_t;
113 outlen: csize_t;
114 inlen: csize_t;
115 inp: PByte;
116 end;
117
118 function blake2s_init( S: Pblake2s_state; const outlen: cuint8 ): cint;
119 function blake2s_update( S: Pblake2s_state; inp: pcuint8; inlen: cuint64 ): cint;
120 function blake2s_final( S: Pblake2s_state; outp: pcuint8; outlen: cuint8 ): cint;
121
122 function blake2sp_init( S: Pblake2sp_state; const outlen: cuint8 ): cint;
123 function blake2sp_update( S: Pblake2sp_state; inp: pcuint8; inlen: cuint64 ): cint;
124 function blake2sp_final( S: Pblake2sp_state; outp: pcuint8; const outlen: cuint8 ): cint;
125
126 function blake2b_init( S: Pblake2b_state; outlen: csize_t ): cint;
127 function blake2b_update( S: Pblake2b_state; pin: pcuint8; inlen: csize_t ): cint;
128 function blake2b_final( S: Pblake2b_state; pout: pcuint8; outlen: csize_t ): cint;
129
130 function blake2bp_init( S: Pblake2bp_state; outlen: csize_t ): cint;
131 function blake2bp_update( S: Pblake2bp_state; inp: pcuint8; inlen: csize_t ): cint;
132 function blake2bp_final( S: Pblake2bp_state; out_: PByte; outlen: csize_t ): cint;
133
134 implementation
135
136 {$IF DEFINED(USE_MTPROCS)}
137 uses
138 MTProcs
139 {$IF DEFINED(CPUX86_64)}
140 , CPU
141 {$ENDIF}
142 ;
143 {$ELSE}
144 {$IF DEFINED(CPUX86_64)}
145 uses
146 CPU;
147 {$ENDIF}
148 type
149 TMultiThreadProcItem = Pointer;
150 {$ENDIF}
151
152 const blake2s_IV: array[0..7] of cuint32 =
153 (
154 $6A09E667, $BB67AE85, $3C6EF372, $A54FF53A,
155 $510E527F, $9B05688C, $1F83D9AB, $5BE0CD19
156 );
157
158 const blake2b_IV: array[0..7] of cint64 =
159 (
160 $6a09e667f3bcc908, $bb67ae8584caa73b,
161 $3c6ef372fe94f82b, $a54ff53a5f1d36f1,
162 $510e527fade682d1, $9b05688c2b3e6c1f,
163 $1f83d9abfb41bd6b, $5be0cd19137e2179
164 );
165
166 function load32( const src: Pointer ): cuint32; inline;
167 begin
168 Result := NtoLE(pcuint32(src)^);
169 end;
170
171 function load64( const src: pointer ): cuint64; inline;
172 begin
173 Result := NtoLE(pcuint64(src)^);
174 end;
175
176 procedure store32( dst: pointer; w: cuint32 ); inline;
177 begin
178 pcuint32(dst)^ := LEtoN(w);
179 end;
180
181 procedure store64( dst: pointer; w: cuint64 ); inline;
182 begin
183 pcuint64(dst)^ := LEtoN(w);
184 end;
185
186 function load48( const src: pointer ): cuint64; inline;
187 var
188 w: cuint64;
189 p: pcuint8;
190 begin
191 p := pcuint8(src);
192 w := p^; Inc(p);
193 w := w or cuint64( p^ ) shl 8; inc(p);
194 w := w or cuint64( p^ ) shl 16; inc(p);
195 w := w or cuint64( p^ ) shl 24; inc(p);
196 w := w or cuint64( p^ ) shl 32; inc(p);
197 w := w or cuint64( p^ ) shl 40; inc(p);
198 Result := w;
199 end;
200
201 procedure store48( dst: pointer; w: cuint64 ); inline;
202 var
203 p: pcuint8;
204 begin
205 p := pcuint8(dst);
206 p^ := cuint8(w); w := w shr 8; inc(p);
207 p^ := cuint8(w); w := w shr 8; inc(p);
208 p^ := cuint8(w); w := w shr 8; inc(p);
209 p^ := cuint8(w); w := w shr 8; inc(p);
210 p^ := cuint8(w); w := w shr 8; inc(p);
211 p^ := cuint8(w); inc(p);
212 end;
213
214 var
215 blake2s_compress: function(S: Pblake2s_state; const block: pcuint8): cint;
216 blake2b_compress: procedure(S: Pblake2b_state; const block: pcuint8);
217
218 {$IF DEFINED(CPUX86_64)}
219 {$include blake2_sse.inc}
220 {$include blake2_avx.inc}
221 {$ELSE}
222 {$include blake2_pas.inc}
223 {$ENDIF}
224
225 function blake2s_set_lastnode( S: Pblake2s_state ): cint; inline;
226 begin
227 S^.f[1] := $FFFFFFFF;
228 Result := 0;
229 end;
230
231 function blake2s_clear_lastnode( S: Pblake2s_state ): cint; inline;
232 begin
233 S^.f[1] := 0;
234 Result := 0;
235 end;
236
237 //* Some helper functions, not necessarily useful */
238 function blake2s_set_lastblock( S: Pblake2s_state ): cint; inline;
239 begin
240 if( S^.last_node <> 0 ) then blake2s_set_lastnode( S );
241
242 S^.f[0] := $FFFFFFFF;
243 Result := 0;
244 end;
245
246 function blake2s_clear_lastblock( S: Pblake2s_state ): cint; inline;
247 begin
248 if( S^.last_node <> 0 ) then blake2s_clear_lastnode( S );
249
250 S^.f[0] := 0;
251 Result := 0;
252 end;
253
254 function blake2s_increment_counter( S: Pblake2s_state; const inc: cuint32 ): cint; inline;
255 begin
256 S^.t[0] += inc;
257 S^.t[1] += cuint32( S^.t[0] < inc );
258 Result := 0;
259 end;
260
261 function blake2s_init0( S: Pblake2s_state ): cint; inline;
262 var
263 i: cint;
264 begin
265 FillChar( S^, sizeof( blake2s_state ), 0 );
266
267 for i := 0 to 8 - 1 do S^.h[i] := blake2s_IV[i];
268
269 Result := 0;
270 end;
271
272 //* init2 xors IV with input parameter block */
273 function blake2s_init_param( S: Pblake2s_state; const P: Pblake2s_param ): cint;
274 var
275 i: csize_t;
276 pp: pcuint32;
277 begin
278 blake2s_init0( S );
279 pp := pcuint32( P );
280
281 //* IV XOR ParamBlock */
282 // for i := 0; i < 8; ++i )
283 for i := 0 to 8 - 1 do
284 S^.h[i] := S^.h[i] xor load32( @pp[i] );
285
286 Result := 0;
287 end;
288
289 // Sequential blake2s initialization
290 function blake2s_init( S: Pblake2s_state; const outlen: cuint8 ): cint;
291 var
292 P: blake2s_param;
293 begin
294 //* Move interval verification here? */
295 if ( ( outlen = 0 ) or ( outlen > BLAKE2S_OUTBYTES ) ) then Exit(-1);
296
297 P.digest_length := outlen;
298 P.key_length := 0;
299 P.fanout := 1;
300 P.depth := 1;
301 store32( @P.leaf_length, 0 );
302 store48( @P.node_offset, 0 );
303 P.node_depth := 0;
304 P.inner_length := 0;
305 // memset(P^.reserved, 0, sizeof(P^.reserved) );
306 FillChar( P.salt, sizeof( P.salt ), 0 );
307 FillChar( P.personal, sizeof( P.personal ), 0 );
308 Result := blake2s_init_param( S, @P );
309 end;
310
311 function blake2s_update( S: Pblake2s_state; inp: pcuint8; inlen: cuint64 ): cint;
312 var
313 left, fill: csize_t;
314 begin
315 while( inlen > 0 ) do
316 begin
317 left := S^.buflen;
318 fill := 2 * BLAKE2S_BLOCKBYTES - left;
319
320 if( inlen > fill ) then
321 begin
322 Move( inp^, S^.buf[left], fill ); // Fill buffer
323 S^.buflen += fill;
324 blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
325 blake2s_compress( S, S^.buf ); // Compress
326 Move( S^.buf[BLAKE2S_BLOCKBYTES], S^.buf, BLAKE2S_BLOCKBYTES ); // Shift buffer left
327 S^.buflen -= BLAKE2S_BLOCKBYTES;
328 inp += fill;
329 inlen -= fill;
330 end
331 else // inlen <= fill
332 begin
333 Move( inp^, S^.buf [left], inlen );
334 S^.buflen += inlen; // Be lazy, do not compress
335 inp += inlen;
336 inlen -= inlen;
337 end;
338 end;
339
340 Result := 0;
341 end;
342
343 function blake2s_final( S: Pblake2s_state; outp: pcuint8; outlen: cuint8 ): cint;
344 var
345 i: cint;
346 buffer: array[0..Pred(BLAKE2S_OUTBYTES)] of cuint8;
347 begin
348 if( S^.buflen > BLAKE2S_BLOCKBYTES ) then
349 begin
350 blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
351 blake2s_compress( S, S^.buf);
352 S^.buflen -= BLAKE2S_BLOCKBYTES;
353 Move( S^.buf[BLAKE2S_BLOCKBYTES], S^.buf, S^.buflen );
354 end;
355
356 blake2s_increment_counter( S, cuint32(S^.buflen) );
357 blake2s_set_lastblock( S );
358 FillChar( S^.buf[S^.buflen], 2 * BLAKE2S_BLOCKBYTES - S^.buflen, 0 ); //* Padding */
359 blake2s_compress( S, S^.buf );
360
361 for i := 0 to 7 do //* Output full hash to temp buffer */
362 store32( @buffer[sizeof( S^.h[i] ) * i], S^.h[i] );
363
364 Move( buffer, outp^, outlen );
365 Result := 0;
366 end;
367
368 function blake2sp_init_leaf(S: Pblake2s_state; outlen: cuint8; keylen: cuint8; offset: cuint64):cint; inline;
369 var
370 P: blake2s_param;
371 begin
372 P.digest_length := outlen;
373 P.key_length := keylen;
374 P.fanout := BLAKE2S_PARALLELISM_DEGREE;
375 P.depth := 2;
376 store32( @P.leaf_length, 0 );
377 store48( @P.node_offset[0], offset );
378 P.node_depth := 0;
379 P.inner_length := BLAKE2S_OUTBYTES;
380 FillChar( P.salt, sizeof( P.salt ), 0 );
381 FillChar( P.personal, sizeof( P.personal ), 0 );
382 Result:= blake2s_init_param( S, @P );
383 end;
384
385 function blake2sp_init_root( S: Pblake2s_state; outlen: cuint8; keylen: cuint8 ): cint; inline;
386 var
387 P: blake2s_param;
388 begin
389 P.digest_length := outlen;
390 P.key_length := keylen;
391 P.fanout := BLAKE2S_PARALLELISM_DEGREE;
392 P.depth := 2;
393 store32( @P.leaf_length, 0 );
394 store48( @P.node_offset[0], 0 );
395 P.node_depth := 1;
396 P.inner_length := BLAKE2S_OUTBYTES;
397 FillChar( P.salt, sizeof( P.salt ), 0 );
398 FillChar( P.personal, sizeof( P.personal ), 0 );
399 Result:= blake2s_init_param( S, @P );
400 end;
401
402 function blake2sp_init( S: Pblake2sp_state; const outlen: cuint8 ): cint;
403 var
404 i: csize_t;
405 begin
406 if (outlen = 0) or (outlen > BLAKE2S_OUTBYTES) then Exit(-1);
407
408 FillChar( S^.buf, sizeof( S^.buf ), 0 );
409 S^.buflen := 0;
410
411 if( blake2sp_init_root( @S^.R, outlen, 0 ) < 0 ) then Exit(-1);
412
413 for i := 0 to BLAKE2S_PARALLELISM_DEGREE - 1 do
414 if ( blake2sp_init_leaf( @S^.S[i], outlen, 0, i ) < 0 ) then Exit(-1);
415
416 S^.R.last_node := 1;
417 S^.S[BLAKE2S_PARALLELISM_DEGREE - 1].last_node := 1;
418 Result := 0;
419 end;
420
421 procedure MTProcedure(id__: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
422 var
423 in__: pcuint8;
424 inlen__: cuint64;
425 S: Pblake2sp_state absolute Data;
426 begin
427 in__ := S^.inp;
428 inlen__ := S^.inlen;
429 in__ += id__ * BLAKE2S_BLOCKBYTES;
430
431 while ( inlen__ >= BLAKE2S_PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES ) do
432 begin
433 blake2s_update( @S^.S[id__], in__, BLAKE2S_BLOCKBYTES );
434 in__ += BLAKE2S_PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
435 inlen__ -= BLAKE2S_PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
436 end;
437 end;
438
439 function blake2sp_update( S: Pblake2sp_state; inp: pcuint8; inlen: cuint64 ): cint;
440 var
441 i, left, fill: csize_t;
442 begin
443 left := S^.buflen;
444 fill := sizeof( S^.buf ) - left;
445
446 if ( left <> 0) and (inlen >= fill ) then
447 begin
448 Move(inp^, S^.buf[left], fill);
449
450 for i := 0 to BLAKE2S_PARALLELISM_DEGREE - 1 do
451 blake2s_update( @S^.S[i], @S^.buf[ i * BLAKE2S_BLOCKBYTES], BLAKE2S_BLOCKBYTES );
452
453 inp += fill;
454 inlen -= fill;
455 left := 0;
456 end;
457
458 S^.inp := inp;
459 S^.inlen := inlen;
460
461 {$IF DEFINED(USE_MTPROCS)}
462 ProcThreadPool.DoParallel(@MTProcedure, 0, BLAKE2S_PARALLELISM_DEGREE - 1, S);
463 {$ELSE}
464 for i := 0 to BLAKE2S_PARALLELISM_DEGREE - 1 do MTProcedure(i, S, nil);
465 {$ENDIF}
466
467 inp += inlen - inlen mod ( BLAKE2S_PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES );
468 inlen := inlen mod (BLAKE2S_PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES);
469
470 if ( inlen > 0 ) then
471 Move(inp^, S^.buf[left], inlen );
472
473 S^.buflen := left + inlen;
474 Result := 0;
475 end;
476
477 function blake2sp_final( S: Pblake2sp_state; outp: pcuint8; const outlen: cuint8 ): cint;
478 var
479 i, left: csize_t;
480 hash: array[0..Pred(BLAKE2S_PARALLELISM_DEGREE), 0..Pred(BLAKE2S_OUTBYTES)] of cuint8;
481 begin
482
483 for i := 0 to BLAKE2S_PARALLELISM_DEGREE - 1 do
484 begin
485 if ( S^.buflen > i * BLAKE2S_BLOCKBYTES ) then
486 begin
487 left := S^.buflen - i * BLAKE2S_BLOCKBYTES;
488
489 if ( left > BLAKE2S_BLOCKBYTES ) then left := BLAKE2S_BLOCKBYTES;
490
491 blake2s_update( @S^.S[i], @S^.buf[i * BLAKE2S_BLOCKBYTES], left );
492 end;
493
494 blake2s_final( @S^.S[i], hash[i], BLAKE2S_OUTBYTES );
495 end;
496
497 for i := 0 to BLAKE2S_PARALLELISM_DEGREE - 1 do
498 blake2s_update( @S^.R, hash[i], BLAKE2S_OUTBYTES );
499
500 blake2s_final( @S^.R, outp, outlen );
501 Result := 0;
502 end;
503
504 procedure blake2b_set_lastnode( S: Pblake2b_state ); inline;
505 begin
506 S^.f[1] := cuint64(-1);
507 end;
508
509 //* Some helper functions, not necessarily useful */
510 function blake2b_is_lastblock( S: Pblake2b_state ): cint; inline;
511 begin
512 Result := cint(S^.f[0] <> 0);
513 end;
514
515 procedure blake2b_set_lastblock( S: Pblake2b_state );
516 begin
517 if( S^.last_node <> 0 ) then blake2b_set_lastnode( S );
518
519 S^.f[0] := cuint64(-1);
520 end;
521
522 procedure blake2b_increment_counter( S: Pblake2b_state; const inc: cuint64 );
523 begin
524 S^.t[0] += inc;
525 S^.t[1] += cuint64( S^.t[0] < inc );
526 end;
527
528 procedure blake2b_init0( S: Pblake2b_state );
529 var
530 i: csize_t;
531 begin
532 fillchar( S^, sizeof( blake2b_state ), 0 );
533
534 for i := 0 to 7 do S^.h[i] := cuint64(blake2b_IV[i]);
535 end;
536
537 //* init xors IV with input parameter block */
538 function blake2b_init_param( S: Pblake2b_state; const P: Pblake2b_param ): cint;
539 var
540 i: csize_t;
541 pp: pcuint8;
542 begin
543 pp := pcuint8( P );
544
545 blake2b_init0( S );
546
547 //* IV XOR ParamBlock */
548 for i := 0 to 7 do
549 S^.h[i] := S^.h[i] xor load64( pp + sizeof( S^.h[i] ) * i );
550
551 S^.outlen := P^.digest_length;
552 Result := 0;
553 end;
554
555 function blake2b_init( S: Pblake2b_state; outlen: csize_t ): cint;
556 var
557 P: blake2b_param;
558 begin
559 if ( ( outlen = 0 ) or ( outlen > BLAKE2B_OUTBYTES ) ) then Exit(-1);
560
561 P.digest_length := cuint8(outlen);
562 P.key_length := 0;
563 P.fanout := 1;
564 P.depth := 1;
565 store32( @P.leaf_length, 0 );
566 store32( @P.node_offset, 0 );
567 store32( @P.xof_length, 0 );
568 P.node_depth := 0;
569 P.inner_length := 0;
570 fillchar( P.reserved, sizeof( P.reserved ), 0 );
571 fillchar( P.salt, sizeof( P.salt ), 0 );
572 fillchar( P.personal, sizeof( P.personal ), 0 );
573 Result := blake2b_init_param( S, @P );
574 end;
575
576 function blake2b_update( S: Pblake2b_state; pin: pcuint8; inlen: csize_t ): cint;
577 var
578 left, fill: csize_t;
579 begin
580 if ( inlen > 0 ) then
581 begin
582 left := S^.buflen;
583 fill := BLAKE2B_BLOCKBYTES - left;
584 if ( inlen > fill ) then
585 begin
586 S^.buflen := 0;
587 Move( pin^, S^.buf[left], fill ); //* Fill buffer */
588 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
589 blake2b_compress( S, S^.buf ); //* Compress */
590 pin += fill; inlen -= fill;
591 while (inlen > BLAKE2B_BLOCKBYTES) do
592 begin
593 blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
594 blake2b_compress( S, pin );
595 pin += BLAKE2B_BLOCKBYTES;
596 inlen -= BLAKE2B_BLOCKBYTES;
597 end
598 end;
599 Move( pin^, S^.buf[S^.buflen], inlen );
600 S^.buflen += inlen;
601 end;
602 Result := 0;
603 end;
604
605 function blake2b_final( S: Pblake2b_state; pout: pcuint8; outlen: csize_t ): cint;
606 var
607 i: csize_t;
608 buffer: array[0..Pred(BLAKE2B_OUTBYTES)] of cuint8;
609 begin
610 if( pout = nil) or (outlen < S^.outlen ) then
611 Exit(-1);
612
613 if ( blake2b_is_lastblock( S ) <> 0 ) then
614 Exit(-1);
615
616 fillchar(buffer[0], BLAKE2B_OUTBYTES, 0);
617 blake2b_increment_counter( S, S^.buflen );
618 blake2b_set_lastblock( S );
619 fillchar( S^.buf[S^.buflen], BLAKE2B_BLOCKBYTES - S^.buflen, 0 ); //* Padding */
620 blake2b_compress( S, S^.buf );
621
622 for i := 0 to 7 do //* Output full hash to temp buffer */
623 store64( @buffer[sizeof( S^.h[i] ) * i], S^.h[i] );
624
625 move( buffer[0], pout^, S^.outlen );
626 fillchar(buffer[0], sizeof(buffer), 0);
627 Result := 0;
628 end;
629
630 function blake2bp_init_leaf_param( S: Pblake2b_state; const P: Pblake2b_param ): cint;
631 begin
632 Result:= blake2b_init_param(S, P);
633 S^.outlen := P^.inner_length;
634 end;
635
636 function blake2bp_init_leaf( S: Pblake2b_state; outlen, keylen: csize_t; offset: cuint64 ): cint;
637 var
638 P: blake2b_param;
639 begin
640 P.digest_length := cuint8(outlen);
641 P.key_length := cuint8(keylen);
642 P.fanout := BLAKE2B_PARALLELISM_DEGREE;
643 P.depth := 2;
644 store32( @P.leaf_length, 0 );
645 store32( @P.node_offset, offset );
646 store32( @P.xof_length, 0 );
647 P.node_depth := 0;
648 P.inner_length := BLAKE2B_OUTBYTES;
649 FillChar( P.reserved[0], sizeof( P.reserved ), 0 );
650 FillChar( P.salt[0], sizeof( P.salt ), 0 );
651 FillChar( P.personal[0], sizeof( P.personal ), 0 );
652 Result:= blake2bp_init_leaf_param( S, @P );
653 end;
654
655 function blake2bp_init_root( S: Pblake2b_state; outlen, keylen: csize_t ): cint;
656 var
657 P: blake2b_param;
658 begin
659 P.digest_length := cuint8(outlen);
660 P.key_length := cuint8(keylen);
661 P.fanout := BLAKE2B_PARALLELISM_DEGREE;
662 P.depth := 2;
663 store32( @P.leaf_length, 0 );
664 store32( @P.node_offset, 0 );
665 store32( @P.xof_length, 0 );
666 P.node_depth := 1;
667 P.inner_length := BLAKE2B_OUTBYTES;
668 FillChar( P.reserved[0], sizeof( P.reserved ), 0 );
669 FillChar( P.salt[0], sizeof( P.salt ), 0 );
670 FillChar( P.personal[0], sizeof( P.personal ), 0 );
671 Result:= blake2b_init_param( S, @P );
672 end;
673
674 function blake2bp_init( S: Pblake2bp_state; outlen: csize_t ): cint;
675 var
676 i: csize_t;
677 begin
678 if (outlen = 0) or (outlen > BLAKE2B_OUTBYTES) then Exit(-1);
679
680 FillChar( S^.buf[0], sizeof( S^.buf ), 0 );
681 S^.buflen := 0;
682 S^.outlen := outlen;
683
684 if( blake2bp_init_root( @S^.R, outlen, 0 ) < 0 ) then
685 Exit(-1);
686
687 for i := 0 to BLAKE2B_PARALLELISM_DEGREE - 1 do
688 if ( blake2bp_init_leaf( @S^.S[i], outlen, 0, i ) < 0 ) then Exit(-1);
689
690 S^.R.last_node := 1;
691 S^.S[BLAKE2B_PARALLELISM_DEGREE - 1].last_node := 1;
692 Result:= 0;
693 end;
694
695 procedure blake2bp_MTProcedure(i: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
696 var
697 in__: pcuint8;
698 inlen__: cuint64;
699 S: Pblake2bp_state absolute Data;
700 begin
701 in__ := S^.inp;
702 inlen__ := S^.inlen;
703 in__ += i * BLAKE2B_BLOCKBYTES;
704
705 while ( inlen__ >= BLAKE2B_PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES ) do
706 begin
707 blake2b_update( @S^.S[i], in__, BLAKE2B_BLOCKBYTES );
708 in__ += BLAKE2B_PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES;
709 inlen__ -= BLAKE2B_PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES;
710 end;
711 end;
712
713 function blake2bp_update( S: Pblake2bp_state; inp: pcuint8; inlen: csize_t ): cint;
714 var
715 left, fill, i: csize_t;
716 begin
717 left := S^.buflen;
718 fill := sizeof( S^.buf ) - left;
719
720 if( left > 0) and (inlen >= fill ) then
721 begin
722 Move( inp^, S^.buf[left], fill );
723
724 for i := 0 to BLAKE2B_PARALLELISM_DEGREE - 1 do
725 blake2b_update( @S^.S[i], @S^.buf[i * BLAKE2B_BLOCKBYTES], BLAKE2B_BLOCKBYTES );
726
727 inp += fill;
728 inlen -= fill;
729 left := 0;
730 end;
731
732 S^.inp := inp;
733 S^.inlen := inlen;
734
735 {$IF DEFINED(USE_MTPROCS)}
736 ProcThreadPool.DoParallel(@blake2bp_MTProcedure, 0, BLAKE2B_PARALLELISM_DEGREE - 1, S);
737 {$ELSE}
738 for i := 0 to BLAKE2B_PARALLELISM_DEGREE - 1 do blake2bp_MTProcedure(i, S, nil);
739 {$ENDIF}
740
741 inp += inlen - inlen mod ( BLAKE2B_PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES );
742 inlen := inlen mod (BLAKE2B_PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES);
743
744 if ( inlen > 0 ) then
745 Move( inp^, S^.buf[left], inlen );
746
747 S^.buflen := left + inlen;
748 Result:= 0;
749 end;
750
751 function blake2bp_final( S: Pblake2bp_state; out_: PByte; outlen: csize_t ): cint;
752 var
753 i, left: csize_t;
754 hash: array[0..Pred(BLAKE2B_PARALLELISM_DEGREE), 0..Pred(BLAKE2B_OUTBYTES)] of cuint8;
755 begin
756 if (out_ = nil) or (outlen < S^.outlen) then Exit(-1);
757
758 for i := 0 to BLAKE2B_PARALLELISM_DEGREE - 1 do
759 begin
760 if ( S^.buflen > i * BLAKE2B_BLOCKBYTES ) then
761 begin
762 left := S^.buflen - i * BLAKE2B_BLOCKBYTES;
763
764 if ( left > BLAKE2B_BLOCKBYTES ) then left := BLAKE2B_BLOCKBYTES;
765
766 blake2b_update( @S^.S[i], @S^.buf[i * BLAKE2B_BLOCKBYTES], left );
767 end;
768
769 blake2b_final( @S^.S[i], hash[i], BLAKE2B_OUTBYTES );
770 end;
771
772 for i := 0 to BLAKE2B_PARALLELISM_DEGREE -1 do
773 blake2b_update( @S^.R, hash[i], BLAKE2B_OUTBYTES );
774
775 Result:= blake2b_final( @S^.R, out_, S^.outlen );
776 end;
777
778 initialization
779 {$IF DEFINED(CPUX86_64)}
780 if AVXSupport then
781 begin
782 blake2s_compress:= @blake2s_compress_avx;
783 blake2b_compress:= @blake2b_compress_avx;
784 end
785 else begin
786 blake2s_compress:= @blake2s_compress_sse;
787 blake2b_compress:= @blake2b_compress_sse;
788 end;
789 {$ELSE}
790 blake2s_compress:= @blake2s_compress_pas;
791 blake2b_compress:= @blake2b_compress_pas;
792 {$ENDIF}
793 end.