-fix
[oweals/gnunet.git] / src / util / crypto_symmetric.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
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
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include <gcrypt.h>
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33
34 /**
35  * Create a new SessionKey (for symmetric encryption).
36  *
37  * @param key session key to initialize
38  */
39 void
40 GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key)
41 {
42   gcry_randomize (key->aes_key,
43                   GNUNET_CRYPTO_AES_KEY_LENGTH,
44                   GCRY_STRONG_RANDOM);
45   gcry_randomize (key->twofish_key,
46                   GNUNET_CRYPTO_AES_KEY_LENGTH,
47                   GCRY_STRONG_RANDOM);
48 }
49
50
51 /**
52  * Initialize AES cipher.
53  *
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
58  */
59 static int
60 setup_cipher_aes (gcry_cipher_hd_t *handle,
61                   const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
62                   const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
63 {
64   int rc;
65
66   GNUNET_assert (0 ==
67                  gcry_cipher_open (handle, GCRY_CIPHER_AES256,
68                                    GCRY_CIPHER_MODE_CFB, 0));
69   rc = gcry_cipher_setkey (*handle,
70                            sessionkey->aes_key,
71                            sizeof (sessionkey->aes_key));
72   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
73   rc = gcry_cipher_setiv (*handle,
74                           iv->aes_iv,
75                           sizeof (iv->aes_iv));
76   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
77   return GNUNET_OK;
78 }
79
80
81 /**
82  * Initialize TWOFISH cipher.
83  *
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
88  */
89 static int
90 setup_cipher_twofish (gcry_cipher_hd_t *handle,
91                       const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
92                       const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
93 {
94   int rc;
95
96   GNUNET_assert (0 ==
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,
104                           iv->twofish_iv,
105                           sizeof (iv->twofish_iv));
106   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
107   return GNUNET_OK;
108 }
109
110
111 /**
112  * Encrypt a block with a symmetric session key.
113  *
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.
123  */
124 ssize_t
125 GNUNET_CRYPTO_symmetric_encrypt (const void *block,
126                                  size_t size,
127                                  const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
128                                  const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
129                                  void *result)
130 {
131   gcry_cipher_hd_t handle;
132   char tmp[size];
133
134   if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
135     return -1;
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))
139     return -1;
140   GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, size, tmp, size));
141   gcry_cipher_close (handle);
142   memset (tmp, 0, sizeof (tmp));
143   return size;
144 }
145
146
147 /**
148  * Decrypt a given block with the session key.
149  *
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.
159  */
160 ssize_t
161 GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size,
162                                  const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
163                                  const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
164                                  void *result)
165 {
166   gcry_cipher_hd_t handle;
167   char tmp[size];
168
169   if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
170     return -1;
171   GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, block, size));
172   gcry_cipher_close (handle);
173   if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
174     return -1;
175   GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, tmp, size));
176   gcry_cipher_close (handle);
177   memset (tmp, 0, sizeof (tmp));
178   return size;
179 }
180
181
182 /**
183  * @brief Derive an IV
184  *
185  * @param iv initialization vector
186  * @param skey session key
187  * @param salt salt for the derivation
188  * @param salt_len size of the @a salt
189  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
190  */
191 void
192 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
193                              const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
194                              const void *salt, size_t salt_len, ...)
195 {
196   va_list argp;
197
198   va_start (argp, salt_len);
199   GNUNET_CRYPTO_symmetric_derive_iv_v (iv, skey, salt, salt_len, argp);
200   va_end (argp);
201 }
202
203
204 /**
205  * @brief Derive an IV
206  *
207  * @param iv initialization vector
208  * @param skey session key
209  * @param salt salt for the derivation
210  * @param salt_len size of the salt
211  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
212  */
213 void
214 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
215                                const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
216                                const void *salt, size_t salt_len, va_list argp)
217 {
218   char aes_salt[salt_len + 4];
219   char twofish_salt[salt_len + 4];
220
221   memcpy (aes_salt, salt, salt_len);
222   memcpy (&aes_salt[salt_len], "AES!", 4);
223   memcpy (twofish_salt, salt, salt_len);
224   memcpy (&twofish_salt[salt_len], "FISH", 4);
225   GNUNET_CRYPTO_kdf_v (iv->aes_iv, sizeof (iv->aes_iv),
226                        aes_salt, salt_len + 4,
227                        skey->aes_key, sizeof (skey->aes_key),
228                        argp);
229   GNUNET_CRYPTO_kdf_v (iv->twofish_iv, sizeof (iv->twofish_iv),
230                        twofish_salt, salt_len + 4,
231                        skey->twofish_key, sizeof (skey->twofish_key),
232                        argp);
233 }
234
235 /* end of crypto_aes.c */