-removing 2nd argument from GNUNET_CLIENT_disconnect as it was virtually always GNUNE...
[oweals/gnunet.git] / src / util / crypto_aes.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006 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 2, 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_aes.c
23  * @brief Symmetric encryption services.
24  * @author Christian Grothoff
25  * @author Ioana Patrascu
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_crypto_lib.h"
31 #include <gcrypt.h>
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
35 /**
36  * Create a new SessionKey (for AES-256).
37  *
38  * @param key session key to initialize
39  */
40 void
41 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
42 {
43   gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH,
44                   GCRY_STRONG_RANDOM);
45   key->crc32 =
46       htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH));
47 }
48
49
50 /**
51  * Check that a new session key is well-formed.
52  *
53  * @return GNUNET_OK if the key is valid
54  */
55 int
56 GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
57                                      *key)
58 {
59   uint32_t crc;
60
61   crc = GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH);
62   if (ntohl (key->crc32) == crc)
63     return GNUNET_OK;
64   GNUNET_break_op (0);
65   return GNUNET_SYSERR;
66 }
67
68
69 /**
70  * Initialize AES cipher.
71  *
72  * @param handle handle to initialize
73  * @param sessionkey session key to use
74  * @param iv initialization vector to use
75  * @return GNUNET_OK on success, GNUNET_SYSERR on error
76  */
77 static int
78 setup_cipher (gcry_cipher_hd_t *handle,
79               const struct GNUNET_CRYPTO_AesSessionKey *
80               sessionkey,
81               const struct GNUNET_CRYPTO_AesInitializationVector *
82               iv)
83 {
84   int rc;
85
86   if (GNUNET_OK !=
87       GNUNET_CRYPTO_aes_check_session_key (sessionkey))
88   {
89     GNUNET_break (0);
90     return GNUNET_SYSERR;
91   }
92   GNUNET_assert (0 ==
93                  gcry_cipher_open (handle, GCRY_CIPHER_AES256,
94                                    GCRY_CIPHER_MODE_CFB, 0));
95   rc = gcry_cipher_setkey (*handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
96   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
97   rc = gcry_cipher_setiv (*handle, iv,
98                           sizeof (struct
99                                   GNUNET_CRYPTO_AesInitializationVector));
100   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
101   return GNUNET_OK;
102 }
103
104
105 /**
106  * Encrypt a block with the public key of another
107  * host that uses the same cyper.
108  *
109  * @param block the block to encrypt
110  * @param len the size of the block
111  * @param sessionkey the key used to encrypt
112  * @param iv the initialization vector to use, use INITVALUE
113  *        for streams.
114  * @param result the output parameter in which to store the encrypted result
115  * @returns the size of the encrypted block, -1 for errors
116  */
117 ssize_t
118 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
119                            const struct GNUNET_CRYPTO_AesSessionKey *
120                            sessionkey,
121                            const struct GNUNET_CRYPTO_AesInitializationVector *
122                            iv, void *result)
123 {
124   gcry_cipher_hd_t handle;
125
126   if (GNUNET_OK != setup_cipher (&handle, sessionkey, iv))
127     return -1;
128   GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len));
129   gcry_cipher_close (handle);
130   return len;
131 }
132
133
134 /**
135  * Decrypt a given block with the sessionkey.
136  *
137  * @param block the data to decrypt, encoded as returned by encrypt
138  * @param size the size of the block to decrypt
139  * @param sessionkey the key used to decrypt
140  * @param iv the initialization vector to use, use INITVALUE
141  *        for streams.
142  * @param result address to store the result at
143  * @return -1 on failure, size of decrypted block on success
144  */
145 ssize_t
146 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
147                            const struct GNUNET_CRYPTO_AesSessionKey *
148                            sessionkey,
149                            const struct GNUNET_CRYPTO_AesInitializationVector *
150                            iv, void *result)
151 {
152   gcry_cipher_hd_t handle;
153
154   if (GNUNET_OK != setup_cipher (&handle, sessionkey, iv))
155     return -1;
156   GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size));
157   gcry_cipher_close (handle);
158   return size;
159 }
160
161
162 /**
163  * @brief Derive an IV
164  *
165  * @param iv initialization vector
166  * @param skey session key
167  * @param salt salt for the derivation
168  * @param salt_len size of the salt
169  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
170  */
171 void
172 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
173                              const struct GNUNET_CRYPTO_AesSessionKey *skey,
174                              const void *salt, size_t salt_len, ...)
175 {
176   va_list argp;
177
178   va_start (argp, salt_len);
179   GNUNET_CRYPTO_aes_derive_iv_v (iv, skey, salt, salt_len, argp);
180   va_end (argp);
181 }
182
183
184 /**
185  * @brief Derive an IV
186  *
187  * @param iv initialization vector
188  * @param skey session key
189  * @param salt salt for the derivation
190  * @param salt_len size of the salt
191  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
192  */
193 void
194 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
195                                const struct GNUNET_CRYPTO_AesSessionKey *skey,
196                                const void *salt, size_t salt_len, va_list argp)
197 {
198   GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key,
199                        sizeof (skey->key), argp);
200 }
201
202 /* end of crypto_aes.c */