Add AES-256-GCM support to SPTPS.
[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 1
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 static const size_t SPTPS_OVERHEAD = 19;
38 static const size_t SPTPS_HEADER = 3;
39 static const size_t SPTPS_DATAGRAM_OVERHEAD = 21;
40 static const size_t SPTPS_DATAGRAM_HEADER = 5;
41
42 typedef bool (*send_data_t)(void *handle, uint8_t type, const void *data, size_t len);
43 typedef bool (*receive_record_t)(void *handle, uint8_t type, const void *data, uint16_t len);
44
45 // Key exchange states
46 typedef enum sptps_state_t {
47         SPTPS_KEX = 1,           // Waiting for the first Key EXchange record
48         SPTPS_SECONDARY_KEX = 2, // Ready to receive a secondary Key EXchange record
49         SPTPS_SIG = 3,           // Waiting for a SIGnature record
50         SPTPS_ACK = 4,           // Waiting for an ACKnowledgement record
51 } sptps_state_t;
52
53 PACKED(struct sptps_kex_t {
54         uint8_t version;
55         uint8_t preferred_suite;
56         uint16_t cipher_suites;
57         uint8_t nonce[ECDH_SIZE];
58         uint8_t pubkey[ECDH_SIZE];
59 });
60
61 typedef struct sptps_kex_t sptps_kex_t;
62
63 STATIC_ASSERT(sizeof(sptps_kex_t) == 68, "sptps_kex_t has invalid size");
64
65 typedef union sptps_key_t {
66         struct {
67                 uint8_t key0[CHACHA_POLY1305_KEYLEN];
68                 uint8_t key1[CHACHA_POLY1305_KEYLEN];
69         };
70         uint8_t both[CHACHA_POLY1305_KEYLEN * 2];
71 } sptps_key_t;
72
73 STATIC_ASSERT(sizeof(sptps_key_t) == 128, "sptps_key_t has invalid size");
74
75 // Public key suites
76 enum {
77         SPTPS_ED25519 = 0,
78 };
79
80 // Cipher suites
81 enum {
82         SPTPS_CHACHA_POLY1305 = 0,
83         SPTPS_AES256_GCM = 1,
84         SPTPS_ALL_CIPHER_SUITES = 0x3,
85 };
86
87 typedef struct sptps_params {
88         void *handle;
89         bool initiator;
90         bool datagram;
91         uint8_t preferred_suite;
92         uint16_t cipher_suites;
93         ecdsa_t *mykey;
94         ecdsa_t *hiskey;
95         const void *label;
96         size_t labellen;
97         send_data_t send_data;
98         receive_record_t receive_record;
99 } sptps_params_t;
100
101 typedef struct sptps {
102         bool initiator;
103         bool datagram;
104         uint8_t preferred_suite;
105         uint16_t cipher_suites;
106
107         uint8_t pk_suite;
108         uint8_t cipher_suite;
109         sptps_state_t state;
110
111         uint8_t *inbuf;
112         size_t buflen;
113         uint16_t reclen;
114
115         bool instate;
116         void *incipher;
117         uint32_t inseqno;
118         uint32_t received;
119         unsigned int replaywin;
120         unsigned int farfuture;
121         uint8_t *late;
122
123         bool outstate;
124         void *outcipher;
125         uint32_t outseqno;
126
127         ecdsa_t *mykey;
128         ecdsa_t *hiskey;
129         ecdh_t *ecdh;
130
131         sptps_kex_t *mykex;
132         sptps_kex_t *hiskex;
133         sptps_key_t *key;
134         uint8_t *label;
135         size_t labellen;
136
137         void *handle;
138         send_data_t send_data;
139         receive_record_t receive_record;
140 } sptps_t;
141
142 extern unsigned int sptps_replaywin;
143 extern void sptps_log_quiet(sptps_t *s, int s_errno, const char *format, va_list ap) ATTR_FORMAT(printf, 3, 0);
144 extern void sptps_log_stderr(sptps_t *s, int s_errno, const char *format, va_list ap) ATTR_FORMAT(printf, 3, 0);
145 extern void (*sptps_log)(sptps_t *s, int s_errno, const char *format, va_list ap) ATTR_FORMAT(printf, 3, 0);
146 extern bool sptps_start(sptps_t *s, const struct sptps_params *params);
147 extern bool sptps_stop(sptps_t *s);
148 extern bool sptps_send_record(sptps_t *s, uint8_t type, const void *data, uint16_t len);
149 extern size_t sptps_receive_data(sptps_t *s, const void *data, size_t len);
150 extern bool sptps_force_kex(sptps_t *s);
151 extern bool sptps_verify_datagram(sptps_t *s, const void *data, size_t len);
152
153 #endif