-towards addressing #3047, note this causes the code to FTBFS
[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 the public key of another
113  * host that uses the same cyper.
114  *
115  * @param block the block to encrypt
116  * @param len the size of the @a block
117  * @param sessionkey the key used to encrypt
118  * @param iv the initialization vector to use, use INITVALUE
119  *        for streams.
120  * @param result the output parameter in which to store the encrypted result
121  * @returns the size of the encrypted block, -1 for errors
122  */
123 ssize_t
124 GNUNET_CRYPTO_symmetric_encrypt (const void *block, size_t len,
125                            const struct GNUNET_CRYPTO_SymmetricSessionKey *
126                            sessionkey,
127                            const struct GNUNET_CRYPTO_SymmetricInitializationVector *
128                            iv, void *result)
129 {
130   gcry_cipher_hd_t handle;
131   char tmp[len];
132
133   if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
134     return -1;
135   GNUNET_assert (0 == gcry_cipher_encrypt (handle, tmp, len, block, len));
136   gcry_cipher_close (handle);
137   if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
138     return -1;
139   GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, tmp, len));
140   gcry_cipher_close (handle);
141   memset (tmp, 0, sizeof (tmp));
142   return len;
143 }
144
145
146 /**
147  * Decrypt a given block with the sessionkey.
148  *
149  * @param block the data to decrypt, encoded as returned by encrypt
150  * @param size the size of the @a block to decrypt
151  * @param sessionkey the key used to decrypt
152  * @param iv the initialization vector to use, use INITVALUE
153  *        for streams.
154  * @param result address to store the result at
155  * @return -1 on failure, size of decrypted block on success
156  */
157 ssize_t
158 GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size,
159                            const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
160                            const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, 
161                            void *result)
162 {
163   gcry_cipher_hd_t handle;
164   char tmp[size];
165
166   if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
167     return -1;
168   GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, block, size));
169   gcry_cipher_close (handle);
170   if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
171     return -1;
172   GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, tmp, size));
173   gcry_cipher_close (handle);
174   memset (tmp, 0, sizeof (tmp));
175   return size;
176 }
177
178
179 /**
180  * @brief Derive an IV
181  *
182  * @param iv initialization vector
183  * @param skey session key
184  * @param salt salt for the derivation
185  * @param salt_len size of the @a salt
186  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
187  */
188 void
189 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
190                              const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
191                              const void *salt, size_t salt_len, ...)
192 {
193   va_list argp;
194
195   va_start (argp, salt_len);
196   GNUNET_CRYPTO_symmetric_derive_iv_v (iv, skey, salt, salt_len, argp);
197   va_end (argp);
198 }
199
200
201 /**
202  * @brief Derive an IV
203  *
204  * @param iv initialization vector
205  * @param skey session key
206  * @param salt salt for the derivation
207  * @param salt_len size of the salt
208  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
209  */
210 void
211 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
212                                const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
213                                const void *salt, size_t salt_len, va_list argp)
214 {
215   char aes_salt[salt_len + 4];
216   char twofish_salt[salt_len + 4];
217
218   memcpy (aes_salt, salt, salt_len);
219   memcpy (&aes_salt[salt_len], "AES!", 4);
220   memcpy (twofish_salt, salt, salt_len);
221   memcpy (&twofish_salt[salt_len], "FISH", 4);
222   GNUNET_CRYPTO_kdf_v (iv->aes_iv, sizeof (iv->aes_iv), 
223                        aes_salt, salt_len + 4, 
224                        skey->aes_key, sizeof (skey->aes_key), 
225                        argp);
226   GNUNET_CRYPTO_kdf_v (iv->twofish_iv, sizeof (iv->twofish_iv),
227                        twofish_salt, salt_len + 4, 
228                        skey->twofish_key, sizeof (skey->twofish_key), 
229                        argp);
230 }
231
232 /* end of crypto_aes.c */