OLMKit: SAS: Added macLongKdf support

(cherry picked from commit 934d516eb35c488ee197e1bab78a4c81e3c8241d)
This commit is contained in:
manuroe 2019-04-08 18:48:09 +02:00
parent 5de295da3e
commit 4057f59453
3 changed files with 63 additions and 1 deletions

View file

@ -52,7 +52,18 @@ NS_ASSUME_NONNULL_BEGIN
@param error the error if any.
@return the MAC.
*/
- (NSString *)calculateMac:(NSString*)input info:(NSString*)info error:(NSError* _Nullable *)error; // TODO: NSError?
- (NSString *)calculateMac:(NSString*)input info:(NSString*)info error:(NSError* _Nullable *)error;
/**
Generate a message authentication code (MAC) based on the shared secret.
For compatibility with an old version of olm.js.
@param input the message to produce the authentication code for.
@param info extra information to mix in when generating the MAC, as per the Matrix spec.
@param error the error if any.
@return the MAC.
*/
- (NSString *)calculateMacLongKdf:(NSString*)input info:(NSString*)info error:(NSError* _Nullable *)error;
@end

View file

@ -137,4 +137,38 @@
return mac;
}
- (NSString *)calculateMacLongKdf:(NSString *)input info:(NSString *)info error:(NSError *__autoreleasing _Nullable *)error {
NSMutableData *inputData = [input dataUsingEncoding:NSUTF8StringEncoding].mutableCopy;
NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding];
size_t macLength = olm_sas_mac_length(olmSAS);
NSMutableData *macData = [NSMutableData dataWithLength:macLength];
if (!macData) {
return nil;
}
size_t result = olm_sas_calculate_mac_long_kdf(olmSAS,
inputData.mutableBytes, inputData.length,
infoData.bytes, infoData.length,
macData.mutableBytes, macLength);
if (result == olm_error()) {
const char *olm_error = olm_sas_last_error(olmSAS);
NSLog(@"[OLMSAS] calculateMacLongKdf: olm_sas_calculate_mac error: %s", olm_error);
NSString *errorString = [NSString stringWithUTF8String:olm_error];
if (error && olm_error && errorString) {
*error = [NSError errorWithDomain:OLMErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey: errorString,
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_sas_calculate_mac_long_kdf error: %@", errorString]
}];
}
return nil;
}
NSString *mac = [[NSString alloc] initWithData:macData encoding:NSUTF8StringEncoding];
return mac;
}
@end

View file

@ -66,4 +66,21 @@
XCTAssertNil(bobError);
}
- (void)testMACLongKdfsMatch {
[alice setTheirPublicKey:bob.publicKey];
[bob setTheirPublicKey:alice.publicKey];
NSString *string = @"test";
NSString *info = @"MAC";
NSError *aliceError, *bobError;
XCTAssertEqualObjects([alice calculateMacLongKdf:string info:info error:&aliceError],
[bob calculateMacLongKdf:string info:info error:&bobError]);
XCTAssertNotEqualObjects([alice calculateMacLongKdf:string info:info error:&aliceError],
[bob calculateMac:string info:info error:&bobError]);
XCTAssertNil(aliceError);
XCTAssertNil(bobError);
}
@end