FreeRDP
Loading...
Searching...
No Matches
Encryptor Class Reference
Inheritance diagram for Encryptor:
Collaboration diagram for Encryptor:

Instance Methods

(id) - initWithPassword:
 
(NSData *) - encryptData:
 
(NSData *) - decryptData:
 
(NSData *) - encryptString:
 
(NSString *) - decryptString:
 

Properties

NSString * plaintextPassword
 

Detailed Description

Definition at line 27 of file Encryptor.h.

Method Documentation

◆ decryptData:

- (NSData *) decryptData: (NSData *)  encrypted_data

Definition at line 1 of file Encryptor.m.

102 :(NSData *)encrypted_data
103{
104 if ([encrypted_data length] <= TSXEncryptorBlockCipherBlockSize)
105 return nil;
106
107 NSMutableData *plaintext_data =
108 [NSMutableData dataWithLength:[encrypted_data length] + TSXEncryptorBlockCipherBlockSize];
109 size_t data_out_moved = 0;
110
111 int ret =
112 CCCrypt(kCCDecrypt, TSXEncryptorBlockCipherAlgo, TSXEncryptorBlockCipherOptions,
113 [_encryption_key bytes], TSXEncryptorBlockCipherKeySize, [encrypted_data bytes],
114 [encrypted_data bytes] + TSXEncryptorBlockCipherBlockSize,
115 [encrypted_data length] - TSXEncryptorBlockCipherBlockSize,
116 [plaintext_data mutableBytes], [plaintext_data length], &data_out_moved);
117
118 switch (ret)
119 {
120 case kCCSuccess:
121 [plaintext_data setLength:data_out_moved];
122 return plaintext_data;
123
124 case kCCBufferTooSmall: // Our output buffer is big enough to decrypt valid data. This
125 // return code indicates malformed data.
126 case kCCAlignmentError: // Shouldn't get this, since we're using padding.
127 case kCCDecodeError: // Wrong key.
128 return nil;
129
130 default:
131 NSLog(@"%s: uncaught error, ret CCCryptorStatus = %d (encrypted data len = %lu; buffer "
132 @"size = %lu; dom = %lu)",
133 __func__, ret, (unsigned long)[encrypted_data length],
134 (unsigned long)[plaintext_data length], data_out_moved);
135 return nil;
136 }
137
138 return nil;
139}

◆ decryptString:

- (NSString *) decryptString: (NSData *)  encrypted_string

Definition at line 1 of file Encryptor.m.

146 :(NSData *)encrypted_string
147{
148 return [[[NSString alloc] initWithData:[self decryptData:encrypted_string]
149 encoding:NSUTF8StringEncoding] autorelease];
150}

◆ encryptData:

- (NSData *) encryptData: (NSData *)  plaintext_data

Definition at line 1 of file Encryptor.m.

67 :(NSData *)plaintext_data
68{
69 if (![plaintext_data length])
70 return nil;
71
72 NSData *iv = [self randomInitializationVector];
73 NSMutableData *encrypted_data = [NSMutableData
74 dataWithLength:[iv length] + [plaintext_data length] + TSXEncryptorBlockCipherBlockSize];
75 [encrypted_data replaceBytesInRange:NSMakeRange(0, [iv length]) withBytes:[iv bytes]];
76
77 size_t data_out_moved = 0;
78 int ret = CCCrypt(kCCEncrypt, TSXEncryptorBlockCipherAlgo, TSXEncryptorBlockCipherOptions,
79 [_encryption_key bytes], TSXEncryptorBlockCipherKeySize, [iv bytes],
80 [plaintext_data bytes], [plaintext_data length],
81 [encrypted_data mutableBytes] + [iv length],
82 [encrypted_data length] - [iv length], &data_out_moved);
83
84 switch (ret)
85 {
86 case kCCSuccess:
87 [encrypted_data setLength:[iv length] + data_out_moved];
88 return encrypted_data;
89
90 default:
91 NSLog(
92 @"%s: uncaught error, ret CCCryptorStatus = %d (plaintext len = %lu; buffer size = "
93 @"%lu)",
94 __func__, ret, (unsigned long)[plaintext_data length],
95 (unsigned long)([encrypted_data length] - [iv length]));
96 return nil;
97 }
98
99 return nil;
100}

◆ encryptString:

- (NSData *) encryptString: (NSString *)  plaintext_string

Definition at line 1 of file Encryptor.m.

141 :(NSString *)plaintext_string
142{
143 return [self encryptData:[plaintext_string dataUsingEncoding:NSUTF8StringEncoding]];
144}

◆ initWithPassword:

- (id) initWithPassword: (NSString *)  plaintext_password

Definition at line 1 of file Encryptor.m.

26 :(NSString *)plaintext_password
27{
28 if (plaintext_password == nil)
29 return nil;
30
31 if (!(self = [super init]))
32 return nil;
33
34 _plaintext_password = [plaintext_password retain];
35 const char *plaintext_password_data =
36 [plaintext_password length] ? [plaintext_password UTF8String] : " ";
37
38 if (!plaintext_password_data || !strlen(plaintext_password_data))
39 [NSException raise:NSInternalInconsistencyException
40 format:@"%s: plaintext password data is zero length!", __func__];
41
42 uint8_t *derived_key = calloc(1, TSXEncryptorPBKDF2KeySize);
43
44 int ret = CCKeyDerivationPBKDF(
45 kCCPBKDF2, plaintext_password_data, strlen(plaintext_password_data) - 1,
46 (const uint8_t *)TSXEncryptorPBKDF2Salt, TSXEncryptorPBKDF2SaltLen, kCCPRFHmacAlgSHA1,
47 TSXEncryptorPBKDF2Rounds, derived_key, TSXEncryptorPBKDF2KeySize);
48
49 if (ret)
50 {
51 NSLog(@"%s: CCKeyDerivationPBKDF ret == %d, indicating some sort of failure.", __func__,
52 ret);
53 free(derived_key);
54 [self autorelease];
55 return nil;
56 }
57
58 _encryption_key = [[NSData alloc] initWithBytesNoCopy:derived_key
59 length:TSXEncryptorPBKDF2KeySize
60 freeWhenDone:YES];
61 return self;
62}

Property Documentation

◆ plaintextPassword

- (NSString*) plaintextPassword
readatomicassign

Definition at line 34 of file Encryptor.h.


The documentation for this class was generated from the following files: