b01f5267c04c17456f16f55646b95d18a2758906
[oweals/tinc.git] / src / openssl / cipher.c
1 /*
2     cipher.c -- Symmetric block cipher handling
3     Copyright (C) 2007-2013 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 <openssl/rand.h>
23 #include <openssl/err.h>
24 #include <openssl/evp.h>
25
26 #include "../cipher.h"
27 #include "../logger.h"
28 #include "../xalloc.h"
29
30 struct cipher {
31         EVP_CIPHER_CTX ctx;
32         const EVP_CIPHER *cipher;
33         struct cipher_counter *counter;
34 };
35
36 typedef struct cipher_counter {
37         unsigned char counter[CIPHER_MAX_IV_SIZE];
38         unsigned char block[CIPHER_MAX_IV_SIZE];
39         int n;
40 } cipher_counter_t;
41
42 static cipher_t *cipher_open(const EVP_CIPHER *evp_cipher) {
43         cipher_t *cipher = xzalloc(sizeof *cipher);
44         cipher->cipher = evp_cipher;
45         EVP_CIPHER_CTX_init(&cipher->ctx);
46
47         return cipher;
48 }
49
50 cipher_t *cipher_open_by_name(const char *name) {
51         const EVP_CIPHER *evp_cipher = EVP_get_cipherbyname(name);
52         if(!evp_cipher) {
53                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name);
54                 return NULL;
55         }
56
57         return cipher_open(evp_cipher);
58 }
59
60 cipher_t *cipher_open_by_nid(int nid) {
61         const EVP_CIPHER *evp_cipher = EVP_get_cipherbynid(nid);
62         if(!evp_cipher) {
63                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid);
64                 return NULL;
65         }
66
67         return cipher_open(evp_cipher);
68 }
69
70 cipher_t *cipher_open_blowfish_ofb(void) {
71         return cipher_open(EVP_bf_ofb());
72 }
73
74 void cipher_close(cipher_t *cipher) {
75         if(!cipher)
76                 return;
77
78         EVP_CIPHER_CTX_cleanup(&cipher->ctx);
79         free(cipher->counter);
80         free(cipher);
81 }
82
83 size_t cipher_keylength(const cipher_t *cipher) {
84         if(!cipher || !cipher->cipher)
85                 return 0;
86
87         return cipher->cipher->key_len + cipher->cipher->iv_len;
88 }
89
90 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
91         bool result;
92
93         if(encrypt)
94                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
95         else
96                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
97
98         if(result)
99                 return true;
100
101         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
102         return false;
103 }
104
105 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
106         bool result;
107
108         if(encrypt)
109                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key + len - cipher->cipher->key_len, (unsigned char *)key + len - cipher->cipher->iv_len - cipher->cipher->key_len);
110         else
111                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key + len - cipher->cipher->key_len, (unsigned char *)key + len - cipher->cipher->iv_len - cipher->cipher->key_len);
112
113         if(result)
114                 return true;
115
116         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
117         return false;
118 }
119
120 bool cipher_set_counter(cipher_t *cipher, const void *counter, size_t len) {
121         if(len > cipher->cipher->iv_len - 4) {
122                 logger(DEBUG_ALWAYS, LOG_ERR, "Counter too long");
123                 return false;
124         }
125
126         memcpy(cipher->counter->counter, counter, len);
127         cipher->counter->n = 0;
128
129         return true;
130 }
131
132 bool cipher_set_counter_key(cipher_t *cipher, void *key) {
133         int result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, NULL);
134         if(!result) {
135                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
136                 return false;
137         }
138
139         if(!cipher->counter)
140                 cipher->counter = xzalloc(sizeof *cipher->counter);
141         else
142                 cipher->counter->n = 0;
143
144         memcpy(cipher->counter->counter, (unsigned char *)key + cipher->cipher->key_len, cipher->cipher->iv_len);
145
146         return true;
147 }
148
149 bool cipher_gcm_encrypt_start(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
150         int len = 0;
151         if(!EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, cipher->counter->counter)
152                         || (inlen && !EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, (unsigned char *)indata, inlen))) {
153                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
154                 return false;
155         }
156         if(outlen)
157                 *outlen = len;
158         return true;
159 }
160
161 bool cipher_gcm_encrypt_finish(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
162         int len = 0, pad = 0;
163         if(!(inlen && EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, (unsigned char *)indata, inlen))
164                         || !EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
165                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
166                 return false;
167         }
168         EVP_CIPHER_CTX_ctrl(&cipher->ctx, EVP_CTRL_GCM_GET_TAG, 16, (unsigned char *)outdata + len + pad);
169         if(outlen)
170                 *outlen = len + pad + 16;
171         return true;
172 }
173
174 bool cipher_gcm_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
175         int len = 0, pad = 0;
176         if(!EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, cipher->counter->counter) ||
177                         !EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, (unsigned char *)indata, inlen) ||
178                         !EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
179                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
180                 return false;
181         }
182         EVP_CIPHER_CTX_ctrl(&cipher->ctx, EVP_CTRL_GCM_GET_TAG, 16, (unsigned char *)outdata + len + pad);
183         if(outlen)
184                 *outlen = len + pad + 16;
185         return true;
186 }
187
188 bool cipher_gcm_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
189         if(inlen < 16)
190                 return false;
191
192         int len = 0, pad = 0;
193         if(!EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, cipher->counter->counter)) {
194                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
195                 return false;
196         }
197
198         EVP_CIPHER_CTX_ctrl(&cipher->ctx, EVP_CTRL_GCM_SET_TAG, 16, (unsigned char *)indata + inlen - 16);
199
200         if(!EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, (unsigned char *)indata, inlen - 16) ||
201                         !EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
202                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
203                 return false;
204         }
205         if(outlen)
206                 *outlen = len;
207         return true;
208 }
209
210 bool cipher_gcm_decrypt_start(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
211         int len = 0;
212         if(!EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, cipher->counter->counter)
213                         || (inlen && !EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, (unsigned char *)indata, inlen))) {
214                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
215                 return false;
216         }
217         if(outlen)
218                 *outlen = len;
219         return true;
220 }
221
222 bool cipher_gcm_decrypt_finish(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
223         if(inlen < 16)
224                 return false;
225
226         EVP_CIPHER_CTX_ctrl(&cipher->ctx, EVP_CTRL_GCM_SET_TAG, 16, (unsigned char *)indata + inlen - 16);
227
228         int len = 0, pad = 0;
229         if((inlen > 16 && !EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, (unsigned char *)indata, inlen - 16))
230                         || !EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
231                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
232                 return false;
233         }
234         return true;
235 }
236
237
238 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
239         if(oneshot) {
240                 int len, pad;
241                 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
242                                 && EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
243                                 && EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
244                         if(outlen) *outlen = len + pad;
245                         return true;
246                 }
247         } else {
248                 int len;
249                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
250                         if(outlen) *outlen = len;
251                         return true;
252                 }
253         }
254
255         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
256         return false;
257 }
258
259 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
260         if(oneshot) {
261                 int len, pad;
262                 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
263                                 && EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
264                                 && EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
265                         if(outlen) *outlen = len + pad;
266                         return true;
267                 }
268         } else {
269                 int len;
270                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
271                         if(outlen) *outlen = len;
272                         return true;
273                 }
274         }
275
276         logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
277         return false;
278 }
279
280 int cipher_get_nid(const cipher_t *cipher) {
281         if(!cipher || !cipher->cipher)
282                 return 0;
283
284         return cipher->cipher->nid;
285 }
286
287 bool cipher_active(const cipher_t *cipher) {
288         return cipher && cipher->cipher && cipher->cipher->nid != 0;
289 }