Fix spelling errors.
[tinc] / src / gcrypt / cipher.c
1 /*
2     cipher.c -- Symmetric block cipher handling
3     Copyright (C) 2007-2012 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 "logger.h"
24 #include "xalloc.h"
25
26 static struct {
27         const char *name;
28         int algo;
29         int mode;
30         int nid;
31 } ciphertable[] = {
32         {"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
33
34         {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 92},
35         {"blowfish", GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC, 91},
36         {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CFB, 93},
37         {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB, 94},
38
39         {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 418},
40         {"aes", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 419},
41         {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 421},
42         {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_OFB, 420},
43
44         {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_ECB, 422},
45         {"aes192", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC, 423},
46         {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CFB, 425},
47         {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_OFB, 424},
48
49         {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 426},
50         {"aes256", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 427},
51         {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, 429},
52         {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, 428},
53 };
54
55 static bool nametocipher(const char *name, int *algo, int *mode) {
56         size_t i;
57
58         for(i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
59                 if(ciphertable[i].name && !strcasecmp(name, ciphertable[i].name)) {
60                         *algo = ciphertable[i].algo;
61                         *mode = ciphertable[i].mode;
62                         return true;
63                 }
64         }
65
66         return false;
67 }
68
69 static bool nidtocipher(int nid, int *algo, int *mode) {
70         size_t i;
71
72         for(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(int algo, int mode, int *nid) {
84         size_t i;
85
86         for(i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
87                 if(algo == ciphertable[i].algo && mode == ciphertable[i].mode) {
88                         *nid = ciphertable[i].nid;
89                         return true;
90                 }
91         }
92
93         return false;
94 }
95
96 static bool cipher_open(cipher_t *cipher, int algo, int mode) {
97         gcry_error_t err;
98
99         if(!ciphertonid(algo, mode, &cipher->nid)) {
100                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Cipher %d mode %d has no corresponding nid!", algo, mode);
101                 return false;
102         }
103
104         if((err = gcry_cipher_open(&cipher->handle, algo, mode, 0))) {
105                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unable to initialise cipher %d mode %d: %s", algo, mode, gcry_strerror(err));
106                 return false;
107         }
108
109         cipher->keylen = gcry_cipher_get_algo_keylen(algo);
110         cipher->blklen = gcry_cipher_get_algo_blklen(algo);
111         cipher->key = xmalloc(cipher->keylen + cipher->blklen);
112         cipher->padding = mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC;
113
114         return true;
115 }
116
117 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
118         int algo, mode;
119
120         if(!nametocipher(name, &algo, &mode)) {
121                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown cipher name '%s'!", name);
122                 return false;
123         }
124
125         return cipher_open(cipher, algo, mode);
126 }
127
128 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
129         int algo, mode;
130
131         if(!nidtocipher(nid, &algo, &mode)) {
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 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
140         return cipher_open(cipher, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB);
141 }
142
143 void cipher_close(cipher_t *cipher) {
144         if(cipher->handle) {
145                 gcry_cipher_close(cipher->handle);
146                 cipher->handle = NULL;
147         }
148
149         if(cipher->key) {
150                 free(cipher->key);
151                 cipher->key = NULL;
152         }
153 }
154
155 size_t cipher_keylength(const cipher_t *cipher) {
156         return cipher->keylen + cipher->blklen;
157 }
158
159 void cipher_get_key(const cipher_t *cipher, void *key) {
160         memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
161 }
162
163 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
164         memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
165
166         gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
167         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
168
169         return true;
170 }
171
172 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
173         memcpy(cipher->key, key + len - cipher->keylen, cipher->keylen + cipher->blklen);
174         memcpy(cipher->key + cipher->keylen, key + len - cipher->keylen - cipher->blklen, cipher->blklen);
175
176         gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
177         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
178
179         return true;
180 }
181
182 bool cipher_regenerate_key(cipher_t *cipher, bool encrypt) {
183         gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
184
185         gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
186         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
187
188         return true;
189 }
190
191 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
192         gcry_error_t err;
193         uint8_t pad[cipher->blklen];
194
195         if(cipher->padding) {
196                 if(!oneshot) {
197                         return false;
198                 }
199
200                 size_t reqlen = ((inlen + cipher->blklen) / cipher->blklen) * cipher->blklen;
201
202                 if(*outlen < reqlen) {
203                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: not enough room for padding");
204                         return false;
205                 }
206
207                 uint8_t padbyte = reqlen - inlen;
208                 inlen = reqlen - cipher->blklen;
209
210                 for(int i = 0; i < cipher->blklen; i++)
211                         if(i < cipher->blklen - padbyte) {
212                                 pad[i] = ((uint8_t *)indata)[inlen + i];
213                         } else {
214                                 pad[i] = padbyte;
215                         }
216         }
217
218         if(oneshot) {
219                 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
220         }
221
222         if((err = gcry_cipher_encrypt(cipher->handle, outdata, *outlen, indata, inlen))) {
223                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", gcry_strerror(err));
224                 return false;
225         }
226
227         if(cipher->padding) {
228                 if((err = gcry_cipher_encrypt(cipher->handle, outdata + inlen, cipher->blklen, pad, cipher->blklen))) {
229                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", gcry_strerror(err));
230                         return false;
231                 }
232
233                 inlen += cipher->blklen;
234         }
235
236         *outlen = inlen;
237         return true;
238 }
239
240 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
241         gcry_error_t err;
242
243         if(oneshot) {
244                 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
245         }
246
247         if((err = gcry_cipher_decrypt(cipher->handle, outdata, *outlen, indata, inlen))) {
248                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", gcry_strerror(err));
249                 return false;
250         }
251
252         if(cipher->padding) {
253                 if(!oneshot) {
254                         return false;
255                 }
256
257                 uint8_t padbyte = ((uint8_t *)outdata)[inlen - 1];
258
259                 if(padbyte == 0 || padbyte > cipher->blklen || padbyte > inlen) {
260                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: invalid padding");
261                         return false;
262                 }
263
264                 size_t origlen = inlen - padbyte;
265
266                 for(int i = inlen - 1; i >= origlen; i--)
267                         if(((uint8_t *)outdata)[i] != padbyte) {
268                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: invalid padding");
269                                 return false;
270                         }
271
272                 *outlen = origlen;
273         } else {
274                 *outlen = inlen;
275         }
276
277         return true;
278 }
279
280 int cipher_get_nid(const cipher_t *cipher) {
281         return cipher->nid;
282 }
283
284 bool cipher_active(const cipher_t *cipher) {
285         return cipher->nid != 0;
286 }