Skip to content

Commit

Permalink
Implement changes to crypto spec.
Browse files Browse the repository at this point in the history
Fixes #264.
  • Loading branch information
tcard committed Feb 29, 2016
1 parent 8992f71 commit c377cae
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 119 deletions.
4 changes: 2 additions & 2 deletions ably-ios/ARTChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)op
_name = name;
self.options = options;
NSError *error;
_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:_options.cipherParams error:&error];
_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:_options.cipher error:&error];
if (error != nil) {
[logger warn:@"creating ARTDataEncoder: %@", error];
_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:nil error:nil];
Expand All @@ -34,7 +34,7 @@ - (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)op

- (void)setOptions:(ARTChannelOptions *)options {
if (!options) {
_options = [[ARTChannelOptions alloc] initEncrypted:false cipherParams:nil];
_options = [[ARTChannelOptions alloc] initWithCipher:nil];
} else {
_options = options;
}
Expand Down
10 changes: 5 additions & 5 deletions ably-ios/ARTChannelOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

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

@class ARTCipherParams;
#import "ARTCrypto.h"

ART_ASSUME_NONNULL_BEGIN

@interface ARTChannelOptions : NSObject

@property (nonatomic, assign) BOOL encrypted;
@property (nonatomic, strong, art_nullable) ARTCipherParams *cipherParams;
@property (nonatomic, strong, art_nullable) ARTCipherParams *cipher;


- (instancetype)initEncrypted:(BOOL)encrypted cipherParams:(ARTCipherParams *__art_nullable)cipherParams;
- (instancetype)initWithCipher:(id<ARTCipherParamsCompatible> __art_nullable)cipherParams;
- (instancetype)initWithCipherKey:(id<ARTCipherKeyCompatible>)key;

@end

Expand Down
9 changes: 6 additions & 3 deletions ably-ios/ARTChannelOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@

@implementation ARTChannelOptions

- (instancetype)initEncrypted:(BOOL)encrypted cipherParams:(ARTCipherParams *)cipherParams {
- (instancetype)initWithCipher:(id<ARTCipherParamsCompatible>)cipherParams {
if (self = [super init]) {
self->_encrypted = encrypted;
self->_cipherParams = cipherParams;
self->_cipher = [cipherParams toCipherParams];
}
return self;
}

- (instancetype)initWithCipherKey:(id<ARTCipherKeyCompatible>)key {
return [self initWithCipher:@{@"key": key}];
}

@end
25 changes: 21 additions & 4 deletions ably-ios/ARTCrypto+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,50 @@
#import "ARTCrypto.h"
#import "ARTLog.h"

ART_ASSUME_NONNULL_BEGIN

@interface ARTCipherParams ()

@property (readonly, strong, nonatomic, art_nullable) NSData *iv;
@property (nonatomic, weak) ARTLog *logger;
- (instancetype)initWithAlgorithm:(NSString *)algorithm key:(NSData *)key keyLength:(NSUInteger)keyLength iv:(NSData *)iv;
- (instancetype)initWithAlgorithm:(NSString *)algorithm key:(id<ARTCipherKeyCompatible>)key iv:(NSData *__art_nullable)iv;

@end

@protocol ARTChannelCipher

- (ARTStatus *)encrypt:(NSData *)plaintext output:(NSData **)output;
- (ARTStatus *)decrypt:(NSData *)ciphertext output:(NSData **)output;
- (ARTStatus *)encrypt:(NSData *)plaintext output:(NSData *__art_nullable*__art_nullable)output;
- (ARTStatus *)decrypt:(NSData *)ciphertext output:(NSData *__art_nullable*__art_nullable)output;
- (NSString *)cipherName;
- (size_t) keyLength;

@end

@interface ARTCbcCipher : NSObject<ARTChannelCipher>

- (id)initWithCipherParams:(ARTCipherParams *)cipherParams;
+ (instancetype)cbcCipherWithParams:(ARTCipherParams *)cipherParams;


@property (nonatomic, weak) ARTLog * logger;
@property (readonly, strong, nonatomic) NSData *keySpec;
@property NSData *iv;
@property (readonly) NSUInteger blockLength;

@end

@interface ARTCrypto ()

+ (NSString *)defaultAlgorithm;
+ (int)defaultKeyLength;
+ (int)defaultBlockLength;

+ (NSData *)generateRandomData:(size_t)length;
+ (NSData *)generateSecureRandomData:(size_t)length;

+ (id<ARTChannelCipher>)cipherWithParams:(ARTCipherParams *)params;

@end

ART_ASSUME_NONNULL_END

#endif /* ARTCrypto_Private_h */
38 changes: 33 additions & 5 deletions ably-ios/ARTCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,49 @@
#import "ARTTypes.h"
#import "ARTStatus.h"

@interface ARTCipherParams : NSObject
ART_ASSUME_NONNULL_BEGIN

@protocol ARTCipherKeyCompatible <NSObject>
- (NSData *)toData;
@end

@interface NSString (ARTCipherKeyCompatible) <ARTCipherKeyCompatible>
- (NSData *)toData;
@end

@interface NSData (ARTCipherKeyCompatible) <ARTCipherKeyCompatible>
- (NSData *)toData;
@end

@class ARTCipherParams;

@protocol ARTCipherParamsCompatible <NSObject>
- (ARTCipherParams *)toCipherParams;
@end

@interface NSDictionary (ARTCipherParamsCompatible) <ARTCipherParamsCompatible>
- (ARTCipherParams *)toCipherParams;
@end

@interface ARTCipherParams : NSObject <ARTCipherParamsCompatible>
@property (readonly, strong, nonatomic) NSString *algorithm;
@property (readonly, strong, nonatomic) NSData *key;
@property (readonly, nonatomic) NSUInteger keyLength;
@property (readonly, strong, nonatomic) NSData *iv;
@property (readonly, getter=getMode) NSString *mode;

- (instancetype)init UNAVAILABLE_ATTRIBUTE;
- (instancetype)initWithAlgorithm:(NSString *)algorithm key:(NSData *)key keyLength:(NSUInteger)keyLength;
- (instancetype)initWithAlgorithm:(NSString *)algorithm key:(id<ARTCipherKeyCompatible>)key;

- (ARTCipherParams *)toCipherParams;

@end

@interface ARTCrypto : NSObject

+ (ARTCipherParams *)getDefaultParams;
+ (ARTCipherParams *)getDefaultParams:(NSData *)key;
+ (ARTCipherParams *)getDefaultParams:(NSDictionary *)values;
+ (NSData *)generateRandomKey;
+ (NSData *)generateRandomKey:(NSUInteger)length;

@end

ART_ASSUME_NONNULL_END
Loading

0 comments on commit c377cae

Please sign in to comment.