63892f3adba0afc049fe0bd3a35d2fc5299cdfed
[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(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(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         if(cipher->counter) {
69                 free(cipher->counter);
70                 cipher->counter = 0;
71         }
72 }
73
74 size_t cipher_keylength(const cipher_t *cipher) {
75         return cipher->cipher->key_len + cipher->cipher->block_size;
76 }
77
78 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
79         bool result;
80
81         if(encrypt)
82                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
83         else
84                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
85
86         if(result)
87                 return true;
88
89         logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
90         return false;
91 }
92
93 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
94         bool result;
95
96         if(encrypt)
97                 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);
98         else
99                 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);
100
101         if(result)
102                 return true;
103
104         logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
105         return false;
106 }
107
108 bool cipher_set_counter_key(cipher_t *cipher, void *key) {
109         int result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, NULL);
110         if(!result) {
111                 logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
112                 return false;
113         }
114
115         if(!cipher->counter)
116                 cipher->counter = xmalloc_and_zero(sizeof *cipher->counter);
117         else
118                 cipher->counter->n = 0;
119
120         memcpy(cipher->counter->counter, (unsigned char *)key + cipher->cipher->key_len, cipher->cipher->block_size);
121
122         return true;
123 }
124
125 bool cipher_counter_xor(cipher_t *cipher, const void *indata, size_t inlen, void *outdata) {
126         if(!cipher->counter) {
127                 logger(LOG_ERR, "Counter not initialized");
128                 return false;
129         }
130
131         const unsigned char *in = indata;
132         unsigned char *out = outdata;
133
134         while(inlen--) {
135                 // Encrypt the new counter value if we need it 
136                 if(!cipher->counter->n) {
137                         int len;
138                         if(!EVP_EncryptUpdate(&cipher->ctx, cipher->counter->block, &len, cipher->counter->counter, cipher->cipher->block_size)) {
139                                 logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
140                                 return false;
141                         }
142
143                         // Increase the counter value
144                         for(int i = 0; i < cipher->cipher->block_size; i++)
145                                 if(++cipher->counter->counter[i])
146                                         break;
147                 }
148
149                 *out++ = *in++ ^ cipher->counter->counter[cipher->counter->n++];
150
151                 if(cipher->counter->n >= cipher->cipher->block_size)
152                         cipher->counter->n = 0;
153         }
154
155         return true;
156 }
157
158
159 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
160         if(oneshot) {
161                 int len, pad;
162                 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
163                                 && EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
164                                 && EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
165                         if(outlen) *outlen = len + pad;
166                         return true;
167                 }
168         } else {
169                 int len;
170                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
171                         if(outlen) *outlen = len;
172                         return true;
173                 }
174         }
175
176         logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
177         return false;
178 }
179
180 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
181         if(oneshot) {
182                 int len, pad;
183                 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
184                                 && EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
185                                 && EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
186                         if(outlen) *outlen = len + pad;
187                         return true;
188                 }
189         } else {
190                 int len;
191                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
192                         if(outlen) *outlen = len;
193                         return true;
194                 }
195         }
196
197         logger(LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
198         return false;
199 }
200
201 int cipher_get_nid(const cipher_t *cipher) {
202         return cipher->cipher ? cipher->cipher->nid : 0;
203 }
204
205 bool cipher_active(const cipher_t *cipher) {
206         return cipher->cipher && cipher->cipher->nid != 0;
207 }