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_aes_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) \ 48 _(cbc, AES_128_CTR, EVP_aes_128_ctr) \ 49 _(cbc, AES_192_CTR, EVP_aes_192_ctr) \ 50 _(cbc, AES_256_CTR, EVP_aes_256_ctr) \ 52 #define foreach_openssl_chacha20_evp_op \ 53 _(chacha20_poly1305, CHACHA20_POLY1305, EVP_chacha20_poly1305) \ 55 #if OPENSSL_VERSION_NUMBER >= 0x10100000L 56 #define foreach_openssl_evp_op foreach_openssl_aes_evp_op \ 57 foreach_openssl_chacha20_evp_op 59 #define foreach_openssl_evp_op foreach_openssl_aes_evp_op 62 #ifndef EVP_CTRL_AEAD_GET_TAG 63 #define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG 66 #ifndef EVP_CTRL_AEAD_SET_TAG 67 #define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG 70 #define foreach_openssl_hmac_op \ 73 _(SHA224, EVP_sha224) \ 74 _(SHA256, EVP_sha256) \ 75 _(SHA384, EVP_sha384) \ 81 const EVP_CIPHER * cipher)
87 u32 i, j, curr_len = 0;
90 for (i = 0; i < n_ops; i++)
97 if (op->
op == VNET_CRYPTO_OP_3DES_CBC_ENC
98 || op->
op == VNET_CRYPTO_OP_DES_CBC_ENC)
104 RAND_bytes (op->
iv, iv_len);
106 EVP_EncryptInit_ex (ctx, cipher, NULL, key->
data, op->
iv);
109 EVP_CIPHER_CTX_set_padding (ctx, 0);
117 EVP_EncryptUpdate (ctx, out_buf + offset, &out_len, chp->
src,
123 if (out_len < curr_len)
124 EVP_EncryptFinal_ex (ctx, out_buf + offset, &out_len);
137 EVP_EncryptUpdate (ctx, op->
dst, &out_len, op->
src, op->
len);
138 if (out_len < op->
len)
139 EVP_EncryptFinal_ex (ctx, op->
dst + out_len, &out_len);
141 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
149 const EVP_CIPHER * cipher)
155 u32 i, j, curr_len = 0;
158 for (i = 0; i < n_ops; i++)
164 EVP_DecryptInit_ex (ctx, cipher, NULL, key->
data, op->
iv);
167 EVP_CIPHER_CTX_set_padding (ctx, 0);
175 EVP_DecryptUpdate (ctx, out_buf + offset, &out_len, chp->
src,
181 if (out_len < curr_len)
182 EVP_DecryptFinal_ex (ctx, out_buf + offset, &out_len);
195 EVP_DecryptUpdate (ctx, op->
dst, &out_len, op->
src, op->
len);
196 if (out_len < op->
len)
197 EVP_DecryptFinal_ex (ctx, op->
dst + out_len, &out_len);
199 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
207 const EVP_CIPHER * cipher,
int is_gcm)
214 for (i = 0; i < n_ops; i++)
221 RAND_bytes (op->
iv, 8);
223 EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
225 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
226 EVP_EncryptInit_ex (ctx, 0, 0, key->
data, op->
iv);
228 EVP_EncryptUpdate (ctx, NULL, &len, op->
aad, op->
aad_len);
234 EVP_EncryptUpdate (ctx, chp->
dst, &len, chp->
src, chp->
len);
239 EVP_EncryptUpdate (ctx, op->
dst, &len, op->
src, op->
len);
240 EVP_EncryptFinal_ex (ctx, op->
dst + len, &len);
241 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_GET_TAG, op->
tag_len, op->
tag);
242 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
250 const EVP_CIPHER * cipher)
259 const EVP_CIPHER * cipher)
268 const EVP_CIPHER * cipher,
int is_gcm)
274 u32 i, j, n_fail = 0;
275 for (i = 0; i < n_ops; i++)
281 EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
283 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
284 EVP_DecryptInit_ex (ctx, 0, 0, key->
data, op->
iv);
286 EVP_DecryptUpdate (ctx, 0, &len, op->
aad, op->
aad_len);
292 EVP_DecryptUpdate (ctx, chp->
dst, &len, chp->
src, chp->
len);
297 EVP_DecryptUpdate (ctx, op->
dst, &len, op->
src, op->
len);
298 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_SET_TAG, op->
tag_len, op->
tag);
300 if (EVP_DecryptFinal_ex (ctx, op->
dst + len, &len) > 0)
301 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
305 op->
status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
308 return n_ops - n_fail;
314 const EVP_CIPHER * cipher)
323 const EVP_CIPHER * cipher)
339 u32 i, j, n_fail = 0;
340 for (i = 0; i < n_ops; i++)
344 unsigned int out_len = 0;
353 HMAC_Update (ctx, chp->
src, chp->
len);
358 HMAC_Update (ctx, op->
src, op->
len);
359 HMAC_Final (ctx, buffer, &out_len);
363 if ((memcmp (op->
digest, buffer, sz)))
366 op->
status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
372 op->
status = VNET_CRYPTO_OP_STATUS_COMPLETED;
374 return n_ops - n_fail;
379 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \ 380 { return openssl_ops_enc_##m (vm, ops, 0, n_ops, b ()); } \ 383 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \ 384 { return openssl_ops_dec_##m (vm, ops, 0, n_ops, b ()); } \ 387 openssl_ops_enc_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \ 388 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \ 389 { return openssl_ops_enc_##m (vm, ops, chunks, n_ops, b ()); } \ 392 openssl_ops_dec_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \ 393 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \ 394 { return openssl_ops_dec_##m (vm, ops, chunks, n_ops, b ()); } 401 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \ 402 { return openssl_ops_hmac (vm, ops, 0, n_ops, b ()); } \ 404 openssl_ops_hmac_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \ 405 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \ 406 { return openssl_ops_hmac (vm, ops, chunks, n_ops, b ()); } \ 424 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \ 425 openssl_ops_enc_##a, \ 426 openssl_ops_enc_chained_##a); \ 427 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \ 428 openssl_ops_dec_##a, \ 429 openssl_ops_dec_chained_##a); \ 435 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \ 436 openssl_ops_hmac_##a, \ 437 openssl_ops_hmac_chained_##a); \ 448 #if OPENSSL_VERSION_NUMBER >= 0x10100000L 451 HMAC_CTX_init (&(ptd->_hmac_ctx));
458 vec_add (seed_data, &t,
sizeof (t));
459 vec_add (seed_data, &pid,
sizeof (pid));
460 vec_add (seed_data, seed_data,
sizeof (seed_data));
462 RAND_seed ((
const void *) seed_data,
vec_len (seed_data));
472 .runs_after =
VLIB_INITS (
"vnet_crypto_init"),
479 .version = VPP_BUILD_VER,
480 .description =
"OpenSSL Crypto Engine",
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
static_always_inline u32 openssl_ops_hmac(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_MD *md)
#define clib_memcpy_fast(a, b, c)
static_always_inline u32 openssl_ops_enc_cbc(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher)
#define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS
static_always_inline u32 openssl_ops_enc_aead(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher, int is_gcm)
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
static_always_inline u32 openssl_ops_dec_cbc(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher)
#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)
static_always_inline u32 openssl_ops_dec_gcm(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static_always_inline u32 openssl_ops_dec_aead(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher, int is_gcm)
clib_error_t * crypto_openssl_init(vlib_main_t *vm)
#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK
EVP_CIPHER_CTX * evp_cipher_ctx
static_always_inline u32 openssl_ops_enc_chacha20_poly1305(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher)
#define foreach_openssl_hmac_op
#define VNET_CRYPTO_OP_FLAG_INIT_IV
sll srl srl sll sra u16x4 i
#define vec_free(V)
Free vector's memory (no header).
static_always_inline u32 openssl_ops_dec_chacha20_poly1305(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher)
template key/value backing page structure
static_always_inline vnet_crypto_key_t * vnet_crypto_get_key(vnet_crypto_key_index_t index)
#define foreach_openssl_evp_op
#define VLIB_BUFFER_DEFAULT_DATA_SIZE
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
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)
static_always_inline u32 openssl_ops_enc_gcm(vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_CIPHER *cipher)