2 * Copyright (c) 2021 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.h>
17 #include <quic/quic_crypto.h>
18 #include <vnet/session/session.h>
21 #include <picotls/openssl.h>
24 #define QUICLY_EPOCH_1RTT 3
26 extern quic_main_t quic_main;
27 extern quic_ctx_t *quic_get_conn_ctx (quicly_conn_t *conn);
28 vnet_crypto_main_t *cm = &crypto_main;
30 typedef struct crypto_key_
32 vnet_crypto_alg_t algo;
37 struct cipher_context_t
39 ptls_cipher_context_t super;
41 vnet_crypto_op_id_t id;
45 struct aead_crypto_context_t
47 ptls_aead_context_t super;
48 EVP_CIPHER_CTX *evp_ctx;
49 uint8_t static_iv[PTLS_MAX_IV_SIZE];
53 vnet_crypto_op_id_t id;
54 uint8_t iv[PTLS_MAX_IV_SIZE];
58 quic_crypto_setup_cipher (quicly_crypto_engine_t *engine, quicly_conn_t *conn,
59 size_t epoch, int is_enc,
60 ptls_cipher_context_t **header_protect_ctx,
61 ptls_aead_context_t **packet_protect_ctx,
62 ptls_aead_algorithm_t *aead,
63 ptls_hash_algorithm_t *hash, const void *secret)
65 uint8_t hpkey[PTLS_MAX_SECRET_SIZE];
68 *packet_protect_ctx = NULL;
69 /* generate new header protection key */
70 if (header_protect_ctx != NULL)
72 *header_protect_ctx = NULL;
74 ptls_hkdf_expand_label (hash, hpkey, aead->ctr_cipher->key_size,
75 ptls_iovec_init (secret, hash->digest_size),
76 "quic hp", ptls_iovec_init (NULL, 0), NULL);
79 *header_protect_ctx = ptls_cipher_new (aead->ctr_cipher, is_enc, hpkey);
80 if (NULL == *header_protect_ctx)
82 ret = PTLS_ERROR_NO_MEMORY;
87 /* generate new AEAD context */
89 ptls_aead_new (aead, hash, is_enc, secret, QUICLY_AEAD_BASE_LABEL);
90 if (NULL == *packet_protect_ctx)
92 ret = PTLS_ERROR_NO_MEMORY;
96 if (epoch == QUICLY_EPOCH_1RTT && !is_enc)
98 quic_ctx_t *qctx = quic_get_conn_ctx (conn);
99 if (qctx->ingress_keys.aead_ctx != NULL)
100 qctx->key_phase_ingress++;
102 qctx->ingress_keys.aead_ctx = *packet_protect_ctx;
103 if (header_protect_ctx != NULL)
104 qctx->ingress_keys.hp_ctx = *header_protect_ctx;
112 if (*packet_protect_ctx != NULL)
114 ptls_aead_free (*packet_protect_ctx);
115 *packet_protect_ctx = NULL;
117 if (header_protect_ctx && *header_protect_ctx != NULL)
119 ptls_cipher_free (*header_protect_ctx);
120 *header_protect_ctx = NULL;
123 ptls_clear_memory (hpkey, sizeof (hpkey));
128 quic_crypto_set_key (crypto_key_t *key)
130 u8 thread_index = vlib_get_thread_index ();
131 u32 key_id = quic_main.per_thread_crypto_key_indices[thread_index];
132 vnet_crypto_key_t *vnet_key = vnet_crypto_get_key (key_id);
133 vlib_main_t *vm = vlib_get_main ();
134 vnet_crypto_engine_t *engine;
136 vec_foreach (engine, cm->engines)
137 if (engine->key_op_handler)
138 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, key_id);
140 vnet_key->alg = key->algo;
141 clib_memcpy (vnet_key->data, key->key, key->key_len);
143 vec_foreach (engine, cm->engines)
144 if (engine->key_op_handler)
145 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, key_id);
151 quic_crypto_aead_decrypt (quic_ctx_t *qctx, ptls_aead_context_t *_ctx,
152 void *_output, const void *input, size_t inlen,
153 uint64_t decrypted_pn, const void *aad,
156 vlib_main_t *vm = vlib_get_main ();
158 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
160 vnet_crypto_op_init (&ctx->op, ctx->id);
161 ctx->op.aad = (u8 *) aad;
162 ctx->op.aad_len = aadlen;
163 ctx->op.iv = ctx->iv;
164 ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv,
166 ctx->op.src = (u8 *) input;
167 ctx->op.dst = _output;
168 ctx->op.key_index = quic_crypto_set_key (&ctx->key);
169 ctx->op.len = inlen - ctx->super.algo->tag_size;
170 ctx->op.tag_len = ctx->super.algo->tag_size;
171 ctx->op.tag = ctx->op.src + ctx->op.len;
173 vnet_crypto_process_ops (vm, &(ctx->op), 1);
179 quic_crypto_decrypt_packet (quic_ctx_t *qctx, quic_rx_packet_ctx_t *pctx)
181 ptls_cipher_context_t *header_protection = NULL;
182 ptls_aead_context_t *aead = NULL;
185 /* Long Header packets are not decrypted by vpp */
186 if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
189 uint64_t next_expected_packet_number =
190 quicly_get_next_expected_packet_number (qctx->conn);
191 if (next_expected_packet_number == UINT64_MAX)
194 aead = qctx->ingress_keys.aead_ctx;
195 header_protection = qctx->ingress_keys.hp_ctx;
197 if (!aead || !header_protection)
200 size_t encrypted_len = pctx->packet.octets.len - pctx->packet.encrypted_off;
201 uint8_t hpmask[5] = { 0 };
203 size_t pnlen, ptlen, i;
205 /* decipher the header protection, as well as obtaining pnbits, pnlen */
206 if (encrypted_len < header_protection->algo->iv_size + QUICLY_MAX_PN_SIZE)
208 ptls_cipher_init (header_protection, pctx->packet.octets.base +
209 pctx->packet.encrypted_off +
211 ptls_cipher_encrypt (header_protection, hpmask, hpmask, sizeof (hpmask));
212 pctx->packet.octets.base[0] ^=
214 (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]) ? 0xf : 0x1f);
215 pnlen = (pctx->packet.octets.base[0] & 0x3) + 1;
216 for (i = 0; i != pnlen; ++i)
218 pctx->packet.octets.base[pctx->packet.encrypted_off + i] ^=
220 pnbits = (pnbits << 8) |
221 pctx->packet.octets.base[pctx->packet.encrypted_off + i];
224 size_t aead_off = pctx->packet.encrypted_off + pnlen;
226 pn = quicly_determine_packet_number (pnbits, pnlen * 8,
227 next_expected_packet_number);
230 (pctx->packet.octets.base[0] & QUICLY_KEY_PHASE_BIT) != 0;
232 if (key_phase_bit != (qctx->key_phase_ingress & 1))
234 pctx->packet.octets.base[0] ^=
236 (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]) ? 0xf :
238 for (i = 0; i != pnlen; ++i)
240 pctx->packet.octets.base[pctx->packet.encrypted_off + i] ^=
246 if ((ptlen = quic_crypto_aead_decrypt (
247 qctx, aead, pctx->packet.octets.base + aead_off,
248 pctx->packet.octets.base + aead_off,
249 pctx->packet.octets.len - aead_off, pn, pctx->packet.octets.base,
250 aead_off)) == SIZE_MAX)
252 fprintf (stderr, "%s: aead decryption failure (pn: %d)\n", __FUNCTION__,
257 pctx->packet.encrypted_off = aead_off;
258 pctx->packet.octets.len = ptlen + aead_off;
260 pctx->packet.decrypted.pn = pn;
261 pctx->packet.decrypted.key_phase = qctx->key_phase_ingress;
265 quic_crypto_encrypt_packet (struct st_quicly_crypto_engine_t *engine,
267 ptls_cipher_context_t *header_protect_ctx,
268 ptls_aead_context_t *packet_protect_ctx,
269 ptls_iovec_t datagram, size_t first_byte_at,
270 size_t payload_from, uint64_t packet_number,
273 vlib_main_t *vm = vlib_get_main ();
275 struct cipher_context_t *hp_ctx =
276 (struct cipher_context_t *) header_protect_ctx;
277 struct aead_crypto_context_t *aead_ctx =
278 (struct aead_crypto_context_t *) packet_protect_ctx;
280 void *input = datagram.base + payload_from;
281 void *output = input;
283 datagram.len - payload_from - packet_protect_ctx->algo->tag_size;
284 const void *aad = datagram.base + first_byte_at;
285 size_t aadlen = payload_from - first_byte_at;
287 /* Build AEAD encrypt crypto operation */
288 vnet_crypto_op_init (&aead_ctx->op, aead_ctx->id);
289 aead_ctx->op.aad = (u8 *) aad;
290 aead_ctx->op.aad_len = aadlen;
291 aead_ctx->op.iv = aead_ctx->iv;
292 ptls_aead__build_iv (aead_ctx->super.algo, aead_ctx->op.iv,
293 aead_ctx->static_iv, packet_number);
294 aead_ctx->op.key_index = quic_crypto_set_key (&aead_ctx->key);
295 aead_ctx->op.src = (u8 *) input;
296 aead_ctx->op.dst = output;
297 aead_ctx->op.len = inlen;
298 aead_ctx->op.tag_len = aead_ctx->super.algo->tag_size;
299 aead_ctx->op.tag = aead_ctx->op.src + inlen;
300 vnet_crypto_process_ops (vm, &(aead_ctx->op), 1);
301 assert (aead_ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
303 /* Build Header protection crypto operation */
304 ptls_aead_supplementary_encryption_t supp = {
305 .ctx = header_protect_ctx,
307 datagram.base + payload_from - QUICLY_SEND_PN_SIZE + QUICLY_MAX_PN_SIZE
310 /* Build Header protection crypto operation */
311 vnet_crypto_op_init (&hp_ctx->op, hp_ctx->id);
312 memset (supp.output, 0, sizeof (supp.output));
313 hp_ctx->op.iv = (u8 *) supp.input;
314 hp_ctx->op.key_index = quic_crypto_set_key (&hp_ctx->key);
316 hp_ctx->op.src = (u8 *) supp.output;
317 hp_ctx->op.dst = (u8 *) supp.output;
318 hp_ctx->op.len = sizeof (supp.output);
319 vnet_crypto_process_ops (vm, &(hp_ctx->op), 1);
320 assert (hp_ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
322 datagram.base[first_byte_at] ^=
324 (QUICLY_PACKET_IS_LONG_HEADER (datagram.base[first_byte_at]) ? 0xf : 0x1f);
325 for (size_t i = 0; i != QUICLY_SEND_PN_SIZE; ++i)
326 datagram.base[payload_from + i - QUICLY_SEND_PN_SIZE] ^=
331 quic_crypto_cipher_setup_crypto (ptls_cipher_context_t *_ctx, int is_enc,
332 const void *key, const EVP_CIPHER *cipher)
334 struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
336 vnet_crypto_alg_t algo;
337 if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
339 algo = VNET_CRYPTO_ALG_AES_128_CTR;
340 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_CTR_ENC :
341 VNET_CRYPTO_OP_AES_128_CTR_DEC;
342 ptls_openssl_aes128ctr.setup_crypto (_ctx, is_enc, key);
344 else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
346 algo = VNET_CRYPTO_ALG_AES_256_CTR;
347 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_CTR_ENC :
348 VNET_CRYPTO_OP_AES_256_CTR_DEC;
349 ptls_openssl_aes256ctr.setup_crypto (_ctx, is_enc, key);
353 QUIC_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
358 if (quic_main.vnet_crypto_enabled)
361 // quic_crypto_go_setup_key (algo, key, _ctx->algo->key_size);
362 ctx->key.algo = algo;
363 ctx->key.key_len = _ctx->algo->key_size;
364 assert (ctx->key.key_len <= 32);
365 clib_memcpy (&ctx->key.key, key, ctx->key.key_len);
372 quic_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t *ctx, int is_enc,
375 return quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr ());
379 quic_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t *ctx, int is_enc,
382 return quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr ());
386 quic_crypto_aead_setup_crypto (ptls_aead_context_t *_ctx, int is_enc,
387 const void *key, const void *iv,
388 const EVP_CIPHER *cipher)
390 struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
392 vnet_crypto_alg_t algo;
393 if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
395 algo = VNET_CRYPTO_ALG_AES_128_GCM;
396 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_GCM_ENC :
397 VNET_CRYPTO_OP_AES_128_GCM_DEC;
398 ptls_openssl_aes128gcm.setup_crypto (_ctx, is_enc, key, iv);
400 else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
402 algo = VNET_CRYPTO_ALG_AES_256_GCM;
403 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_GCM_ENC :
404 VNET_CRYPTO_OP_AES_256_GCM_DEC;
405 ptls_openssl_aes256gcm.setup_crypto (_ctx, is_enc, key, iv);
409 QUIC_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
414 if (quic_main.vnet_crypto_enabled)
416 clib_memcpy (ctx->static_iv, iv, ctx->super.algo->iv_size);
418 // quic_crypto_go_setup_key (algo, key, _ctx->algo->key_size);
419 ctx->key.algo = algo;
420 ctx->key.key_len = _ctx->algo->key_size;
421 assert (ctx->key.key_len <= 32);
422 clib_memcpy (&ctx->key.key, key, ctx->key.key_len);
429 quic_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t *ctx, int is_enc,
430 const void *key, const void *iv)
432 return quic_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
437 quic_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t *ctx, int is_enc,
438 const void *key, const void *iv)
440 return quic_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
445 quic_encrypt_ticket_cb (ptls_encrypt_ticket_t *_self, ptls_t *tls,
446 int is_encrypt, ptls_buffer_t *dst, ptls_iovec_t src)
448 quic_session_cache_t *self = (void *) _self;
454 /* replace the cached entry along with a newly generated session id */
455 clib_mem_free (self->data.base);
456 if ((self->data.base = clib_mem_alloc (src.len)) == NULL)
457 return PTLS_ERROR_NO_MEMORY;
459 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
460 clib_memcpy (self->data.base, src.base, src.len);
461 self->data.len = src.len;
463 /* store the session id in buffer */
464 if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
466 clib_memcpy (dst->base + dst->off, self->id, sizeof (self->id));
467 dst->off += sizeof (self->id);
471 /* check if session id is the one stored in cache */
472 if (src.len != sizeof (self->id))
473 return PTLS_ERROR_SESSION_NOT_FOUND;
474 if (clib_memcmp (self->id, src.base, sizeof (self->id)) != 0)
475 return PTLS_ERROR_SESSION_NOT_FOUND;
477 /* return the cached value */
478 if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
480 clib_memcpy (dst->base + dst->off, self->data.base, self->data.len);
481 dst->off += self->data.len;
487 ptls_cipher_algorithm_t quic_crypto_aes128ctr = {
489 PTLS_AES128_KEY_SIZE,
492 sizeof (struct cipher_context_t),
493 quic_crypto_aes128ctr_setup_crypto
496 ptls_cipher_algorithm_t quic_crypto_aes256ctr = {
498 PTLS_AES256_KEY_SIZE,
501 sizeof (struct cipher_context_t),
502 quic_crypto_aes256ctr_setup_crypto
505 #define PTLS_X86_CACHE_LINE_ALIGN_BITS 6
506 ptls_aead_algorithm_t quic_crypto_aes128gcm = {
508 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
509 PTLS_AESGCM_INTEGRITY_LIMIT,
510 &quic_crypto_aes128ctr,
511 &ptls_openssl_aes128ecb,
512 PTLS_AES128_KEY_SIZE,
514 PTLS_AESGCM_TAG_SIZE,
515 { PTLS_TLS12_AESGCM_FIXED_IV_SIZE, PTLS_TLS12_AESGCM_RECORD_IV_SIZE },
517 PTLS_X86_CACHE_LINE_ALIGN_BITS,
518 sizeof (struct aead_crypto_context_t),
519 quic_crypto_aead_aes128gcm_setup_crypto
522 ptls_aead_algorithm_t quic_crypto_aes256gcm = {
524 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
525 PTLS_AESGCM_INTEGRITY_LIMIT,
526 &quic_crypto_aes256ctr,
527 &ptls_openssl_aes256ecb,
528 PTLS_AES256_KEY_SIZE,
530 PTLS_AESGCM_TAG_SIZE,
531 { PTLS_TLS12_AESGCM_FIXED_IV_SIZE, PTLS_TLS12_AESGCM_RECORD_IV_SIZE },
533 PTLS_X86_CACHE_LINE_ALIGN_BITS,
534 sizeof (struct aead_crypto_context_t),
535 quic_crypto_aead_aes256gcm_setup_crypto
538 ptls_cipher_suite_t quic_crypto_aes128gcmsha256 = {
539 PTLS_CIPHER_SUITE_AES_128_GCM_SHA256, &quic_crypto_aes128gcm,
543 ptls_cipher_suite_t quic_crypto_aes256gcmsha384 = {
544 PTLS_CIPHER_SUITE_AES_256_GCM_SHA384, &quic_crypto_aes256gcm,
548 ptls_cipher_suite_t *quic_crypto_cipher_suites[] = {
549 &quic_crypto_aes256gcmsha384, &quic_crypto_aes128gcmsha256, NULL
552 quicly_crypto_engine_t quic_crypto_engine = { quic_crypto_setup_cipher,
553 quic_crypto_encrypt_packet };
556 * fd.io coding-style-patch-verification: ON
559 * eval: (c-set-style "gnu")