Debian Codegraph @

plugins/wcx/zip/src/fparchive/abdfcrys.pas

raw
1 (* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is TurboPower Abbrevia
15 *
16 * The Initial Developer of the Original Code is
17 * TurboPower Software
18 *
19 * Portions created by the Initial Developer are Copyright (C) 1997-2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * ***** END LICENSE BLOCK ***** *)
25
26 {*********************************************************}
27 {* ABBREVIA: AbDfCryS.pas *}
28 {*********************************************************}
29 {* Deflate encryption streams *}
30 {*********************************************************}
31
32 unit AbDfCryS;
33
34 {$I AbDefine.inc}
35
36 interface
37
38 uses
39 Classes;
40
41 type
42 TAbZipEncryptHeader = array [0..11] of byte;
43
44 TAbZipDecryptEngine = class
45 private
46 FReady : boolean;
47 FState : array [0..2] of longint;
48 protected
49 procedure zdeInitState(const aPassphrase : AnsiString);
50 public
51 constructor Create;
52
53 function Decode(aCh : byte) : byte;
54 {-decodes a byte}
55 procedure DecodeBuffer(var aBuffer; aCount : integer);
56 {-decodes a buffer}
57
58 function VerifyHeader(const aHeader : TAbZipEncryptHeader;
59 const aPassphrase : AnsiString;
60 aCheckValue : longint) : boolean;
61 {-validate an encryption header}
62 end;
63
64 TAbDfDecryptStream = class(TStream)
65 private
66 FCheckValue : longint;
67 FEngine : TAbZipDecryptEngine;
68 FOwnsStream : Boolean;
69 FPassphrase : AnsiString;
70 FReady : boolean;
71 FStream : TStream;
72 protected
73 public
74 constructor Create(aStream : TStream;
75 aCheckValue : longint;
76 const aPassphrase : AnsiString);
77 destructor Destroy; override;
78
79 function IsValid : boolean;
80
81 function Read(var aBuffer; aCount : longint) : longint; override;
82 function Seek(aOffset : longint; aOrigin : word) : longint; override;
83 function Write(const aBuffer; aCount : longint) : longint; override;
84
85 property OwnsStream : Boolean
86 read FOwnsStream
87 write FOwnsStream;
88 end;
89
90 TAbZipEncryptEngine = class
91 private
92 FReady : boolean;
93 FState : array [0..2] of longint;
94 protected
95 procedure zeeInitState(const aPassphrase : AnsiString);
96 public
97 constructor Create;
98
99 function Encode(aCh : byte) : byte;
100 {-encodes a byte}
101 procedure EncodeBuffer(var aBuffer; aCount : integer);
102 {-encodes a buffer}
103
104 procedure CreateHeader(var aHeader : TAbZipEncryptHeader;
105 const aPassphrase : AnsiString;
106 aCheckValue : longint);
107 {-generate an encryption header}
108 end;
109
110 TAbDfEncryptStream = class(TStream)
111 private
112 FBuffer : PAnsiChar;
113 FBufSize : integer;
114 FEngine : TAbZipEncryptEngine;
115 FStream : TStream;
116 protected
117 public
118 constructor Create(aStream : TStream;
119 aCheckValue : longint;
120 const aPassphrase : AnsiString);
121 destructor Destroy; override;
122
123 function Read(var aBuffer; aCount : longint) : longint; override;
124 function Seek(aOffset : longint; aOrigin : word) : longint; override;
125 function Write(const aBuffer; aCount : longint) : longint; override;
126 end;
127
128 implementation
129
130 {Notes: the ZIP spec defines a couple of primitive routines for
131 performing encryption. For speed Abbrevia inlines them into
132 the respective methods of the encryption/decryption engines
133
134 char crc32(long,char)
135 return updated CRC from current CRC and next char
136
137 update_keys(char):
138 Key(0) <- crc32(key(0),char)
139 Key(1) <- Key(1) + (Key(0) & 000000ffH)
140 Key(1) <- Key(1) * 134775813 + 1
141 Key(2) <- crc32(key(2),key(1) >> 24)
142 end update_keys
143
144 char decrypt_byte()
145 local unsigned short temp
146 temp <- Key(2) | 2
147 decrypt_byte <- (temp * (temp ^ 1)) >> 8
148 end decrypt_byte
149 }
150
151 uses
152 AbUtils;
153
154 {---magic numbers from ZIP spec---}
155 const
156 StateInit1 = 305419896;
157 StateInit2 = 591751049;
158 StateInit3 = 878082192;
159 MagicNumber = 134775813;
160
161 {===internal encryption class========================================}
162 constructor TAbZipDecryptEngine.Create;
163 begin
164 {create the ancestor}
165 inherited Create;
166
167 {we're not ready for decryption yet since a header hasn't been
168 properly verified with VerifyHeader}
169 FReady := false;
170 end;
171 {--------}
172 function TAbZipDecryptEngine.Decode(aCh : byte) : byte;
173 var
174 Temp : longint;
175 begin
176 {check for programming error}
177 Assert(FReady,
178 'TAbZipDecryptEngine.Decode: must successfully call VerifyHeader first');
179
180 {calculate the decoded byte (uses inlined decrypt_byte)}
181 Temp := (FState[2] and $FFFF) or 2;
182 Result := aCh xor ((Temp * (Temp xor 1)) shr 8);
183
184 {mix the decoded byte into the state (uses inlined update_keys)}
185 FState[0] := AbUpdateCrc32(Result, FState[0]);
186 FState[1] := FState[1] + (FState[0] and $FF);
187 FState[1] := (FState[1] * MagicNumber) + 1;
188 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
189 end;
190 {--------}
191 procedure TAbZipDecryptEngine.DecodeBuffer(var aBuffer; aCount : integer);
192 var
193 i : integer;
194 Temp : longint;
195 Buffer : PAnsiChar;
196 WorkState : array [0..2] of longint;
197 begin
198 {check for programming error}
199 Assert(FReady,
200 'TAbZipDecryptEngine.Decode: must successfully call VerifyHeader first');
201
202 {move the state to a local variable--for better speed}
203 WorkState[0] := FState[0];
204 WorkState[1] := FState[1];
205 WorkState[2] := FState[2];
206
207 {reference the buffer as a PChar--easier arithmetic}
208 Buffer := @aBuffer;
209
210 {for each byte in the buffer...}
211 for i := 0 to pred(aCount) do begin
212
213 {calculate the next decoded byte (uses inlined decrypt_byte)}
214 Temp := (WorkState[2] and $FFFF) or 2;
215 Buffer^ := AnsiChar(
216 byte(Buffer^) xor ((Temp * (Temp xor 1)) shr 8));
217
218 {mix the decoded byte into the state (uses inlined update_keys)}
219 WorkState[0] := AbUpdateCrc32(byte(Buffer^), WorkState[0]);
220 WorkState[1] := WorkState[1] + (WorkState[0] and $FF);
221 WorkState[1] := (WorkState[1] * MagicNumber) + 1;
222 WorkState[2] := AbUpdateCrc32(WorkState[1] shr 24, WorkState[2]);
223
224 {move onto the next byte}
225 inc(Buffer);
226 end;
227
228 {save the state}
229 FState[0] := WorkState[0];
230 FState[1] := WorkState[1];
231 FState[2] := WorkState[2];
232 end;
233 {--------}
234 function TAbZipDecryptEngine.VerifyHeader(const aHeader : TAbZipEncryptHeader;
235 const aPassphrase : AnsiString;
236 aCheckValue : longint) : boolean;
237 type
238 TLongAsBytes = packed record
239 L1, L2, L3, L4 : byte
240 end;
241 var
242 i : integer;
243 Temp : longint;
244 WorkHeader : TAbZipEncryptHeader;
245 begin
246 {check for programming errors}
247 Assert(aPassphrase <> '',
248 'TAbZipDecryptEngine.VerifyHeader: need a passphrase');
249
250 {initialize the decryption state}
251 zdeInitState(aPassphrase);
252
253 {decrypt the bytes in the header}
254 for i := 0 to 11 do begin
255
256 {calculate the next decoded byte (uses inlined decrypt_byte)}
257 Temp := (FState[2] and $FFFF) or 2;
258 WorkHeader[i] := aHeader[i] xor ((Temp * (Temp xor 1)) shr 8);
259
260 {mix the decoded byte into the state (uses inlined update_keys)}
261 FState[0] := AbUpdateCrc32(WorkHeader[i], FState[0]);
262 FState[1] := FState[1] + (FState[0] and $FF);
263 FState[1] := (FState[1] * MagicNumber) + 1;
264 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
265 end;
266
267 {the header is valid if the twelfth byte of the decrypted header
268 equals the fourth byte of the check value}
269 Result := WorkHeader[11] = TLongAsBytes(aCheckValue).L4;
270
271 {note: zips created with PKZIP prior to version 2.0 also checked
272 that the tenth byte of the decrypted header equals the third
273 byte of the check value}
274 FReady := Result;
275 end;
276 {--------}
277 procedure TAbZipDecryptEngine.zdeInitState(const aPassphrase : AnsiString);
278 var
279 i : integer;
280 begin
281 {initialize the decryption state}
282 FState[0] := StateInit1;
283 FState[1] := StateInit2;
284 FState[2] := StateInit3;
285
286 {mix in the passphrase to the state (uses inlined update_keys)}
287 for i := 1 to length(aPassphrase) do begin
288 FState[0] := AbUpdateCrc32(byte(aPassphrase[i]), FState[0]);
289 FState[1] := FState[1] + (FState[0] and $FF);
290 FState[1] := (FState[1] * MagicNumber) + 1;
291 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
292 end;
293 end;
294 {====================================================================}
295
296
297 {====================================================================}
298 constructor TAbDfDecryptStream.Create(aStream : TStream;
299 aCheckValue : longint;
300 const aPassphrase : AnsiString);
301 begin
302 {create the ancestor}
303 inherited Create;
304
305 {save the parameters}
306 FStream := aStream;
307 FCheckValue := aCheckValue;
308 FPassphrase := aPassphrase;
309
310 {create the decryption engine}
311 FEngine := TAbZipDecryptEngine.Create;
312 end;
313 {--------}
314 destructor TAbDfDecryptStream.Destroy; {new !!.02}
315 begin
316 FEngine.Free;
317 if FOwnsStream then
318 FStream.Free;
319 inherited Destroy;
320 end;
321 {--------}
322 function TAbDfDecryptStream.IsValid : boolean;
323 var
324 Header : TAbZipEncryptHeader;
325 begin
326 {read the header from the stream}
327 FStream.ReadBuffer(Header, sizeof(Header));
328
329 {check to see if the decryption engine agrees it's valid}
330 Result := FEngine.VerifyHeader(Header, FPassphrase, FCheckValue);
331
332 {if it isn't valid, reposition the stream, ready for the next try}
333 if not Result then begin
334 FStream.Seek(-sizeof(Header), soCurrent);
335 FReady := false;
336 end
337
338 {otherwise, the stream is ready for decrypting data}
339 else
340 FReady := true;
341 end;
342 {--------}
343 function TAbDfDecryptStream.Read(var aBuffer; aCount : longint) : longint;
344 begin
345 {check for programming error}
346 Assert(FReady,
347 'TAbDfDecryptStream.Read: the stream header has not been verified');
348
349 {read the data from the underlying stream}
350 Result := FStream.Read(aBuffer, aCount);
351
352 {decrypt the data}
353 FEngine.DecodeBuffer(aBuffer, Result);
354 end;
355 {--------}
356 function TAbDfDecryptStream.Seek(aOffset : longint; aOrigin : word) : longint;
357 begin
358 Result := FStream.Seek(aOffset, aOrigin);
359 end;
360 {--------}
361 function TAbDfDecryptStream.Write(const aBuffer; aCount : longint) : longint;
362 begin
363 {check for programming error}
364 Assert(false,
365 'TAbDfDecryptStream.Write: the stream is read-only');
366 Result := 0;
367 end;
368 {====================================================================}
369
370
371 {===TAbZipEncryptEngine==============================================}
372 constructor TAbZipEncryptEngine.Create;
373 begin
374 {create the ancestor}
375 inherited Create;
376
377 {we're not ready for encryption yet since a header hasn't been
378 properly generated with CreateHeader}
379 FReady := false;
380 end;
381 {--------}
382 procedure TAbZipEncryptEngine.CreateHeader(
383 var aHeader : TAbZipEncryptHeader;
384 const aPassphrase : AnsiString;
385 aCheckValue : longint);
386 type
387 TLongAsBytes = packed record
388 L1, L2, L3, L4 : byte
389 end;
390 var
391 Ch : byte;
392 i : integer;
393 Temp : longint;
394 WorkHeader : TAbZipEncryptHeader;
395 begin
396 {check for programming errors}
397 Assert(aPassphrase <> '',
398 'TAbZipEncryptEngine.CreateHeader: need a passphrase');
399
400 {set the first ten bytes of the header with random values (in fact,
401 we use a random value for each byte and mix it in with the state)}
402
403 {initialize the decryption state}
404 zeeInitState(aPassphrase);
405
406 {for the first ten bytes...}
407 for i := 0 to 9 do begin
408
409 {get a random value}
410 Ch := Random( 256 );
411
412 {calculate the XOR encoding byte (uses inlined decrypt_byte)}
413 Temp := (FState[2] and $FFFF) or 2;
414 Temp := (Temp * (Temp xor 1)) shr 8;
415
416 {mix the unencoded byte into the state (uses inlined update_keys)}
417 FState[0] := AbUpdateCrc32(Ch, FState[0]);
418 FState[1] := FState[1] + (FState[0] and $FF);
419 FState[1] := (FState[1] * MagicNumber) + 1;
420 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
421
422 {set the current byte of the header}
423 WorkHeader[i] := Ch xor Temp;
424 end;
425
426 {now encrypt the first ten bytes of the header (this merely sets up
427 the state so that we can encrypt the last two bytes)}
428
429 {reinitialize the decryption state}
430 zeeInitState(aPassphrase);
431
432 {for the first ten bytes...}
433 for i := 0 to 9 do begin
434
435 {calculate the XOR encoding byte (uses inlined decrypt_byte)}
436 Temp := (FState[2] and $FFFF) or 2;
437 Temp := (Temp * (Temp xor 1)) shr 8;
438
439 {mix the unencoded byte into the state (uses inlined update_keys)}
440 FState[0] := AbUpdateCrc32(WorkHeader[i], FState[0]);
441 FState[1] := FState[1] + (FState[0] and $FF);
442 FState[1] := (FState[1] * MagicNumber) + 1;
443 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
444
445 {set the current byte of the header}
446 WorkHeader[i] := WorkHeader[i] xor Temp;
447 end;
448
449 {now initialize byte 10 of the header, and encrypt it}
450 Ch := TLongAsBytes(aCheckValue).L3;
451 Temp := (FState[2] and $FFFF) or 2;
452 Temp := (Temp * (Temp xor 1)) shr 8;
453 FState[0] := AbUpdateCrc32(Ch, FState[0]);
454 FState[1] := FState[1] + (FState[0] and $FF);
455 FState[1] := (FState[1] * MagicNumber) + 1;
456 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
457 WorkHeader[10] := Ch xor Temp;
458
459 {now initialize byte 11 of the header, and encrypt it}
460 Ch := TLongAsBytes(aCheckValue).L4;
461 Temp := (FState[2] and $FFFF) or 2;
462 Temp := (Temp * (Temp xor 1)) shr 8;
463 FState[0] := AbUpdateCrc32(Ch, FState[0]);
464 FState[1] := FState[1] + (FState[0] and $FF);
465 FState[1] := (FState[1] * MagicNumber) + 1;
466 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
467 WorkHeader[11] := Ch xor Temp;
468
469 {we're now ready to encrypt}
470 FReady := true;
471
472 {return the header}
473 aHeader := WorkHeader;
474 end;
475 {--------}
476 function TAbZipEncryptEngine.Encode(aCh : byte) : byte;
477 var
478 Temp : longint;
479 begin
480 {check for programming error}
481 Assert(FReady,
482 'TAbZipEncryptEngine.Encode: must call CreateHeader first');
483
484 {calculate the encoded byte (uses inlined decrypt_byte)}
485 Temp := (FState[2] and $FFFF) or 2;
486 Result := aCh xor (Temp * (Temp xor 1)) shr 8;
487
488 {mix the unencoded byte into the state (uses inlined update_keys)}
489 FState[0] := AbUpdateCrc32(aCh, FState[0]);
490 FState[1] := FState[1] + (FState[0] and $FF);
491 FState[1] := (FState[1] * MagicNumber) + 1;
492 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
493 end;
494 {--------}
495 procedure TAbZipEncryptEngine.EncodeBuffer(var aBuffer; aCount : integer);
496 var
497 Ch : byte;
498 i : integer;
499 Temp : longint;
500 Buffer : PAnsiChar;
501 WorkState : array [0..2] of longint;
502 begin
503 {check for programming error}
504 Assert(FReady,
505 'TAbZipEncryptEngine.EncodeBuffer: must call CreateHeader first');
506
507 {move the state to a local variable--for better speed}
508 WorkState[0] := FState[0];
509 WorkState[1] := FState[1];
510 WorkState[2] := FState[2];
511
512 {reference the buffer as a PChar--easier arithmetic}
513 Buffer := @aBuffer;
514
515 {for each byte in the buffer...}
516 for i := 0 to pred(aCount) do begin
517
518 {calculate the next encoded byte (uses inlined decrypt_byte)}
519 Temp := (WorkState[2] and $FFFF) or 2;
520 Ch := byte(Buffer^);
521 Buffer^ := AnsiChar(Ch xor ((Temp * (Temp xor 1)) shr 8));
522
523 {mix the decoded byte into the state (uses inlined update_keys)}
524 WorkState[0] := AbUpdateCrc32(Ch, WorkState[0]);
525 WorkState[1] := WorkState[1] + (WorkState[0] and $FF);
526 WorkState[1] := (WorkState[1] * MagicNumber) + 1;
527 WorkState[2] := AbUpdateCrc32(WorkState[1] shr 24, WorkState[2]);
528
529 {move onto the next byte}
530 inc(Buffer);
531 end;
532
533 {save the state}
534 FState[0] := WorkState[0];
535 FState[1] := WorkState[1];
536 FState[2] := WorkState[2];
537 end;
538 {--------}
539 procedure TAbZipEncryptEngine.zeeInitState(const aPassphrase : AnsiString);
540 var
541 i : integer;
542 begin
543 {initialize the decryption state}
544 FState[0] := StateInit1;
545 FState[1] := StateInit2;
546 FState[2] := StateInit3;
547
548 {mix in the passphrase to the state (uses inlined update_keys)}
549 for i := 1 to length(aPassphrase) do begin
550 FState[0] := AbUpdateCrc32(byte(aPassphrase[i]), FState[0]);
551 FState[1] := FState[1] + (FState[0] and $FF);
552 FState[1] := (FState[1] * MagicNumber) + 1;
553 FState[2] := AbUpdateCrc32(FState[1] shr 24, FState[2]);
554 end;
555 end;
556 {====================================================================}
557
558
559 {===TAbDfEncryptStream===============================================}
560 constructor TAbDfEncryptStream.Create(aStream : TStream;
561 aCheckValue : longint;
562 const aPassphrase : AnsiString);
563 var
564 Header : TAbZipEncryptHeader;
565 begin
566 {create the ancestor}
567 inherited Create;
568
569 {save the stream parameter}
570 FStream := aStream;
571
572 {create the encryption engine}
573 FEngine := TAbZipEncryptEngine.Create;
574
575 {generate the encryption header, write it to the stream}
576 FEngine.CreateHeader(Header, aPassphrase, aCheckValue);
577 aStream.WriteBuffer(Header, sizeof(Header));
578 end;
579 {--------}
580 destructor TAbDfEncryptStream.Destroy;
581 begin
582 {free the internal buffer if used}
583 if (FBuffer <> nil) then
584 FreeMem(FBuffer);
585
586 {free the engine}
587 FEngine.Free;
588
589 {destroy the ancestor}
590 inherited Destroy;
591 end;
592 {--------}
593 function TAbDfEncryptStream.Read(var aBuffer; aCount : longint) : longint;
594 begin
595 {check for programming error}
596 Assert(false,
597 'TAbDfEncryptStream.Read: the stream is write-only');
598 Result := 0;
599 end;
600 {--------}
601 function TAbDfEncryptStream.Seek(aOffset : longint; aOrigin : word) : longint;
602 begin
603 Result := FStream.Seek(aOffset, aOrigin);
604 end;
605 {--------}
606 function TAbDfEncryptStream.Write(const aBuffer; aCount : longint) : longint;
607 begin
608 {note: since we cannot alter a const parameter, we should copy the
609 data to our own buffer, encrypt it and then write it}
610
611 {check that our buffer is large enough}
612 if (FBufSize < aCount) then begin
613 if (FBuffer <> nil) then
614 FreeMem(FBuffer);
615 GetMem(FBuffer, aCount);
616 FBufSize := aCount;
617 end;
618
619 {copy the data to our buffer}
620 Move(aBuffer, FBuffer^, aCount);
621
622 {encrypt the data in our buffer}
623 FEngine.EncodeBuffer(FBuffer^, aCount);
624
625 {write the data in our buffer to the underlying stream}
626 Result := FStream.Write(FBuffer^, aCount);
627 end;
628 {====================================================================}
629
630
631 end.
632
633
634