Extract common logic in OpenSSL-specific code
[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/err.h>
23 #include <openssl/evp.h>
24
25 #include "log.h"
26 #include "cipher.h"
27 #include "../cipher.h"
28 #include "../logger.h"
29
30 typedef int (enc_init_t)(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const unsigned char *key, const unsigned char *iv);
31 typedef int (enc_update_t)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
32 typedef int (enc_final_t)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
33
34 static void cipher_open(cipher_t *cipher, const EVP_CIPHER *evp_cipher) {
35         cipher->cipher = evp_cipher;
36         cipher->ctx = EVP_CIPHER_CTX_new();
37
38         if(!cipher->ctx) {
39                 abort();
40         }
41 }
42
43 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
44         const EVP_CIPHER *evp_cipher = EVP_get_cipherbyname(name);
45
46         if(!evp_cipher) {
47                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name);
48                 return false;
49         }
50
51         cipher_open(cipher, evp_cipher);
52         return true;
53 }
54
55 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
56         const EVP_CIPHER *evp_cipher = EVP_get_cipherbynid(nid);
57
58         if(!evp_cipher) {
59                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid);
60                 return false;
61         }
62
63         cipher_open(cipher, evp_cipher);
64         return true;
65 }
66
67 void cipher_close(cipher_t *cipher) {
68         if(!cipher) {
69                 return;
70         }
71
72         if(cipher->ctx) {
73                 EVP_CIPHER_CTX_free(cipher->ctx);
74         }
75
76         memset(cipher, 0, sizeof(*cipher));
77 }
78
79 size_t cipher_keylength(const cipher_t *cipher) {
80         if(!cipher || !cipher->cipher) {
81                 return 0;
82         }
83
84         return EVP_CIPHER_key_length(cipher->cipher) + EVP_CIPHER_iv_length(cipher->cipher);
85 }
86
87 uint64_t cipher_budget(const cipher_t *cipher) {
88         /* Hopefully some failsafe way to calculate the maximum amount of bytes to
89            send/receive with a given cipher before we might run into birthday paradox
90            attacks. Because we might use different modes, the block size of the mode
91            might be 1 byte. In that case, use the IV length. Ensure the whole thing
92            is limited to what can be represented with a 64 bits integer.
93          */
94
95         if(!cipher || !cipher->cipher) {
96                 return UINT64_MAX;        // NULL cipher
97         }
98
99         int ivlen = EVP_CIPHER_iv_length(cipher->cipher);
100         int blklen = EVP_CIPHER_block_size(cipher->cipher);
101         int len = blklen > 1 ? blklen : ivlen > 1 ? ivlen : 8;
102         int bits = len * 4 - 1;
103         return bits < 64 ? UINT64_C(1) << bits : UINT64_MAX;
104 }
105
106 size_t cipher_blocksize(const cipher_t *cipher) {
107         if(!cipher || !cipher->cipher) {
108                 return 1;
109         }
110
111         return EVP_CIPHER_block_size(cipher->cipher);
112 }
113
114 static bool cipher_init_ctx(cipher_t *cipher, bool encrypt, const unsigned char *key, const unsigned char *iv) {
115         bool result;
116
117         if(encrypt) {
118                 result = EVP_EncryptInit_ex(cipher->ctx, cipher->cipher, NULL, key, iv);
119         } else {
120                 result = EVP_DecryptInit_ex(cipher->ctx, cipher->cipher, NULL, key, iv);
121         }
122
123         if(result) {
124                 return true;
125         }
126
127         openssl_err("set key");
128         return false;
129 }
130
131 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
132         unsigned char *iv = (unsigned char *)key + EVP_CIPHER_key_length(cipher->cipher);
133         return cipher_init_ctx(cipher, encrypt, key, iv);
134 }
135
136 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
137         unsigned char *k = (unsigned char *)key + len - EVP_CIPHER_key_length(cipher->cipher);
138         unsigned char *iv = k - EVP_CIPHER_iv_length(cipher->cipher);
139         return cipher_init_ctx(cipher, encrypt, k, iv);
140 }
141
142 static bool cipher_encrypt_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot,
143                                    enc_init_t init, enc_update_t update, enc_final_t final) {
144         if(oneshot) {
145                 int len, pad;
146
147                 if(init(cipher->ctx, NULL, NULL, NULL, NULL)
148                                 && update(cipher->ctx, (unsigned char *)outdata, &len, indata, (int)inlen)
149                                 && final(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(update(cipher->ctx, outdata, &len, indata, (int)inlen)) {
160                         if(outlen) {
161                                 *outlen = len;
162                         }
163
164                         return true;
165                 }
166         }
167
168         openssl_err("encrypt or decrypt data");
169         return false;
170 }
171
172 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
173         return cipher_encrypt_decrypt(cipher, indata, inlen, outdata, outlen, oneshot,
174                                       EVP_EncryptInit_ex, EVP_EncryptUpdate, EVP_EncryptFinal_ex);
175 }
176
177 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
178         return cipher_encrypt_decrypt(cipher, indata, inlen, outdata, outlen, oneshot,
179                                       EVP_DecryptInit_ex, EVP_DecryptUpdate, EVP_DecryptFinal_ex);
180 }
181
182 int cipher_get_nid(const cipher_t *cipher) {
183         if(!cipher || !cipher->cipher) {
184                 return 0;
185         }
186
187         return EVP_CIPHER_nid(cipher->cipher);
188 }
189
190 bool cipher_active(const cipher_t *cipher) {
191         return cipher && cipher->cipher && EVP_CIPHER_nid(cipher->cipher) != 0;
192 }