86a1acad889d981a8afcbdbed2eb58b0490b5260
[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         EVP_CIPHER_CTX_init(&cipher->ctx);
31
32         return true;
33 }
34
35 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
36         cipher->cipher = EVP_get_cipherbyname(name);
37
38         if(cipher->cipher)
39                 return cipher_open(cipher);
40
41         logger(LOG_ERR, "Unknown cipher name '%s'!", name);
42         return false;
43 }
44
45 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
46         cipher->cipher = EVP_get_cipherbynid(nid);
47
48         if(cipher->cipher)
49                 return cipher_open(cipher);
50
51         logger(LOG_ERR, "Unknown cipher nid %d!", nid);
52         return false;
53 }
54
55 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
56         cipher->cipher = EVP_bf_ofb();
57         return cipher_open(cipher);
58 }
59
60 void cipher_close(cipher_t *cipher) {
61         EVP_CIPHER_CTX_cleanup(&cipher->ctx);
62 }
63
64 size_t cipher_keylength(const cipher_t *cipher) {
65         return cipher->cipher->key_len + cipher->cipher->iv_len;
66 }
67
68 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
69         bool result;
70
71         if(encrypt)
72                 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
73         else
74                 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
75
76         if(result)
77                 return true;
78
79         logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
80         return false;
81 }
82
83 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
84         bool result;
85
86         if(encrypt)
87                 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);
88         else
89                 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);
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_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
99         if(oneshot) {
100                 int len, pad;
101                 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
102                                 && EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
103                                 && EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
104                         *outlen = len + pad;
105                         return true;
106                 }
107         } else {
108                 int len;
109                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
110                         *outlen = len;
111                         return true;
112                 }
113         }
114
115         logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
116         return false;
117 }
118
119 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
120         if(oneshot) {
121                 int len, pad;
122                 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
123                                 && EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
124                                 && EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
125                         *outlen = len + pad;
126                         return true;
127                 }
128         } else {
129                 int len;
130                 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
131                         *outlen = len;
132                         return true;
133                 }
134         }
135
136         logger(LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
137         return false;
138 }
139
140 int cipher_get_nid(const cipher_t *cipher) {
141         return cipher->cipher ? cipher->cipher->nid : 0;
142 }
143
144 bool cipher_active(const cipher_t *cipher) {
145         return cipher->cipher && cipher->cipher->nid != 0;
146 }