Cipher type BIO
--- Translation according to OpenSSL DOC / CRYPTO / BIO_F_CIPHER.POD and your understanding
(Author: DragonKing, Mail: wzhah@263.net, Posted on: http: //gdwzh.126.com of openssl professional forum)
This type is filter type BIO, which is defined as follows (OpenSSL / BIO.H, OpenSSL / EVP.H):
BIO_METHOD * BIO_F_CIPHER (VOID);
Void Bio_Set_CIpher (Bio * B, Const EVP_CIPHER * CIPHER,
Unsigned char * key, unsigned char * iv, int enc);
INT BIO_GET_CIPHER_STATUS (BIO * B)
INT BIO_GET_CIPHER_CTX (BIO * B, EVP_CIPHER_CTX ** PCTX)
[BIO_F_CIPHER]
This function returns the Cipher type BIO_METHOD structure, which is defined as follows (EVP / BIO_ENC.C):
Static Bio_Method Methods_enc =
{
Bio_type_cipher, "cipher",
ENC_WRITE,
ENC_READ,
NULL, / * ENC_PUTS, * /
NULL, / * ENC_GETS, * /
ENC_CTRL,
ENC_NEW,
ENC_FREE,
ENC_CALLBACK_CTRL,
}
This type of BIO will write the data encryption of the BIO, and data is decrypted from the BIO read data. It is in fact to encapsulate EVP_CIPHERINIT, EVP_CIPHERUPDATE, EVP_CIPHERFINAL three methods. It does not support BIO_PUTS and BIO_GETS methods, and if you want to use these two methods, you can implement it by adding a BIO of a buffer type in front, which is introduced before.
Similar to the Base64 Bio, when the BIO_FLUSH function is called, it indicates that all data has been encrypted by this type BIO, and the last segment of data is encrypted by the BIO. At the time of encryption, the BIO_FLUSH function must be called to encrypt the last data via the BIO, otherwise the last data will fail when decrypt. When data is read from a encrypted type BIO, when the last paragraph data is read, the data end flag is automatically detected by detecting EOF and the data is automatically decrypted.
[BIO_SET_CIPHER]
This function sets the encryption algorithm of the BIO, the data uses the parameter key as the encryption key, the parameter IV as an encrypted IV (initialization vector). If the ENC is set to 1, it is encrypted, and the ENC is set to 0, it is decrypted. This function does not return a value.
[BIO_GET_CIPHER_STATUS]]
This function is a Bio_Ctrl macro to detect whether decryption is successful. Because when decryption (when performing a read operation), if the last paragraph of data occurs, it will return 0, and it will return 0 after the EOF successfully completed operation, so the function must be called to determine if the decryption operation is successfully executed. The decryption successfully returns 1, otherwise it returns 0.
[BIO_GET_CIPHER_CTX]
This function is also a macro definition function of BIO_CTRL, which returns the internal encryption system of the BIO. The returned encryption system can be set using standard encryption rules. This is useful when the flexibility of the BIO_SET_CIPHER function cannot adapt to the need for the application. This function always returns 1.