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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <openssl/rand.h>
25 #include <openssl/err.h>
31 static bool cipher_open(cipher_t *cipher) {
32 cipher->keylen = cipher->cipher->key_len;
33 cipher->blklen = cipher->cipher->iv_len;
35 cipher->key = xmalloc(cipher->keylen + cipher->blklen);
37 EVP_CIPHER_CTX_init(&cipher->ctx);
42 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
43 cipher->cipher = EVP_get_cipherbyname(name);
46 return cipher_open(cipher);
48 logger(LOG_DEBUG, _("Unknown cipher name '%s'!"), name);
52 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
53 cipher->cipher = EVP_get_cipherbynid(nid);
56 return cipher_open(cipher);
58 logger(LOG_DEBUG, _("Unknown cipher nid %d!"), nid);
62 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
63 cipher->cipher = EVP_bf_ofb();
64 return cipher_open(cipher);
67 void cipher_close(cipher_t *cipher) {
68 EVP_CIPHER_CTX_cleanup(&cipher->ctx);
76 size_t cipher_keylength(const cipher_t *cipher) {
77 return cipher->keylen + cipher->blklen;
80 void cipher_get_key(const cipher_t *cipher, void *key) {
81 memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
84 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
85 memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
89 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
91 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
96 logger(LOG_ERR, _("Error while setting key: %s"), ERR_error_string(ERR_get_error(), NULL));
100 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
101 memcpy(cipher->key, key + len - (size_t)cipher->keylen, cipher->keylen);
102 memcpy(cipher->key + cipher->keylen, key + len - (size_t)cipher->keylen - (size_t)cipher->blklen, cipher->blklen);
106 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
108 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
113 logger(LOG_ERR, _("Error while setting key: %s"), ERR_error_string(ERR_get_error(), NULL));
117 bool cipher_regenerate_key(cipher_t *cipher, bool encrypt) {
120 RAND_pseudo_bytes((unsigned char *)cipher->key, cipher->keylen + cipher->blklen);
123 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
125 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)cipher->key, (unsigned char *)cipher->key + cipher->keylen);
130 logger(LOG_ERR, _("Error while regenerating key: %s"), ERR_error_string(ERR_get_error(), NULL));
134 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
136 int len = *outlen, pad;
137 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
138 &&EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)
139 && EVP_EncryptFinal(&cipher->ctx, outdata + len, &pad)) {
145 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
151 logger(LOG_ERR, _("Error while encrypting: %s"), ERR_error_string(ERR_get_error(), NULL));
155 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
157 int len = *outlen, pad;
158 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
159 && EVP_DecryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)
160 && EVP_DecryptFinal(&cipher->ctx, outdata + len, &pad)) {
166 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
172 logger(LOG_ERR, _("Error while encrypting: %s"), ERR_error_string(ERR_get_error(), NULL));
176 int cipher_get_nid(const cipher_t *cipher) {
177 return cipher->cipher ? cipher->cipher->nid : 0;
180 bool cipher_active(const cipher_t *cipher) {
181 return cipher->cipher && cipher->cipher->nid != 0;