-
Notifications
You must be signed in to change notification settings - Fork 45
/
XAD7ZipAESHandle.m
172 lines (136 loc) · 3.87 KB
/
XAD7ZipAESHandle.m
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
/*
* XAD7ZipAESHandle.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "XAD7ZipAESHandle.h"
#import "Crypto/sha.h"
@implementation XAD7ZipAESHandle
+(int)logRoundsForPropertyData:(NSData *)propertydata
{
int length=[propertydata length];
const uint8_t *bytes=[propertydata bytes];
if(length<1) return -1;
return bytes[0]&0x3f;
}
+(NSData *)saltForPropertyData:(NSData *)propertydata
{
int length=[propertydata length];
const uint8_t *bytes=[propertydata bytes];
if(length<1) return nil;
uint8_t flags=bytes[0];
if(flags&0xc0)
{
if(length<2) return nil;
if(flags&0x80)
{
int saltlength=(bytes[1]>>4)+1;
if(length<2+saltlength) return nil;
return [NSData dataWithBytes:&bytes[2] length:saltlength];
}
}
return [NSData data];
}
+(NSData *)IVForPropertyData:(NSData *)propertydata
{
int length=[propertydata length];
const uint8_t *bytes=[propertydata bytes];
if(length<1) return nil;
uint8_t flags=bytes[0];
if(flags&0xc0)
{
if(length<2) return nil;
int saltlength=0;
if(flags&0x80) saltlength=(bytes[1]>>4)+1;
if(flags&0x40)
{
int ivlength=(bytes[1]&0x0f)+1;
if(length<2+saltlength+ivlength) return nil;
return [NSData dataWithBytes:&bytes[2+saltlength] length:ivlength];
}
}
return [NSData data];
}
+(NSData *)keyForPassword:(NSString *)password salt:(NSData *)salt logRounds:(int)logrounds
{
uint8_t key[32];
int passchars=[password length];
int passlength=passchars*2;
uint8_t passbytes[passlength];
for(int i=0;i<passchars;i++)
{
unichar c=[password characterAtIndex:i];
passbytes[2*i]=c;
passbytes[2*i+1]=c>>8;
}
int saltlength=[salt length];
const uint8_t *saltbytes=[salt bytes];
if(logrounds==0x3f)
{
int passcopylength=passlength;
if(passcopylength+saltlength>sizeof(key)) passcopylength=sizeof(key)-saltlength;
memset(key,0,sizeof(key));
memcpy(&key[0],saltbytes,saltlength);
memcpy(&key[saltlength],passbytes,passcopylength);
}
else
{
SHA_CTX sha;
SHA256_Init(&sha);
uint64_t numrounds=1LL<<logrounds;
for(uint64_t i=0;i<numrounds;i++)
{
SHA256_Update(&sha,saltbytes,saltlength);
SHA256_Update(&sha,passbytes,passlength);
SHA256_Update(&sha,(uint8_t[8]) {
i&0xff,(i>>8)&0xff,(i>>16)&0xff,(i>>24)&0xff,
(i>>32)&0xff,(i>>40)&0xff,(i>>48)&0xff,(i>>56)&0xff,
},8);
}
SHA256_Final(key,&sha);
}
return [NSData dataWithBytes:key length:sizeof(key)];
}
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length key:(NSData *)keydata IV:(NSData *)ivdata
{
if(self=[super initWithParentHandle:handle length:length])
{
startoffs=[handle offsetInFile];
int ivlength=[ivdata length];
const uint8_t *ivbytes=[ivdata bytes];
memset(iv,0,sizeof(iv));
memcpy(iv,ivbytes,ivlength);
const uint8_t *keybytes=[keydata bytes];
aes_decrypt_key256(keybytes,&aes);
}
return self;
}
-(void)resetBlockStream
{
[parent seekToFileOffset:startoffs];
[self setBlockPointer:buffer];
memcpy(block,iv,sizeof(iv));
}
-(int)produceBlockAtOffset:(off_t)pos
{
int actual=[parent readAtMost:sizeof(buffer) toBuffer:buffer];
if(actual==0) return -1;
aes_cbc_decrypt(buffer,buffer,actual&~15,block,&aes);
return actual;
}
@end