Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Crypto, CipherParams and ChannelOptions to spec. #231

Merged
merged 3 commits into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ably-ios/ARTChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
#import "ARTTypes.h"
#import "ARTDataEncoder.h"
#import "ARTLog.h"

@class ARTChannelOptions;
@class ARTMessage;
Expand Down
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 unencrypted];
_options = [[ARTChannelOptions alloc] initWithCipher:nil];
} else {
_options = options;
}
Expand Down
10 changes: 4 additions & 6 deletions ably-ios/ARTChannelOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +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)unencrypted;

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

@end

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

@implementation ARTChannelOptions

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

return self;
}

+ (instancetype)unencrypted {
static id unencrypted;
static dispatch_once_t once;
dispatch_once(&once, ^{
unencrypted = [[ARTChannelOptions alloc] init];
});

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

@end
61 changes: 61 additions & 0 deletions ably-ios/ARTCrypto+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// ARTCrypto+Private.h
// ably
//
// Created by Toni Cárdenas on 19/2/16.
// Copyright © 2016 Ably. All rights reserved.
//

#ifndef ARTCrypto_Private_h
#define ARTCrypto_Private_h

#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:(id<ARTCipherKeyCompatible>)key iv:(NSData *__art_nullable)iv;

@end

@protocol ARTChannelCipher

- (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 *)generateSecureRandomData:(size_t)length;

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

@end

ART_ASSUME_NONNULL_END

#endif /* ARTCrypto_Private_h */
60 changes: 28 additions & 32 deletions ably-ios/ARTCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,51 @@

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

@class ARTLog;
ART_ASSUME_NONNULL_BEGIN

@interface ARTIvParameterSpec : NSObject
@property (nonatomic, weak) ARTLog * logger;
@property (readonly, nonatomic) NSData *iv;
@protocol ARTCipherKeyCompatible <NSObject>
- (NSData *)toData;
@end

- (instancetype)init UNAVAILABLE_ATTRIBUTE;
- (instancetype)initWithIv:(NSData *)iv;
+ (instancetype)ivSpecWithIv:(NSData *)iv;
@interface NSString (ARTCipherKeyCompatible) <ARTCipherKeyCompatible>
- (NSData *)toData;
@end

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

@interface ARTCipherParams : NSObject
@property (nonatomic, weak) ARTLog *logger;
@property (readonly, strong, nonatomic) NSString *algorithm;
@property (readonly, strong, nonatomic) NSData *keySpec;
@property (readonly, strong, nonatomic) ARTIvParameterSpec *ivSpec;
@class ARTCipherParams;

- (instancetype)init UNAVAILABLE_ATTRIBUTE;
- (instancetype)initWithAlgorithm:(NSString *)algorithm keySpec:(NSData *)keySpec ivSpec:(ARTIvParameterSpec *)ivSpec;
+ (instancetype)cipherParamsWithAlgorithm:(NSString *)algorithm keySpec:(NSData *)keySpec ivSpec:(ARTIvParameterSpec *)ivSpec;
@protocol ARTCipherParamsCompatible <NSObject>
- (ARTCipherParams *)toCipherParams;
@end

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

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

- (ARTStatus *)encrypt:(NSData *)plaintext output:(NSData **)output;
- (ARTStatus *)decrypt:(NSData *)ciphertext output:(NSData **)output;
- (NSString *)cipherName;
- (size_t) keyLength;
- (instancetype)init UNAVAILABLE_ATTRIBUTE;
- (instancetype)initWithAlgorithm:(NSString *)algorithm key:(id<ARTCipherKeyCompatible>)key;

- (ARTCipherParams *)toCipherParams;

@end

@interface ARTCrypto : NSObject

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

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

+ (ARTCipherParams *)defaultParams;
+ (ARTCipherParams *)defaultParamsWithKey:(NSData *)key;
+ (ARTCipherParams *)defaultParamsWithKey:(NSData *)key iv:(NSData *)iv;
+ (id<ARTChannelCipher>)cipherWithParams:(ARTCipherParams *)params;
+ (ARTCipherParams *)getDefaultParams:(NSDictionary *)values;
+ (NSData *)generateRandomKey;
+ (NSData *)generateRandomKey:(NSUInteger)length;

@end

ART_ASSUME_NONNULL_END
Loading