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 typedef struct cipher_counter {
30 unsigned char counter[EVP_MAX_IV_LENGTH];
31 unsigned char block[EVP_MAX_IV_LENGTH];
35 static bool cipher_open(cipher_t *cipher) {
36 EVP_CIPHER_CTX_init(&cipher->ctx);
41 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
42 cipher->cipher = EVP_get_cipherbyname(name);
45 return cipher_open(cipher);
47 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name);
51 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
52 cipher->cipher = EVP_get_cipherbynid(nid);
55 return cipher_open(cipher);
57 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid);
61 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
62 cipher->cipher = EVP_bf_ofb();
63 return cipher_open(cipher);
66 void cipher_close(cipher_t *cipher) {
67 EVP_CIPHER_CTX_cleanup(&cipher->ctx);
69 free(cipher->counter);
74 size_t cipher_keylength(const cipher_t *cipher) {
75 return cipher->cipher->key_len + cipher->cipher->block_size;
78 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
82 result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
84 result = EVP_DecryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, (unsigned char *)key + cipher->cipher->key_len);
89 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
93 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
97 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);
99 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);
104 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
108 bool cipher_set_counter(cipher_t *cipher, const void *counter, size_t len) {
109 if(len > cipher->cipher->block_size - 4) {
110 logger(DEBUG_ALWAYS, LOG_ERR, "Counter too long");
114 memcpy(cipher->counter->counter + cipher->cipher->block_size - len, counter, len);
115 memset(cipher->counter->counter, 0, 4);
116 cipher->counter->n = 0;
121 bool cipher_set_counter_key(cipher_t *cipher, void *key) {
122 int result = EVP_EncryptInit_ex(&cipher->ctx, cipher->cipher, NULL, (unsigned char *)key, NULL);
124 logger(DEBUG_ALWAYS, LOG_ERR, "Error while setting key: %s", ERR_error_string(ERR_get_error(), NULL));
129 cipher->counter = xmalloc_and_zero(sizeof *cipher->counter);
131 cipher->counter->n = 0;
133 memcpy(cipher->counter->counter, (unsigned char *)key + cipher->cipher->key_len, cipher->cipher->block_size);
138 bool cipher_counter_xor(cipher_t *cipher, const void *indata, size_t inlen, void *outdata) {
139 if(!cipher->counter) {
140 logger(DEBUG_ALWAYS, LOG_ERR, "Counter not initialized");
144 const unsigned char *in = indata;
145 unsigned char *out = outdata;
148 // Encrypt the new counter value if we need it
149 if(!cipher->counter->n) {
151 if(!EVP_EncryptUpdate(&cipher->ctx, cipher->counter->block, &len, cipher->counter->counter, cipher->cipher->block_size)) {
152 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
156 // Increase the counter value
157 for(int i = 0; i < cipher->cipher->block_size; i++)
158 if(++cipher->counter->counter[i])
162 *out++ = *in++ ^ cipher->counter->counter[cipher->counter->n++];
164 if(cipher->counter->n >= cipher->cipher->block_size)
165 cipher->counter->n = 0;
172 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
175 if(EVP_EncryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
176 && EVP_EncryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
177 && EVP_EncryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
178 if(outlen) *outlen = len + pad;
183 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
184 if(outlen) *outlen = len;
189 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
193 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
196 if(EVP_DecryptInit_ex(&cipher->ctx, NULL, NULL, NULL, NULL)
197 && EVP_DecryptUpdate(&cipher->ctx, (unsigned char *)outdata, &len, indata, inlen)
198 && EVP_DecryptFinal(&cipher->ctx, (unsigned char *)outdata + len, &pad)) {
199 if(outlen) *outlen = len + pad;
204 if(EVP_EncryptUpdate(&cipher->ctx, outdata, &len, indata, inlen)) {
205 if(outlen) *outlen = len;
210 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", ERR_error_string(ERR_get_error(), NULL));
214 int cipher_get_nid(const cipher_t *cipher) {
215 return cipher->cipher ? cipher->cipher->nid : 0;
218 bool cipher_active(const cipher_t *cipher) {
219 return cipher->cipher && cipher->cipher->nid != 0;