ngtcp2_crypto.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_CRYPTO_H
  26. #define NGTCP2_CRYPTO_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_mem.h"
  32. /* NGTCP2_INITIAL_AEAD_OVERHEAD is an overhead of AEAD used by Initial
  33. packets. Because QUIC uses AEAD_AES_128_GCM, the overhead is 16
  34. bytes. */
  35. #define NGTCP2_INITIAL_AEAD_OVERHEAD 16
  36. /* NGTCP2_MAX_AEAD_OVERHEAD is the maximum AEAD overhead. */
  37. #define NGTCP2_MAX_AEAD_OVERHEAD 16
  38. /* NGTCP2_CRYPTO_KM_FLAG_NONE indicates that no flag is set. */
  39. #define NGTCP2_CRYPTO_KM_FLAG_NONE 0x00u
  40. /* NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE is set if key phase bit is
  41. set. */
  42. #define NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE 0x01u
  43. typedef struct ngtcp2_crypto_km {
  44. ngtcp2_vec secret;
  45. ngtcp2_crypto_aead_ctx aead_ctx;
  46. ngtcp2_vec iv;
  47. /* pkt_num is a packet number of a packet which uses this keying
  48. material. For encryption key, it is the lowest packet number of
  49. a packet. For decryption key, it is the lowest packet number of
  50. a packet which can be decrypted with this keying material. */
  51. int64_t pkt_num;
  52. /* use_count is the number of encryption applied with this key. */
  53. uint64_t use_count;
  54. /* flags is the bitwise OR of zero or more of
  55. NGTCP2_CRYPTO_KM_FLAG_*. */
  56. uint8_t flags;
  57. } ngtcp2_crypto_km;
  58. /*
  59. * ngtcp2_crypto_km_new creates new ngtcp2_crypto_km object, and
  60. * assigns its pointer to |*pckm|. The |secret| of length
  61. * |secretlen|, |aead_ctx|, and the |iv| of length |ivlen| are copied
  62. * to |*pckm|. If |secretlen| == 0, the function assumes no secret is
  63. * given which is acceptable. The sole reason to store secret is
  64. * update keys. Only 1RTT key can be updated.
  65. */
  66. int ngtcp2_crypto_km_new(ngtcp2_crypto_km **pckm, const uint8_t *secret,
  67. size_t secretlen,
  68. const ngtcp2_crypto_aead_ctx *aead_ctx,
  69. const uint8_t *iv, size_t ivlen,
  70. const ngtcp2_mem *mem);
  71. /*
  72. * ngtcp2_crypto_km_nocopy_new is similar to ngtcp2_crypto_km_new, but
  73. * it does not copy secret, aead context, and IV.
  74. */
  75. int ngtcp2_crypto_km_nocopy_new(ngtcp2_crypto_km **pckm, size_t secretlen,
  76. size_t ivlen, const ngtcp2_mem *mem);
  77. void ngtcp2_crypto_km_del(ngtcp2_crypto_km *ckm, const ngtcp2_mem *mem);
  78. typedef struct ngtcp2_crypto_cc {
  79. ngtcp2_crypto_aead aead;
  80. ngtcp2_crypto_cipher hp;
  81. ngtcp2_crypto_km *ckm;
  82. ngtcp2_crypto_cipher_ctx hp_ctx;
  83. ngtcp2_encrypt encrypt;
  84. ngtcp2_decrypt decrypt;
  85. ngtcp2_hp_mask hp_mask;
  86. } ngtcp2_crypto_cc;
  87. void ngtcp2_crypto_create_nonce(uint8_t *dest, const uint8_t *iv, size_t ivlen,
  88. int64_t pkt_num);
  89. #endif /* !defined(NGTCP2_CRYPTO_H) */