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 _(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_hmac_op \
55 _(SHA224, EVP_sha224) \
56 _(SHA256, EVP_sha256) \
57 _(SHA384, EVP_sha384) \
60 static_always_inline u32
61 openssl_ops_enc_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
62 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
63 const EVP_CIPHER * cipher)
65 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
67 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
68 vnet_crypto_op_chunk_t *chp;
69 u32 i, j, curr_len = 0;
70 u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
72 for (i = 0; i < n_ops; i++)
74 vnet_crypto_op_t *op = ops[i];
75 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
79 if (op->op == VNET_CRYPTO_OP_3DES_CBC_ENC
80 || op->op == VNET_CRYPTO_OP_DES_CBC_ENC)
85 if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
86 RAND_bytes (op->iv, iv_len);
88 EVP_EncryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
90 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
91 EVP_CIPHER_CTX_set_padding (ctx, 0);
93 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
95 chp = chunks + op->chunk_index;
97 for (j = 0; j < op->n_chunks; j++)
99 EVP_EncryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
105 if (out_len < curr_len)
106 EVP_EncryptFinal_ex (ctx, out_buf + offset, &out_len);
109 chp = chunks + op->chunk_index;
110 for (j = 0; j < op->n_chunks; j++)
112 clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
119 EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
120 if (out_len < op->len)
121 EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
123 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
128 static_always_inline u32
129 openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
130 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
131 const EVP_CIPHER * cipher)
133 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
135 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
136 vnet_crypto_op_chunk_t *chp;
137 u32 i, j, curr_len = 0;
138 u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
140 for (i = 0; i < n_ops; i++)
142 vnet_crypto_op_t *op = ops[i];
143 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
146 EVP_DecryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
148 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
149 EVP_CIPHER_CTX_set_padding (ctx, 0);
151 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
153 chp = chunks + op->chunk_index;
155 for (j = 0; j < op->n_chunks; j++)
157 EVP_DecryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
163 if (out_len < curr_len)
164 EVP_DecryptFinal_ex (ctx, out_buf + offset, &out_len);
167 chp = chunks + op->chunk_index;
168 for (j = 0; j < op->n_chunks; j++)
170 clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
177 EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
178 if (out_len < op->len)
179 EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
181 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
186 static_always_inline u32
187 openssl_ops_enc_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[],
188 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
189 const EVP_CIPHER * cipher)
191 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
193 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
194 vnet_crypto_op_chunk_t *chp;
196 for (i = 0; i < n_ops; i++)
198 vnet_crypto_op_t *op = ops[i];
199 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
202 if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
203 RAND_bytes (op->iv, 8);
205 EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
206 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
207 EVP_EncryptInit_ex (ctx, 0, 0, key->data, op->iv);
209 EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
210 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
212 chp = chunks + op->chunk_index;
213 for (j = 0; j < op->n_chunks; j++)
215 EVP_EncryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
220 EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
221 EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
222 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_GET_TAG, op->tag_len, op->tag);
223 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
228 static_always_inline u32
229 openssl_ops_dec_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[],
230 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
231 const EVP_CIPHER * cipher)
233 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
235 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
236 vnet_crypto_op_chunk_t *chp;
237 u32 i, j, n_fail = 0;
238 for (i = 0; i < n_ops; i++)
240 vnet_crypto_op_t *op = ops[i];
241 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
244 EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
245 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
246 EVP_DecryptInit_ex (ctx, 0, 0, key->data, op->iv);
248 EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
249 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
251 chp = chunks + op->chunk_index;
252 for (j = 0; j < op->n_chunks; j++)
254 EVP_DecryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
259 EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
260 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_TAG, op->tag_len, op->tag);
262 if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
263 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
267 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
270 return n_ops - n_fail;
273 static_always_inline u32
274 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[],
275 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
279 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
281 HMAC_CTX *ctx = ptd->hmac_ctx;
282 vnet_crypto_op_chunk_t *chp;
283 u32 i, j, n_fail = 0;
284 for (i = 0; i < n_ops; i++)
286 vnet_crypto_op_t *op = ops[i];
287 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
288 unsigned int out_len = 0;
289 size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
291 HMAC_Init_ex (ctx, key->data, vec_len (key->data), md, NULL);
292 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
294 chp = chunks + op->chunk_index;
295 for (j = 0; j < op->n_chunks; j++)
297 HMAC_Update (ctx, chp->src, chp->len);
302 HMAC_Update (ctx, op->src, op->len);
303 HMAC_Final (ctx, buffer, &out_len);
305 if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
307 if ((memcmp (op->digest, buffer, sz)))
310 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
315 clib_memcpy_fast (op->digest, buffer, sz);
316 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
318 return n_ops - n_fail;
323 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
324 { return openssl_ops_enc_##m (vm, ops, 0, n_ops, b ()); } \
327 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
328 { return openssl_ops_dec_##m (vm, ops, 0, n_ops, b ()); } \
331 openssl_ops_enc_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
332 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
333 { return openssl_ops_enc_##m (vm, ops, chunks, n_ops, b ()); } \
336 openssl_ops_dec_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
337 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
338 { return openssl_ops_dec_##m (vm, ops, chunks, n_ops, b ()); }
340 foreach_openssl_evp_op;
345 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
346 { return openssl_ops_hmac (vm, ops, 0, n_ops, b ()); } \
348 openssl_ops_hmac_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
349 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
350 { return openssl_ops_hmac (vm, ops, chunks, n_ops, b ()); } \
352 foreach_openssl_hmac_op;
357 crypto_openssl_init (vlib_main_t * vm)
359 vlib_thread_main_t *tm = vlib_get_thread_main ();
360 openssl_per_thread_data_t *ptd;
365 u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
368 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
369 openssl_ops_enc_##a, \
370 openssl_ops_enc_chained_##a); \
371 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
372 openssl_ops_dec_##a, \
373 openssl_ops_dec_chained_##a); \
375 foreach_openssl_evp_op;
379 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
380 openssl_ops_hmac_##a, \
381 openssl_ops_hmac_chained_##a); \
383 foreach_openssl_hmac_op;
386 vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
387 CLIB_CACHE_LINE_BYTES);
389 vec_foreach (ptd, per_thread_data)
391 ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
392 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
393 ptd->hmac_ctx = HMAC_CTX_new ();
395 HMAC_CTX_init (&(ptd->_hmac_ctx));
396 ptd->hmac_ctx = &ptd->_hmac_ctx;
402 vec_add (seed_data, &t, sizeof (t));
403 vec_add (seed_data, &pid, sizeof (pid));
404 vec_add (seed_data, seed_data, sizeof (seed_data));
406 RAND_seed ((const void *) seed_data, vec_len (seed_data));
408 vec_free (seed_data);
414 VLIB_INIT_FUNCTION (crypto_openssl_init) =
416 .runs_after = VLIB_INITS ("vnet_crypto_init"),
422 VLIB_PLUGIN_REGISTER () = {
423 .version = VPP_BUILD_VER,
424 .description = "OpenSSL Crypto Engine",
429 * fd.io coding-style-patch-verification: ON
432 * eval: (c-set-style "gnu")