src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / util / crypto_symmetric.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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_crypto_lib.h"
30 #include <gcrypt.h>
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-symmetric", __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,
162                                  size_t size,
163                                  const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
164                                  const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
165                                  void *result)
166 {
167   gcry_cipher_hd_t handle;
168   char tmp[size];
169
170   if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
171     return -1;
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))
175     return -1;
176   GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, tmp, size));
177   gcry_cipher_close (handle);
178   memset (tmp, 0, sizeof (tmp));
179   return size;
180 }
181
182
183 /**
184  * @brief Derive an IV
185  *
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
191  */
192 void
193 GNUNET_CRYPTO_symmetric_derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
194                                    const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
195                                    const void *salt,
196                                    size_t salt_len,
197                                    ...)
198 {
199   va_list argp;
200
201   va_start (argp, salt_len);
202   GNUNET_CRYPTO_symmetric_derive_iv_v (iv, skey, salt, salt_len, argp);
203   va_end (argp);
204 }
205
206
207 /**
208  * @brief Derive an IV
209  *
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
215  */
216 void
217 GNUNET_CRYPTO_symmetric_derive_iv_v (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
218                                      const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
219                                      const void *salt,
220                                      size_t salt_len,
221                                      va_list argp)
222 {
223   char aes_salt[salt_len + 4];
224   char twofish_salt[salt_len + 4];
225
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,
231                        sizeof (iv->aes_iv),
232                        aes_salt,
233                        salt_len + 4,
234                        skey->aes_key,
235                        sizeof (skey->aes_key),
236                        argp);
237   GNUNET_CRYPTO_kdf_v (iv->twofish_iv,
238                        sizeof (iv->twofish_iv),
239                        twofish_salt,
240                        salt_len + 4,
241                        skey->twofish_key,
242                        sizeof (skey->twofish_key),
243                        argp);
244 }
245
246 /* end of crypto_symmetric.c */