1 Simple Peer-to-Peer Security
2 ----------------------------
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.
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.
19 A record consists of these fields:
21 - uint32_t seqno (little endian)
22 - uint16_t length (little endian)
25 - opaque hmac[HMAC_SIZE] (HMAC over all preceding fields)
29 - The seqno field is never sent to the peer, but is included in the calculation
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.
38 - 0..127 represent application records. The meaning of the value is application
40 - 128 is a handshake record.
41 - 129..255 are reserved and never to be used for application records.
46 A record consists of these fields:
48 - uint16_t length (little endian)
49 - uint32_t seqno (little endian)
52 - opaque hmac[HMAC_SIZE] (HMAC over all preceding fields)
56 - The length field is never sent to the peer, but is included in the calculation
58 - The rest is the same as the stream record layer.
60 Authentication protocol
61 -----------------------
63 The authentication consists of an exchange of Key EXchange, SIGnature and
64 ACKnowledge messages, transmitted using type 128 records.
75 ...encrypt and HMAC using session keys from now on...
82 ...key renegotiation starts here...
91 ...encrypt and HMAC using new session keys from now on...
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
103 Key EXchange message:
105 - uint8_t kex_version (always 1 in this version of SPTPS)
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]
115 - opaque ecdsa_signature[ECDSA_SIZE]
119 - empty (only sent after key renegotiation)
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
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.
137 The signature is calculated over this string:
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]
144 The PRF is calculated as follows:
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.
152 The seed is as follows:
154 - const char[13] "key expansion"
155 - opaque responder_nonce[32]
156 - opaque initiator_nonce[32]
157 - opaque label[label_length]
159 The expanded key is used as follows:
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]
166 Where initiator_cipher_key is the key used by session initiator to encrypt
167 messages sent to the responder.
175 Symmetric cipher suites
176 -----------------------
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
182 0: Chacha20-Poly1305 (1)
185 Cipher suite selection
186 ----------------------
188 Public key suites are required to match on both sides. The symmetric suite is chosen as follows:
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.
198 - Document format of ECDH public key, ECDSA signature
199 - Refer to TLS RFCs where appropriate