2 *------------------------------------------------------------------
3 * Copyright (c) 2019 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
18 #include <openssl/evp.h>
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
22 #include <vlib/vlib.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vnet/crypto/crypto.h>
25 #include <vpp/app/version.h>
29 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
30 EVP_CIPHER_CTX *evp_cipher_ctx;
32 #if OPENSSL_VERSION_NUMBER < 0x10100000L
35 } openssl_per_thread_data_t;
37 static openssl_per_thread_data_t *per_thread_data = 0;
39 #define foreach_openssl_evp_op \
40 _(DES_CBC, EVP_des_cbc) \
41 _(3DES_CBC, EVP_des_ede3_cbc) \
42 _(AES_128_CBC, EVP_aes_128_cbc) \
43 _(AES_192_CBC, EVP_aes_192_cbc) \
44 _(AES_256_CBC, EVP_aes_256_cbc)
46 #define foreach_openssl_hmac_op \
49 _(SHA224, EVP_sha224) \
50 _(SHA256, EVP_sha256) \
51 _(SHA384, EVP_sha384) \
54 static_always_inline u32
55 openssl_ops_enc_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
56 const EVP_CIPHER * cipher)
58 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
60 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
62 for (i = 0; i < n_ops; i++)
64 vnet_crypto_op_t *op = ops[i];
67 if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
68 RAND_bytes (op->iv, 16);
70 EVP_EncryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
71 EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
72 EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
73 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
78 static_always_inline u32
79 openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
80 const EVP_CIPHER * cipher)
82 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
84 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
86 for (i = 0; i < n_ops; i++)
88 vnet_crypto_op_t *op = ops[i];
91 EVP_DecryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
92 EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
93 EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
94 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
99 static_always_inline u32
100 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
103 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
105 HMAC_CTX *ctx = ptd->hmac_ctx;
107 for (i = 0; i < n_ops; i++)
109 vnet_crypto_op_t *op = ops[i];
110 unsigned int out_len;
112 HMAC_Init_ex (ctx, op->key, op->key_len, md, NULL);
113 HMAC_Update (ctx, op->src, op->len);
114 HMAC_Final (ctx, op->dst, &out_len);
115 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
122 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
123 { return openssl_ops_enc_cbc (vm, ops, n_ops, b ()); } \
126 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
127 { return openssl_ops_dec_cbc (vm, ops, n_ops, b ()); }
129 foreach_openssl_evp_op;
134 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
135 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
137 foreach_openssl_hmac_op;
142 crypto_openssl_init (vlib_main_t * vm)
144 vlib_thread_main_t *tm = vlib_get_thread_main ();
145 openssl_per_thread_data_t *ptd;
150 u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
153 if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
157 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
158 openssl_ops_enc_##a); \
159 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
160 openssl_ops_dec_##a);
162 foreach_openssl_evp_op;
166 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
167 openssl_ops_hmac_##a); \
169 foreach_openssl_hmac_op;
172 vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
173 CLIB_CACHE_LINE_BYTES);
175 vec_foreach (ptd, per_thread_data)
177 ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
178 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
179 ptd->hmac_ctx = HMAC_CTX_new ();
181 HMAC_CTX_init (&(ptd->_hmac_ctx));
182 ptd->hmac_ctx = &ptd->_hmac_ctx;
188 vec_add (seed_data, &t, sizeof (t));
189 vec_add (seed_data, &pid, sizeof (pid));
190 vec_add (seed_data, seed_data, sizeof (seed_data));
192 RAND_seed ((const void *) seed_data, vec_len (seed_data));
194 vec_free (seed_data);
199 VLIB_INIT_FUNCTION (crypto_openssl_init);
202 VLIB_PLUGIN_REGISTER () = {
203 .version = VPP_BUILD_VER,
204 .description = "OpenSSL Crypto Engine Plugin",
209 * fd.io coding-style-patch-verification: ON
212 * eval: (c-set-style "gnu")