Wipe (some) secrets from memory after use
[tinc] / src / sptps.h
1 #ifndef TINC_SPTPS_H
2 #define TINC_SPTPS_H
3
4 /*
5     sptps.h -- Simple Peer-to-Peer Security
6     Copyright (C) 2011-2014 Guus Sliepen <guus@tinc-vpn.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "system.h"
24
25 #include "chacha-poly1305/chacha-poly1305.h"
26 #include "ecdh.h"
27 #include "ecdsa.h"
28
29 #define SPTPS_VERSION 0
30
31 // Record types
32 #define SPTPS_HANDSHAKE 128   // Key exchange and authentication
33 #define SPTPS_ALERT 129       // Warning or error messages
34 #define SPTPS_CLOSE 130       // Application closed the connection
35
36 // Overhead for datagrams
37 #define SPTPS_DATAGRAM_OVERHEAD 21
38
39 typedef bool (*send_data_t)(void *handle, uint8_t type, const void *data, size_t len);
40 typedef bool (*receive_record_t)(void *handle, uint8_t type, const void *data, uint16_t len);
41
42 // Key exchange states
43 typedef enum sptps_state_t {
44         SPTPS_KEX = 1,           // Waiting for the first Key EXchange record
45         SPTPS_SECONDARY_KEX = 2, // Ready to receive a secondary Key EXchange record
46         SPTPS_SIG = 3,           // Waiting for a SIGnature record
47         SPTPS_ACK = 4,           // Waiting for an ACKnowledgement record
48 } sptps_state_t;
49
50 PACKED(struct sptps_kex_t {
51         uint8_t version;
52         uint8_t nonce[ECDH_SIZE];
53         uint8_t pubkey[ECDH_SIZE];
54 });
55
56 typedef struct sptps_kex_t sptps_kex_t;
57
58 STATIC_ASSERT(sizeof(sptps_kex_t) == 65, "sptps_kex_t has invalid size");
59
60 typedef union sptps_key_t {
61         struct {
62                 uint8_t key0[CHACHA_POLY1305_KEYLEN];
63                 uint8_t key1[CHACHA_POLY1305_KEYLEN];
64         };
65         uint8_t both[CHACHA_POLY1305_KEYLEN * 2];
66 } sptps_key_t;
67
68 STATIC_ASSERT(sizeof(sptps_key_t) == 128, "sptps_key_t has invalid size");
69
70 typedef struct sptps {
71         bool initiator;
72         bool datagram;
73         sptps_state_t state;
74
75         uint8_t *inbuf;
76         size_t buflen;
77         uint16_t reclen;
78
79         bool instate;
80         chacha_poly1305_ctx_t *incipher;
81         uint32_t inseqno;
82         uint32_t received;
83         unsigned int replaywin;
84         unsigned int farfuture;
85         uint8_t *late;
86
87         bool outstate;
88         chacha_poly1305_ctx_t *outcipher;
89         uint32_t outseqno;
90
91         ecdsa_t *mykey;
92         ecdsa_t *hiskey;
93         ecdh_t *ecdh;
94
95         sptps_kex_t *mykex;
96         sptps_kex_t *hiskex;
97         sptps_key_t *key;
98         uint8_t *label;
99         size_t labellen;
100
101         void *handle;
102         send_data_t send_data;
103         receive_record_t receive_record;
104 } sptps_t;
105
106 extern unsigned int sptps_replaywin;
107 extern void sptps_log_quiet(sptps_t *s, int s_errno, const char *format, va_list ap);
108 extern void sptps_log_stderr(sptps_t *s, int s_errno, const char *format, va_list ap);
109 extern void (*sptps_log)(sptps_t *s, int s_errno, const char *format, va_list ap);
110 extern 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);
111 extern bool sptps_stop(sptps_t *s);
112 extern bool sptps_send_record(sptps_t *s, uint8_t type, const void *data, uint16_t len);
113 extern size_t sptps_receive_data(sptps_t *s, const void *data, size_t len);
114 extern bool sptps_force_kex(sptps_t *s);
115 extern bool sptps_verify_datagram(sptps_t *s, const void *data, size_t len);
116
117 #endif