21 #include <picotls/openssl.h>
24 #define QUICLY_EPOCH_1RTT 3
54 uint8_t
iv[PTLS_MAX_IV_SIZE];
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;
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;
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;
103 if (header_protect_ctx != NULL)
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));
140 vnet_key->
alg =
key->algo;
152 void *_output,
const void *input,
size_t inlen,
153 uint64_t decrypted_pn,
const void *aad,
161 ctx->op.aad = (
u8 *) aad;
162 ctx->op.aad_len = aadlen;
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;
169 ctx->op.len = inlen -
ctx->super.algo->tag_size;
170 ctx->op.tag_len =
ctx->super.algo->tag_size;
181 ptls_cipher_context_t *header_protection = NULL;
182 ptls_aead_context_t *aead = NULL;
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)
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;
206 if (encrypted_len < header_protection->algo->iv_size + QUICLY_MAX_PN_SIZE)
208 ptls_cipher_init (header_protection,
211 ptls_cipher_encrypt (header_protection, hpmask, hpmask,
sizeof (hpmask));
212 pctx->
packet.octets.base[0] ^=
213 hpmask[0] & (QUICLY_PACKET_IS_LONG_HEADER (pctx->
packet.octets.base[0]) ?
215 pnlen = (pctx->
packet.octets.base[0] & 0x3) + 1;
216 for (
i = 0;
i != pnlen; ++
i)
221 (pnbits << 8) | pctx->
packet.octets.base[pctx->
packet.encrypted_off +
225 size_t aead_off = pctx->
packet.encrypted_off + pnlen;
228 quicly_determine_packet_number (pnbits, pnlen * 8,
229 next_expected_packet_number);
232 (pctx->
packet.octets.base[0] & QUICLY_KEY_PHASE_BIT) != 0;
236 pctx->
packet.octets.base[0] ^=
238 (QUICLY_PACKET_IS_LONG_HEADER (pctx->
packet.octets.base[0]) ? 0xf :
240 for (
i = 0;
i != pnlen; ++
i)
249 qctx, aead, pctx->
packet.octets.base + aead_off,
250 pctx->
packet.octets.base + aead_off,
251 pctx->
packet.octets.len - aead_off, pn, pctx->
packet.octets.base,
252 aead_off)) == SIZE_MAX)
255 "%s: aead decryption failure (pn: %d)\n", __FUNCTION__, pn);
259 pctx->
packet.encrypted_off = aead_off;
260 pctx->
packet.octets.len = ptlen + aead_off;
262 pctx->
packet.decrypted.pn = pn;
269 ptls_cipher_context_t *header_protect_ctx,
270 ptls_aead_context_t *packet_protect_ctx,
271 ptls_iovec_t datagram,
size_t first_byte_at,
272 size_t payload_from, uint64_t packet_number,
282 void *input = datagram.base + payload_from;
283 void *output = input;
285 datagram.len - payload_from - packet_protect_ctx->algo->tag_size;
286 const void *aad = datagram.base + first_byte_at;
287 size_t aadlen = payload_from - first_byte_at;
293 aead_ctx->
op.
iv = aead_ctx->
iv;
294 ptls_aead__build_iv (aead_ctx->
super.algo, aead_ctx->
op.
iv,
297 aead_ctx->
op.
src = (
u8 *) input;
298 aead_ctx->
op.
dst = output;
299 aead_ctx->
op.
len = inlen;
306 ptls_aead_supplementary_encryption_t supp = {
307 .ctx = header_protect_ctx,
309 datagram.base + payload_from - QUICLY_SEND_PN_SIZE + QUICLY_MAX_PN_SIZE
314 memset (supp.output, 0, sizeof (supp.output));
315 hp_ctx->
op.
iv = (
u8 *) supp.input;
318 hp_ctx->
op.
src = (
u8 *) supp.output;
319 hp_ctx->
op.
dst = (
u8 *) supp.output;
320 hp_ctx->
op.
len =
sizeof (supp.output);
324 datagram.base[first_byte_at] ^=
326 (QUICLY_PACKET_IS_LONG_HEADER (datagram.base[first_byte_at]) ? 0xf : 0x1f);
327 for (
size_t i = 0;
i != QUICLY_SEND_PN_SIZE; ++
i)
328 datagram.base[payload_from +
i - QUICLY_SEND_PN_SIZE] ^=
334 const void *
key,
const EVP_CIPHER *cipher)
339 if (!strcmp (
ctx->super.algo->name,
"AES128-CTR"))
341 algo = VNET_CRYPTO_ALG_AES_128_CTR;
342 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_CTR_ENC :
343 VNET_CRYPTO_OP_AES_128_CTR_DEC;
344 ptls_openssl_aes128ctr.setup_crypto (_ctx, is_enc,
key);
346 else if (!strcmp (
ctx->super.algo->name,
"AES256-CTR"))
348 algo = VNET_CRYPTO_ALG_AES_256_CTR;
349 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_CTR_ENC :
350 VNET_CRYPTO_OP_AES_256_CTR_DEC;
351 ptls_openssl_aes256ctr.setup_crypto (_ctx, is_enc,
key);
355 QUIC_DBG (1,
"%s, Invalid crypto cipher : ", __FUNCTION__,
364 ctx->key.algo = algo;
365 ctx->key.key_len = _ctx->algo->key_size;
389 const void *
key,
const void *
iv,
390 const EVP_CIPHER *cipher)
395 if (!strcmp (
ctx->super.algo->name,
"AES128-GCM"))
397 algo = VNET_CRYPTO_ALG_AES_128_GCM;
398 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_GCM_ENC :
399 VNET_CRYPTO_OP_AES_128_GCM_DEC;
400 ptls_openssl_aes128gcm.setup_crypto (_ctx, is_enc,
key,
iv);
402 else if (!strcmp (
ctx->super.algo->name,
"AES256-GCM"))
404 algo = VNET_CRYPTO_ALG_AES_256_GCM;
405 ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_GCM_ENC :
406 VNET_CRYPTO_OP_AES_256_GCM_DEC;
407 ptls_openssl_aes256gcm.setup_crypto (_ctx, is_enc,
key,
iv);
411 QUIC_DBG (1,
"%s, invalied aead cipher %s", __FUNCTION__,
421 ctx->key.algo = algo;
422 ctx->key.key_len = _ctx->algo->key_size;
432 const void *
key,
const void *
iv)
440 const void *
key,
const void *
iv)
448 int is_encrypt, ptls_buffer_t *
dst, ptls_iovec_t
src)
459 return PTLS_ERROR_NO_MEMORY;
461 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
463 self->data.len =
src.len;
466 if ((ret = ptls_buffer_reserve (
dst,
sizeof (self->id))) != 0)
469 dst->off +=
sizeof (
self->id);
474 if (
src.len != sizeof (self->id))
475 return PTLS_ERROR_SESSION_NOT_FOUND;
477 return PTLS_ERROR_SESSION_NOT_FOUND;
480 if ((ret = ptls_buffer_reserve (
dst, self->data.len)) != 0)
483 dst->off +=
self->data.len;
491 PTLS_AES128_KEY_SIZE,
500 PTLS_AES256_KEY_SIZE,
509 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
510 PTLS_AESGCM_INTEGRITY_LIMIT,
512 &ptls_openssl_aes128ecb,
513 PTLS_AES128_KEY_SIZE,
515 PTLS_AESGCM_TAG_SIZE,
522 PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
523 PTLS_AESGCM_INTEGRITY_LIMIT,
525 &ptls_openssl_aes256ecb,
526 PTLS_AES256_KEY_SIZE,
528 PTLS_AESGCM_TAG_SIZE,
534 PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
539 PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,