2 sptps.c -- Simple Peer-to-Peer Security
3 Copyright (C) 2011-2015 Guus Sliepen <guus@tinc-vpn.org>,
4 2010 Brandon L. Black <blblack@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "chacha-poly1305/chacha-poly1305.h"
31 unsigned int sptps_replaywin = 16;
34 Nonce MUST be exchanged first (done)
35 Signatures MUST be done over both nonces, to guarantee the signature is fresh
36 Otherwise: if ECDHE key of one side is compromised, it can be reused!
38 Add explicit tag to beginning of structure to distinguish the client and server when signing. (done)
40 Sign all handshake messages up to ECDHE kex with long-term public keys. (done)
42 HMACed KEX finished message to prevent downgrade attacks and prove you have the right key material (done by virtue of Ed25519 over the whole ECDHE exchange?)
44 Explicit close message needs to be added.
46 Maybe do add some alert messages to give helpful error messages? Not more than TLS sends.
48 Use counter mode instead of OFB. (done)
50 Make sure ECC operations are fixed time (aka prevent side-channel attacks).
53 void sptps_log_quiet(sptps_t *s, int s_errno, const char *format, va_list ap) {
60 void sptps_log_stderr(sptps_t *s, int s_errno, const char *format, va_list ap) {
64 vfprintf(stderr, format, ap);
68 void (*sptps_log)(sptps_t *s, int s_errno, const char *format, va_list ap) = sptps_log_stderr;
70 // Log an error message.
71 static bool error(sptps_t *s, int s_errno, const char *format, ...) {
78 sptps_log(s, s_errno, format, ap);
86 static void warning(sptps_t *s, const char *format, ...) {
89 sptps_log(s, 0, format, ap);
93 // Send a record (datagram version, accepts all record types, handles encryption and authentication).
94 static bool send_record_priv_datagram(sptps_t *s, uint8_t type, const void *data, uint16_t len) {
95 uint8_t buffer[len + 21UL];
97 // Create header with sequence number, length and record type
98 uint32_t seqno = s->outseqno++;
99 uint32_t netseqno = ntohl(seqno);
101 memcpy(buffer, &netseqno, 4);
103 memcpy(buffer + 5, data, len);
106 // If first handshake has finished, encrypt and HMAC
107 chacha_poly1305_encrypt(s->outcipher, seqno, buffer + 4, len + 1, buffer + 4, NULL);
108 return s->send_data(s->handle, type, buffer, len + 21UL);
110 // Otherwise send as plaintext
111 return s->send_data(s->handle, type, buffer, len + 5UL);
114 // Send a record (private version, accepts all record types, handles encryption and authentication).
115 static bool send_record_priv(sptps_t *s, uint8_t type, const void *data, uint16_t len) {
117 return send_record_priv_datagram(s, type, data, len);
120 uint8_t buffer[len + 19UL];
122 // Create header with sequence number, length and record type
123 uint32_t seqno = s->outseqno++;
124 uint16_t netlen = htons(len);
126 memcpy(buffer, &netlen, 2);
128 memcpy(buffer + 3, data, len);
131 // If first handshake has finished, encrypt and HMAC
132 chacha_poly1305_encrypt(s->outcipher, seqno, buffer + 2, len + 1, buffer + 2, NULL);
133 return s->send_data(s->handle, type, buffer, len + 19UL);
135 // Otherwise send as plaintext
136 return s->send_data(s->handle, type, buffer, len + 3UL);
140 // Send an application record.
141 bool sptps_send_record(sptps_t *s, uint8_t type, const void *data, uint16_t len) {
142 // Sanity checks: application cannot send data before handshake is finished,
143 // and only record types 0..127 are allowed.
145 return error(s, EINVAL, "Handshake phase not finished yet");
148 if(type >= SPTPS_HANDSHAKE) {
149 return error(s, EINVAL, "Invalid application record type");
152 return send_record_priv(s, type, data, len);
155 // Send a Key EXchange record, containing a random nonce and an ECDHE public key.
156 static bool send_kex(sptps_t *s) {
157 size_t keylen = ECDH_SIZE;
159 // Make room for our KEX message, which we will keep around since send_sig() needs it.
164 s->mykex = realloc(s->mykex, 1 + 32 + keylen);
167 return error(s, errno, strerror(errno));
170 // Set version byte to zero.
171 s->mykex[0] = SPTPS_VERSION;
173 // Create a random nonce.
174 randomize(s->mykex + 1, 32);
176 // Create a new ECDH public key.
177 if(!(s->ecdh = ecdh_generate_public(s->mykex + 1 + 32))) {
178 return error(s, EINVAL, "Failed to generate ECDH public key");
181 return send_record_priv(s, SPTPS_HANDSHAKE, s->mykex, 1 + 32 + keylen);
184 // Send a SIGnature record, containing an Ed25519 signature over both KEX records.
185 static bool send_sig(sptps_t *s) {
186 size_t keylen = ECDH_SIZE;
187 size_t siglen = ecdsa_size(s->mykey);
189 // Concatenate both KEX messages, plus tag indicating if it is from the connection originator, plus label
190 uint8_t msg[(1 + 32 + keylen) * 2 + 1 + s->labellen];
193 msg[0] = s->initiator;
194 memcpy(msg + 1, s->mykex, 1 + 32 + keylen);
195 memcpy(msg + 1 + 33 + keylen, s->hiskex, 1 + 32 + keylen);
196 memcpy(msg + 1 + 2 * (33 + keylen), s->label, s->labellen);
199 if(!ecdsa_sign(s->mykey, msg, sizeof(msg), sig)) {
200 return error(s, EINVAL, "Failed to sign SIG record");
203 // Send the SIG exchange record.
204 return send_record_priv(s, SPTPS_HANDSHAKE, sig, sizeof(sig));
207 // Generate key material from the shared secret created from the ECDHE key exchange.
208 static bool generate_key_material(sptps_t *s, const uint8_t *shared, size_t len) {
209 // Initialise cipher and digest structures if necessary
211 s->incipher = chacha_poly1305_init();
212 s->outcipher = chacha_poly1305_init();
214 if(!s->incipher || !s->outcipher) {
215 return error(s, EINVAL, "Failed to open cipher");
219 // Allocate memory for key material
220 size_t keylen = 2 * CHACHA_POLY1305_KEYLEN;
222 s->key = realloc(s->key, keylen);
225 return error(s, errno, strerror(errno));
228 // Create the HMAC seed, which is "key expansion" + session label + server nonce + client nonce
229 uint8_t seed[s->labellen + 64 + 13];
230 memcpy(seed, "key expansion", 13);
233 memcpy(seed + 13, s->mykex + 1, 32);
234 memcpy(seed + 45, s->hiskex + 1, 32);
236 memcpy(seed + 13, s->hiskex + 1, 32);
237 memcpy(seed + 45, s->mykex + 1, 32);
240 memcpy(seed + 77, s->label, s->labellen);
242 // Use PRF to generate the key material
243 if(!prf(shared, len, seed, s->labellen + 64 + 13, s->key, keylen)) {
244 return error(s, EINVAL, "Failed to generate key material");
250 // Send an ACKnowledgement record.
251 static bool send_ack(sptps_t *s) {
252 return send_record_priv(s, SPTPS_HANDSHAKE, "", 0);
255 // Receive an ACKnowledgement record.
256 static bool receive_ack(sptps_t *s, const uint8_t *data, uint16_t len) {
260 return error(s, EIO, "Invalid ACK record length");
264 if(!chacha_poly1305_set_key(s->incipher, s->key)) {
265 return error(s, EINVAL, "Failed to set counter");
268 if(!chacha_poly1305_set_key(s->incipher, s->key + CHACHA_POLY1305_KEYLEN)) {
269 return error(s, EINVAL, "Failed to set counter");
280 // Receive a Key EXchange record, respond by sending a SIG record.
281 static bool receive_kex(sptps_t *s, const uint8_t *data, uint16_t len) {
282 // Verify length of the HELLO record
283 if(len != 1 + 32 + ECDH_SIZE) {
284 return error(s, EIO, "Invalid KEX record length");
287 // Ignore version number for now.
289 // Make a copy of the KEX message, send_sig() and receive_sig() need it
291 return error(s, EINVAL, "Received a second KEX message before first has been processed");
294 s->hiskex = realloc(s->hiskex, len);
297 return error(s, errno, strerror(errno));
300 memcpy(s->hiskex, data, len);
309 // Receive a SIGnature record, verify it, if it passed, compute the shared secret and calculate the session keys.
310 static bool receive_sig(sptps_t *s, const uint8_t *data, uint16_t len) {
311 size_t keylen = ECDH_SIZE;
312 size_t siglen = ecdsa_size(s->hiskey);
314 // Verify length of KEX record.
316 return error(s, EIO, "Invalid KEX record length");
319 // Concatenate both KEX messages, plus tag indicating if it is from the connection originator
320 uint8_t msg[(1 + 32 + keylen) * 2 + 1 + s->labellen];
322 msg[0] = !s->initiator;
323 memcpy(msg + 1, s->hiskex, 1 + 32 + keylen);
324 memcpy(msg + 1 + 33 + keylen, s->mykex, 1 + 32 + keylen);
325 memcpy(msg + 1 + 2 * (33 + keylen), s->label, s->labellen);
328 if(!ecdsa_verify(s->hiskey, msg, sizeof(msg), data)) {
329 return error(s, EIO, "Failed to verify SIG record");
332 // Compute shared secret.
333 uint8_t shared[ECDH_SHARED_SIZE];
335 if(!ecdh_compute_shared(s->ecdh, s->hiskex + 1 + 32, shared)) {
336 return error(s, EINVAL, "Failed to compute ECDH shared secret");
341 // Generate key material from shared secret.
342 if(!generate_key_material(s, shared, sizeof(shared))) {
346 if(!s->initiator && !send_sig(s)) {
356 // Send cipher change record
357 if(s->outstate && !send_ack(s)) {
361 // TODO: only set new keys after ACK has been set/received
363 if(!chacha_poly1305_set_key(s->outcipher, s->key + CHACHA_POLY1305_KEYLEN)) {
364 return error(s, EINVAL, "Failed to set key");
367 if(!chacha_poly1305_set_key(s->outcipher, s->key)) {
368 return error(s, EINVAL, "Failed to set key");
375 // Force another Key EXchange (for testing purposes).
376 bool sptps_force_kex(sptps_t *s) {
377 if(!s->outstate || s->state != SPTPS_SECONDARY_KEX) {
378 return error(s, EINVAL, "Cannot force KEX in current state");
381 s->state = SPTPS_KEX;
385 // Receive a handshake record.
386 static bool receive_handshake(sptps_t *s, const uint8_t *data, uint16_t len) {
387 // Only a few states to deal with handshaking.
389 case SPTPS_SECONDARY_KEX:
391 // We receive a secondary KEX request, first respond by sending our own.
399 // We have sent our KEX request, we expect our peer to sent one as well.
400 if(!receive_kex(s, data, len)) {
404 s->state = SPTPS_SIG;
409 // If we already sent our secondary public ECDH key, we expect the peer to send his.
410 if(!receive_sig(s, data, len)) {
415 s->state = SPTPS_ACK;
419 if(!receive_ack(s, NULL, 0)) {
423 s->receive_record(s->handle, SPTPS_HANDSHAKE, NULL, 0);
424 s->state = SPTPS_SECONDARY_KEX;
431 // We expect a handshake message to indicate transition to the new keys.
432 if(!receive_ack(s, data, len)) {
436 s->receive_record(s->handle, SPTPS_HANDSHAKE, NULL, 0);
437 s->state = SPTPS_SECONDARY_KEX;
440 // TODO: split ACK into a VERify and ACK?
442 return error(s, EIO, "Invalid session state %d", s->state);
446 static bool sptps_check_seqno(sptps_t *s, uint32_t seqno, bool update_state) {
447 // Replay protection using a sliding window of configurable size.
448 // s->inseqno is expected sequence number
449 // seqno is received sequence number
450 // s->late[] is a circular buffer, a 1 bit means a packet has not been received yet
451 // The circular buffer contains bits for sequence numbers from s->inseqno - s->replaywin * 8 to (but excluding) s->inseqno.
453 if(seqno != s->inseqno) {
454 if(seqno >= s->inseqno + s->replaywin * 8) {
455 // Prevent packets that jump far ahead of the queue from causing many others to be dropped.
456 bool farfuture = s->farfuture < s->replaywin >> 2;
463 return update_state ? error(s, EIO, "Packet is %d seqs in the future, dropped (%u)\n", seqno - s->inseqno, s->farfuture) : false;
466 // Unless we have seen lots of them, in which case we consider the others lost.
468 warning(s, "Lost %d packets\n", seqno - s->inseqno);
472 // Mark all packets in the replay window as being late.
473 memset(s->late, 255, s->replaywin);
475 } else if(seqno < s->inseqno) {
476 // If the sequence number is farther in the past than the bitmap goes, or if the packet was already received, drop it.
477 if((s->inseqno >= s->replaywin * 8 && seqno < s->inseqno - s->replaywin * 8) || !(s->late[(seqno / 8) % s->replaywin] & (1 << seqno % 8))) {
478 return update_state ? error(s, EIO, "Received late or replayed packet, seqno %d, last received %d\n", seqno, s->inseqno) : false;
480 } else if(update_state) {
481 // We missed some packets. Mark them in the bitmap as being late.
482 for(uint32_t i = s->inseqno; i < seqno; i++) {
483 s->late[(i / 8) % s->replaywin] |= 1 << i % 8;
489 // Mark the current packet as not being late.
490 s->late[(seqno / 8) % s->replaywin] &= ~(1 << seqno % 8);
496 if(seqno >= s->inseqno) {
497 s->inseqno = seqno + 1;
510 // Check datagram for valid HMAC
511 bool sptps_verify_datagram(sptps_t *s, const void *vdata, size_t len) {
512 if(!s->instate || len < 21) {
513 return error(s, EIO, "Received short packet");
516 const uint8_t *data = vdata;
518 memcpy(&seqno, data, 4);
519 seqno = ntohl(seqno);
521 if(!sptps_check_seqno(s, seqno, false)) {
527 return chacha_poly1305_decrypt(s->incipher, seqno, data + 4, len - 4, buffer, &outlen);
530 // Receive incoming data, datagram version.
531 static bool sptps_receive_data_datagram(sptps_t *s, const uint8_t *data, size_t len) {
532 if(len < (s->instate ? 21 : 5)) {
533 return error(s, EIO, "Received short packet");
537 memcpy(&seqno, data, 4);
538 seqno = ntohl(seqno);
543 if(seqno != s->inseqno) {
544 return error(s, EIO, "Invalid packet seqno: %d != %d", seqno, s->inseqno);
547 s->inseqno = seqno + 1;
549 uint8_t type = *(data++);
552 if(type != SPTPS_HANDSHAKE) {
553 return error(s, EIO, "Application record received before handshake finished");
556 return receive_handshake(s, data, len);
564 if(!chacha_poly1305_decrypt(s->incipher, seqno, data, len, buffer, &outlen)) {
565 return error(s, EIO, "Failed to decrypt and verify packet");
568 if(!sptps_check_seqno(s, seqno, true)) {
572 // Append a NULL byte for safety.
578 uint8_t type = *(data++);
581 if(type < SPTPS_HANDSHAKE) {
583 return error(s, EIO, "Application record received before handshake finished");
586 if(!s->receive_record(s->handle, type, data, len)) {
589 } else if(type == SPTPS_HANDSHAKE) {
590 if(!receive_handshake(s, data, len)) {
594 return error(s, EIO, "Invalid record type %d", type);
600 // Receive incoming data. Check if it contains a complete record, if so, handle it.
601 size_t sptps_receive_data(sptps_t *s, const void *vdata, size_t len) {
602 const uint8_t *data = vdata;
603 size_t total_read = 0;
606 return error(s, EIO, "Invalid session state zero");
610 return sptps_receive_data_datagram(s, data, len) ? len : false;
613 // First read the 2 length bytes.
615 size_t toread = 2 - s->buflen;
621 memcpy(s->inbuf + s->buflen, data, toread);
623 total_read += toread;
628 // Exit early if we don't have the full length.
633 // Get the length bytes
635 memcpy(&s->reclen, s->inbuf, 2);
636 s->reclen = ntohs(s->reclen);
638 // If we have the length bytes, ensure our buffer can hold the whole request.
639 s->inbuf = realloc(s->inbuf, s->reclen + 19UL);
642 return error(s, errno, strerror(errno));
645 // Exit early if we have no more data to process.
651 // Read up to the end of the record.
652 size_t toread = s->reclen + (s->instate ? 19UL : 3UL) - s->buflen;
658 memcpy(s->inbuf + s->buflen, data, toread);
659 total_read += toread;
662 // If we don't have a whole record, exit.
663 if(s->buflen < s->reclen + (s->instate ? 19UL : 3UL)) {
667 // Update sequence number.
669 uint32_t seqno = s->inseqno++;
671 // Check HMAC and decrypt.
673 if(!chacha_poly1305_decrypt(s->incipher, seqno, s->inbuf + 2UL, s->reclen + 17UL, s->inbuf + 2UL, NULL)) {
674 return error(s, EINVAL, "Failed to decrypt and verify record");
678 // Append a NULL byte for safety.
679 s->inbuf[s->reclen + 3UL] = 0;
681 uint8_t type = s->inbuf[2];
683 if(type < SPTPS_HANDSHAKE) {
685 return error(s, EIO, "Application record received before handshake finished");
688 if(!s->receive_record(s->handle, type, s->inbuf + 3, s->reclen)) {
691 } else if(type == SPTPS_HANDSHAKE) {
692 if(!receive_handshake(s, s->inbuf + 3, s->reclen)) {
696 return error(s, EIO, "Invalid record type %d", type);
704 // Start a SPTPS session.
705 bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_t *mykey, ecdsa_t *hiskey, const void *label, size_t labellen, send_data_t send_data, receive_record_t receive_record) {
706 // Initialise struct sptps
707 memset(s, 0, sizeof(*s));
710 s->initiator = initiator;
711 s->datagram = datagram;
714 s->replaywin = sptps_replaywin;
717 s->late = malloc(s->replaywin);
720 return error(s, errno, strerror(errno));
723 memset(s->late, 0, s->replaywin);
726 s->label = malloc(labellen);
729 return error(s, errno, strerror(errno));
733 s->inbuf = malloc(7);
736 return error(s, errno, strerror(errno));
742 memcpy(s->label, label, labellen);
743 s->labellen = labellen;
745 s->send_data = send_data;
746 s->receive_record = receive_record;
748 // Do first KEX immediately
749 s->state = SPTPS_KEX;
753 // Stop a SPTPS session.
754 bool sptps_stop(sptps_t *s) {
755 // Clean up any resources.
756 chacha_poly1305_exit(s->incipher);
757 chacha_poly1305_exit(s->outcipher);
765 memset(s, 0, sizeof(*s));