2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_common.h"
30 #include "gnunet_crypto_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36 * Create a new SessionKey (for symmetric encryption).
38 * @param key session key to initialize
41 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key)
43 gcry_randomize (key->aes_key,
44 GNUNET_CRYPTO_AES_KEY_LENGTH,
46 gcry_randomize (key->twofish_key,
47 GNUNET_CRYPTO_AES_KEY_LENGTH,
53 * Initialize AES cipher.
55 * @param handle handle to initialize
56 * @param sessionkey session key to use
57 * @param iv initialization vector to use
58 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
61 setup_cipher_aes (gcry_cipher_hd_t *handle,
62 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
63 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
68 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
69 GCRY_CIPHER_MODE_CFB, 0));
70 rc = gcry_cipher_setkey (*handle,
72 sizeof (sessionkey->aes_key));
73 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
74 rc = gcry_cipher_setiv (*handle,
77 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
83 * Initialize TWOFISH cipher.
85 * @param handle handle to initialize
86 * @param sessionkey session key to use
87 * @param iv initialization vector to use
88 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
91 setup_cipher_twofish (gcry_cipher_hd_t *handle,
92 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
93 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
98 gcry_cipher_open (handle, GCRY_CIPHER_TWOFISH,
99 GCRY_CIPHER_MODE_CFB, 0));
100 rc = gcry_cipher_setkey (*handle,
101 sessionkey->twofish_key,
102 sizeof (sessionkey->twofish_key));
103 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
104 rc = gcry_cipher_setiv (*handle,
106 sizeof (iv->twofish_iv));
107 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
113 * Encrypt a block with the public key of another
114 * host that uses the same cyper.
116 * @param block the block to encrypt
117 * @param len the size of the @a block
118 * @param sessionkey the key used to encrypt
119 * @param iv the initialization vector to use, use INITVALUE
121 * @param result the output parameter in which to store the encrypted result
122 * @returns the size of the encrypted block, -1 for errors
125 GNUNET_CRYPTO_symmetric_encrypt (const void *block, size_t len,
126 const struct GNUNET_CRYPTO_SymmetricSessionKey *
128 const struct GNUNET_CRYPTO_SymmetricInitializationVector *
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, len, block, len));
137 gcry_cipher_close (handle);
138 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
140 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, tmp, len));
141 gcry_cipher_close (handle);
142 memset (tmp, 0, sizeof (tmp));
148 * Decrypt a given block with the sessionkey.
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
155 * @param result address to store the result at
156 * @return -1 on failure, size of decrypted block on success
159 GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size,
160 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
161 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
164 gcry_cipher_hd_t handle;
167 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
169 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, block, size));
170 gcry_cipher_close (handle);
171 if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
173 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, tmp, size));
174 gcry_cipher_close (handle);
175 memset (tmp, 0, sizeof (tmp));
181 * @brief Derive an IV
183 * @param iv initialization vector
184 * @param skey session key
185 * @param salt salt for the derivation
186 * @param salt_len size of the @a salt
187 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
190 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
191 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
192 const void *salt, size_t salt_len, ...)
196 va_start (argp, salt_len);
197 GNUNET_CRYPTO_symmetric_derive_iv_v (iv, skey, salt, salt_len, argp);
203 * @brief Derive an IV
205 * @param iv initialization vector
206 * @param skey session key
207 * @param salt salt for the derivation
208 * @param salt_len size of the salt
209 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
212 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
213 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
214 const void *salt, size_t salt_len, va_list argp)
216 char aes_salt[salt_len + 4];
217 char twofish_salt[salt_len + 4];
219 memcpy (aes_salt, salt, salt_len);
220 memcpy (&aes_salt[salt_len], "AES!", 4);
221 memcpy (twofish_salt, salt, salt_len);
222 memcpy (&twofish_salt[salt_len], "FISH", 4);
223 GNUNET_CRYPTO_kdf_v (iv->aes_iv, sizeof (iv->aes_iv),
224 aes_salt, salt_len + 4,
225 skey->aes_key, sizeof (skey->aes_key),
227 GNUNET_CRYPTO_kdf_v (iv->twofish_iv, sizeof (iv->twofish_iv),
228 twofish_salt, salt_len + 4,
229 skey->twofish_key, sizeof (skey->twofish_key),
233 /* end of crypto_aes.c */