For example, the command:
openssl enc -aes-256-cbc -a -in test.txt -k pinkrhino -nosalt -p -out openssl_output.txt
outputs something like:
key = 33D890D33F91D52FC9B405A0DDA65336C3C4B557A3D79FE69AB674BE82C5C3D2
iv = 677C95C475C0E057B739750748608A49
How is that key generated? (C code as an answer would be too awesome to ask for :) ) Also, how is the iv generated?
Looks like some kind of hex to me.
OpenSSL uses the function EVP_BytesToKey. You can find the call to it in
apps/enc.c
. Theenc
utility used to use the MD5 digest by default in the Key Derivation Algorithm (KDF) if you didn't specify a different digest with the-md
argument. Now it uses SHA-256 by default. Here's a working example using MD5:Example usage:
Which generates the same key as this OpenSSL command line:
OpenSSL 1.1.0c changed the digest algorithm used in some internal components. Formerly, MD5 was used, and 1.1.0 switched to SHA256. Be careful the change is not affecting you in both
EVP_BytesToKey
and commands likeopenssl enc
.If anyone passing through here is looking for a working, performant reference implementation in Haskell, here it is:
It uses hash algorithms provided by the cryptonite package. The arguments are desired key and IV size in bytes, the hash algorithm to use (like e.g.
(undefined :: MD5)
), optional salt and the password. The result is a tuple of key and IV.If anyone is looking for implementing the same in SWIFT I converted the
EVP_BytesToKey
in swiftI use CryptoSwift for the hash. This is a much cleaner way as apples does not recommend OpenSSL in iOS
UPDATE : Swift 3
Here is a version for mbedTLS / Polar SSL - tested and working.