-
Notifications
You must be signed in to change notification settings - Fork 13
/
AESEncryptSource.m
109 lines (87 loc) · 2.14 KB
/
AESEncryptSource.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
//
// AESEncryptSource.m
// KeePass2
//
// Created by Qiang Yu on 2/21/10.
// Copyright 2010 Qiang Yu. All rights reserved.
//
#import "AESEncryptSource.h"
#import "ByteBuffer.h"
@implementation AESEncryptSource
@synthesize _data;
-(id)init:(uint8_t *)keys andIV:(uint8_t *)iv{
if(self=[super init]){
_cryptorRef = nil;
CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keys, kCCKeySizeAES256, iv, &_cryptorRef);
CC_SHA256_Init(&_shaCtx);
}
return self;
}
-(void)dealloc{
CCCryptorRelease(_cryptorRef);
[_data release];
[super dealloc];
}
-(void)setData:(NSMutableData *)data{
if(_data!=data){
[_data release];
_data = [data retain];
_initDataLen = [data length];
}
}
-(void)update:(void *)buffer size:(uint32_t)size{
size_t length = CCCryptorGetOutputLength(_cryptorRef, size, NO);
ByteBuffer * bb = nil;
uint8_t * b = nil;
uint32_t s = 64;
//DLog(@"length-->%d", length);
if(length<=64){
b = _buffer;
}else{
bb = [[ByteBuffer alloc] initWithSize:size+32];
b = bb._bytes;
s = bb._size;
}
CC_SHA256_Update(&_shaCtx, buffer, size);
@try{
size_t movedBytes = 0;
CCCryptorStatus cs;
if(cs=CCCryptorUpdate(_cryptorRef, buffer, size, b, s, &movedBytes)){
@throw [NSException exceptionWithName:@"EncryptError" reason:@"EncryptError" userInfo:nil];
};
[_data appendBytes:b length:movedBytes];
_updatedBytes += size;
}@finally {
[bb release];
}
}
-(void)final{
size_t length = CCCryptorGetOutputLength(_cryptorRef, _updatedBytes, YES);
uint32_t size = length - [_data length] + _initDataLen;
//DLog(@"final length-->%d", size);
ByteBuffer * bb = nil;
uint8_t * b = nil;
uint32_t s = 64;
if(size<=64){
b = _buffer;
}else{
bb = [[ByteBuffer alloc] initWithSize:size];
b = bb._bytes;
s = bb._size;
}
CC_SHA256_Final(_hash, &_shaCtx);
@try{
size_t movedBytes = 0;
CCCryptorStatus cs;
if(cs=CCCryptorFinal(_cryptorRef, b, s, &movedBytes)){
@throw [NSException exceptionWithName:@"EncryptError" reason:@"EncryptError" userInfo:nil];
}
[_data appendBytes:b length:movedBytes];
}@finally {
[bb release];
}
}
-(uint8_t *)getHash{
return _hash;
}
@end