2 cipher.c -- Symmetric block cipher handling
3 Copyright (C) 2007-2012 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.
32 {"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
34 {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 92},
35 {"blowfish", GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC, 91},
36 {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CFB, 93},
37 {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB, 94},
39 {"aes-128-ecb", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 418},
40 {"aes-128-cbc", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 419},
41 {"aes-128-cfb", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 421},
42 {"aes-128-ofb", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_OFB, 420},
44 {"aes-192-ecb", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_ECB, 422},
45 {"aes-192-cbc", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC, 423},
46 {"aes-192-cfb", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CFB, 425},
47 {"aes-192-ofb", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_OFB, 424},
49 {"aes-256-ecb", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 426},
50 {"aes-256-cbc", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 427},
51 {"aes-256-cfb", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, 429},
52 {"aes-256-ofb", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, 428},
55 static bool nametocipher(const char *name, int *algo, int *mode) {
58 for(i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
59 if(ciphertable[i].name && !strcasecmp(name, ciphertable[i].name)) {
60 *algo = ciphertable[i].algo;
61 *mode = ciphertable[i].mode;
69 static bool nidtocipher(int nid, int *algo, int *mode) {
72 for(i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
73 if(nid == ciphertable[i].nid) {
74 *algo = ciphertable[i].algo;
75 *mode = ciphertable[i].mode;
83 static bool ciphertonid(int algo, int mode, int *nid) {
86 for(i = 0; i < sizeof(ciphertable) / sizeof(*ciphertable); i++) {
87 if(algo == ciphertable[i].algo && mode == ciphertable[i].mode) {
88 *nid = ciphertable[i].nid;
96 static bool cipher_open(cipher_t *cipher, int algo, int mode) {
99 if(!ciphertonid(algo, mode, &cipher->nid)) {
100 logger(DEBUG_ALWAYS, LOG_DEBUG, "Cipher %d mode %d has no corresponding nid!", algo, mode);
104 if((err = gcry_cipher_open(&cipher->handle, algo, mode, 0))) {
105 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unable to initialise cipher %d mode %d: %s", algo, mode, gcry_strerror(err));
109 cipher->keylen = gcry_cipher_get_algo_keylen(algo);
110 cipher->blklen = gcry_cipher_get_algo_blklen(algo);
111 cipher->key = xmalloc(cipher->keylen + cipher->blklen);
112 cipher->padding = mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC;
117 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
120 if(!nametocipher(name, &algo, &mode)) {
121 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown cipher name '%s'!", name);
125 return cipher_open(cipher, algo, mode);
128 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
131 if(!nidtocipher(nid, &algo, &mode)) {
132 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown cipher ID %d!", nid);
136 return cipher_open(cipher, algo, mode);
139 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
140 return cipher_open(cipher, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB);
143 void cipher_close(cipher_t *cipher) {
145 gcry_cipher_close(cipher->handle);
146 cipher->handle = NULL;
151 memset(cipher, 0, sizeof(*cipher));
154 size_t cipher_keylength(const cipher_t *cipher) {
159 return cipher->keylen + cipher->blklen;
162 uint64_t cipher_budget(const cipher_t *cipher) {
164 return UINT64_MAX; // NULL cipher
167 size_t ivlen = cipher->blklen;
168 size_t blklen = cipher->blklen;
170 size_t len = blklen > 1
172 : ivlen > 1 ? ivlen : 8;
173 size_t bits = len * 4 - 1;
176 ? UINT64_C(1) << bits
180 size_t cipher_blocksize(const cipher_t *cipher) {
181 if(!cipher || !cipher->blklen) {
185 return cipher->blklen;
188 void cipher_get_key(const cipher_t *cipher, void *key) {
189 memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
192 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
193 memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
195 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
196 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
201 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
203 key + len - cipher->keylen,
205 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
207 memcpy(cipher->key + cipher->keylen,
208 key + len - cipher->blklen - cipher->keylen,
210 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
215 bool cipher_regenerate_key(cipher_t *cipher, bool encrypt) {
216 gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
218 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
219 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
224 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
226 uint8_t pad[cipher->blklen];
228 if(cipher->padding) {
233 size_t reqlen = ((inlen + cipher->blklen) / cipher->blklen) * cipher->blklen;
235 if(*outlen < reqlen) {
236 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: not enough room for padding");
240 uint8_t padbyte = reqlen - inlen;
241 inlen = reqlen - cipher->blklen;
243 for(int i = 0; i < cipher->blklen; i++)
244 if(i < cipher->blklen - padbyte) {
245 pad[i] = ((uint8_t *)indata)[inlen + i];
252 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
255 if((err = gcry_cipher_encrypt(cipher->handle, outdata, *outlen, indata, inlen))) {
256 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", gcry_strerror(err));
260 if(cipher->padding) {
261 if((err = gcry_cipher_encrypt(cipher->handle, outdata + inlen, cipher->blklen, pad, cipher->blklen))) {
262 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting: %s", gcry_strerror(err));
266 inlen += cipher->blklen;
273 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
277 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
280 if((err = gcry_cipher_decrypt(cipher->handle, outdata, *outlen, indata, inlen))) {
281 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: %s", gcry_strerror(err));
285 if(cipher->padding) {
290 uint8_t padbyte = ((uint8_t *)outdata)[inlen - 1];
292 if(padbyte == 0 || padbyte > cipher->blklen || padbyte > inlen) {
293 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: invalid padding");
297 size_t origlen = inlen - padbyte;
299 for(size_t i = inlen - 1; i >= origlen; i--)
300 if(((uint8_t *)outdata)[i] != padbyte) {
301 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting: invalid padding");
313 int cipher_get_nid(const cipher_t *cipher) {
314 if(!cipher || !cipher->nid) {
321 bool cipher_active(const cipher_t *cipher) {
322 return cipher->nid != 0;