61179078cbe88d605e1882d141c2c5eb6c1fdc4c
[tinc] / src / cipher.c
1 /*
2     cipher.c -- Symmetric block cipher handling
3     Copyright (C) 2007 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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include "system.h"
23
24 #include "cipher.h"
25 #include "logger.h"
26 #include "xalloc.h"
27
28 static struct {
29         const char *name;
30         int algo;
31         int mode;
32         int nid;
33 } ciphertable[] = {
34         {"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
35
36         {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 92},
37         {"blowfish", GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC, 91},
38         {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CFB, 93},
39         {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB, 94},
40
41         {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 418},
42         {"aes", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 419},
43         {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 421},
44         {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_OFB, 420},
45
46         {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_ECB, 422},
47         {"aes192", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC, 423},
48         {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CFB, 425},
49         {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_OFB, 424},
50
51         {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 426},
52         {"aes256", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 427},
53         {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, 429},
54         {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, 428},
55 };
56
57 static bool nametocipher(const char *name, int *algo, int *mode) {
58         int i;
59
60         for(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(int nid, int *algo, int *mode) {
72         int i;
73
74         for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
75                 if(nid == ciphertable[i].nid) {
76                         *algo = ciphertable[i].algo;
77                         *mode = ciphertable[i].mode;
78                         return true;
79                 }
80         }
81
82         return false;
83 }
84
85 static bool ciphertonid(int algo, int mode, int *nid) {
86         int i;
87
88         for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
89                 if(algo == ciphertable[i].algo && mode == ciphertable[i].mode) {
90                         *nid = ciphertable[i].nid;
91                         return true;
92                 }
93         }
94
95         return false;
96 }
97
98 static bool cipher_open(cipher_t *cipher, int algo, int mode) {
99         gcry_error_t err;
100
101         if(!ciphertonid(algo, mode, &cipher->nid)) {
102                 logger(LOG_DEBUG, _("Cipher %d mode %d has no corresponding nid!"), algo, mode);
103                 return false;
104         }
105
106         if((err = gcry_cipher_open(&cipher->handle, algo, mode, 0))) {
107                 logger(LOG_DEBUG, _("Unable to intialise cipher %d mode %d!"), algo, mode);
108                 return false;
109         }
110
111         cipher->keylen = gcry_cipher_get_algo_keylen(algo);
112         if(mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC)
113                 cipher->blklen = gcry_cipher_get_algo_blklen(algo);
114         else
115                 cipher->blklen = 0;
116         cipher->key = xmalloc(cipher->keylen + cipher->blklen);
117
118         return true;
119 }
120
121 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
122         int algo, mode;
123
124         if(!nametocipher(name, &algo, &mode)) {
125                 logger(LOG_DEBUG, _("Unknown cipher name '%s'!"), name);
126                 return false;
127         }
128
129         return cipher_open(cipher, algo, mode);
130 }
131
132 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
133         int algo, mode;
134
135         if(!nidtocipher(nid, &algo, &mode)) {
136                 logger(LOG_DEBUG, _("Unknown cipher ID %d!"), nid);
137                 return false;
138         }
139
140         return cipher_open(cipher, algo, mode);
141 }
142
143 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
144         return cipher_open(cipher, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB);
145 }
146
147 void cipher_close(cipher_t *cipher) {
148         if(cipher->handle) {
149                 gcry_cipher_close(cipher->handle);
150                 cipher->handle = NULL;
151         }
152
153         if(cipher->key) {
154                 free(cipher->key);
155                 cipher->key = NULL;
156         }
157 }
158
159 size_t cipher_keylength(const cipher_t *cipher) {
160         return cipher->keylen + cipher->blklen;
161 }
162
163 void cipher_get_key(const cipher_t *cipher, void *key) {
164         memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
165 }
166
167 bool cipher_set_key(cipher_t *cipher, void *key) {
168         memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
169
170         gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
171         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
172
173         return true;
174 }
175
176 bool cipher_regenerate_key(cipher_t *cipher) {
177         gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
178
179         gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
180         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
181
182         return true;
183 }
184
185 bool cipher_add_padding(cipher_t *cipher, void *indata, size_t inlen, size_t *outlen) {
186         size_t reqlen;
187
188         if(cipher->blklen == 1) {
189                 *outlen = inlen;
190                 return true;
191         }
192
193         reqlen = ((inlen + 1) / cipher->blklen) * cipher->blklen;
194         if(reqlen > *outlen)
195                 return false;
196
197         // add padding
198
199         *outlen = reqlen;
200         return true;
201 }
202
203 bool cipher_remove_padding(cipher_t *cipher, void *indata, size_t inlen, size_t *outlen) {
204         size_t origlen;
205
206         if(cipher->blklen == 1) {
207                 *outlen = inlen;
208                 return true;
209         }
210
211         if(inlen % cipher->blklen)
212                 return false;
213
214         // check and remove padding
215
216         *outlen = origlen;
217         return true;
218 }
219
220 bool cipher_encrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
221         gcry_error_t err;
222
223         if((err = gcry_cipher_encrypt(cipher->handle, outdata, inlen, indata, inlen))) {
224                 logger(LOG_ERR, _("Error while encrypting"));
225                 return false;
226         }
227
228         return true;
229 }
230
231 bool cipher_decrypt(cipher_t *cipher, void *indata, size_t inlen, void *outdata, size_t *outlen) {
232         gcry_error_t err;
233
234         if((err = gcry_cipher_decrypt(cipher->handle, outdata, inlen, indata, inlen))) {
235                 logger(LOG_ERR, _("Error while encrypting"));
236                 return false;
237         }
238
239         return true;
240 }
241
242 void cipher_reset(cipher_t *cipher) {
243         gcry_cipher_reset(cipher->handle);
244         gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
245 }
246
247 int cipher_get_nid(const cipher_t *cipher) {
248         return cipher->nid;
249 }
250
251 bool cipher_active(const cipher_t *cipher) {
252         return cipher->nid != 0;
253 }