Skip to content

Commit e9b072b

Browse files
author
David Benko
committed
Added Doxygen comments
1 parent ff66871 commit e9b072b

2 files changed

Lines changed: 134 additions & 51 deletions

File tree

ObjectiveTLS/ObjectiveTLS.h

Lines changed: 108 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,138 @@ typedef NSData* (^IVSeparatorBlock) (NSData **data, NSData **key);
2929
@property (readwrite, copy) IVMixerBlock ivMixer; // Block to mix IV with key or data
3030
@property (readwrite, copy) IVSeparatorBlock ivSeparator; // Block to separate IV from key or data
3131

32-
/*
33-
*
34-
* Init with either:
35-
* - DER-encoded X509 RSA Public Key Path (.der)
36-
* - Base64 encoded X509 RSA Public Key Data
32+
#pragma mark - Init
33+
34+
/**
35+
* Initializes a new ObjectiveTLS object with the contents of a X.509 RSA public key
3736
*
37+
* @param base64KeyData The contents of the public key
38+
* @return new ObjectiveTLS instance
3839
*/
3940
- (ObjectiveTLS *)initWithX509PublicKeyData:(NSData *)base64KeyData;
41+
42+
/**
43+
* Initializes a new ObjectiveTLS object with the contents of a X.509 RSA public key at a given path
44+
*
45+
* @param publicKeyPath The file path of the public key
46+
* @return new ObjectiveTLS instance
47+
*/
4048
- (ObjectiveTLS *)initWithX509PublicKey:(NSString *)publicKeyPath;
4149

50+
#pragma mark - PKCS#12 RSA Private Key (.p12)
4251

43-
/*
44-
*
45-
* (Optional) Set Private Key
46-
* - PKCS#12 RSA Private Key (.p12)
47-
*
48-
* Returns BOOL - True or False based on success of private key trust
52+
/**
53+
* Sets the RSA private key for decryption
4954
*
55+
* @param privateKeyPath The file path of the private key (.p12)
56+
* @param password The password to read the private key (nil for no password)
57+
* @return a boolean representing the success of the operation
5058
*/
51-
5259
-(BOOL)setPrivateKey:(NSString *)privateKeyPath withPassphrase:(NSString *)password;
5360

54-
/*
55-
*
56-
* AES Encrypt NSData or NSString
57-
*
58-
* Returns Encrypted NSData
59-
* Sets Pointer to RSA-Encrypted AES Key
60-
* Sets Pointer to Randomly Generated IV
61-
* Sets Pointer to NSError
61+
62+
#pragma mark - Public Encryption Methods
63+
64+
/**
65+
* Generates symmetric key and iv, symmetrically encrypts data, RSA encrypts symmetric key
6266
*
67+
* @param data The data to be encrypted
68+
* @param key The RSA-encrypted, randomly generated, symmetric key
69+
* @param iv The randomly generated IV
70+
* @param error Errors will be filled here
71+
* @return the encrypted data
6372
*/
6473
- (NSData *)encryptData:(NSData *)data rsaEncryptedKey:(NSData **)key iv:(NSData **)iv error:(NSError **)error;
74+
75+
/**
76+
* Generates symmetric key and iv, symmetrically encrypts string, RSA encrypts symmetric key
77+
*
78+
* @param string The string to be encrypted
79+
* @param key The RSA-encrypted, randomly generated, symmetric key
80+
* @param iv The randomly generated IV
81+
* @param error Errors will be filled here
82+
* @return the encrypted data
83+
*/
6584
- (NSData *)encryptString:(NSString *)string rsaEncryptedKey:(NSData **)key iv:(NSData **)iv error:(NSError **)error;
6685

86+
/**
87+
* Generates symmetric key and iv, symmetrically encrypts data, RSA encrypts symmetric key
88+
* The IVMixerBlock is fired after the symmetric data encryption and before the symmetric key is encrypted.
89+
* Use the IVMixerBlock to mix the IV with either the data or key.
90+
* If passed here, the IVMixerBlock will override the ivMixer property, but only for this call
91+
*
92+
* @param data The data to be encrypted
93+
* @param ivMixer Block to mix the IV with key or data
94+
* @param key The RSA-encrypted, randomly generated, symmetric key
95+
* @param error Errors will be filled here
96+
* @return the encrypted data
97+
*/
6798
- (NSData *)encryptData:(NSData *)data withIVMixer:(IVMixerBlock)ivMixer rsaEncryptedKey:(NSData **)key error:(NSError **)error;
68-
- (NSData *)encryptString:(NSString *)string withIVMixer:(IVMixerBlock)ivMixer rsaEncryptedKey:(NSData **)key error:(NSError **)error;
6999

70-
/*
100+
/**
101+
* Generates symmetric key and iv, symmetrically encrypts string, RSA encrypts symmetric key
102+
* The IVMixerBlock is fired after the symmetric data encryption and before the symmetric key is encrypted.
103+
* Use the IVMixerBlock to mix the IV with either the data or key.
104+
* If passed here, the IVMixerBlock will override the ivMixer property, but only for this call
71105
*
72-
* AES Decrypt NSData
73-
*
74-
* Returns Decrypted NSData or NSString
75-
* Sets Pointer to NSError
106+
* @param string The string to be encrypted
107+
* @param ivMixer Block to mix the IV with key or data
108+
* @param key The RSA-encrypted, randomly generated, symmetric key
109+
* @param error Errors will be filled here
110+
* @return the encrypted data
111+
*/
112+
- (NSData *)encryptString:(NSString *)string withIVMixer:(IVMixerBlock)ivMixer rsaEncryptedKey:(NSData **)key error:(NSError **)error;
113+
114+
#pragma mark - Public Decryption Methods
115+
116+
/**
117+
* Decrypts data. The private key must be set for this method to function.
118+
* @see setPrivateKey:withPassphrase:
76119
*
120+
* @param data The data to be decrypted
121+
* @param key The RSA-encrypted symmetric key
122+
* @param iv The IV
123+
* @param error Errors will be filled here
124+
* @return the decrypted data
77125
*/
78126
- (NSData *)decryptData:(NSData *)data rsaEncryptedKey:(NSData *)key iv:(NSData *)iv error:(NSError **)error;
127+
128+
/**
129+
* Decrypts string from encrypted data. The private key must be set for this method to function.
130+
* @see setPrivateKey:withPassphrase:
131+
*
132+
* @param data The data to be decrypted
133+
* @param key The RSA-encrypted symmetric key
134+
* @param iv The IV
135+
* @param error Errors will be filled here
136+
* @return the decrypted string
137+
*/
79138
- (NSString *)decryptString:(NSData *)data rsaEncryptedKey:(NSData *)key iv:(NSData *)iv error:(NSError **)error;
80139

140+
/**
141+
* Decrypts data. The private key must be set for this method to function.
142+
* @see setPrivateKey:withPassphrase:
143+
* The IVSeparatorBlock should undo the IVMixerBlock run during encryption
144+
*
145+
* @param data The data to be decrypted
146+
* @param ivSeparator The IVSeparatorBlock to retrieve the IV from the key or data
147+
* @param key The RSA-encrypted symmetric key
148+
* @param error Errors will be filled here
149+
* @return the decrypted data
150+
*/
81151
- (NSData *)decryptData:(NSData *)data withIVSeparator:(IVSeparatorBlock)ivSeparator rsaEncryptedKey:(NSData *)key error:(NSError **)error;
152+
153+
/**
154+
* Decrypts string from encrypted data. The private key must be set for this method to function.
155+
* @see setPrivateKey:withPassphrase:
156+
* The IVSeparatorBlock should undo the IVMixerBlock run during encryption
157+
*
158+
* @param data The data to be decrypted
159+
* @param ivSeparator The IVSeparatorBlock to retrieve the IV from the key or data
160+
* @param key The RSA-encrypted symmetric key
161+
* @param error Errors will be filled here
162+
* @return the decrypted string
163+
*/
82164
- (NSString *)decryptString:(NSData *)data withIVSeparator:(IVSeparatorBlock)ivSeparator rsaEncryptedKey:(NSData *)key error:(NSError **)error;
83165

84166
@end

ObjectiveTLS/ObjectiveTLS.m

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ - (ObjectiveTLS *)initWithX509PublicKey:(NSString *)publicKeyPath {
6363
return [self initWithX509PublicKeyData:publicKeyFileContent];
6464
}
6565

66-
#pragma mark - Public Key (.der)
66+
#pragma mark - X.509 RSA Public Key (.der)
6767
- (BOOL)setPublicKey:(NSData *)publicKeyContents{
6868
if (publicKeyContents == nil) {
6969
return false;
@@ -99,7 +99,7 @@ - (BOOL)setPublicKey:(NSData *)publicKeyContents{
9999
return true;
100100
}
101101

102-
#pragma mark - Private Key (.p12)
102+
#pragma mark - PKCS#12 RSA Private Key (.p12)
103103
-(BOOL)setPrivateKey:(NSString *)privateKeyPath withPassphrase:(NSString *)password{
104104
NSData *pkcs12key = [NSData dataWithContentsOfFile:privateKeyPath];
105105
NSDictionary* options = NULL;
@@ -218,17 +218,17 @@ - (NSData *)encryptData:(NSData *)data key:(NSData **)key iv:(NSData **)iv error
218218
NSMutableData *cipherData = [NSMutableData dataWithLength:data.length + self.encryptorAlgorithmBlockSize];
219219

220220
CCCryptorStatus
221-
result = CCCrypt(kCCEncrypt, // operation
222-
self.encryptorAlgorithm, // Algorithm
223-
self.encryptorAlgorithmOptions, // options
224-
(*key).bytes, // key
225-
(*key).length, // keylength
226-
(iv != NULL) ? (*iv).bytes : NULL,// iv
227-
data.bytes, // dataIn
228-
data.length, // dataInLength,
229-
cipherData.mutableBytes, // dataOut
230-
cipherData.length, // dataOutAvailable
231-
&outLength); // dataOutMoved
221+
result = CCCrypt(kCCEncrypt, // operation
222+
self.encryptorAlgorithm, // Algorithm
223+
self.encryptorAlgorithmOptions, // options
224+
(*key).bytes, // key
225+
(*key).length, // keylength
226+
(iv != NULL) ? (*iv).bytes : NULL, // iv
227+
data.bytes, // dataIn
228+
data.length, // dataInLength,
229+
cipherData.mutableBytes, // dataOut
230+
cipherData.length, // dataOutAvailable
231+
&outLength); // dataOutMoved
232232

233233
if (result == kCCSuccess) {
234234
cipherData.length = outLength;
@@ -260,17 +260,17 @@ - (NSData *)decryptData:(NSData *)data key:(NSData *)key iv:(NSData *)iv error:(
260260
size_t outLength;
261261
NSMutableData *decryptedData = [NSMutableData dataWithLength:data.length];
262262
CCCryptorStatus
263-
result = CCCrypt(kCCDecrypt, // operation
264-
self.encryptorAlgorithm, // Algorithm
265-
self.encryptorAlgorithmOptions, // options
266-
key.bytes, // key
267-
key.length, // keylength
268-
(iv != NULL) ? iv.bytes : NULL,// iv
269-
data.bytes, // dataIn
270-
data.length, // dataInLength,
271-
decryptedData.mutableBytes, // dataOut
272-
decryptedData.length, // dataOutAvailable
273-
&outLength); // dataOutMoved
263+
result = CCCrypt(kCCDecrypt, // operation
264+
self.encryptorAlgorithm, // Algorithm
265+
self.encryptorAlgorithmOptions, // options
266+
key.bytes, // key
267+
key.length, // keylength
268+
(iv != NULL) ? iv.bytes : NULL, // iv
269+
data.bytes, // dataIn
270+
data.length, // dataInLength,
271+
decryptedData.mutableBytes, // dataOut
272+
decryptedData.length, // dataOutAvailable
273+
&outLength); // dataOutMoved
274274

275275
if (result == kCCSuccess) {
276276
[decryptedData setLength:outLength];
@@ -290,7 +290,7 @@ - (NSData *)decryptData:(NSData *)data key:(NSData *)key iv:(NSData *)iv error:(
290290
return decryptedData;
291291
}
292292

293-
#pragma mark - Public TLS Methods
293+
#pragma mark - Public Encryption Methods
294294
- (NSData *)encryptData:(NSData *)data rsaEncryptedKey:(NSData **)key iv:(NSData **)iv error:(NSError **)error{
295295
NSData *secret = nil;
296296
NSData *encryptedData = [self encryptData:data key:&secret iv:iv error:error];
@@ -318,6 +318,7 @@ - (NSData *)encryptString:(NSString *)string withIVMixer:(IVMixerBlock)ivMixer r
318318
return [self encryptData:dataToEncrypt withIVMixer:ivMixer rsaEncryptedKey:key error:error];
319319
}
320320

321+
#pragma mark - Public Decryption Methods
321322
- (NSData *)decryptData:(NSData *)data rsaEncryptedKey:(NSData *)key iv:(NSData *)iv error:(NSError **)error{
322323
NSData *secret = [self RSADecryptData:key];
323324
if (secret) {

0 commit comments

Comments
 (0)