11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * ngtcp2
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Copyright (c) 2017 ngtcp2 contributors
51cb0ef41Sopenharmony_ci *
61cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
71cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the
81cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
91cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
101cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
111cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
121cb0ef41Sopenharmony_ci * the following conditions:
131cb0ef41Sopenharmony_ci *
141cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be
151cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software.
161cb0ef41Sopenharmony_ci *
171cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
181cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
191cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
201cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
211cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
221cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
231cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
241cb0ef41Sopenharmony_ci */
251cb0ef41Sopenharmony_ci#ifndef NGTCP2_PPE_H
261cb0ef41Sopenharmony_ci#define NGTCP2_PPE_H
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#ifdef HAVE_CONFIG_H
291cb0ef41Sopenharmony_ci#  include <config.h>
301cb0ef41Sopenharmony_ci#endif /* HAVE_CONFIG_H */
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci#include <ngtcp2/ngtcp2.h>
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#include "ngtcp2_pkt.h"
351cb0ef41Sopenharmony_ci#include "ngtcp2_buf.h"
361cb0ef41Sopenharmony_ci#include "ngtcp2_crypto.h"
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci/*
391cb0ef41Sopenharmony_ci * ngtcp2_ppe is the Protected Packet Encoder.
401cb0ef41Sopenharmony_ci */
411cb0ef41Sopenharmony_citypedef struct ngtcp2_ppe {
421cb0ef41Sopenharmony_ci  ngtcp2_buf buf;
431cb0ef41Sopenharmony_ci  ngtcp2_crypto_cc *cc;
441cb0ef41Sopenharmony_ci  /* hdlen is the number of bytes for packet header written in buf. */
451cb0ef41Sopenharmony_ci  size_t hdlen;
461cb0ef41Sopenharmony_ci  /* len_offset is the offset to Length field. */
471cb0ef41Sopenharmony_ci  size_t len_offset;
481cb0ef41Sopenharmony_ci  /* pkt_num_offset is the offset to packet number field. */
491cb0ef41Sopenharmony_ci  size_t pkt_num_offset;
501cb0ef41Sopenharmony_ci  /* pkt_numlen is the number of bytes used to encode a packet
511cb0ef41Sopenharmony_ci     number */
521cb0ef41Sopenharmony_ci  size_t pkt_numlen;
531cb0ef41Sopenharmony_ci  /* sample_offset is the offset to sample for packet number
541cb0ef41Sopenharmony_ci     encryption. */
551cb0ef41Sopenharmony_ci  size_t sample_offset;
561cb0ef41Sopenharmony_ci  /* pkt_num is the packet number written in buf. */
571cb0ef41Sopenharmony_ci  int64_t pkt_num;
581cb0ef41Sopenharmony_ci  /* nonce is the buffer to store nonce.  It should be equal or longer
591cb0ef41Sopenharmony_ci     than then length of IV. */
601cb0ef41Sopenharmony_ci  uint8_t nonce[32];
611cb0ef41Sopenharmony_ci} ngtcp2_ppe;
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci/*
641cb0ef41Sopenharmony_ci * ngtcp2_ppe_init initializes |ppe| with the given buffer.
651cb0ef41Sopenharmony_ci */
661cb0ef41Sopenharmony_civoid ngtcp2_ppe_init(ngtcp2_ppe *ppe, uint8_t *out, size_t outlen,
671cb0ef41Sopenharmony_ci                     ngtcp2_crypto_cc *cc);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci/*
701cb0ef41Sopenharmony_ci * ngtcp2_ppe_encode_hd encodes |hd|.
711cb0ef41Sopenharmony_ci *
721cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or one of the following
731cb0ef41Sopenharmony_ci * negative error codes:
741cb0ef41Sopenharmony_ci *
751cb0ef41Sopenharmony_ci * NGTCP2_ERR_NOBUF
761cb0ef41Sopenharmony_ci *     The buffer is too small.
771cb0ef41Sopenharmony_ci */
781cb0ef41Sopenharmony_ciint ngtcp2_ppe_encode_hd(ngtcp2_ppe *ppe, const ngtcp2_pkt_hd *hd);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci/*
811cb0ef41Sopenharmony_ci * ngtcp2_ppe_encode_frame encodes |fr|.
821cb0ef41Sopenharmony_ci *
831cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or one of the following
841cb0ef41Sopenharmony_ci * negative error codes:
851cb0ef41Sopenharmony_ci *
861cb0ef41Sopenharmony_ci * NGTCP2_ERR_NOBUF
871cb0ef41Sopenharmony_ci *     The buffer is too small.
881cb0ef41Sopenharmony_ci */
891cb0ef41Sopenharmony_ciint ngtcp2_ppe_encode_frame(ngtcp2_ppe *ppe, ngtcp2_frame *fr);
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci/*
921cb0ef41Sopenharmony_ci * ngtcp2_ppe_final encrypts QUIC packet payload.  If |**ppkt| is not
931cb0ef41Sopenharmony_ci * NULL, the pointer to the packet is assigned to it.
941cb0ef41Sopenharmony_ci *
951cb0ef41Sopenharmony_ci * This function returns the length of QUIC packet, including header,
961cb0ef41Sopenharmony_ci * and payload if it succeeds, or one of the following negative error
971cb0ef41Sopenharmony_ci * codes:
981cb0ef41Sopenharmony_ci *
991cb0ef41Sopenharmony_ci * NGTCP2_ERR_CALLBACK_FAILURE
1001cb0ef41Sopenharmony_ci *     User-defined callback function failed.
1011cb0ef41Sopenharmony_ci */
1021cb0ef41Sopenharmony_cingtcp2_ssize ngtcp2_ppe_final(ngtcp2_ppe *ppe, const uint8_t **ppkt);
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci/*
1051cb0ef41Sopenharmony_ci * ngtcp2_ppe_left returns the number of bytes left to write
1061cb0ef41Sopenharmony_ci * additional frames.  This does not count AEAD overhead.
1071cb0ef41Sopenharmony_ci */
1081cb0ef41Sopenharmony_cisize_t ngtcp2_ppe_left(ngtcp2_ppe *ppe);
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci/*
1111cb0ef41Sopenharmony_ci * ngtcp2_ppe_pktlen returns the provisional packet length.  It
1121cb0ef41Sopenharmony_ci * includes AEAD overhead.
1131cb0ef41Sopenharmony_ci */
1141cb0ef41Sopenharmony_cisize_t ngtcp2_ppe_pktlen(ngtcp2_ppe *ppe);
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci/**
1171cb0ef41Sopenharmony_ci * @function
1181cb0ef41Sopenharmony_ci *
1191cb0ef41Sopenharmony_ci * `ngtcp2_ppe_padding` encodes PADDING frames to the end of the
1201cb0ef41Sopenharmony_ci * buffer.  This function returns the number of bytes padded.
1211cb0ef41Sopenharmony_ci */
1221cb0ef41Sopenharmony_cisize_t ngtcp2_ppe_padding(ngtcp2_ppe *ppe);
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci/*
1251cb0ef41Sopenharmony_ci * ngtcp2_ppe_padding_hp_sample adds PADDING frame if the current
1261cb0ef41Sopenharmony_ci * payload does not have enough space for header protection sample.
1271cb0ef41Sopenharmony_ci * This function should be called just before calling
1281cb0ef41Sopenharmony_ci * ngtcp2_ppe_final().
1291cb0ef41Sopenharmony_ci *
1301cb0ef41Sopenharmony_ci * This function returns the number of bytes added as padding.
1311cb0ef41Sopenharmony_ci */
1321cb0ef41Sopenharmony_cisize_t ngtcp2_ppe_padding_hp_sample(ngtcp2_ppe *ppe);
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci/*
1351cb0ef41Sopenharmony_ci * ngtcp2_ppe_padding_size adds PADDING frame so that the size of QUIC
1361cb0ef41Sopenharmony_ci * packet is at least |n| bytes long.  If it is unable to add PADDING
1371cb0ef41Sopenharmony_ci * in that way, this function still adds PADDING frame as much as
1381cb0ef41Sopenharmony_ci * possible.  This function should be called just before calling
1391cb0ef41Sopenharmony_ci * ngtcp2_ppe_final().  For Short packet, this function should be
1401cb0ef41Sopenharmony_ci * called instead of ngtcp2_ppe_padding_hp_sample.
1411cb0ef41Sopenharmony_ci *
1421cb0ef41Sopenharmony_ci * This function returns the number of bytes added as padding.
1431cb0ef41Sopenharmony_ci */
1441cb0ef41Sopenharmony_cisize_t ngtcp2_ppe_padding_size(ngtcp2_ppe *ppe, size_t n);
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci/*
1471cb0ef41Sopenharmony_ci * ngtcp2_ppe_ensure_hp_sample returns nonzero if the buffer has
1481cb0ef41Sopenharmony_ci * enough space for header protection sample.  This should be called
1491cb0ef41Sopenharmony_ci * right after packet header is written.
1501cb0ef41Sopenharmony_ci */
1511cb0ef41Sopenharmony_ciint ngtcp2_ppe_ensure_hp_sample(ngtcp2_ppe *ppe);
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci#endif /* NGTCP2_PPE_H */
154