2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/crypto_symmetric.c
23 * @brief Symmetric encryption services; combined cipher AES+TWOFISH (256-bit each)
24 * @author Christian Grothoff
25 * @author Ioana Patrascu
29 #include "gnunet_crypto_lib.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-symmetric", __VA_ARGS__)
35 * Create a new SessionKey (for symmetric encryption).
37 * @param key session key to initialize
40 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key)
42 gcry_randomize (key->aes_key,
43 GNUNET_CRYPTO_AES_KEY_LENGTH,
45 gcry_randomize (key->twofish_key,
46 GNUNET_CRYPTO_AES_KEY_LENGTH,
52 * Initialize AES cipher.
54 * @param handle handle to initialize
55 * @param sessionkey session key to use
56 * @param iv initialization vector to use
57 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
60 setup_cipher_aes (gcry_cipher_hd_t *handle,
61 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
62 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
67 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
68 GCRY_CIPHER_MODE_CFB, 0));
69 rc = gcry_cipher_setkey (*handle,
71 sizeof (sessionkey->aes_key));
72 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
73 rc = gcry_cipher_setiv (*handle,
76 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
82 * Initialize TWOFISH cipher.
84 * @param handle handle to initialize
85 * @param sessionkey session key to use
86 * @param iv initialization vector to use
87 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
90 setup_cipher_twofish (gcry_cipher_hd_t *handle,
91 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
92 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
97 gcry_cipher_open (handle, GCRY_CIPHER_TWOFISH,
98 GCRY_CIPHER_MODE_CFB, 0));
99 rc = gcry_cipher_setkey (*handle,
100 sessionkey->twofish_key,
101 sizeof (sessionkey->twofish_key));
102 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
103 rc = gcry_cipher_setiv (*handle,
105 sizeof (iv->twofish_iv));
106 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
112 * Encrypt a block with a symmetric session key.
114 * @param block the block to encrypt
115 * @param size the size of the @a block
116 * @param sessionkey the key used to encrypt
117 * @param iv the initialization vector to use, use INITVALUE for streams
118 * @param result the output parameter in which to store the encrypted result
119 * can be the same or overlap with @c block
120 * @returns the size of the encrypted block, -1 for errors.
121 * Due to the use of CFB and therefore an effective stream cipher,
122 * this size should be the same as @c len.
125 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
127 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
128 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
131 gcry_cipher_hd_t handle;
134 if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
136 GNUNET_assert (0 == gcry_cipher_encrypt (handle, tmp, size, block, size));
137 gcry_cipher_close (handle);
138 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
140 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, size, tmp, size));
141 gcry_cipher_close (handle);
142 memset (tmp, 0, sizeof (tmp));
148 * Decrypt a given block with the session key.
150 * @param block the data to decrypt, encoded as returned by encrypt
151 * @param size the size of the @a block to decrypt
152 * @param sessionkey the key used to decrypt
153 * @param iv the initialization vector to use, use INITVALUE for streams
154 * @param result address to store the result at
155 * can be the same or overlap with @c block
156 * @return -1 on failure, size of decrypted block on success.
157 * Due to the use of CFB and therefore an effective stream cipher,
158 * this size should be the same as @c size.
161 GNUNET_CRYPTO_symmetric_decrypt (const void *block,
163 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
164 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
167 gcry_cipher_hd_t handle;
170 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
172 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, block, size));
173 gcry_cipher_close (handle);
174 if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
176 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, tmp, size));
177 gcry_cipher_close (handle);
178 memset (tmp, 0, sizeof (tmp));
184 * @brief Derive an IV
186 * @param iv initialization vector
187 * @param skey session key
188 * @param salt salt for the derivation
189 * @param salt_len size of the @a salt
190 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
193 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
194 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
201 va_start (argp, salt_len);
202 GNUNET_CRYPTO_symmetric_derive_iv_v (iv, skey, salt, salt_len, argp);
208 * @brief Derive an IV
210 * @param iv initialization vector
211 * @param skey session key
212 * @param salt salt for the derivation
213 * @param salt_len size of the salt
214 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
217 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
218 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
223 char aes_salt[salt_len + 4];
224 char twofish_salt[salt_len + 4];
226 GNUNET_memcpy (aes_salt, salt, salt_len);
227 GNUNET_memcpy (&aes_salt[salt_len], "AES!", 4);
228 GNUNET_memcpy (twofish_salt, salt, salt_len);
229 GNUNET_memcpy (&twofish_salt[salt_len], "FISH", 4);
230 GNUNET_CRYPTO_kdf_v (iv->aes_iv,
235 sizeof (skey->aes_key),
237 GNUNET_CRYPTO_kdf_v (iv->twofish_iv,
238 sizeof (iv->twofish_iv),
242 sizeof (skey->twofish_key),
246 /* end of crypto_symmetric.c */