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)
84 if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
85 RAND_bytes (op->iv, iv_len);
87 EVP_EncryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
89 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
90 EVP_CIPHER_CTX_set_padding (ctx, 0);
92 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
94 chp = chunks + op->chunk_index;
96 for (j = 0; j < op->n_chunks; j++)
98 EVP_EncryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
104 if (out_len < curr_len)
105 EVP_EncryptFinal_ex (ctx, out_buf + offset, &out_len);
108 chp = chunks + op->chunk_index;
109 for (j = 0; j < op->n_chunks; j++)
111 clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
118 EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
119 if (out_len < op->len)
120 EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
122 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
127 static_always_inline u32
128 openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
129 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
130 const EVP_CIPHER * cipher)
132 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
134 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
135 vnet_crypto_op_chunk_t *chp;
136 u32 i, j, curr_len = 0;
137 u8 out_buf[VLIB_BUFFER_DEFAULT_DATA_SIZE * 5];
139 for (i = 0; i < n_ops; i++)
141 vnet_crypto_op_t *op = ops[i];
142 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
145 EVP_DecryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
147 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
148 EVP_CIPHER_CTX_set_padding (ctx, 0);
150 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
152 chp = chunks + op->chunk_index;
154 for (j = 0; j < op->n_chunks; j++)
156 EVP_DecryptUpdate (ctx, out_buf + offset, &out_len, chp->src,
162 if (out_len < curr_len)
163 EVP_DecryptFinal_ex (ctx, out_buf + offset, &out_len);
166 chp = chunks + op->chunk_index;
167 for (j = 0; j < op->n_chunks; j++)
169 clib_memcpy_fast (chp->dst, out_buf + offset, chp->len);
176 EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
177 if (out_len < op->len)
178 EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
180 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
185 static_always_inline u32
186 openssl_ops_enc_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[],
187 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
188 const EVP_CIPHER * cipher)
190 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
192 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
193 vnet_crypto_op_chunk_t *chp;
195 for (i = 0; i < n_ops; i++)
197 vnet_crypto_op_t *op = ops[i];
198 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
201 if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
202 RAND_bytes (op->iv, 8);
204 EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
205 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
206 EVP_EncryptInit_ex (ctx, 0, 0, key->data, op->iv);
208 EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
209 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
211 chp = chunks + op->chunk_index;
212 for (j = 0; j < op->n_chunks; j++)
214 EVP_EncryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
219 EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
220 EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
221 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_GET_TAG, op->tag_len, op->tag);
222 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
227 static_always_inline u32
228 openssl_ops_dec_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[],
229 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
230 const EVP_CIPHER * cipher)
232 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
234 EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
235 vnet_crypto_op_chunk_t *chp;
236 u32 i, j, n_fail = 0;
237 for (i = 0; i < n_ops; i++)
239 vnet_crypto_op_t *op = ops[i];
240 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
243 EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
244 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
245 EVP_DecryptInit_ex (ctx, 0, 0, key->data, op->iv);
247 EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
248 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
250 chp = chunks + op->chunk_index;
251 for (j = 0; j < op->n_chunks; j++)
253 EVP_DecryptUpdate (ctx, chp->dst, &len, chp->src, chp->len);
258 EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
259 EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_TAG, op->tag_len, op->tag);
261 if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
262 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
266 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
269 return n_ops - n_fail;
272 static_always_inline u32
273 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[],
274 vnet_crypto_op_chunk_t * chunks, u32 n_ops,
278 openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
280 HMAC_CTX *ctx = ptd->hmac_ctx;
281 vnet_crypto_op_chunk_t *chp;
282 u32 i, j, n_fail = 0;
283 for (i = 0; i < n_ops; i++)
285 vnet_crypto_op_t *op = ops[i];
286 vnet_crypto_key_t *key = vnet_crypto_get_key (op->key_index);
287 unsigned int out_len;
288 size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
290 HMAC_Init_ex (ctx, key->data, vec_len (key->data), md, NULL);
291 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
293 chp = chunks + op->chunk_index;
294 for (j = 0; j < op->n_chunks; j++)
296 HMAC_Update (ctx, chp->src, chp->len);
301 HMAC_Update (ctx, op->src, op->len);
302 HMAC_Final (ctx, buffer, &out_len);
304 if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
306 if ((memcmp (op->digest, buffer, sz)))
309 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
314 clib_memcpy_fast (op->digest, buffer, sz);
315 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
317 return n_ops - n_fail;
322 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
323 { return openssl_ops_enc_##m (vm, ops, 0, n_ops, b ()); } \
326 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
327 { return openssl_ops_dec_##m (vm, ops, 0, n_ops, b ()); } \
330 openssl_ops_enc_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
331 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
332 { return openssl_ops_enc_##m (vm, ops, chunks, n_ops, b ()); } \
335 openssl_ops_dec_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
336 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
337 { return openssl_ops_dec_##m (vm, ops, chunks, n_ops, b ()); }
339 foreach_openssl_evp_op;
344 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
345 { return openssl_ops_hmac (vm, ops, 0, n_ops, b ()); } \
347 openssl_ops_hmac_chained_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
348 vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
349 { return openssl_ops_hmac (vm, ops, chunks, n_ops, b ()); } \
351 foreach_openssl_hmac_op;
356 crypto_openssl_init (vlib_main_t * vm)
358 vlib_thread_main_t *tm = vlib_get_thread_main ();
359 openssl_per_thread_data_t *ptd;
364 u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
367 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
368 openssl_ops_enc_##a, \
369 openssl_ops_enc_chained_##a); \
370 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
371 openssl_ops_dec_##a, \
372 openssl_ops_dec_chained_##a); \
374 foreach_openssl_evp_op;
378 vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
379 openssl_ops_hmac_##a, \
380 openssl_ops_hmac_chained_##a); \
382 foreach_openssl_hmac_op;
385 vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
386 CLIB_CACHE_LINE_BYTES);
388 vec_foreach (ptd, per_thread_data)
390 ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
391 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
392 ptd->hmac_ctx = HMAC_CTX_new ();
394 HMAC_CTX_init (&(ptd->_hmac_ctx));
395 ptd->hmac_ctx = &ptd->_hmac_ctx;
401 vec_add (seed_data, &t, sizeof (t));
402 vec_add (seed_data, &pid, sizeof (pid));
403 vec_add (seed_data, seed_data, sizeof (seed_data));
405 RAND_seed ((const void *) seed_data, vec_len (seed_data));
407 vec_free (seed_data);
413 VLIB_INIT_FUNCTION (crypto_openssl_init) =
415 .runs_after = VLIB_INITS ("vnet_crypto_init"),
421 VLIB_PLUGIN_REGISTER () = {
422 .version = VPP_BUILD_VER,
423 .description = "OpenSSL Crypto Engine",
428 * fd.io coding-style-patch-verification: ON
431 * eval: (c-set-style "gnu")