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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 {"none", GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0},
36 {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 92},
37 {"blowfish", GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC, 91},
38 {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CFB, 93},
39 {NULL, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB, 94},
41 {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 418},
42 {"aes", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 419},
43 {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 421},
44 {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_OFB, 420},
46 {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_ECB, 422},
47 {"aes192", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC, 423},
48 {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CFB, 425},
49 {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_OFB, 424},
51 {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 426},
52 {"aes256", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 427},
53 {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, 429},
54 {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, 428},
57 static bool nametocipher(const char *name, int *algo, int *mode) {
60 for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
61 if(ciphertable[i].name && !strcasecmp(name, ciphertable[i].name)) {
62 *algo = ciphertable[i].algo;
63 *mode = ciphertable[i].mode;
71 static bool nidtocipher(int nid, int *algo, int *mode) {
74 for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
75 if(nid == ciphertable[i].nid) {
76 *algo = ciphertable[i].algo;
77 *mode = ciphertable[i].mode;
85 static bool ciphertonid(int algo, int mode, int *nid) {
88 for(i = 0; i < sizeof ciphertable / sizeof *ciphertable; i++) {
89 if(algo == ciphertable[i].algo && mode == ciphertable[i].mode) {
90 *nid = ciphertable[i].nid;
98 static bool cipher_open(cipher_t *cipher, int algo, int mode) {
101 if(!ciphertonid(algo, mode, &cipher->nid)) {
102 logger(LOG_DEBUG, _("Cipher %d mode %d has no corresponding nid!"), algo, mode);
106 if((err = gcry_cipher_open(&cipher->handle, algo, mode, 0))) {
107 logger(LOG_DEBUG, _("Unable to intialise cipher %d mode %d: %s"), algo, mode, gcry_strerror(err));
111 cipher->keylen = gcry_cipher_get_algo_keylen(algo);
112 if(mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC)
113 cipher->blklen = gcry_cipher_get_algo_blklen(algo);
116 cipher->key = xmalloc(cipher->keylen + cipher->blklen);
121 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
124 if(!nametocipher(name, &algo, &mode)) {
125 logger(LOG_DEBUG, _("Unknown cipher name '%s'!"), name);
129 return cipher_open(cipher, algo, mode);
132 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
135 if(!nidtocipher(nid, &algo, &mode)) {
136 logger(LOG_DEBUG, _("Unknown cipher ID %d!"), nid);
140 return cipher_open(cipher, algo, mode);
143 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
144 return cipher_open(cipher, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB);
147 void cipher_close(cipher_t *cipher) {
149 gcry_cipher_close(cipher->handle);
150 cipher->handle = NULL;
159 size_t cipher_keylength(const cipher_t *cipher) {
160 return cipher->keylen + cipher->blklen;
163 void cipher_get_key(const cipher_t *cipher, void *key) {
164 memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
167 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
168 memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
170 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
171 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
176 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
177 memcpy(cipher->key, key + len - cipher->keylen, cipher->keylen + cipher->blklen);
178 memcpy(cipher->key + cipher->keylen, key + len - cipher->keylen - cipher->blklen, cipher->blklen);
180 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
181 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
186 bool cipher_regenerate_key(cipher_t *cipher, bool encrypt) {
187 gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
189 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
190 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
195 static bool cipher_add_padding(cipher_t *cipher, void *indata, size_t inlen, size_t *outlen) {
198 if(cipher->blklen == 1) {
203 reqlen = ((inlen + 1) / cipher->blklen) * cipher->blklen;
213 static bool cipher_remove_padding(cipher_t *cipher, void *indata, size_t inlen, size_t *outlen) {
216 if(cipher->blklen == 1) {
221 if(inlen % cipher->blklen)
224 // check and remove padding
230 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
235 if((err = gcry_cipher_encrypt(cipher->handle, outdata, inlen, indata, inlen))) {
236 logger(LOG_ERR, _("Error while encrypting: %s"), gcry_strerror(err));
243 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
248 if((err = gcry_cipher_decrypt(cipher->handle, outdata, inlen, indata, inlen))) {
249 logger(LOG_ERR, _("Error while decrypting: %s"), gcry_strerror(err));
256 int cipher_get_nid(const cipher_t *cipher) {
260 bool cipher_active(const cipher_t *cipher) {
261 return cipher->nid != 0;