Skip to content

Commit

Permalink
Update Cocoapods.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesmchen-signal committed Aug 7, 2018
1 parent b9d00f4 commit 5da11de
Show file tree
Hide file tree
Showing 96 changed files with 7,802 additions and 27,726 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

@protocol CipherMessage <NSObject>

- (NSData*)serialized;
- (NSData *)serialized;

@end
25 changes: 17 additions & 8 deletions AxolotlKit/AxolotlKit/Classes/CipherMessage/PreKeyWhisperMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@
// Copyright (c) 2014 Frederic Jacobs. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "WhisperMessage.h"
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface PreKeyWhisperMessage : NSObject <CipherMessage>

- (instancetype)initWithData:(NSData *)serialized;

- (instancetype)initWithWhisperMessage:(WhisperMessage*)whisperMessage registrationId:(int)registrationId prekeyId:(int)prekeyId signedPrekeyId:(int)signedPrekeyId baseKey:(NSData*)baseKey identityKey:(NSData*)identityKey;
- (instancetype)initWithWhisperMessage:(WhisperMessage *)whisperMessage
registrationId:(int)registrationId
prekeyId:(int)prekeyId
signedPrekeyId:(int)signedPrekeyId
baseKey:(NSData *)baseKey
identityKey:(NSData *)identityKey;

@property (nonatomic, readonly) int registrationId;
@property (nonatomic, readonly) int version;
@property (nonatomic, readonly) int prekeyID;
@property (nonatomic, readonly) int signedPrekeyId;
@property (nonatomic, readonly) NSData *baseKey;
@property (nonatomic, readonly) NSData *identityKey;
@property (nonatomic, readonly) int registrationId;
@property (nonatomic, readonly) int version;
@property (nonatomic, readonly) int prekeyID;
@property (nonatomic, readonly) int signedPrekeyId;
@property (nonatomic, readonly) NSData *baseKey;
@property (nonatomic, readonly) NSData *identityKey;
@property (nonatomic, readonly) WhisperMessage *message;

@end

NS_ASSUME_NONNULL_END
143 changes: 89 additions & 54 deletions AxolotlKit/AxolotlKit/Classes/CipherMessage/PreKeyWhisperMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,123 @@
// Copyright (c) 2014 Frederic Jacobs. All rights reserved.
//

#import "AxolotlExceptions.h"
#import "PreKeyWhisperMessage.h"
#import "AxolotlExceptions.h"
#import "Constants.h"
#import "WhisperTextProtocol.pb.h"
#import "SerializationUtilities.h"
#import <AxolotlKit/AxolotlKit-Swift.h>

NS_ASSUME_NONNULL_BEGIN

@interface PreKeyWhisperMessage ()

@property (nonatomic, readwrite) NSData *identityKey;
@property (nonatomic, readwrite) NSData *baseKey;
@property (nonatomic, readwrite) NSData *serialized;

@end

#pragma mark -

@implementation PreKeyWhisperMessage

-(instancetype)initWithWhisperMessage:(WhisperMessage*)whisperMessage registrationId:(int)registrationId prekeyId:(int)prekeyId signedPrekeyId:(int)signedPrekeyId baseKey:(NSData*)baseKey identityKey:(NSData*)identityKey{

self = [super init];

if (self) {
_registrationId = registrationId;
_version = whisperMessage.version;
_prekeyID = prekeyId;
_signedPrekeyId = signedPrekeyId;
_baseKey = baseKey;
_identityKey = identityKey;
_message = whisperMessage;
}

TSProtoPreKeyWhisperMessageBuilder *builder = [TSProtoPreKeyWhisperMessage builder];
[builder setSignedPreKeyId:signedPrekeyId];
[builder setBaseKey:baseKey];
[builder setIdentityKey:identityKey];
[builder setMessage:whisperMessage.serialized];
[builder setRegistrationId:registrationId];

if (prekeyId != -1) {
[builder setPreKeyId:prekeyId];
- (instancetype)initWithWhisperMessage:(WhisperMessage *)whisperMessage
registrationId:(int)registrationId
prekeyId:(int)prekeyId
signedPrekeyId:(int)signedPrekeyId
baseKey:(NSData *)baseKey
identityKey:(NSData *)identityKey
{
if (self = [super init]) {
_registrationId = registrationId;
_version = whisperMessage.version;
_prekeyID = prekeyId;
_signedPrekeyId = signedPrekeyId;
_baseKey = baseKey;
_identityKey = identityKey;
_message = whisperMessage;

SPKProtoTSProtoPreKeyWhisperMessageBuilder *messageBuilder = [SPKProtoTSProtoPreKeyWhisperMessageBuilder new];
[messageBuilder setSignedPreKeyID:signedPrekeyId];
[messageBuilder setBaseKey:baseKey];
[messageBuilder setIdentityKey:identityKey];
[messageBuilder setMessage:whisperMessage.serialized];
[messageBuilder setRegistrationID:registrationId];

if (prekeyId != -1) {
[messageBuilder setPreKeyID:prekeyId];
}

Byte versionByte = [SerializationUtilities intsToByteHigh:_version low:CURRENT_VERSION];
NSMutableData *serialized = [NSMutableData dataWithBytes:&versionByte length:1];

NSError *error;
NSData *_Nullable messageData = [messageBuilder buildSerializedDataAndReturnError:&error];
if (!messageData || error) {
SPKFail(@"%@ Could not serialize proto: %@.", self.logTag, error);
@throw [NSException exceptionWithName:InvalidMessageException
reason:@"could not serialize proto"
userInfo:@{}];
}
[serialized appendData:messageData];

_serialized = [serialized copy];
}

Byte versionByte = [SerializationUtilities intsToByteHigh:_version low:CURRENT_VERSION];
NSMutableData *serialized = [NSMutableData dataWithBytes:&versionByte length:1];
NSData *messageBytes = builder.build.data;
[serialized appendData:messageBytes];

_serialized = [NSData dataWithData:serialized];


return self;
}

- (instancetype)initWithData:(NSData*)serialized{
self = [super init];

if (self) {
- (instancetype)initWithData:(NSData *)serialized
{
if (self = [super init]) {
Byte version;
[serialized getBytes:&version length:1];
_version = [SerializationUtilities highBitsToIntFromByte:version];

if (_version > CURRENT_VERSION && _version < MINIMUM_SUPPORTED_VERSION) {
@throw [NSException exceptionWithName:InvalidVersionException reason:@"Unknown version" userInfo:@{@"version":[NSNumber numberWithInt:_version]}];
@throw [NSException exceptionWithName:InvalidVersionException
reason:@"Unknown version"
userInfo:@{ @"version" : [NSNumber numberWithInt:_version] }];
}

NSData *message = [serialized subdataWithRange:NSMakeRange(1, serialized.length-1)];

TSProtoPreKeyWhisperMessage *preKeyWhisperMessage = [TSProtoPreKeyWhisperMessage parseFromData:message];

if (!preKeyWhisperMessage.hasSignedPreKeyId || !preKeyWhisperMessage.hasBaseKey || !preKeyWhisperMessage.hasIdentityKey || !preKeyWhisperMessage.hasMessage) {
@throw [NSException exceptionWithName:InvalidMessageException reason:@"Incomplete Message" userInfo:@{}];

NSData *messageData = [serialized subdataWithRange:NSMakeRange(1, serialized.length - 1)];

NSError *error;
SPKProtoTSProtoPreKeyWhisperMessage *_Nullable preKeyWhisperMessage =
[SPKProtoTSProtoPreKeyWhisperMessage parseData:messageData error:&error];
if (!preKeyWhisperMessage || error) {
SPKFail(@"%@ Could not parse proto: %@.", self.logTag, error);
@throw [NSException exceptionWithName:InvalidMessageException reason:@"Could not parse proto" userInfo:@{}];
}
_serialized = serialized;
_registrationId = preKeyWhisperMessage.registrationId;

_serialized = serialized;
_registrationId = preKeyWhisperMessage.registrationID;

// This method is called when decrypting a received PreKeyMessage, but to be symmetrical with
// encrypting a PreKeyWhisperMessage before sending, we use "-1" to indicate *no* unsignd prekey was
// included.
_prekeyID = preKeyWhisperMessage.hasPreKeyId ? preKeyWhisperMessage.preKeyId : -1;
_signedPrekeyId = preKeyWhisperMessage.signedPreKeyId;
_baseKey = preKeyWhisperMessage.baseKey;
_identityKey = preKeyWhisperMessage.identityKey;
_message = [[WhisperMessage alloc] initWithData:preKeyWhisperMessage.message];
_prekeyID = preKeyWhisperMessage.hasPreKeyID ? preKeyWhisperMessage.preKeyID : -1;
_signedPrekeyId = preKeyWhisperMessage.signedPreKeyID;
_baseKey = preKeyWhisperMessage.baseKey;
_identityKey = preKeyWhisperMessage.identityKey;
_message = [[WhisperMessage alloc] initWithData:preKeyWhisperMessage.message];
}

return self;
}

#pragma mark - Logging

+ (NSString *)logTag
{
return [NSString stringWithFormat:@"[%@]", self.class];
}

- (NSString *)logTag
{
return self.class.logTag;
}

@end

NS_ASSUME_NONNULL_END
40 changes: 27 additions & 13 deletions AxolotlKit/AxolotlKit/Classes/CipherMessage/WhisperMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,38 @@
// Copyright (c) 2014 Frederic Jacobs. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CipherMessage.h"
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class ECKeyPair;

@interface WhisperMessage : NSObject <CipherMessage>

@property (nonatomic, readonly) int version;
@property (nonatomic, readonly) NSData *senderRatchetKey;
@property (nonatomic, readonly) int previousCounter;
@property (nonatomic, readonly) int counter;
@property (nonatomic, readonly) NSData *cipherText;
@property (nonatomic, readonly) NSData *serialized;

- (instancetype)initWithData:(NSData*)serialized;

- (instancetype)initWithVersion:(int)version macKey:(NSData*)macKey senderRatchetKey:(NSData*)senderRatchetKey counter:(int)counter previousCounter:(int)previousCounter cipherText:(NSData*)cipherText senderIdentityKey:(NSData*)senderIdentityKey receiverIdentityKey:(NSData*)receiverIdentityKey;

- (void)verifyMacWithVersion:(int)messageVersion senderIdentityKey:(NSData *)senderIdentityKey receiverIdentityKey:(NSData*)receiverIdentityKey macKey:(NSData *)macKey;
@property (nonatomic, readonly) int version;
@property (nonatomic, readonly) NSData *senderRatchetKey;
@property (nonatomic, readonly) int previousCounter;
@property (nonatomic, readonly) int counter;
@property (nonatomic, readonly) NSData *cipherText;
@property (nonatomic, readonly) NSData *serialized;

- (instancetype)initWithData:(NSData *)serialized;

- (instancetype)initWithVersion:(int)version
macKey:(NSData *)macKey
senderRatchetKey:(NSData *)senderRatchetKey
counter:(int)counter
previousCounter:(int)previousCounter
cipherText:(NSData *)cipherText
senderIdentityKey:(NSData *)senderIdentityKey
receiverIdentityKey:(NSData *)receiverIdentityKey;

- (void)verifyMacWithVersion:(int)messageVersion
senderIdentityKey:(NSData *)senderIdentityKey
receiverIdentityKey:(NSData *)receiverIdentityKey
macKey:(NSData *)macKey;

@end

NS_ASSUME_NONNULL_END

0 comments on commit 5da11de

Please sign in to comment.