Debian Codegraph @

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

raw
1 (* ***** BEGIN LICENSE BLOCK *****
2 *
3 * WinZip AES decryption stream
4 *
5 * Copyright (C) 2017 Alexander Koblov ([email protected])
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * ***** END LICENSE BLOCK ***** *)
27
28 unit AbWinZipAes;
29
30 {$mode objfpc}{$H+}
31
32 interface
33
34 uses
35 Classes, SysUtils, DCPrijndael, HMAC;
36
37 const
38 Ab_WinZipAesID : Word = $9901;
39
40 type
41
42 { TWinZipAesRec }
43
44 PWinZipAesRec = ^TWinZipAesRec;
45 TWinZipAesRec = packed record
46 Version: Word;
47 Vendor: Word;
48 Strength: Byte;
49 Method: Word;
50 end;
51
52 { TAbWinZipAesDecryptStream }
53
54 TAbWinZipAesDecryptStream = class(TStream)
55 private
56 FKey : TBytes;
57 FOwnsStream : Boolean;
58 FReady : Boolean;
59 FStream : TStream;
60 FDataStream : TStream;
61 FPassword : AnsiString;
62 FContext : THMAC_Context;
63 FDecoder : TDCP_rijndael;
64 FExtraField : TWinZipAesRec;
65 public
66 constructor Create(aStream : TStream;
67 aExtraField: PWinZipAesRec;
68 const aPassword : AnsiString);
69 destructor Destroy; override;
70
71 function IsValid : Boolean;
72 function Verify : Boolean;
73
74 function Read(var aBuffer; aCount : Longint) : Longint; override;
75 function Seek(aOffset : Longint; aOrigin : Word) : Longint; override;
76 function Write(const aBuffer; aCount : Longint) : Longint; override;
77
78 property ExtraField : TWinZipAesRec read FExtraField;
79 property OwnsStream : Boolean read FOwnsStream write FOwnsStream;
80 end;
81
82 implementation
83
84 uses
85 AbUnzOutStm, DCPcrypt2, SHA1, Hash, kdf;
86
87 const
88 CTR : array[0..15] of Byte = (1, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0);
90
91 const
92 MAC_LENGTH = 10;
93 PWD_VER_LENGTH = 2;
94 KEY_LENGTH: array[1..3] of Byte = (16, 24, 32);
95 SALT_LENGTH: array[1..3] of Byte = (8, 12, 16);
96
97 { TAbWinZipAesDecryptStream }
98
99 constructor TAbWinZipAesDecryptStream.Create(aStream: TStream;
100 aExtraField: PWinZipAesRec; const aPassword: AnsiString);
101 begin
102 inherited Create;
103
104 FStream := aStream;
105 FExtraField := aExtraField^;
106 FPassword := aPassword;
107
108 FDecoder := TDCP_rijndael.Create(nil);
109 end;
110
111 destructor TAbWinZipAesDecryptStream.Destroy;
112 begin
113 FDecoder.Free;
114 FDataStream.Free;
115 if FOwnsStream then
116 FStream.Free;
117 inherited Destroy;
118 end;
119
120 function TAbWinZipAesDecryptStream.IsValid: Boolean;
121 var
122 F: WordRec;
123 Salt: AnsiString;
124 HashDesc: PHashDesc;
125 AKeyLength: Integer;
126 ASaltLength: Integer;
127 AExtraLength: Integer;
128 begin
129 // Integer mode value indicating AES encryption strength
130 if not FExtraField.Strength in [1..3] then Exit(False);
131
132 AKeyLength := KEY_LENGTH[FExtraField.Strength];
133 ASaltLength := SALT_LENGTH[FExtraField.Strength];
134 AExtraLength := AKeyLength * 2 + PWD_VER_LENGTH;
135
136 SetLength(FKey, AExtraLength);
137 HashDesc:= FindHash_by_ID(_SHA1);
138
139 // Read salt value
140 SetLength(Salt, ASaltLength);
141 FStream.Read(Salt[1], ASaltLength);
142 // Read password verification value
143 FStream.Read({%H-}F, PWD_VER_LENGTH);
144
145 pbkdf2(HashDesc, Pointer(FPassword), Length(FPassword),
146 Pointer(Salt), Length(Salt), 1000, FKey[0], AExtraLength);
147
148 Result := (FKey[AExtraLength - 2] = F.Lo) and (FKey[AExtraLength - 1] = F.Hi);
149
150 if Result then
151 begin
152 FReady := True;
153 FDecoder.Init(FKey[0], AKeyLength * 8, @CTR[0]);
154 // Initialize for authentication using second key part
155 hmac_init(FContext, HashDesc, @FKey[AKeyLength], AKeyLength);
156 // Create encrypted file data stream
157 AExtraLength := ASaltLength + PWD_VER_LENGTH + MAC_LENGTH;
158 FDataStream := TAbUnzipSubsetStream.Create(FStream, FStream.Size - AExtraLength);
159 end
160 else begin
161 FReady := False;
162 FStream.Seek(-(ASaltLength + PWD_VER_LENGTH), soCurrent);
163 end
164 end;
165
166 function TAbWinZipAesDecryptStream.Verify: Boolean;
167 var
168 AMac: THashDigest;
169 ABuffer: array[0..MAC_LENGTH - 1] of Byte;
170 begin
171 hmac_final(FContext, {%H-}AMac);
172 FStream.Read({%H-}ABuffer[0], MAC_LENGTH);
173 Result := CompareByte(ABuffer[0], AMac[0], MAC_LENGTH) = 0;
174 end;
175
176 function TAbWinZipAesDecryptStream.Read(var aBuffer; aCount: Longint): Longint;
177 begin
178 Assert(FReady, 'TAbWinZipAesDecryptStream.Read: the stream header has not been verified');
179 Result := FDataStream.Read(aBuffer, aCount);
180 if Result > 0 then
181 begin
182 hmac_updateXL(FContext, @aBuffer, Result);
183 FDecoder.DecryptCTR(aBuffer, aBuffer, Result);
184 end;
185 end;
186
187 function TAbWinZipAesDecryptStream.Seek(aOffset: Longint; aOrigin: Word): Longint;
188 begin
189 Result := FDataStream.Seek(aOffset, aOrigin);
190 end;
191
192 function TAbWinZipAesDecryptStream.Write(const aBuffer; aCount: Longint): Longint;
193 begin
194 Assert(False, 'TAbWinZipAesDecryptStream.Write: the stream is read-only');
195 Result := 0;
196 end;
197
198 end.
199