Update FSF address in files not covered by the merge.
[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 static bool cipher_open(cipher_t *cipher) {
30         cipher->keylen = cipher->cipher->key_len;
31         cipher->blklen = cipher->cipher->iv_len;
32
33         cipher->key = xmalloc(cipher->keylen + cipher->blklen);
34
35         EVP_CIPHER_CTX_init(&cipher->ctx);
36
37         return true;
38 }
39
40 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
41         cipher->cipher = EVP_get_cipherbyname(name);
42
43         if(cipher->cipher)
44                 return cipher_open(cipher);
45
46         logger(LOG_DEBUG, "Unknown cipher name '%s'!", name);
47         return false;
48 }
49
50 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
51         cipher->cipher = EVP_get_cipherbynid(nid);
52
53         if(cipher->cipher)
54                 return cipher_open(cipher);
55
56         logger(LOG_DEBUG, "Unknown cipher nid %d!", nid);
57         return false;
58 }
59
60 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
61         cipher->cipher = EVP_bf_ofb();
62         return cipher_open(cipher);
63 }
64
65 void cipher_close(cipher_t *cipher) {
66         EVP_CIPHER_CTX_cleanup(&cipher->ctx);
67
68         if(cipher->key) {
69                 free(cipher->key);
70                 cipher->key = NULL;
71         }
72 }
73
74 size_t cipher_keylength(const cipher_t *cipher) {
75         return cipher->keylen + cipher->blklen;
76 }
77
78 void cipher_get_key(const cipher_t *cipher, void *key) {
79         memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
80 }
81
82 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
83         memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
84         bool result;
85
86         if(encrypt)
87                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
88         else
89                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
90
91         if(result)
92                 return true;
93
94         logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
95         return false;
96 }
97
98 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
99         memcpy(cipher->key, key + len - (size_t)cipher->keylen, cipher->keylen);
100         memcpy(cipher->key + cipher->keylen, key + len - (size_t)cipher->keylen - (size_t)cipher->blklen, cipher->blklen);
101         bool result;
102
103         if(encrypt)
104                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
105         else
106                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
107
108         if(result)
109                 return true;
110
111         logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
112         return false;
113 }
114
115 bool cipher_regenerate_key(cipher_t *cipher, bool encrypt) {
116         bool result;
117
118         RAND_pseudo_bytes((unsigned char *)cipher->key, cipher->keylen + cipher->blklen);
119
120         if(encrypt)
121                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
122         else
123                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
124
125         if(result)
126                 return true;
127         
128         logger(LOG_ERR, "Error while regenerating key: %s", ERR_error_string(ERR_get_error(), NULL));
129         return false;
130 }
131
132 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
133         if(oneshot) {
134                 int len = *outlen, pad;
135                 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
136                                 &&EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)
137                                 && EVP_EncryptFinal(&cipher->ctx, outdata + len, &pad)) {
138                         *outlen = len + pad;
139                         return true;
140                 }
141         } else {
142                 int len = *outlen;
143                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
144                         *outlen = len;
145                         return true;
146                 }
147         }
148
149         logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
150         return false;
151 }
152
153 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
154         if(oneshot) {
155                 int len = *outlen, pad;
156                 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
157                                 && EVP_DecryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)
158                                 && EVP_DecryptFinal(&cipher->ctx, outdata + len, &pad)) {
159                         *outlen = len + pad;
160                         return true;
161                 }
162         } else {
163                 int len = *outlen;
164                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
165                         *outlen = len;
166                         return true;
167                 }
168         }
169
170         logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
171         return false;
172 }
173
174 int cipher_get_nid(const cipher_t *cipher) {
175         return cipher->cipher ? cipher->cipher->nid : 0;
176 }
177
178 bool cipher_active(const cipher_t *cipher) {
179         return cipher->cipher && cipher->cipher->nid != 0;
180 }