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