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_init (ptls_aead_context_t * _ctx, const void *iv,
141 const void *aad, size_t aadlen)
143 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
145 vnet_crypto_op_id_t id;
146 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
148 id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
150 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
152 id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
159 vnet_crypto_op_init (&ctx->op, id);
160 ctx->op.aad = (u8 *) aad;
161 ctx->op.aad_len = aadlen;
162 ctx->op.iv = (u8 *) iv;
163 ctx->op.key_index = ctx->key_index;
167 quic_crypto_aead_encrypt_update (ptls_aead_context_t * _ctx, void *output,
168 const void *input, size_t inlen)
170 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
172 ctx->op.src = (u8 *) input;
173 ctx->op.dst = output;
175 ctx->op.tag_len = ctx->super.algo->tag_size;
176 ctx->op.tag = ctx->op.src + inlen;
182 quic_crypto_aead_encrypt_final (ptls_aead_context_t * _ctx, void *output)
184 vlib_main_t *vm = vlib_get_main ();
185 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
187 vnet_crypto_process_ops (vm, &ctx->op, 1);
189 return ctx->op.len + ctx->op.tag_len;
193 quic_crypto_aead_decrypt (ptls_aead_context_t * _ctx, void *_output,
194 const void *input, size_t inlen, const void *iv,
195 const void *aad, size_t aadlen)
197 vlib_main_t *vm = vlib_get_main ();
198 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
200 vnet_crypto_op_id_t id;
201 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
203 id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
205 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
207 id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
214 vnet_crypto_op_init (&ctx->op, id);
215 ctx->op.aad = (u8 *) aad;
216 ctx->op.aad_len = aadlen;
217 ctx->op.iv = (u8 *) iv;
219 ctx->op.src = (u8 *) input;
220 ctx->op.dst = _output;
221 ctx->op.key_index = ctx->key_index;
222 ctx->op.len = inlen - ctx->super.algo->tag_size;
224 ctx->op.tag_len = ctx->super.algo->tag_size;
225 ctx->op.tag = ctx->op.src + ctx->op.len;
227 vnet_crypto_process_ops (vm, &ctx->op, 1);
233 quic_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
239 quic_crypto_aead_setup_crypto (ptls_aead_context_t * _ctx, int is_enc,
240 const void *key, const EVP_CIPHER * cipher)
242 vlib_main_t *vm = vlib_get_main ();
243 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
245 vnet_crypto_alg_t algo;
246 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
248 algo = VNET_CRYPTO_ALG_AES_128_GCM;
250 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
252 algo = VNET_CRYPTO_ALG_AES_256_GCM;
256 QUIC_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
261 ctx->super.do_decrypt = quic_crypto_aead_decrypt;
262 ctx->super.do_encrypt_init = quic_crypto_aead_encrypt_init;
263 ctx->super.do_encrypt_update = quic_crypto_aead_encrypt_update;
264 ctx->super.do_encrypt_final = quic_crypto_aead_encrypt_final;
265 ctx->super.dispose_crypto = quic_crypto_aead_dispose_crypto;
267 ctx->key_index = vnet_crypto_key_add (vm, algo,
268 (u8 *) key, _ctx->algo->key_size);
274 quic_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t * ctx,
275 int is_enc, const void *key)
277 return quic_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_128_gcm ());
281 quic_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t * ctx,
282 int is_enc, const void *key)
284 return quic_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_256_gcm ());
287 ptls_cipher_algorithm_t quic_crypto_aes128ctr = { "AES128-CTR",
288 PTLS_AES128_KEY_SIZE,
290 sizeof (struct cipher_context_t),
291 aes128ctr_setup_crypto
294 ptls_cipher_algorithm_t quic_crypto_aes256ctr = { "AES256-CTR",
295 PTLS_AES256_KEY_SIZE,
298 sizeof (struct cipher_context_t),
299 aes256ctr_setup_crypto
302 ptls_aead_algorithm_t quic_crypto_aes128gcm = { "AES128-GCM",
303 &quic_crypto_aes128ctr,
305 PTLS_AES128_KEY_SIZE,
307 PTLS_AESGCM_TAG_SIZE,
308 sizeof (struct aead_crypto_context_t),
309 quic_crypto_aead_aes128gcm_setup_crypto
312 ptls_aead_algorithm_t quic_crypto_aes256gcm = { "AES256-GCM",
313 &quic_crypto_aes256ctr,
315 PTLS_AES256_KEY_SIZE,
317 PTLS_AESGCM_TAG_SIZE,
318 sizeof (struct aead_crypto_context_t),
319 quic_crypto_aead_aes256gcm_setup_crypto
322 ptls_cipher_suite_t quic_crypto_aes128gcmsha256 =
323 { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
324 &quic_crypto_aes128gcm,
328 ptls_cipher_suite_t quic_crypto_aes256gcmsha384 =
329 { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
330 &quic_crypto_aes256gcm,
334 ptls_cipher_suite_t *quic_crypto_cipher_suites[] =
335 { &quic_crypto_aes256gcmsha384,
336 &quic_crypto_aes128gcmsha256,
341 quic_encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
342 int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
344 quic_session_cache_t *self = (void *) _self;
350 /* replace the cached entry along with a newly generated session id */
351 clib_mem_free (self->data.base);
352 if ((self->data.base = clib_mem_alloc (src.len)) == NULL)
353 return PTLS_ERROR_NO_MEMORY;
355 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
356 clib_memcpy (self->data.base, src.base, src.len);
357 self->data.len = src.len;
359 /* store the session id in buffer */
360 if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
362 clib_memcpy (dst->base + dst->off, self->id, sizeof (self->id));
363 dst->off += sizeof (self->id);
369 /* check if session id is the one stored in cache */
370 if (src.len != sizeof (self->id))
371 return PTLS_ERROR_SESSION_NOT_FOUND;
372 if (clib_memcmp (self->id, src.base, sizeof (self->id)) != 0)
373 return PTLS_ERROR_SESSION_NOT_FOUND;
375 /* return the cached value */
376 if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
378 clib_memcpy (dst->base + dst->off, self->data.base, self->data.len);
379 dst->off += self->data.len;
386 * fd.io coding-style-patch-verification: ON
389 * eval: (c-set-style "gnu")