Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit cfe18ab

Browse files
edewitBruno Oliveira
authored andcommitted
removed pod file
1 parent 81d7988 commit cfe18ab

File tree

11 files changed

+553
-8
lines changed

11 files changed

+553
-8
lines changed

plugin.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@
5252
<source-file src="src/ios/CryptoPlugin.m"/>
5353
<header-file src="src/ios/CryptoPlugin.h"/>
5454

55-
<asset src="src/ios/Podfile" target="../../Podfile" />
55+
<header-file src="src/ios/0.1.0/AeroGearCrypto.h"/>
56+
<header-file src="src/ios/0.1.0/AGCryptoBox.h"/>
57+
<source-file src="src/ios/0.1.0/AGCryptoBox.m"/>
58+
<header-file src="src/ios/0.1.0/AGPBKDF2.h"/>
59+
<source-file src="src/ios/0.1.0/AGPBKDF2.m"/>
60+
<header-file src="src/ios/0.1.0/AGRandomGenerator.h"/>
61+
<source-file src="src/ios/0.1.0/AGRandomGenerator.m"/>
62+
<header-file src="src/ios/0.1.0/AGSymmetricCryptoEngine.h"/>
63+
<source-file src="src/ios/0.1.0/AGSymmetricCryptoEngine.m"/>
64+
65+
<framework src="Security.framework"/>
5666
</platform>
5767

5868
</plugin>

src/ios/0.1.0/AGCryptoBox.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright Red Hat, Inc., and individual contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import <Foundation/Foundation.h>
19+
20+
/**
21+
* Main class for performing encrypt/decrypt operations. Currently it only supports symmetric
22+
* key encryption but asymmetric support is currently in the works.
23+
*/
24+
@interface AGCryptoBox : NSObject
25+
26+
/**
27+
* Default initializer.
28+
*
29+
* @param key The encryption key to use for the encryption/decryption.
30+
*
31+
* @return the AGCryptoBox object.
32+
*/
33+
- (id)initWithKey:(NSData *)key;
34+
35+
/**
36+
* Encrypts the data object passed in.
37+
*
38+
* @param data The data object to encrypt.
39+
* @param IV A randomly choosen value used as the initialization vector during encrypt.
40+
*
41+
* @return An NSData object that holds the encrypted(cipher) data.
42+
*/
43+
- (NSData *)encrypt:(NSData *)data IV:(NSData *)IV;
44+
45+
/**
46+
* Decrypts the data object(cipher) passed in.
47+
*
48+
* @param data The data object(cipher) to decrypt.
49+
* @param IV A randomly choosen value used as the initialization vector during decrypt.
50+
*
51+
* @return An NSData object that holds the decrypted data.
52+
*/
53+
- (NSData *)decrypt:(NSData *)data IV:(NSData *)IV;
54+
55+
@end

src/ios/0.1.0/AGCryptoBox.m

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright Red Hat, Inc., and individual contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import "AGCryptoBox.h"
19+
#import "AGSymmetricCryptoEngine.h"
20+
21+
@implementation AGCryptoBox {
22+
NSData *_key;
23+
}
24+
25+
- (id)initWithKey:(NSData *)key {
26+
self = [super init];
27+
if (self) {
28+
_key = key;
29+
}
30+
31+
return self;
32+
}
33+
34+
- (NSData *)encrypt:(NSData *)data IV:(NSData *)IV {
35+
NSParameterAssert(data != nil);
36+
NSParameterAssert(IV != nil);
37+
38+
NSError *error;
39+
AGSymmetricCryptoEngine *engine = [[AGSymmetricCryptoEngine alloc] initWithOperation:kCCEncrypt
40+
key:_key
41+
IV:IV
42+
error:&error];
43+
44+
NSMutableData *cipher = [NSMutableData data];
45+
46+
[cipher appendData:[engine add:data error:&error]];
47+
[cipher appendData:[engine finish:&error]];
48+
49+
return cipher;
50+
}
51+
52+
- (NSData *)decrypt:(NSData *)data IV:(NSData *)IV {
53+
NSParameterAssert(data != nil);
54+
NSParameterAssert(IV != nil);
55+
56+
NSError *error;
57+
AGSymmetricCryptoEngine *engine = [[AGSymmetricCryptoEngine alloc] initWithOperation:kCCDecrypt
58+
key:_key
59+
IV:IV
60+
error:&error];
61+
62+
NSMutableData *cipher = [NSMutableData data];
63+
64+
[cipher appendData:[engine add:data error:&error]];
65+
[cipher appendData:[engine finish:&error]];
66+
67+
return cipher;
68+
}
69+
70+
@end

src/ios/0.1.0/AGPBKDF2.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright Red Hat, Inc., and individual contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import <Foundation/Foundation.h>
19+
20+
// constants used by PBKDF2 algorithm.
21+
extern const NSInteger AGPBKDF2Iterations;
22+
extern const NSInteger AGPBKDF2MinimumIterations;
23+
extern const NSInteger AGPBKDF2DerivedKeyLength;
24+
extern const NSInteger AGPBKDF2MinimumSaltLength;
25+
26+
/**
27+
* Class that derives a key from a text password/passphrase using
28+
* the PBKDF2 algorithm provided by CommonCrypto.
29+
* (see http://en.wikipedia.org/wiki/PBKDF2)
30+
*/
31+
@interface AGPBKDF2 : NSObject
32+
33+
/**
34+
* Derive a key from text password/passphrase.
35+
*
36+
* @param password The password/passphrase to use for key derivation.
37+
*
38+
* @return an NSData object containing the derived key.
39+
*/
40+
- (NSData *)deriveKey:(NSString *)password;
41+
42+
/**
43+
* Derive a key from text password/passphrase.
44+
*
45+
* @param password The password/passphrase to use for key derivation.
46+
* @param salt A randomly choosen value used used during key derivation.
47+
*
48+
* @return an NSData object containing the derived key.
49+
*/
50+
- (NSData *)deriveKey:(NSString *)password salt:(NSData *)salt;
51+
52+
/**
53+
* Derive a key from text password/passphrase.
54+
*
55+
* @param password The password/passphrase to use for key derivation.
56+
* @param salt A randomly choosen value used used during key derivation.
57+
* @param iterations The number of iterations against the cryptographic hash.
58+
*
59+
* @return an NSData object containing the derived key.
60+
*/
61+
- (NSData *)deriveKey:(NSString *)password salt:(NSData *)salt iterations:(NSInteger)iterations;
62+
63+
- (BOOL)validate:(NSString *)password encryptedPassword:(NSData *)encryptedPassword salt:(NSData *)salt;
64+
65+
/**
66+
* Returns the salt used for the key derivation
67+
*
68+
* @return an NSData object containing the salt
69+
*/
70+
- (NSData *)salt;
71+
72+
@end

src/ios/0.1.0/AGPBKDF2.m

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright Red Hat, Inc., and individual contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import "AGPBKDF2.h"
19+
#import "AGRandomGenerator.h"
20+
21+
#import <CommonCrypto/CommonCryptor.h>
22+
#import <CommonCrypto/CommonKeyDerivation.h>
23+
24+
const NSInteger AGPBKDF2Iterations = 20000;
25+
const NSInteger AGPBKDF2MinimumIterations = 10000;
26+
const NSInteger AGPBKDF2DerivedKeyLength = 160;
27+
const NSInteger AGPBKDF2MinimumSaltLength = 16;
28+
29+
@implementation AGPBKDF2 {
30+
NSData *_salt;
31+
}
32+
33+
- (NSData *)deriveKey:(NSString *)password {
34+
return [self deriveKey:password salt:[AGRandomGenerator randomBytes]];
35+
}
36+
37+
- (NSData *)deriveKey:(NSString *)password salt:(NSData *)salt {
38+
return [self deriveKey:password salt:salt iterations:AGPBKDF2Iterations];
39+
}
40+
41+
- (NSData *)deriveKey:(NSString *)password salt:(NSData *)salt iterations:(NSInteger)iterations {
42+
NSParameterAssert(password != nil);
43+
NSParameterAssert(salt != nil && [salt length] >= AGPBKDF2MinimumSaltLength);
44+
NSParameterAssert(iterations >= AGPBKDF2MinimumIterations);
45+
46+
_salt = salt;
47+
48+
NSMutableData *key = [NSMutableData dataWithLength:AGPBKDF2DerivedKeyLength];
49+
50+
int result = CCKeyDerivationPBKDF(kCCPBKDF2,
51+
[password UTF8String],
52+
[password length],
53+
[salt bytes],
54+
[salt length],
55+
kCCPRFHmacAlgSHA1,
56+
iterations,
57+
[key mutableBytes],
58+
AGPBKDF2DerivedKeyLength);
59+
if (result == kCCParamError) {
60+
return nil;
61+
}
62+
63+
return key;
64+
}
65+
66+
- (BOOL)validate:(NSString *)password encryptedPassword:(NSData *)encryptedPassword salt:(NSData *)salt {
67+
NSData *attempt = [self deriveKey:password salt:salt];
68+
69+
return [encryptedPassword isEqual:attempt];
70+
}
71+
72+
- (NSData *)salt {
73+
return _salt;
74+
}
75+
76+
@end

src/ios/0.1.0/AGRandomGenerator.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright Red Hat, Inc., and individual contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import <Foundation/Foundation.h>
19+
20+
/**
21+
* Utility class for random generation of cryptographically secure random numbers.
22+
*/
23+
@interface AGRandomGenerator : NSObject
24+
25+
/**
26+
* Generate secure random numbers with default size of 16 bytes.
27+
*
28+
* @return an NSData object filled with random bytes.
29+
*/
30+
+ (NSData *)randomBytes;
31+
32+
/**
33+
* Generate secure random numbers with length bytes.
34+
*
35+
* @param key The length of the random bytes to generate.
36+
*
37+
* @return an NSData object filled with random bytes.
38+
*/
39+
+ (NSData *)randomBytes:(size_t)length;
40+
41+
@end

src/ios/0.1.0/AGRandomGenerator.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright Red Hat, Inc., and individual contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import "AGRandomGenerator.h"
19+
20+
#import <CommonCrypto/CommonCryptor.h>
21+
#import <CommonCrypto/CommonKeyDerivation.h>
22+
23+
@implementation AGRandomGenerator
24+
25+
+ (NSData *)randomBytes {
26+
return [self randomBytes:16];
27+
}
28+
29+
+ (NSData *)randomBytes:(size_t)length {
30+
NSMutableData *data = [NSMutableData dataWithLength:length];
31+
32+
int res = SecRandomCopyBytes(kSecRandomDefault, length, [data mutableBytes]);
33+
34+
return (res == noErr? data: nil);
35+
}
36+
37+
@end

0 commit comments

Comments
 (0)