]> git.saurik.com Git - apple/security.git/blob - KeychainCircle/Tests/KCAESGCMTest.m
Security-59754.41.1.tar.gz
[apple/security.git] / KeychainCircle / Tests / KCAESGCMTest.m
1 //
2 // KCAESGCMTest.m
3 // Keychain Circle
4 //
5 //
6
7 #import <XCTest/XCTest.h>
8
9 #import <Foundation/Foundation.h>
10 #import <KeychainCircle/KCAESGCMDuplexSession.h>
11 #import <Foundation/NSKeyedArchiver_Private.h>
12
13 @interface KCAESGCMTest : XCTestCase
14
15 @end
16
17 @implementation KCAESGCMTest
18
19 - (void)setUp {
20 [super setUp];
21 // Put setup code here. This method is called before the invocation of each test method in the class.
22 }
23
24 - (void)tearDown {
25 // Put teardown code here. This method is called after the invocation of each test method in the class.
26 [super tearDown];
27 }
28
29 - (void) sendMessage: (NSData*) message
30 from: (KCAESGCMDuplexSession*) sender
31 to: (KCAESGCMDuplexSession*) receiver {
32 NSError* error = nil;
33 NSData* sendToRecv = [sender encrypt:message error:&error];
34
35 XCTAssertNil(error, @"Got error");
36 XCTAssertNotNil(sendToRecv, @"Failed to get data");
37
38 error = nil;
39 NSData* decryptedSendToRecv = [receiver decryptAndVerify:sendToRecv error:&error];
40
41 XCTAssertNil(error, @"Error decrypting");
42 XCTAssertNotNil(decryptedSendToRecv, @"Got decryption");
43
44 XCTAssertEqualObjects(message, decryptedSendToRecv, @"Send to recv failed.");
45 }
46
47 - (void)testAESGCMDuplex {
48 uint64_t context = 0x81FC134000123041;
49 uint8_t secretBytes[] = { 0x11, 0x22, 0x33, 0x13, 0x44, 0xF1, 0x13, 0x92, 0x11, 0x22, 0x33, 0x13, 0x44, 0xF1, 0x13, 0x92 };
50 NSData* secret = [NSData dataWithBytes:secretBytes length:sizeof(secretBytes)];
51
52 KCAESGCMDuplexSession* sender = [KCAESGCMDuplexSession sessionAsSender:secret
53 context:context];
54
55 KCAESGCMDuplexSession* receiver = [KCAESGCMDuplexSession sessionAsReceiver:secret
56 context:context];
57
58 uint8_t sendToRecvBuffer[] = { 0x1, 0x2, 0x3, 0x88, 0xFF, 0xE1 };
59 NSData* sendToRecvData = [NSData dataWithBytes:sendToRecvBuffer length:sizeof(sendToRecvBuffer)];
60
61 [self sendMessage:sendToRecvData from:sender to:receiver];
62
63 uint8_t recvToSendBuffer[] = { 0x81, 0x52, 0x63, 0x88, 0xFF, 0xE1 };
64 NSData* recvToSendData = [NSData dataWithBytes:recvToSendBuffer length:sizeof(recvToSendBuffer)];
65
66 [self sendMessage:recvToSendData from:receiver to:sender];
67 }
68
69 - (KCAESGCMDuplexSession*) archiveDearchive: (KCAESGCMDuplexSession*) original {
70 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
71 [archiver encodeObject:original forKey:@"Top"];
72
73 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:archiver.encodedData error:nil];
74
75 // Customize the unarchiver.
76 KCAESGCMDuplexSession *result = [unarchiver decodeObjectForKey:@"Top"];
77 [unarchiver finishDecoding];
78
79 return result;
80 }
81
82 - (void)doAESGCMDuplexCodingFlattenSender: (bool) flattenSender
83 Receiver: (bool) flattenReceiver {
84 uint64_t context = 0x81FC134000123041;
85 uint8_t secretBytes[] = { 0x73, 0xb7, 0x7f, 0xff, 0x7f, 0xe3, 0x44, 0x6b, 0xa4, 0xec, 0x9d, 0x5d, 0x68, 0x12, 0x13, 0x71 };
86 NSData* secret = [NSData dataWithBytes:secretBytes length:sizeof(secretBytes)];
87
88 KCAESGCMDuplexSession* sender = [KCAESGCMDuplexSession sessionAsSender:secret
89 context:context];
90
91 KCAESGCMDuplexSession* receiver = [KCAESGCMDuplexSession sessionAsReceiver:secret
92 context:context];
93
94 {
95 uint8_t sendToRecvBuffer[] = { 0x0e, 0x9b, 0x9d, 0x2c, 0x90, 0x96, 0x8a };
96 NSData* sendToRecvData = [NSData dataWithBytes:sendToRecvBuffer length:sizeof(sendToRecvBuffer)];
97
98 [self sendMessage:sendToRecvData from:sender to:receiver];
99
100
101 uint8_t recvToSendBuffer[] = { 0x9b, 0x63, 0xaf, 0xb5, 0x4d, 0xa0, 0xfa, 0x9d, 0x90 };
102 NSData* recvToSendData = [NSData dataWithBytes:recvToSendBuffer length:sizeof(recvToSendBuffer)];
103
104 [self sendMessage:recvToSendData from:receiver to:sender];
105 }
106
107 // Re-encode...
108 if (flattenSender) {
109 sender = [self archiveDearchive:sender];
110 }
111
112 if (flattenReceiver) {
113 receiver = [self archiveDearchive:receiver];
114 }
115
116 {
117 uint8_t sendToRecvBuffer[] = { 0xae, 0xee, 0x5f, 0x62, 0xb2, 0x72, 0x6f, 0x0a, 0xb6, 0x56 };
118 NSData* sendToRecvData = [NSData dataWithBytes:sendToRecvBuffer length:sizeof(sendToRecvBuffer)];
119
120 [self sendMessage:sendToRecvData from:sender to:receiver];
121
122
123 uint8_t recvToSendBuffer[] = { 0x49, 0x0b, 0xbb, 0x2d, 0x20, 0xb1, 0x8a, 0xfc, 0xba, 0xd1, 0xFF };
124 NSData* recvToSendData = [NSData dataWithBytes:recvToSendBuffer length:sizeof(recvToSendBuffer)];
125
126 [self sendMessage:recvToSendData from:receiver to:sender];
127 }
128 }
129
130 - (void)testAESGCMDuplexCoding {
131 [self doAESGCMDuplexCodingFlattenSender:NO Receiver:YES];
132 [self doAESGCMDuplexCodingFlattenSender:YES Receiver:NO];
133 [self doAESGCMDuplexCodingFlattenSender:YES Receiver:YES];
134 }
135
136
137 @end