Use conditional compilation for cryptographic functions.
[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         struct cipher_counter *counter;
34 };
35
36 typedef struct cipher_counter {
37         unsigned char counter[CIPHER_MAX_IV_SIZE];
38         unsigned char block[CIPHER_MAX_IV_SIZE];
39         int n;
40 } cipher_counter_t;
41
42 static cipher_t *cipher_open(const EVP_CIPHER *evp_cipher) {
43         cipher_t *cipher = xmalloc_and_zero(sizeof *cipher);
44         cipher->cipher = evp_cipher;
45         EVP_CIPHER_CTX_init(&cipher->ctx);
46
47         return cipher;
48 }
49
50 cipher_t *cipher_open_by_name(const char *name) {
51         const EVP_CIPHER *evp_cipher = EVP_get_cipherbyname(name);
52         if(!evp_cipher) {
53                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name);
54                 return NULL;
55         }
56
57         return cipher_open(evp_cipher);
58 }
59
60 cipher_t *cipher_open_by_nid(int nid) {
61         const EVP_CIPHER *evp_cipher = EVP_get_cipherbynid(nid);
62         if(!evp_cipher) {
63                 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid);
64                 return NULL;
65         }
66
67         return cipher_open(evp_cipher);
68 }
69
70 cipher_t *cipher_open_blowfish_ofb(void) {
71         return cipher_open(EVP_bf_ofb());
72 }
73
74 void cipher_close(cipher_t *cipher) {
75         if(!cipher)
76                 return;
77
78         EVP_CIPHER_CTX_cleanup(&cipher->ctx);
79         free(cipher->counter);
80         free(cipher);
81 }
82
83 size_t cipher_keylength(const cipher_t *cipher) {
84         return cipher->cipher->key_len + cipher->cipher->block_size;
85 }
86
87 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
88         bool result;
89
90         if(encrypt)
91                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
92         else
93                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
94
95         if(result)
96                 return true;
97
98         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
99         return false;
100 }
101
102 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
103         bool result;
104
105         if(encrypt)
106                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key + len - cipher->cipher->key_len, (unsigned char *)key + len - cipher->cipher->iv_len - cipher->cipher->key_len);
107         else
108                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key + len - cipher->cipher->key_len, (unsigned char *)key + len - cipher->cipher->iv_len - cipher->cipher->key_len);
109
110         if(result)
111                 return true;
112
113         logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
114         return false;
115 }
116
117 bool cipher_set_counter(cipher_t *cipher, const void *counter, size_t len) {
118         if(len > cipher->cipher->block_size - 4) {
119                 logger(DEBUG_ALWAYS, LOG_ERR, "Counter too long");
120                 abort();
121         }
122
123         memcpy(cipher->counter->counter + cipher->cipher->block_size - len, counter, len);
124         memset(cipher->counter->counter, 0, 4);
125         cipher->counter->n = 0;
126
127         return true;
128 }
129
130 bool cipher_set_counter_key(cipher_t *cipher, void *key) {
131         int result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, NULL);
132         if(!result) {
133                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
134                 return false;
135         }
136
137         if(!cipher->counter)
138                 cipher->counter = xmalloc_and_zero(sizeof *cipher->counter);
139         else
140                 cipher->counter->n = 0;
141
142         memcpy(cipher->counter->counter, (unsigned char *)key + cipher->cipher->key_len, cipher->cipher->block_size);
143
144         return true;
145 }
146
147 bool cipher_counter_xor(cipher_t *cipher, const void *indata, size_t inlen, void *outdata) {
148         if(!cipher->counter) {
149                 logger(DEBUG_ALWAYS, LOG_ERR, "Counter not initialized");
150                 return false;
151         }
152
153         const unsigned char *in = indata;
154         unsigned char *out = outdata;
155
156         while(inlen--) {
157                 // Encrypt the new counter value if we need it
158                 if(!cipher->counter->n) {
159                         int len;
160                         if(!EVP_EncryptUpdate(&cipher->ctx, cipher->counter->block, &len, cipher->counter->counter, cipher->cipher->block_size)) {
161                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
162                                 return false;
163                         }
164
165                         // Increase the counter value
166                         for(int i = 0; i < cipher->cipher->block_size; i++)
167                                 if(++cipher->counter->counter[i])
168                                         break;
169                 }
170
171                 *out++ = *in++ ^ cipher->counter->counter[cipher->counter->n++];
172
173                 if(cipher->counter->n >= cipher->cipher->block_size)
174                         cipher->counter->n = 0;
175         }
176
177         return true;
178 }
179
180
181 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
182         if(oneshot) {
183                 int len, pad;
184                 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
185                                 && EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
186                                 && EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
187                         if(outlen) *outlen = len + pad;
188                         return true;
189                 }
190         } else {
191                 int len;
192                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
193                         if(outlen) *outlen = len;
194                         return true;
195                 }
196         }
197
198         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
199         return false;
200 }
201
202 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
203         if(oneshot) {
204                 int len, pad;
205                 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
206                                 && EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
207                                 && EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
208                         if(outlen) *outlen = len + pad;
209                         return true;
210                 }
211         } else {
212                 int len;
213                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
214                         if(outlen) *outlen = len;
215                         return true;
216                 }
217         }
218
219         logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
220         return false;
221 }
222
223 int cipher_get_nid(const cipher_t *cipher) {
224         return cipher->cipher ? cipher->cipher->nid : 0;
225 }
226
227 bool cipher_active(const cipher_t *cipher) {
228         return cipher && cipher->cipher && cipher->cipher->nid != 0;
229 }