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