18 #include <openssl/evp.h> 19 #include <openssl/hmac.h> 20 #include <openssl/rand.h> 25 #include <vpp/app/version.h> 32 #if OPENSSL_VERSION_NUMBER < 0x10100000L 39 #define foreach_openssl_evp_op \ 40 _(cbc, DES_CBC, EVP_des_cbc) \ 41 _(cbc, 3DES_CBC, EVP_des_ede3_cbc) \ 42 _(cbc, AES_128_CBC, EVP_aes_128_cbc) \ 43 _(cbc, AES_192_CBC, EVP_aes_192_cbc) \ 44 _(cbc, AES_256_CBC, EVP_aes_256_cbc) \ 45 _(gcm, AES_128_GCM, EVP_aes_128_gcm) \ 46 _(gcm, AES_192_GCM, EVP_aes_192_gcm) \ 47 _(gcm, AES_256_GCM, EVP_aes_256_gcm) 49 #define foreach_openssl_hmac_op \ 52 _(SHA224, EVP_sha224) \ 53 _(SHA256, EVP_sha256) \ 54 _(SHA384, EVP_sha384) \ 59 const EVP_CIPHER * cipher)
65 for (i = 0; i < n_ops; i++)
71 RAND_bytes (op->
iv, 16);
73 EVP_EncryptInit_ex (ctx, cipher,
NULL, op->
key, op->
iv);
74 EVP_EncryptUpdate (ctx, op->
dst, &out_len, op->
src, op->
len);
75 if (out_len < op->
len)
76 EVP_EncryptFinal_ex (ctx, op->
dst + out_len, &out_len);
77 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
84 const EVP_CIPHER * cipher)
90 for (i = 0; i < n_ops; i++)
95 EVP_DecryptInit_ex (ctx, cipher,
NULL, op->
key, op->
iv);
96 EVP_DecryptUpdate (ctx, op->
dst, &out_len, op->
src, op->
len);
97 if (out_len < op->
len)
98 EVP_DecryptFinal_ex (ctx, op->
dst + out_len, &out_len);
99 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
106 const EVP_CIPHER * cipher)
112 for (i = 0; i < n_ops; i++)
119 RAND_bytes (op->
iv, 8);
124 EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
125 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12,
NULL);
126 EVP_EncryptInit_ex (ctx, 0, 0, op->
key, (
u8 *) nonce);
129 EVP_EncryptUpdate (ctx, op->
dst, &len, op->
src, op->
len);
130 EVP_EncryptFinal_ex (ctx, op->
dst + len, &len);
131 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_GET_TAG, op->
tag_len, op->
tag);
132 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
139 const EVP_CIPHER * cipher)
145 for (i = 0; i < n_ops; i++)
150 EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
151 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, op->
iv_len, 0);
152 EVP_DecryptInit_ex (ctx, 0, 0, op->
key, op->
iv);
154 EVP_DecryptUpdate (ctx, 0, &len, op->
aad, op->
aad_len);
155 EVP_DecryptUpdate (ctx, op->
dst, &len, op->
src, op->
len);
156 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_TAG, op->
tag_len, op->
tag);
158 if (EVP_DecryptFinal_ex (ctx, op->
dst + len, &len) > 0)
159 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
163 op->
status = VNET_CRYPTO_OP_STATUS_FAIL_DECRYPT;
166 return n_ops - n_fail;
178 for (i = 0; i < n_ops; i++)
181 unsigned int out_len;
185 HMAC_Update (ctx, op->
src, op->
len);
186 HMAC_Final (ctx, buffer, &out_len);
190 if ((memcmp (op->
digest, buffer, sz)))
193 op->
status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
199 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
201 return n_ops - n_fail;
206 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \ 207 { return openssl_ops_enc_##m (vm, ops, n_ops, b ()); } \ 210 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \ 211 { return openssl_ops_dec_##m (vm, ops, n_ops, b ()); } 218 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \ 219 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \ 241 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \ 242 openssl_ops_enc_##a); \ 243 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \ 244 openssl_ops_dec_##a); 250 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \ 251 openssl_ops_hmac_##a); \ 262 #if OPENSSL_VERSION_NUMBER >= 0x10100000L 265 HMAC_CTX_init (&(ptd->_hmac_ctx));
272 vec_add (seed_data, &t,
sizeof (t));
273 vec_add (seed_data, &pid,
sizeof (pid));
274 vec_add (seed_data, seed_data,
sizeof (seed_data));
276 RAND_seed ((
const void *) seed_data,
vec_len (seed_data));
287 .version = VPP_BUILD_VER,
288 .description =
"OpenSSL Crypto Engine",
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
static_always_inline u32 openssl_ops_enc_cbc(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
#define clib_memcpy_fast(a, b, c)
static_always_inline u32 openssl_ops_dec_cbc(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
#define static_always_inline
#define VLIB_INIT_FUNCTION(x)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define vlib_call_init_function(vm, x)
clib_error_t * crypto_openssl_init(vlib_main_t *vm)
#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK
EVP_CIPHER_CTX * evp_cipher_ctx
#define foreach_openssl_hmac_op
#define VNET_CRYPTO_OP_FLAG_INIT_IV
#define vec_free(V)
Free vector's memory (no header).
static_always_inline u32 openssl_ops_dec_gcm(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
static_always_inline u32 openssl_ops_hmac(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_MD *md)
#define foreach_openssl_evp_op
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static_always_inline u32 openssl_ops_enc_gcm(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
vnet_crypto_op_status_t status
static vlib_thread_main_t * vlib_get_thread_main()
#define vec_foreach(var, vec)
Vector iterator.
#define CLIB_CACHE_LINE_BYTES
u32 vnet_crypto_register_engine(vlib_main_t *vm, char *name, int prio, char *desc)
clib_error_t * vnet_crypto_init(vlib_main_t *vm)