8 #import <Foundation/Foundation.h>
9 #import "KCSRPContext.h"
13 #include <corecrypto/ccsrp.h>
14 #include <corecrypto/ccsha2.h>
15 #include <corecrypto/ccdh_gp.h>
16 #include <corecrypto/ccder.h>
18 #import "NSError+KCCreationHelpers.h"
20 static const NSStringEncoding srpStringEncoding = NSUTF8StringEncoding;
22 @interface KCSRPContext ()
23 @property (readwrite) struct ccsrp_ctx* context;
24 @property (readwrite) struct ccrng_state *rng;
25 @property (readwrite) NSString* user;
28 @implementation KCSRPContext
30 + (KCSRPContext*) createWithUser: (NSString*) user
31 digestInfo: (const struct ccdigest_info *) di
32 group: (ccsrp_const_gp_t) gp
33 randomSource: (struct ccrng_state *) rng {
34 return [[self alloc] initWithUser:user
40 - (NSData*) dataForPassword: (NSString*) password {
41 return [password dataUsingEncoding:srpStringEncoding];
44 - (nullable const char *) userNameString {
45 return [self.user cStringUsingEncoding:srpStringEncoding];
48 - (instancetype) initWithUser: (NSString*) user
49 digestInfo: (const struct ccdigest_info *) di
50 group: (ccsrp_const_gp_t) gp
51 randomSource: (struct ccrng_state *) rng
53 if ((self = [super init])) {
54 self.context = malloc(ccsrp_sizeof_srp(di, gp));
55 ccsrp_ctx_init(self.context, di, gp);
63 #pragma clang diagnostic push
64 #pragma clang diagnostic ignored "-Wdeprecated-implementations"
66 ccsrp_ctx_clear(ccsrp_ctx_di(self.context),
67 ccsrp_ctx_gp(self.context),
72 #pragma clang diagnostic pop
75 size_t key_length = 0;
76 const void * key = ccsrp_get_session_key(self.context, &key_length);
78 return key ? [NSData dataWithBytesNoCopy:(void *)key length:key_length freeWhenDone:false] : nil;
81 - (bool) isAuthenticated {
82 return ccsrp_is_authenticated(self.context);
88 @implementation KCSRPClientContext
90 - (NSData*) copyStart: (NSError**) error {
91 NSMutableData* A_data = [NSMutableData dataWithLength: ccsrp_exchange_size(self.context)];
93 int result = ccsrp_client_start_authentication(self.context, self.rng, A_data.mutableBytes);
94 if (!CoreCryptoError(result, error, @"Start packet copy failed: %d", result)) {
101 static bool ExactDataSizeRequirement(NSData* data, NSUInteger expectedLength, NSError**error, NSString* name) {
102 return RequirementError(data.length == expectedLength, error, @"%@ incorrect size, Expected %ld, got %ld", name, (unsigned long)expectedLength, (unsigned long)data.length);
105 - (nullable NSData*) copyResposeToChallenge: (NSData*) B_data
106 password: (NSString*) password
108 error: (NSError**) error {
110 if (!ExactDataSizeRequirement(B_data, ccsrp_exchange_size(self.context), error, @"challenge data"))
113 NSMutableData* M_data = [NSMutableData dataWithLength: ccsrp_session_size(self.context)];
114 NSData* passwordData = [self dataForPassword: password];
116 int result = ccsrp_client_process_challenge(self.context,
117 [self userNameString],
123 M_data.mutableBytes);
125 if (!CoreCryptoError(result, error, @"Challenge processing failed: %d", result)) {
132 - (bool) verifyConfirmation: (NSData*) HAMK_data
133 error: (NSError**) error {
134 if (!ExactDataSizeRequirement(HAMK_data, ccsrp_session_size(self.context), error, @"confirmation data"))
137 return ccsrp_client_verify_session(self.context, HAMK_data.bytes);
142 @interface KCSRPServerContext ()
143 @property (readwrite) NSData* verifier;
146 @implementation KCSRPServerContext
148 - (bool) resetWithPassword: (NSString*) password
149 error: (NSError**) error {
150 const int salt_length = 16;
152 NSMutableData* salt = [NSMutableData dataWithLength: salt_length];
153 NSMutableData* verifier = [NSMutableData dataWithLength: ccsrp_ctx_sizeof_n(self.context)];
155 NSData* passwordData = [self dataForPassword: password];
157 int generateResult = ccsrp_generate_salt_and_verification(self.context,
159 [self userNameString],
164 verifier.mutableBytes);
166 if (!CoreCryptoError(generateResult, error, @"Error generating SRP salt/verifier")) {
170 self.verifier = verifier;
176 - (instancetype) initWithUser: (NSString*)user
177 password: (NSString*)password
178 digestInfo: (const struct ccdigest_info *) di
179 group: (ccsrp_const_gp_t) gp
180 randomSource: (struct ccrng_state *) rng {
181 if ((self = [super initWithUser: user
184 randomSource: rng])) {
185 if (![self resetWithPassword:password error:nil]) {
192 - (instancetype) initWithUser: (NSString*) user
194 verifier: (NSData*) verifier
195 digestInfo: (const struct ccdigest_info *) di
196 group: (ccsrp_const_gp_t) gp
197 randomSource: (struct ccrng_state *) rng {
198 if ((self = [super initWithUser: user
201 randomSource: rng])) {
202 self.verifier = verifier;
209 - (NSData*) copyChallengeFor: (NSData*) A_data
210 error: (NSError**) error {
211 if (!ExactDataSizeRequirement(A_data, ccsrp_exchange_size(self.context), error, @"start data"))
214 NSMutableData* B_data = [NSMutableData dataWithLength: ccsrp_exchange_size(self.context)];
216 int result = ccsrp_server_start_authentication(self.context, self.rng,
217 [self userNameString],
218 self.salt.length, self.salt.bytes,
219 self.verifier.bytes, A_data.bytes,
220 B_data.mutableBytes);
222 if (!CoreCryptoError(result, error, @"Server start authentication failed: %d", result)) {
229 - (NSData*) copyConfirmationFor: (NSData*) M_data
230 error: (NSError**) error {
231 if (!ExactDataSizeRequirement(M_data, ccsrp_session_size(self.context), error, @"response data"))
234 NSMutableData* HAMK_data = [NSMutableData dataWithLength: ccsrp_session_size(self.context)];
236 bool verify = ccsrp_server_verify_session(self.context, M_data.bytes, HAMK_data.mutableBytes);
238 if (!CoreCryptoError(!verify, error, @"SRP verification failed")) {