2 cipher.c -- Symmetric block cipher handling
3 Copyright (C) 2007-2022 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/err.h>
23 #include <openssl/evp.h>
27 #include "../cipher.h"
28 #include "../logger.h"
30 typedef int (enc_init_t)(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const unsigned char *key, const unsigned char *iv);
31 typedef int (enc_update_t)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
32 typedef int (enc_final_t)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
34 static void cipher_open(cipher_t *cipher, const EVP_CIPHER *evp_cipher) {
35 cipher->cipher = evp_cipher;
36 cipher->ctx = EVP_CIPHER_CTX_new();
43 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
44 const EVP_CIPHER *evp_cipher = EVP_get_cipherbyname(name);
47 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name);
51 cipher_open(cipher, evp_cipher);
55 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
56 const EVP_CIPHER *evp_cipher = EVP_get_cipherbynid(nid);
59 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid);
63 cipher_open(cipher, evp_cipher);
67 void cipher_close(cipher_t *cipher) {
73 EVP_CIPHER_CTX_free(cipher->ctx);
76 memset(cipher, 0, sizeof(*cipher));
79 size_t cipher_keylength(const cipher_t *cipher) {
80 if(!cipher || !cipher->cipher) {
84 return EVP_CIPHER_key_length(cipher->cipher) + EVP_CIPHER_iv_length(cipher->cipher);
87 uint64_t cipher_budget(const cipher_t *cipher) {
88 /* Hopefully some failsafe way to calculate the maximum amount of bytes to
89 send/receive with a given cipher before we might run into birthday paradox
90 attacks. Because we might use different modes, the block size of the mode
91 might be 1 byte. In that case, use the IV length. Ensure the whole thing
92 is limited to what can be represented with a 64 bits integer.
95 if(!cipher || !cipher->cipher) {
96 return UINT64_MAX; // NULL cipher
99 int ivlen = EVP_CIPHER_iv_length(cipher->cipher);
100 int blklen = EVP_CIPHER_block_size(cipher->cipher);
101 int len = blklen > 1 ? blklen : ivlen > 1 ? ivlen : 8;
102 int bits = len * 4 - 1;
103 return bits < 64 ? UINT64_C(1) << bits : UINT64_MAX;
106 size_t cipher_blocksize(const cipher_t *cipher) {
107 if(!cipher || !cipher->cipher) {
111 return EVP_CIPHER_block_size(cipher->cipher);
114 static bool cipher_init_ctx(cipher_t *cipher, bool encrypt, const unsigned char *key, const unsigned char *iv) {
118 result = EVP_EncryptInit_ex(cipher->ctx, cipher->cipher, NULL, key, iv);
120 result = EVP_DecryptInit_ex(cipher->ctx, cipher->cipher, NULL, key, iv);
127 openssl_err("set key");
131 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
132 unsigned char *iv = (unsigned char *)key + EVP_CIPHER_key_length(cipher->cipher);
133 return cipher_init_ctx(cipher, encrypt, key, iv);
136 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
137 unsigned char *k = (unsigned char *)key + len - EVP_CIPHER_key_length(cipher->cipher);
138 unsigned char *iv = k - EVP_CIPHER_iv_length(cipher->cipher);
139 return cipher_init_ctx(cipher, encrypt, k, iv);
142 static bool cipher_encrypt_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot,
143 enc_init_t init, enc_update_t update, enc_final_t final) {
147 if(init(cipher->ctx, NULL, NULL, NULL, NULL)
148 && update(cipher->ctx, (unsigned char *)outdata, &len, indata, (int)inlen)
149 && final(cipher->ctx, (unsigned char *)outdata + len, &pad)) {
159 if(update(cipher->ctx, outdata, &len, indata, (int)inlen)) {
168 openssl_err("encrypt or decrypt data");
172 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
173 return cipher_encrypt_decrypt(cipher, indata, inlen, outdata, outlen, oneshot,
174 EVP_EncryptInit_ex, EVP_EncryptUpdate, EVP_EncryptFinal_ex);
177 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
178 return cipher_encrypt_decrypt(cipher, indata, inlen, outdata, outlen, oneshot,
179 EVP_DecryptInit_ex, EVP_DecryptUpdate, EVP_DecryptFinal_ex);
182 int cipher_get_nid(const cipher_t *cipher) {
183 if(!cipher || !cipher->cipher) {
187 return EVP_CIPHER_nid(cipher->cipher);
190 bool cipher_active(const cipher_t *cipher) {
191 return cipher && cipher->cipher && EVP_CIPHER_nid(cipher->cipher) != 0;