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;
34 vnet_crypto_op_id_t id;
38 struct vpp_aead_context_t
40 ptls_aead_context_t super;
41 EVP_CIPHER_CTX *evp_ctx;
42 uint8_t static_iv[PTLS_MAX_IV_SIZE];
45 vnet_crypto_op_id_t id;
46 vnet_crypto_op_chunk_t chunks[2];
47 vnet_crypto_alg_t alg;
49 uint8_t iv[PTLS_MAX_IV_SIZE];
53 ptls_vpp_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
55 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
57 vnet_crypto_op_init (&ctx->op, ctx->id);
58 ctx->op.iv = (u8 *) iv;
59 ctx->op.key_index = ctx->key_index;
63 ptls_vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
69 ptls_vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
70 const void *input, size_t _len)
72 vlib_main_t *vm = vlib_get_main ();
73 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
75 ctx->op.src = (u8 *) input;
79 vnet_crypto_process_ops (vm, &ctx->op, 1);
83 ptls_vpp_crypto_cipher_setup_crypto (ptls_cipher_context_t * _ctx, int is_enc,
85 const EVP_CIPHER * cipher,
86 ptls_vpp_do_transform_fn do_transform)
88 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
90 ctx->super.do_dispose = ptls_vpp_crypto_cipher_dispose;
91 ctx->super.do_init = ptls_vpp_crypto_cipher_do_init;
92 ctx->super.do_transform = do_transform;
94 vlib_main_t *vm = vlib_get_main ();
95 vnet_crypto_alg_t algo;
96 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
98 algo = VNET_CRYPTO_ALG_AES_128_CTR;
99 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_CTR_ENC :
100 VNET_CRYPTO_OP_AES_128_CTR_DEC;
102 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
104 algo = VNET_CRYPTO_ALG_AES_256_CTR;
105 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_CTR_ENC :
106 VNET_CRYPTO_OP_AES_256_CTR_DEC;
110 TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
115 clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
116 ctx->key_index = vnet_crypto_key_add (vm, algo,
117 (u8 *) key, _ctx->algo->key_size);
118 clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
124 ptls_vpp_crypto_aead_decrypt (ptls_aead_context_t *_ctx, void *_output,
125 const void *input, size_t inlen, uint64_t seq,
126 const void *aad, size_t aadlen)
128 vlib_main_t *vm = vlib_get_main ();
129 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
130 int tag_size = ctx->super.algo->tag_size;
132 vnet_crypto_op_init (&ctx->op, ctx->id);
133 ctx->op.aad = (u8 *) aad;
134 ctx->op.aad_len = aadlen;
135 ctx->op.iv = ctx->iv;
136 ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv, seq);
137 ctx->op.src = (u8 *) input;
138 ctx->op.dst = _output;
139 ctx->op.key_index = ctx->key_index;
140 ctx->op.len = inlen - tag_size;
141 ctx->op.tag_len = tag_size;
142 ctx->op.tag = ctx->op.src + ctx->op.len;
144 vnet_crypto_process_ops (vm, &(ctx->op), 1);
145 assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
151 ptls_vpp_crypto_aead_encrypt_init (ptls_aead_context_t *_ctx, uint64_t seq,
152 const void *aad, size_t aadlen)
154 struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
156 vnet_crypto_op_init (&ctx->op, ctx->id);
157 ctx->op.aad = (void *) aad;
158 ctx->op.aad_len = aadlen;
159 ctx->op.iv = ctx->iv;
160 ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv, seq);
161 ctx->op.key_index = ctx->key_index;
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)
204 ptls_vpp_crypto_aead_setup_crypto (ptls_aead_context_t *_ctx, int is_enc,
205 const void *key, const void *iv,
206 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 if (alg == VNET_CRYPTO_ALG_AES_128_GCM)
214 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_GCM_ENC :
215 VNET_CRYPTO_OP_AES_128_GCM_DEC;
217 else if (alg == VNET_CRYPTO_ALG_AES_256_GCM)
219 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_GCM_ENC :
220 VNET_CRYPTO_OP_AES_256_GCM_DEC;
224 TLS_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
230 ctx->chunk_index = 0;
231 clib_memcpy (ctx->static_iv, iv, ctx->super.algo->iv_size);
233 clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
234 ctx->key_index = vnet_crypto_key_add (vm, alg, (void *) key, key_len);
235 clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
239 ctx->super.do_encrypt_init = ptls_vpp_crypto_aead_encrypt_init;
240 ctx->super.do_encrypt_update = ptls_vpp_crypto_aead_encrypt_update;
241 ctx->super.do_encrypt_final = ptls_vpp_crypto_aead_encrypt_final;
245 ctx->super.do_decrypt = ptls_vpp_crypto_aead_decrypt;
247 ctx->super.dispose_crypto = ptls_vpp_crypto_aead_dispose_crypto;
253 ptls_vpp_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx,
254 int is_enc, const void *key)
256 return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
257 ptls_vpp_crypto_cipher_encrypt);
261 ptls_vpp_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx,
262 int is_enc, const void *key)
264 return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
265 ptls_vpp_crypto_cipher_encrypt);
269 ptls_vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t *ctx,
270 int is_enc, const void *key,
273 return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
274 VNET_CRYPTO_ALG_AES_128_GCM);
278 ptls_vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t *ctx,
279 int is_enc, const void *key,
282 return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
283 VNET_CRYPTO_ALG_AES_256_GCM);
286 ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr = {
288 PTLS_AES128_KEY_SIZE,
291 sizeof (struct vpp_aead_context_t),
292 ptls_vpp_crypto_aes128ctr_setup_crypto
295 ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr = {
297 PTLS_AES256_KEY_SIZE,
300 sizeof (struct vpp_aead_context_t),
301 ptls_vpp_crypto_aes256ctr_setup_crypto
304 ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = {
306 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
307 PTLS_AESGCM_INTEGRITY_LIMIT,
308 &ptls_vpp_crypto_aes128ctr,
310 PTLS_AES128_KEY_SIZE,
312 PTLS_AESGCM_TAG_SIZE,
313 sizeof (struct vpp_aead_context_t),
314 ptls_vpp_crypto_aead_aes128gcm_setup_crypto
317 ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = {
319 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
320 PTLS_AESGCM_INTEGRITY_LIMIT,
321 &ptls_vpp_crypto_aes256ctr,
323 PTLS_AES256_KEY_SIZE,
325 PTLS_AESGCM_TAG_SIZE,
326 sizeof (struct vpp_aead_context_t),
327 ptls_vpp_crypto_aead_aes256gcm_setup_crypto
330 ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
331 { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
332 &ptls_vpp_crypto_aes128gcm,
336 ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
337 { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
338 &ptls_vpp_crypto_aes256gcm,
342 ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
343 { &ptls_vpp_crypto_aes256gcmsha384,
344 &ptls_vpp_crypto_aes128gcmsha256,
349 * fd.io coding-style-patch-verification: ON
352 * eval: (c-set-style "gnu")