]> git.saurik.com Git - apple/security.git/blob - KeychainCircle/Tests/KCSRPTests.m
Security-59306.41.2.tar.gz
[apple/security.git] / KeychainCircle / Tests / KCSRPTests.m
1 //
2 // KeychainCircleTests.m
3 // KeychainCircleTests
4 //
5 //
6
7 #import <XCTest/XCTest.h>
8
9 #import "KCSRPContext.h"
10 #include <corecrypto/ccrng.h>
11 #include <corecrypto/ccsha2.h>
12 #include <corecrypto/ccdh_gp.h>
13 #include <CommonCrypto/CommonRandomSPI.h>
14
15 @interface KCSRPTests : XCTestCase
16
17 @end
18
19 @implementation KCSRPTests
20
21 - (void)setUp {
22 [super setUp];
23 // Put setup code here. This method is called before the invocation of each test method in the class.
24 }
25
26 - (void)tearDown {
27 // Put teardown code here. This method is called after the invocation of each test method in the class.
28 [super tearDown];
29 }
30
31 - (void) negotiateWithUser: (NSString*) user
32 digestInfo: (const struct ccdigest_info*) di
33 group: (ccsrp_const_gp_t) group
34 randomSource: (struct ccrng_state *) rng {
35
36 NSString* password = @"TryMeAs a ΓΌ password, sucka";
37
38 KCSRPClientContext * client = [[KCSRPClientContext alloc] initWithUser: user
39 digestInfo: di
40 group: group
41 randomSource: rng];
42 XCTAssert([client getKey] == NULL, @"No key yet");
43 XCTAssert(![client isAuthenticated], @"Not yet authenticated");
44
45 XCTAssert(client, @"No KCSRPClientContext created");
46
47 KCSRPServerContext * server = [[KCSRPServerContext alloc] initWithUser:user
48 password:password
49 digestInfo:di
50 group:group
51 randomSource:rng];
52
53
54 XCTAssert(server, @"No KCSRPServerContext created");
55
56 XCTAssert([server getKey] == NULL, @"No key yet");
57
58 NSError* error = nil;
59
60 NSData* A_data = [client copyStart:&error];
61 XCTAssert(A_data, @"copied start failed (%@)", error);
62 error = nil;
63
64 XCTAssert([client getKey] == NULL, @"Shouldn't have key");
65 XCTAssert(![client isAuthenticated], @"Not yet authenticated");
66
67 NSData* B_data = [server copyChallengeFor:A_data error: &error];
68 XCTAssert(B_data, @"Copied challenge for start failed (%@)", error);
69 error = nil;
70
71 XCTAssert([server getKey] != NULL, @"Should have key");
72 XCTAssert(![server isAuthenticated], @"Not yet authenticated");
73
74 NSData* M_data = [client copyResposeToChallenge:B_data
75 password:password
76 salt:server.salt
77 error:&error];
78 XCTAssert(M_data, @"Copied responseToChallenge failed (%@)", error);
79 error = nil;
80
81 XCTAssert([client getKey] != NULL, @"Don't have key");
82 XCTAssert(![client isAuthenticated], @"Not yet authenticated");
83
84 NSData* HAMK_data = [server copyConfirmationFor:M_data error:&error];
85 XCTAssert(HAMK_data, @"Copied confirmation failed (%@)", error);
86 error = nil;
87
88 XCTAssert([server getKey] != NULL, @"Don't have key");
89 XCTAssert([server isAuthenticated], @"Not yet authenticated");
90
91 bool verified = [client verifyConfirmation:HAMK_data error:&error];
92 XCTAssert(verified, @"Verification failed (%@)", error);
93 error = nil;
94
95 XCTAssert([client getKey] != NULL, @"Don't have key");
96 XCTAssert([client isAuthenticated], @"Should be authenticated");
97
98
99 }
100
101 - (void)testNegotiation {
102 [self negotiateWithUser: @"TestUser"
103 digestInfo: ccsha256_di()
104 group: ccsrp_gp_rfc5054_3072()
105 randomSource: ccDRBGGetRngState()];
106 }
107
108 @end