Debian Codegraph @

components/kascrypt/Hashes/dcpblake2.pas

raw
1 {******************************************************************************}
2 {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
3 {******************************************************************************}
4 {* A binary compatible implementation of BLAKE2S, BLAKE2SP, BLAKE2B, BLAKE2BP *}
5 {******************************************************************************}
6 {* Copyright (C) 2014-2018 Alexander Koblov ([email protected]) *}
7 {* Permission is hereby granted, free of charge, to any person obtaining a *}
8 {* copy of this software and associated documentation files (the "Software"), *}
9 {* to deal in the Software without restriction, including without limitation *}
10 {* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
11 {* and/or sell copies of the Software, and to permit persons to whom the *}
12 {* Software is furnished to do so, subject to the following conditions: *}
13 {* *}
14 {* The above copyright notice and this permission notice shall be included in *}
15 {* all copies or substantial portions of the Software. *}
16 {* *}
17 {* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
18 {* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
19 {* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
20 {* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
21 {* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
22 {* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
23 {* DEALINGS IN THE SOFTWARE. *}
24 {******************************************************************************}
25 unit DCPblake2;
26
27 {$mode delphi}
28
29 interface
30
31 uses
32 Classes, SysUtils, CTypes, DCPcrypt2, DCPconst, DCblake2, Hash;
33
34 type
35
36 { TDCP_blake2s }
37
38 TDCP_blake2s = class(TDCP_hash)
39 protected
40 S: blake2s_state;
41 public
42 class function GetId: integer; override;
43 class function GetAlgorithm: string; override;
44 class function GetHashSize: integer; override;
45 class function SelfTest: boolean; override;
46 procedure Init; override;
47 procedure Burn; override;
48 procedure Update(const Buffer; Size: longword); override;
49 procedure Final(var Digest); override;
50 end;
51
52 { TDCP_blake2sp }
53
54 TDCP_blake2sp = class(TDCP_hash)
55 protected
56 S: blake2sp_state;
57 public
58 class function GetId: integer; override;
59 class function GetAlgorithm: string; override;
60 class function GetHashSize: integer; override;
61 class function SelfTest: boolean; override;
62 procedure Init; override;
63 procedure Burn; override;
64 procedure Update(const Buffer; Size: longword); override;
65 procedure Final(var Digest); override;
66 end;
67
68 { TDCP_blake2b }
69
70 TDCP_blake2b = class(TDCP_hash)
71 protected
72 S: blake2b_state;
73 public
74 class function GetId: integer; override;
75 class function GetAlgorithm: string; override;
76 class function GetHashSize: integer; override;
77 class function SelfTest: boolean; override;
78 procedure Init; override;
79 procedure Burn; override;
80 procedure Update(const Buffer; Size: longword); override;
81 procedure Final(var Digest); override;
82 end;
83
84 { TDCP_blake2bp }
85
86 TDCP_blake2bp = class(TDCP_hash)
87 protected
88 S: blake2bp_state;
89 public
90 class function GetId: integer; override;
91 class function GetAlgorithm: string; override;
92 class function GetHashSize: integer; override;
93 class function SelfTest: boolean; override;
94 procedure Init; override;
95 procedure Burn; override;
96 procedure Update(const Buffer; Size: longword); override;
97 procedure Final(var Digest); override;
98 end;
99
100 implementation
101
102 { TDCP_blake2s }
103
104 class function TDCP_blake2s.GetId: integer;
105 begin
106 Result:= DCP_blake2s;
107 end;
108
109 class function TDCP_blake2s.GetAlgorithm: string;
110 begin
111 Result:= 'BLAKE2S';
112 end;
113
114 class function TDCP_blake2s.GetHashSize: integer;
115 begin
116 Result:= 256;
117 end;
118
119 class function TDCP_blake2s.SelfTest: boolean;
120 const
121 Test1Out: array[0..31] of byte=
122 ($50, $8c, $5e, $8c, $32, $7c, $14, $e2, $e1, $a7, $2b, $a3, $4e, $eb, $45, $2f,
123 $37, $45, $8b, $20, $9e, $d6, $3a, $29, $4d, $99, $9b, $4c, $86, $67, $59, $82);
124 Test2Out: array[0..31] of byte=
125 ($6f, $4d, $f5, $11, $6a, $6f, $33, $2e, $da, $b1, $d9, $e1, $0e, $e8, $7d, $f6,
126 $55, $7b, $ea, $b6, $25, $9d, $76, $63, $f3, $bc, $d5, $72, $2c, $13, $f1, $89 );
127 var
128 TestHash: TDCP_blake2s;
129 TestOut: array[0..31] of byte;
130 begin
131 dcpFillChar(TestOut, SizeOf(TestOut), 0);
132 TestHash:= TDCP_blake2s.Create(nil);
133 TestHash.Init;
134 TestHash.UpdateStr('abc');
135 TestHash.Final(TestOut);
136 Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
137 TestHash.Init;
138 TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
139 TestHash.Final(TestOut);
140 Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
141 TestHash.Free;
142 end;
143
144 procedure TDCP_blake2s.Init;
145 begin
146 if blake2s_init( @S, BLAKE2S_OUTBYTES ) < 0 then
147 raise EDCP_hash.Create('blake2s_init');
148 fInitialized:= true;
149 end;
150
151 procedure TDCP_blake2s.Burn;
152 begin
153 fInitialized:= false;
154 end;
155
156 procedure TDCP_blake2s.Update(const Buffer; Size: longword);
157 begin
158 if blake2s_update(@S, @Buffer, Size) < 0 then
159 raise EDCP_hash.Create('blake2s_update');
160 end;
161
162 procedure TDCP_blake2s.Final(var Digest);
163 var
164 Hash: array[0..Pred(BLAKE2S_OUTBYTES)] of cuint8;
165 begin
166 if not fInitialized then
167 raise EDCP_hash.Create('Hash not initialized');
168 if blake2s_final(@S, Hash, SizeOf(Hash)) < 0 then
169 raise EDCP_hash.Create('blake2s_final');
170 Move(Hash, Digest, Sizeof(Hash));
171 Burn;
172 end;
173
174 { TDCP_blake2sp }
175
176 class function TDCP_blake2sp.GetId: integer;
177 begin
178 Result:= DCP_blake2sp;
179 end;
180
181 class function TDCP_blake2sp.GetAlgorithm: string;
182 begin
183 Result:= 'BLAKE2SP';
184 end;
185
186 class function TDCP_blake2sp.GetHashSize: integer;
187 begin
188 Result:= 256;
189 end;
190
191 class function TDCP_blake2sp.SelfTest: boolean;
192 const
193 Test1Out: array[0..31] of byte=
194 ($70, $f7, $5b, $58, $f1, $fe, $ca, $b8, $21, $db, $43, $c8, $8a, $d8, $4e, $dd,
195 $e5, $a5, $26, $00, $61, $6c, $d2, $25, $17, $b7, $bb, $14, $d4, $40, $a7, $d5);
196 Test2Out: array[0..31] of byte=
197 ($3d, $10, $7e, $42, $f1, $7c, $13, $c8, $2b, $43, $6e, $bb, $65, $1a, $48, $de,
198 $f6, $7e, $77, $72, $fa, $06, $f4, $73, $8e, $e9, $68, $c7, $f4, $d8, $b4, $8b);
199 var
200 TestHash: TDCP_blake2sp;
201 TestOut: array[0..31] of byte;
202 begin
203 dcpFillChar(TestOut, SizeOf(TestOut), 0);
204 TestHash:= TDCP_blake2sp.Create(nil);
205 TestHash.Init;
206 TestHash.UpdateStr('abc');
207 TestHash.Final(TestOut);
208 Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
209 TestHash.Init;
210 TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
211 TestHash.Final(TestOut);
212 Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
213 TestHash.Free;
214 end;
215
216 procedure TDCP_blake2sp.Init;
217 begin
218 if blake2sp_init( @S, BLAKE2S_OUTBYTES ) < 0 then
219 raise EDCP_hash.Create('blake2sp_init');
220 fInitialized:= true;
221 end;
222
223 procedure TDCP_blake2sp.Burn;
224 begin
225 fInitialized:= false;
226 end;
227
228 procedure TDCP_blake2sp.Update(const Buffer; Size: longword);
229 begin
230 if blake2sp_update(@S, @Buffer, Size) < 0 then
231 raise EDCP_hash.Create('blake2sp_update');
232 end;
233
234 procedure TDCP_blake2sp.Final(var Digest);
235 var
236 Hash: array[0..Pred(BLAKE2S_OUTBYTES)] of cuint8;
237 begin
238 if not fInitialized then
239 raise EDCP_hash.Create('Hash not initialized');
240 if blake2sp_final(@S, Hash, SizeOf(Hash)) < 0 then
241 raise EDCP_hash.Create('blake2sp_final');
242 Move(Hash, Digest, Sizeof(Hash));
243 Burn;
244 end;
245
246 { TDCP_blake2b }
247
248 class function TDCP_blake2b.GetId: integer;
249 begin
250 Result:= DCP_blake2b;
251 end;
252
253 class function TDCP_blake2b.GetAlgorithm: string;
254 begin
255 Result:= 'BLAKE2B';
256 end;
257
258 class function TDCP_blake2b.GetHashSize: integer;
259 begin
260 Result:= 512;
261 end;
262
263 class function TDCP_blake2b.SelfTest: boolean;
264 const
265 Test1Out: array[0..63] of byte =
266 ($ba, $80, $a5, $3f, $98, $1c, $4d, $0d, $6a, $27, $97, $b6, $9f, $12, $f6, $e9,
267 $4c, $21, $2f, $14, $68, $5a, $c4, $b7, $4b, $12, $bb, $6f, $db, $ff, $a2, $d1,
268 $7d, $87, $c5, $39, $2a, $ab, $79, $2d, $c2, $52, $d5, $de, $45, $33, $cc, $95,
269 $18, $d3, $8a, $a8, $db, $f1, $92, $5a, $b9, $23, $86, $ed, $d4, $00, $99, $23);
270 Test2Out: array[0..63] of byte =
271 ($72, $85, $ff, $3e, $8b, $d7, $68, $d6, $9b, $e6, $2b, $3b, $f1, $87, $65, $a3,
272 $25, $91, $7f, $a9, $74, $4a, $c2, $f5, $82, $a2, $08, $50, $bc, $2b, $11, $41,
273 $ed, $1b, $3e, $45, $28, $59, $5a, $cc, $90, $77, $2b, $df, $2d, $37, $dc, $8a,
274 $47, $13, $0b, $44, $f3, $3a, $02, $e8, $73, $0e, $5a, $d8, $e1, $66, $e8, $88);
275 var
276 TestHash: TDCP_blake2b;
277 TestOut: array[0..63] of byte;
278 begin
279 dcpFillChar(TestOut, SizeOf(TestOut), 0);
280 TestHash:= TDCP_blake2b.Create(nil);
281 TestHash.Init;
282 TestHash.UpdateStr('abc');
283 TestHash.Final(TestOut);
284 Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
285 TestHash.Init;
286 TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
287 TestHash.Final(TestOut);
288 Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
289 TestHash.Free;
290 end;
291
292 procedure TDCP_blake2b.Init;
293 begin
294 if blake2b_init( @S, BLAKE2B_OUTBYTES ) < 0 then
295 raise EDCP_hash.Create('blake2b_init');
296 fInitialized:= true;
297 end;
298
299 procedure TDCP_blake2b.Burn;
300 begin
301 fInitialized:= false;
302 end;
303
304 procedure TDCP_blake2b.Update(const Buffer; Size: longword);
305 begin
306 if blake2b_update(@S, @Buffer, Size) < 0 then
307 raise EDCP_hash.Create('blake2b_update');
308 end;
309
310 procedure TDCP_blake2b.Final(var Digest);
311 var
312 Hash: array[0..Pred(BLAKE2B_OUTBYTES)] of cuint8;
313 begin
314 if not fInitialized then
315 raise EDCP_hash.Create('Hash not initialized');
316 if blake2b_final(@S, Hash, SizeOf(Hash)) < 0 then
317 raise EDCP_hash.Create('blake2b_final');
318 Move(Hash, Digest, Sizeof(Hash));
319 Burn;
320 end;
321
322 { TDCP_blake2bp }
323
324 class function TDCP_blake2bp.GetId: integer;
325 begin
326 Result:= DCP_blake2bp;
327 end;
328
329 class function TDCP_blake2bp.GetAlgorithm: string;
330 begin
331 Result:= 'BLAKE2BP';
332 end;
333
334 class function TDCP_blake2bp.GetHashSize: integer;
335 begin
336 Result:= 512;
337 end;
338
339 class function TDCP_blake2bp.SelfTest: boolean;
340 const
341 Test1Out: array[0..63] of byte =
342 ($b9, $1a, $6b, $66, $ae, $87, $52, $6c, $40, $0b, $0a, $8b, $53, $77, $4d, $c6,
343 $52, $84, $ad, $8f, $65, $75, $f8, $14, $8f, $f9, $3d, $ff, $94, $3a, $6e, $cd,
344 $83, $62, $13, $0f, $22, $d6, $da, $e6, $33, $aa, $0f, $91, $df, $4a, $c8, $9a,
345 $af, $f3, $1d, $0f, $1b, $92, $3c, $89, $8e, $82, $02, $5d, $ed, $bd, $ad, $6e);
346 Test2Out: array[0..63] of byte =
347 ($c5, $a0, $34, $1e, $eb, $b6, $15, $50, $3e, $22, $93, $30, $e0, $6a, $3d, $ce,
348 $88, $05, $b4, $34, $ca, $75, $8e, $89, $9e, $72, $ac, $40, $ba, $c3, $6e, $63,
349 $7b, $70, $09, $8a, $24, $ae, $5c, $3c, $4d, $39, $a1, $83, $a4, $3e, $b9, $74,
350 $82, $3e, $3d, $db, $5b, $09, $e0, $7a, $d1, $e5, $26, $e9, $05, $f6, $5b, $c4);
351 var
352 TestHash: TDCP_blake2bp;
353 TestOut: array[0..63] of byte;
354 begin
355 dcpFillChar(TestOut, SizeOf(TestOut), 0);
356 TestHash:= TDCP_blake2bp.Create(nil);
357 TestHash.Init;
358 TestHash.UpdateStr('abc');
359 TestHash.Final(TestOut);
360 Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
361 TestHash.Init;
362 TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
363 TestHash.Final(TestOut);
364 Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
365 TestHash.Free;
366 end;
367
368 procedure TDCP_blake2bp.Init;
369 begin
370 if blake2bp_init( @S, BLAKE2B_OUTBYTES ) < 0 then
371 raise EDCP_hash.Create('blake2bp_init');
372 fInitialized:= true;
373 end;
374
375 procedure TDCP_blake2bp.Burn;
376 begin
377 fInitialized:= false;
378 end;
379
380 procedure TDCP_blake2bp.Update(const Buffer; Size: longword);
381 begin
382 if blake2bp_update(@S, @Buffer, Size) < 0 then
383 raise EDCP_hash.Create('blake2bp_update');
384 end;
385
386 procedure TDCP_blake2bp.Final(var Digest);
387 var
388 Hash: array[0..Pred(BLAKE2B_OUTBYTES)] of cuint8;
389 begin
390 if not fInitialized then
391 raise EDCP_hash.Create('Hash not initialized');
392 if blake2bp_final(@S, Hash, SizeOf(Hash)) < 0 then
393 raise EDCP_hash.Create('blake2bp_final');
394 Move(Hash, Digest, Sizeof(Hash));
395 Burn;
396 end;
397
398 end.
399