Debian Codegraph @

components/kascrypt/Hashes/dcpsha3.pas

raw
1 {******************************************************************************}
2 {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
3 {******************************************************************************}
4 {* A binary compatible implementation of SHA3 (224, 256, 384, 512) ************}
5 {******************************************************************************}
6 {* Copyright (C) 2016 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 DCPsha3;
26
27 {$mode objfpc}{$H+}
28
29 interface
30
31 uses
32 Classes, SysUtils, DCPcrypt2, DCPconst, SHA3;
33
34 type
35
36 { TDCP_sha3base }
37
38 TDCP_sha3base = class(TDCP_hash)
39 protected
40 FState: TSHA3State;
41 public
42 procedure Final(var Digest); override;
43 procedure Update(const Buffer; Size: longword); override;
44 end;
45
46 { TDCP_sha3_224 }
47
48 TDCP_sha3_224 = class(TDCP_sha3base)
49 public
50 class function GetAlgorithm: string; override;
51 class function GetHashSize: integer; override;
52 class function SelfTest: boolean; override;
53 procedure Init; override;
54 end;
55
56 { TDCP_sha3_256 }
57
58 TDCP_sha3_256 = class(TDCP_sha3base)
59 public
60 class function GetAlgorithm: string; override;
61 class function GetHashSize: integer; override;
62 class function SelfTest: boolean; override;
63 procedure Init; override;
64 end;
65
66 { TDCP_sha3_384 }
67
68 TDCP_sha3_384 = class(TDCP_sha3base)
69 public
70 class function GetAlgorithm: string; override;
71 class function GetHashSize: integer; override;
72 class function SelfTest: boolean; override;
73 procedure Init; override;
74 end;
75
76 { TDCP_sha3_512 }
77
78 TDCP_sha3_512 = class(TDCP_sha3base)
79 public
80 class function GetAlgorithm: string; override;
81 class function GetHashSize: integer; override;
82 class function SelfTest: boolean; override;
83 procedure Init; override;
84 end;
85
86 implementation
87
88 { TDCP_sha3base }
89
90 procedure TDCP_sha3base.Final(var Digest);
91 begin
92 SHA3_FinalBit_LSB(FState, 0, 0, @Digest, FState.fixedOutputLength);
93 end;
94
95 procedure TDCP_sha3base.Update(const Buffer; Size: longword);
96 begin
97 SHA3_UpdateXL(FState, @Buffer, Size);
98 end;
99
100 { TDCP_sha3_224 }
101
102 class function TDCP_sha3_224.GetAlgorithm: string;
103 begin
104 Result:= 'SHA3_224';
105 end;
106
107 class function TDCP_sha3_224.GetHashSize: integer;
108 begin
109 Result:= 224;
110 end;
111
112 class function TDCP_sha3_224.SelfTest: boolean;
113 begin
114 Result:= False; // TODO: SelfTest SHA3_224
115 end;
116
117 procedure TDCP_sha3_224.Init;
118 begin
119 SHA3_Init(FState, __SHA3_224);
120 end;
121
122 { TDCP_sha3_256 }
123
124 class function TDCP_sha3_256.GetAlgorithm: string;
125 begin
126 Result:= 'SHA3_256';
127 end;
128
129 class function TDCP_sha3_256.GetHashSize: integer;
130 begin
131 Result:= 256;
132 end;
133
134 class function TDCP_sha3_256.SelfTest: boolean;
135 begin
136 Result:= False; // TODO: SelfTest SHA3_256
137 end;
138
139 procedure TDCP_sha3_256.Init;
140 begin
141 SHA3_Init(FState, __SHA3_256);
142 end;
143
144 { TDCP_sha3_384 }
145
146 class function TDCP_sha3_384.GetAlgorithm: string;
147 begin
148 Result:= 'SHA3_384';
149 end;
150
151 class function TDCP_sha3_384.GetHashSize: integer;
152 begin
153 Result:= 384;
154 end;
155
156 class function TDCP_sha3_384.SelfTest: boolean;
157 begin
158 Result:= False; // TODO: SelfTest SHA3_384
159 end;
160
161 procedure TDCP_sha3_384.Init;
162 begin
163 SHA3_Init(FState, __SHA3_384);
164 end;
165
166 { TDCP_sha3_512 }
167
168 class function TDCP_sha3_512.GetAlgorithm: string;
169 begin
170 Result:= 'SHA3_512';
171 end;
172
173 class function TDCP_sha3_512.GetHashSize: integer;
174 begin
175 Result:= 512;
176 end;
177
178 class function TDCP_sha3_512.SelfTest: boolean;
179 begin
180 Result:= False; // TODO: SelfTest SHA3_512
181 end;
182
183 procedure TDCP_sha3_512.Init;
184 begin
185 SHA3_Init(FState, __SHA3_512);
186 end;
187
188 end.
189