Is it possible to use AES128 with GCM mode on iOS?

时光怂恿深爱的人放手 提交于 2019-11-29 11:41:06

RNCryptor uses a HMAC, which is considered a cryptographically strong method of creating an authentication tag. It is open source as well. So I would seriously consider cloning RNCryptor code for this (keep the copyright in the header intact!).

Password based encryption consists of deriving a keys (and possibly an IV), then performing the encryption and authentication. So you should get a long way by simply removing the key derivation part.

The only time when this doesn't work is if the code for key derivation is woven into the part that does the encryption/authentication, but this does not seem to be the case for RNCryptor.

Thanks to owlstead suggest I take a look deeper into RNCryptor and found a solution.

First of all after lots of googling it's seems that Zaph were right and iOS doesn't provide GCM but use it in iOS. ref there: iOS Security feb 2014

Second, RNCryptor doesn't use GCM but use AES256 in CBC mode (Cipher Block Chaining), which is fine, and then authenticate with HMAC+SHA1. This fits my requirements.

To encrypt with a key and to skip the password derivation part, RNCryptor provide this function:

NSData *encryptedData = [RNEncryptor encryptData:yourData
                                        withSettings:kRNCryptorAES256Settings
                                       encryptionKey:encryptionKey
                                             HMACKey:HMACKey
                                               error:&error];

and then decrypt with this

NSData *decryptedData = [RNDecryptor decryptData:encryptedData withEncryptionKey:encryptionKey HMACKey:HMACKey error:&decryptionError];

RNCryptor also provide random generation methods for keys.

Note: take care when using AES256, the key schedule can be weak: Schneier article but no drama and there are other point of view on AES256 that are pros: Colin Percival article

GCM is missing from CommonCrypto. Interestingly Apple is using GCM for the keychain since ios5.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!