ngtcp2_ppe.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include "ngtcp2_ppe.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "ngtcp2_str.h"
  29. #include "ngtcp2_conv.h"
  30. #include "ngtcp2_macro.h"
  31. void ngtcp2_ppe_init(ngtcp2_ppe *ppe, uint8_t *out, size_t outlen,
  32. size_t dgram_offset, ngtcp2_crypto_cc *cc) {
  33. ngtcp2_buf_init(&ppe->buf, out, outlen);
  34. ppe->dgram_offset = dgram_offset;
  35. ppe->hdlen = 0;
  36. ppe->len_offset = 0;
  37. ppe->pkt_num_offset = 0;
  38. ppe->pkt_numlen = 0;
  39. ppe->pkt_num = 0;
  40. ppe->cc = cc;
  41. }
  42. int ngtcp2_ppe_encode_hd(ngtcp2_ppe *ppe, const ngtcp2_pkt_hd *hd) {
  43. ngtcp2_ssize rv;
  44. ngtcp2_buf *buf = &ppe->buf;
  45. ngtcp2_crypto_cc *cc = ppe->cc;
  46. if (ngtcp2_buf_left(buf) < cc->aead.max_overhead) {
  47. return NGTCP2_ERR_NOBUF;
  48. }
  49. if (hd->flags & NGTCP2_PKT_FLAG_LONG_FORM) {
  50. ppe->len_offset = 1 + 4 + 1 + hd->dcid.datalen + 1 + hd->scid.datalen;
  51. if (hd->type == NGTCP2_PKT_INITIAL) {
  52. ppe->len_offset += ngtcp2_put_uvarintlen(hd->tokenlen) + hd->tokenlen;
  53. }
  54. ppe->pkt_num_offset = ppe->len_offset + NGTCP2_PKT_LENGTHLEN;
  55. rv = ngtcp2_pkt_encode_hd_long(
  56. buf->last, ngtcp2_buf_left(buf) - cc->aead.max_overhead, hd);
  57. } else {
  58. ppe->pkt_num_offset = 1 + hd->dcid.datalen;
  59. rv = ngtcp2_pkt_encode_hd_short(
  60. buf->last, ngtcp2_buf_left(buf) - cc->aead.max_overhead, hd);
  61. }
  62. if (rv < 0) {
  63. return (int)rv;
  64. }
  65. buf->last += rv;
  66. ppe->pkt_numlen = hd->pkt_numlen;
  67. ppe->hdlen = (size_t)rv;
  68. ppe->pkt_num = hd->pkt_num;
  69. return 0;
  70. }
  71. int ngtcp2_ppe_encode_frame(ngtcp2_ppe *ppe, ngtcp2_frame *fr) {
  72. ngtcp2_ssize rv;
  73. ngtcp2_buf *buf = &ppe->buf;
  74. ngtcp2_crypto_cc *cc = ppe->cc;
  75. if (ngtcp2_buf_left(buf) < cc->aead.max_overhead) {
  76. return NGTCP2_ERR_NOBUF;
  77. }
  78. rv = ngtcp2_pkt_encode_frame(
  79. buf->last, ngtcp2_buf_left(buf) - cc->aead.max_overhead, fr);
  80. if (rv < 0) {
  81. return (int)rv;
  82. }
  83. buf->last += rv;
  84. return 0;
  85. }
  86. /*
  87. * ppe_sample_offset returns the offset to sample for packet number
  88. * encryption.
  89. */
  90. static size_t ppe_sample_offset(ngtcp2_ppe *ppe) {
  91. return ppe->pkt_num_offset + 4;
  92. }
  93. ngtcp2_ssize ngtcp2_ppe_final(ngtcp2_ppe *ppe, const uint8_t **ppkt) {
  94. ngtcp2_buf *buf = &ppe->buf;
  95. ngtcp2_crypto_cc *cc = ppe->cc;
  96. uint8_t *payload = buf->begin + ppe->hdlen;
  97. size_t payloadlen = ngtcp2_buf_len(buf) - ppe->hdlen;
  98. uint8_t mask[NGTCP2_HP_SAMPLELEN];
  99. uint8_t *p;
  100. size_t i;
  101. int rv;
  102. assert(cc->encrypt);
  103. assert(cc->hp_mask);
  104. if (ppe->len_offset) {
  105. ngtcp2_put_uvarint30(
  106. buf->begin + ppe->len_offset,
  107. (uint16_t)(payloadlen + ppe->pkt_numlen + cc->aead.max_overhead));
  108. }
  109. ngtcp2_crypto_create_nonce(ppe->nonce, cc->ckm->iv.base, cc->ckm->iv.len,
  110. ppe->pkt_num);
  111. rv = cc->encrypt(payload, &cc->aead, &cc->ckm->aead_ctx, payload, payloadlen,
  112. ppe->nonce, cc->ckm->iv.len, buf->begin, ppe->hdlen);
  113. if (rv != 0) {
  114. return NGTCP2_ERR_CALLBACK_FAILURE;
  115. }
  116. buf->last = payload + payloadlen + cc->aead.max_overhead;
  117. /* Make sure that we have enough space to get sample */
  118. assert(ppe_sample_offset(ppe) + NGTCP2_HP_SAMPLELEN <= ngtcp2_buf_len(buf));
  119. rv = cc->hp_mask(mask, &cc->hp, &cc->hp_ctx,
  120. buf->begin + ppe_sample_offset(ppe));
  121. if (rv != 0) {
  122. return NGTCP2_ERR_CALLBACK_FAILURE;
  123. }
  124. p = buf->begin;
  125. if (*p & NGTCP2_HEADER_FORM_BIT) {
  126. *p = (uint8_t)(*p ^ (mask[0] & 0x0f));
  127. } else {
  128. *p = (uint8_t)(*p ^ (mask[0] & 0x1f));
  129. }
  130. p = buf->begin + ppe->pkt_num_offset;
  131. for (i = 0; i < ppe->pkt_numlen; ++i) {
  132. *(p + i) ^= mask[i + 1];
  133. }
  134. if (ppkt != NULL) {
  135. *ppkt = buf->begin;
  136. }
  137. return (ngtcp2_ssize)ngtcp2_buf_len(buf);
  138. }
  139. size_t ngtcp2_ppe_left(const ngtcp2_ppe *ppe) {
  140. ngtcp2_crypto_cc *cc = ppe->cc;
  141. if (ngtcp2_buf_left(&ppe->buf) < cc->aead.max_overhead) {
  142. return 0;
  143. }
  144. return ngtcp2_buf_left(&ppe->buf) - cc->aead.max_overhead;
  145. }
  146. size_t ngtcp2_ppe_pktlen(const ngtcp2_ppe *ppe) {
  147. ngtcp2_crypto_cc *cc = ppe->cc;
  148. return ngtcp2_buf_len(&ppe->buf) + cc->aead.max_overhead;
  149. }
  150. size_t ngtcp2_ppe_padding_size(ngtcp2_ppe *ppe, size_t n) {
  151. ngtcp2_crypto_cc *cc = ppe->cc;
  152. ngtcp2_buf *buf = &ppe->buf;
  153. size_t pktlen = ngtcp2_buf_len(buf) + cc->aead.max_overhead;
  154. size_t len = 0;
  155. size_t max_samplelen;
  156. n = ngtcp2_min_size(n, ngtcp2_buf_cap(buf));
  157. if (pktlen < n) {
  158. len = n - pktlen;
  159. }
  160. /* Ensure header protection sample */
  161. max_samplelen =
  162. ngtcp2_buf_len(buf) + cc->aead.max_overhead - ppe_sample_offset(ppe);
  163. if (max_samplelen < NGTCP2_HP_SAMPLELEN) {
  164. len = ngtcp2_max_size(len, NGTCP2_HP_SAMPLELEN - max_samplelen);
  165. }
  166. assert(ngtcp2_buf_left(buf) >= len + cc->aead.max_overhead);
  167. buf->last = ngtcp2_setmem(buf->last, 0, len);
  168. return len;
  169. }
  170. size_t ngtcp2_ppe_dgram_padding(ngtcp2_ppe *ppe) {
  171. return ngtcp2_ppe_dgram_padding_size(ppe, NGTCP2_MAX_UDP_PAYLOAD_SIZE);
  172. }
  173. size_t ngtcp2_ppe_dgram_padding_size(ngtcp2_ppe *ppe, size_t n) {
  174. ngtcp2_crypto_cc *cc = ppe->cc;
  175. ngtcp2_buf *buf = &ppe->buf;
  176. size_t dgramlen =
  177. ppe->dgram_offset + ngtcp2_buf_len(buf) + cc->aead.max_overhead;
  178. size_t len;
  179. n = ngtcp2_min_size(n, ppe->dgram_offset + ngtcp2_buf_cap(buf));
  180. if (dgramlen >= n) {
  181. return 0;
  182. }
  183. len = n - dgramlen;
  184. buf->last = ngtcp2_setmem(buf->last, 0, len);
  185. return len;
  186. }
  187. int ngtcp2_ppe_ensure_hp_sample(ngtcp2_ppe *ppe) {
  188. ngtcp2_buf *buf = &ppe->buf;
  189. return ngtcp2_buf_left(buf) >= (4 - ppe->pkt_numlen) + NGTCP2_HP_SAMPLELEN;
  190. }