ObjC implementation of Galois/Counter Mode (GCM) with Advanced Encryption System (AES).
As pointed out in this comment in StackOverflow, iOS already has some GCM crypt functions, however they are not public. In the mean time, you can use the methods in this repo.
The documents used as guide to code this algorithm were:
- Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
- The Galois/Counter Mode of Operation (GCM)
AesGcm is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "AesGcm"#import "IAGAesGcm.h"
u_char keyBytes[16] = {...};
NSData *key = [NSData dataWithBytes:keyBytes length:sizeof(keyBytes)];
u_char ivBytes[12] = {...};
NSData *iv = [NSData dataWithBytes:ivBytes length:sizeof(ivBytes)];
NSData *aad = [@"AdditionalAuthenticatedData" dataUsingEncoding:NSUTF8StringEncoding];
// Authenticated Encryption Function
NSData *expectedPlainData = [@"PlainData" dataUsingEncoding:NSUTF8StringEncoding];
IAGCipheredData *cipheredData = [IAGAesGcm cipheredDataByAuthenticatedEncryptingPlainData:expectedPlainData
withAdditionalAuthenticatedData:aad
authenticationTagLength:IAGAuthenticationTagLength128
initializationVector:iv
key:key
error:nil];
// Authenticated Decryption Function
NSData *plainData = [IAGAesGcm plainDataByAuthenticatedDecryptingCipheredData:cipheredData
withAdditionalAuthenticatedData:aad
initializationVector:iv
key:key
error:nil];
XCTAssertEqualObjects(expectedPlainData, plainData);AesGcm is available under the MIT license. See the LICENSE file for more info.