Sortix cross-volatile manual
This manual documents Sortix cross-volatile. You can instead view this document in the latest official manual.
NAME
EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_reset, EVP_CIPHER_CTX_free, EVP_CIPHER_CTX_copy, EVP_EncryptInit_ex, EVP_EncryptUpdate, EVP_EncryptFinal_ex, EVP_DecryptInit_ex, EVP_DecryptUpdate, EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherUpdate, EVP_CipherFinal_ex, EVP_EncryptInit, EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal, EVP_CipherInit, EVP_CipherFinal, EVP_CIPHER_CTX_encrypting, EVP_get_cipherbyname, EVP_get_cipherbynid, EVP_get_cipherbyobj, EVP_CIPHER_CTX_cipher, EVP_enc_null, EVP_idea_cbc, EVP_idea_ecb, EVP_idea_cfb64, EVP_idea_cfb, EVP_idea_ofb, EVP_bf_cbc, EVP_bf_ecb, EVP_bf_cfb64, EVP_bf_cfb, EVP_bf_ofb, EVP_cast5_cbc, EVP_cast5_ecb, EVP_cast5_cfb64, EVP_cast5_cfb, EVP_cast5_ofb — EVP cipher routinesSYNOPSIS
library “libcrypto”#include <openssl/evp.h>
EVP_CIPHER_CTX_new(void);
EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in);
EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *engine, const unsigned char *key, const unsigned char *iv);
EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len, const unsigned char *in, int in_len);
EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len);
EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *engine, const unsigned char *key, const unsigned char *iv);
EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len, const unsigned char *in, int in_len);
EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len);
EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *engine, const unsigned char *key, const unsigned char *iv, int enc);
EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len, const unsigned char *in, int in_len);
EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len);
EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, const unsigned char *key, const unsigned char *iv);
EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len);
EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, const unsigned char *key, const unsigned char *iv);
EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len);
EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, const unsigned char *key, const unsigned char *iv, int enc);
EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len);
EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx);
EVP_get_cipherbyname(const char *name);
EVP_get_cipherbynid(int nid);
EVP_get_cipherbyobj(const ASN1_OBJECT *a);
EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);
DESCRIPTION
The EVP cipher routines are a high level interface to certain symmetric ciphers.0, and out such that the cipher implementation can perform further algorithm- and implementation-specific initializations after the algorithm- and implementation-specific cipher data has been copied. Among the cipher algorithms built into the library, EVP_CIPH_CUSTOM_COPY and EVP_CTRL_COPY are used by some of the ciphers documented in the EVP_aes_256_gcm(3) manual page.+ cipher_block_size - 1) so out should contain sufficient room. The actual number of bytes written is placed in *out_len.+ cipher_block_size) bytes unless the cipher block size is 1 in which case in_len bytes is sufficient.RETURN VALUES
EVP_CIPHER_CTX_new() returns a pointer to a newly created EVP_CIPHER_CTX for success or NULL for failure.CIPHER LISTING
- EVP_enc_null()
- Null cipher: does nothing.
- EVP_idea_cbc(), EVP_idea_ecb(), EVP_idea_cfb64(), EVP_idea_ofb()
- IDEA encryption algorithm in CBC, ECB, CFB and OFB modes respectively. IDEA is a block cipher operating on 64 bit blocks using a 128 bit key. EVP_idea_cfb() is an alias for EVP_idea_cfb64(), implemented as a macro.
- EVP_bf_cbc(), EVP_bf_ecb(), EVP_bf_cfb64(), EVP_bf_ofb()
- Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes respectively. Blowfish is a block cipher operating on 64 bit blocks using a variable key length. The default key length is 128 bits. EVP_bf_cfb() is an alias for EVP_bf_cfb64(), implemented as a macro.
- EVP_cast5_cbc(), EVP_cast5_ecb(), EVP_cast5_cfb64(), EVP_cast5_ofb()
- CAST-128 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. CAST-128 is a block cipher operating on 64 bit blocks using a variable key length. The default and maximum key length is 128 bits. EVP_cast5_cfb() is an alias for EVP_cast5_cfb64(), implemented as a macro.
| manual page | block size |
key size (in bits) |
| EVP_aes_128_cbc(3) | 128 | 128, 192, 256 |
| EVP_aes_128_ccm(3) | 128 | 128, 192, 256 |
| EVP_aes_128_gcm(3) | 128 | 128, 192, 256 |
| EVP_camellia_128_cbc(3) | 128 | 128, 192, 256 |
| EVP_chacha20(3) | stream | 256 |
| EVP_des_cbc(3) | 64 | 64 |
| EVP_rc2_cbc(3) | 64 | variable, default 128 |
| EVP_rc4(3) | stream | variable, default 128 |
| EVP_sm4_cbc(3) | 128 | 128 |
EXAMPLES
Encrypt a string using blowfish:
int
do_crypt(char *out_filename)
{
unsigned char out_buf[1024];
int out_len, tmp_len;
/*
* Bogus key and IV: we'd 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};
const char in_text[] = "Some Crypto Text";
EVP_CIPHER_CTX *ctx;
FILE *out_fileptr;
ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_bf_cbc(), NULL, key, iv);
if (!EVP_EncryptUpdate(ctx, out_buf, &out_len, in_text,
strlen(in_text))) {
/* 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, out_buf + out_len, &tmp_len)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
out_len += tmp_len;
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't be NUL terminated and may contain embedded
* NULs.
*/
out_fileptr = fopen(out_filename, "wb");
if (out_fileptr == NULL) {
/* Error */
return 0;
}
fwrite(out_buf, 1, out_len, out_fileptr);
fclose(out_fileptr);
return 1;
}
openssl bf -in cipher.bin -K 000102030405060708090A0B0C0D0E0F \
-iv 0102030405060708 -d
int
do_crypt(FILE *in_fileptr, FILE *out_fileptr, int do_encrypt)
{
/* Allow enough space in output buffer for additional block */
unsigned char in_buf[1024], out_buf[1024 + EVP_MAX_BLOCK_LENGTH];
int in_len, out_len;
EVP_CIPHER_CTX *ctx;
/*
* Bogus key and IV: we'd normally set these from
* another source.
*/
unsigned char key[] = "0123456789abcdeF";
unsigned char iv[] = "1234567887654321";
ctx = EVP_CIPHER_CTX_new();
EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
do_encrypt);
EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
for (;;) {
in_len = fread(in_buf, 1, 1024, in_fileptr);
if (in_len <= 0)
break;
if (!EVP_CipherUpdate(ctx, out_buf, &out_len, in_buf,
in_len)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
fwrite(out_buf, 1, out_len, out_fileptr);
}
if (!EVP_CipherFinal_ex(ctx, out_buf, &out_len)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
fwrite(out_buf, 1, out_len, out_fileptr);
EVP_CIPHER_CTX_free(ctx);
return 1;
}