-
Notifications
You must be signed in to change notification settings - Fork 6
/
aes_eax.pas
360 lines (307 loc) · 13.7 KB
/
aes_eax.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
unit AES_EAX;
(*************************************************************************
DESCRIPTION : AES EAX mode functions
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : Stack: local EAX ctx in AES_EAX_Enc_Auth/AES_EAX_Dec_Veri
DISPLAY MODE : ---
REFERENCES : [1] EAX: A Conventional Authenticated-Encryption Mode,
M.Bellare, P.Rogaway, D.Wagner <http://eprint.iacr.org/2003/069>
[2] http://csrc.nist.gov/CryptoToolkit/modes/proposedmodes/eax/eax-spec.pdf
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 11.06.04 we initial version (BP7+)
0.11 12.06.04 we uses BLKSIZE constant
0.12 13.06.04 we TP5/5.5/6
0.13 02.07.04 we {$ifdef DLL} stdcall; {$endif}
0.14 30.11.04 we AES_XorBlock, AESBLKSIZE
0.15 09.07.06 we Checked: D9-D10
0.16 14.06.07 we Type TAES_EAXContext
0.17 20.07.08 we All-in-one functions AES_EAX_Enc_Auth/AES_EAX_Dec_Veri
0.18 01.08.08 we Fix for loop in Internal_Veri
0.19 02.08.08 we Local ctx for AES_EAX_Enc_Auth/AES_EAX_Dec_Veri
0.20 06.08.08 we Suppress D4+ warning
0.21 09.08.08 we Check tLen in ANU_EAX_Dec_Veri
0.22 16.11.08 we Use Ptr2Inc, pByte from BTypes
0.23 27.07.10 we Longint ILen in AES_EAX_En/Decrypt
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2004-2010 Wolfgang Ehrhardt
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$i STD.INC}
{Use TEAXContext for legacy AES_EAX source codes. The new}
{context type TAES_EAXContext should be used instead.}
{.$define Support_Old_AES_EAXContext_Type}
interface
uses
BTypes, AES_Type, AES_Base, AES_CTR, AES_OMAC;
type
TAES_EAXContext = packed record
HdrOMAC : TAESContext; {Hdr OMAC1 context}
MsgOMAC : TAESContext; {Msg OMAC1 context}
ctr_ctx : TAESContext; {Msg AESCTR context}
NonceTag: TAESBlock; {nonce tag }
tagsize : word; {tag size (unused) }
flags : word; {ctx flags (unused)}
end;
{$ifdef Support_Old_AES_EAXContext_Type}
TEAXContext = TAES_EAXContext;
{$endif}
{$ifdef CONST}
function AES_EAX_Init(const Key; KBits: word; const nonce; nLen: word; var ctx: TAES_EAXContext): integer;
{$ifdef DLL} stdcall; {$endif}
{-Init hdr and msg OMACs, setp AESCTR with nonce tag}
{$else}
function AES_EAX_Init(var Key; KBits: word; var nonce; nLen: word; var ctx: TAES_EAXContext): integer;
{-Init hdr and msg OMACs, setp AESCTR with nonce tag}
{$endif}
function AES_EAX_Provide_Header(Hdr: pointer; hLen: word; var ctx: TAES_EAXContext): integer;
{$ifdef DLL} stdcall; {$endif}
{-Supply a message header. The header "grows" with each call}
function AES_EAX_Encrypt(ptp, ctp: Pointer; ILen: longint; var ctx: TAES_EAXContext): integer;
{$ifdef DLL} stdcall; {$endif}
{-Encrypt ILen bytes from ptp^ to ctp^ in CTR mode, update OMACs}
function AES_EAX_Decrypt(ctp, ptp: Pointer; ILen: longint; var ctx: TAES_EAXContext): integer;
{$ifdef DLL} stdcall; {$endif}
{-Encrypt ILen bytes from ptp^ to ctp^ in CTR mode, update OMACs}
procedure AES_EAX_Final(var tag: TAESBlock; var ctx: TAES_EAXContext);
{$ifdef DLL} stdcall; {$endif}
{-Compute EAX tag from context}
function AES_EAX_Enc_Auth(var tag: TAESBlock; {Tag record}
{$ifdef CONST}const{$else}var{$endif} Key; KBits: word; {key and bitlength of key}
{$ifdef CONST}const{$else}var{$endif} nonce; nLen: word; {nonce: address / length}
Hdr: pointer; hLen: word; {header: address / length}
ptp: pointer; pLen: longint; {plaintext: address / length}
ctp: pointer {ciphertext: address}
): integer;
{$ifdef DLL} stdcall; {$endif}
{-All-in-one call to encrypt/authenticate}
function AES_EAX_Dec_Veri( ptag: pointer; tLen: word; {Tag: address / length (0..16)}
{$ifdef CONST}const{$else}var{$endif} Key; KBits: word; {key and bitlength of key}
{$ifdef CONST}const{$else}var{$endif} nonce; nLen: word; {nonce: address / length}
Hdr: pointer; hLen: word; {header: address / length}
ctp: pointer; cLen: longint; {ciphertext: address / length}
ptp: pointer {plaintext: address}
): integer;
{$ifdef DLL} stdcall; {$endif}
{-All-in-one call to decrypt/verify. Decryption is done only if ptag^ is verified}
implementation
{---------------------------------------------------------------------------}
{$ifdef CONST}
function AES_EAX_Init(const Key; KBits: word; const nonce; nLen: word; var ctx: TAES_EAXContext): integer;
{-Init hdr and msg OMACs, setp AESCTR with nonce tag}
{$else}
function AES_EAX_Init(var Key; KBits: word; var nonce; nLen: word; var ctx: TAES_EAXContext): integer;
{-Init hdr and msg OMACs, setp AESCTR with nonce tag}
{$endif}
var
err: integer;
t_n: TAESBlock;
begin
fillchar(ctx, sizeof(ctx), 0);
{Initialize OMAC context with key}
err := AES_OMAC_Init(Key, KBits, ctx.HdrOMAC);
if err=0 then begin
{copy fresh context, first use MsgOMAC for nonce OMAC}
ctx.MsgOMAC := ctx.HdrOMAC;
fillchar(t_n, sizeof(t_n), 0);
err := AES_OMAC_Update(@t_n, sizeof(t_n), ctx.MsgOMAC);
if err=0 then err := AES_OMAC_Update(@nonce, nLen, ctx.MsgOMAC);
if err=0 then AES_OMAC_Final(ctx.NonceTag, ctx.MsgOMAC);
{inititialize AES-CTR context}
if err=0 then err := AES_CTR_Init(Key, KBits, ctx.NonceTag, ctx.ctr_ctx);
if err=0 then begin
{initialize msg OMAC}
ctx.MsgOMAC := ctx.HdrOMAC;
t_n[AESBLKSIZE-1] := 2;
err := AES_OMAC_Update(@t_n, sizeof(t_n), ctx.MsgOMAC);
{initialize header OMAC}
t_n[AESBLKSIZE-1] := 1;
if err=0 then err := AES_OMAC_Update(@t_n, sizeof(t_n), ctx.HdrOMAC);
end;
end;
AES_EAX_Init := err;
end;
{---------------------------------------------------------------------------}
function AES_EAX_Provide_Header(Hdr: pointer; hLen: word; var ctx: TAES_EAXContext): integer;
{-Supply a message header. The header "grows" with each call}
begin
AES_EAX_Provide_Header := AES_OMAC_Update(Hdr, hLen, ctx.HdrOMAC);
end;
{---------------------------------------------------------------------------}
function AES_EAX_Encrypt(ptp, ctp: Pointer; ILen: longint; var ctx: TAES_EAXContext): integer;
{-Encrypt ILen bytes from ptp^ to ctp^ in CTR mode, update OMACs}
var
err: integer;
begin
{encrypt (and check for nil pointers)}
err := AES_CTR_Encrypt(ptp, ctp, ILen, ctx.ctr_ctx);
if err=0 then begin
{OMAC1 ciphertext}
err := AES_OMAC_Update(ctp, ILen, ctx.MsgOMAC);
end;
AES_EAX_Encrypt := err;
end;
{---------------------------------------------------------------------------}
function AES_EAX_Decrypt(ctp, ptp: Pointer; ILen: longint; var ctx: TAES_EAXContext): integer;
{-Encrypt ILen bytes from ptp^ to ctp^ in CTR mode, update OMACs}
var
err: integer;
begin
{OMAC1 ciphertext}
err := AES_OMAC_Update(ctp, ILen, ctx.MsgOMAC);
if err=0 then begin
{decrypt}
err := AES_CTR_Decrypt(ctp, ptp, ILen, ctx.ctr_ctx);
end;
AES_EAX_Decrypt := err;
end;
{---------------------------------------------------------------------------}
procedure AES_EAX_Final(var tag: TAESBlock; var ctx: TAES_EAXContext);
{-Compute EAX tag from context}
var
ht: TAESBlock;
begin
AES_OMAC1_Final(ht, ctx.HdrOMAC);
AES_OMAC1_Final(tag, ctx.MsgOMAC);
AES_XorBlock(tag,ht,tag);
AES_XorBlock(tag,ctx.NonceTag,tag);
end;
{---------------------------------------------------------------------------}
function AES_EAX_Enc_Auth(var tag: TAESBlock; {Tag record}
{$ifdef CONST}const{$else}var{$endif} Key; KBits: word; {key and bitlength of key}
{$ifdef CONST}const{$else}var{$endif} nonce; nLen: word; {nonce: address / length}
Hdr: pointer; hLen: word; {header: address / length}
ptp: pointer; pLen: longint; {plaintext: address / length}
ctp: pointer {ciphertext: address}
): integer;
{-All-in-one call to encrypt/authenticate}
var
err : integer;
ILen: word;
ctx : TAES_EAXContext;
const
CHUNK=$8000;
begin
{$ifdef BIT16}
if (pLen>$FFFF) or (ofs(ptp^)+pLen>$FFFF) or (ofs(ctp^)+pLen>$FFFF) then begin
AES_EAX_Enc_Auth := AES_Err_EAX_Inv_Text_Length;
exit;
end;
{$endif}
if (ptp=nil) or (ctp=nil) then begin
if pLen>0 then begin
AES_EAX_Enc_Auth := AES_Err_NIL_Pointer;
exit;
end;
end;
err := AES_EAX_Init(Key, KBits, nonce, nLen, ctx);
if err=0 then err := AES_EAX_Provide_Header(Hdr, hLen, ctx);
while (err=0) and (pLen>0) do begin
if pLen>CHUNK then ILen := CHUNK else ILen := pLen;
err := AES_EAX_Encrypt(ptp, ctp, ILen, ctx);
inc(Ptr2Inc(ptp), ILen);
inc(Ptr2Inc(ctp), ILen);
dec(pLen, ILen);
end;
if err=0 then AES_EAX_Final(tag, ctx);
fillchar(ctx, sizeof(ctx), 0);
AES_EAX_Enc_Auth := err;
end;
{---------------------------------------------------------------------------}
function Internal_Veri(var ctx: TAES_EAXContext; ptag: pointer; tLen: word;
ctp: pointer; cLen: longint): integer;
{-calculate and verify tLen bytes of ptag^, performs OMAC phase of EAX}
var
err,i: integer;
ILen: word;
atag: TAESBlock;
const
CHUNK=$8000;
begin
{internal, assumes ctx is initialized, nonce and header}
{are processed, cLen, tLen are with in allowed ranges}
err := 0;
{calculate the ciphertext OMAC}
while (err=0) and (cLen>0) do begin
if cLen>CHUNK then ILen := CHUNK else ILen := cLen;
err := AES_OMAC_Update(ctp, ILen, ctx.MsgOMAC);
inc(Ptr2Inc(ctp), ILen);
dec(cLen, ILen);
end;
if (err=0) and (tLen>0) then begin
{calculate actual tag and compare with supplied tag}
AES_EAX_Final(atag, ctx);
for i:=0 to pred(tLen) do begin
if pByte(ptag)^<>atag[i] then begin
Internal_Veri := AES_Err_EAX_Verify_Tag;
exit;
end;
inc(Ptr2Inc(ptag));
end;
end;
Internal_Veri := err;
end;
{---------------------------------------------------------------------------}
function AES_EAX_Dec_Veri( ptag: pointer; tLen: word; {Tag: address / length (0..16)}
{$ifdef CONST}const{$else}var{$endif} Key; KBits: word; {key and bitlength of key}
{$ifdef CONST}const{$else}var{$endif} nonce; nLen: word; {nonce: address / length}
Hdr: pointer; hLen: word; {header: address / length}
ctp: pointer; cLen: longint; {ciphertext: address / length}
ptp: pointer {plaintext: address}
): integer;
{-All-in-one call to decrypt/verify. Decryption is done only if ptag^ is verified}
var
err : integer;
ILen: word;
ctx : TAES_EAXContext;
const
CHUNK=$8000;
begin
{$ifdef BIT16}
if (cLen>$FFFF) or (ofs(ptp^)+cLen>$FFFF) or (ofs(ctp^)+cLen>$FFFF) then begin
AES_EAX_Dec_Veri := AES_Err_EAX_Inv_Text_Length;
exit;
end;
{$endif}
if (ptp=nil) or (ctp=nil) then begin
if cLen>0 then begin
AES_EAX_Dec_Veri := AES_Err_NIL_Pointer;
exit;
end;
end;
if tLen>AESBLKSIZE then begin
AES_EAX_Dec_Veri := AES_Err_EAX_Inv_TAG_Length;
exit;
end;
err := AES_EAX_Init(Key, KBits, nonce, nLen, ctx);
if err=0 then err := AES_EAX_Provide_Header(Hdr, hLen, ctx);
if err=0 then begin
{First pass through ciphertext, calculated and compare tag}
err := Internal_Veri(ctx, ptag, tLen, ctp, cLen);
{if error or verfication failed, decrypt loop is skipped}
while (err=0) and (cLen>0) do begin
if cLen>CHUNK then ILen := CHUNK else ILen := cLen;
err := AES_CTR_Decrypt(ctp, ptp, ILen, ctx.ctr_ctx);
inc(Ptr2Inc(ptp), ILen);
inc(Ptr2Inc(ctp), ILen);
dec(cLen, ILen);
end;
end;
fillchar(ctx, sizeof(ctx), 0);
AES_EAX_Dec_Veri:= err;
end;
end.