2 * Copyright (c) 2021 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;
45 uint8_t iv[PTLS_MAX_IV_SIZE];
46 uint8_t static_iv[PTLS_MAX_IV_SIZE];
50 ptls_vpp_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
52 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
54 vnet_crypto_op_id_t id;
55 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
57 id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
59 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
61 id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
65 TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
70 vnet_crypto_op_init (&ctx->op, id);
71 ctx->op.iv = (u8 *) iv;
72 ctx->op.key_index = ctx->key_index;
76 ptls_vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
82 ptls_vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
83 const void *input, size_t _len)
85 vlib_main_t *vm = vlib_get_main ();
86 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
88 ctx->op.src = (u8 *) input;
92 vnet_crypto_process_ops (vm, &ctx->op, 1);
96 ptls_vpp_crypto_cipher_setup_crypto (ptls_cipher_context_t * _ctx, int is_enc,
98 const EVP_CIPHER * cipher,
99 ptls_vpp_do_transform_fn do_transform)
101 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
103 ctx->super.do_dispose = ptls_vpp_crypto_cipher_dispose;
104 ctx->super.do_init = ptls_vpp_crypto_cipher_do_init;
105 ctx->super.do_transform = do_transform;
107 vlib_main_t *vm = vlib_get_main ();
108 vnet_crypto_alg_t algo;
109 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
111 algo = VNET_CRYPTO_ALG_AES_128_CTR;
113 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
115 algo = VNET_CRYPTO_ALG_AES_256_CTR;
119 TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
124 clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
125 ctx->key_index = vnet_crypto_key_add (vm, algo,
126 (u8 *) key, _ctx->algo->key_size);
127 clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
133 ptls_vpp_crypto_aead_decrypt (ptls_aead_context_t *_ctx, void *_output,
134 const void *input, size_t inlen, uint64_t seq,
135 const void *aad, size_t aadlen)
137 vlib_main_t *vm = vlib_get_main ();
138 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
139 int tag_size = ctx->super.algo->tag_size;
141 ctx->op.dst = _output;
142 ctx->op.src = (void *) input;
143 ctx->op.len = inlen - tag_size;;
144 ctx->op.iv = ctx->static_iv;
145 ctx->op.aad = (void *) aad;
146 ctx->op.aad_len = aadlen;
147 ctx->op.tag = (void *) input + inlen - tag_size;
148 ctx->op.tag_len = tag_size;
150 vnet_crypto_process_ops (vm, &(ctx->op), 1);
151 assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
153 return inlen - tag_size;
157 ptls_vpp_crypto_aead_encrypt_init (ptls_aead_context_t *_ctx, uint64_t seq,
158 const void *aad, size_t aadlen)
160 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
161 ctx->op.iv = ctx->iv;
162 ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv, seq);
163 ctx->op.iv = ctx->static_iv;
164 ctx->op.aad = (void *) aad;
165 ctx->op.aad_len = aadlen;
166 ctx->op.n_chunks = 2;
167 ctx->op.chunk_index = 0;
169 ctx->op.flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
173 ptls_vpp_crypto_aead_encrypt_update (ptls_aead_context_t * _ctx, void *output,
174 const void *input, size_t inlen)
176 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
177 ctx->chunks[ctx->chunk_index].dst = output;
178 ctx->chunks[ctx->chunk_index].src = (void *) input;
179 ctx->chunks[ctx->chunk_index].len = inlen;
181 ctx->chunk_index = ctx->chunk_index == 0 ? 1 : 0;
187 ptls_vpp_crypto_aead_encrypt_final (ptls_aead_context_t * _ctx, void *_output)
189 struct vlib_main_t *vm = vlib_get_main ();
190 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
192 ctx->op.tag = _output;
193 ctx->op.tag_len = ctx->super.algo->tag_size;
195 vnet_crypto_process_chained_ops (vm, &(ctx->op), ctx->chunks, 1);
196 assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
198 return ctx->super.algo->tag_size;
202 ptls_vpp_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
208 ptls_vpp_crypto_aead_setup_crypto (ptls_aead_context_t *_ctx, int is_enc,
209 const void *key, const void *iv,
210 vnet_crypto_alg_t alg)
212 struct vlib_main_t *vm = vlib_get_main ();
213 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
214 u16 key_len = ctx->super.algo->key_size;
216 memset (&(ctx->op), 0, sizeof (vnet_crypto_op_t));
218 if (alg == VNET_CRYPTO_ALG_AES_128_GCM)
221 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_128_GCM_ENC);
223 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_128_GCM_DEC);
225 else if (alg == VNET_CRYPTO_ALG_AES_256_GCM)
229 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_256_GCM_ENC);
232 vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_256_GCM_DEC);
236 TLS_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
243 clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
245 vnet_crypto_key_add (vm, ctx->alg, (void *) key, key_len);
246 clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
247 ctx->chunk_index = 0;
248 clib_memcpy (ctx->static_iv, iv, ctx->super.algo->iv_size);
250 ctx->super.do_decrypt = ptls_vpp_crypto_aead_decrypt;
251 ctx->super.do_encrypt_init = ptls_vpp_crypto_aead_encrypt_init;
252 ctx->super.do_encrypt_update = ptls_vpp_crypto_aead_encrypt_update;
253 ctx->super.do_encrypt_final = ptls_vpp_crypto_aead_encrypt_final;
254 ctx->super.dispose_crypto = ptls_vpp_crypto_aead_dispose_crypto;
260 ptls_vpp_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx,
261 int is_enc, const void *key)
263 return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
264 ptls_vpp_crypto_cipher_encrypt);
268 ptls_vpp_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx,
269 int is_enc, const void *key)
271 return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
272 ptls_vpp_crypto_cipher_encrypt);
276 ptls_vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t *ctx,
277 int is_enc, const void *key,
280 return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
281 VNET_CRYPTO_ALG_AES_128_GCM);
285 ptls_vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t *ctx,
286 int is_enc, const void *key,
289 return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
290 VNET_CRYPTO_ALG_AES_256_GCM);
293 ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr = {
295 PTLS_AES128_KEY_SIZE,
298 sizeof (struct vpp_aead_context_t),
299 ptls_vpp_crypto_aes128ctr_setup_crypto
302 ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr = {
304 PTLS_AES256_KEY_SIZE,
307 sizeof (struct vpp_aead_context_t),
308 ptls_vpp_crypto_aes256ctr_setup_crypto
311 ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = {
313 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
314 PTLS_AESGCM_INTEGRITY_LIMIT,
315 &ptls_vpp_crypto_aes128ctr,
317 PTLS_AES128_KEY_SIZE,
319 PTLS_AESGCM_TAG_SIZE,
320 sizeof (struct vpp_aead_context_t),
321 ptls_vpp_crypto_aead_aes128gcm_setup_crypto
324 ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = {
326 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
327 PTLS_AESGCM_INTEGRITY_LIMIT,
328 &ptls_vpp_crypto_aes256ctr,
330 PTLS_AES256_KEY_SIZE,
332 PTLS_AESGCM_TAG_SIZE,
333 sizeof (struct vpp_aead_context_t),
334 ptls_vpp_crypto_aead_aes256gcm_setup_crypto
337 ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
338 { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
339 &ptls_vpp_crypto_aes128gcm,
343 ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
344 { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
345 &ptls_vpp_crypto_aes256gcm,
349 ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
350 { &ptls_vpp_crypto_aes256gcmsha384,
351 &ptls_vpp_crypto_aes128gcmsha256,
356 * fd.io coding-style-patch-verification: ON
359 * eval: (c-set-style "gnu")