-
Notifications
You must be signed in to change notification settings - Fork 3
/
Crypto.m
643 lines (535 loc) · 22.6 KB
/
Crypto.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//
// Crypto
//
// Copyright (c) 2012 Rob Napier
//
// This code is licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#import "RNCryptor+Private.h"
#import "Crypto.h"
static const NSUInteger kPreambleSize = 2;
@interface NSData (RNCryptor_ConsistentCompare)
/** Compare two NSData in time proportional to the compared data (otherData)
*
* isEqual:-based comparisons stop comparing at the first difference. This can be used by attackers, in some situations,
* to determine a secret value by considering the time required to compare the values.
*
* It is slightly better to call this as [secret rnc_isEqualInConsistentTime:attackersData] rather than the reverse,
* but it is not a major issue either way. In the first case, the time required is proportional to the attacker's data,
* which provides the attacker no information about the length of secret. In the second case, the time is proportional
* to the length of secret, which leaks a small amount of informaiont, but in a way that does not varry in proportion to
* the attacker's data.
*
* @param otherData data to compare
* @returns YES if values are equal
*/
- (BOOL)rnc_isEqualInConsistentTime:(NSData *)otherData;
@end
@implementation NSData (RNCryptor_ConstantCompare)
- (BOOL)rnc_isEqualInConsistentTime:(NSData *)otherData {
// The point of this routine is XOR the bytes of each data and accumulate the results with OR.
// If any bytes are different, then the OR will accumulate some non-0 value.
const uint8_t *myBytes = [self bytes];
const NSUInteger myLength = [self length];
const uint8_t *otherBytes = [otherData bytes];
const NSUInteger otherLength = [otherData length];
uint8_t result = otherLength != myLength; // Start with 0 (equal) only if our lengths are equal
for (NSUInteger i = 0; i < otherLength; ++i) {
// Use mod to wrap around ourselves if they are longer than we are.
// Remember, we already broke equality if our lengths are different.
result |= myBytes[i % myLength] ^ otherBytes[i];
}
return result == 0;
}
@end
@interface Crypto ()
@property (nonatomic, readonly) CCCryptorRef cryptor;
@property (nonatomic, readonly) NSMutableData *buffer;
@property (nonatomic, readwrite, strong) NSData *encryptionSalt;
@property (nonatomic, readwrite, strong) NSData *HMACSalt;
@property (nonatomic, readwrite, strong) NSData *IV;
@property (nonatomic, readwrite, assign) BOOL haveWrittenHeader;
@property (nonatomic, readonly, strong) NSMutableData *inData;
@property (nonatomic, readwrite, copy) NSData *encryptionKey;
@property (nonatomic, readwrite, copy) NSData *HMACKey;
@property (nonatomic, readwrite, copy) NSString *password;
@property (nonatomic, readwrite, assign) BOOL hasV1HMAC;
@property (nonatomic, readwrite, assign) RNCryptorSettings settings;
@end
@implementation Crypto
{
CCHmacContext _HMACContext;
NSMutableData *__inData;
}
@synthesize cryptor = __cryptor;
@synthesize buffer = __buffer;
@synthesize encryptionSalt = _encryptionSalt;
@synthesize HMACSalt = _HMACSalt;
@synthesize IV = _IV;
@synthesize haveWrittenHeader = _haveWrittenHeader;
@synthesize encryptionKey = _encryptionKey;
@synthesize HMACKey = _HMACKey;
@synthesize password = _password;
@synthesize settings = _settings;
- (Crypto *)initWithOperation:(CCOperation)operation settings:(RNCryptorSettings)settings key:(NSData *)key IV:(NSData *)IV error:(NSError **)error
{
self = [super init];
if (self) {
CCCryptorStatus
cryptorStatus = CCCryptorCreate(operation,
settings.algorithm,
settings.options,
key.bytes,
key.length,
IV.bytes,
&__cryptor);
if (cryptorStatus != kCCSuccess || __cryptor == NULL) {
if (error) {
*error = [NSError errorWithDomain:kRNCryptorErrorDomain code:cryptorStatus userInfo:nil];
}
self = nil;
return nil;
}
__buffer = [NSMutableData data];
}
return self;
}
- (void)dealloc
{
if (__cryptor) {
CCCryptorRelease(__cryptor);
}
}
- (NSData *)addData:(NSData *)data error:(NSError **)error
{
NSMutableData *buffer = self.buffer;
[buffer setLength:CCCryptorGetOutputLength(self.cryptor, [data length], true)]; // We'll reuse the buffer in -finish
size_t dataOutMoved;
CCCryptorStatus
cryptorStatus = CCCryptorUpdate(self.cryptor, // cryptor
data.bytes, // dataIn
data.length, // dataInLength (verified > 0 above)
buffer.mutableBytes, // dataOut
buffer.length, // dataOutAvailable
&dataOutMoved); // dataOutMoved
if (cryptorStatus != kCCSuccess) {
if (error) {
*error = [NSError errorWithDomain:kRNCryptorErrorDomain code:cryptorStatus userInfo:nil];
}
return nil;
}
return [buffer subdataWithRange:NSMakeRange(0, dataOutMoved)];
}
- (NSData *)finishWithError:(NSError **)error
{
NSMutableData *buffer = self.buffer;
size_t dataOutMoved;
CCCryptorStatus
cryptorStatus = CCCryptorFinal(self.cryptor, // cryptor
buffer.mutableBytes, // dataOut
buffer.length, // dataOutAvailable
&dataOutMoved); // dataOutMoved
if (cryptorStatus != kCCSuccess) {
if (error) {
*error = [NSError errorWithDomain:kRNCryptorErrorDomain code:cryptorStatus userInfo:nil];
}
return nil;
}
return [buffer subdataWithRange:NSMakeRange(0, dataOutMoved)];
}
+ (NSData *) encryptData: (NSData *) data password: (NSString *) password
{
return [self encryptData: data withSettings: kRNCryptorAES256Settings password: password error: nil];
}
+ (NSData *) encryptData: (NSData *) data password: (NSString *) password error: (NSError **) error
{
return [self encryptData: data withSettings: kRNCryptorAES256Settings password: password error: error];
}
+ (NSData *)encryptData:(NSData *)thePlaintext withSettings:(RNCryptorSettings)theSettings password:(NSString *)aPassword error:(NSError **)anError
{
Crypto *cryptor = [[self alloc] initWithSettings:theSettings
password:aPassword
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:thePlaintext direction:@"encrypt" error:anError];
}
+ (NSData *)encryptData:(NSData *)thePlaintext
withSettings:(RNCryptorSettings)theSettings
password:(NSString *)aPassword
IV:(NSData *)anIV
encryptionSalt:(NSData *)anEncryptionSalt
HMACSalt:(NSData *)anHMACSalt
error:(NSError **)anError
{
Crypto *cryptor = [[self alloc] initWithSettings:theSettings
password:aPassword
IV:anIV
encryptionSalt:anEncryptionSalt
HMACSalt:anHMACSalt
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:thePlaintext direction:@"encrypt" error:anError];
}
+ (NSData *)encryptData:(NSData *)thePlaintext withSettings:(RNCryptorSettings)theSettings encryptionKey:(NSData *)anEncryptionKey HMACKey:(NSData *)anHMACKey error:(NSError **)anError {
Crypto *cryptor = [[self alloc] initWithSettings:theSettings
encryptionKey:anEncryptionKey
HMACKey:anHMACKey
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:thePlaintext direction:@"encrypt" error:anError];
}
+ (NSData *)encryptData:(NSData *)thePlaintext
withSettings:(RNCryptorSettings)theSettings
encryptionKey:(NSData *)anEncryptionKey
HMACKey:(NSData *)anHMACKey
IV:(NSData *)anIV
error:(NSError **)anError
{
Crypto *cryptor = [[self alloc] initWithSettings:theSettings
encryptionKey:anEncryptionKey
HMACKey:anHMACKey
IV:anIV
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:thePlaintext direction:@"encrypt" error:anError];
}
- (Crypto *)initWithSettings:(RNCryptorSettings)theSettings
encryptionKey:(NSData *)anEncryptionKey
HMACKey:(NSData *)anHMACKey
handler:(RNCryptorHandler)aHandler {
return [self initWithSettings:kRNCryptorAES256Settings
encryptionKey:anEncryptionKey
HMACKey:anHMACKey
IV:[[self class] randomDataOfLength:theSettings.IVSize]
handler:aHandler];
}
- (Crypto *)initWithSettings:(RNCryptorSettings)theSettings
encryptionKey:(NSData *)anEncryptionKey
HMACKey:(NSData *)anHMACKey
IV:(NSData *)anIV
handler:(RNCryptorHandler)aHandler
{
self = [super initWithHandler:aHandler];
if (self) {
self.IV = anIV;
if (anHMACKey) {
CCHmacInit(&_HMACContext, theSettings.HMACAlgorithm, anHMACKey.bytes, anHMACKey.length);
self.HMACLength = theSettings.HMACLength;
}
NSError *error = nil;
self.engine = [[Crypto alloc] initWithOperation:kCCEncrypt
settings:theSettings
key:anEncryptionKey
IV:self.IV
error:&error];
if (!self.engine) {
[self cleanupAndNotifyWithError:error];
self = nil;
return nil;
}
}
return self;
}
- (Crypto *)initWithSettings:(RNCryptorSettings)theSettings password:(NSString *)aPassword handler:(RNCryptorHandler)aHandler {
return [self initWithSettings:theSettings
password:aPassword
IV:[[self class] randomDataOfLength:theSettings.IVSize]
encryptionSalt:[[self class] randomDataOfLength:theSettings.keySettings.saltSize]
HMACSalt:[[self class] randomDataOfLength:theSettings.HMACKeySettings.saltSize]
handler:aHandler];
}
- (Crypto *)initWithSettings:(RNCryptorSettings)theSettings
password:(NSString *)aPassword
IV:(NSData *)anIV
encryptionSalt:(NSData *)anEncryptionSalt
HMACSalt:(NSData *)anHMACSalt
handler:(RNCryptorHandler)aHandler;
{
NSParameterAssert(aPassword.length > 0); // We'll go forward, but this is undefined behavior for RNCryptor
NSParameterAssert(anIV);
NSParameterAssert(anEncryptionSalt);
NSParameterAssert(anHMACSalt);
NSData *encryptionKey = [[self class] keyForPassword:aPassword salt:anEncryptionSalt settings:theSettings.keySettings];
NSData *HMACKey = [[self class] keyForPassword:aPassword salt:anHMACSalt settings:theSettings.HMACKeySettings];
self = [self initWithSettings:theSettings
encryptionKey:encryptionKey
HMACKey:HMACKey
IV:anIV
handler:aHandler];
if (self) {
self.options |= kRNCryptorOptionHasPassword;
self.encryptionSalt = anEncryptionSalt;
self.HMACSalt = anHMACSalt;
}
return self;
}
- (NSData *)header
{
uint8_t header[2] = {kRNCryptorFileVersion, self.options};
NSMutableData *headerData = [NSMutableData dataWithBytes:header length:sizeof(header)];
if (self.options & kRNCryptorOptionHasPassword) {
[headerData appendData:self.encryptionSalt];
[headerData appendData:self.HMACSalt];
}
[headerData appendData:self.IV];
return headerData;
}
- (void)addEncryptionData:(NSData *)data
{
if (self.isFinished) {
return;
}
dispatch_async(self.queue, ^{
if (!self.haveWrittenHeader) {
NSData *header = [self header];
[self.outData setData:header];
if (self.hasHMAC) {
CCHmacUpdate(&_HMACContext, [header bytes], [header length]);
}
self.haveWrittenHeader = YES;
}
NSError *error = nil;
NSData *encryptedData = [self.engine addData:data error:&error];
if (!encryptedData) {
[self cleanupAndNotifyWithError:error];
}
if (self.hasHMAC) {
CCHmacUpdate(&_HMACContext, encryptedData.bytes, encryptedData.length);
}
[self.outData appendData:encryptedData];
dispatch_sync(self.responseQueue, ^{
self.handler(self, self.outData);
});
[self.outData setLength:0];
});
}
- (void)encryptionFinish
{
if (self.isFinished) {
return;
}
dispatch_async(self.queue, ^{
NSError *error = nil;
NSData *encryptedData = [self.engine finishWithError:&error];
[self.outData appendData:encryptedData];
if (self.hasHMAC) {
CCHmacUpdate(&_HMACContext, encryptedData.bytes, encryptedData.length);
NSMutableData *HMACData = [NSMutableData dataWithLength:self.HMACLength];
CCHmacFinal(&_HMACContext, [HMACData mutableBytes]);
[self.outData appendData:HMACData];
}
[self cleanupAndNotifyWithError:error];
});
}
+ (NSData *)decryptData:(NSData *)theCipherText withSettings:(RNCryptorSettings)settings password:(NSString *)aPassword error:(NSError **)anError
{
Crypto *cryptor = [[self alloc] initWithPassword:aPassword
handler:^(RNCryptor *c, NSData *d) {}];
cryptor.settings = settings;
return [self synchronousResultForCryptor:cryptor data:theCipherText direction:@"decrypt" error:anError];
}
+ (NSData *)decryptData:(NSData *)theCipherText withSettings:(RNCryptorSettings)settings encryptionKey:(NSData *)encryptionKey HMACKey:(NSData *)HMACKey error:(NSError **)anError
{
Crypto *cryptor = [[self alloc] initWithEncryptionKey:encryptionKey
HMACKey:HMACKey
handler:^(RNCryptor *c, NSData *d) {}];
cryptor.settings = settings;
return [self synchronousResultForCryptor:cryptor data:theCipherText direction:@"decrypt" error:anError];
}
+ (NSData *)decryptData:(NSData *)data password:(NSString *)password
{
return [self decryptData: data withPassword: password error: nil];
}
+ (NSData *)decryptData:(NSData *)data password:(NSString *)password error:(NSError **)error
{
return [self decryptData: data withPassword: password error: error];
}
+ (NSData *)decryptData:(NSData *)theCipherText withPassword:(NSString *)aPassword error:(NSError **)anError
{
Crypto *cryptor = [[self alloc] initWithPassword:aPassword
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:theCipherText direction:@"decrypt" error:anError];
}
+ (NSData *)decryptData:(NSData *)theCipherText withEncryptionKey:(NSData *)encryptionKey HMACKey:(NSData *)HMACKey error:(NSError **)anError;
{
Crypto *cryptor = [[self alloc] initWithEncryptionKey:encryptionKey
HMACKey:HMACKey
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:theCipherText direction:@"decrypt" error:anError];
}
- (Crypto *)initWithEncryptionKey:(NSData *)anEncryptionKey HMACKey:(NSData *)anHMACKey handler:(RNCryptorHandler)aHandler
{
self = [super initWithHandler:aHandler];
if (self) {
_encryptionKey = [anEncryptionKey copy];
_HMACKey = [anHMACKey copy];
_settings = kRNCryptorAES256Settings;
}
return self;
}
- (Crypto *)initWithPassword:(NSString *)aPassword handler:(RNCryptorHandler)aHandler
{
NSParameterAssert(aPassword != nil);
self = [self initWithEncryptionKey:nil HMACKey:nil handler:aHandler];
if (self) {
_password = [aPassword copy];
_settings = kRNCryptorAES256Settings;
}
return self;
}
- (NSMutableData *)inData
{
if (!__inData) {
__inData = [NSMutableData data];
}
return __inData;
}
- (void)decryptData:(NSData *)data
{
dispatch_async(self.queue, ^{
if (self.hasHMAC) {
CCHmacUpdate(&_HMACContext, data.bytes, data.length);
}
NSError *error = nil;
NSData *decryptedData = [self.engine addData:data error:&error];
if (!decryptedData) {
[self cleanupAndNotifyWithError:error];
return;
}
[self.outData appendData:decryptedData];
dispatch_sync(self.responseQueue, ^{
self.handler(self, self.outData);
});
[self.outData setLength:0];
});
}
- (void)addDecryptionData:(NSData *)theData
{
if (self.isFinished) {
return;
}
[self.inData appendData:theData];
if (!self.engine) {
[self consumeHeaderFromData:self.inData];
}
if (self.engine) {
NSUInteger HMACLength = self.HMACLength;
if (self.inData.length > HMACLength) {
NSData *data = [self.inData _RNConsumeToIndex:self.inData.length - HMACLength];
[self decryptData:data];
}
}
}
- (BOOL)updateOptionsForPreamble:(NSData *)preamble
{
const uint8_t *bytes = [preamble bytes];
// See http://robnapier.net/blog/rncryptor-hmac-vulnerability-827 for information on the v1 bad HMAC
#ifdef RNCRYPTOR_ALLOW_V1_BAD_HMAC
if (bytes[0] == 1) {
self.options = bytes[1];
self.hasV1HMAC = YES;
return YES;
}
#endif
if (bytes[0] == 2) {
self.options = bytes[1];
RNCryptorSettings settings = self.settings;
settings.keySettings.hasV2Password = YES;
settings.HMACKeySettings.hasV2Password = YES;
self.settings = settings;
return YES;
}
if (bytes[0] == kRNCryptorFileVersion) {
self.options = bytes[1];
return YES;
}
return NO;
}
- (void)consumeHeaderFromData:(NSMutableData *)data
{
if (data.length < kPreambleSize) {
return;
}
if (![self updateOptionsForPreamble:[data subdataWithRange:NSMakeRange(0, kPreambleSize)]]) {
[self cleanupAndNotifyWithError:[NSError errorWithDomain:kRNCryptorErrorDomain
code:kRNCryptorUnknownHeader
userInfo:[NSDictionary dictionaryWithObject:@"Unknown header" /* DNL */
forKey:NSLocalizedDescriptionKey]]];
return;
}
NSUInteger headerSize = kPreambleSize + self.settings.IVSize;
if (self.options & kRNCryptorOptionHasPassword) {
headerSize += self.settings.keySettings.saltSize + self.settings.HMACKeySettings.saltSize;
}
if (data.length < headerSize) {
return;
}
NSData *header = [data subdataWithRange:NSMakeRange(0, headerSize)]; // We'll need this for the HMAC later
[[data _RNConsumeToIndex:kPreambleSize] mutableCopy]; // Throw away the preamble
NSError *error = nil;
if (self.options & kRNCryptorOptionHasPassword) {
NSAssert(!self.encryptionKey && !self.HMACKey, @"Both password and the key (%d) or HMACKey (%d) are set.", self.encryptionKey != nil, self.HMACKey != nil);
NSData *encryptionKeySalt = [data _RNConsumeToIndex:self.settings.keySettings.saltSize];
NSData *HMACKeySalt = [data _RNConsumeToIndex:self.settings.HMACKeySettings.saltSize];
self.encryptionKey = [[self class] keyForPassword:self.password salt:encryptionKeySalt settings:self.settings.keySettings];
self.HMACKey = [[self class] keyForPassword:self.password salt:HMACKeySalt settings:self.settings.HMACKeySettings];
self.password = nil; // Don't need this anymore.
}
NSData *IV = [data _RNConsumeToIndex:self.settings.IVSize];
self.engine = [[Crypto alloc] initWithOperation:kCCDecrypt settings:self.settings key:self.encryptionKey IV:IV error:&error];
self.encryptionKey = nil; // Don't need this anymore
if (!self.engine) {
[self cleanupAndNotifyWithError:error];
return;
}
if (self.HMACKey) {
CCHmacInit(&_HMACContext, self.settings.HMACAlgorithm, self.HMACKey.bytes, self.HMACKey.length);
self.HMACLength = self.settings.HMACLength;
self.HMACKey = nil; // Don't need this anymore
if (! self.hasV1HMAC) {
CCHmacUpdate(&_HMACContext, [header bytes], [header length]);
}
}
}
- (void)decryptionFinish
{
if (self.isFinished) {
return;
}
dispatch_async(self.queue, ^{
NSError *error = nil;
NSData *decryptedData = [self.engine finishWithError:&error];
if (!decryptedData) {
[self cleanupAndNotifyWithError:error];
return;
}
[self.outData appendData:decryptedData];
if (self.hasHMAC) {
NSMutableData *HMACData = [NSMutableData dataWithLength:self.HMACLength];
CCHmacFinal(&_HMACContext, [HMACData mutableBytes]);
if (![HMACData rnc_isEqualInConsistentTime:self.inData]) {
[self cleanupAndNotifyWithError:[NSError errorWithDomain:kRNCryptorErrorDomain
code:kRNCryptorHMACMismatch
userInfo:[NSDictionary dictionaryWithObject:@"HMAC Mismatch" /* DNL */
forKey:NSLocalizedDescriptionKey]]];
return;
}
}
[self cleanupAndNotifyWithError:nil];
});
}
@end