Unable to set IV for aes gcm using openssl

痴心易碎 提交于 2019-12-08 14:02:22

问题


I am trying to use AES GCM encryption mechanism provided by OpenSSL in C++ and using example on this link as reference: https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption

However, following statement gives me error:

/* Set IV length if default 12 bytes (96 bits) is not appropriate */
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
    handleErrors();

The error that I get is:

error: ‘EVP_CTRL_GCM_SET_IVLEN’ was not declared in this scope".

I do not understand, why I cannot set IVLEN to 16 bytes? I do not want to use the default value of 12 bytes. Any pointers will be great.


回答1:


I resolved the error. Actually, in the example code, the order for initializing encryption operation and setting IV length is as follows:

* Initialise the encryption operation. */
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
    handleErrors();

/* Set IV length if default 12 bytes (96 bits) is not appropriate */
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
    handleErrors();

I had reverse order for these statements i.e. setting IV length first and then initializing encryption operation. I thought, these were independent steps and the order didn't matter. But, maybe, the interface needs to know, which encryption mechanism it has use before setting any parameters.



来源:https://stackoverflow.com/questions/39088633/unable-to-set-iv-for-aes-gcm-using-openssl

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