-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
HmacBlockInputStream.m
214 lines (165 loc) · 6.17 KB
/
HmacBlockInputStream.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
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
//
// HmacBlockStream.m
// Strongbox
//
// Created by Strongbox on 08/06/2020.
// Copyright © 2014-2021 Mark McGuill. All rights reserved.
//
#import "HmacBlockInputStream.h"
#import "Kdbx4Serialization.h"
#import "Utils.h"
#import <CommonCrypto/CommonCrypto.h>
#import "KeePassCiphers.h"
#import "Utils.h"
@interface HmacBlockInputStream ()
@property NSUInteger workingBlockOffset;
@property uint8_t* workingBlock;
@property size_t workingBlockLength;
@property int workingBlockIndex;
@property NSData *hmacKey;
@property size_t readSoFar;
@property NSError* error;
@property NSInputStream* innerStream;
@property BOOL finished;
@end
@implementation HmacBlockInputStream
- (instancetype)initWithStream:(NSInputStream *)stream hmacKey:(NSData *)hmacKey {
if (self = [super init]) {
if (!stream) {
return nil;
}
self.workingBlock = nil;
self.workingBlockOffset = 0;
self.workingBlockIndex = 0;
self.hmacKey = hmacKey;
self.innerStream = stream;
self.finished = NO;
}
return self;
}
- (void)open {
if (self.innerStream) {
[self.innerStream open];
}
}
- (void)close {
if (self.innerStream) {
[self.innerStream close];
self.innerStream = nil;
}
if (self.workingBlock) {
free(self.workingBlock);
}
self.workingBlock = nil;
}
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)maxLength {
NSUInteger bufferWritten = 0;
NSUInteger bufferOffset = 0;
while (bufferWritten < maxLength) {
NSUInteger bufferAvailable = maxLength - bufferWritten;
NSUInteger workingAvailable = self.workingBlockLength - self.workingBlockOffset;
if (self.workingBlock == nil || workingAvailable == 0) {
if (! [self loadNextBlock] ) {
return -1;
}
if (self.workingBlockLength == 0) {
break;
}
}
workingAvailable = self.workingBlockLength - self.workingBlockOffset;
NSUInteger bytesToWriteToBuffer = MIN(bufferAvailable, workingAvailable);
uint8_t *src = &self.workingBlock[self.workingBlockOffset];
uint8_t *dest = &buffer[bufferOffset];
memcpy(dest, src, bytesToWriteToBuffer);
bufferWritten += bytesToWriteToBuffer;
bufferOffset += bytesToWriteToBuffer;
self.workingBlockOffset += bytesToWriteToBuffer;
}
return bufferWritten;
}
- (BOOL)loadNextBlock {
if (self.finished) {
if (self.workingBlock) {
free(self.workingBlock);
}
self.workingBlock = nil;
self.workingBlockLength = 0;
return YES;
}
HmacBlockHeader blockHeader;
NSInteger bytesRead = [self.innerStream read:(uint8_t*)&blockHeader maxLength:SIZE_OF_HMAC_BLOCK_HEADER];
if (bytesRead != SIZE_OF_HMAC_BLOCK_HEADER) {
return NO;
}
size_t blockLength = littleEndian4BytesToUInt32(blockHeader.lengthBytes);
if (blockLength > 0) {
if (self.workingBlock != nil) {
free(self.workingBlock);
}
self.workingBlock = malloc(blockLength);
bytesRead = [self.innerStream read:self.workingBlock maxLength:blockLength];
if(bytesRead < 0 || bytesRead < blockLength) {
slog(@"Not enough data to decrypt Block! [%@]", self.innerStream.streamError);
self.error = self.innerStream.streamError ? self.innerStream.streamError : [Utils createNSError:@"Error: HmacBlocStream - Could not read enough from inner stream to decrypt block." errorCode:-1];
free(self.workingBlock);
self.workingBlock = nil;
self.workingBlockLength = 0;
self.finished = YES;
return NO;
}
self.workingBlockLength = blockLength;
NSData *actualHmac = getBlockHmacBytes(self.workingBlock, self.workingBlockLength, self.hmacKey, self.workingBlockIndex);
NSData *expectedHmac = [NSData dataWithBytes:blockHeader.hmacSha256 length:CC_SHA256_DIGEST_LENGTH];
if(![actualHmac isEqualToData:expectedHmac]) {
slog(@"Actual Block HMAC does not match expected. Block has been corrupted.");
self.error = [Utils createNSError:@"Actual Block HMAC does not match expected. Block has been corrupted." errorCode:-1];
self.workingBlock = nil;
self.finished = YES;
return NO;
}
}
else {
if (self.workingBlock) {
free(self.workingBlock);
}
self.workingBlock = nil;
self.workingBlockLength = 0;
self.finished = YES;
}
self.readSoFar += blockLength;
self.workingBlockIndex++;
self.workingBlockOffset = 0;
return YES;
}
NSData* getBlockHmac(NSData *data, NSData* hmacKey, uint64_t blockIndex) {
return getBlockHmacBytes((uint8_t*)data.bytes, data.length, hmacKey, blockIndex);
}
NSData* getBlockHmacBytes(const uint8_t* data, size_t len, NSData* hmacKey, uint64_t blockIndex) {
NSData* blockKey = getHmacKeyForBlock(hmacKey, blockIndex);
NSData* index = Uint64ToLittleEndianData(blockIndex);
NSData* blockSizeData = Uint32ToLittleEndianData((uint32_t)len);
CCHmacContext ctx;
CCHmacInit(&ctx, kCCHmacAlgSHA256, blockKey.bytes, blockKey.length);
CCHmacUpdate(&ctx, index.bytes, index.length);
CCHmacUpdate(&ctx, blockSizeData.bytes, blockSizeData.length);
if(len){
CCHmacUpdate(&ctx, data, len);
}
NSMutableData *hmac = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
CCHmacFinal(&ctx, hmac.mutableBytes);
return hmac;
}
NSData* getHmacKeyForBlock(NSData* key, uint64_t blockIndex) {
NSData* index = Uint64ToLittleEndianData(blockIndex);
NSMutableData *hash = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH];
CC_SHA512_CTX ctx;
CC_SHA512_Init(&ctx);
CC_SHA512_Update(&ctx, index.bytes, (CC_LONG)index.length);
CC_SHA512_Update(&ctx, key.bytes, (CC_LONG)key.length);
CC_SHA512_Final(hash.mutableBytes, &ctx);
return hash;
}
- (NSError *)streamError {
return self.error;
}
@end