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