Standard preamble:
========================================================================
..
.... Set up some character translations and predefined strings. \*(-- will
give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
double quote, and \*(R" will give a right double quote. \*(C+ will
give a nicer C++. Capital omega is used to do unbreakable dashes and
therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
nothing in troff, for use with C<>.
.tr \(*W- . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\}
Escape single quotes in literal strings from groff's Unicode transform.
If the F register is >0, we'll generate index entries on stderr for
titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
entries marked with X<> in POD. Of course, you'll have to process the
output yourself in some meaningful fashion.
Avoid warning from groff about undefined register 'F'.
.. .nr rF 0 . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF
Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] .\} . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents . \" corrections for vroff . \" for low resolution devices (crt and lpr) \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} ========================================================================
Title "EVP_ENCRYPTINIT 3"
way too many mistakes in technical documents.
\fBEVP_CIPHER_CTX_new() creates a cipher context.
\fBEVP_CIPHER_CTX_free() clears all information from a cipher context and free up any allocated memory associate with it, including ctx itself. This function should be called after all operations using a cipher are complete so sensitive information does not remain in memory.
\fBEVP_EncryptInit_ex() sets up cipher context ctx for encryption with cipher type from \s-1ENGINE\s0 impl. ctx must be created before calling this function. type is normally supplied by a function such as EVP_aes_256_cbc(). If impl is \s-1NULL\s0 then the default implementation is used. key is the symmetric key to use and iv is the \s-1IV\s0 to use (if necessary), the actual number of bytes used for the key and \s-1IV\s0 depends on the cipher. It is possible to set all parameters to \s-1NULL\s0 except type in an initial call and supply the remaining parameters in subsequent calls, all of which have type set to \s-1NULL.\s0 This is done when the default cipher parameters are not appropriate.
\fBEVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes the encrypted version to out. This function can be called multiple times to encrypt successive blocks of data. The amount of data written depends on the block alignment of the encrypted data. For most ciphers and modes, the amount of data written can be anything from zero bytes to (inl + cipher_block_size - 1) bytes. For wrap cipher modes, the amount of data written can be anything from zero bytes to (inl + cipher_block_size) bytes. For stream ciphers, the amount of data written can be anything from zero bytes to inl bytes. Thus, out should contain sufficient room for the operation being performed. The actual number of bytes written is placed in outl. It also checks if in and out are partially overlapping, and if they are 0 is returned to indicate failure.
If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts the \*(L"final\*(R" data, that is any data that remains in a partial block. It uses standard block padding (aka \s-1PKCS\s0 padding) as described in the \s-1NOTES\s0 section, below. The encrypted final data is written to out which should have sufficient space for one cipher block. The number of bytes written is placed in outl. After this function is called the encryption operation is finished and no further calls to EVP_EncryptUpdate() should be made.
If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more data and it will return an error if any data remains in a partial block: that is if the total data length is not a multiple of the block size.
\fBEVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the corresponding decryption operations. EVP_DecryptFinal() will return an error code if padding is enabled and the final block is not correctly formatted. The parameters and restrictions are identical to the encryption operations except that if padding is enabled the decrypted data buffer out passed to EVP_DecryptUpdate() should have sufficient room for (inl + cipher_block_size) bytes unless the cipher block size is 1 in which case inl bytes is sufficient.
\fBEVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex() are functions that can be used for decryption or encryption. The operation performed depends on the value of the enc parameter. It should be set to 1 for encryption, 0 for decryption and -1 to leave the value unchanged (the actual value of 'enc' being supplied in a previous call).
\fBEVP_CIPHER_CTX_reset() clears all information from a cipher context and free up any allocated memory associate with it, except the ctx itself. This function should be called anytime ctx is to be reused for another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of calls.
\fBEVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit() behave in a similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and \fBEVP_CipherInit_ex() except they always use the default cipher implementation.
\fBEVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal() are identical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and \fBEVP_CipherFinal_ex(). In previous releases they also cleaned up the ctx, but this is no longer done and EVP_CIPHER_CTX_clean() must be called to free any context resources.
\fBEVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() return an \s-1EVP_CIPHER\s0 structure when passed a cipher name, a \s-1NID\s0 or an \s-1ASN1_OBJECT\s0 structure.
\fBEVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return the \s-1NID\s0 of a cipher when passed an \s-1EVP_CIPHER\s0 or \s-1EVP_CIPHER_CTX\s0 structure. The actual \s-1NID\s0 value is an internal value which may not have a corresponding \s-1OBJECT IDENTIFIER.\s0
\fBEVP_CIPHER_CTX_set_padding() enables or disables padding. This function should be called after the context is set up for encryption or decryption with EVP_EncryptInit_ex(), EVP_DecryptInit_ex() or \fBEVP_CipherInit_ex(). By default encryption operations are padded using standard block padding and the padding is checked and removed when decrypting. If the pad parameter is zero then no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of the block size or an error will occur.
\fBEVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key length of a cipher when passed an \s-1EVP_CIPHER\s0 or \s-1EVP_CIPHER_CTX\s0 structure. The constant \s-1EVP_MAX_KEY_LENGTH\s0 is the maximum key length for all ciphers. Note: although EVP_CIPHER_key_length() is fixed for a given cipher, the value of EVP_CIPHER_CTX_key_length() may be different for variable key length ciphers.
\fBEVP_CIPHER_CTX_set_key_length() sets the key length of the cipher ctx. If the cipher is a fixed length cipher then attempting to set the key length to any value other than the fixed value is an error.
\fBEVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the \s-1IV\s0 length of a cipher when passed an \s-1EVP_CIPHER\s0 or \s-1EVP_CIPHER_CTX\s0. It will return zero if the cipher does not use an \s-1IV.\s0 The constant \fB\s-1EVP_MAX_IV_LENGTH\s0 is the maximum \s-1IV\s0 length for all ciphers.
\fBEVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block size of a cipher when passed an \s-1EVP_CIPHER\s0 or \s-1EVP_CIPHER_CTX\s0 structure. The constant \s-1EVP_MAX_BLOCK_LENGTH\s0 is also the maximum block length for all ciphers.
\fBEVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the type of the passed cipher or context. This \*(L"type\*(R" is the actual \s-1NID\s0 of the cipher \s-1OBJECT IDENTIFIER\s0 as such it ignores the cipher parameters and 40 bit \s-1RC2\s0 and 128 bit \s-1RC2\s0 have the same \s-1NID.\s0 If the cipher does not have an object identifier or does not have \s-1ASN1\s0 support this function will return \fBNID_undef.
\fBEVP_CIPHER_CTX_cipher() returns the \s-1EVP_CIPHER\s0 structure when passed an \s-1EVP_CIPHER_CTX\s0 structure.
\fBEVP_CIPHER_mode() and EVP_CIPHER_CTX_mode() return the block cipher mode: \s-1EVP_CIPH_ECB_MODE, EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE, EVP_CIPH_OFB_MODE, EVP_CIPH_CTR_MODE, EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE, EVP_CIPH_XTS_MODE, EVP_CIPH_WRAP_MODE\s0 or \s-1EVP_CIPH_OCB_MODE.\s0 If the cipher is a stream cipher then \s-1EVP_CIPH_STREAM_CIPHER\s0 is returned.
\fBEVP_CIPHER_param_to_asn1() sets the AlgorithmIdentifier \*(L"parameter\*(R" based on the passed cipher. This will typically include any parameters and an \s-1IV.\s0 The cipher \s-1IV\s0 (if any) must be set when this call is made. This call should be made before the cipher is actually \*(L"used\*(R" (before any \fBEVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). This function may fail if the cipher does not have any \s-1ASN1\s0 support.
\fBEVP_CIPHER_asn1_to_param() sets the cipher parameters based on an \s-1ASN1\s0 AlgorithmIdentifier \*(L"parameter\*(R". The precise effect depends on the cipher In the case of \s-1RC2,\s0 for example, it will set the \s-1IV\s0 and effective key length. This function should be called after the base cipher type is set but before the key is set. For example EVP_CipherInit() will be called with the \s-1IV\s0 and key set to \s-1NULL,\s0 EVP_CIPHER_asn1_to_param() will be called and finally \fBEVP_CipherInit() again with all parameters except the key set to \s-1NULL.\s0 It is possible for this function to fail if the cipher does not have any \s-1ASN1\s0 support or the parameters cannot be set (for example the \s-1RC2\s0 effective key length is not supported.
\fBEVP_CIPHER_CTX_ctrl() allows various cipher specific parameters to be determined and set.
\fBEVP_CIPHER_CTX_rand_key() generates a random key of the appropriate length based on the cipher context. The \s-1EVP_CIPHER\s0 can provide its own random key generation routine to support keys of a specific form. Key must point to a buffer at least as big as the value returned by EVP_CIPHER_CTX_key_length().
\fBEVP_EncryptInit_ex(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex() return 1 for success and 0 for failure.
\fBEVP_DecryptInit_ex() and EVP_DecryptUpdate() return 1 for success and 0 for failure. \fBEVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for success.
\fBEVP_CipherInit_ex() and EVP_CipherUpdate() return 1 for success and 0 for failure. \fBEVP_CipherFinal_ex() returns 0 for a decryption failure or 1 for success.
\fBEVP_CIPHER_CTX_reset() returns 1 for success and 0 for failure.
\fBEVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() return an \s-1EVP_CIPHER\s0 structure or \s-1NULL\s0 on error.
\fBEVP_CIPHER_nid() and EVP_CIPHER_CTX_nid() return a \s-1NID.\s0
\fBEVP_CIPHER_block_size() and EVP_CIPHER_CTX_block_size() return the block size.
\fBEVP_CIPHER_key_length() and EVP_CIPHER_CTX_key_length() return the key length.
\fBEVP_CIPHER_CTX_set_padding() always returns 1.
\fBEVP_CIPHER_iv_length() and EVP_CIPHER_CTX_iv_length() return the \s-1IV\s0 length, zero if the cipher does not use an \s-1IV\s0 and a negative value on error.
\fBEVP_CIPHER_type() and EVP_CIPHER_CTX_type() return the \s-1NID\s0 of the cipher's \s-1OBJECT IDENTIFIER\s0 or NID_undef if it has no defined \s-1OBJECT IDENTIFIER.\s0
\fBEVP_CIPHER_CTX_cipher() returns an \s-1EVP_CIPHER\s0 structure.
\fBEVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() return greater than zero for success and zero or a negative number on failure.
\fBEVP_CIPHER_CTX_rand_key() returns 1 for success.
Refer to \*(L"\s-1SEE ALSO\*(R"\s0 for the full list of ciphers available through the \s-1EVP\s0 interface.
To specify additional authenticated data (\s-1AAD\s0), a call to EVP_CipherUpdate(), \fBEVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with the output parameter out set to \s-1NULL\s0.
When decrypting, the return value of EVP_DecryptFinal() or EVP_CipherFinal() indicates whether the operation was successful. If it does not indicate success, the authentication operation has failed and any output data \s-1MUST NOT\s0 be used as it is corrupted.
For \s-1CCM\s0 mode, the total plaintext or ciphertext length \s-1MUST\s0 be passed to \fBEVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output and input parameters (in and out) set to \s-1NULL\s0 and the length passed in the inl parameter.
The following ctrls are supported in \s-1CCM\s0 mode.
\s-1PKCS\s0 padding works by adding n padding bytes of value n to make the total length of the encrypted data a multiple of the block size. Padding is always added so if the data is already a multiple of the block size n will equal the block size. For example if the block size is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5 will be added.
When decrypting the final block is checked to see if it has the correct form.
Although the decryption operation can produce an error if padding is enabled, it is not a strong test that the input data or key is correct. A random block has better than 1 in 256 chance of being of the correct format and problems with the input data earlier on will not produce a final decrypt error.
If padding is disabled then the decryption operation will always succeed if the total amount of data decrypted is a multiple of the block size.
The functions EVP_EncryptInit(), EVP_EncryptFinal(), EVP_DecryptInit(), \fBEVP_CipherInit() and EVP_CipherFinal() are obsolete but are retained for compatibility with existing code. New code should use EVP_EncryptInit_ex(), \fBEVP_EncryptFinal_ex(), EVP_DecryptInit_ex(), EVP_DecryptFinal_ex(), \fBEVP_CipherInit_ex() and EVP_CipherFinal_ex() because they can reuse an existing context without allocating and freeing it up on each call.
There are some differences between functions EVP_CipherInit() and \fBEVP_CipherInit_ex(), significant in some circumstances. EVP_CipherInit() fills the passed context object with zeros. As a consequence, EVP_CipherInit() does not allow step-by-step initialization of the ctx when the key and iv are passed in separate calls. It also means that the flags set for the \s-1CTX\s0 are removed, and it is especially important for the \fB\s-1EVP_CIPHER_CTX_FLAG_WRAP_ALLOW\s0 flag treated specially in \fBEVP_CipherInit_ex().
\fBEVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as macros.
The \s-1ASN1\s0 code is incomplete (and sometimes inaccurate) it has only been tested for certain common S/MIME ciphers (\s-1RC2, DES,\s0 triple \s-1DES\s0) in \s-1CBC\s0 mode.
.Vb 10 int do_crypt(char *outfile) { unsigned char outbuf[1024]; int outlen, tmplen; /* * Bogus key and IV: we\*(Aqd normally set these from * another source. */ unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; unsigned char iv[] = {1,2,3,4,5,6,7,8}; char intext[] = "Some Crypto Text"; EVP_CIPHER_CTX *ctx; FILE *out; \& ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv); \& if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) { /* Error */ EVP_CIPHER_CTX_free(ctx); return 0; } /* * Buffer passed to EVP_EncryptFinal() must be after data just * encrypted to avoid overwriting it. */ if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) { /* Error */ EVP_CIPHER_CTX_free(ctx); return 0; } outlen += tmplen; EVP_CIPHER_CTX_free(ctx); /* * Need binary mode for fopen because encrypted data is * binary data. Also cannot use strlen() on it because * it won\*(Aqt be NUL terminated and may contain embedded * NULs. */ out = fopen(outfile, "wb"); if (out == NULL) { /* Error */ return 0; } fwrite(outbuf, 1, outlen, out); fclose(out); return 1; } .Ve
The ciphertext from the above example can be decrypted using the openssl utility with the command line (shown on two lines for clarity):
.Vb 2 openssl idea -d \e -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename .Ve
General encryption and decryption function example using \s-1FILE I/O\s0 and \s-1AES128\s0 with a 128-bit key:
.Vb 12 int do_crypt(FILE *in, FILE *out, int do_encrypt) { /* Allow enough space in output buffer for additional block */ unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; int inlen, outlen; EVP_CIPHER_CTX *ctx; /* * Bogus key and IV: we\*(Aqd normally set these from * another source. */ unsigned char key[] = "0123456789abcdeF"; unsigned char iv[] = "1234567887654321"; \& /* Don\*(Aqt set key or IV right away; we want to check lengths */ ctx = EVP_CIPHER_CTX_new(); EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL, do_encrypt); OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16); OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16); \& /* Now we can set key and IV */ EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt); \& for (;;) { inlen = fread(inbuf, 1, 1024, in); if (inlen <= 0) break; if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) { /* Error */ EVP_CIPHER_CTX_free(ctx); return 0; } fwrite(outbuf, 1, outlen, out); } if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) { /* Error */ EVP_CIPHER_CTX_free(ctx); return 0; } fwrite(outbuf, 1, outlen, out); \& EVP_CIPHER_CTX_free(ctx); return 1; } .Ve
Supported ciphers are listed in:
\fBEVP_aes\|(3), \fBEVP_aria\|(3), \fBEVP_bf\|(3), \fBEVP_camellia\|(3), \fBEVP_cast5\|(3), \fBEVP_chacha20\|(3), \fBEVP_des\|(3), \fBEVP_desx\|(3), \fBEVP_idea\|(3), \fBEVP_rc2\|(3), \fBEVP_rc4\|(3), \fBEVP_rc5\|(3), \fBEVP_seed\|(3), \fBEVP_sm4\|(3)
\fB\s-1EVP_CIPHER_CTX\s0 was made opaque in OpenSSL 1.1.0. As a result, \fBEVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup() disappeared. EVP_CIPHER_CTX_init() remains as an alias for \fBEVP_CIPHER_CTX_reset().
Licensed under the OpenSSL license (the \*(L"License\*(R"). You may not use this file except in compliance with the License. You can obtain a copy in the file \s-1LICENSE\s0 in the source distribution or at <https://www.openssl.org/source/license.html>.