8 #include <botan/internal/openssl_engine.h> 9 #include <openssl/evp.h> 18 class EVP_BlockCipher :
public BlockCipher
22 std::string name()
const {
return cipher_name; }
23 BlockCipher* clone()
const;
25 size_t block_size()
const {
return block_sz; }
27 EVP_BlockCipher(
const EVP_CIPHER*,
const std::string&);
29 EVP_BlockCipher(
const EVP_CIPHER*,
const std::string&,
30 size_t,
size_t,
size_t);
32 Key_Length_Specification key_spec()
const {
return cipher_key_spec; }
36 void encrypt_n(
const byte in[],
byte out[],
size_t blocks)
const;
37 void decrypt_n(
const byte in[],
byte out[],
size_t blocks)
const;
38 void key_schedule(
const byte[],
size_t);
41 Key_Length_Specification cipher_key_spec;
42 std::string cipher_name;
43 mutable EVP_CIPHER_CTX encrypt, decrypt;
49 EVP_BlockCipher::EVP_BlockCipher(
const EVP_CIPHER* algo,
50 const std::string& algo_name) :
51 block_sz(EVP_CIPHER_block_size(algo)),
52 cipher_key_spec(EVP_CIPHER_key_length(algo)),
53 cipher_name(algo_name)
55 if(EVP_CIPHER_mode(algo) != EVP_CIPH_ECB_MODE)
58 EVP_CIPHER_CTX_init(&encrypt);
59 EVP_CIPHER_CTX_init(&decrypt);
61 EVP_EncryptInit_ex(&encrypt, algo, 0, 0, 0);
62 EVP_DecryptInit_ex(&decrypt, algo, 0, 0, 0);
64 EVP_CIPHER_CTX_set_padding(&encrypt, 0);
65 EVP_CIPHER_CTX_set_padding(&decrypt, 0);
71 EVP_BlockCipher::EVP_BlockCipher(
const EVP_CIPHER* algo,
72 const std::string& algo_name,
73 size_t key_min,
size_t key_max,
75 block_sz(EVP_CIPHER_block_size(algo)),
76 cipher_key_spec(key_min, key_max, key_mod),
77 cipher_name(algo_name)
79 if(EVP_CIPHER_mode(algo) != EVP_CIPH_ECB_MODE)
82 EVP_CIPHER_CTX_init(&encrypt);
83 EVP_CIPHER_CTX_init(&decrypt);
85 EVP_EncryptInit_ex(&encrypt, algo, 0, 0, 0);
86 EVP_DecryptInit_ex(&decrypt, algo, 0, 0, 0);
88 EVP_CIPHER_CTX_set_padding(&encrypt, 0);
89 EVP_CIPHER_CTX_set_padding(&decrypt, 0);
95 EVP_BlockCipher::~EVP_BlockCipher()
97 EVP_CIPHER_CTX_cleanup(&encrypt);
98 EVP_CIPHER_CTX_cleanup(&decrypt);
104 void EVP_BlockCipher::encrypt_n(
const byte in[],
byte out[],
108 EVP_EncryptUpdate(&encrypt, out, &out_len, in, blocks * block_sz);
114 void EVP_BlockCipher::decrypt_n(
const byte in[],
byte out[],
118 EVP_DecryptUpdate(&decrypt, out, &out_len, in, blocks * block_sz);
124 void EVP_BlockCipher::key_schedule(
const byte key[],
size_t length)
126 SecureVector<byte> full_key(key, length);
128 if(cipher_name ==
"TripleDES" && length == 16)
130 full_key += std::make_pair(key, 8);
133 if(EVP_CIPHER_CTX_set_key_length(&encrypt, length) == 0 ||
134 EVP_CIPHER_CTX_set_key_length(&decrypt, length) == 0)
138 if(cipher_name ==
"RC2")
140 EVP_CIPHER_CTX_ctrl(&encrypt, EVP_CTRL_SET_RC2_KEY_BITS, length*8, 0);
141 EVP_CIPHER_CTX_ctrl(&decrypt, EVP_CTRL_SET_RC2_KEY_BITS, length*8, 0);
144 EVP_EncryptInit_ex(&encrypt, 0, 0, full_key.begin(), 0);
145 EVP_DecryptInit_ex(&decrypt, 0, 0, full_key.begin(), 0);
151 BlockCipher* EVP_BlockCipher::clone()
const 153 return new EVP_BlockCipher(EVP_CIPHER_CTX_cipher(&encrypt),
155 cipher_key_spec.minimum_keylength(),
156 cipher_key_spec.maximum_keylength(),
157 cipher_key_spec.keylength_multiple());
163 void EVP_BlockCipher::clear()
165 const EVP_CIPHER* algo = EVP_CIPHER_CTX_cipher(&encrypt);
167 EVP_CIPHER_CTX_cleanup(&encrypt);
168 EVP_CIPHER_CTX_cleanup(&decrypt);
169 EVP_CIPHER_CTX_init(&encrypt);
170 EVP_CIPHER_CTX_init(&decrypt);
171 EVP_EncryptInit_ex(&encrypt, algo, 0, 0, 0);
172 EVP_DecryptInit_ex(&decrypt, algo, 0, 0, 0);
173 EVP_CIPHER_CTX_set_padding(&encrypt, 0);
174 EVP_CIPHER_CTX_set_padding(&decrypt, 0);
186 #define HANDLE_EVP_CIPHER(NAME, EVP) \ 187 if(request.algo_name() == NAME && request.arg_count() == 0) \ 188 return new EVP_BlockCipher(EVP, NAME); 190 #define HANDLE_EVP_CIPHER_KEYLEN(NAME, EVP, MIN, MAX, MOD) \ 191 if(request.algo_name() == NAME && request.arg_count() == 0) \ 192 return new EVP_BlockCipher(EVP, NAME, MIN, MAX, MOD); 194 #if !defined(OPENSSL_NO_AES) 204 #if !defined(OPENSSL_NO_DES) 209 #if !defined(OPENSSL_NO_BF) 213 #if !defined(OPENSSL_NO_CAST) 217 #if !defined(OPENSSL_NO_CAMELLIA) 223 #if !defined(OPENSSL_NO_RC2) 227 #if !defined(OPENSSL_NO_RC5) && 0 230 return new EVP_BlockCipher(EVP_rc5_32_12_16_ecb(),
231 "RC5(12)", 1, 32, 1);
234 #if !defined(OPENSSL_NO_IDEA) && 0 238 #if !defined(OPENSSL_NO_SEED) 242 #undef HANDLE_EVP_CIPHER 243 #undef HANDLE_EVP_CIPHER_KEYLEN
#define HANDLE_EVP_CIPHER_KEYLEN(NAME, EVP, MIN, MAX, MOD)
size_t arg_as_integer(size_t i, size_t def_value) const
std::invalid_argument Invalid_Argument
#define HANDLE_EVP_CIPHER(NAME, EVP)
BlockCipher * find_block_cipher(const SCAN_Name &, Algorithm_Factory &) const
std::string algo_name() const