2 cipher.c -- Symmetric block cipher handling
3 Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
22 #include <openssl/rand.h>
23 #include <openssl/err.h>
29 static bool cipher_open(cipher_t *cipher) {
30 cipher->keylen = cipher->cipher->key_len;
31 cipher->blklen = cipher->cipher->iv_len;
33 cipher->key = xmalloc(cipher->keylen + cipher->blklen);
35 EVP_CIPHER_CTX_init(&cipher->ctx);
40 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
41 cipher->cipher = EVP_get_cipherbyname(name);
44 return cipher_open(cipher);
46 logger(LOG_DEBUG, "Unknown cipher name '%s'!", name);
50 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
51 cipher->cipher = EVP_get_cipherbynid(nid);
54 return cipher_open(cipher);
56 logger(LOG_DEBUG, "Unknown cipher nid %d!", nid);
60 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
61 cipher->cipher = EVP_bf_ofb();
62 return cipher_open(cipher);
65 void cipher_close(cipher_t *cipher) {
66 EVP_CIPHER_CTX_cleanup(&cipher->ctx);
74 size_t cipher_keylength(const cipher_t *cipher) {
75 return cipher->keylen + cipher->blklen;
78 void cipher_get_key(const cipher_t *cipher, void *key) {
79 memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
82 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
83 memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
87 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
89 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
94 logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
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);
104 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
106 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
111 logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
115 bool cipher_regenerate_key(cipher_t *cipher, bool encrypt) {
118 RAND_pseudo_bytes((unsigned char *)cipher->key, cipher->keylen + cipher->blklen);
121 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
123 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
128 logger(LOG_ERR, "Error while regenerating key: %s", ERR_error_string(ERR_get_error(), NULL));
132 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool 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)) {
143 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
149 logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
153 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool 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)) {
164 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
170 logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
174 int cipher_get_nid(const cipher_t *cipher) {
175 return cipher->cipher ? cipher->cipher->nid : 0;
178 bool cipher_active(const cipher_t *cipher) {
179 return cipher->cipher && cipher->cipher->nid != 0;