2 * Copyright (c) 2020 Intel and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/crypto/crypto.h>
17 #include <vnet/tls/tls.h>
18 #include <picotls/openssl.h>
21 #include <tlspicotls/pico_vpp_crypto.h>
22 #include <tlspicotls/tls_picotls.h>
24 typedef void (*ptls_vpp_do_transform_fn) (ptls_cipher_context_t *, void *,
25 const void *, size_t);
27 vnet_crypto_main_t *cm = &crypto_main;
28 extern picotls_main_t picotls_main;
30 struct cipher_context_t
32 ptls_cipher_context_t super;
37 struct vpp_aead_context_t
39 ptls_aead_context_t super;
41 vnet_crypto_op_chunk_t chunks[2];
42 vnet_crypto_alg_t alg;
48 ptls_vpp_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
50 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
52 vnet_crypto_op_id_t id;
53 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
55 id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
57 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
59 id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
63 TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
68 vnet_crypto_op_init (&ctx->op, id);
69 ctx->op.iv = (u8 *) iv;
70 ctx->op.key_index = ctx->key_index;
74 ptls_vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
80 ptls_vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
81 const void *input, size_t _len)
83 vlib_main_t *vm = vlib_get_main ();
84 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
86 ctx->op.src = (u8 *) input;
90 vnet_crypto_process_ops (vm, &ctx->op, 1);
94 ptls_vpp_crypto_cipher_setup_crypto (ptls_cipher_context_t * _ctx, int is_enc,
96 const EVP_CIPHER * cipher,
97 ptls_vpp_do_transform_fn do_transform)
99 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
101 ctx->super.do_dispose = ptls_vpp_crypto_cipher_dispose;
102 ctx->super.do_init = ptls_vpp_crypto_cipher_do_init;
103 ctx->super.do_transform = do_transform;
105 vlib_main_t *vm = vlib_get_main ();
106 vnet_crypto_alg_t algo;
107 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
109 algo = VNET_CRYPTO_ALG_AES_128_CTR;
111 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
113 algo = VNET_CRYPTO_ALG_AES_256_CTR;
117 TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
122 clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
123 ctx->key_index = vnet_crypto_key_add (vm, algo,
124 (u8 *) key, _ctx->algo->key_size);
125 clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
131 ptls_vpp_crypto_aead_decrypt (ptls_aead_context_t * _ctx, void *_output,
132 const void *input, size_t inlen, const void *iv,
133 const void *aad, size_t aadlen)
135 vlib_main_t *vm = vlib_get_main ();
136 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
137 int tag_size = ctx->super.algo->tag_size;
139 ctx->op.dst = _output;
140 ctx->op.src = (void *) input;
141 ctx->op.len = inlen - tag_size;;
142 ctx->op.iv = (void *) iv;
143 ctx->op.aad = (void *) aad;
144 ctx->op.aad_len = aadlen;
145 ctx->op.tag = (void *) input + inlen - tag_size;
146 ctx->op.tag_len = tag_size;
148 vnet_crypto_process_ops (vm, &(ctx->op), 1);
149 assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
151 return inlen - tag_size;
155 ptls_vpp_crypto_aead_encrypt_init (ptls_aead_context_t * _ctx, const void *iv,
156 const void *aad, size_t aadlen)
158 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
159 ctx->op.iv = (void *) iv;
160 ctx->op.aad = (void *) aad;
161 ctx->op.aad_len = aadlen;
162 ctx->op.n_chunks = 2;
163 ctx->op.chunk_index = 0;
165 ctx->op.flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
169 ptls_vpp_crypto_aead_encrypt_update (ptls_aead_context_t * _ctx, void *output,
170 const void *input, size_t inlen)
172 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
173 ctx->chunks[ctx->chunk_index].dst = output;
174 ctx->chunks[ctx->chunk_index].src = (void *) input;
175 ctx->chunks[ctx->chunk_index].len = inlen;
177 ctx->chunk_index = ctx->chunk_index == 0 ? 1 : 0;
183 ptls_vpp_crypto_aead_encrypt_final (ptls_aead_context_t * _ctx, void *_output)
185 struct vlib_main_t *vm = vlib_get_main ();
186 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
188 ctx->op.tag = _output;
189 ctx->op.tag_len = ctx->super.algo->tag_size;
191 vnet_crypto_process_chained_ops (vm, &(ctx->op), ctx->chunks, 1);
192 assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
194 return ctx->super.algo->tag_size;
198 ptls_vpp_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
205 ptls_vpp_crypto_aead_setup_crypto (ptls_aead_context_t * _ctx, int is_enc,
206 const void *key, vnet_crypto_alg_t alg)
208 struct vlib_main_t *vm = vlib_get_main ();
209 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
210 u16 key_len = ctx->super.algo->key_size;
212 memset (&(ctx->op), 0, sizeof (vnet_crypto_op_t));
214 if (alg == VNET_CRYPTO_ALG_AES_128_GCM)
217 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_128_GCM_ENC);
219 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_128_GCM_DEC);
221 else if (alg == VNET_CRYPTO_ALG_AES_256_GCM)
225 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_256_GCM_ENC);
228 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_256_GCM_DEC);
232 TLS_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
239 clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
241 vnet_crypto_key_add (vm, ctx->alg, (void *) key, key_len);
242 clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
243 ctx->chunk_index = 0;
245 ctx->super.do_decrypt = ptls_vpp_crypto_aead_decrypt;
246 ctx->super.do_encrypt_init = ptls_vpp_crypto_aead_encrypt_init;
247 ctx->super.do_encrypt_update = ptls_vpp_crypto_aead_encrypt_update;
248 ctx->super.do_encrypt_final = ptls_vpp_crypto_aead_encrypt_final;
249 ctx->super.dispose_crypto = ptls_vpp_crypto_aead_dispose_crypto;
255 ptls_vpp_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx,
256 int is_enc, const void *key)
258 return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
259 ptls_vpp_crypto_cipher_encrypt);
263 ptls_vpp_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx,
264 int is_enc, const void *key)
266 return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
267 ptls_vpp_crypto_cipher_encrypt);
271 ptls_vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t * ctx,
272 int is_enc, const void *key)
274 return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key,
275 VNET_CRYPTO_ALG_AES_128_GCM);
279 ptls_vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t * ctx,
280 int is_enc, const void *key)
282 return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key,
283 VNET_CRYPTO_ALG_AES_256_GCM);
286 ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr = { "AES128-CTR",
287 PTLS_AES128_KEY_SIZE,
289 sizeof (struct vpp_aead_context_t),
290 ptls_vpp_crypto_aes128ctr_setup_crypto
293 ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr = { "AES256-CTR",
294 PTLS_AES256_KEY_SIZE,
297 sizeof (struct vpp_aead_context_t),
298 ptls_vpp_crypto_aes256ctr_setup_crypto
301 ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = { "AES128-GCM",
302 &ptls_vpp_crypto_aes128ctr,
304 PTLS_AES128_KEY_SIZE,
306 PTLS_AESGCM_TAG_SIZE,
307 sizeof (struct vpp_aead_context_t),
308 ptls_vpp_crypto_aead_aes128gcm_setup_crypto
311 ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = { "AES256-GCM",
312 &ptls_vpp_crypto_aes256ctr,
314 PTLS_AES256_KEY_SIZE,
316 PTLS_AESGCM_TAG_SIZE,
317 sizeof (struct vpp_aead_context_t),
318 ptls_vpp_crypto_aead_aes256gcm_setup_crypto
321 ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
322 { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
323 &ptls_vpp_crypto_aes128gcm,
327 ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
328 { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
329 &ptls_vpp_crypto_aes256gcm,
333 ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
334 { &ptls_vpp_crypto_aes256gcmsha384,
335 &ptls_vpp_crypto_aes128gcmsha256,
340 * fd.io coding-style-patch-verification: ON
343 * eval: (c-set-style "gnu")