1 {Kol modifications done by neuron, note that this only includes the twofish
2 implementation, not the (loads) of other free encryption types.
6 neuron, neuron@hollowtube.mine.nu, problems with the modification can be sent
11 var TFData:TTwofishData;
12 const key=array[0..7] of char = 'qm3igmfk'
13 // inblock and outblock can be pretty much anything
14 // if you use it on strings, remember, string[0] is the length
15 // so use twofishencrypt___(data,string[1],x,length(string));
17 TwofishInit(tfdata,key,8,nil);
18 twofishencryptcbc(tfdata,inblock,outblock,length(inblock));
20 twofishdecryptcbc(tfdata,outblock,inblock,length(outblock));
23 {******************************************************************************}
24 {** A binary compatible implementation of Twofish *****************************}
25 {******************************************************************************}
26 {** Written by David Barton (davebarton@bigfoot.com) **************************}
27 {** http://www.scramdisk.clara.net/ *******************************************}
28 {******************************************************************************}
33 {** DCPcrypt include file **}
35 {$Q-} { over flow checking off }
36 {$R-} { range checking off }
49 PDWordArray
= ^TDWordArray
;
50 TDWordArray
= array[0..1023] of our_DWord
;
51 PByteArray
= ^TByteArray
;
52 TByteArray
= array[0..4095] of byte;
56 BLU
: array[0..3] of our_DWord
= (0, 8, 16, 24);
57 TWOFISH_BLOCKSIZE
= 16;
59 OUTPUTWHITEN
= (TWOFISH_BLOCKSIZE
div 4);
61 ROUNDSUBKEYS
= (OUTPUTWHITEN
+ TWOFISH_BLOCKSIZE
div 4);
62 TOTALSUBKEYS
= (ROUNDSUBKEYS
+ NUMROUNDS
* 2);
91 IV
, LB
: array[0..15] of byte;
93 SubKeys
: array[0..TOTALSUBKEYS
-1] of our_DWord
;
94 sboxKeys
: array[0..3] of our_DWord
;
95 sbox
: array[0..3,0..255] of our_DWord
;
98 procedure TwofishInit(var Data
: TTwofishData
; var Key
; Size
: longint; IVector
: pointer);
99 procedure TwofishReset(var Data
: TTwofishData
);
100 procedure TwofishBurn(var Data
: TTwofishData
);
101 procedure TwofishEncryptECB(var Data
: TTwofishData
; const InBlock
; var OutBlock
);
102 procedure TwofishDecryptECB(var Data
: TTwofishData
; const InBlock
; var OutBlock
);
103 procedure TwofishEncryptCBC(var Data
: TTwofishData
; const InData
; var OutData
; Size
: longint);
104 procedure TwofishDecryptCBC(var Data
: TTwofishData
; const InData
; var OutData
; Size
: longint);
105 procedure TwofishEncryptCFB(var Data
: TTwofishData
; const InData
; var OutData
; Size
: longint);
106 procedure TwofishDecryptCFB(var Data
: TTwofishData
; const InData
; var OutData
; Size
: longint);
108 {******************************************************************************}
109 {******************************************************************************}
115 MDS
: array[0..3,0..255] of our_DWord
;
118 function LRot32(X
: our_DWord
; c
: longint): our_DWord
; register; assembler;
124 function RRot32(X
: our_DWord
; c
: longint): our_DWord
; register; assembler;
130 procedure XorBlock(I1
, I2
, O1
: PByteArray
; Len
: longint);
134 for i
:= 0 to Len
-1 do
135 O1
^[i
]:= I1
^[i
] xor I2
^[i
];
144 function LFSR1(x
: our_DWord
): our_DWord
;
146 if (x
and 1)<> 0 then
147 Result
:= (x
shr 1) xor (MDS_GF_FDBK
div 2)
151 function LFSR2(x
: our_DWord
): our_DWord
;
153 if (x
and 2)<> 0 then
154 if (x
and 1)<> 0 then
155 Result
:= (x
shr 2) xor (MDS_GF_FDBK
div 2) xor (MDS_GF_FDBK
div 4)
157 Result
:= (x
shr 2) xor (MDS_GF_FDBK
div 2)
159 if (x
and 1)<> 0 then
160 Result
:= (x
shr 2) xor (MDS_GF_FDBK
div 4)
164 function Mul_X(x
: our_DWord
): our_DWord
;
166 Result
:= x
xor LFSR2(x
);
168 function Mul_Y(x
: our_DWord
): our_DWord
;
170 Result
:= x
xor LFSR1(x
) xor LFSR2(x
);
173 function RS_MDS_Encode(lK0
, lK1
: our_dword
): our_dword
;
175 lR
, nI
, nJ
, lG2
, lG3
: our_dword
;
188 if (bB
and $80)<> 0 then
189 lG2
:= ((bB
shl 1) xor RS_GF_FDBK
) and $FF
191 lG2
:= (bB
shl 1) and $FF;
192 if (bB
and 1)<> 0 then
193 lG3
:= ((bB
shr 1) and $7f) xor (RS_GF_FDBK
shr 1) xor lG2
195 lG3
:= ((bB
shr 1) and $7f) xor lG2
;
196 lR
:= (lR
shl 8) xor (lG3
shl 24) xor (lG2
shl 16) xor (lG3
shl 8) xor bB
;
202 function f32(x
: our_dword
; K32
: PDWordArray
; Len
: our_dword
): our_dword
;
204 t0
, t1
, t2
, t3
: our_dword
;
207 t1
:= (x
shr 8) and $FF;
208 t2
:= (x
shr 16) and $FF;
212 t0
:= p8x8
[p_04
,t0
] xor ((K32
^[3]) and $FF);
213 t1
:= p8x8
[p_14
,t1
] xor ((K32
^[3] shr 8) and $FF);
214 t2
:= p8x8
[p_24
,t2
] xor ((K32
^[3] shr 16) and $FF);
215 t3
:= p8x8
[p_34
,t3
] xor ((K32
^[3] shr 24));
219 t0
:= p8x8
[p_03
,t0
] xor ((K32
^[2]) and $FF);
220 t1
:= p8x8
[p_13
,t1
] xor ((K32
^[2] shr 8) and $FF);
221 t2
:= p8x8
[p_23
,t2
] xor ((K32
^[2] shr 16) and $FF);
222 t3
:= p8x8
[p_33
,t3
] xor ((K32
^[2] shr 24));
224 Result
:= MDS
[0,p8x8
[p_01
,p8x8
[p_02
,t0
] xor ((K32
^[1]) and $FF)] xor ((K32
^[0]) and $FF)] xor
225 MDS
[1,p8x8
[p_11
,p8x8
[p_12
,t1
] xor ((K32
^[1] shr 8) and $FF)] xor ((K32
^[0] shr 8) and $FF)] xor
226 MDS
[2,p8x8
[p_21
,p8x8
[p_22
,t2
] xor ((K32
^[1] shr 16) and $FF)] xor ((K32
^[0] shr 16) and $FF)] xor
227 MDS
[3,p8x8
[p_31
,p8x8
[p_32
,t3
] xor ((K32
^[1] shr 24))] xor ((K32
^[0] shr 24))];
230 procedure TwofishEncryptECB
;
234 X
: array[0..3] of our_dword
;
237 X
[0]:= PDWord(@InBlock
)^ xor SubKeys
[INPUTWHITEN
];
238 X
[1]:= PDWord(longint(@InBlock
)+4)^ xor SubKeys
[INPUTWHITEN
+1];
239 X
[2]:= PDWord(longint(@InBlock
)+8)^ xor SubKeys
[INPUTWHITEN
+2];
240 X
[3]:= PDWord(longint(@InBlock
)+12)^ xor SubKeys
[INPUTWHITEN
+3];
242 while i
<= NUMROUNDS
-2 do
244 t0
:= sBox
[0,2*(x
[0] and $ff)] xor sBox
[0,2*(((x
[0]) shr 8) and $ff)+1]
245 xor sBox
[2,2*((x
[0] shr 16) and $ff)] xor sBox
[2,2*((x
[0] shr 24) and $ff)+1];
246 t1
:= sBox
[0,2*((x
[1] shr 24) and $ff)] xor sBox
[0,2*(x
[1] and $ff)+1]
247 xor sBox
[2,2*((x
[1] shr 8) and $ff)] xor sBox
[2,2*((x
[1] shr 16) and $ff)+1];
248 x
[3]:= LRot32(x
[3],1);
249 x
[2]:= x
[2] xor (t0
+ t1
+ SubKeys
[ROUNDSUBKEYS
+2*i
]);
250 x
[3]:= x
[3] xor (t0
+ 2*t1
+ SubKeys
[ROUNDSUBKEYS
+2*i
+1]);
251 x
[2]:= RRot32(x
[2],1);
253 t0
:= sBox
[0,2*(x
[2] and $ff)] xor sBox
[0,2*((x
[2] shr 8) and $ff)+1]
254 xor sBox
[2,2*((x
[2] shr 16) and $ff)] xor sBox
[2,2*((x
[2] shr 24) and $ff)+1];
255 t1
:= sBox
[0,2*((x
[3] shr 24) and $ff)] xor sBox
[0,2*(x
[3] and $ff)+1]
256 xor sBox
[2,2*((x
[3] shr 8) and $ff)] xor sBox
[2,2*((x
[3] shr 16) and $ff)+1];
257 x
[1]:= LRot32(x
[1],1);
258 x
[0]:= x
[0] xor (t0
+ t1
+ SubKeys
[ROUNDSUBKEYS
+2*(i
+1)]);
259 x
[1]:= x
[1] xor (t0
+ 2*t1
+ SubKeys
[ROUNDSUBKEYS
+2*(i
+1)+1]);
260 x
[0]:= RRot32(x
[0],1);
263 PDWord(longint(@OutBlock
)+ 0)^:= X
[2] xor SubKeys
[OUTPUTWHITEN
];
264 PDWord(longint(@OutBlock
)+ 4)^:= X
[3] xor SubKeys
[OUTPUTWHITEN
+1];
265 PDWord(longint(@OutBlock
)+ 8)^:= X
[0] xor SubKeys
[OUTPUTWHITEN
+2];
266 PDWord(longint(@OutBlock
)+12)^:= X
[1] xor SubKeys
[OUTPUTWHITEN
+3];
270 procedure TwofishDecryptECB
;
274 X
: array[0..3] of our_dword
;
277 X
[2]:= PDWord(@InBlock
)^ xor SubKeys
[OUTPUTWHITEN
];
278 X
[3]:= PDWord(longint(@InBlock
)+4)^ xor SubKeys
[OUTPUTWHITEN
+1];
279 X
[0]:= PDWord(longint(@InBlock
)+8)^ xor SubKeys
[OUTPUTWHITEN
+2];
280 X
[1]:= PDWord(longint(@InBlock
)+12)^ xor SubKeys
[OUTPUTWHITEN
+3];
284 t0
:= sBox
[0,2*(x
[2] and $ff)] xor sBox
[0,2*((x
[2] shr 8) and $ff)+1]
285 xor sBox
[2,2*((x
[2] shr 16) and $ff)] xor sBox
[2,2*((x
[2] shr 24) and $ff)+1];
286 t1
:= sBox
[0,2*((x
[3] shr 24) and $ff)] xor sBox
[0,2*(x
[3] and $ff)+1]
287 xor sBox
[2,2*((x
[3] shr 8) and $ff)] xor sBox
[2,2*((x
[3] shr 16) and $ff)+1];
288 x
[0]:= LRot32(x
[0],1);
289 x
[0]:= x
[0] xor (t0
+ t1
+ Subkeys
[ROUNDSUBKEYS
+2*(i
+1)]);
290 x
[1]:= x
[1] xor (t0
+ 2*t1
+ Subkeys
[ROUNDSUBKEYS
+2*(i
+1)+1]);
291 x
[1]:= RRot32(x
[1],1);
293 t0
:= sBox
[0,2*(x
[0] and $ff)] xor sBox
[0,2*((x
[0] shr 8) and $ff)+1]
294 xor sBox
[2,2*((x
[0] shr 16) and $ff)] xor sBox
[2,2*((x
[0] shr 24) and $ff)+1];
295 t1
:= sBox
[0,2*((x
[1] shr 24) and $ff)] xor sBox
[0,2*(x
[1] and $ff)+1]
296 xor sBox
[2,2*((x
[1] shr 8) and $ff)] xor sBox
[2,2*((x
[1] shr 16) and $ff)+1];
297 x
[2]:= LRot32(x
[2],1);
298 x
[2]:= x
[2] xor (t0
+ t1
+ Subkeys
[ROUNDSUBKEYS
+2*i
]);
299 x
[3]:= x
[3] xor (t0
+ 2*t1
+ Subkeys
[ROUNDSUBKEYS
+2*i
+1]);
300 x
[3]:= RRot32(x
[3],1);
303 PDWord(longint(@OutBlock
)+ 0)^:= X
[0] xor SubKeys
[INPUTWHITEN
];
304 PDWord(longint(@OutBlock
)+ 4)^:= X
[1] xor SubKeys
[INPUTWHITEN
+1];
305 PDWord(longint(@OutBlock
)+ 8)^:= X
[2] xor SubKeys
[INPUTWHITEN
+2];
306 PDWord(longint(@OutBlock
)+12)^:= X
[3] xor SubKeys
[INPUTWHITEN
+3];
310 procedure TwofishInit
;
311 procedure Xor256(Dst
, Src
: PDWordArray
; v
: byte);
316 Dst
^[i
]:= Src
^[i
] xor (v
* $01010101);
319 key32
: array[0..7] of our_dword
;
320 k32e
, k32o
: array[0..3] of our_dword
;
321 k64Cnt
, i
, j
, A
, B
, q
, subkeyCnt
: our_dword
;
322 L0
, L1
: array[0..255] of byte;
324 if (Size
> 256) or (Size
<= 0) or ((Size
mod 8)<> 0) then
328 FillChar(Key32
,Sizeof(Key32
),0);
329 Move(Key
,Key32
,Size
div 8);
330 if Size
<= 128 then { pad the key to either 128bit, 192bit or 256bit}
332 else if Size
<= 192 then
336 subkeyCnt
:= ROUNDSUBKEYS
+ 2*NUMROUNDS
;
338 k64Cnt
:= Size
div 64;
342 k32e
[i
]:= key32
[2*i
];
343 k32o
[i
]:= key32
[2*i
+1];
344 sboxKeys
[j
]:= RS_MDS_Encode(k32e
[i
],k32o
[i
]);
348 for i
:= 0 to ((subkeyCnt
div 2)-1) do
350 A
:= f32(q
,@k32e
,Size
);
351 B
:= f32(q
+SK_BUMP
,@k32o
,Size
);
355 SubKeys
[2*i
+1]:= LRot32(B
,SK_ROTL
);
360 Xor256(@L0
,@p8x8
[p_02
],(sboxKeys
[1] and $FF));
361 A
:= (sboxKeys
[0] and $FF);
365 sBox
[0 and 2,2*i
+(0 and 1)]:= MDS
[0,p8x8
[p_01
,L0
[i
]] xor A
];
366 sBox
[0 and 2,2*i
+(0 and 1)+2]:= MDS
[0,p8x8
[p_01
,L0
[i
+1]] xor A
];
369 Xor256(@L0
,@p8x8
[p_12
],(sboxKeys
[1] shr 8) and $FF);
370 A
:= (sboxKeys
[0] shr 8) and $FF;
374 sBox
[1 and 2,2*i
+(1 and 1)]:= MDS
[1,p8x8
[p_11
,L0
[i
]] xor A
];
375 sBox
[1 and 2,2*i
+(1 and 1)+2]:= MDS
[1,p8x8
[p_11
,L0
[i
+1]] xor A
];
378 Xor256(@L0
,@p8x8
[p_22
],(sboxKeys
[1] shr 16) and $FF);
379 A
:= (sboxKeys
[0] shr 16) and $FF;
383 sBox
[2 and 2,2*i
+(2 and 1)]:= MDS
[2,p8x8
[p_21
,L0
[i
]] xor A
];
384 sBox
[2 and 2,2*i
+(2 and 1)+2]:= MDS
[2,p8x8
[p_21
,L0
[i
+1]] xor A
];
387 Xor256(@L0
,@p8x8
[p_32
],(sboxKeys
[1] shr 24));
388 A
:= (sboxKeys
[0] shr 24);
392 sBox
[3 and 2,2*i
+(3 and 1)]:= MDS
[3,p8x8
[p_31
,L0
[i
]] xor A
];
393 sBox
[3 and 2,2*i
+(3 and 1)+2]:= MDS
[3,p8x8
[p_31
,L0
[i
+1]] xor A
];
398 Xor256(@L0
,@p8x8
[p_03
],sboxKeys
[2] and $FF);
399 A
:= sboxKeys
[0] and $FF;
400 B
:= sboxKeys
[1] and $FF;
404 sBox
[0 and 2,2*i
+(0 and 1)]:= MDS
[0,p8x8
[p_01
,p8x8
[p_02
,L0
[i
]] xor B
] xor A
];
405 sBox
[0 and 2,2*i
+(0 and 1)+2]:= MDS
[0,p8x8
[p_01
,p8x8
[p_02
,L0
[i
+1]] xor B
] xor A
];
408 Xor256(@L0
,@p8x8
[p_13
],(sboxKeys
[2] shr 8) and $FF);
409 A
:= (sboxKeys
[0] shr 8) and $FF;
410 B
:= (sboxKeys
[1] shr 8) and $FF;
414 sBox
[1 and 2,2*i
+(1 and 1)]:= MDS
[1,p8x8
[p_11
,p8x8
[p_12
,L0
[i
]] xor B
] xor A
];
415 sBox
[1 and 2,2*i
+(1 and 1)+2]:= MDS
[1,p8x8
[p_11
,p8x8
[p_12
,L0
[i
+1]] xor B
] xor A
];
418 Xor256(@L0
,@p8x8
[p_23
],(sboxKeys
[2] shr 16) and $FF);
419 A
:= (sboxKeys
[0] shr 16) and $FF;
420 B
:= (sboxKeys
[1] shr 16) and $FF;
424 sBox
[2 and 2,2*i
+(2 and 1)]:= MDS
[2,p8x8
[p_21
,p8x8
[p_22
,L0
[i
]] xor B
] xor A
];
425 sBox
[2 and 2,2*i
+(2 and 1)+2]:= MDS
[2,p8x8
[p_21
,p8x8
[p_22
,L0
[i
+1]] xor B
] xor A
];
428 Xor256(@L0
,@p8x8
[p_33
],(sboxKeys
[2] shr 24));
429 A
:= (sboxKeys
[0] shr 24);
430 B
:= (sboxKeys
[1] shr 24);
434 sBox
[3 and 2,2*i
+(3 and 1)]:= MDS
[3,p8x8
[p_31
,p8x8
[p_32
,L0
[i
]] xor B
] xor A
];
435 sBox
[3 and 2,2*i
+(3 and 1)+2]:= MDS
[3,p8x8
[p_31
,p8x8
[p_32
,L0
[i
+1]] xor B
] xor A
];
440 Xor256(@L1
,@p8x8
[p_04
],(sboxKeys
[3]) and $FF);
444 L0
[i
]:= p8x8
[p_03
,L1
[i
]];
445 L0
[i
+1]:= p8x8
[p_03
,L1
[i
+1]];
448 Xor256(@L0
,@L0
,(sboxKeys
[2]) and $FF);
449 A
:= (sboxKeys
[0]) and $FF;
450 B
:= (sboxKeys
[1]) and $FF;
454 sBox
[0 and 2,2*i
+(0 and 1)]:= MDS
[0,p8x8
[p_01
,p8x8
[p_02
,L0
[i
]] xor B
] xor A
];
455 sBox
[0 and 2,2*i
+(0 and 1)+2]:= MDS
[0,p8x8
[p_01
,p8x8
[p_02
,L0
[i
+1]] xor B
] xor A
];
458 Xor256(@L1
,@p8x8
[p_14
],(sboxKeys
[3] shr 8) and $FF);
462 L0
[i
]:= p8x8
[p_13
,L1
[i
]];
463 L0
[i
+1]:= p8x8
[p_13
,L1
[i
+1]];
466 Xor256(@L0
,@L0
,(sboxKeys
[2] shr 8) and $FF);
467 A
:= (sboxKeys
[0] shr 8) and $FF;
468 B
:= (sboxKeys
[1] shr 8) and $FF;
472 sBox
[1 and 2,2*i
+(1 and 1)]:= MDS
[1,p8x8
[p_11
,p8x8
[p_12
,L0
[i
]] xor B
] xor A
];
473 sBox
[1 and 2,2*i
+(1 and 1)+2]:= MDS
[1,p8x8
[p_11
,p8x8
[p_12
,L0
[i
+1]] xor B
] xor A
];
477 Xor256(@L1
,@p8x8
[p_24
],(sboxKeys
[3] shr 16) and $FF);
481 L0
[i
]:= p8x8
[p_23
,L1
[i
]];
482 L0
[i
+1]:= p8x8
[p_23
,L1
[i
+1]];
485 Xor256(@L0
,@L0
,(sboxKeys
[2] shr 16) and $FF);
486 A
:= (sboxKeys
[0] shr 16) and $FF;
487 B
:= (sboxKeys
[1] shr 16) and $FF;
491 sBox
[2 and 2,2*i
+(2 and 1)]:= MDS
[2,p8x8
[p_21
,p8x8
[p_22
,L0
[i
]] xor B
] xor A
];
492 sBox
[2 and 2,2*i
+(2 and 1)+2]:= MDS
[2,p8x8
[p_21
,p8x8
[p_22
,L0
[i
+1]] xor B
] xor A
];
495 Xor256(@L1
,@p8x8
[p_34
],(sboxKeys
[3] shr 24));
499 L0
[i
]:= p8x8
[p_33
,L1
[i
]];
500 L0
[i
+1]:= p8x8
[p_33
,L1
[i
+1]];
503 Xor256(@L0
,@L0
,(sboxKeys
[2] shr 24));
504 A
:= (sboxKeys
[0] shr 24);
505 B
:= (sboxKeys
[1] shr 24);
509 sBox
[3 and 2,2*i
+(3 and 1)]:= MDS
[3,p8x8
[p_31
,p8x8
[p_32
,L0
[i
]] xor B
] xor A
];
510 sBox
[3 and 2,2*i
+(3 and 1)+2]:= MDS
[3,p8x8
[p_31
,p8x8
[p_32
,L0
[i
+1]] xor B
] xor A
];
518 FillChar(IV
,Sizeof(IV
),$FF);
519 TwofishEncryptECB(Data
,IV
,IV
);
520 Move(IV
,LB
,Sizeof(LB
));
524 Move(IVector
^,IV
,Sizeof(IV
));
525 Move(IV
,LB
,Sizeof(IV
));
530 procedure TwofishBurn
;
533 FillChar(sBox
,Sizeof(sBox
),$FF);
534 FillChar(sBoxKeys
,Sizeof(sBoxKeys
),$FF);
535 FillChar(SubKeys
,Sizeof(SubKeys
),$FF);
536 FillChar(IV
,Sizeof(IV
),$FF);
537 FillChar(LB
,Sizeof(LB
),$FF);
541 procedure TwofishReset
;
544 Move(IV
,LB
,Sizeof(LB
));
547 procedure TwofishEncryptCBC
;
549 TB
: array[0..15] of byte;
553 for i
:= 1 to (Size
div 16) do
555 XorBlock(pointer(longint(@InData
)+((i
-1)*16)),@LB
,@TB
,Sizeof(TB
));
556 TwofishEncryptECB(Data
,TB
,TB
);
557 Move(TB
,pointer(longint(@OutData
)+((i
-1)*16))^,Sizeof(TB
));
558 Move(TB
,LB
,Sizeof(TB
));
560 if (Size
mod 16)<> 0 then
562 TwofishEncryptECB(Data
,LB
,TB
);
563 XorBlock(@TB
,@pointer(longint(@InData
)+Size
-(Size
mod 16))^,@pointer(longint(@OutData
)+Size
-(Size
mod 16))^,Size
mod 16);
565 FillChar(TB
,Sizeof(TB
),$FF);
569 procedure TwofishDecryptCBC
;
571 TB
: array[0..15] of byte;
575 for i
:= 1 to (Size
div 16) do
577 Move(pointer(longint(@InData
)+((i
-1)*16))^,TB
,Sizeof(TB
));
578 TwofishDecryptECB(Data
,pointer(longint(@InData
)+((i
-1)*16))^,pointer(longint(@OutData
)+((i
-1)*16))^);
579 XorBlock(@LB
,pointer(longint(@OutData
)+((i
-1)*16)),pointer(longint(@OutData
)+((i
-1)*16)),Sizeof(TB
));
580 Move(TB
,LB
,Sizeof(TB
));
582 if (Size
mod 16)<> 0 then
584 TwofishEncryptECB(Data
,LB
,TB
);
585 XorBlock(@TB
,@pointer(longint(@InData
)+Size
-(Size
mod 16))^,@pointer(longint(@OutData
)+Size
-(Size
mod 16))^,Size
mod 16);
587 FillChar(TB
,Sizeof(TB
),$FF);
591 procedure TwofishEncryptCFB
;
594 TB
: array[0..15] of byte;
597 for i
:= 0 to Size
-1 do
599 TwofishEncryptECB(Data
,LB
,TB
);
600 PByteArray(@OutData
)^[i
]:= PByteArray(@InData
)^[i
] xor TB
[0];
601 Move(LB
[1],LB
[0],15);
602 LB
[15]:= PByteArray(@OutData
)^[i
];
607 procedure TwofishDecryptCFB
;
610 TB
: array[0..15] of byte;
614 for i
:= 0 to Size
-1 do
616 b
:= PByteArray(@InData
)^[i
];
617 TwofishEncryptECB(Data
,LB
,TB
);
618 PByteArray(@OutData
)^[i
]:= PByteArray(@InData
)^[i
] xor TB
[0];
619 Move(LB
[1],LB
[0],15);
625 procedure PreCompMDS
;
627 m1
, mx
, my
: array[0..1] of our_dword
;
633 mx
[0]:= Mul_X(m1
[0]);
634 my
[0]:= Mul_Y(m1
[0]);
636 mx
[1]:= Mul_X(m1
[1]);
637 my
[1]:= Mul_Y(m1
[1]);
638 mds
[0,nI
]:= (m1
[P_00
] shl 0) or
642 mds
[1,nI
]:= (my
[p_10
] shl 0) or
646 mds
[2,nI
]:= (mx
[p_20
] shl 0) or
650 mds
[3,nI
]:= (mx
[p_30
] shl 0) or