OpenSSL EVP Series 5 --- EVP_ENCRYPT Series Function Detailed (2)
--- Translation and your own understanding
(Author: DragonKing, Mail: wzhah@263.net, Posted on: http: //gdwzh.126.com of openssl professional forums, version: openssl-0.9.7)
The previous article we introduced the basic part of the EVP_Encrypt Series function. This article will introduce some of their expansion sections, which is some parameter settings and other auxiliary functions, which are defined as follows (OpenSSL / EVP.H):
INT EVP_CIPHER_CTX_SET_PADDING (EVP_CIPHER_CTX * X, INT PADDING);
INT EVP_CIPHER_CTX_SET_KEY_LENGTH (EVP_CIPHER_CTX * X, INT KEYLEN);
Const EVP_CIPHER * EVP_GET_CIPHERBYNAME (Const Char * Name);
#DEFINE EVP_GET_CIPHERBYNID (A) EVP_GET_CIPHERBYNAME (Obj_nid2Sn (a))
#define evp_get_cipherbyobj (a) EVP_GET_CIPHERBYNID (Obj_obj2nid (a))
INT EVP_CIPHER_CTX_CLEANUP (EVP_CIPHER_CTX * A);
#define evp_cipher_nid (e) -> NID)
#define evp_cipher_block_size (e) ((e) -> block_size)
#DEFINE EVP_CIPHER_KEY_LENGTH (E) ((e) -> Key_LEN)
#DEFINE EVP_CIPHER_IV_LENGTH (E) ((e) -> IV_LEN)
#define evp_cipher_flags (e) ((e) -> Flags)
#DEFINE EVP_CIPHER_MODE (E) ((e) -> Flags) & EVP_CIPH_MODE)
INT EVP_CIPHER_TYPE (const EVP_CIPHER * CTX);
#define evp_cipher_ctx_cipher (e) ((e) -> cipher)
#define evp_cipher_ctx_nid (e) ((e) -> cipher-> nid)
#define evp_cipher_ctx_block_size (e) ((e) -> cipher-> block_size)
#define evp_cipher_ctx_key_length (e) ((e) -> key_len)
#define evp_cipher_ctx_iv_length (e) ((e) -> cipher-> iv_len)
#DEFINE EVP_CIPHER_CTX_GET_APP_DATA (E) ((e) -> app_data)
#define evp_cipher_ctx_set_app_data (e, d) ((e) -> app_data = (char *) (d))
#define evp_cipher_ctx_type (c) EVP_CIPHER_TYPE (EVP_CIPHER_CTX_CIPHER (C))
#define evp_cipher_ctx_flags (e) ((e) -> Cipher-> Flags)
#define evp_cipher_ctx_mode (e) ((e) -> Cipher-> Flags & EVP_CIPH_MODE) INT EVP_CIPHER_PARAM_TO_ASN1 (EVP_CIPHER_CTX * C, ASN1_TYPE * TYPE);
INT EVP_CIPHER_ASN1_TO_PARAM (EVP_CIPHER_CTX * C, ASN1_TYPE * TYPE);
[Evp_cipher_ctx_set_padding]
This function is set to use a padding function. In the case of the algorithm, it is the use of standard block Padding features, and Padding is automatically received when decrypting and deletes it. If the parameter PAD is set to 0, the padding function will be disabled, then the data should be an integer multiple of the length of the encryption block when encrypted and decrypts, otherwise it will be wrong. Function constant returns 1.
[Evp_cipher_ctx_set_key_length]
This function performs the setting of the EVP_CIPHER_CTX key length of the encryption algorithm structure. If the algorithm is a key length fixed algorithm, then if the length of the set key is not consistent with its fixed length, it will generate an error.
[EVP_GET_CIPHERBYNAME, EVP_GET_CIPHERBYNID and EVP_GET_CIPHERBYOBJ]
These three functions returns an EVP_CIPHER structure according to a given parameter, and the given parameters are the algorithm name, the NID of the algorithm and an ASN1_Object structure. Specific algorithm name, NID, and ASN1_Object structures, please refer to the definition of the object / boject.h file.
[EVP_CIPHER_NID and EVP_CIPHER_CTX_NID]
These two functions returned to the NID of the algorithm inside the EVP_CIPHER or the EVP_CIPHER_CTX structure. The returned NID value is just an internal storage value and does not necessarily have a corresponding object definition.
[EVP_CIPHER_KEY_LENGTH and EVP_CIPHER_CTX_KEY_LENGTH]
These two functions returns the key length of the algorithm inside the EVP_CIPHER or the EVP_CIPHER_CTX structure. Constant EVP_MAX_KEY_LENGTH defines the longest key length of all algorithms. It should be noted that for the EVP_CIPHER_KEY_LENGTH function, the length of the specific algorithm key is constant, but the EVP_CIPHER_CTX_KEY_LENGTH function is variable to the same algorithm key length.
[EVP_CIPHER_IV_LENGTH and EVP_CIPHER_CTX_IV_LENGTH]
These two functions returned to the initialization vector length of the algorithm inside the EVP_CIPHER or EVP_CIPHER_CTX structure. If the algorithm does not use IV, it will return 0. Constant EVP_MAX_IV_LENGTH defines all algorithms to the longest IV length
[EVP_CIPHER_BLOCK_SIZE and EVP_CIPHER_CTX_BLOCK_SIZE]
These two functions returned to the encryption block length of the algorithm inside the EVP_CIPHER or EVP_CIPHER_CTX structure. Constant EVP_MAX_IV_LENGTH is also the longest block length of all algorithms.
[EVP_CIPHER_TYPE and EVP_CIPHER_CTX_TYPE]
These two functions returns the type of algorithm inside the EVP_CIPHER or EVP_CIPHER_CTX structure. The value of this type is NID of the algorithm. In general, NID ignores some of the parameters of the algorithm, such as the NID of the 40-bit and 129-bit RC2 algorithms. If the algorithm does not have the corresponding defined NID or not supported by ASN1, this function will return NID_UNDEF.
[EVP_CIPHER_CTX_CIPHER]
This function returns the EVP_CIPHER structure in the EVP_CIPHER_CTX structure.
[EVP_CIPHER_MODE and EVP_CIPHER_CTX_MODE]
These two functions return block encryption mode of the corresponding structural algorithm, including EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE, and EVP_CIPH_OFB_MODE; if the algorithm is a stream incidence algorithm, then returns EVP_CIPH_STREAM_CIPHER. [EVP_CIPHER_PARAM_TO_ASN1]
This function sets the parameters of the algorithm structure, which is generally set to include all parameters and an IV value. If the algorithm has IV, IV is required when calling the function. This function must be called before the algorithm structure set (such as calling evP_encryptupdate and evp_decryptupdate functions). If the ASN1 does not support the algorithm, then call the function will result in failure. Operation successfully returns 1, otherwise it returns 0.
[EVP_CIPHER_ASN1_TO_PARAM]
This function sets the structure of the parameter TYPE in the value of the algorithm structure. The contents of which are determined by the specific algorithm. As in the RC2 algorithm, it sets the IV and valid key length. This function should be set in the basic algorithm type of the algorithm structure but the key is not set yet. For example, use the parameter IV when calling the evp_cipherinit function, and sets the key null, and then call this function, then call EVP_CIPHERINIT, which is in addition to the key setting bit NULL should be set. When the ASN1 does not support the algorithm or if the parameter cannot be set (such as the valid key length of RC2), the function call will fail. Operation successfully returns 1, otherwise it returns 0.
[Evp_cipher_ctx_ctrl]
This function can set specific parameters of different algorithms. Currently only the effective key length of the RC2 algorithm and the number of encryption of the RC5 algorithm are available.
BTW: I feel a little slower, I know that everyone wants to know how to program, but first introduce so many functions, and it will be more relaxed below, the next article will introduce Evp_encrypt * ... * Series function programming framework and several examples.