• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif /* HAVE_CONFIG_H */
31 
32 #include <ngtcp2/ngtcp2.h>
33 
34 #include "ngtcp2_mem.h"
35 
36 /* NGTCP2_INITIAL_AEAD_OVERHEAD is an overhead of AEAD used by Initial
37    packets.  Because QUIC uses AEAD_AES_128_GCM, the overhead is 16
38    bytes. */
39 #define NGTCP2_INITIAL_AEAD_OVERHEAD 16
40 
41 /* NGTCP2_MAX_AEAD_OVERHEAD is expected maximum AEAD overhead. */
42 #define NGTCP2_MAX_AEAD_OVERHEAD 16
43 
44 /* ngtcp2_transport_param_id is the registry of QUIC transport
45    parameter ID. */
46 typedef enum ngtcp2_transport_param_id {
47   NGTCP2_TRANSPORT_PARAM_ORIGINAL_DESTINATION_CONNECTION_ID = 0x0000,
48   NGTCP2_TRANSPORT_PARAM_MAX_IDLE_TIMEOUT = 0x0001,
49   NGTCP2_TRANSPORT_PARAM_STATELESS_RESET_TOKEN = 0x0002,
50   NGTCP2_TRANSPORT_PARAM_MAX_UDP_PAYLOAD_SIZE = 0x0003,
51   NGTCP2_TRANSPORT_PARAM_INITIAL_MAX_DATA = 0x0004,
52   NGTCP2_TRANSPORT_PARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL = 0x0005,
53   NGTCP2_TRANSPORT_PARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE = 0x0006,
54   NGTCP2_TRANSPORT_PARAM_INITIAL_MAX_STREAM_DATA_UNI = 0x0007,
55   NGTCP2_TRANSPORT_PARAM_INITIAL_MAX_STREAMS_BIDI = 0x0008,
56   NGTCP2_TRANSPORT_PARAM_INITIAL_MAX_STREAMS_UNI = 0x0009,
57   NGTCP2_TRANSPORT_PARAM_ACK_DELAY_EXPONENT = 0x000a,
58   NGTCP2_TRANSPORT_PARAM_MAX_ACK_DELAY = 0x000b,
59   NGTCP2_TRANSPORT_PARAM_DISABLE_ACTIVE_MIGRATION = 0x000c,
60   NGTCP2_TRANSPORT_PARAM_PREFERRED_ADDRESS = 0x000d,
61   NGTCP2_TRANSPORT_PARAM_ACTIVE_CONNECTION_ID_LIMIT = 0x000e,
62   NGTCP2_TRANSPORT_PARAM_INITIAL_SOURCE_CONNECTION_ID = 0x000f,
63   NGTCP2_TRANSPORT_PARAM_RETRY_SOURCE_CONNECTION_ID = 0x0010,
64   /* https://datatracker.ietf.org/doc/html/rfc9221 */
65   NGTCP2_TRANSPORT_PARAM_MAX_DATAGRAM_FRAME_SIZE = 0x0020,
66   NGTCP2_TRANSPORT_PARAM_GREASE_QUIC_BIT = 0x2ab2,
67   /* https://quicwg.org/quic-v2/draft-ietf-quic-v2.html */
68   NGTCP2_TRANSPORT_PARAM_VERSION_INFORMATION_DRAFT = 0xff73db,
69 } ngtcp2_transport_param_id;
70 
71 /* NGTCP2_CRYPTO_KM_FLAG_NONE indicates that no flag is set. */
72 #define NGTCP2_CRYPTO_KM_FLAG_NONE 0x00u
73 /* NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE is set if key phase bit is
74    set. */
75 #define NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE 0x01u
76 
77 typedef struct ngtcp2_crypto_km {
78   ngtcp2_vec secret;
79   ngtcp2_crypto_aead_ctx aead_ctx;
80   ngtcp2_vec iv;
81   /* pkt_num is a packet number of a packet which uses this keying
82      material.  For encryption key, it is the lowest packet number of
83      a packet.  For decryption key, it is the lowest packet number of
84      a packet which can be decrypted with this keying material. */
85   int64_t pkt_num;
86   /* use_count is the number of encryption applied with this key.
87      This field is only used for tx key. */
88   uint64_t use_count;
89   /* flags is the bitwise OR of zero or more of
90      NGTCP2_CRYPTO_KM_FLAG_*. */
91   uint8_t flags;
92 } ngtcp2_crypto_km;
93 
94 /*
95  * ngtcp2_crypto_km_new creates new ngtcp2_crypto_km object and
96  * assigns its pointer to |*pckm|.  The |secret| of length
97  * |secretlen|, the |key| of length |keylen| and the |iv| of length
98  * |ivlen| are copied to |*pckm|.  If |secretlen| == 0, the function
99  * assumes no secret is given which is acceptable.  The sole reason to
100  * store secret is update keys.  Only 1RTT key can be updated.
101  */
102 int ngtcp2_crypto_km_new(ngtcp2_crypto_km **pckm, const uint8_t *secret,
103                          size_t secretlen,
104                          const ngtcp2_crypto_aead_ctx *aead_ctx,
105                          const uint8_t *iv, size_t ivlen,
106                          const ngtcp2_mem *mem);
107 
108 /*
109  * ngtcp2_crypto_km_nocopy_new is similar to ngtcp2_crypto_km_new, but
110  * it does not copy secret, key and IV.
111  */
112 int ngtcp2_crypto_km_nocopy_new(ngtcp2_crypto_km **pckm, size_t secretlen,
113                                 size_t ivlen, const ngtcp2_mem *mem);
114 
115 void ngtcp2_crypto_km_del(ngtcp2_crypto_km *ckm, const ngtcp2_mem *mem);
116 
117 typedef struct ngtcp2_crypto_cc {
118   ngtcp2_crypto_aead aead;
119   ngtcp2_crypto_cipher hp;
120   ngtcp2_crypto_km *ckm;
121   ngtcp2_crypto_cipher_ctx hp_ctx;
122   ngtcp2_encrypt encrypt;
123   ngtcp2_decrypt decrypt;
124   ngtcp2_hp_mask hp_mask;
125 } ngtcp2_crypto_cc;
126 
127 void ngtcp2_crypto_create_nonce(uint8_t *dest, const uint8_t *iv, size_t ivlen,
128                                 int64_t pkt_num);
129 
130 /*
131  * ngtcp2_transport_params_copy_new makes a copy of |src|, and assigns
132  * it to |*pdest|.  If |src| is NULL, NULL is assigned to |*pdest|.
133  *
134  * Caller is responsible to call ngtcp2_transport_params_del to free
135  * the memory assigned to |*pdest|.
136  *
137  * This function returns 0 if it succeeds, or one of the following
138  * negative error codes:
139  *
140  * NGTCP2_ERR_NOMEM
141  *     Out of memory.
142  */
143 int ngtcp2_transport_params_copy_new(ngtcp2_transport_params **pdest,
144                                      const ngtcp2_transport_params *src,
145                                      const ngtcp2_mem *mem);
146 
147 #endif /* NGTCP2_CRYPTO_H */
148