What is the default IV when encrypting with aes_256_cbc cipher?

前端 未结 2 586
北恋
北恋 2021-02-19 08:44

I\'ve generated a random 256 bit symmetric key, in a file, to use for encrypting some data using the OpenSSL command line which I need to decrypt later programmatically using th

2条回答
  •  情书的邮戳
    2021-02-19 08:54

    EVP_DecryptInit_ex is an interface to the AES decryption primitive. That is just one piece of what you need to decrypt the OpenSSL encryption format. The OpenSSL encryption format is not well documented, but you can work it backwards from the code and some of the docs. The key and IV computation is explained in the EVP_BytesToKey documentation:

       The key and IV is derived by concatenating D_1, D_2, etc until enough
       data is available for the key and IV. D_i is defined as:
    
               D_i = HASH^count(D_(i-1) || data || salt)
    
       where || denotes concatentaion, D_0 is empty, HASH is the digest
       algorithm in use, HASH^1(data) is simply HASH(data), HASH^2(data) is
       HASH(HASH(data)) and so on.
    
       The initial bytes are used for the key and the subsequent bytes for the
       IV.
    

    "HASH" here is MD5. In practice, this means you compute hashes like this:

    Hash0 = ''
    Hash1 = MD5(Hash0 + Password + Salt)
    Hash2 = MD5(Hash1 + Password + Salt)
    Hash3 = MD5(Hash2 + Password + Salt)
    ...
    

    Then you pull of the bytes you need for the key, and then pull the bytes you need for the IV. For AES-128 that means Hash1 is the key and Hash2 is the IV. For AES-256, the key is Hash1+Hash2 (concatenated, not added) and Hash3 is the IV.

    You need to strip off the leading Salted___ header, then use the salt to compute the key and IV. Then you'll have the pieces to feed into EVP_DecryptInit_ex.

    Since you're doing this in C++, though, you can probably just dig through the enc code and reuse it (after verifying its license is compatible with your use).

    Note that the OpenSSL IV is randomly generated, since it's the output of a hashing process involving a random salt. The security of the first block doesn't depend on the IV being random per se; it just requires that a particular IV+Key pair never be repeated. The OpenSSL process ensures that as long as the random salt is never repeated.

    It is possible that using MD5 this way entangles the key and IV in a way that leaks information, but I've never seen an analysis that claims that. If you have to use the OpenSSL format, I wouldn't have any hesitations over its IV generation. The big problems with the OpenSSL format is that it's fast to brute force (4 rounds of MD5 is not enough stretching) and it lacks any authentication.

提交回复
热议问题