0

I want to make an AES 256 CBC key and iv with SHA1 digest like in openssl's EVP_BytesToKey function, but with iOS tools. Is there an iOS alternative for that?

In openssl it looks like:

int ret = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, keyingData, strlen(keyingData), 2048 /*iteration*/, resultKey, resultIV);

Later I want to use that generated key (resultKey) and iv (resultIV) in CommonCrypto's CCCrypt, but I don't know how to generate them with iOS tools. Please help.

UPDATE

I've found a code snippet in this video, which maybe do the job, but I'm not sure. Also it's just the key, the IV is still unknown to me.

let keyByteCount = 256/8
var key = Array(repeating: 0, count: keyByteCount)
let error = SecRandomCopyBytes(kSecRandomDefault, keyByteCount, &key)
if error != errSecSuccess {
    return 1
}
memset_s(&key, keyByteCount, 0, keyByteCount)

But I think this is not what I'm looking for, because it doesn't require a salt or iteration count or any algorithm (like SHA1).

arunna
  • 1
  • 4
  • https://github.com/rnapier/RNOpenSSLCryptor The specific part you're looking for is probably https://github.com/rnapier/RNOpenSSLCryptor/blob/master/RNOpenSSLCryptor.m#L66-L99 – Rob Napier Sep 09 '22 at 14:17
  • @RobNapier Thanks, I managed to do this based on this article [link](https://robnapier.net/aes-commoncrypto) – arunna Sep 23 '22 at 08:09

0 Answers0