fix: use EVP_DecryptUpdate while decrypting
[tinc] / src / openssl / cipher.c
1 /*
2     cipher.c -- Symmetric block cipher handling
3     Copyright (C) 2007-2017 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 };
34
35 static cipher_t *cipher_open(const EVP_CIPHER *evp_cipher) {
36         cipher_t *cipher = xzalloc(sizeof(*cipher));
37         cipher->cipher = evp_cipher;
38         cipher->ctx = EVP_CIPHER_CTX_new();
39
40         if(!cipher->ctx) {
41                 abort();
42         }
43
44         return cipher;
45 }
46
47 cipher_t *cipher_open_by_name(const char *name) {
48         const EVP_CIPHER *evp_cipher = EVP_get_cipherbyname(name);
49
50         if(!evp_cipher) {
51                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name);
52                 return NULL;
53         }
54
55         return cipher_open(evp_cipher);
56 }
57
58 cipher_t *cipher_open_by_nid(int nid) {
59         const EVP_CIPHER *evp_cipher = EVP_get_cipherbynid(nid);
60
61         if(!evp_cipher) {
62                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid);
63                 return NULL;
64         }
65
66         return cipher_open(evp_cipher);
67 }
68
69 void cipher_close(cipher_t *cipher) {
70         if(!cipher) {
71                 return;
72         }
73
74         EVP_CIPHER_CTX_free(cipher->ctx);
75         free(cipher);
76 }
77
78 size_t cipher_keylength(const cipher_t *cipher) {
79         if(!cipher || !cipher->cipher) {
80                 return 0;
81         }
82
83         return EVP_CIPHER_key_length(cipher->cipher) + EVP_CIPHER_iv_length(cipher->cipher);
84 }
85
86 uint64_t cipher_budget(const cipher_t *cipher) {
87         /* Hopefully some failsafe way to calculate the maximum amount of bytes to
88            send/receive with a given cipher before we might run into birthday paradox
89            attacks. Because we might use different modes, the block size of the mode
90            might be 1 byte. In that case, use the IV length. Ensure the whole thing
91            is limited to what can be represented with a 64 bits integer.
92          */
93
94         if(!cipher || !cipher->cipher) {
95                 return UINT64_MAX;        // NULL cipher
96         }
97
98         int ivlen = EVP_CIPHER_iv_length(cipher->cipher);
99         int blklen = EVP_CIPHER_block_size(cipher->cipher);
100         int len = blklen > 1 ? blklen : ivlen > 1 ? ivlen : 8;
101         int bits = len * 4 - 1;
102         return bits < 64 ? UINT64_C(1) << bits : UINT64_MAX;
103 }
104
105 size_t cipher_blocksize(const cipher_t *cipher) {
106         if(!cipher || !cipher->cipher) {
107                 return 1;
108         }
109
110         return EVP_CIPHER_block_size(cipher->cipher);
111 }
112
113 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
114         bool result;
115
116         if(encrypt) {
117                 result = EVP_EncryptInit_ex(cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + EVP_CIPHER_key_length(cipher->cipher));
118         } else {
119                 result = EVP_DecryptInit_ex(cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + EVP_CIPHER_key_length(cipher->cipher));
120         }
121
122         if(result) {
123                 return true;
124         }
125
126         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
127         return false;
128 }
129
130 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
131         bool result;
132
133         if(encrypt) {
134                 result = EVP_EncryptInit_ex(cipher->ctx, cipher->cipher, NULL, (unsigned char *)key + len - EVP_CIPHER_key_length(cipher->cipher), (unsigned char *)key + len - EVP_CIPHER_iv_length(cipher->cipher) - EVP_CIPHER_key_length(cipher->cipher));
135         } else {
136                 result = EVP_DecryptInit_ex(cipher->ctx, cipher->cipher, NULL, (unsigned char *)key + len - EVP_CIPHER_key_length(cipher->cipher), (unsigned char *)key + len - EVP_CIPHER_iv_length(cipher->cipher) - EVP_CIPHER_key_length(cipher->cipher));
137         }
138
139         if(result) {
140                 return true;
141         }
142
143         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
144         return false;
145 }
146
147 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
148         if(oneshot) {
149                 int len, pad;
150
151                 if(EVP_EncryptInit_ex(cipher->ctx, NULL, NULL, NULL, NULL)
152                                 && EVP_EncryptUpdate(cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
153                                 && EVP_EncryptFinal_ex(cipher->ctx, (unsigned char *)outdata + len, &pad)) {
154                         if(outlen) {
155                                 *outlen = len + pad;
156                         }
157
158                         return true;
159                 }
160         } else {
161                 int len;
162
163                 if(EVP_EncryptUpdate(cipher->ctx, outdata, &len, indata, inlen)) {
164                         if(outlen) {
165                                 *outlen = len;
166                         }
167
168                         return true;
169                 }
170         }
171
172         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
173         return false;
174 }
175
176 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
177         if(oneshot) {
178                 int len, pad;
179
180                 if(EVP_DecryptInit_ex(cipher->ctx, NULL, NULL, NULL, NULL)
181                                 && EVP_DecryptUpdate(cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
182                                 && EVP_DecryptFinal_ex(cipher->ctx, (unsigned char *)outdata + len, &pad)) {
183                         if(outlen) {
184                                 *outlen = len + pad;
185                         }
186
187                         return true;
188                 }
189         } else {
190                 int len;
191
192                 if(EVP_DecryptUpdate(cipher->ctx, outdata, &len, indata, inlen)) {
193                         if(outlen) {
194                                 *outlen = len;
195                         }
196
197                         return true;
198                 }
199         }
200
201         logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
202         return false;
203 }
204
205 int cipher_get_nid(const cipher_t *cipher) {
206         if(!cipher || !cipher->cipher) {
207                 return 0;
208         }
209
210         return EVP_CIPHER_nid(cipher->cipher);
211 }
212
213 bool cipher_active(const cipher_t *cipher) {
214         return cipher && cipher->cipher && EVP_CIPHER_nid(cipher->cipher) != 0;
215 }