Clean up formatting.

This commit is contained in:
Matthew Chen 2018-07-27 10:36:28 -04:00
parent 337cf5dd53
commit 516d47b426
4 changed files with 96 additions and 100 deletions

15
.clang-format Normal file
View File

@ -0,0 +1,15 @@
---
BasedOnStyle: WebKit
AllowShortFunctionsOnASingleLine: false
BinPackArguments: false
BinPackParameters: false
ColumnLimit: 120
IndentCaseLabels: true
MaxEmptyLinesToKeep: 2
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 8
UseTab: Never
...

View File

@ -13,121 +13,110 @@ NSString * const TSECKeyPairPreKeyId = @"TSECKeyPairPreKeyId";
extern void curve25519_donna(unsigned char *output, const unsigned char *a, const unsigned char *b);
extern int curve25519_sign(unsigned char* signature_out, /* 64 bytes */
const unsigned char* curve25519_privkey, /* 32 bytes */
const unsigned char* msg, const unsigned long msg_len,
const unsigned char* random); /* 64 bytes */
extern int curve25519_sign(unsigned char *signature_out, /* 64 bytes */
const unsigned char *curve25519_privkey, /* 32 bytes */
const unsigned char *msg,
const unsigned long msg_len,
const unsigned char *random); /* 64 bytes */
@implementation ECKeyPair
+ (BOOL)supportsSecureCoding {
return YES;
return YES;
}
-(void)encodeWithCoder:(NSCoder *)coder {
[coder encodeBytes:self.publicKey.bytes
length:ECCKeyLength
forKey:TSECKeyPairPublicKey];
[coder encodeBytes:self.privateKey.bytes
length:ECCKeyLength
forKey:TSECKeyPairPrivateKey];
[coder encodeBytes:self.publicKey.bytes length:ECCKeyLength forKey:TSECKeyPairPublicKey];
[coder encodeBytes:self.privateKey.bytes length:ECCKeyLength forKey:TSECKeyPairPrivateKey];
}
- (nullable id)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
NSUInteger returnedLength = 0;
const uint8_t *returnedBuffer = NULL;
// De-serialize public key
returnedBuffer = [coder decodeBytesForKey:TSECKeyPairPublicKey
returnedLength:&returnedLength];
if (returnedLength != ECCKeyLength) {
return nil;
}
_publicKey = [NSData dataWithBytes:returnedBuffer length:returnedLength];
self = [super init];
if (self) {
NSUInteger returnedLength = 0;
const uint8_t *returnedBuffer = NULL;
// De-serialize public key
returnedBuffer = [coder decodeBytesForKey:TSECKeyPairPublicKey returnedLength:&returnedLength];
if (returnedLength != ECCKeyLength) {
return nil;
}
_publicKey = [NSData dataWithBytes:returnedBuffer length:returnedLength];
// De-serialize private key
returnedBuffer = [coder decodeBytesForKey:TSECKeyPairPrivateKey
returnedLength:&returnedLength];
if (returnedLength != ECCKeyLength) {
return nil;
// De-serialize private key
returnedBuffer = [coder decodeBytesForKey:TSECKeyPairPrivateKey returnedLength:&returnedLength];
if (returnedLength != ECCKeyLength) {
return nil;
}
_privateKey = [NSData dataWithBytes:returnedBuffer length:returnedLength];
}
_privateKey = [NSData dataWithBytes:returnedBuffer length:returnedLength];
}
return self;
return self;
}
- (nullable id)initWithPublicKey:(NSData *)publicKey
privateKey:(NSData *)privateKey {
if (self = [super init]) {
_publicKey = publicKey;
_privateKey = privateKey;
}
return self;
if (self = [super init]) {
_publicKey = publicKey;
_privateKey = privateKey;
}
return self;
}
+ (ECKeyPair *)generateKeyPair {
// Generate key pair as described in
// https://code.google.com/p/curve25519-donna/
NSMutableData *privateKey =
[[Randomness generateRandomBytes:ECCKeyLength] mutableCopy];
uint8_t *privateKeyBytes = privateKey.mutableBytes;
privateKeyBytes[0] &= 248;
privateKeyBytes[31] &= 127;
privateKeyBytes[31] |= 64;
// Generate key pair as described in
// https://code.google.com/p/curve25519-donna/
NSMutableData *privateKey = [[Randomness generateRandomBytes:ECCKeyLength] mutableCopy];
uint8_t *privateKeyBytes = privateKey.mutableBytes;
privateKeyBytes[0] &= 248;
privateKeyBytes[31] &= 127;
privateKeyBytes[31] |= 64;
static const uint8_t basepoint[ECCKeyLength] = {9};
static const uint8_t basepoint[ECCKeyLength] = { 9 };
NSMutableData *publicKey = [NSMutableData new];
publicKey.length = ECCKeyLength;
NSMutableData *publicKey = [NSMutableData new];
publicKey.length = ECCKeyLength;
curve25519_donna(publicKey.mutableBytes, privateKey.mutableBytes, basepoint);
curve25519_donna(publicKey.mutableBytes, privateKey.mutableBytes, basepoint);
return [[ECKeyPair alloc] initWithPublicKey:[publicKey copy]
privateKey:[privateKey copy]];
return [[ECKeyPair alloc] initWithPublicKey:[publicKey copy] privateKey:[privateKey copy]];
}
- (NSData *)sign:(NSData *)data {
NSMutableData *signatureData =
[NSMutableData dataWithLength:ECCSignatureLength];
if (!signatureData) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"Could not allocate buffer"
userInfo:nil];
}
NSMutableData *signatureData = [NSMutableData dataWithLength:ECCSignatureLength];
if (!signatureData) {
@throw
[NSException exceptionWithName:NSInvalidArgumentException reason:@"Could not allocate buffer" userInfo:nil];
}
NSData *randomBytes = [Randomness generateRandomBytes:64];
NSData *randomBytes = [Randomness generateRandomBytes:64];
if (curve25519_sign(signatureData.mutableBytes, self.privateKey.bytes,
[data bytes], [data length], [randomBytes bytes]) == -1) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Message couldn't be signed."
userInfo:nil];
}
if (curve25519_sign(
signatureData.mutableBytes, self.privateKey.bytes, [data bytes], [data length], [randomBytes bytes])
== -1) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Message couldn't be signed."
userInfo:nil];
}
return [signatureData copy];
return [signatureData copy];
}
- (NSData *)generateSharedSecretFromPublicKey:(NSData *)theirPublicKey {
if ([theirPublicKey length] != ECCKeyLength) {
@throw [NSException
exceptionWithName:NSInvalidArgumentException
reason:@"The supplied public key does not contain 32 bytes"
userInfo:nil];
}
if ([theirPublicKey length] != ECCKeyLength) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"The supplied public key does not contain 32 bytes"
userInfo:nil];
}
NSMutableData *sharedSecretData = [NSMutableData dataWithLength:32];
if (!sharedSecretData) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"Could not allocate buffer"
userInfo:nil];
}
NSMutableData *sharedSecretData = [NSMutableData dataWithLength:32];
if (!sharedSecretData) {
@throw
[NSException exceptionWithName:NSInvalidArgumentException reason:@"Could not allocate buffer" userInfo:nil];
}
curve25519_donna(sharedSecretData.mutableBytes, self.privateKey.bytes,
[theirPublicKey bytes]);
curve25519_donna(sharedSecretData.mutableBytes, self.privateKey.bytes, [theirPublicKey bytes]);
return [sharedSecretData copy];
return [sharedSecretData copy];
}
@end
@ -137,12 +126,12 @@ extern int curve25519_sign(unsigned char* signature_out, /* 64 bytes */
@implementation Curve25519
+ (ECKeyPair *)generateKeyPair {
return [ECKeyPair generateKeyPair];
return [ECKeyPair generateKeyPair];
}
+ (NSData *)generateSharedSecretFromPublicKey:(NSData *)theirPublicKey
andKeyPair:(ECKeyPair *)keyPair {
return [keyPair generateSharedSecretFromPublicKey:theirPublicKey];
return [keyPair generateSharedSecretFromPublicKey:theirPublicKey];
}
@end

View File

@ -35,9 +35,7 @@ curve25519_verify(const unsigned char *signature, /* 64 bytes */
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Data needs to be at least one byte" userInfo:nil];
}
if ([data length] >= ULONG_MAX) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"Data is too long"
userInfo:nil];
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Data is too long" userInfo:nil];
}
if ([pubKey length] != ECCKeyLength) {

View File

@ -7,22 +7,16 @@
@implementation Randomness
+ (NSData *)generateRandomBytes:(int)numberBytes {
NSMutableData *_Nullable randomBytes =
[NSMutableData dataWithLength:numberBytes];
if (!randomBytes) {
@throw [NSException exceptionWithName:@"random problem"
reason:@"problem generating the random "
userInfo:nil];
}
int err = 0;
err = SecRandomCopyBytes(kSecRandomDefault, numberBytes,
[randomBytes mutableBytes]);
if (err != noErr && randomBytes.length != numberBytes) {
@throw [NSException exceptionWithName:@"random problem"
reason:@"problem generating the random "
userInfo:nil];
}
return [randomBytes copy];
NSMutableData *_Nullable randomBytes = [NSMutableData dataWithLength:numberBytes];
if (!randomBytes) {
@throw [NSException exceptionWithName:@"random problem" reason:@"problem generating the random " userInfo:nil];
}
int err = 0;
err = SecRandomCopyBytes(kSecRandomDefault, numberBytes, [randomBytes mutableBytes]);
if (err != noErr && randomBytes.length != numberBytes) {
@throw [NSException exceptionWithName:@"random problem" reason:@"problem generating the random " userInfo:nil];
}
return [randomBytes copy];
}
@end