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 quic_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 quic_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
76 quic_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 quic_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 = quic_crypto_cipher_dispose;
97 ctx->super.do_init = quic_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 quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
128 quic_crypto_cipher_encrypt);
132 aes256ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
135 return quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
136 quic_crypto_cipher_encrypt);
140 quic_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 vlib_main_t *vm = vlib_get_main ();
145 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
147 vnet_crypto_op_id_t id;
148 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
150 id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
152 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
154 id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
161 vnet_crypto_op_init (&ctx->op, id);
162 ctx->op.aad = (u8 *) aad;
163 ctx->op.aad_len = aadlen;
164 ctx->op.iv = (u8 *) iv;
166 ctx->op.src = (u8 *) input;
167 ctx->op.dst = output;
168 ctx->op.key_index = ctx->key_index;
171 ctx->op.tag_len = ctx->super.algo->tag_size;
172 ctx->op.tag = ctx->op.src + inlen;
174 vnet_crypto_process_ops (vm, &ctx->op, 1);
176 return ctx->op.len + ctx->op.tag_len;
180 quic_crypto_aead_decrypt (ptls_aead_context_t * _ctx, void *_output,
181 const void *input, size_t inlen, const void *iv,
182 const void *aad, size_t aadlen)
184 vlib_main_t *vm = vlib_get_main ();
185 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
187 vnet_crypto_op_id_t id;
188 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
190 id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
192 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
194 id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
201 vnet_crypto_op_init (&ctx->op, id);
202 ctx->op.aad = (u8 *) aad;
203 ctx->op.aad_len = aadlen;
204 ctx->op.iv = (u8 *) iv;
206 ctx->op.src = (u8 *) input;
207 ctx->op.dst = _output;
208 ctx->op.key_index = ctx->key_index;
209 ctx->op.len = inlen - ctx->super.algo->tag_size;
211 ctx->op.tag_len = ctx->super.algo->tag_size;
212 ctx->op.tag = ctx->op.src + ctx->op.len;
214 vnet_crypto_process_ops (vm, &ctx->op, 1);
220 quic_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
226 quic_crypto_aead_setup_crypto (ptls_aead_context_t * _ctx, int is_enc,
227 const void *key, const EVP_CIPHER * cipher)
229 vlib_main_t *vm = vlib_get_main ();
230 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
232 vnet_crypto_alg_t algo;
233 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
235 algo = VNET_CRYPTO_ALG_AES_128_GCM;
237 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
239 algo = VNET_CRYPTO_ALG_AES_256_GCM;
243 QUIC_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
248 ctx->super.do_decrypt = quic_crypto_aead_decrypt;
249 ctx->super.do_encrypt = quic_crypto_aead_encrypt;
250 ctx->super.dispose_crypto = quic_crypto_aead_dispose_crypto;
252 ctx->key_index = vnet_crypto_key_add (vm, algo,
253 (u8 *) key, _ctx->algo->key_size);
259 quic_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t * ctx,
260 int is_enc, const void *key)
262 return quic_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_128_gcm ());
266 quic_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t * ctx,
267 int is_enc, const void *key)
269 return quic_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_256_gcm ());
272 ptls_cipher_algorithm_t quic_crypto_aes128ctr = { "AES128-CTR",
273 PTLS_AES128_KEY_SIZE,
275 sizeof (struct cipher_context_t),
276 aes128ctr_setup_crypto
279 ptls_cipher_algorithm_t quic_crypto_aes256ctr = { "AES256-CTR",
280 PTLS_AES256_KEY_SIZE,
283 sizeof (struct cipher_context_t),
284 aes256ctr_setup_crypto
287 ptls_aead_algorithm_t quic_crypto_aes128gcm = { "AES128-GCM",
288 &quic_crypto_aes128ctr,
290 PTLS_AES128_KEY_SIZE,
292 PTLS_AESGCM_TAG_SIZE,
293 sizeof (struct aead_crypto_context_t),
294 quic_crypto_aead_aes128gcm_setup_crypto
297 ptls_aead_algorithm_t quic_crypto_aes256gcm = { "AES256-GCM",
298 &quic_crypto_aes256ctr,
300 PTLS_AES256_KEY_SIZE,
302 PTLS_AESGCM_TAG_SIZE,
303 sizeof (struct aead_crypto_context_t),
304 quic_crypto_aead_aes256gcm_setup_crypto
307 ptls_cipher_suite_t quic_crypto_aes128gcmsha256 =
308 { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
309 &quic_crypto_aes128gcm,
313 ptls_cipher_suite_t quic_crypto_aes256gcmsha384 =
314 { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
315 &quic_crypto_aes256gcm,
319 ptls_cipher_suite_t *quic_crypto_cipher_suites[] =
320 { &quic_crypto_aes256gcmsha384,
321 &quic_crypto_aes128gcmsha256,
326 * fd.io coding-style-patch-verification: ON
329 * eval: (c-set-style "gnu")