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_aes_evp_op \
40 _ (cbc, DES_CBC, EVP_des_cbc, 8) \
41 _ (cbc, 3DES_CBC, EVP_des_ede3_cbc, 8) \
42 _ (cbc, AES_128_CBC, EVP_aes_128_cbc, 16) \
43 _ (cbc, AES_192_CBC, EVP_aes_192_cbc, 16) \
44 _ (cbc, AES_256_CBC, EVP_aes_256_cbc, 16) \
45 _ (gcm, AES_128_GCM, EVP_aes_128_gcm, 8) \
46 _ (gcm, AES_192_GCM, EVP_aes_192_gcm, 8) \
47 _ (gcm, AES_256_GCM, EVP_aes_256_gcm, 8) \
48 _ (cbc, AES_128_CTR, EVP_aes_128_ctr, 8) \
49 _ (cbc, AES_192_CTR, EVP_aes_192_ctr, 8) \
50 _ (cbc, AES_256_CTR, EVP_aes_256_ctr, 8)
52 #define foreach_openssl_chacha20_evp_op \
53 _ (chacha20_poly1305, CHACHA20_POLY1305, EVP_chacha20_poly1305, 8)
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) \
78 static_always_inline u32
79 openssl_ops_enc_cbc (vlib_main_t *vm, vnet_crypto_op_t *ops[],
80 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
81 const EVP_CIPHER *cipher, const int iv_len)
83 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
85 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
86 vnet_crypto_op_chunk_t *chp;
87 u32 i, j, curr_len = 0;
88 u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
90 for (i = 0; i < n_ops; i++)
92 vnet_crypto_op_t *op = ops[i];
93 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
96 if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
97 RAND_bytes (op->iv, iv_len);
99 EVP_EncryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
101 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
102 EVP_CIPHER_CTX_set_padding (ctx, 0);
104 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
106 chp = chunks + op->chunk_index;
108 for (j = 0; j < op->n_chunks; j++)
110 EVP_EncryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
116 if (out_len < curr_len)
117 EVP_EncryptFinal_ex (ctx, out_buf + offset, &out_len);
120 chp = chunks + op->chunk_index;
121 for (j = 0; j < op->n_chunks; j++)
123 clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
130 EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
131 if (out_len < op->len)
132 EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
134 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
139 static_always_inline u32
140 openssl_ops_dec_cbc (vlib_main_t *vm, vnet_crypto_op_t *ops[],
141 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
142 const EVP_CIPHER *cipher, const int iv_len)
144 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
146 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
147 vnet_crypto_op_chunk_t *chp;
148 u32 i, j, curr_len = 0;
149 u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
151 for (i = 0; i < n_ops; i++)
153 vnet_crypto_op_t *op = ops[i];
154 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
157 EVP_DecryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
159 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
160 EVP_CIPHER_CTX_set_padding (ctx, 0);
162 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
164 chp = chunks + op->chunk_index;
166 for (j = 0; j < op->n_chunks; j++)
168 EVP_DecryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
174 if (out_len < curr_len)
175 EVP_DecryptFinal_ex (ctx, out_buf + offset, &out_len);
178 chp = chunks + op->chunk_index;
179 for (j = 0; j < op->n_chunks; j++)
181 clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
188 EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
189 if (out_len < op->len)
190 EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
192 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
197 static_always_inline u32
198 openssl_ops_enc_aead (vlib_main_t *vm, vnet_crypto_op_t *ops[],
199 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
200 const EVP_CIPHER *cipher, int is_gcm, const int iv_len)
202 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
204 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
205 vnet_crypto_op_chunk_t *chp;
207 for (i = 0; i < n_ops; i++)
209 vnet_crypto_op_t *op = ops[i];
210 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
213 if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
214 RAND_bytes (op->iv, 8);
216 EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
218 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
219 EVP_EncryptInit_ex (ctx, 0, 0, key->data, op->iv);
221 EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
222 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
224 chp = chunks + op->chunk_index;
225 for (j = 0; j < op->n_chunks; j++)
227 EVP_EncryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
232 EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
233 EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
234 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_GET_TAG, op->tag_len, op->tag);
235 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
240 static_always_inline u32
241 openssl_ops_enc_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[],
242 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
243 const EVP_CIPHER *cipher, const int iv_len)
245 return openssl_ops_enc_aead (vm, ops, chunks, n_ops, cipher,
246 /* is_gcm */ 1, iv_len);
249 static_always_inline __clib_unused u32
250 openssl_ops_enc_chacha20_poly1305 (vlib_main_t *vm, vnet_crypto_op_t *ops[],
251 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
252 const EVP_CIPHER *cipher, const int iv_len)
254 return openssl_ops_enc_aead (vm, ops, chunks, n_ops, cipher,
255 /* is_gcm */ 0, iv_len);
258 static_always_inline u32
259 openssl_ops_dec_aead (vlib_main_t *vm, vnet_crypto_op_t *ops[],
260 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
261 const EVP_CIPHER *cipher, int is_gcm, const int iv_len)
263 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
265 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
266 vnet_crypto_op_chunk_t *chp;
267 u32 i, j, n_fail = 0;
268 for (i = 0; i < n_ops; i++)
270 vnet_crypto_op_t *op = ops[i];
271 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
274 EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
276 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
277 EVP_DecryptInit_ex (ctx, 0, 0, key->data, op->iv);
279 EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
280 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
282 chp = chunks + op->chunk_index;
283 for (j = 0; j < op->n_chunks; j++)
285 EVP_DecryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
290 EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
291 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_SET_TAG, op->tag_len, op->tag);
293 if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
294 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
298 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
301 return n_ops - n_fail;
304 static_always_inline u32
305 openssl_ops_dec_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[],
306 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
307 const EVP_CIPHER *cipher, const int iv_len)
309 return openssl_ops_dec_aead (vm, ops, chunks, n_ops, cipher,
310 /* is_gcm */ 1, iv_len);
313 static_always_inline __clib_unused u32
314 openssl_ops_dec_chacha20_poly1305 (vlib_main_t *vm, vnet_crypto_op_t *ops[],
315 vnet_crypto_op_chunk_t *chunks, u32 n_ops,
316 const EVP_CIPHER *cipher, const int iv_len)
318 return openssl_ops_dec_aead (vm, ops, chunks, n_ops, cipher,
319 /* is_gcm */ 0, iv_len);
322 static_always_inline u32
323 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[],
324 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
328 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
330 HMAC_CTX *ctx = ptd->hmac_ctx;
331 vnet_crypto_op_chunk_t *chp;
332 u32 i, j, n_fail = 0;
333 for (i = 0; i < n_ops; i++)
335 vnet_crypto_op_t *op = ops[i];
336 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
337 unsigned int out_len = 0;
338 size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
340 HMAC_Init_ex (ctx, key->data, vec_len (key->data), md, NULL);
341 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
343 chp = chunks + op->chunk_index;
344 for (j = 0; j < op->n_chunks; j++)
346 HMAC_Update (ctx, chp->src, chp->len);
351 HMAC_Update (ctx, op->src, op->len);
352 HMAC_Final (ctx, buffer, &out_len);
354 if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
356 if ((memcmp (op->digest, buffer, sz)))
359 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
364 clib_memcpy_fast (op->digest, buffer, sz);
365 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
367 return n_ops - n_fail;
370 #define _(m, a, b, iv) \
371 static u32 openssl_ops_enc_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[], \
374 return openssl_ops_enc_##m (vm, ops, 0, n_ops, b (), iv); \
377 u32 openssl_ops_dec_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[], \
380 return openssl_ops_dec_##m (vm, ops, 0, n_ops, b (), iv); \
383 static u32 openssl_ops_enc_chained_##a ( \
384 vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
387 return openssl_ops_enc_##m (vm, ops, chunks, n_ops, b (), iv); \
390 static u32 openssl_ops_dec_chained_##a ( \
391 vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
394 return openssl_ops_dec_##m (vm, ops, chunks, n_ops, b (), iv); \
397 foreach_openssl_evp_op;
402 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
403 { return openssl_ops_hmac (vm, ops, 0, n_ops, b ()); } \
405 openssl_ops_hmac_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
406 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
407 { return openssl_ops_hmac (vm, ops, chunks, n_ops, b ()); } \
409 foreach_openssl_hmac_op;
414 crypto_openssl_init (vlib_main_t * vm)
416 vlib_thread_main_t *tm = vlib_get_thread_main ();
417 openssl_per_thread_data_t *ptd;
422 u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
424 #define _(m, a, b, iv) \
425 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
426 openssl_ops_enc_##a, \
427 openssl_ops_enc_chained_##a); \
428 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
429 openssl_ops_dec_##a, \
430 openssl_ops_dec_chained_##a);
432 foreach_openssl_evp_op;
436 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
437 openssl_ops_hmac_##a, \
438 openssl_ops_hmac_chained_##a); \
440 foreach_openssl_hmac_op;
443 vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
444 CLIB_CACHE_LINE_BYTES);
446 vec_foreach (ptd, per_thread_data)
448 ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
449 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
450 ptd->hmac_ctx = HMAC_CTX_new ();
452 HMAC_CTX_init (&(ptd->_hmac_ctx));
453 ptd->hmac_ctx = &ptd->_hmac_ctx;
459 vec_add (seed_data, &t, sizeof (t));
460 vec_add (seed_data, &pid, sizeof (pid));
461 vec_add (seed_data, seed_data, sizeof (seed_data));
463 RAND_seed ((const void *) seed_data, vec_len (seed_data));
465 vec_free (seed_data);
471 VLIB_INIT_FUNCTION (crypto_openssl_init) =
473 .runs_after = VLIB_INITS ("vnet_crypto_init"),
479 VLIB_PLUGIN_REGISTER () = {
480 .version = VPP_BUILD_VER,
481 .description = "OpenSSL Crypto Engine",
486 * fd.io coding-style-patch-verification: ON
489 * eval: (c-set-style "gnu")