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