Add AES-256-GCM support to SPTPS.
[tinc] / doc / SPTPS
1 Simple Peer-to-Peer Security
2 ----------------------------
3
4 SPTPS is a protocol that, like TLS, aims to provide a secure transport layer
5 for applications. However, it is specifically aimed at peer-to-peer
6 applications. Specifically, peers have each other's credentials beforehand,
7 they need not negotiate certificates. Also, the security parameters of the
8 application is also known beforehand, so they need not negotiate cipher suites.
9 Only one cipher suite is available, and only one authentication method is used.
10 This not only greatly simplifies the protocol, it also gets rid of an entire
11 class of attacks and possible programming mistakes.
12
13 SPTPS can be used both on top of reliable stream protocols such as TCP or on
14 top of datagram protocols such as UDP.
15
16 Stream record layer
17 -------------------
18
19 A record consists of these fields:
20
21 - uint32_t seqno (little endian)
22 - uint16_t length (little endian)
23 - uint8_t type
24 - opaque data[length]
25 - opaque hmac[HMAC_SIZE] (HMAC over all preceding fields)
26
27 Remarks:
28
29 - The seqno field is never sent to the peer, but is included in the calculation
30   of the HMAC.
31 - At the start of the session, the HMAC field does not appear until after the
32   SIGnature records have been exchanged.
33 - After the authentication phase, the type and data fields are encrypted before
34   the HMAC is calculated.
35
36 Message type:
37
38 - 0..127 represent application records. The meaning of the value is application
39   specific.
40 - 128 is a handshake record.
41 - 129..255 are reserved and never to be used for application records.
42
43 Datagram record layer
44 ---------------------
45
46 A record consists of these fields:
47
48 - uint16_t length (little endian)
49 - uint32_t seqno (little endian)
50 - uint8_t type
51 - opaque data[length]
52 - opaque hmac[HMAC_SIZE] (HMAC over all preceding fields)
53
54 Remarks:
55
56 - The length field is never sent to the peer, but is included in the calculation
57   of the HMAC.
58 - The rest is the same as the stream record layer.
59
60 Authentication protocol
61 -----------------------
62
63 The authentication consists of an exchange of Key EXchange, SIGnature and
64 ACKnowledge messages, transmitted using type 128 records.
65
66 Overview:
67
68 Initiator   Responder
69 ---------------------
70 KEX ->
71             <- KEX
72 SIG ->
73             <- SIG
74
75 ...encrypt and HMAC using session keys from now on...
76
77 App ->
78             <- App
79 ...
80             ...
81
82 ...key renegotiation starts here...
83
84 KEX ->
85             <- KEX
86 SIG ->
87             <- SIG
88 ACK ->
89             <- ACK
90
91 ...encrypt and HMAC using new session keys from now on...
92
93 App ->
94             <- App
95 ...
96             ...
97 ---------------------
98
99 Note that the responder does not need to wait before it receives the first KEX
100 message, it can immediately send its own once it has accepted an incoming
101 connection.
102
103 Key EXchange message:
104
105 - uint8_t kex_version (always 1 in this version of SPTPS)
106 - uint8_t
107   - high 4 bits: public key algorithm
108   - low 4 bits: preferred cipher suite
109 - uint16_t bitmask of cipher suites supported
110 - opaque nonce[32] (random number)
111 - opaque ecdh_key[ECDH_SIZE]
112
113 SIGnature message:
114
115 - opaque ecdsa_signature[ECDSA_SIZE]
116
117 ACKnowledge message:
118
119 - empty (only sent after key renegotiation)
120
121 Remarks:
122
123 - At the start, both peers generate a random nonce and an Elliptic Curve public
124   key and send it to the other in the KEX message.
125 - After receiving the other's KEX message, both KEX messages are concatenated
126   (see below), and the result is signed using ECDSA. The result is sent to the
127   other.
128 - After receiving the other's SIG message, the signature is verified. If it is
129   correct, the shared secret is calculated from the public keys exchanged in the
130   KEX message using the Elliptic Curve Diffie-Helman algorithm.
131 - The shared secret key is expanded using a PRF. Both nonces and the application
132   specific label are also used as input for the PRF.
133 - An ACK message is sent only when doing key renegotiation, and is sent using
134   the old encryption keys.
135 - The expanded key is used to key the encryption and HMAC algorithms.
136
137 The signature is calculated over this string:
138
139 - uint8_t initiator (0 = local peer, 1 = remote peer is initiator)
140 - opaque remote_kex_message[1 + 32 + ECDH_SIZE]
141 - opaque local_kex_message[1 + 32 + ECDH_SIZE]
142 - opaque label[label_length]
143
144 The PRF is calculated as follows:
145
146 - A HMAC using SHA512 is used, the shared secret is used as the key.
147 - For each block of 64 bytes, a HMAC is calculated. For block n: hmac[n] =
148   HMAC_SHA512(hmac[n - 1] + seed)
149 - For the first block (n = 1), hmac[0] is given by HMAC_SHA512(zeroes + seed),
150   where zeroes is a block of 64 zero bytes.
151
152 The seed is as follows:
153
154 - const char[13] "key expansion"
155 - opaque responder_nonce[32]
156 - opaque initiator_nonce[32]
157 - opaque label[label_length]
158
159 The expanded key is used as follows:
160
161 - opaque responder_cipher_key[CIPHER_KEYSIZE]
162 - opaque responder_digest_key[DIGEST_KEYSIZE]
163 - opaque initiator_cipher_key[CIPHER_KEYSIZE]
164 - opaque initiator_digest_key[DIGEST_KEYSIZE]
165
166 Where initiator_cipher_key is the key used by session initiator to encrypt
167 messages sent to the responder.
168
169 Public key suites
170 -----------------
171
172 0: Ed25519 + SHA512
173 1: Ed448 + SHAKE256?
174
175 Symmetric cipher suites
176 -----------------------
177
178 Value in parentheses is the static priority used to break ties in cipher suite
179 negotiation. We favor those algorithms that run faster without hardware
180 acceleration.
181
182 0: Chacha20-Poly1305 (1)
183 1: AES256-GCM        (0)
184
185 Cipher suite selection
186 ----------------------
187
188 Public key suites are required to match on both sides. The symmetric suite is chosen as follows:
189
190 1. AND the supported cipher suite bitmasks
191 2. If both preferred cipher suites are possible, choose the one with the highest static priority.
192 3. If only one is possible, choose that one.
193 4. If none is possible, choose the suite from the resulting bitmask that has the highest static priority.
194
195 TODO:
196 -----
197
198 - Document format of ECDH public key, ECDSA signature
199 - Refer to TLS RFCs where appropriate