Wipe (some) secrets from memory after use
[tinc] / src / gcrypt / cipher.c
1 /*
2     cipher.c -- Symmetric block cipher handling
3     Copyright (C) 2007-2022 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "../system.h"
21
22 #include "cipher.h"
23 #include "../cipher.h"
24 #include "../logger.h"
25 #include "../xalloc.h"
26
27 typedef enum gcry_cipher_algos cipher_algo_t;
28 typedef enum gcry_cipher_modes cipher_mode_t;
29
30 static struct {
31         const char *name;
32         cipher_algo_t algo;
33         cipher_mode_t mode;
34         nid_t nid;
35 } ciphertable[] = {
36         {"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
37
38         {NULL,       GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 92},
39         {"blowfish", GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC, 91},
40         {NULL,       GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CFB, 93},
41         {NULL,       GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB, 94},
42
43         {"aes-128-ecb", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 418},
44         {"aes-128-cbc", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 419},
45         {"aes-128-cfb", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 421},
46         {"aes-128-ofb", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_OFB, 420},
47
48         {"aes-192-ecb", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_ECB, 422},
49         {"aes-192-cbc", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC, 423},
50         {"aes-192-cfb", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CFB, 425},
51         {"aes-192-ofb", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_OFB, 424},
52
53         {"aes-256-ecb", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 426},
54         {"aes-256-cbc", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 427},
55         {"aes-256-cfb", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, 429},
56         {"aes-256-ofb", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, 428},
57 };
58
59 static bool nametocipher(const char *name, cipher_algo_t *algo, cipher_mode_t *mode) {
60         for(size_t i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
61                 if(ciphertable[i].name && !strcasecmp(name, ciphertable[i].name)) {
62                         *algo = ciphertable[i].algo;
63                         *mode = ciphertable[i].mode;
64                         return true;
65                 }
66         }
67
68         return false;
69 }
70
71 static bool nidtocipher(cipher_algo_t *algo, cipher_mode_t *mode, nid_t nid) {
72         for(size_t i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
73                 if(nid == ciphertable[i].nid) {
74                         *algo = ciphertable[i].algo;
75                         *mode = ciphertable[i].mode;
76                         return true;
77                 }
78         }
79
80         return false;
81 }
82
83 static bool ciphertonid(nid_t *nid, cipher_algo_t algo, cipher_mode_t mode) {
84         for(size_t i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
85                 if(algo == ciphertable[i].algo && mode == ciphertable[i].mode) {
86                         *nid = ciphertable[i].nid;
87                         return true;
88                 }
89         }
90
91         return false;
92 }
93
94 static bool cipher_open(cipher_t *cipher, cipher_algo_t algo, cipher_mode_t mode) {
95         gcry_error_t err;
96
97         if(!ciphertonid(&cipher->nid, algo, mode)) {
98                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Cipher %d mode %d has no corresponding nid!", algo, mode);
99                 return false;
100         }
101
102         if((err = gcry_cipher_open(&cipher->handle, algo, mode, 0))) {
103                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unable to initialise cipher %d mode %d: %s", algo, mode, gcry_strerror(err));
104                 return false;
105         }
106
107         cipher->keylen = gcry_cipher_get_algo_keylen(algo);
108         cipher->blklen = gcry_cipher_get_algo_blklen(algo);
109         cipher->key = xmalloc(cipher_keylength(cipher));
110         cipher->padding = mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC;
111
112         return true;
113 }
114
115 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
116         cipher_algo_t algo;
117         cipher_mode_t mode;
118
119         if(!nametocipher(name, &algo, &mode)) {
120                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown cipher name '%s'!", name);
121                 return false;
122         }
123
124         return cipher_open(cipher, algo, mode);
125 }
126
127 bool cipher_open_by_nid(cipher_t *cipher, nid_t nid) {
128         cipher_algo_t algo;
129         cipher_mode_t mode;
130
131         if(!nidtocipher(&algo, &mode, nid)) {
132                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown cipher ID %d!", nid);
133                 return false;
134         }
135
136         return cipher_open(cipher, algo, mode);
137 }
138
139 void cipher_close(cipher_t *cipher) {
140         if(!cipher) {
141                 return;
142         }
143
144         if(cipher->handle) {
145                 gcry_cipher_close(cipher->handle);
146         }
147
148         xzfree(cipher->key, cipher_keylength(cipher));
149         memset(cipher, 0, sizeof(*cipher));
150 }
151
152 size_t cipher_keylength(const cipher_t *cipher) {
153         if(!cipher) {
154                 return 0;
155         }
156
157         return cipher->keylen + cipher->blklen;
158 }
159
160 uint64_t cipher_budget(const cipher_t *cipher) {
161         if(!cipher) {
162                 return UINT64_MAX; // NULL cipher
163         }
164
165         size_t ivlen = cipher->blklen;
166         size_t blklen = cipher->blklen;
167
168         size_t len = blklen > 1
169                      ? blklen
170                      : ivlen > 1 ? ivlen : 8;
171         size_t bits = len * 4 - 1;
172
173         return bits < 64
174                ? UINT64_C(1) << bits
175                : UINT64_MAX;
176 }
177
178 size_t cipher_blocksize(const cipher_t *cipher) {
179         if(!cipher || !cipher->blklen) {
180                 return 1;
181         }
182
183         return cipher->blklen;
184 }
185
186 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
187         (void)encrypt;
188
189         memcpy(cipher->key, key, cipher_keylength(cipher));
190
191         gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
192         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
193
194         return true;
195 }
196
197 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
198         (void)encrypt;
199
200         memcpy(cipher->key, (char *)key + len - cipher->keylen, cipher->keylen);
201         gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
202
203         memcpy((char *)cipher->key + cipher->keylen, (char *)key + len - cipher->blklen - cipher->keylen, cipher->blklen);
204         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
205
206         return true;
207 }
208
209 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
210         gcry_error_t err;
211         uint8_t pad[cipher->blklen];
212
213         if(cipher->padding) {
214                 if(!oneshot) {
215                         return false;
216                 }
217
218                 size_t reqlen = ((inlen + cipher->blklen) / cipher->blklen) * cipher->blklen;
219
220                 if(*outlen < reqlen) {
221                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: not enough room for padding");
222                         return false;
223                 }
224
225                 uint8_t padbyte = reqlen - inlen;
226                 inlen = reqlen - cipher->blklen;
227
228                 for(int i = 0; i < cipher->blklen; i++)
229                         if(i < cipher->blklen - padbyte) {
230                                 pad[i] = ((uint8_t *)indata)[inlen + i];
231                         } else {
232                                 pad[i] = padbyte;
233                         }
234         }
235
236         if(oneshot) {
237                 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
238         }
239
240         if((err = gcry_cipher_encrypt(cipher->handle, outdata, *outlen, indata, inlen))) {
241                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", gcry_strerror(err));
242                 return false;
243         }
244
245         if(cipher->padding) {
246                 if((err = gcry_cipher_encrypt(cipher->handle, (char *)outdata + inlen, cipher->blklen, pad, cipher->blklen))) {
247                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", gcry_strerror(err));
248                         return false;
249                 }
250
251                 inlen += cipher->blklen;
252         }
253
254         *outlen = inlen;
255         return true;
256 }
257
258 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
259         gcry_error_t err;
260
261         if(oneshot) {
262                 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
263         }
264
265         if((err = gcry_cipher_decrypt(cipher->handle, outdata, *outlen, indata, inlen))) {
266                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", gcry_strerror(err));
267                 return false;
268         }
269
270         if(cipher->padding) {
271                 if(!oneshot) {
272                         return false;
273                 }
274
275                 uint8_t padbyte = ((uint8_t *)outdata)[inlen - 1];
276
277                 if(padbyte == 0 || padbyte > cipher->blklen || padbyte > inlen) {
278                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: invalid padding");
279                         return false;
280                 }
281
282                 size_t origlen = inlen - padbyte;
283
284                 for(size_t i = inlen - 1; i >= origlen; i--)
285                         if(((uint8_t *)outdata)[i] != padbyte) {
286                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: invalid padding");
287                                 return false;
288                         }
289
290                 *outlen = origlen;
291         } else {
292                 *outlen = inlen;
293         }
294
295         return true;
296 }
297
298 nid_t cipher_get_nid(const cipher_t *cipher) {
299         if(!cipher || !cipher->nid) {
300                 return 0;
301         }
302
303         return cipher->nid;
304 }
305
306 bool cipher_active(const cipher_t *cipher) {
307         return cipher->nid != 0;
308 }