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.
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 {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 418},
40 {"aes", GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 419},
41 {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CFB, 421},
42 {NULL, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_OFB, 420},
44 {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_ECB, 422},
45 {"aes192", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC, 423},
46 {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CFB, 425},
47 {NULL, GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_OFB, 424},
49 {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 426},
50 {"aes256", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 427},
51 {NULL, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB, 429},
52 {NULL, 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(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(LOG_DEBUG, "Unable to intialise cipher %d mode %d: %s", algo, mode, gcry_strerror(err));
109 cipher->keylen = gcry_cipher_get_algo_keylen(algo);
110 if(mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC)
111 cipher->blklen = gcry_cipher_get_algo_blklen(algo);
114 cipher->key = xmalloc(cipher->keylen + cipher->blklen);
119 bool cipher_open_by_name(cipher_t *cipher, const char *name) {
122 if(!nametocipher(name, &algo, &mode)) {
123 logger(LOG_DEBUG, "Unknown cipher name '%s'!", name);
127 return cipher_open(cipher, algo, mode);
130 bool cipher_open_by_nid(cipher_t *cipher, int nid) {
133 if(!nidtocipher(nid, &algo, &mode)) {
134 logger(LOG_DEBUG, "Unknown cipher ID %d!", nid);
138 return cipher_open(cipher, algo, mode);
141 bool cipher_open_blowfish_ofb(cipher_t *cipher) {
142 return cipher_open(cipher, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB);
145 void cipher_close(cipher_t *cipher) {
147 gcry_cipher_close(cipher->handle);
148 cipher->handle = NULL;
157 size_t cipher_keylength(const cipher_t *cipher) {
158 return cipher->keylen + cipher->blklen;
161 void cipher_get_key(const cipher_t *cipher, void *key) {
162 memcpy(key, cipher->key, cipher->keylen + cipher->blklen);
165 bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
166 memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
168 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
169 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
174 bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
175 memcpy(cipher->key, key + len - cipher->keylen, cipher->keylen + cipher->blklen);
176 memcpy(cipher->key + cipher->keylen, key + len - cipher->keylen - cipher->blklen, cipher->blklen);
178 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
179 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
184 bool cipher_regenerate_key(cipher_t *cipher, bool encrypt) {
185 gcry_create_nonce(cipher->key, cipher->keylen + cipher->blklen);
187 gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
188 gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);
193 static bool cipher_add_padding(cipher_t *cipher, void *indata, size_t inlen, size_t *outlen) {
196 if(cipher->blklen == 1) {
201 reqlen = ((inlen + 1) / cipher->blklen) * cipher->blklen;
211 static bool cipher_remove_padding(cipher_t *cipher, void *indata, size_t inlen, size_t *outlen) {
214 if(cipher->blklen == 1) {
219 if(inlen % cipher->blklen)
222 // check and remove padding
228 bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
233 if((err = gcry_cipher_encrypt(cipher->handle, outdata, inlen, indata, inlen))) {
234 logger(LOG_ERR, "Error while encrypting: %s", gcry_strerror(err));
241 bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *outdata, size_t *outlen, bool oneshot) {
246 if((err = gcry_cipher_decrypt(cipher->handle, outdata, inlen, indata, inlen))) {
247 logger(LOG_ERR, "Error while decrypting: %s", gcry_strerror(err));
254 int cipher_get_nid(const cipher_t *cipher) {
258 bool cipher_active(const cipher_t *cipher) {
259 return cipher->nid != 0;