TOMBO source code
Revision | 14dc9092f5137fe53d45a62a229ceb82345ee6e6 (tree) |
---|---|
Time | 2012-04-08 21:39:17 |
Author | Hirami <tomohisa.hirami@nift...> |
Commiter | Hirami |
Implement crypt related features.
Blowfish and CBC methods.
MD5 methods.
@@ -11,6 +11,7 @@ | ||
11 | 11 | 922F592A15295FAA009B1E32 /* EditViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 922F592915295FAA009B1E32 /* EditViewController.m */; }; |
12 | 12 | 9243C7B51529D8610092B506 /* DetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9243C7B41529D8610092B506 /* DetailViewController.m */; }; |
13 | 13 | 9243C7BC152AC7430092B506 /* NewFolderAlert.m in Sources */ = {isa = PBXBuildFile; fileRef = 9243C7BB152AC7430092B506 /* NewFolderAlert.m */; }; |
14 | + 9243C7ED1531ADA80092B506 /* CryptCore.m in Sources */ = {isa = PBXBuildFile; fileRef = 9243C7EC1531ADA80092B506 /* CryptCore.m */; }; | |
14 | 15 | 92DE332D151E277D00AD06EC /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE332C151E277D00AD06EC /* UIKit.framework */; }; |
15 | 16 | 92DE332F151E277D00AD06EC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE332E151E277D00AD06EC /* Foundation.framework */; }; |
16 | 17 | 92DE3331151E277D00AD06EC /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92DE3330151E277D00AD06EC /* CoreGraphics.framework */; }; |
@@ -54,6 +55,8 @@ | ||
54 | 55 | 9243C7B41529D8610092B506 /* DetailViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DetailViewController.m; sourceTree = "<group>"; }; |
55 | 56 | 9243C7BA152AC7430092B506 /* NewFolderAlert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewFolderAlert.h; sourceTree = "<group>"; }; |
56 | 57 | 9243C7BB152AC7430092B506 /* NewFolderAlert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewFolderAlert.m; sourceTree = "<group>"; }; |
58 | + 9243C7EB1531ADA80092B506 /* CryptCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptCore.h; sourceTree = "<group>"; }; | |
59 | + 9243C7EC1531ADA80092B506 /* CryptCore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CryptCore.m; sourceTree = "<group>"; }; | |
57 | 60 | 92DE3328151E277D00AD06EC /* Tombo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tombo.app; sourceTree = BUILT_PRODUCTS_DIR; }; |
58 | 61 | 92DE332C151E277D00AD06EC /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; |
59 | 62 | 92DE332E151E277D00AD06EC /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; |
@@ -157,6 +160,8 @@ | ||
157 | 160 | 92DE3333151E277D00AD06EC /* Supporting Files */, |
158 | 161 | 92DE33841522998700AD06EC /* Storage.h */, |
159 | 162 | 92DE33851522998700AD06EC /* Storage.m */, |
163 | + 9243C7EB1531ADA80092B506 /* CryptCore.h */, | |
164 | + 9243C7EC1531ADA80092B506 /* CryptCore.m */, | |
160 | 165 | 92DE338E1522FFEB00AD06EC /* FileItem.h */, |
161 | 166 | 92DE338F1522FFEB00AD06EC /* FileItem.m */, |
162 | 167 | 9243C7B31529D8610092B506 /* DetailViewController.h */, |
@@ -332,6 +337,7 @@ | ||
332 | 337 | 9228CF2815298F7200E20355 /* EditCancelAlert.m in Sources */, |
333 | 338 | 9243C7B51529D8610092B506 /* DetailViewController.m in Sources */, |
334 | 339 | 9243C7BC152AC7430092B506 /* NewFolderAlert.m in Sources */, |
340 | + 9243C7ED1531ADA80092B506 /* CryptCore.m in Sources */, | |
335 | 341 | ); |
336 | 342 | runOnlyForDeploymentPostprocessing = 0; |
337 | 343 | }; |
@@ -0,0 +1,10 @@ | ||
1 | +#import <Foundation/Foundation.h> | |
2 | + | |
3 | +@interface CryptCore : NSObject | |
4 | + | |
5 | ++ (void)md5:(NSString *)input buffer:(unsigned char*)digest; | |
6 | + | |
7 | ++ (NSString *)bytes2hex:(unsigned char *)data length:(NSUInteger) len; | |
8 | + | |
9 | ++ (BOOL)selftest; | |
10 | +@end |
@@ -0,0 +1,154 @@ | ||
1 | +#import <CommonCrypto/CommonDigest.h> | |
2 | +#import <CommonCrypto/CommonCryptor.h> | |
3 | +#import "CryptCore.h" | |
4 | + | |
5 | +@implementation CryptCore | |
6 | + | |
7 | +// digest size should be CC_MD5_DIGEST_LENGTH | |
8 | ++ (void)md5:(NSString *)input buffer:(unsigned char*)digest { | |
9 | + const char *cStr = [input UTF8String]; | |
10 | + CC_MD5(cStr, strlen(cStr), digest); | |
11 | +} | |
12 | + | |
13 | ++ (NSString *)bytes2hex:(unsigned char *)data length:(NSUInteger) len { | |
14 | + NSMutableString *out = [NSMutableString stringWithCapacity:len]; | |
15 | + for (int i = 0; i < len; i++) { | |
16 | + [out appendFormat:@"%02x", data[i]]; | |
17 | + } | |
18 | + return out; | |
19 | +} | |
20 | + | |
21 | ++ (BOOL)selftest { | |
22 | + unsigned char key1[] = {0x83, 0x45, 0x92, 0x17, 0x68, 0xA9, 0x35, 0xDF, | |
23 | + 0x25, 0x58, 0x9F, 0x11, 0x72, 0x87, 0x99, 0xFE}; | |
24 | + unsigned char dat1[] = {0x85, 0x28, 0xF9, 0x43, 0xE8, 0xC2, 0x12, 0x50}; | |
25 | + | |
26 | + unsigned char enc1[] = {0x87, 0x66, 0x8b, 0xc8, 0xeb, 0xfc, 0xe6, 0xde}; | |
27 | + | |
28 | + unsigned char buf[16]; | |
29 | + if (![CryptCore encrypt:key1 keyLen:16 plain:dat1 dataLen:8 enc:buf]) { | |
30 | + NSLog(@"Test1: Encrypt fail."); | |
31 | + return NO; | |
32 | + } | |
33 | + | |
34 | + if (![CryptCore check:enc1 and:buf length:8]) { | |
35 | + NSLog(@"Test1: Check fail."); | |
36 | + return NO; | |
37 | + } | |
38 | + | |
39 | + unsigned char dat2[] = {0x85, 0x28, 0xF9, 0x43, 0xE8, 0xC2, 0x12, 0x50, | |
40 | + 0x48, 0x02, 0xE1, 0x45, 0x69, 0x3C, 0x8A, 0x9F}; | |
41 | + unsigned char enc2[] = {0xbd, 0xfd, 0x34, 0xa6, 0xb2, 0xa6, 0xed, 0xb9, | |
42 | + 0x3f, 0x23, 0x3f, 0x73, 0x5a, 0x52, 0x09, 0xfb}; | |
43 | + if (![CryptCore encrypt:key1 keyLen:16 plain:dat2 dataLen:16 enc:buf]) { | |
44 | + NSLog(@"Test2: Encrypt fail."); | |
45 | + return NO; | |
46 | + } | |
47 | + if (![CryptCore check:enc2 and:buf length:16]) { | |
48 | + NSLog(@"Test2: Check fail."); | |
49 | + return NO; | |
50 | + } | |
51 | + if (![CryptCore decrypt:key1 keyLen:16 crypt:enc1 dataLen:8 plain:buf]) { | |
52 | + NSLog(@"Test3: Decrypt fail."); | |
53 | + return NO; | |
54 | + } | |
55 | + if (![CryptCore check:dat1 and:buf length:8]) { | |
56 | + NSLog(@"Test3: Check fail."); | |
57 | + return NO; | |
58 | + } | |
59 | + | |
60 | + if (![CryptCore decrypt:key1 keyLen:16 crypt:enc2 dataLen:16 plain:buf]) { | |
61 | + NSLog(@"Test4: Decrypt fail."); | |
62 | + return NO; | |
63 | + } | |
64 | + if (![CryptCore check:dat2 and:buf length:16]) { | |
65 | + NSLog(@"Test4: Check fail."); | |
66 | + return NO; | |
67 | + } | |
68 | + return YES; | |
69 | +} | |
70 | + | |
71 | ++ (BOOL) check:(unsigned char *)c1 and:(unsigned char *)c2 length:(size_t)len { | |
72 | + for (size_t i = 0; i < len; i++) { | |
73 | + if (c1[i] != c2[i]) return NO; | |
74 | + } | |
75 | + return YES; | |
76 | +} | |
77 | + | |
78 | ++ (BOOL)encrypt:(unsigned char*)key keyLen:(size_t)keyLen | |
79 | + plain:(unsigned char*)dat dataLen:(size_t)dataLen | |
80 | + enc:(unsigned char*)enc { | |
81 | + unsigned char inBuffer[8]; | |
82 | + unsigned char buffer[8]; | |
83 | + unsigned char *iv = (unsigned char*)[@"BLOWFISH" UTF8String]; | |
84 | + unsigned char zeroIv[] = {0, 0, 0, 0, 0, 0, 0, 0}; | |
85 | + | |
86 | + unsigned char *vector = iv; | |
87 | + | |
88 | + CCCryptorRef ref; | |
89 | + CCCryptorCreate(kCCEncrypt, kCCAlgorithmBlowfish, 0, | |
90 | + key, keyLen, NULL, &ref); | |
91 | + | |
92 | + size_t numCrypt; | |
93 | + | |
94 | + int j; | |
95 | + int n = dataLen / 8; | |
96 | + | |
97 | + if (n == 1) { | |
98 | + vector = zeroIv; | |
99 | + } | |
100 | + | |
101 | + for (j = 0; j < n; j++) { | |
102 | + for (int i = 0; i < 8; i++) { | |
103 | + inBuffer[i] = dat[i+ j * 8] ^ vector[i]; | |
104 | + } | |
105 | + CCCryptorReset(ref, NULL); | |
106 | + CCCryptorUpdate(ref, inBuffer, 8, buffer, 8, &numCrypt); | |
107 | + memcpy(enc + j * 8, buffer, 8); | |
108 | + vector = buffer; | |
109 | + } | |
110 | + | |
111 | + CCCryptorFinal(ref, buffer, 8, &numCrypt); | |
112 | + CCCryptorRelease(ref); | |
113 | + | |
114 | + return YES; | |
115 | +} | |
116 | + | |
117 | ++ (BOOL)decrypt:(unsigned char*)key keyLen:(size_t)keyLen | |
118 | + crypt:(unsigned char*)crypt dataLen:(size_t)dataLen | |
119 | + plain:(unsigned char*)plain { | |
120 | + | |
121 | + unsigned char buffer[8]; | |
122 | + unsigned char *iv = (unsigned char*)[@"BLOWFISH" UTF8String]; | |
123 | + unsigned char *vector = iv; | |
124 | + | |
125 | + CCCryptorRef ref; | |
126 | + CCCryptorCreate(kCCDecrypt, kCCAlgorithmBlowfish, 0, key, keyLen, NULL, &ref); | |
127 | + | |
128 | + size_t numCrypt; | |
129 | + int j; | |
130 | + int n = dataLen / 8; | |
131 | + | |
132 | + if (n == 1) { | |
133 | + CCCryptorUpdate(ref, crypt, 8, plain, 8, &numCrypt); | |
134 | + CCCryptorFinal(ref, buffer, 8, &numCrypt); | |
135 | + CCCryptorRelease(ref); | |
136 | + return YES; | |
137 | + } | |
138 | + | |
139 | + for (j = 0; j < n; j++) { | |
140 | + CCCryptorReset(ref, NULL); | |
141 | + CCCryptorUpdate(ref, crypt + j * 8, 8, buffer, 8, &numCrypt); | |
142 | + for (int i = 0; i < 8; i++) { | |
143 | + buffer[i] = buffer[i] ^ vector[i]; | |
144 | + } | |
145 | + memcpy(plain + j * 8, buffer, 8); | |
146 | + vector = crypt + j * 8; | |
147 | + } | |
148 | + | |
149 | + CCCryptorFinal(ref, buffer, 8, &numCrypt); | |
150 | + CCCryptorRelease(ref); | |
151 | + | |
152 | + return YES; | |
153 | +} | |
154 | +@end |