- Remove AeroGear-Crypto

- Add NSData-AES
This commit is contained in:
giuseppenuc committed 2016-12-29 16:18:35 +01:00
1 parent 0b0803f3f9
commit 87b4f742e3
580 files changed
+3753 -24516

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
Copyright (c) 2016 Paolo Tagliani <paolo@mobilejazz.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+46
View File
@@ -0,0 +1,46 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
#import "NSDataAESCipher.h"
/**
* NSData AES encryption category.
**/
@interface NSData (AES)
/** *************************************************** **
* @name Encrypt
** *************************************************** **/
- (NSData*)aes_encrypt:(NSData*)key;
- (NSData*)aes_encryptWithString:(NSString*)key;
- (NSData*)aes_encrypt:(NSData*)key withPadding:(MJCCOption) options;
- (NSData*)aes_encrypt:(NSData*)key withInitial:(NSData*)iv;
- (NSData*)aes_encrypt:(NSData*)key withInitial:(NSData*)iv andPadding:(MJCCOption)options;
/** *************************************************** **
* @name Decrypt
** *************************************************** **/
- (NSData*)aes_decrypt:(NSData*)key;
- (NSData*)aes_decryptWithString:(NSString*)key;
- (NSData*)aes_decrypt:(NSData*)key withPadding:(MJCCOption)options;
- (NSData*)aes_decrypt:(NSData*)key withInitial:(NSData*)iv;
- (NSData*)aes_decrypt:(NSData*)key withInitial:(NSData*)iv andPadding:(MJCCOption)options;
@end
+85
View File
@@ -0,0 +1,85 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <CommonCrypto/CommonCryptor.h>
#import "NSData+AES.h"
@implementation NSData (AES)
- (NSData*)aes_encrypt:(NSData*)key
{
return [self aes_encrypt:key withInitial:nil];
}
- (NSData*)aes_encryptWithString:(NSString*)key
{
return [self aes_encrypt:[key dataUsingEncoding:NSUTF8StringEncoding] withInitial:nil];
}
- (NSData*)aes_encrypt:(NSData*)key withInitial:(NSData*)iv
{
return [self aes_encrypt:key withInitial:iv andPadding:kCCOptionPKCS7Padding];
}
- (NSData*)aes_encrypt:(NSData*)key withPadding:(MJCCOption)options
{
return [self aes_encrypt:key withInitial:nil andPadding:options];
}
- (NSData*)aes_encrypt:(NSData*)key withInitial:(NSData*)iv andPadding:(MJCCOption)options
{
return [NSDataAESCipher cipherWithkey:key
value:self
iv:iv
operation:kCCEncrypt
options:options
output:nil
error:nil];
}
- (NSData*)aes_decrypt:(NSData*)key
{
return [self aes_decrypt:key withInitial:nil];
}
- (NSData*)aes_decryptWithString:(NSString*)key
{
return [self aes_decrypt:[key dataUsingEncoding:NSUTF8StringEncoding] withInitial:nil];
}
- (NSData*)aes_decrypt:(NSData*)key withInitial:(NSData*)iv
{
return [self aes_decrypt:key withInitial:iv andPadding:kCCOptionPKCS7Padding];
}
- (NSData *)aes_decrypt:(NSData *)key withPadding:(MJCCOption)options
{
return [self aes_decrypt:key withInitial:nil andPadding:options];
}
- (NSData*)aes_decrypt:(NSData*)key withInitial:(NSData*)iv andPadding:(MJCCOption)options
{
return [NSDataAESCipher cipherWithkey:key
value:self
iv:nil
operation:kCCDecrypt
options:options
output:nil
error:nil];
}
@end
+38
View File
@@ -0,0 +1,38 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSData+AES.h"
/**
* AES category on NSData to encrypt data using self as encryption key.
**/
@interface NSData (AESKey)
/** *************************************************** **
* @name Encrypt
** *************************************************** **/
- (NSData*)aes_encryptValue:(NSData*)value;
- (NSData*)aes_encryptValue:(NSData*)value usingPadding:(MJCCOption)options;
/** *************************************************** **
* @name Decrypt
** *************************************************** **/
- (NSData*)aes_decryptValue:(NSData*)value;
- (NSData*)aes_decryptValue:(NSData*)value usingPadding:(MJCCOption)options;
@end
+41
View File
@@ -0,0 +1,41 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSData+AESKey.h"
@implementation NSData (AESKey)
- (NSData*)aes_encryptValue:(NSData*)value
{
return [value aes_encrypt:self withPadding:MJCCOptionPKCS7Padding];
}
- (NSData*)aes_encryptValue:(NSData*)value usingPadding:(MJCCOption)options
{
return [value aes_encrypt:self withPadding:options];
}
- (NSData*)aes_decryptValue:(NSData*)value
{
return [value aes_decrypt:self withPadding:MJCCOptionPKCS7Padding];
}
- (NSData*)aes_decryptValue:(NSData*)value usingPadding:(MJCCOption)options
{
return [value aes_decrypt:self withPadding:options];
}
@end
+38
View File
@@ -0,0 +1,38 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSData+AES.h"
/**
* AES category on NSData to encrypt data using self as value to be encrypted.
**/
@interface NSData (AESValue)
/** *************************************************** **
* @name Encrypt
** *************************************************** **/
- (NSData*)aes_encryptWithKey:(NSData*)key;
- (NSData*)aes_encryptWithKey:(NSData*)key usingPadding:(MJCCOption)options;
/** *************************************************** **
* @name Decrypt
** *************************************************** **/
- (NSData*)aes_decryptWithKey:(NSData*)key;
- (NSData*)aes_decryptWithKey:(NSData*)key usingPadding:(MJCCOption)options;
@end
+41
View File
@@ -0,0 +1,41 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSData+AESValue.h"
@implementation NSData (AESValue)
- (NSData*)aes_encryptWithKey:(NSData*)key
{
return [self aes_encrypt:key withPadding:MJCCOptionPKCS7Padding];
}
- (NSData*)aes_encryptWithKey:(NSData*)key usingPadding:(MJCCOption)options
{
return [self aes_encrypt:key withPadding:options];
}
- (NSData*)aes_decryptWithKey:(NSData*)key
{
return [self aes_decrypt:key withPadding:MJCCOptionPKCS7Padding];
}
- (NSData*)aes_decryptWithKey:(NSData*)key usingPadding:(MJCCOption)options
{
return [self aes_decrypt:key withPadding:options];
}
@end
+31
View File
@@ -0,0 +1,31 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
extern NSString * const NSDataSHAErrorDomain;
extern NSInteger const NSDataSHA1ErrorCode;
extern NSInteger const NSDataSHA256ErrorCode;
@interface NSData (SHA)
- (NSData*)sha_SHA1;
- (NSData*)sha_SHA1WithError:(NSError**)error;
- (NSData*)sha_SHA256;
- (NSData*)sha_SHA256WithError:(NSError**)error;
@end
+62
View File
@@ -0,0 +1,62 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSData+SHA.h"
#import <CommonCrypto/CommonDigest.h>
NSString * const NSDataSHAErrorDomain = @"com.mobilejazz.NSData+SHA";
NSInteger const NSDataSHA1ErrorCode = 1;
NSInteger const NSDataSHA256ErrorCode = 2;
@implementation NSData (SHA)
- (NSData*)sha_SHA1
{
return [self sha_SHA1WithError:nil];
}
- (NSData*)sha_SHA1WithError:(NSError**)error
{
NSMutableData *md = [NSMutableData dataWithLength:16];
unsigned char *result = [md mutableBytes];
if (result != CC_SHA1(self.bytes, (CC_LONG)self.length, result))
{
if (error)
*error = [NSError errorWithDomain:NSDataSHAErrorDomain code:NSDataSHA1ErrorCode userInfo:nil];
}
return md;
}
- (NSData*)sha_SHA256
{
return [self sha_SHA256WithError:nil];
}
- (NSData*)sha_SHA256WithError:(NSError**)error
{
NSMutableData *md = [NSMutableData dataWithLength:32];
unsigned char *result = [md mutableBytes];
if (result != CC_SHA256(self.bytes, (CC_LONG)self.length, result))
{
if (error)
*error = [NSError errorWithDomain:NSDataSHAErrorDomain code:NSDataSHA256ErrorCode userInfo:nil];
}
return md;
}
@end
@@ -0,0 +1,44 @@
//
// NSDataAESCipher.h
// MJ-iOS-Toolkit
//
// Created by Joan Martin on 09/05/16.
// Copyright © 2016 Mobile Jazz. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(uint32_t, MJCCOption) {
MJCCOptionPKCS7Padding = 0x0001,
MJCCOptionECBMode = 0x0002
};
typedef NS_ENUM(uint32_t, MJCCOperation) {
MJCCEncrypt = 0,
MJCCDecrypt,
};
extern NSString * NSDataAESCipherErrorDomain;
extern NSInteger const NSDataAESCipherUndefinedErrorCode;
extern NSInteger const NSDataAESCipherParamErrorCode;
extern NSInteger const NSDataAESCipherBufferTooSmallErrorCode;
extern NSInteger const NSDataAESCipherMemoryFailureErrorCode;
extern NSInteger const NSDataAESCipherAlignmentErrorCode;
extern NSInteger const NSDataAESCipherDecodeErrorCode;
extern NSInteger const NSDataAESCipherCUnimplementedErrorCode;
/**
* Cipher NSData handler
**/
@interface NSDataAESCipher : NSObject
+ (NSData*)cipherWithkey:(NSData*)key
value:(NSData*)value
iv:(NSData*)iv
operation:(MJCCOperation)operation
options:(MJCCOption)options
output:(NSMutableData*)output
error:(NSError**)error;
@end
+126
View File
@@ -0,0 +1,126 @@
//
// NSDataAESCipher.m
// MJ-iOS-Toolkit
//
// Created by Joan Martin on 09/05/16.
// Copyright © 2016 Mobile Jazz. All rights reserved.
//
#import "NSDataAESCipher.h"
#import "NSData+SHA.h"
#import <CommonCrypto/CommonCryptor.h>
NSString * NSDataAESCipherErrorDomain = @"com.mobilejazz.NSDataAESCipher";
NSInteger const NSDataAESCipherUndefinedErrorCode = 0;
NSInteger const NSDataAESCipherParamErrorCode = kCCParamError;
NSInteger const NSDataAESCipherBufferTooSmallErrorCode = kCCBufferTooSmall;
NSInteger const NSDataAESCipherMemoryFailureErrorCode = kCCMemoryFailure;
NSInteger const NSDataAESCipherAlignmentErrorCode = kCCAlignmentError;
NSInteger const NSDataAESCipherDecodeErrorCode = kCCDecodeError;
NSInteger const NSDataAESCipherCUnimplementedErrorCode = kCCUnimplemented;
@implementation NSDataAESCipher
+ (NSData*)cipherWithkey:(NSData*)key
value:(NSData*)value
iv:(NSData*)iv
operation:(MJCCOperation)operation
options:(MJCCOption)options
output:(NSMutableData*)output
error:(NSError**)error
{
if (kCCKeySizeAES256 != key.length)
{
// SHA256 the key unless it's already 256 bits.
key = [key sha_SHA256];
}
NSUInteger len = value.length;
NSUInteger capacity = (NSUInteger)(len / kCCBlockSizeAES128 + 1) * kCCBlockSizeAES128;
NSMutableData *data;
if (nil == output)
{
data = [NSMutableData dataWithLength:capacity];
}
else
{
data = output;
if (data.length < capacity)
{
[data setLength:capacity];
}
}
if (iv && kCCBlockSizeAES128 != iv.length)
{
// SHA1 the iv if provided.
iv = [iv sha_SHA1];
}
else
{
iv = [NSMutableData dataWithLength:kCCBlockSizeAES128];
}
const void *_iv = iv.bytes;
size_t dataOutMoved;
CCCryptorStatus ccStatus = CCCrypt(operation,
kCCAlgorithmAES128,
options,
(const char*)key.bytes,
key.length,
_iv,
(const void *)value.bytes,
[value length],
(void *)data.mutableBytes,
capacity,
&dataOutMoved
);
if (dataOutMoved < data.length)
{
[data setLength:dataOutMoved];
}
switch (ccStatus)
{
case kCCSuccess:
return data;
break;
case kCCParamError:
if (error)
*error = [NSError errorWithDomain:NSDataAESCipherErrorDomain code:NSDataAESCipherParamErrorCode userInfo:nil];
break;
case kCCBufferTooSmall:
if (error)
*error = [NSError errorWithDomain:NSDataAESCipherErrorDomain code:NSDataAESCipherBufferTooSmallErrorCode userInfo:nil];
break;
case kCCMemoryFailure:
if (error)
*error = [NSError errorWithDomain:NSDataAESCipherErrorDomain code:NSDataAESCipherMemoryFailureErrorCode userInfo:nil];
break;
case kCCAlignmentError:
if (error)
*error = [NSError errorWithDomain:NSDataAESCipherErrorDomain code:NSDataAESCipherAlignmentErrorCode userInfo:nil];
break;
case kCCDecodeError:
if (error)
*error = [NSError errorWithDomain:NSDataAESCipherErrorDomain code:NSDataAESCipherDecodeErrorCode userInfo:nil];
break;
case kCCUnimplemented:
if (error)
*error = [NSError errorWithDomain:NSDataAESCipherErrorDomain code:NSDataAESCipherCUnimplementedErrorCode userInfo:nil];
break;
default:
if (error)
*error = [NSError errorWithDomain:NSDataAESCipherErrorDomain code:NSDataAESCipherUndefinedErrorCode userInfo:nil];
break;
}
return nil;
}
@end
+39
View File
@@ -0,0 +1,39 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSData+AES.h"
#import "NSDataAESCipher.h"
/**
* Category on NSMutableData for AES encryption.
**/
@interface NSMutableData (AES)
/** *************************************************** **
* @name Encrypt
** *************************************************** **/
- (void)aes_encryptInPlace:(NSData*)key;
- (void)aes_encryptInPlace:(NSData*)key withPadding:(MJCCOption)options;
/** *************************************************** **
* @name Decrypt
** *************************************************** **/
- (void)aes_decryptInPlace:(NSData*)key;
- (void)aes_decryptInPlace:(NSData*)key withPadding:(MJCCOption)options;
@end
+57
View File
@@ -0,0 +1,57 @@
//
// Copyright 2016 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NSDataAESCipher.h"
#import "NSMutableData+AES.h"
@implementation NSMutableData (AES)
- (void)aes_encryptInPlace:(NSData*)key
{
[self aes_encryptInPlace:key withPadding:MJCCOptionPKCS7Padding];
}
- (void)aes_encryptInPlace:(NSData*)key withPadding:(MJCCOption)options
{
NSData *data = [NSDataAESCipher cipherWithkey:key
value:self
iv:nil
operation:MJCCEncrypt
options:options
output:nil
error:nil];
[self setData:data];
}
- (void)aes_decryptInPlace:(NSData*)key
{
[self aes_decryptInPlace:key withPadding:MJCCOptionPKCS7Padding];
}
- (void)aes_decryptInPlace:(NSData*)key withPadding:(MJCCOption)options
{
NSData *data = [NSDataAESCipher cipherWithkey:key
value:self
iv:nil
operation:MJCCDecrypt
options:options
output:nil
error:nil];
[self setData:data];
}
@end
+30
View File
@@ -0,0 +1,30 @@
![Banner](https://raw.githubusercontent.com/mobilejazz/metadata/master/images/banners/mobile-jazz-nsdataaes.png)
# ![Mobile Jazz Badge](https://raw.githubusercontent.com/mobilejazz/metadata/master/images/icons/mj-40x40.png) NSDataAES
[![Version](https://img.shields.io/cocoapods/v/NSDataAES.svg?style=flat)](http://cocoapods.org/pods/NSDataAES)
[![License](https://img.shields.io/cocoapods/l/NSDataAES.svg?style=flat)](http://cocoapods.org/pods/NSDataAES)
[![Platform](https://img.shields.io/cocoapods/p/NSDataAES.svg?style=flat)](http://cocoapods.org/pods/NSDataAES)
Simple NSData category that encrypt and decrypt data using AES chiper.
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Installation
NSDataAES is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod "NSDataAES"
```
## Author
Joan Martin, joan@mobilejazz.com
Paolo Tagliani, paolo@mobilejazz.com
## License
NSDataAES is available under the MIT license. See the LICENSE file for more info.