Debian Codegraph @

components/kascrypt/dcpblockciphers.pas

raw
1 {******************************************************************************}
2 {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
3 {******************************************************************************}
4 {* Block cipher component definitions *****************************************}
5 {******************************************************************************}
6 {* Copyright (c) 1999-2002 David Barton *}
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 DCPblockciphers;
26
27 {$MODE Delphi}
28
29 interface
30 uses
31 Classes, Sysutils, DCPcrypt2;
32
33 {******************************************************************************}
34 { Base type definition for 64 bit block ciphers }
35 type
36 TDCP_blockcipher64= class(TDCP_blockcipher)
37 private
38 IV, CV: array[0..7] of byte;
39
40 procedure IncCounter;
41 public
42 class function GetBlockSize: integer; override;
43 { Get the block size of the cipher (in bits) }
44
45 procedure Reset; override;
46 { Reset any stored chaining information }
47 procedure Burn; override;
48 { Clear all stored key information and chaining information }
49 procedure SetIV(const Value); override;
50 { Sets the IV to Value and performs a reset }
51 procedure GetIV(var Value); override;
52 { Returns the current chaining information, not the actual IV }
53 procedure Init(const Key; Size: longword; InitVector: pointer); override;
54 { Do key setup based on the data in Key, size is in bits }
55
56 procedure EncryptCBC(const Indata; var Outdata; Size: longword); override;
57 { Encrypt size bytes of data using the CBC method of encryption }
58 procedure DecryptCBC(const Indata; var Outdata; Size: longword); override;
59 { Decrypt size bytes of data using the CBC method of decryption }
60 procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); override;
61 { Encrypt size bytes of data using the CFB (8 bit) method of encryption }
62 procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); override;
63 { Decrypt size bytes of data using the CFB (8 bit) method of decryption }
64 procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); override;
65 { Encrypt size bytes of data using the CFB (block) method of encryption }
66 procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); override;
67 { Decrypt size bytes of data using the CFB (block) method of decryption }
68 procedure EncryptOFB(const Indata; var Outdata; Size: longword); override;
69 { Encrypt size bytes of data using the OFB method of encryption }
70 procedure DecryptOFB(const Indata; var Outdata; Size: longword); override;
71 { Decrypt size bytes of data using the OFB method of decryption }
72 procedure EncryptCTR(const Indata; var Outdata; Size: longword); override;
73 { Encrypt size bytes of data using the CTR method of encryption }
74 procedure DecryptCTR(const Indata; var Outdata; Size: longword); override;
75 { Decrypt size bytes of data using the CTR method of decryption }
76 end;
77
78 {******************************************************************************}
79 { Base type definition for 128 bit block ciphers }
80 type
81 TDCP_blockcipher128= class(TDCP_blockcipher)
82 private
83 IV, CV: array[0..15] of byte;
84
85 procedure IncCounter;
86 public
87 class function GetBlockSize: integer; override;
88 { Get the block size of the cipher (in bits) }
89
90 procedure Reset; override;
91 { Reset any stored chaining information }
92 procedure Burn; override;
93 { Clear all stored key information and chaining information }
94 procedure SetIV(const Value); override;
95 { Sets the IV to Value and performs a reset }
96 procedure GetIV(var Value); override;
97 { Returns the current chaining information, not the actual IV }
98 procedure Init(const Key; Size: longword; InitVector: pointer); override;
99 { Do key setup based on the data in Key, size is in bits }
100
101 procedure EncryptCBC(const Indata; var Outdata; Size: longword); override;
102 { Encrypt size bytes of data using the CBC method of encryption }
103 procedure DecryptCBC(const Indata; var Outdata; Size: longword); override;
104 { Decrypt size bytes of data using the CBC method of decryption }
105 procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); override;
106 { Encrypt size bytes of data using the CFB (8 bit) method of encryption }
107 procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); override;
108 { Decrypt size bytes of data using the CFB (8 bit) method of decryption }
109 procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); override;
110 { Encrypt size bytes of data using the CFB (block) method of encryption }
111 procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); override;
112 { Decrypt size bytes of data using the CFB (block) method of decryption }
113 procedure EncryptOFB(const Indata; var Outdata; Size: longword); override;
114 { Encrypt size bytes of data using the OFB method of encryption }
115 procedure DecryptOFB(const Indata; var Outdata; Size: longword); override;
116 { Decrypt size bytes of data using the OFB method of decryption }
117 procedure EncryptCTR(const Indata; var Outdata; Size: longword); override;
118 { Encrypt size bytes of data using the CTR method of encryption }
119 procedure DecryptCTR(const Indata; var Outdata; Size: longword); override;
120 { Decrypt size bytes of data using the CTR method of decryption }
121 end;
122
123 implementation
124
125
126 {** TDCP_blockcipher64 ********************************************************}
127
128 procedure TDCP_blockcipher64.IncCounter;
129 var
130 i: integer;
131 begin
132 Inc(CV[7]);
133 i:= 7;
134 while (i> 0) and (CV[i] = 0) do
135 begin
136 Inc(CV[i-1]);
137 Dec(i);
138 end;
139 end;
140
141 class function TDCP_blockcipher64.GetBlockSize: integer;
142 begin
143 Result:= 64;
144 end;
145
146 procedure TDCP_blockcipher64.Init(const Key; Size: longword; InitVector: pointer);
147 begin
148 inherited Init(Key,Size,InitVector);
149 InitKey(Key,Size);
150 if InitVector= nil then
151 begin
152 FillChar(IV,8,{$IFDEF DCP1COMPAT}$FF{$ELSE}0{$ENDIF});
153 EncryptECB(IV,IV);
154 Reset;
155 end
156 else
157 begin
158 Move(InitVector^,IV,8);
159 Reset;
160 end;
161 end;
162
163 procedure TDCP_blockcipher64.SetIV(const Value);
164 begin
165 if not fInitialized then
166 raise EDCP_blockcipher.Create('Cipher not initialized');
167 Move(Value,IV,8);
168 Reset;
169 end;
170
171 procedure TDCP_blockcipher64.GetIV(var Value);
172 begin
173 if not fInitialized then
174 raise EDCP_blockcipher.Create('Cipher not initialized');
175 Move(CV,Value,8);
176 end;
177
178 procedure TDCP_blockcipher64.Reset;
179 begin
180 if not fInitialized then
181 raise EDCP_blockcipher.Create('Cipher not initialized')
182 else
183 Move(IV,CV,8);
184 end;
185
186 procedure TDCP_blockcipher64.Burn;
187 begin
188 FillChar(IV,8,$FF);
189 FillChar(CV,8,$FF);
190 inherited Burn;
191 end;
192
193 procedure TDCP_blockcipher64.EncryptCBC(const Indata; var Outdata; Size: longword);
194 var
195 i: longword;
196 p1, p2: pointer;
197 begin
198 if not fInitialized then
199 raise EDCP_blockcipher.Create('Cipher not initialized');
200 p1:= @Indata;
201 p2:= @Outdata;
202 for i:= 1 to (Size div 8) do
203 begin
204 Move(p1^,p2^,8);
205 XorBlock(p2^,CV,8);
206 EncryptECB(p2^,p2^);
207 Move(p2^,CV,8);
208 p1:= pointer(p1 + 8);
209 p2:= pointer(p2 + 8);
210 end;
211 if (Size mod 8)<> 0 then
212 begin
213 EncryptECB(CV,CV);
214 Move(p1^,p2^,Size mod 8);
215 XorBlock(p2^,CV,Size mod 8);
216 end;
217 end;
218
219 procedure TDCP_blockcipher64.DecryptCBC(const Indata; var Outdata; Size: longword);
220 var
221 i: longword;
222 p1, p2: pointer;
223 Temp: array[0..7] of byte;
224 begin
225 if not fInitialized then
226 raise EDCP_blockcipher.Create('Cipher not initialized');
227 dcpFillChar(Temp, SizeOf(Temp), 0);
228 p1:= @Indata;
229 p2:= @Outdata;
230 for i:= 1 to (Size div 8) do
231 begin
232 Move(p1^,p2^,8);
233 Move(p1^,Temp,8);
234 DecryptECB(p2^,p2^);
235 XorBlock(p2^,CV,8);
236 Move(Temp,CV,8);
237 p1:= pointer(p1 + 8);
238 p2:= pointer(p2 + 8);
239 end;
240 if (Size mod 8)<> 0 then
241 begin
242 EncryptECB(CV,CV);
243 Move(p1^,p2^,Size mod 8);
244 XorBlock(p2^,CV,Size mod 8);
245 end;
246 end;
247
248 procedure TDCP_blockcipher64.EncryptCFB8bit(const Indata; var Outdata; Size: longword);
249 var
250 i: longword;
251 p1, p2: Pbyte;
252 Temp: array[0..7] of byte;
253 begin
254 if not fInitialized then
255 raise EDCP_blockcipher.Create('Cipher not initialized');
256 p1:= @Indata;
257 p2:= @Outdata;
258 dcpFillChar(Temp, SizeOf(Temp), 0);
259 for i:= 1 to Size do
260 begin
261 EncryptECB(CV,Temp);
262 p2^:= p1^ xor Temp[0];
263 Move(CV[1],CV[0],8-1);
264 CV[7]:= p2^;
265 Inc(p1);
266 Inc(p2);
267 end;
268 end;
269
270 procedure TDCP_blockcipher64.DecryptCFB8bit(const Indata; var Outdata; Size: longword);
271 var
272 i: longword;
273 p1, p2: Pbyte;
274 TempByte: byte;
275 Temp: array[0..7] of byte;
276 begin
277 if not fInitialized then
278 raise EDCP_blockcipher.Create('Cipher not initialized');
279 p1:= @Indata;
280 p2:= @Outdata;
281 dcpFillChar(Temp, SizeOf(Temp), 0);
282 for i:= 1 to Size do
283 begin
284 TempByte:= p1^;
285 EncryptECB(CV,Temp);
286 p2^:= p1^ xor Temp[0];
287 Move(CV[1],CV[0],8-1);
288 CV[7]:= TempByte;
289 Inc(p1);
290 Inc(p2);
291 end;
292 end;
293
294 procedure TDCP_blockcipher64.EncryptCFBblock(const Indata; var Outdata; Size: longword);
295 var
296 i: longword;
297 p1, p2: Pbyte;
298 begin
299 if not fInitialized then
300 raise EDCP_blockcipher.Create('Cipher not initialized');
301 p1:= @Indata;
302 p2:= @Outdata;
303 for i:= 1 to (Size div 8) do
304 begin
305 EncryptECB(CV,CV);
306 Move(p1^,p2^,8);
307 XorBlock(p2^,CV,8);
308 Move(p2^,CV,8);
309 p1:= pointer(pointer(p1) + 8);
310 p2:= pointer(pointer(p2) + 8);
311 end;
312 if (Size mod 8)<> 0 then
313 begin
314 EncryptECB(CV,CV);
315 Move(p1^,p2^,Size mod 8);
316 XorBlock(p2^,CV,Size mod 8);
317 end;
318 end;
319
320 procedure TDCP_blockcipher64.DecryptCFBblock(const Indata; var Outdata; Size: longword);
321 var
322 i: longword;
323 p1, p2: Pbyte;
324 Temp: array[0..7] of byte;
325 begin
326 if not fInitialized then
327 raise EDCP_blockcipher.Create('Cipher not initialized');
328 p1:= @Indata;
329 p2:= @Outdata;
330 dcpFillChar(Temp, SizeOf(Temp), 0);
331 for i:= 1 to (Size div 8) do
332 begin
333 Move(p1^,Temp,8);
334 EncryptECB(CV,CV);
335 Move(p1^,p2^,8);
336 XorBlock(p2^,CV,8);
337 Move(Temp,CV,8);
338 p1:= pointer(pointer(p1) + 8);
339 p2:= pointer(pointer(p2) + 8);
340 end;
341 if (Size mod 8)<> 0 then
342 begin
343 EncryptECB(CV,CV);
344 Move(p1^,p2^,Size mod 8);
345 XorBlock(p2^,CV,Size mod 8);
346 end;
347 end;
348
349 procedure TDCP_blockcipher64.EncryptOFB(const Indata; var Outdata; Size: longword);
350 var
351 i: longword;
352 p1, p2: pointer;
353 begin
354 if not fInitialized then
355 raise EDCP_blockcipher.Create('Cipher not initialized');
356 p1:= @Indata;
357 p2:= @Outdata;
358 for i:= 1 to (Size div 8) do
359 begin
360 EncryptECB(CV,CV);
361 Move(p1^,p2^,8);
362 XorBlock(p2^,CV,8);
363 p1:= pointer(p1 + 8);
364 p2:= pointer(p2 + 8);
365 end;
366 if (Size mod 8)<> 0 then
367 begin
368 EncryptECB(CV,CV);
369 Move(p1^,p2^,Size mod 8);
370 XorBlock(p2^,CV,Size mod 8);
371 end;
372 end;
373
374 procedure TDCP_blockcipher64.DecryptOFB(const Indata; var Outdata; Size: longword);
375 var
376 i: longword;
377 p1, p2: pointer;
378 begin
379 if not fInitialized then
380 raise EDCP_blockcipher.Create('Cipher not initialized');
381 p1:= @Indata;
382 p2:= @Outdata;
383 for i:= 1 to (Size div 8) do
384 begin
385 EncryptECB(CV,CV);
386 Move(p1^,p2^,8);
387 XorBlock(p2^,CV,8);
388 p1:= pointer(p1 + 8);
389 p2:= pointer(p2 + 8);
390 end;
391 if (Size mod 8)<> 0 then
392 begin
393 EncryptECB(CV,CV);
394 Move(p1^,p2^,Size mod 8);
395 XorBlock(p2^,CV,Size mod 8);
396 end;
397 end;
398
399 procedure TDCP_blockcipher64.EncryptCTR(const Indata; var Outdata; Size: longword);
400 var
401 temp: array[0..7] of byte;
402 i: longword;
403 p1, p2: pointer;
404 begin
405 if not fInitialized then
406 raise EDCP_blockcipher.Create('Cipher not initialized');
407 p1:= @Indata;
408 p2:= @Outdata;
409 dcpFillChar(Temp, SizeOf(Temp), 0);
410 for i:= 1 to (Size div 8) do
411 begin
412 EncryptECB(CV,temp);
413 IncCounter;
414 Move(p1^,p2^,8);
415 XorBlock(p2^,temp,8);
416 p1:= pointer(p1 + 8);
417 p2:= pointer(p2 + 8);
418 end;
419 if (Size mod 8)<> 0 then
420 begin
421 EncryptECB(CV,temp);
422 IncCounter;
423 Move(p1^,p2^,Size mod 8);
424 XorBlock(p2^,temp,Size mod 8);
425 end;
426 end;
427
428 procedure TDCP_blockcipher64.DecryptCTR(const Indata; var Outdata; Size: longword);
429 var
430 temp: array[0..7] of byte;
431 i: longword;
432 p1, p2: pointer;
433 begin
434 if not fInitialized then
435 raise EDCP_blockcipher.Create('Cipher not initialized');
436 p1:= @Indata;
437 p2:= @Outdata;
438 dcpFillChar(Temp, SizeOf(Temp), 0);
439 for i:= 1 to (Size div 8) do
440 begin
441 EncryptECB(CV,temp);
442 IncCounter;
443 Move(p1^,p2^,8);
444 XorBlock(p2^,temp,8);
445 p1:= pointer(p1 + 8);
446 p2:= pointer(p2 + 8);
447 end;
448 if (Size mod 8)<> 0 then
449 begin
450 EncryptECB(CV,temp);
451 IncCounter;
452 Move(p1^,p2^,Size mod 8);
453 XorBlock(p2^,temp,Size mod 8);
454 end;
455 end;
456
457 {** TDCP_blockcipher128 ********************************************************}
458
459 procedure TDCP_blockcipher128.IncCounter;
460 begin
461 Inc(PQWord(@CV[0])^);
462 if (PQWord(@CV[0])^ = 0) then Inc(PQWord(@CV[8])^);
463 end;
464
465 class function TDCP_blockcipher128.GetBlockSize: integer;
466 begin
467 Result:= 128;
468 end;
469
470 procedure TDCP_blockcipher128.Init(const Key; Size: longword; InitVector: pointer);
471 begin
472 inherited Init(Key,Size,InitVector);
473 InitKey(Key,Size);
474 if InitVector= nil then
475 begin
476 FillChar(IV,16,{$IFDEF DCP1COMPAT}$FF{$ELSE}0{$ENDIF});
477 EncryptECB(IV,IV);
478 Reset;
479 end
480 else
481 begin
482 Move(InitVector^,IV,16);
483 Reset;
484 end;
485 end;
486
487 procedure TDCP_blockcipher128.SetIV(const Value);
488 begin
489 if not fInitialized then
490 raise EDCP_blockcipher.Create('Cipher not initialized');
491 Move(Value,IV,16);
492 Reset;
493 end;
494
495 procedure TDCP_blockcipher128.GetIV(var Value);
496 begin
497 if not fInitialized then
498 raise EDCP_blockcipher.Create('Cipher not initialized');
499 Move(CV,Value,16);
500 end;
501
502 procedure TDCP_blockcipher128.Reset;
503 begin
504 if not fInitialized then
505 raise EDCP_blockcipher.Create('Cipher not initialized')
506 else
507 Move(IV,CV,16);
508 end;
509
510 procedure TDCP_blockcipher128.Burn;
511 begin
512 FillChar(IV,16,$FF);
513 FillChar(CV,16,$FF);
514 inherited Burn;
515 end;
516
517 procedure TDCP_blockcipher128.EncryptCBC(const Indata; var Outdata; Size: longword);
518 var
519 i: longword;
520 p1, p2: pointer;
521 begin
522 if not fInitialized then
523 raise EDCP_blockcipher.Create('Cipher not initialized');
524 p1:= @Indata;
525 p2:= @Outdata;
526 for i:= 1 to (Size div 16) do
527 begin
528 Move(p1^,p2^,16);
529 XorBlock(p2^,CV,16);
530 EncryptECB(p2^,p2^);
531 Move(p2^,CV,16);
532 p1:= pointer(p1 + 16);
533 p2:= pointer(p2 + 16);
534 end;
535 if (Size mod 16)<> 0 then
536 begin
537 EncryptECB(CV,CV);
538 Move(p1^,p2^,Size mod 16);
539 XorBlock(p2^,CV,Size mod 16);
540 end;
541 end;
542
543 procedure TDCP_blockcipher128.DecryptCBC(const Indata; var Outdata; Size: longword);
544 var
545 i: longword;
546 p1, p2: pointer;
547 Temp: array[0..15] of byte;
548 begin
549 if not fInitialized then
550 raise EDCP_blockcipher.Create('Cipher not initialized');
551 p1:= @Indata;
552 p2:= @Outdata;
553 dcpFillChar(Temp, SizeOf(Temp), 0);
554 for i:= 1 to (Size div 16) do
555 begin
556 Move(p1^,p2^,16);
557 Move(p1^,Temp,16);
558 DecryptECB(p2^,p2^);
559 XorBlock(p2^,CV,16);
560 Move(Temp,CV,16);
561 p1:= pointer(p1 + 16);
562 p2:= pointer(p2 + 16);
563 end;
564 if (Size mod 16)<> 0 then
565 begin
566 EncryptECB(CV,CV);
567 Move(p1^,p2^,Size mod 16);
568 XorBlock(p2^,CV,Size mod 16);
569 end;
570 end;
571
572 procedure TDCP_blockcipher128.EncryptCFB8bit(const Indata; var Outdata; Size: longword);
573 var
574 i: longword;
575 p1, p2: Pbyte;
576 Temp: array[0..15] of byte;
577 begin
578 if not fInitialized then
579 raise EDCP_blockcipher.Create('Cipher not initialized');
580 p1:= @Indata;
581 p2:= @Outdata;
582 dcpFillChar(Temp, SizeOf(Temp), 0);
583 for i:= 1 to Size do
584 begin
585 EncryptECB(CV,Temp);
586 p2^:= p1^ xor Temp[0];
587 Move(CV[1],CV[0],15);
588 CV[15]:= p2^;
589 Inc(p1);
590 Inc(p2);
591 end;
592 end;
593
594 procedure TDCP_blockcipher128.DecryptCFB8bit(const Indata; var Outdata; Size: longword);
595 var
596 i: longword;
597 p1, p2: Pbyte;
598 TempByte: byte;
599 Temp: array[0..15] of byte;
600 begin
601 if not fInitialized then
602 raise EDCP_blockcipher.Create('Cipher not initialized');
603 p1:= @Indata;
604 p2:= @Outdata;
605 dcpFillChar(Temp, SizeOf(Temp), 0);
606 for i:= 1 to Size do
607 begin
608 TempByte:= p1^;
609 EncryptECB(CV,Temp);
610 p2^:= p1^ xor Temp[0];
611 Move(CV[1],CV[0],15);
612 CV[15]:= TempByte;
613 Inc(p1);
614 Inc(p2);
615 end;
616 end;
617
618 procedure TDCP_blockcipher128.EncryptCFBblock(const Indata; var Outdata; Size: longword);
619 var
620 i: longword;
621 p1, p2: Pbyte;
622 begin
623 if not fInitialized then
624 raise EDCP_blockcipher.Create('Cipher not initialized');
625 p1:= @Indata;
626 p2:= @Outdata;
627 for i:= 1 to (Size div 16) do
628 begin
629 EncryptECB(CV,CV);
630 Move(p1^,p2^,16);
631 XorBlock(p2^,CV,16);
632 Move(p2^,CV,16);
633 p1:= pointer(pointer(p1) + 16);
634 p2:= pointer(pointer(p2) + 16);
635 end;
636 if (Size mod 16)<> 0 then
637 begin
638 EncryptECB(CV,CV);
639 Move(p1^,p2^,Size mod 16);
640 XorBlock(p2^,CV,Size mod 16);
641 end;
642 end;
643
644 procedure TDCP_blockcipher128.DecryptCFBblock(const Indata; var Outdata; Size: longword);
645 var
646 i: longword;
647 p1, p2: Pbyte;
648 Temp: array[0..15] of byte;
649 begin
650 if not fInitialized then
651 raise EDCP_blockcipher.Create('Cipher not initialized');
652 p1:= @Indata;
653 p2:= @Outdata;
654 dcpFillChar(Temp, SizeOf(Temp), 0);
655 for i:= 1 to (Size div 16) do
656 begin
657 Move(p1^,Temp,16);
658 EncryptECB(CV,CV);
659 Move(p1^,p2^,16);
660 XorBlock(p2^,CV,16);
661 Move(Temp,CV,16);
662 p1:= pointer(pointer(p1) + 16);
663 p2:= pointer(pointer(p2) + 16);
664 end;
665 if (Size mod 16)<> 0 then
666 begin
667 EncryptECB(CV,CV);
668 Move(p1^,p2^,Size mod 16);
669 XorBlock(p2^,CV,Size mod 16);
670 end;
671 end;
672
673 procedure TDCP_blockcipher128.EncryptOFB(const Indata; var Outdata; Size: longword);
674 var
675 i: longword;
676 p1, p2: pointer;
677 begin
678 if not fInitialized then
679 raise EDCP_blockcipher.Create('Cipher not initialized');
680 p1:= @Indata;
681 p2:= @Outdata;
682 for i:= 1 to (Size div 16) do
683 begin
684 EncryptECB(CV,CV);
685 Move(p1^,p2^,16);
686 XorBlock(p2^,CV,16);
687 p1:= pointer(p1 + 16);
688 p2:= pointer(p2 + 16);
689 end;
690 if (Size mod 16)<> 0 then
691 begin
692 EncryptECB(CV,CV);
693 Move(p1^,p2^,Size mod 16);
694 XorBlock(p2^,CV,Size mod 16);
695 end;
696 end;
697
698 procedure TDCP_blockcipher128.DecryptOFB(const Indata; var Outdata; Size: longword);
699 var
700 i: longword;
701 p1, p2: pointer;
702 begin
703 if not fInitialized then
704 raise EDCP_blockcipher.Create('Cipher not initialized');
705 p1:= @Indata;
706 p2:= @Outdata;
707 for i:= 1 to (Size div 16) do
708 begin
709 EncryptECB(CV,CV);
710 Move(p1^,p2^,16);
711 XorBlock(p2^,CV,16);
712 p1:= pointer(p1 + 16);
713 p2:= pointer(p2 + 16);
714 end;
715 if (Size mod 16)<> 0 then
716 begin
717 EncryptECB(CV,CV);
718 Move(p1^,p2^,Size mod 16);
719 XorBlock(p2^,CV,Size mod 16);
720 end;
721 end;
722
723 procedure TDCP_blockcipher128.EncryptCTR(const Indata; var Outdata; Size: longword);
724 var
725 temp: array[0..15] of byte;
726 i: longword;
727 p1, p2: pointer;
728 begin
729 if not fInitialized then
730 raise EDCP_blockcipher.Create('Cipher not initialized');
731 p1:= @Indata;
732 p2:= @Outdata;
733 dcpFillChar(Temp, SizeOf(Temp), 0);
734 for i:= 1 to (Size div 16) do
735 begin
736 EncryptECB(CV,temp);
737 IncCounter;
738 Move(p1^,p2^,16);
739 XorBlock(p2^,temp,16);
740 p1:= pointer(p1 + 16);
741 p2:= pointer(p2 + 16);
742 end;
743 if (Size mod 16)<> 0 then
744 begin
745 EncryptECB(CV,temp);
746 IncCounter;
747 Move(p1^,p2^,Size mod 16);
748 XorBlock(p2^,temp,Size mod 16);
749 end;
750 end;
751
752 procedure TDCP_blockcipher128.DecryptCTR(const Indata; var Outdata; Size: longword);
753 var
754 temp: array[0..15] of byte;
755 i: longword;
756 p1, p2: pointer;
757 begin
758 if not fInitialized then
759 raise EDCP_blockcipher.Create('Cipher not initialized');
760 p1:= @Indata;
761 p2:= @Outdata;
762 dcpFillChar(Temp, SizeOf(Temp), 0);
763 for i:= 1 to (Size div 16) do
764 begin
765 EncryptECB(CV,temp);
766 IncCounter;
767 Move(p1^,p2^,16);
768 XorBlock(p2^,temp,16);
769 p1:= pointer(p1 + 16);
770 p2:= pointer(p2 + 16);
771 end;
772 if (Size mod 16)<> 0 then
773 begin
774 EncryptECB(CV,temp);
775 IncCounter;
776 Move(p1^,p2^,Size mod 16);
777 XorBlock(p2^,temp,Size mod 16);
778 end;
779 end;
780
781 end.