Created
December 13, 2021 10:34
-
-
Save seifolahi/504f01472fa3c3901f3dbba98ba5e54a to your computer and use it in GitHub Desktop.
Open and use non-public CCCryptorGCM to encrypt and decrypt using AES and GCM in iOS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // AesGcmEncryptor.h | |
| // | |
| // Created by Hamidreza Seifolahi on 12/13/21. | |
| // Copyright © 2021 AsanPardakht. All rights reserved. | |
| // | |
| #import <CommonCrypto/CommonCryptor.h> | |
| NS_ASSUME_NONNULL_BEGIN | |
| @interface AesGcmEncryptor : NSObject | |
| + (NSData *) encrypt:(NSData *)dataIn | |
| context:(CCOperation)encryptOrDecrypt | |
| iv:(NSData *)iv | |
| key:(NSData *)symmetricKey; | |
| @end | |
| NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // AesGcmEncryptor.m | |
| // | |
| // Created by Hamidreza Seifolahi on 12/13/21. | |
| // Copyright © 2021 AsanPardakht. All rights reserved. | |
| // | |
| #import "AesGcmEncryptor.h" | |
| #import <dlfcn.h> | |
| @implementation AesGcmEncryptor | |
| + (NSData *) encrypt:(NSData *)dataIn | |
| context:(CCOperation)encryptOrDecrypt | |
| iv:(NSData *)iv | |
| key:(NSData *)symmetricKey { | |
| int (*CCCryptorGCM)() = dlsym([self sharedHandler], "CCCryptorGCM"); | |
| CCCryptorStatus ccStatus = kCCSuccess; | |
| size_t tagLength = kCCBlockSizeAES128; | |
| NSData *tagData = [NSMutableData dataWithLength:tagLength]; | |
| NSData *aData = [[NSData alloc] init]; | |
| NSMutableData *dataOut; | |
| if (encryptOrDecrypt == kCCDecrypt) { | |
| // in decrypt tag is attached to the dataIn and should be seprated | |
| dataOut = [NSMutableData dataWithLength:dataIn.length - tagLength]; | |
| tagData = [dataIn subdataWithRange:NSMakeRange(dataIn.length - tagLength, tagLength)]; | |
| dataIn = [dataIn subdataWithRange:NSMakeRange(0, dataIn.length - tagLength)]; | |
| } else { | |
| dataOut = [NSMutableData dataWithLength:dataIn.length]; | |
| tagData = [NSMutableData dataWithLength:tagLength]; | |
| } | |
| ccStatus = CCCryptorGCM(encryptOrDecrypt, | |
| kCCAlgorithmAES128, | |
| symmetricKey.bytes, | |
| symmetricKey.length, | |
| iv.bytes, | |
| iv.length, | |
| aData.bytes, | |
| aData.length, | |
| dataIn.bytes, | |
| dataIn.length, | |
| dataOut.mutableBytes, | |
| tagData.bytes, | |
| &tagLength); | |
| if (encryptOrDecrypt == kCCEncrypt) { | |
| // in encrypt the tag should be appended to dataOut | |
| [dataOut appendData:tagData]; | |
| } | |
| if (ccStatus == kCCSuccess) { | |
| return dataOut; | |
| } else { | |
| return nil; | |
| } | |
| } | |
| + (void *)sharedHandler | |
| { | |
| static void * sharedInstance = nil; | |
| static dispatch_once_t onceToken; | |
| dispatch_once(&onceToken, ^{ | |
| sharedInstance = dlopen("/usr/lib/system/libcommonCrypto.dylib", RTLD_NOW); | |
| }); | |
| return sharedInstance; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment