2 * Copyright (c) 2019 Cisco 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 <quic/quic_crypto.h>
17 #include <quic/quic.h>
19 #include <vnet/crypto/crypto.h>
21 #include <picotls/openssl.h>
24 typedef void (*quicly_do_transform_fn) (ptls_cipher_context_t *, void *,
25 const void *, size_t);
27 struct cipher_context_t
29 ptls_cipher_context_t super;
34 struct aead_crypto_context_t
36 ptls_aead_context_t super;
41 vnet_crypto_main_t *cm = &crypto_main;
44 vpp_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
46 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
48 vnet_crypto_op_id_t id;
49 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
51 id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
53 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
55 id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
59 QUIC_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
64 vnet_crypto_op_init (&ctx->op, id);
65 ctx->op.iv = (u8 *) iv;
66 ctx->op.key_index = ctx->key_index;
70 vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
76 vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
77 const void *input, size_t _len)
79 vlib_main_t *vm = vlib_get_main ();
80 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
82 ctx->op.src = (u8 *) input;
86 vnet_crypto_process_ops (vm, &ctx->op, 1);
90 vpp_crypto_cipher_setup_crypto (ptls_cipher_context_t * _ctx, int is_enc,
91 const void *key, const EVP_CIPHER * cipher,
92 quicly_do_transform_fn do_transform)
94 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
96 ctx->super.do_dispose = vpp_crypto_cipher_dispose;
97 ctx->super.do_init = vpp_crypto_cipher_do_init;
98 ctx->super.do_transform = do_transform;
100 vlib_main_t *vm = vlib_get_main ();
101 vnet_crypto_alg_t algo;
102 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
104 algo = VNET_CRYPTO_ALG_AES_128_CTR;
106 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
108 algo = VNET_CRYPTO_ALG_AES_256_CTR;
112 QUIC_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
117 ctx->key_index = vnet_crypto_key_add (vm, algo,
118 (u8 *) key, _ctx->algo->key_size);
124 aes128ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
127 return vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
128 vpp_crypto_cipher_encrypt);
132 aes256ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
135 return vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
136 vpp_crypto_cipher_encrypt);
140 vpp_crypto_aead_encrypt (ptls_aead_context_t * _ctx, void *output,
141 const void *input, size_t inlen, uint64_t seq,
142 const void *iv, const void *aad, size_t aadlen)
144 QUIC_DBG (1, "[quic] %s", __FUNCTION__);
146 vlib_main_t *vm = vlib_get_main ();
147 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
149 vnet_crypto_op_id_t id;
150 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
152 id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
154 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
156 id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
163 vnet_crypto_op_init (&ctx->op, id);
164 ctx->op.aad = (u8 *) aad;
165 ctx->op.aad_len = aadlen;
166 ctx->op.iv = (u8 *) iv;
168 ctx->op.src = (u8 *) input;
169 ctx->op.dst = output;
170 ctx->op.key_index = ctx->key_index;
173 ctx->op.tag_len = ctx->super.algo->tag_size;
174 ctx->op.tag = ctx->op.src + inlen;
176 vnet_crypto_process_ops (vm, &ctx->op, 1);
178 return ctx->op.len + ctx->op.tag_len;
182 vpp_crypto_aead_decrypt (ptls_aead_context_t * _ctx, void *_output,
183 const void *input, size_t inlen, const void *iv,
184 const void *aad, size_t aadlen)
186 QUIC_DBG (1, "[quic] %s", __FUNCTION__);
188 vlib_main_t *vm = vlib_get_main ();
189 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
191 vnet_crypto_op_id_t id;
192 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
194 id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
196 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
198 id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
205 vnet_crypto_op_init (&ctx->op, id);
206 ctx->op.aad = (u8 *) aad;
207 ctx->op.aad_len = aadlen;
208 ctx->op.iv = (u8 *) iv;
210 ctx->op.src = (u8 *) input;
211 ctx->op.dst = _output;
212 ctx->op.key_index = ctx->key_index;
213 ctx->op.len = inlen - ctx->super.algo->tag_size;
215 ctx->op.tag_len = ctx->super.algo->tag_size;
216 ctx->op.tag = ctx->op.src + ctx->op.len;
218 vnet_crypto_process_ops (vm, &ctx->op, 1);
224 vpp_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
226 QUIC_DBG (1, "[quic] %s", __FUNCTION__);
230 vpp_crypto_aead_setup_crypto (ptls_aead_context_t * _ctx, int is_enc,
231 const void *key, const EVP_CIPHER * cipher)
233 QUIC_DBG (1, "%s, algo : ", __FUNCTION__, _ctx->algo->name);
235 vlib_main_t *vm = vlib_get_main ();
236 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
238 vnet_crypto_alg_t algo;
239 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
241 algo = VNET_CRYPTO_ALG_AES_128_GCM;
243 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
245 algo = VNET_CRYPTO_ALG_AES_256_GCM;
249 QUIC_DBG (1, "%s, algo : ", __FUNCTION__, _ctx->algo->name);
253 ctx->super.do_decrypt = vpp_crypto_aead_decrypt;
254 ctx->super.do_encrypt = vpp_crypto_aead_encrypt;
255 ctx->super.dispose_crypto = vpp_crypto_aead_dispose_crypto;
257 ctx->key_index = vnet_crypto_key_add (vm, algo,
258 (u8 *) key, _ctx->algo->key_size);
264 vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t * ctx, int is_enc,
267 return vpp_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_128_gcm ());
271 vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t * ctx, int is_enc,
274 return vpp_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_256_gcm ());
277 ptls_cipher_algorithm_t vpp_crypto_aes128ctr = { "AES128-CTR",
278 PTLS_AES128_KEY_SIZE,
280 sizeof (struct cipher_context_t),
281 aes128ctr_setup_crypto
284 ptls_cipher_algorithm_t vpp_crypto_aes256ctr = { "AES256-CTR",
285 PTLS_AES256_KEY_SIZE,
288 sizeof (struct cipher_context_t),
289 aes256ctr_setup_crypto
292 ptls_aead_algorithm_t vpp_crypto_aes128gcm = { "AES128-GCM",
293 &vpp_crypto_aes128ctr,
295 PTLS_AES128_KEY_SIZE,
297 PTLS_AESGCM_TAG_SIZE,
298 sizeof (struct aead_crypto_context_t),
299 vpp_crypto_aead_aes128gcm_setup_crypto
302 ptls_aead_algorithm_t vpp_crypto_aes256gcm = { "AES256-GCM",
303 &vpp_crypto_aes256ctr,
305 PTLS_AES256_KEY_SIZE,
307 PTLS_AESGCM_TAG_SIZE,
308 sizeof (struct aead_crypto_context_t),
309 vpp_crypto_aead_aes256gcm_setup_crypto
312 ptls_cipher_suite_t vpp_crypto_aes128gcmsha256 =
313 { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
314 &vpp_crypto_aes128gcm,
318 ptls_cipher_suite_t vpp_crypto_aes256gcmsha384 =
319 { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
320 &vpp_crypto_aes256gcm,
324 ptls_cipher_suite_t *vpp_crypto_cipher_suites[] =
325 { &vpp_crypto_aes256gcmsha384,
326 &vpp_crypto_aes128gcmsha256,
331 * fd.io coding-style-patch-verification: ON
334 * eval: (c-set-style "gnu")