Menu

[r1]: / Blowfish.pas  Maximize  Restore  History

Download this file

293 lines (268 with data), 14.0 kB

  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
{
***************************************************
* A binary compatible Blowfish implementation *
* written by Dave Barton (davebarton@bigfoot.com) *
***************************************************
* 64bit block encryption *
* Variable size key - up to 448bit *
***************************************************
}
unit Blowfish;
interface
uses
Sysutils, Tools;
type
TBlowfishData= record
InitBlock: array[0..7] of byte; { initial IV }
LastBlock: array[0..7] of byte; { current IV }
SBoxM: array[0..3,0..255] of DWORD;
PBoxM: array[0..17] of DWORD;
end;
function BlowfishSelfTest: boolean;
{ performs a self test on this implementation }
procedure BlowfishInit(var Data: TBlowfishData; Key: pointer; Len: integer; IV: pointer);
{ initializes the TBlowfishData structure with the key information and IV if applicable }
procedure BlowfishBurn(var Data: TBlowfishData);
{ erases all information about the key }
procedure BlowfishEncryptECB(const Data: TBlowfishData; InData, OutData: pointer);
{ encrypts the data in a 64bit block using the ECB mode }
procedure BlowfishEncryptCBC(var Data: TBlowfishData; InData, OutData: pointer);
{ encrypts the data in a 64bit block using the CBC chaining mode }
procedure BlowfishEncryptOFB(var Data: TBlowfishData; InData, OutData: pointer);
{ encrypts the data in a 64bit block using the OFB chaining mode }
procedure BlowfishEncryptCFB(var Data: TBlowfishData; InData, OutData: pointer; Len: integer);
{ encrypts Len bytes of data using the CFB chaining mode }
procedure BlowfishEncryptOFBC(var Data: TBlowfishData; InData, OutData: pointer; Len: integer);
{ encrypts Len bytes of data using the OFB counter chaining mode }
procedure BlowfishDecryptECB(const Data: TBlowfishData; InData, OutData: pointer);
{ decrypts the data in a 64bit block using the ECB mode }
procedure BlowfishDecryptCBC(var Data: TBlowfishData; InData, OutData: pointer);
{ decrypts the data in a 64bit block using the CBC chaining mode }
procedure BlowfishDecryptOFB(var Data: TBlowfishData; InData, OutData: pointer);
{ decrypts the data in a 64bit block using the OFB chaining mode }
procedure BlowfishDecryptCFB(var Data: TBlowfishData; InData, OutData: pointer; Len: integer);
{ decrypts Len bytes of data using the CFB chaining mode }
procedure BlowfishDecryptOFBC(var Data: TBlowfishData; InData, OutData: pointer; Len: integer);
{ decrypts Len bytes of data using the OFB counter chaining mode }
procedure BlowfishReset(var Data: TBlowfishData);
{ resets the chaining mode information }
{******************************************************************************}
implementation
{$I Blowfish.inc}
{$R-}
function BlowfishSelfTest;
const
Key: array[0..7] of byte= ($01,$23,$45,$67,$89,$AB,$CD,$EF);
InBlock: array[0..7] of byte= ($11,$11,$11,$11,$11,$11,$11,$11);
OutBlock: array[0..7] of byte= ($61,$F9,$C3,$80,$22,$81,$B0,$96);
var
Block: array[0..7] of byte;
Data: TBlowfishData;
begin
BlowfishInit(Data,@Key,Sizeof(Key),nil);
BlowfishEncryptECB(Data,@InBlock,@Block);
Result:= CompareMem(@Block,@OutBlock,Sizeof(Block));
BlowfishDecryptECB(Data,@Block,@Block);
Result:= Result and CompareMem(@Block,@InBlock,Sizeof(Block));
BlowfishBurn(Data);
end;
procedure BlowfishInit;
var
i, k: integer;
A: DWord;
KeyB: PByteArray;
Block: array[0..7] of byte;
begin
if (Len<= 0) or (Len> 56) then
raise Exception.Create('Blowfish: Key must be between 1 and 56 bytes long');
KeyB:= Key;
Move(SBox,Data.SBoxM,Sizeof(SBox));
Move(PBox,Data.PBoxM,Sizeof(PBox));
with Data do
begin
if IV= nil then
begin
FillChar(InitBlock,8,0);
FillChar(LastBlock,8,0);
end
else
begin
Move(IV^,InitBlock,8);
Move(IV^,LastBlock,8);
end;
k:= 0;
for i:= 0 to 17 do
begin
A:= KeyB[(k+3) mod Len];
A:= A + (KeyB[(k+2) mod Len] shl 8);
A:= A + (KeyB[(k+1) mod Len] shl 16);
A:= A + (KeyB[k] shl 24);
PBoxM[i]:= PBoxM[i] xor A;
k:= (k+4) mod Len;
end;
FillChar(Block,Sizeof(Block),0);
for i:= 0 to 8 do
begin
BlowfishEncryptECB(Data,@Block,@Block);
PBoxM[i*2]:= Block[3] + (Block[2] shl 8) + (Block[1] shl 16) + (Block[0] shl 24);
PBoxM[i*2+1]:= Block[7] + (Block[6] shl 8) + (Block[5] shl 16) + (Block[4] shl 24);
end;
for k:= 0 to 3 do
begin
for i:= 0 to 127 do
begin
BlowfishEncryptECB(Data,@Block,@Block);
SBoxM[k,i*2]:= Block[3] + (Block[2] shl 8) + (Block[1] shl 16) + (Block[0] shl 24);
SBoxM[k,i*2+1]:= Block[7] + (Block[6] shl 8) + (Block[5] shl 16) + (Block[4] shl 24);
end;
end;
end;
end;
procedure BlowfishBurn;
begin
FillChar(Data,Sizeof(Data),0);
end;
procedure BlowfishEncryptECB;
var
xL, xR: DWord;
begin
Move(InData^,xL,4);
Move(pointer(integer(InData)+4)^,xR,4);
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
xL:= xL xor Data.PBoxM[0];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[1];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[2];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[3];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[4];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[5];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[6];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[7];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[8];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[9];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[10];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[11];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[12];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[13];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[14];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[15];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[16];
xR:= xR xor Data.PBoxM[17];
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
Move(xR,OutData^,4);
Move(xL,pointer(integer(OutData)+4)^,4);
end;
procedure BlowfishDecryptECB;
var
xL, xR: DWord;
begin
Move(InData^,xL,4);
Move(pointer(integer(InData)+4)^,xR,4);
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
xL:= xL xor Data.PBoxM[17];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[16];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[15];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[14];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[13];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[12];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[11];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[10];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[9];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[8];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[7];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[6];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[5];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[4];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[3];
xR:= xR xor (((Data.SBoxM[0,(xL shr 24) and $FF] + Data.SBoxM[1,(xL shr 16) and $FF]) xor Data.SBoxM[2,(xL shr 8) and $FF]) + Data.SBoxM[3,xL and $FF]) xor Data.PBoxM[2];
xL:= xL xor (((Data.SBoxM[0,(xR shr 24) and $FF] + Data.SBoxM[1,(xR shr 16) and $FF]) xor Data.SBoxM[2,(xR shr 8) and $FF]) + Data.SBoxM[3,xR and $FF]) xor Data.PBoxM[1];
xR:= xR xor Data.PBoxM[0];
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
Move(xR,OutData^,4);
Move(xL,pointer(integer(OutData)+4)^,4);
end;
procedure BlowfishEncryptCBC;
begin
XorBlock(InData,@Data.LastBlock,OutData,8);
BlowfishEncryptECB(Data,OutData,OutData);
Move(OutData^,Data.LastBlock,8);
end;
procedure BlowfishDecryptCBC;
var
TempBlock: array[0..7] of byte;
begin
Move(InData^,TempBlock,8);
BlowfishDecryptECB(Data,InData,OutData);
XorBlock(OutData,@Data.LastBlock,OutData,8);
Move(TempBlock,Data.LastBlock,8);
end;
procedure BlowfishEncryptCFB;
var
i: integer;
TempBlock: array[0..7] of byte;
begin
for i:= 0 to Len-1 do
begin
BlowfishEncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
Move(Data.LastBlock[1],Data.LastBlock[0],7);
Data.LastBlock[7]:= PByteArray(OutData)[i];
end;
end;
procedure BlowfishDecryptCFB;
var
i: integer;
TempBlock: array[0..7] of byte;
b: byte;
begin
for i:= 0 to Len-1 do
begin
b:= PByteArray(InData)[i];
BlowfishEncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
Move(Data.LastBlock[1],Data.LastBlock[0],7);
Data.LastBlock[7]:= b;
end;
end;
procedure BlowfishEncryptOFB;
begin
BlowfishEncryptECB(Data,@Data.LastBlock,@Data.LastBlock);
XorBlock(@Data.LastBlock,InData,OutData,8);
end;
procedure BlowfishDecryptOFB;
begin
BlowfishEncryptECB(Data,@Data.LastBlock,@Data.LastBlock);
XorBlock(@Data.LastBlock,InData,OutData,8);
end;
procedure BlowfishEncryptOFBC;
var
i: integer;
TempBlock: array[0..7] of byte;
begin
for i:= 0 to Len-1 do
begin
BlowfishEncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
IncBlock(@Data.LastBlock,8);
end;
end;
procedure BlowfishDecryptOFBC;
var
i: integer;
TempBlock: array[0..7] of byte;
begin
for i:= 0 to Len-1 do
begin
BlowfishEncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
IncBlock(@Data.LastBlock,8);
end;
end;
procedure BlowfishReset;
begin
Move(Data.InitBlock,Data.LastBlock,8);
end;
end.