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