830c755779a919864ea8630d0a28d7321f97f30e
[openssl.git] / include / internal / quic_types.h
1 /*
2  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OSSL_QUIC_TYPES_H
11 # define OSSL_QUIC_TYPES_H
12
13 # include <openssl/ssl.h>
14 # include <internal/ssl.h>
15 # include <assert.h>
16 # include <string.h>
17
18 # ifndef OPENSSL_NO_QUIC
19
20 /* QUIC encryption levels. */
21 #  define QUIC_ENC_LEVEL_INITIAL          0
22 #  define QUIC_ENC_LEVEL_HANDSHAKE        1
23 #  define QUIC_ENC_LEVEL_0RTT             2
24 #  define QUIC_ENC_LEVEL_1RTT             3
25 #  define QUIC_ENC_LEVEL_NUM              4
26
27 /* QUIC packet number spaces. */
28 #  define QUIC_PN_SPACE_INITIAL           0
29 #  define QUIC_PN_SPACE_HANDSHAKE         1
30 #  define QUIC_PN_SPACE_APP               2
31 #  define QUIC_PN_SPACE_NUM               3
32
33 static ossl_unused ossl_inline uint32_t
34 ossl_quic_enc_level_to_pn_space(uint32_t enc_level)
35 {
36     switch (enc_level) {
37     case QUIC_ENC_LEVEL_INITIAL:
38         return QUIC_PN_SPACE_INITIAL;
39     case QUIC_ENC_LEVEL_HANDSHAKE:
40         return QUIC_PN_SPACE_HANDSHAKE;
41     case QUIC_ENC_LEVEL_0RTT:
42     case QUIC_ENC_LEVEL_1RTT:
43         return QUIC_PN_SPACE_APP;
44     default:
45         assert(0);
46         return QUIC_PN_SPACE_APP;
47     }
48 }
49
50 /* QUIC packet number spaces. */
51 #  define QUIC_PN_SPACE_INITIAL       0
52 #  define QUIC_PN_SPACE_HANDSHAKE     1
53 #  define QUIC_PN_SPACE_APP           2
54 #  define QUIC_PN_SPACE_NUM           3
55
56 /* QUIC packet number representation. */
57 typedef uint64_t QUIC_PN;
58 #  define QUIC_PN_INVALID            UINT64_MAX
59
60 static ossl_unused ossl_inline QUIC_PN ossl_quic_pn_max(QUIC_PN a, QUIC_PN b)
61 {
62     return a > b ? a : b;
63 }
64
65 static ossl_unused ossl_inline QUIC_PN ossl_quic_pn_min(QUIC_PN a, QUIC_PN b)
66 {
67     return a < b ? a : b;
68 }
69
70 /* QUIC connection ID representation. */
71 #  define QUIC_MAX_CONN_ID_LEN   20
72
73 typedef struct quic_conn_id_st {
74     unsigned char id_len, id[QUIC_MAX_CONN_ID_LEN];
75 } QUIC_CONN_ID;
76
77 static ossl_unused ossl_inline int ossl_quic_conn_id_eq(const QUIC_CONN_ID *a,
78                                                         const QUIC_CONN_ID *b)
79 {
80     if (a->id_len != b->id_len || a->id_len > QUIC_MAX_CONN_ID_LEN)
81         return 0;
82     return memcmp(a->id, b->id, a->id_len) == 0;
83 }
84
85 #  define QUIC_MIN_INITIAL_DGRAM_LEN  1200
86
87 #  define QUIC_DEFAULT_ACK_DELAY_EXP  3
88 #  define QUIC_MAX_ACK_DELAY_EXP      20
89
90 #  define QUIC_DEFAULT_MAX_ACK_DELAY  25
91
92 #  define QUIC_MIN_ACTIVE_CONN_ID_LIMIT   2
93
94 /* Arbitrary choice of default idle timeout (not an RFC value). */
95 #  define QUIC_DEFAULT_IDLE_TIMEOUT   30000
96
97 #  define QUIC_STATELESS_RESET_TOKEN_LEN    16
98
99 # endif
100
101 #endif