olm/xcode/OLMKit/OLMUtility.m

122 lines
3.3 KiB
Matlab
Raw Permalink Normal View History

2016-11-17 15:50:23 +01:00
/*
Copyright 2016 Chris Ballinger
Copyright 2016 OpenMarket Ltd
Copyright 2016 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2016-04-09 02:24:41 +02:00
#import "OLMUtility.h"
2016-09-28 16:07:39 +02:00
#include "olm/olm.h"
2016-11-14 16:54:51 +01:00
NSString *const OLMErrorDomain = @"org.matrix.olm";
2016-09-28 16:07:39 +02:00
@interface OLMUtility()
@property (nonatomic) OlmUtility *utility;
@end
2016-04-09 02:24:41 +02:00
@implementation OLMUtility
2016-09-28 16:07:39 +02:00
- (void) dealloc {
olm_clear_utility(_utility);
free(_utility);
}
- (BOOL) initializeUtilityMemory {
size_t utilitySize = olm_utility_size();
_utility = malloc(utilitySize);
NSParameterAssert(_utility != nil);
if (!_utility) {
return NO;
}
_utility = olm_utility(_utility);
NSParameterAssert(_utility != nil);
if (!_utility) {
return NO;
}
return YES;
}
- (instancetype) init {
self = [super init];
if (!self) {
return nil;
}
BOOL success = [self initializeUtilityMemory];
if (!success) {
return nil;
}
return self;
}
2016-10-17 15:47:52 +02:00
- (NSString *)sha256:(NSData *)message {
size_t length = olm_sha256_length(_utility);
NSMutableData *shaData = [NSMutableData dataWithLength:length];
if (!shaData) {
return nil;
}
size_t result = olm_sha256(_utility, message.bytes, message.length, shaData.mutableBytes, shaData.length);
if (result == olm_error()) {
const char *error = olm_utility_last_error(_utility);
2016-11-14 16:54:51 +01:00
NSLog(@"olm_sha256 error: %s", error);
2016-10-17 15:47:52 +02:00
return nil;
}
NSString *sha = [[NSString alloc] initWithData:shaData encoding:NSUTF8StringEncoding];
return sha;
}
2016-10-14 15:57:12 +02:00
- (BOOL)verifyEd25519Signature:(NSString*)signature key:(NSString*)key message:(NSData*)message error:(NSError**)error {
2016-09-28 16:07:39 +02:00
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
NSData *signatureData = [signature dataUsingEncoding:NSUTF8StringEncoding];
size_t result = olm_ed25519_verify(_utility,
keyData.bytes, keyData.length,
2016-10-14 15:57:12 +02:00
message.bytes, message.length,
2016-11-14 17:06:34 +01:00
(void*)signatureData.bytes, signatureData.length
2016-09-28 16:07:39 +02:00
);
2016-10-17 15:47:52 +02:00
if (result == olm_error()) {
2016-09-28 16:07:39 +02:00
if (error) {
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: [NSString stringWithUTF8String:olm_utility_last_error(_utility)]};
// @TODO
*error = [[NSError alloc] initWithDomain:@"OLMKitErrorDomain" code:0 userInfo:userInfo];
}
return NO;
}
else {
return YES;
}
}
2016-04-09 23:00:30 +02:00
+ (NSMutableData*) randomBytesOfLength:(NSUInteger)length {
NSMutableData *randomData = [NSMutableData dataWithLength:length];
if (!randomData) {
2016-04-09 02:24:41 +02:00
return nil;
}
2016-04-09 23:00:30 +02:00
int result = SecRandomCopyBytes(kSecRandomDefault, randomData.length, randomData.mutableBytes);
2016-04-09 02:24:41 +02:00
if (result != 0) {
return nil;
}
2016-04-09 23:00:30 +02:00
return randomData;
2016-04-09 02:24:41 +02:00
}
@end