7 #import <XCTest/XCTest.h>
9 #import <Foundation/Foundation.h>
10 #import <KeychainCircle/KCAESGCMDuplexSession.h>
11 #import <Foundation/NSKeyedArchiver_Private.h>
13 @interface KCAESGCMTest : XCTestCase
17 @implementation KCAESGCMTest
21 // Put setup code here. This method is called before the invocation of each test method in the class.
25 // Put teardown code here. This method is called after the invocation of each test method in the class.
29 - (void) sendMessage: (NSData*) message
30 from: (KCAESGCMDuplexSession*) sender
31 to: (KCAESGCMDuplexSession*) receiver {
33 NSData* sendToRecv = [sender encrypt:message error:&error];
35 XCTAssertNil(error, @"Got error");
36 XCTAssertNotNil(sendToRecv, @"Failed to get data");
39 NSData* decryptedSendToRecv = [receiver decryptAndVerify:sendToRecv error:&error];
41 XCTAssertNil(error, @"Error decrypting");
42 XCTAssertNotNil(decryptedSendToRecv, @"Got decryption");
44 XCTAssertEqualObjects(message, decryptedSendToRecv, @"Send to recv failed.");
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)];
52 KCAESGCMDuplexSession* sender = [KCAESGCMDuplexSession sessionAsSender:secret
55 KCAESGCMDuplexSession* receiver = [KCAESGCMDuplexSession sessionAsReceiver:secret
58 uint8_t sendToRecvBuffer[] = { 0x1, 0x2, 0x3, 0x88, 0xFF, 0xE1 };
59 NSData* sendToRecvData = [NSData dataWithBytes:sendToRecvBuffer length:sizeof(sendToRecvBuffer)];
61 [self sendMessage:sendToRecvData from:sender to:receiver];
63 uint8_t recvToSendBuffer[] = { 0x81, 0x52, 0x63, 0x88, 0xFF, 0xE1 };
64 NSData* recvToSendData = [NSData dataWithBytes:recvToSendBuffer length:sizeof(recvToSendBuffer)];
66 [self sendMessage:recvToSendData from:receiver to:sender];
69 - (KCAESGCMDuplexSession*) archiveDearchive: (KCAESGCMDuplexSession*) original {
70 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
71 [archiver encodeObject:original forKey:@"Top"];
73 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:archiver.encodedData error:nil];
75 // Customize the unarchiver.
76 KCAESGCMDuplexSession *result = [unarchiver decodeObjectForKey:@"Top"];
77 [unarchiver finishDecoding];
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)];
88 KCAESGCMDuplexSession* sender = [KCAESGCMDuplexSession sessionAsSender:secret
91 KCAESGCMDuplexSession* receiver = [KCAESGCMDuplexSession sessionAsReceiver:secret
95 uint8_t sendToRecvBuffer[] = { 0x0e, 0x9b, 0x9d, 0x2c, 0x90, 0x96, 0x8a };
96 NSData* sendToRecvData = [NSData dataWithBytes:sendToRecvBuffer length:sizeof(sendToRecvBuffer)];
98 [self sendMessage:sendToRecvData from:sender to:receiver];
101 uint8_t recvToSendBuffer[] = { 0x9b, 0x63, 0xaf, 0xb5, 0x4d, 0xa0, 0xfa, 0x9d, 0x90 };
102 NSData* recvToSendData = [NSData dataWithBytes:recvToSendBuffer length:sizeof(recvToSendBuffer)];
104 [self sendMessage:recvToSendData from:receiver to:sender];
109 sender = [self archiveDearchive:sender];
112 if (flattenReceiver) {
113 receiver = [self archiveDearchive:receiver];
117 uint8_t sendToRecvBuffer[] = { 0xae, 0xee, 0x5f, 0x62, 0xb2, 0x72, 0x6f, 0x0a, 0xb6, 0x56 };
118 NSData* sendToRecvData = [NSData dataWithBytes:sendToRecvBuffer length:sizeof(sendToRecvBuffer)];
120 [self sendMessage:sendToRecvData from:sender to:receiver];
123 uint8_t recvToSendBuffer[] = { 0x49, 0x0b, 0xbb, 0x2d, 0x20, 0xb1, 0x8a, 0xfc, 0xba, 0xd1, 0xFF };
124 NSData* recvToSendData = [NSData dataWithBytes:recvToSendBuffer length:sizeof(recvToSendBuffer)];
126 [self sendMessage:recvToSendData from:receiver to:sender];
130 - (void)testAESGCMDuplexCoding {
131 [self doAESGCMDuplexCodingFlattenSender:NO Receiver:YES];
132 [self doAESGCMDuplexCodingFlattenSender:YES Receiver:NO];
133 [self doAESGCMDuplexCodingFlattenSender:YES Receiver:YES];