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