iosswiftcryptographycommoncrypto

How to encrypt using AES GCM on iOS?


I need to encrypt/decrypt some data using AES encryption on GCM mode, but apparently this can't be done with the CommonCrypto API. This has been asked previously here, but the accepted answer is not what I'm looking for, since I need to use this specific algorithm.

Any ideas? Should I use OpenSSL? Because I've heard that there are some bugs when using it in iOS.

I'm looking for an answer in Swift, but Objective-C would be fine as well.


Solution

  • There is some GCM crypt functions in the CommonCryptorSPI.h, they are not public yet. But you can use them if you add them to the bridging header.

    #include <CommonCrypto/CommonCryptor.h>
    CCCryptorStatus CCCryptorGCM(
    CCOperation     op,             /* kCCEncrypt, kCCDecrypt */
    CCAlgorithm     alg,
    const void      *key,           /* raw key material */
    size_t          keyLength,  
    const void      *iv,
    size_t          ivLen,
    const void      *aData,
    size_t          aDataLen,
    const void      *dataIn,
    size_t          dataInLength,
    void            *dataOut,
    const void      *tag,
    size_t          *tagLength);
    

    Or you can try the SwCrypt library.