Skip to content

Commit

Permalink
Update Themis iOS wrapper for new SecureMessage API (1 Part) (#393)
Browse files Browse the repository at this point in the history
* ios wrapper: use new secure message API

* update iOS wrapper formatting

* more user-friendly errors and logs
  • Loading branch information
vixentael authored Feb 28, 2019
1 parent e0f71df commit 8d8285c
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 245 deletions.
2 changes: 1 addition & 1 deletion src/wrappers/themis/Obj-C/objcthemis/scell.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN

/** @brief store master key
*/
@property (nonatomic, readonly) NSData * key;
@property(nonatomic, readonly) NSData *key;

/** @brief Initialize Secure Cell object
* @param [in] key master key
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/Obj-C/objcthemis/scell.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ @interface TSCell ()

/** @brief store master key, rewrite
*/
@property (nonatomic, readwrite) NSData * key;
@property(nonatomic, readwrite) NSData *key;

@end

Expand Down
8 changes: 4 additions & 4 deletions src/wrappers/themis/Obj-C/objcthemis/scell_context_imprint.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (nullable NSData *)wrapData:(NSData *)message context:(NSData *)context error:
return nil;
}

unsigned char * wrappedMessage = malloc(wrappedMessageLength);
unsigned char *wrappedMessage = malloc(wrappedMessageLength);
if (!wrappedMessage) {
*error = SCERROR(encryptionResult, @"Secure Cell (Context Imprint) encryption failed, not enough memory");
return nil;
Expand All @@ -60,21 +60,21 @@ - (nullable NSData *)unwrapData:(NSData *)message context:(NSData *)context erro
size_t unwrappedMessageLength = 0;

int decryptionResult = themis_secure_cell_decrypt_context_imprint([self.key bytes], [self.key length],
[message bytes], [message length], [context bytes], [context length], NULL, &unwrappedMessageLength);
[message bytes], [message length], [context bytes], [context length], NULL, &unwrappedMessageLength);

if (decryptionResult != TSErrorTypeBufferTooSmall) {
*error = SCERROR(decryptionResult, @"Secure Cell (Context Imprint) decrypted message length determination failed");
return nil;
}

unsigned char * unwrappedMessage = malloc(unwrappedMessageLength);
unsigned char *unwrappedMessage = malloc(unwrappedMessageLength);
if (!unwrappedMessage) {
*error = SCERROR(decryptionResult, @"Secure Cell (Context Imprint) decryption failed, not enough memory");
return nil;
}

decryptionResult = themis_secure_cell_decrypt_context_imprint([self.key bytes], [self.key length],
[message bytes], [message length], [context bytes], [context length], unwrappedMessage, &unwrappedMessageLength);
[message bytes], [message length], [context bytes], [context length], unwrappedMessage, &unwrappedMessageLength);

if (decryptionResult != TSErrorTypeSuccess) {
free(unwrappedMessage);
Expand Down
8 changes: 4 additions & 4 deletions src/wrappers/themis/Obj-C/objcthemis/scell_seal.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ NS_ASSUME_NONNULL_BEGIN
* @param [in] error pointer to Error on failure
* @return Wrapped message as NSData object on success or nil on failure
*/
- (nullable NSData *)wrapData:(NSData *)message error:(NSError * __autoreleasing *)error;
- (nullable NSData *)wrapData:(NSData *)message error:(NSError *__autoreleasing *)error;

/**
* @brief Unwrap message
* @param [in] message message to unwrap
* @param [in] error pointer to Error on failure
* @return Unwrapped message as NSData object on success or nil on failure
*/
- (nullable NSData *)unwrapData:(NSData *)message error:(NSError * __autoreleasing *)error;
- (nullable NSData *)unwrapData:(NSData *)message error:(NSError *__autoreleasing *)error;

/**
* @brief Wrap message with context
Expand All @@ -76,15 +76,15 @@ NS_ASSUME_NONNULL_BEGIN
* @param [in] error pointer to Error on failure
* @return Wrapped message as NSData object on success or nil on failure
*/
- (nullable NSData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error;
- (nullable NSData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error;

/**
* @brief Unwrap message
* @param [in] message message to unwrap
* @param [in] error pointer to Error on failure
* @return Unwrapped message as NSData object on success or nil on failure
*/
- (nullable NSData *)unwrapData:(NSData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error;
- (nullable NSData *)unwrapData:(NSData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error;

@end

Expand Down
60 changes: 30 additions & 30 deletions src/wrappers/themis/Obj-C/objcthemis/scell_seal.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,84 +26,84 @@ - (nullable instancetype)initWithKey:(NSData *)key {
}


- (nullable NSData *)wrapData:(NSData *)message error:(NSError * __autoreleasing *)error {
- (nullable NSData *)wrapData:(NSData *)message error:(NSError *__autoreleasing *)error {
return [self wrapData:message context:nil error:error];
}


- (nullable NSData *)unwrapData:(NSData *)message error:(NSError * __autoreleasing *)error {
- (nullable NSData *)unwrapData:(NSData *)message error:(NSError *__autoreleasing *)error {
return [self unwrapData:message context:nil error:error];
}


- (nullable NSData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error {
- (nullable NSData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error {
size_t wrappedMessageLength = 0;

const void * contextData = [context bytes];
const void *contextData = [context bytes];
size_t contextLength = [context length];

TSErrorType result = (TSErrorType) themis_secure_cell_encrypt_seal([self.key bytes], [self.key length],
contextData, contextLength, [message bytes], [message length], NULL, &wrappedMessageLength);
contextData, contextLength, [message bytes], [message length], NULL, &wrappedMessageLength);

if (result != TSErrorTypeBufferTooSmall) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) encrypted message length determination failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) encrypted message length determination failed");
}
return nil;
}

unsigned char * wrappedMessage = malloc(wrappedMessageLength);
unsigned char *wrappedMessage = malloc(wrappedMessageLength);
if (!wrappedMessage) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) encryption failed, not enough memory");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) encryption failed, not enough memory");
}
return nil;
}
result = (TSErrorType) themis_secure_cell_encrypt_seal([self.key bytes], [self.key length],
contextData, contextLength, [message bytes], [message length], wrappedMessage, &wrappedMessageLength);
contextData, contextLength, [message bytes], [message length], wrappedMessage, &wrappedMessageLength);

if (result != TSErrorTypeSuccess) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) encryption failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) encryption failed");
}
free(wrappedMessage);
return nil;
}

return [NSData dataWithBytesNoCopy:wrappedMessage length:wrappedMessageLength];
}

- (nullable NSData *)unwrapData:(NSData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error {
- (nullable NSData *)unwrapData:(NSData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error {
size_t unwrappedMessageLength = 0;

const void * contextData = [context bytes];
const void *contextData = [context bytes];
size_t contextLength = [context length];

TSErrorType result = (TSErrorType) themis_secure_cell_decrypt_seal([self.key bytes], [self.key length],
contextData, contextLength, [message bytes], [message length], NULL, &unwrappedMessageLength);
contextData, contextLength, [message bytes], [message length], NULL, &unwrappedMessageLength);

if (result != TSErrorTypeBufferTooSmall) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) decrypted message length determination failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) decrypted message length determination failed");
}
return nil;
}

unsigned char * unwrappedMessage = malloc(unwrappedMessageLength);
unsigned char *unwrappedMessage = malloc(unwrappedMessageLength);
if (!unwrappedMessage) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) decryption failed, not enough memory");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) decryption failed, not enough memory");
}
return nil;
}

result = (TSErrorType) themis_secure_cell_decrypt_seal([self.key bytes], [self.key length],
contextData, contextLength, [message bytes], [message length], unwrappedMessage, &unwrappedMessageLength);
contextData, contextLength, [message bytes], [message length], unwrappedMessage, &unwrappedMessageLength);

if (result != TSErrorTypeSuccess) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) decryption failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Seal) decryption failed");
}
free(unwrappedMessage);
return nil;
}
Expand Down
12 changes: 6 additions & 6 deletions src/wrappers/themis/Obj-C/objcthemis/scell_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ NS_ASSUME_NONNULL_BEGIN
@interface TSCellTokenEncryptedData : NSObject

/**< @breaf cipher text */
@property (nonatomic, strong) NSMutableData * cipherText;
@property(nonatomic, strong) NSMutableData *cipherText;

/**< @breaf token */
@property (nonatomic, strong) NSMutableData * token;
@property(nonatomic, strong) NSMutableData *token;

@end

Expand Down Expand Up @@ -71,15 +71,15 @@ NS_ASSUME_NONNULL_BEGIN
* @param [in] error pointer to Error on failure
* @return Wrapped message as NSData object on success or nil on failure
*/
- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message error:(NSError * __autoreleasing *)error;
- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message error:(NSError *__autoreleasing *)error;

/**
* @brief Unwrap message
* @param [in] message message to unwrap
* @param [in] error pointer to Error on failure
* @return Unwrapped message as NSData object on success or nil on failure
*/
- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message error:(NSError * __autoreleasing *)error;
- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message error:(NSError *__autoreleasing *)error;

/**
* @brief Wrap message with context
Expand All @@ -88,7 +88,7 @@ NS_ASSUME_NONNULL_BEGIN
* @param [in] error pointer to Error on failure
* @return Wrapped message as NSData object on success or nil on failure
*/
- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error;
- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error;

/**
* @brief Unwrap message with context
Expand All @@ -97,7 +97,7 @@ NS_ASSUME_NONNULL_BEGIN
* @param [in] error pointer to Error on failure
* @return Unwrapped message as NSData object on success or nil on failure
*/
- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error;
- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error;

@end

Expand Down
40 changes: 20 additions & 20 deletions src/wrappers/themis/Obj-C/objcthemis/scell_token.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ - (nullable instancetype)initWithKey:(NSData *)key {
}


- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message error:(NSError * __autoreleasing *)error {
- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message error:(NSError *__autoreleasing *)error {
return [self wrapData:message context:nil error:error];
}


- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message error:(NSError * __autoreleasing *)error {
- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message error:(NSError *__autoreleasing *)error {
return [self unwrapData:message context:nil error:error];
}


- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error {
- (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error {
size_t wrappedMessageLength = 0;
size_t tokenLength = 0;

const void * contextData = [context bytes];
const void *contextData = [context bytes];
size_t contextLength = [context length];

TSCellTokenEncryptedData * encryptedMessage = [[TSCellTokenEncryptedData alloc] init];
TSCellTokenEncryptedData *encryptedMessage = [[TSCellTokenEncryptedData alloc] init];
TSErrorType result = (TSErrorType) themis_secure_cell_encrypt_token_protect([self.key bytes], [self.key length],
contextData, contextLength, [message bytes], [message length], NULL, &tokenLength,
NULL, &wrappedMessageLength);

if (result != TSErrorTypeBufferTooSmall) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) encrypted message length determination failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) encrypted message length determination failed");
}
return nil;
}

Expand All @@ -68,40 +68,40 @@ - (nullable TSCellTokenEncryptedData *)wrapData:(NSData *)message context:(nulla
[encryptedMessage.cipherText mutableBytes], &wrappedMessageLength);

if (result != TSErrorTypeSuccess) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) encryption failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) encryption failed");
}
return nil;
}
return encryptedMessage;
}


- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message context:(nullable NSData *)context error:(NSError * __autoreleasing *)error {
- (nullable NSData *)unwrapData:(TSCellTokenEncryptedData *)message context:(nullable NSData *)context error:(NSError *__autoreleasing *)error {
size_t unwrappedMessageLength = 0;
const void * contextData = [context bytes];
const void *contextData = [context bytes];
size_t contextLength = [context length];

TSErrorType result = (TSErrorType) themis_secure_cell_decrypt_token_protect([self.key bytes], [self.key length], contextData, contextLength,
[message.cipherText bytes], [message.cipherText length], [message.token bytes], [message.token length],
NULL, &unwrappedMessageLength);

if (result != TSErrorTypeBufferTooSmall) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) decrypted message length determination failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) decrypted message length determination failed");
}
return nil;
}

NSMutableData * unwrapped_message = [[NSMutableData alloc] initWithLength:unwrappedMessageLength];
NSMutableData *unwrapped_message = [[NSMutableData alloc] initWithLength:unwrappedMessageLength];
result = (TSErrorType) themis_secure_cell_decrypt_token_protect([self.key bytes], [self.key length], contextData, contextLength,
[message.cipherText bytes], [message.cipherText length], [message.token bytes], [message.token length],
[unwrapped_message mutableBytes], &unwrappedMessageLength);

if (result != TSErrorTypeSuccess) {
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) decryption failed");
}
if (error) {
*error = SCERROR(result, @"Secure Cell (Token Protect) decryption failed");
}
return nil;
}
return [unwrapped_message copy];
Expand Down
13 changes: 7 additions & 6 deletions src/wrappers/themis/Obj-C/objcthemis/scomparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
NS_ASSUME_NONNULL_BEGIN

/** @brief Secure comparator states */
typedef NS_ENUM(NSInteger, TSComparatorStateType){
TSComparatorNotReady = 0,
TSComparatorNotMatch = 22,
TSComparatorMatch = 21
typedef NS_ENUM(NSInteger, TSComparatorStateType) {
TSComparatorNotReady = 0,
TSComparatorNotMatch = 22,
TSComparatorMatch = 21
};

/** @brief Secure comparator interface
*
* Secure comparator is a lightweight mechanism
Expand All @@ -54,15 +55,15 @@ typedef NS_ENUM(NSInteger, TSComparatorStateType){
* @param [in] error pointer to Error on failure
* @return Comparation initialization message on success or nil on failure
*/
- (nullable NSData *)beginCompare:(NSError * __autoreleasing *)error;
- (nullable NSData *)beginCompare:(NSError *__autoreleasing *)error;


/** @brief Proceed comparation message
* @param [in] message message to proceed
* @param [in] error pointer to Error on failure
* @return Next comparation message in NSData object on success or nil on failure.
*/
- (nullable NSData *)proceedCompare:(nullable NSData *)message error:(NSError * __autoreleasing *)error;
- (nullable NSData *)proceedCompare:(nullable NSData *)message error:(NSError *__autoreleasing *)error;

/** @brief indicate comparation state.
* @return comparation state.
Expand Down
Loading

0 comments on commit 8d8285c

Please sign in to comment.