2 cipher.c -- Symmetric block cipher handling
3 Copyright (C) 2007-2013 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.
20 #include "../system.h"
22 #include <openssl/rand.h>
23 #include <openssl/err.h>
24 #include <openssl/evp.h>
26 #include "../cipher.h"
27 #include "../logger.h"
28 #include "../xalloc.h"
32 const EVP_CIPHER *cipher;
33 struct cipher_counter *counter;
36 typedef struct cipher_counter {
37 unsigned char counter[CIPHER_MAX_IV_SIZE];
38 unsigned char block[CIPHER_MAX_IV_SIZE];
42 static cipher_t *cipher_open(const EVP_CIPHER *evp_cipher) {
43 cipher_t *cipher = xzalloc(sizeof *cipher);
44 cipher->cipher = evp_cipher;
45 EVP_CIPHER_CTX_init(&cipher->ctx);
50 cipher_t *cipher_open_by_name(const char *name) {
51 const EVP_CIPHER *evp_cipher = EVP_get_cipherbyname(name);
53 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name);
57 return cipher_open(evp_cipher);
60 cipher_t *cipher_open_by_nid(int nid) {
61 const EVP_CIPHER *evp_cipher = EVP_get_cipherbynid(nid);
63 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid);
67 return cipher_open(evp_cipher);
70 cipher_t *cipher_open_blowfish_ofb(void) {
71 return cipher_open(EVP_bf_ofb());
74 void cipher_close(cipher_t *cipher) {
78 EVP_CIPHER_CTX_cleanup(&cipher->ctx);
79 free(cipher->counter);
83 size_t cipher_keylength(const cipher_t *cipher) {
84 if(!cipher || !cipher->cipher)
87 return cipher->cipher->key_len + cipher->cipher->block_size;
90 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
94 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
96 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
101 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
105 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
109 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);
111 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);
116 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
120 bool cipher_set_counter(cipher_t *cipher, const void *counter, size_t len) {
121 if(len > cipher->cipher->block_size - 4) {
122 logger(DEBUG_ALWAYS, LOG_ERR, "Counter too long");
126 memcpy(cipher->counter->counter + cipher->cipher->block_size - len, counter, len);
127 memset(cipher->counter->counter, 0, 4);
128 cipher->counter->n = 0;
133 bool cipher_set_counter_key(cipher_t *cipher, void *key) {
134 int result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, NULL);
136 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
141 cipher->counter = xzalloc(sizeof *cipher->counter);
143 cipher->counter->n = 0;
145 memcpy(cipher->counter->counter, (unsigned char *)key + cipher->cipher->key_len, cipher->cipher->block_size);
150 bool cipher_counter_xor(cipher_t *cipher, const void *indata, size_t inlen, void *outdata) {
151 if(!cipher->counter) {
152 logger(DEBUG_ALWAYS, LOG_ERR, "Counter not initialized");
156 const unsigned char *in = indata;
157 unsigned char *out = outdata;
160 // Encrypt the new counter value if we need it
161 if(!cipher->counter->n) {
163 if(!EVP_EncryptUpdate(&cipher->ctx, cipher->counter->block, &len, cipher->counter->counter, cipher->cipher->block_size)) {
164 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
168 // Increase the counter value
169 for(int i = 0; i < cipher->cipher->block_size; i++)
170 if(++cipher->counter->counter[i])
174 *out++ = *in++ ^ cipher->counter->counter[cipher->counter->n++];
176 if(cipher->counter->n >= cipher->cipher->block_size)
177 cipher->counter->n = 0;
184 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
187 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
188 && EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
189 && EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
190 if(outlen) *outlen = len + pad;
195 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
196 if(outlen) *outlen = len;
201 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
205 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
208 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
209 && EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
210 && EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
211 if(outlen) *outlen = len + pad;
216 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
217 if(outlen) *outlen = len;
222 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
226 int cipher_get_nid(const cipher_t *cipher) {
227 if(!cipher || !cipher->cipher)
230 return cipher->cipher->nid;
233 bool cipher_active(const cipher_t *cipher) {
234 return cipher && cipher->cipher && cipher->cipher->nid != 0;