Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. static NSString * const CCErrorDomain = @"com.apple.commoncrypto.error";
  2.  
  3. static NSString * CCDescriptionForStatus(CCCryptorStatus status) {
  4. static NSString * const _CCStatusDescriptions[] = {
  5. [-kCCSuccess] = @"Operation completed normally.",
  6. [-kCCParamError] = @"Illegal parameter value.",
  7. [-kCCBufferTooSmall] = @"Insufficent buffer provided for specified operation.",
  8. [-kCCMemoryFailure] = @"Memory allocation failure.",
  9. [-kCCAlignmentError] = @"Input size was not aligned properly.",
  10. [-kCCDecodeError] = @"Input data did not decode or decrypt properly.",
  11. [-kCCUnimplemented] = @"Function not implemented for the current algorithm.",
  12. };
  13.  
  14. return _CCStatusDescriptions[-status];
  15. }
  16.  
  17. static NSData * AES128PBKDFKeyWithPassword(NSString *password, NSData *salt, NSError * __autoreleasing *error) {
  18. NSCParameterAssert(password);
  19. NSCParameterAssert(salt);
  20.  
  21. NSMutableData *mutableDerivedKey = [NSMutableData dataWithLength:kCCKeySizeAES128];
  22.  
  23. CCCryptorStatus status = CCKeyDerivationPBKDF(kCCPBKDF2, [password UTF8String], [password lengthOfBytesUsingEncoding:NSUTF8StringEncoding], [salt bytes], [salt length], kCCPRFHmacAlgSHA1, 1024, [mutableDerivedKey mutableBytes], kCCKeySizeAES128);
  24.  
  25. NSData *derivedKey = nil;
  26. if (status != kCCSuccess) {
  27. if (error) {
  28. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: CCDescriptionForStatus(status)};
  29. *error = [[NSError alloc] initWithDomain:CCErrorDomain code:status userInfo:userInfo];
  30. }
  31. } else {
  32. derivedKey = [NSData dataWithData:mutableDerivedKey];
  33. }
  34.  
  35. return derivedKey;
  36. }
  37.  
  38. __attribute__((overloadable)) static NSData * AES128EncryptedDataWithData(NSData *data, NSData *key, NSData * __autoreleasing *initializationVector, NSError * __autoreleasing *error) {
  39. NSCParameterAssert([key length] == kCCKeySizeAES128);
  40. NSCParameterAssert(initializationVector);
  41.  
  42. uint8_t *initializationVectorBuffer = malloc(kCCBlockSizeAES128);
  43. SecRandomCopyBytes(kSecRandomDefault, kCCBlockSizeAES128, initializationVectorBuffer);
  44. *initializationVector = [NSData dataWithBytes:initializationVector length:kCCBlockSizeAES128];
  45.  
  46. size_t size = [data length] + kCCBlockSizeAES128;
  47. void *buffer = malloc(size);
  48.  
  49. size_t numberOfBytesEncrypted = 0;
  50. CCCryptorStatus status = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, [key bytes], [key length], [*initializationVector bytes], [data bytes], [data length], buffer, size, &numberOfBytesEncrypted);
  51.  
  52. NSData *encryptedData = nil;
  53. if (status != kCCSuccess) {
  54. if (error) {
  55. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: CCDescriptionForStatus(status)};
  56. *error = [[NSError alloc] initWithDomain:CCErrorDomain code:status userInfo:userInfo];
  57. }
  58. } else {
  59. encryptedData = [[NSData alloc] initWithBytes:buffer length:numberOfBytesEncrypted];
  60. }
  61.  
  62. return encryptedData;
  63. }
  64.  
  65. __attribute__((overloadable)) static NSData * AES128EncryptedDataWithData(NSData *data, NSString *password, NSData * __autoreleasing *salt, NSData * __autoreleasing *initializationVector, NSError * __autoreleasing *error) {
  66. NSCParameterAssert(salt);
  67.  
  68. uint8_t *saltBuffer = malloc(8);
  69. SecRandomCopyBytes(kSecRandomDefault, 8, saltBuffer);
  70. *salt = [NSData dataWithBytes:saltBuffer length:8];
  71.  
  72. NSData *key = AES128PBKDFKeyWithPassword(password, *salt, error);
  73.  
  74. return AES128EncryptedDataWithData(data, key, initializationVector, error);
  75. }
  76.  
  77. __attribute__((overloadable)) static NSData * AES128DecryptedDataWithData(NSData *data, NSData *key, NSData *initializationVector, NSError * __autoreleasing *error) {
  78. size_t size = [data length] + kCCBlockSizeAES128;
  79. void *buffer = malloc(size);
  80.  
  81. size_t numberOfBytesDecrypted = 0;
  82. CCCryptorStatus status = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, [key bytes], [key length], [initializationVector bytes], [data bytes], [data length], buffer, size, &numberOfBytesDecrypted);
  83.  
  84. NSData *encryptedData = nil;
  85. if (status != kCCSuccess) {
  86. if (error) {
  87. *error = [[NSError alloc] initWithDomain:nil code:status userInfo:nil];
  88. }
  89. } else {
  90. encryptedData = [[NSData alloc] initWithBytes:buffer length:numberOfBytesDecrypted];
  91. }
  92.  
  93. return encryptedData;
  94. }
  95.  
  96. __attribute__((overloadable)) static NSData * AES128DecryptedDataWithData(NSData *data, NSString *password, NSData *salt, NSData *initializationVector, NSError * __autoreleasing *error) {
  97. NSData *key = AES128PBKDFKeyWithPassword(password, salt, error);
  98.  
  99. return AES128DecryptedDataWithData(data, key, initializationVector, error);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement