OpenSSL using EVP vs. algorithm API for symmetric crypto

前端 未结 2 1378
生来不讨喜
生来不讨喜 2020-12-12 22:53

Hi i have installed openssl on my linux machine and going through the header files and documentation (which is highly insufficint :( ).

i am trying to build a projec

相关标签:
2条回答
  • 2020-12-12 23:43

    Using the EVP API has the advantage that you can use the same API for all the symmetric ciphers that OpenSSL supports, in a generic way. This makes it way easier to replace the algorithm used, or make the algorithm user-configurable at a later stage. Most of the code you write is not specific to the encryption algorithm you selected.

    Here's a simple example for encryption with AES-256 in CBC mode:

    #include <stdio.h>
    #include <openssl/evp.h>
    
    int main()
    {
        EVP_CIPHER_CTX ctx;
        unsigned char key[32] = {0};
        unsigned char iv[16] = {0};
        unsigned char in[16] = {0};
        unsigned char out[32]; /* at least one block longer than in[] */
        int outlen1, outlen2;
    
        EVP_EncryptInit(&ctx, EVP_aes_256_cbc(), key, iv);
        EVP_EncryptUpdate(&ctx, out, &outlen1, in, sizeof(in));
        EVP_EncryptFinal(&ctx, out + outlen1, &outlen2);
    
        printf("ciphertext length: %d\n", outlen1 + outlen2);
    
        return 0;
    }
    

    For simplicity, I omitted error handling.

    IMO one of the most important pieces of documentation on OpenSSL is Network Security with OpenSSL by Viega/Messier/Chandra. It is from 2002 (0.9.7), so does not cover changes to OpenSSL during the last 10 years, but it is IMO still a less painful way to learn OpenSSL than by using only the manual pages.

    0 讨论(0)
  • 2020-12-12 23:45

    Currently OpenSSL wiki has good documentation on how to use the EVP family of functions: http://wiki.openssl.org/index.php/EVP

    The other upside of using the EVP over algorithm API is that EVP will automatically use hardware acceleration (like AES-NI instruction set) if available. With algorithm API you need to enable it manually.

    0 讨论(0)
提交回复
热议问题