Debian Codegraph @

components/kascrypt/Hashes/Private/sha3_512.pas

raw
1 unit SHA3_512;
2
3 {SHA3-512 - 512 bit Secure Hash Function}
4
5
6 interface
7
8 (*************************************************************************
9
10 DESCRIPTION : SHA3-512 - 512 bit Secure Hash Function
11
12 REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17-D18/D25S, FPC, VP
13
14 EXTERNAL DATA : ---
15
16 MEMORY USAGE : ---
17
18 DISPLAY MODE : ---
19
20 REFERENCES : - FIPS 202 SHA-3 Standard: 'Permutation-Based Hash and
21 Extendable-Output Functions' available from
22 http://csrc.nist.gov/publications/PubsFIPS.html or
23 http://dx.doi.org/10.6028/NIST.FIPS.202 or
24 http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
25 - Test vectors and intermediate values:
26 http://csrc.nist.gov/groups/ST/toolkit/examples.html
27
28 Version Date Author Modification
29 ------- -------- ------- ------------------------------------------
30 0.10 10.08.15 W.Ehrhardt Initial BP version using SHA3-256 layout
31 0.11 17.08.15 we Updated references
32 0.12 15.05.17 we adjust OID to new MaxOIDLen
33
34 **************************************************************************)
35
36 (*-------------------------------------------------------------------------
37 (C) Copyright 2015-2017 Wolfgang Ehrhardt
38
39 This software is provided 'as-is', without any express or implied warranty.
40 In no event will the authors be held liable for any damages arising from
41 the use of this software.
42
43 Permission is granted to anyone to use this software for any purpose,
44 including commercial applications, and to alter it and redistribute it
45 freely, subject to the following restrictions:
46
47 1. The origin of this software must not be misrepresented; you must not
48 claim that you wrote the original software. If you use this software in
49 a product, an acknowledgment in the product documentation would be
50 appreciated but is not required.
51
52 2. Altered source versions must be plainly marked as such, and must not be
53 misrepresented as being the original software.
54
55 3. This notice may not be removed or altered from any source distribution.
56 ----------------------------------------------------------------------------*)
57
58
59 {$i STD.INC}
60
61 uses
62 BTypes,Hash,SHA3;
63
64
65 procedure SHA3_512Init(var Context: THashContext);
66 {-initialize context}
67
68 procedure SHA3_512Update(var Context: THashContext; Msg: pointer; Len: word);
69 {-update context with Msg data}
70
71 procedure SHA3_512UpdateXL(var Context: THashContext; Msg: pointer; Len: longint);
72 {-update context with Msg data}
73
74 procedure SHA3_512Final(var Context: THashContext; var Digest: TSHA3_512Digest);
75 {-finalize SHA3-512 calculation, clear context}
76
77 procedure SHA3_512FinalEx(var Context: THashContext; var Digest: THashDigest);
78 {-finalize SHA3-512 calculation, clear context}
79
80 procedure SHA3_512FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer);
81 {-finalize SHA3-512 calculation with bitlen bits from BData (big-endian), clear context}
82
83 procedure SHA3_512FinalBits(var Context: THashContext; var Digest: TSHA3_512Digest; BData: byte; bitlen: integer);
84 {-finalize SHA3-512 calculation with bitlen bits from BData (big-endian), clear context}
85
86 procedure SHA3_512FinalBits_LSB(var Context: THashContext; var Digest: TSHA3_512Digest; BData: byte; bitlen: integer);
87 {-finalize SHA3-512 calculation with bitlen bits from BData (LSB format), clear context}
88
89 function SHA3_512SelfTest: boolean;
90 {-self test for string from SHA3-512 documents}
91
92 procedure SHA3_512Full(var Digest: TSHA3_512Digest; Msg: pointer; Len: word);
93 {-SHA3-512 of Msg with init/update/final}
94
95 procedure SHA3_512FullXL(var Digest: TSHA3_512Digest; Msg: pointer; Len: longint);
96 {-SHA3-512 of Msg with init/update/final}
97
98 procedure SHA3_512File({$ifdef CONST} const {$endif} fname: Str255;
99 var Digest: TSHA3_512Digest; var buf; bsize: word; var Err: word);
100 {-SHA3-512 of file, buf: buffer with at least bsize bytes}
101
102
103 implementation
104
105
106 const
107 SHA3_512_BlockLen = 72; {Rate / 8, used only for HMAC} {FIPS202, Tab.3}
108
109 {http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html}
110 {2.16.840.1.101.3.4.2.8}
111 {joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2) SHA3-512(10)}
112
113 const
114 SHA3_512_OID : TOID_Vec = (2,16,840,1,101,3,4,2,10,-1,-1); {Len=9}
115 {??? NIST claims it has reserved SHA3-512(10), but e.g.}
116 {http://www.oid-info.com/ does not know it 2015-08-10. }
117
118 {$ifndef VER5X}
119 const
120 SHA3_512_Desc: THashDesc = (
121 HSig : C_HashSig;
122 HDSize : sizeof(THashDesc);
123 HDVersion : C_HashVers;
124 HBlockLen : SHA3_512_BlockLen;
125 HDigestlen: sizeof(TSHA3_512Digest);
126 {$ifdef FPC_ProcVar}
127 HInit : @SHA3_512Init;
128 HFinal : @SHA3_512FinalEx;
129 HUpdateXL : @SHA3_512UpdateXL;
130 {$else}
131 HInit : SHA3_512Init;
132 HFinal : SHA3_512FinalEx;
133 HUpdateXL : SHA3_512UpdateXL;
134 {$endif}
135 HAlgNum : longint(_SHA3_512);
136 HName : 'SHA3-512';
137 HPtrOID : @SHA3_512_OID;
138 HLenOID : 9;
139 HFill : 0;
140 {$ifdef FPC_ProcVar}
141 HFinalBit : @SHA3_512FinalBitsEx;
142 {$else}
143 HFinalBit : SHA3_512FinalBitsEx;
144 {$endif}
145 HReserved : (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
146 );
147 {$else}
148 var
149 SHA3_512_Desc: THashDesc;
150 {$endif}
151
152
153 {$ifdef BIT16}
154 {$F-}
155 {$endif}
156
157
158 {---------------------------------------------------------------------------}
159 procedure SHA3_512Init(var Context: THashContext);
160 {-initialize context}
161 begin
162 {Clear context}
163 SHA3_LastError := SHA3_Init(TSHA3State(Context),__SHA3_512);
164 end;
165
166
167 {---------------------------------------------------------------------------}
168 procedure SHA3_512UpdateXL(var Context: THashContext; Msg: pointer; Len: longint);
169 {-update context with Msg data}
170 begin
171 SHA3_LastError := SHA3_UpdateXL(TSHA3State(Context), Msg, Len);
172 end;
173
174
175 {---------------------------------------------------------------------------}
176 procedure SHA3_512Update(var Context: THashContext; Msg: pointer; Len: word);
177 {-update context with Msg data}
178 begin
179 SHA3_LastError := SHA3_UpdateXL(TSHA3State(Context), Msg, Len);
180 end;
181
182
183 {---------------------------------------------------------------------------}
184 procedure SHA3_512FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer);
185 {-finalize SHA3-512 calculation with bitlen bits from BData (big-endian), clear context}
186 begin
187 SHA3_LastError := SHA3_FinalBit(TSHA3State(Context), BData, bitlen, @Digest[0], 8*sizeof(Digest));
188 end;
189
190
191 {---------------------------------------------------------------------------}
192 procedure SHA3_512FinalBits(var Context: THashContext; var Digest: TSHA3_512Digest; BData: byte; bitlen: integer);
193 {-finalize SHA3-512 calculation with bitlen bits from BData (big-endian), clear context}
194 begin
195 SHA3_LastError := SHA3_FinalBit(TSHA3State(Context), BData, bitlen, @Digest[0], 8*sizeof(Digest));
196 end;
197
198
199 {---------------------------------------------------------------------------}
200 procedure SHA3_512FinalBits_LSB(var Context: THashContext; var Digest: TSHA3_512Digest; BData: byte; bitlen: integer);
201 {-finalize SHA3-512 calculation with bitlen bits from BData (LSB format), clear context}
202 begin
203 SHA3_LastError := SHA3_FinalBit_LSB(TSHA3State(Context), BData, bitlen, @Digest[0], 8*sizeof(Digest));
204 end;
205
206
207 {---------------------------------------------------------------------------}
208 procedure SHA3_512FinalEx(var Context: THashContext; var Digest: THashDigest);
209 {-finalize SHA3-512 calculation, clear context}
210 begin
211 SHA3_LastError := SHA3_FinalHash(TSHA3State(Context), @Digest[0]);
212 end;
213
214
215 {---------------------------------------------------------------------------}
216 procedure SHA3_512Final(var Context: THashContext; var Digest: TSHA3_512Digest);
217 {-finalize SHA3-512 calculation, clear context}
218 begin
219 SHA3_LastError := SHA3_FinalHash(TSHA3State(Context), @Digest[0]);
220 end;
221
222
223 {---------------------------------------------------------------------------}
224 function SHA3_512SelfTest: boolean;
225 {-self test for string from SHA3-512 documents}
226 const
227 Bl1 = 0;
228 dig1: TSHA3_512Digest = ($A6,$9F,$73,$CC,$A2,$3A,$9A,$C5,
229 $C8,$B5,$67,$DC,$18,$5A,$75,$6E,
230 $97,$C9,$82,$16,$4F,$E2,$58,$59,
231 $E0,$D1,$DC,$C1,$47,$5C,$80,$A6,
232 $15,$B2,$12,$3A,$F1,$F5,$F9,$4C,
233 $11,$E3,$E9,$40,$2C,$3A,$C5,$58,
234 $F5,$00,$19,$9D,$95,$B6,$D3,$E3,
235 $01,$75,$85,$86,$28,$1D,$CD,$26);
236 BL2 = 5;
237 msg2: array[0..0] of byte = ($13);
238 dig2: TSHA3_512Digest = ($A1,$3E,$01,$49,$41,$14,$C0,$98,
239 $00,$62,$2A,$70,$28,$8C,$43,$21,
240 $21,$CE,$70,$03,$9D,$75,$3C,$AD,
241 $D2,$E0,$06,$E4,$D9,$61,$CB,$27,
242 $54,$4C,$14,$81,$E5,$81,$4B,$DC,
243 $EB,$53,$BE,$67,$33,$D5,$E0,$99,
244 $79,$5E,$5E,$81,$91,$8A,$DD,$B0,
245 $58,$E2,$2A,$9F,$24,$88,$3F,$37);
246 BL3 = 30;
247 msg3: array[0..3] of byte = ($53,$58,$7B,$19);
248 dig3: TSHA3_512Digest = ($98,$34,$C0,$5A,$11,$E1,$C5,$D3,
249 $DA,$9C,$74,$0E,$1C,$10,$6D,$9E,
250 $59,$0A,$0E,$53,$0B,$6F,$6A,$AA,
251 $78,$30,$52,$5D,$07,$5C,$A5,$DB,
252 $1B,$D8,$A6,$AA,$98,$1A,$28,$61,
253 $3A,$C3,$34,$93,$4A,$01,$82,$3C,
254 $D4,$5F,$45,$E4,$9B,$6D,$7E,$69,
255 $17,$F2,$F1,$67,$78,$06,$7B,$AB);
256
257 {https://github.com/gvanas/KeccakCodePackage, SKat len=200}
258 BL4 = 200;
259 msg4: array[0..24] of byte = ($aa,$fd,$c9,$24,$3d,$3d,$4a,$09,
260 $65,$58,$a3,$60,$cc,$27,$c8,$d8,
261 $62,$f0,$be,$73,$db,$5e,$88,$aa,$55);
262 dig4: TSHA3_512Digest = ($62,$86,$c3,$db,$87,$d3,$b4,$5c,
263 $fd,$4d,$e8,$5a,$7a,$dd,$18,$e0,
264 $7a,$e2,$2f,$1f,$0f,$46,$75,$e1,
265 $d4,$e1,$fc,$77,$63,$37,$34,$d7,
266 $96,$28,$18,$a9,$f3,$b9,$6b,$37,
267 $fe,$77,$4f,$c2,$6d,$ea,$78,$74,
268 $85,$31,$7b,$96,$22,$27,$5f,$63,
269 $a7,$dd,$6d,$62,$d6,$50,$d3,$07);
270
271 var
272 Context: THashContext;
273 Digest : TSHA3_512Digest;
274
275 function SingleTest(Msg: pointer; BL: word; TDig: TSHA3_512Digest): boolean;
276 var
277 bytes: word;
278 begin
279 SingleTest := false;
280 SHA3_512Init(Context);
281 if SHA3_LastError<>0 then exit;
282 if BL=0 then SHA3_512Final(Context,Digest)
283 else begin
284 if BL>7 then begin
285 bytes := BL shr 3;
286 SHA3_512Update(Context, Msg, BL shr 3);
287 if SHA3_LastError<>0 then exit;
288 inc(Ptr2Inc(Msg), bytes);
289 end;
290 SHA3_512FinalBits_LSB(Context, Digest, pByte(Msg)^, BL and 7);
291 end;
292 if SHA3_LastError<>0 then exit;
293 SingleTest := HashSameDigest(@SHA3_512_Desc, PHashDigest(@TDig), PHashDigest(@Digest));
294 end;
295
296 begin
297 SHA3_512SelfTest := SingleTest(nil, BL1, dig1) and
298 SingleTest(@msg2, BL2, dig2) and
299 SingleTest(@msg3, BL3, dig3) and
300 SingleTest(@msg4, BL4, dig4);
301 end;
302
303
304 {---------------------------------------------------------------------------}
305 procedure SHA3_512FullXL(var Digest: TSHA3_512Digest; Msg: pointer; Len: longint);
306 {-SHA3-512 of Msg with init/update/final}
307 var
308 Context: THashContext;
309 begin
310 SHA3_512Init(Context);
311 if SHA3_LastError=0 then SHA3_512UpdateXL(Context, Msg, Len);
312 SHA3_512Final(Context, Digest);
313 end;
314
315
316 {---------------------------------------------------------------------------}
317 procedure SHA3_512Full(var Digest: TSHA3_512Digest; Msg: pointer; Len: word);
318 {-SHA3-512 of Msg with init/update/final}
319 begin
320 SHA3_512FullXL(Digest, Msg, Len);
321 end;
322
323
324 {---------------------------------------------------------------------------}
325 procedure SHA3_512File({$ifdef CONST} const {$endif} fname: Str255;
326 var Digest: TSHA3_512Digest; var buf; bsize: word; var Err: word);
327 {-SHA3-512 of file, buf: buffer with at least bsize bytes}
328 var
329 tmp: THashDigest;
330 begin
331 HashFile(fname, @SHA3_512_Desc, tmp, buf, bsize, Err);
332 move(tmp, Digest, sizeof(Digest));
333 end;
334
335
336 begin
337 {$ifdef VER5X}
338 fillchar(SHA3_512_Desc, sizeof(SHA3_512_Desc), 0);
339 with SHA3_512_Desc do begin
340 HSig := C_HashSig;
341 HDSize := sizeof(THashDesc);
342 HDVersion := C_HashVers;
343 HBlockLen := SHA3_512_BlockLen;
344 HDigestlen:= sizeof(TSHA3_512Digest);
345 HInit := SHA3_512Init;
346 HFinal := SHA3_512FinalEx;
347 HUpdateXL := SHA3_512UpdateXL;
348 HAlgNum := longint(_SHA3_512);
349 HName := 'SHA3-512';
350 HPtrOID := @SHA3_512_OID;
351 HLenOID := 9;
352 HFinalBit := SHA3_512FinalBitsEx;
353 end;
354 {$endif}
355 RegisterHash(_SHA3_512, @SHA3_512_Desc);
356 end.