Ensure compatibility with OpenSSL 1.1.0.
[tinc] / src / openssl / cipher.c
1 /*
2     cipher.c -- Symmetric block cipher handling
3     Copyright (C) 2007-2013 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 cipher_t *cipher_open_blowfish_ofb(void) {
66         return cipher_open(EVP_bf_ofb());
67 }
68
69 void cipher_close(cipher_t *cipher) {
70         if(!cipher)
71                 return;
72
73         EVP_CIPHER_CTX_free(cipher->ctx);
74         free(cipher);
75 }
76
77 size_t cipher_keylength(const cipher_t *cipher) {
78         if(!cipher || !cipher->cipher)
79                 return 0;
80
81         return EVP_CIPHER_key_length(cipher->cipher) + EVP_CIPHER_iv_length(cipher->cipher);
82 }
83
84 size_t cipher_blocksize(const cipher_t *cipher) {
85         if(!cipher || !cipher->cipher)
86                 return 1;
87
88         return EVP_CIPHER_block_size(cipher->cipher);
89 }
90
91 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
92         bool result;
93
94         if(encrypt)
95                 result = EVP_EncryptInit_ex(cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + EVP_CIPHER_key_length(cipher->cipher));
96         else
97                 result = EVP_DecryptInit_ex(cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + EVP_CIPHER_key_length(cipher->cipher));
98
99         if(result)
100                 return true;
101
102         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
103         return false;
104 }
105
106 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
107         bool result;
108
109         if(encrypt)
110                 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));
111         else
112                 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));
113
114         if(result)
115                 return true;
116
117         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
118         return false;
119 }
120
121 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
122         if(oneshot) {
123                 int len, pad;
124                 if(EVP_EncryptInit_ex(cipher->ctx, NULL, NULL, NULL, NULL)
125                                 && EVP_EncryptUpdate(cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
126                                 && EVP_EncryptFinal(cipher->ctx, (unsigned char *)outdata + len, &pad)) {
127                         if(outlen) *outlen = len + pad;
128                         return true;
129                 }
130         } else {
131                 int len;
132                 if(EVP_EncryptUpdate(cipher->ctx, outdata, &len, indata, inlen)) {
133                         if(outlen) *outlen = len;
134                         return true;
135                 }
136         }
137
138         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
139         return false;
140 }
141
142 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
143         if(oneshot) {
144                 int len, pad;
145                 if(EVP_DecryptInit_ex(cipher->ctx, NULL, NULL, NULL, NULL)
146                                 && EVP_DecryptUpdate(cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
147                                 && EVP_DecryptFinal(cipher->ctx, (unsigned char *)outdata + len, &pad)) {
148                         if(outlen) *outlen = len + pad;
149                         return true;
150                 }
151         } else {
152                 int len;
153                 if(EVP_EncryptUpdate(cipher->ctx, outdata, &len, indata, inlen)) {
154                         if(outlen) *outlen = len;
155                         return true;
156                 }
157         }
158
159         logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
160         return false;
161 }
162
163 int cipher_get_nid(const cipher_t *cipher) {
164         if(!cipher || !cipher->cipher)
165                 return 0;
166
167         return EVP_CIPHER_nid(cipher->cipher);
168 }
169
170 bool cipher_active(const cipher_t *cipher) {
171         return cipher && cipher->cipher && EVP_CIPHER_nid(cipher->cipher) != 0;
172 }