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 EVP_CIPHER_CTX_init(&cipher->ctx);
35 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
36 cipher->cipher = EVP_get_cipherbyname(name);
39 return cipher_open(cipher);
41 logger(LOG_ERR, "Unknown cipher name '%s'!", name);
45 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
46 cipher->cipher = EVP_get_cipherbynid(nid);
49 return cipher_open(cipher);
51 logger(LOG_ERR, "Unknown cipher nid %d!", nid);
55 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
56 cipher->cipher = EVP_bf_ofb();
57 return cipher_open(cipher);
60 void cipher_close(cipher_t *cipher) {
61 EVP_CIPHER_CTX_cleanup(&cipher->ctx);
64 size_t cipher_keylength(const cipher_t *cipher) {
65 return cipher->cipher->key_len + cipher->cipher->iv_len;
68 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
72 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
74 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
79 logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
83 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool 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);
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);
94 logger(LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
98 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
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 if(outlen) *outlen = len + pad;
109 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
110 if(outlen) *outlen = len;
115 logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
119 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
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 if(outlen) *outlen = len + pad;
130 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
131 if(outlen) *outlen = len;
136 logger(LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
140 int cipher_get_nid(const cipher_t *cipher) {
141 return cipher->cipher ? cipher->cipher->nid : 0;
144 bool cipher_active(const cipher_t *cipher) {
145 return cipher->cipher && cipher->cipher->nid != 0;