ngtcp2_ppe.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGTCP2_PPE_H
  26. #define NGTCP2_PPE_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_pkt.h"
  32. #include "ngtcp2_buf.h"
  33. #include "ngtcp2_crypto.h"
  34. /*
  35. * ngtcp2_ppe is the QUIC Packet Encoder.
  36. */
  37. typedef struct ngtcp2_ppe {
  38. /* buf is the buffer where a QUIC packet is written. */
  39. ngtcp2_buf buf;
  40. /* cc is the encryption context that includes callback functions to
  41. encrypt a QUIC packet, and AEAD cipher, etc. */
  42. ngtcp2_crypto_cc *cc;
  43. /* dgram_offset is the offset in UDP datagram payload that this QUIC
  44. packet is positioned at. */
  45. size_t dgram_offset;
  46. /* hdlen is the number of bytes for packet header written in buf. */
  47. size_t hdlen;
  48. /* len_offset is the offset to Length field. */
  49. size_t len_offset;
  50. /* pkt_num_offset is the offset to packet number field. */
  51. size_t pkt_num_offset;
  52. /* pkt_numlen is the number of bytes used to encode a packet
  53. number */
  54. size_t pkt_numlen;
  55. /* pkt_num is the packet number written in buf. */
  56. int64_t pkt_num;
  57. /* nonce is the buffer to store nonce. It should be equal or longer
  58. than the length of IV. */
  59. uint8_t nonce[32];
  60. } ngtcp2_ppe;
  61. /*
  62. * ngtcp2_ppe_init initializes |ppe| with the given buffer.
  63. */
  64. void ngtcp2_ppe_init(ngtcp2_ppe *ppe, uint8_t *out, size_t outlen,
  65. size_t dgram_offset, ngtcp2_crypto_cc *cc);
  66. /*
  67. * ngtcp2_ppe_encode_hd encodes |hd|.
  68. *
  69. * This function returns 0 if it succeeds, or one of the following
  70. * negative error codes:
  71. *
  72. * NGTCP2_ERR_NOBUF
  73. * The buffer is too small.
  74. */
  75. int ngtcp2_ppe_encode_hd(ngtcp2_ppe *ppe, const ngtcp2_pkt_hd *hd);
  76. /*
  77. * ngtcp2_ppe_encode_frame encodes |fr|.
  78. *
  79. * This function returns 0 if it succeeds, or one of the following
  80. * negative error codes:
  81. *
  82. * NGTCP2_ERR_NOBUF
  83. * The buffer is too small.
  84. */
  85. int ngtcp2_ppe_encode_frame(ngtcp2_ppe *ppe, ngtcp2_frame *fr);
  86. /*
  87. * ngtcp2_ppe_final encrypts QUIC packet payload. If |ppkt| is not
  88. * NULL, the pointer to the packet is assigned to it.
  89. *
  90. * This function returns the length of QUIC packet, including header,
  91. * and payload if it succeeds, or one of the following negative error
  92. * codes:
  93. *
  94. * NGTCP2_ERR_CALLBACK_FAILURE
  95. * User-defined callback function failed.
  96. */
  97. ngtcp2_ssize ngtcp2_ppe_final(ngtcp2_ppe *ppe, const uint8_t **ppkt);
  98. /*
  99. * ngtcp2_ppe_left returns the number of bytes left to write
  100. * additional frames. This does not count AEAD overhead.
  101. */
  102. size_t ngtcp2_ppe_left(const ngtcp2_ppe *ppe);
  103. /*
  104. * ngtcp2_ppe_pktlen returns the provisional packet length. It
  105. * includes AEAD overhead.
  106. */
  107. size_t ngtcp2_ppe_pktlen(const ngtcp2_ppe *ppe);
  108. /*
  109. * ngtcp2_ppe_dgram_padding is equivalent to call
  110. * ngtcp2_ppe_dgram_padding_size(ppe, NGTCP2_MAX_UDP_PAYLOAD_SIZE).
  111. * This function should be called just before calling
  112. * ngtcp2_ppe_final().
  113. *
  114. * This function returns the number of bytes padded.
  115. */
  116. size_t ngtcp2_ppe_dgram_padding(ngtcp2_ppe *ppe);
  117. /*
  118. * ngtcp2_ppe_dgram_padding_size adds PADDING frame so that the size
  119. * of a UDP datagram payload is at least |n| bytes long. If it is
  120. * unable to add PADDING in that way, this function still adds PADDING
  121. * frame as much as possible. This function should be called just
  122. * before calling ngtcp2_ppe_final().
  123. *
  124. * This function returns the number of bytes added as padding.
  125. */
  126. size_t ngtcp2_ppe_dgram_padding_size(ngtcp2_ppe *ppe, size_t n);
  127. /*
  128. * ngtcp2_ppe_padding_size adds PADDING frame so that the size of QUIC
  129. * packet is at least |n| bytes long and the current payload has
  130. * enough space for header protection sample. If it is unable to add
  131. * PADDING at least |n| bytes, this function still adds PADDING frames
  132. * as much as possible. This function also adds PADDING frames so
  133. * that the minimum padding requirement of header protection is met.
  134. * Those padding may be larger than |n| bytes. It is recommended to
  135. * make sure that ngtcp2_ppe_ensure_hp_sample succeeds after writing
  136. * QUIC packet header. This function should be called just before
  137. * calling ngtcp2_ppe_final().
  138. *
  139. * This function returns the number of bytes added as padding.
  140. */
  141. size_t ngtcp2_ppe_padding_size(ngtcp2_ppe *ppe, size_t n);
  142. /*
  143. * ngtcp2_ppe_ensure_hp_sample returns nonzero if the buffer has
  144. * enough space for header protection sample. This should be called
  145. * right after packet header is written.
  146. */
  147. int ngtcp2_ppe_ensure_hp_sample(ngtcp2_ppe *ppe);
  148. #endif /* !defined(NGTCP2_PPE_H) */