+void
+quic_crypto_batch_tx_packets (quic_crypto_batch_ctx_t * batch_ctx)
+{
+ vlib_main_t *vm = vlib_get_main ();
+
+ if (batch_ctx->nb_tx_packets <= 0)
+ return;
+
+ clib_rwlock_reader_lock (&quic_main.crypto_keys_quic_rw_lock);
+ vnet_crypto_process_ops (vm, batch_ctx->aead_crypto_tx_packets_ops,
+ batch_ctx->nb_tx_packets);
+ clib_rwlock_reader_unlock (&quic_main.crypto_keys_quic_rw_lock);
+
+ for (int i = 0; i < batch_ctx->nb_tx_packets; i++)
+ clib_mem_free (batch_ctx->aead_crypto_tx_packets_ops[i].iv);
+
+ batch_ctx->nb_tx_packets = 0;
+}
+
+void
+quic_crypto_batch_rx_packets (quic_crypto_batch_ctx_t * batch_ctx)
+{
+ vlib_main_t *vm = vlib_get_main ();
+
+ if (batch_ctx->nb_rx_packets <= 0)
+ return;
+
+ clib_rwlock_reader_lock (&quic_main.crypto_keys_quic_rw_lock);
+ vnet_crypto_process_ops (vm, batch_ctx->aead_crypto_rx_packets_ops,
+ batch_ctx->nb_rx_packets);
+ clib_rwlock_reader_unlock (&quic_main.crypto_keys_quic_rw_lock);
+
+ for (int i = 0; i < batch_ctx->nb_rx_packets; i++)
+ clib_mem_free (batch_ctx->aead_crypto_rx_packets_ops[i].iv);
+
+ batch_ctx->nb_rx_packets = 0;
+}
+
+void
+build_iv (ptls_aead_context_t * ctx, uint8_t * iv, uint64_t seq)
+{
+ size_t iv_size = ctx->algo->iv_size, i;
+ const uint8_t *s = ctx->static_iv;
+ uint8_t *d = iv;
+ /* build iv */
+ for (i = iv_size - 8; i != 0; --i)
+ *d++ = *s++;
+ i = 64;
+ do
+ {
+ i -= 8;
+ *d++ = *s++ ^ (uint8_t) (seq >> i);
+ }
+ while (i != 0);
+}
+
+static void
+do_finalize_send_packet (ptls_cipher_context_t * hp,
+ quicly_datagram_t * packet,
+ size_t first_byte_at, size_t payload_from)
+{
+ uint8_t hpmask[1 + QUICLY_SEND_PN_SIZE] = {
+ 0
+ };
+ size_t i;
+
+ ptls_cipher_init (hp,
+ packet->data.base + payload_from - QUICLY_SEND_PN_SIZE +
+ QUICLY_MAX_PN_SIZE);
+ ptls_cipher_encrypt (hp, hpmask, hpmask, sizeof (hpmask));
+
+ packet->data.base[first_byte_at] ^=
+ hpmask[0] &
+ (QUICLY_PACKET_IS_LONG_HEADER (packet->data.base[first_byte_at]) ? 0xf :
+ 0x1f);
+
+ for (i = 0; i != QUICLY_SEND_PN_SIZE; ++i)
+ packet->data.base[payload_from + i - QUICLY_SEND_PN_SIZE] ^=
+ hpmask[i + 1];
+}
+
+void
+quic_crypto_finalize_send_packet (quicly_datagram_t * packet)
+{
+ quic_encrypt_cb_ctx *encrypt_cb_ctx =
+ (quic_encrypt_cb_ctx *) ((uint8_t *) packet + sizeof (*packet));
+
+ for (int i = 0; i < encrypt_cb_ctx->snd_ctx_count; i++)
+ {
+ do_finalize_send_packet (encrypt_cb_ctx->snd_ctx[i].hp,
+ packet,
+ encrypt_cb_ctx->snd_ctx[i].first_byte_at,
+ encrypt_cb_ctx->snd_ctx[i].payload_from);
+ }
+ encrypt_cb_ctx->snd_ctx_count = 0;
+}
+
+static int
+quic_crypto_setup_cipher (quicly_crypto_engine_t * engine,
+ quicly_conn_t * conn, size_t epoch, int is_enc,
+ ptls_cipher_context_t ** hp_ctx,
+ ptls_aead_context_t ** aead_ctx,
+ ptls_aead_algorithm_t * aead,
+ ptls_hash_algorithm_t * hash, const void *secret)
+{
+ uint8_t hpkey[PTLS_MAX_SECRET_SIZE];
+ int ret;
+
+ *aead_ctx = NULL;
+
+ /* generate new header protection key */
+ if (hp_ctx != NULL)
+ {
+ *hp_ctx = NULL;
+ if ((ret =
+ ptls_hkdf_expand_label (hash, hpkey, aead->ctr_cipher->key_size,
+ ptls_iovec_init (secret,
+ hash->digest_size),
+ "quic hp", ptls_iovec_init (NULL, 0),
+ NULL)) != 0)
+ goto Exit;
+ if ((*hp_ctx =
+ ptls_cipher_new (aead->ctr_cipher, is_enc, hpkey)) == NULL)
+ {
+ ret = PTLS_ERROR_NO_MEMORY;
+ goto Exit;
+ }
+ }
+
+ /* generate new AEAD context */
+ if ((*aead_ctx =
+ ptls_aead_new (aead, hash, is_enc, secret,
+ QUICLY_AEAD_BASE_LABEL)) == NULL)
+ {
+ ret = PTLS_ERROR_NO_MEMORY;
+ goto Exit;
+ }
+
+ if (epoch == QUICLY_EPOCH_1RTT && !is_enc)
+ {
+ quic_ctx_t *qctx = quic_get_conn_ctx (conn);
+ if (qctx->ingress_keys.aead_ctx != NULL)
+ {
+ qctx->key_phase_ingress++;
+ }
+
+ qctx->ingress_keys.aead_ctx = *aead_ctx;
+ if (hp_ctx != NULL)
+ qctx->ingress_keys.hp_ctx = *hp_ctx;
+ }
+
+ ret = 0;
+
+Exit:
+ if (ret != 0)
+ {
+ if (aead_ctx && *aead_ctx != NULL)
+ {
+ ptls_aead_free (*aead_ctx);
+ *aead_ctx = NULL;
+ }
+ if (hp_ctx && *hp_ctx != NULL)
+ {
+ ptls_cipher_free (*hp_ctx);
+ *hp_ctx = NULL;
+ }
+ }
+ ptls_clear_memory (hpkey, sizeof (hpkey));
+ return ret;
+}
+
+void
+quic_crypto_finalize_send_packet_cb (struct st_quicly_crypto_engine_t
+ *engine, quicly_conn_t * conn,
+ ptls_cipher_context_t * hp,
+ ptls_aead_context_t * aead,
+ quicly_datagram_t * packet,
+ size_t first_byte_at,
+ size_t payload_from, int coalesced)
+{
+ quic_encrypt_cb_ctx *encrypt_cb_ctx =
+ (quic_encrypt_cb_ctx *) ((uint8_t *) packet + sizeof (*packet));
+
+ encrypt_cb_ctx->snd_ctx[encrypt_cb_ctx->snd_ctx_count].hp = hp;
+ encrypt_cb_ctx->snd_ctx[encrypt_cb_ctx->snd_ctx_count].first_byte_at =
+ first_byte_at;
+ encrypt_cb_ctx->snd_ctx[encrypt_cb_ctx->snd_ctx_count].payload_from =
+ payload_from;
+ encrypt_cb_ctx->snd_ctx_count++;
+}
+
+void
+quic_crypto_decrypt_packet (quic_ctx_t * qctx, quic_rx_packet_ctx_t * pctx)
+{
+ ptls_cipher_context_t *header_protection = NULL;
+ ptls_aead_context_t *aead = NULL;
+ int pn;
+
+ /* Long Header packets are not decrypted by vpp */
+ if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
+ return;
+
+ uint64_t next_expected_packet_number =
+ quicly_get_next_expected_packet_number (qctx->conn);
+ if (next_expected_packet_number == UINT64_MAX)
+ return;
+
+ aead = qctx->ingress_keys.aead_ctx;
+ header_protection = qctx->ingress_keys.hp_ctx;
+
+ if (!aead || !header_protection)
+ return;
+
+ size_t encrypted_len = pctx->packet.octets.len - pctx->packet.encrypted_off;
+ uint8_t hpmask[5] = { 0 };
+ uint32_t pnbits = 0;
+ size_t pnlen, ptlen, i;
+
+ /* decipher the header protection, as well as obtaining pnbits, pnlen */
+ if (encrypted_len < header_protection->algo->iv_size + QUICLY_MAX_PN_SIZE)
+ return;
+ ptls_cipher_init (header_protection,
+ pctx->packet.octets.base + pctx->packet.encrypted_off +
+ QUICLY_MAX_PN_SIZE);
+ ptls_cipher_encrypt (header_protection, hpmask, hpmask, sizeof (hpmask));
+ pctx->packet.octets.base[0] ^=
+ hpmask[0] & (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]) ?
+ 0xf : 0x1f);
+ pnlen = (pctx->packet.octets.base[0] & 0x3) + 1;
+ for (i = 0; i != pnlen; ++i)
+ {
+ pctx->packet.octets.base[pctx->packet.encrypted_off + i] ^=
+ hpmask[i + 1];
+ pnbits =
+ (pnbits << 8) | pctx->packet.octets.base[pctx->packet.encrypted_off +
+ i];
+ }
+
+ size_t aead_off = pctx->packet.encrypted_off + pnlen;
+
+ pn =
+ quicly_determine_packet_number (pnbits, pnlen * 8,
+ next_expected_packet_number);
+
+ int key_phase_bit =
+ (pctx->packet.octets.base[0] & QUICLY_KEY_PHASE_BIT) != 0;
+
+ if (key_phase_bit != (qctx->key_phase_ingress & 1))
+ {
+ pctx->packet.octets.base[0] ^=
+ hpmask[0] &
+ (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]) ? 0xf :
+ 0x1f);
+ for (i = 0; i != pnlen; ++i)
+ {
+ pctx->packet.octets.base[pctx->packet.encrypted_off + i] ^=
+ hpmask[i + 1];
+ }
+ return;
+ }
+
+ if ((ptlen =
+ quic_crypto_offload_aead_decrypt (qctx, aead,
+ pctx->packet.octets.base + aead_off,
+ pctx->packet.octets.base + aead_off,
+ pctx->packet.octets.len - aead_off,
+ pn, pctx->packet.octets.base,
+ aead_off)) == SIZE_MAX)
+ {
+ fprintf (stderr,
+ "%s: aead decryption failure (pn: %d)\n", __FUNCTION__, pn);
+ return;
+ }
+
+ pctx->packet.encrypted_off = aead_off;
+ pctx->packet.octets.len = ptlen + aead_off;
+
+ pctx->packet.decrypted.pn = pn;
+ pctx->packet.decrypted.key_phase = qctx->key_phase_ingress;
+}
+
+#ifdef QUIC_HP_CRYPTO