-remove async ecc key generation, not needed
[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 }
46
47
48 /**
49  * Initialize AES cipher.
50  *
51  * @param handle handle to initialize
52  * @param sessionkey session key to use
53  * @param iv initialization vector to use
54  * @return GNUNET_OK on success, GNUNET_SYSERR on error
55  */
56 static int
57 setup_cipher (gcry_cipher_hd_t *handle,
58               const struct GNUNET_CRYPTO_AesSessionKey *
59               sessionkey,
60               const struct GNUNET_CRYPTO_AesInitializationVector *
61               iv)
62 {
63   int rc;
64
65   GNUNET_assert (0 ==
66                  gcry_cipher_open (handle, GCRY_CIPHER_AES256,
67                                    GCRY_CIPHER_MODE_CFB, 0));
68   rc = gcry_cipher_setkey (*handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
69   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
70   rc = gcry_cipher_setiv (*handle, iv,
71                           sizeof (struct
72                                   GNUNET_CRYPTO_AesInitializationVector));
73   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
74   return GNUNET_OK;
75 }
76
77
78 /**
79  * Encrypt a block with the public key of another
80  * host that uses the same cyper.
81  *
82  * @param block the block to encrypt
83  * @param len the size of the block
84  * @param sessionkey the key used to encrypt
85  * @param iv the initialization vector to use, use INITVALUE
86  *        for streams.
87  * @param result the output parameter in which to store the encrypted result
88  * @returns the size of the encrypted block, -1 for errors
89  */
90 ssize_t
91 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
92                            const struct GNUNET_CRYPTO_AesSessionKey *
93                            sessionkey,
94                            const struct GNUNET_CRYPTO_AesInitializationVector *
95                            iv, void *result)
96 {
97   gcry_cipher_hd_t handle;
98
99   if (GNUNET_OK != setup_cipher (&handle, sessionkey, iv))
100     return -1;
101   GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len));
102   gcry_cipher_close (handle);
103   return len;
104 }
105
106
107 /**
108  * Decrypt a given block with the sessionkey.
109  *
110  * @param block the data to decrypt, encoded as returned by encrypt
111  * @param size the size of the block to decrypt
112  * @param sessionkey the key used to decrypt
113  * @param iv the initialization vector to use, use INITVALUE
114  *        for streams.
115  * @param result address to store the result at
116  * @return -1 on failure, size of decrypted block on success
117  */
118 ssize_t
119 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
120                            const struct GNUNET_CRYPTO_AesSessionKey *
121                            sessionkey,
122                            const struct GNUNET_CRYPTO_AesInitializationVector *
123                            iv, void *result)
124 {
125   gcry_cipher_hd_t handle;
126
127   if (GNUNET_OK != setup_cipher (&handle, sessionkey, iv))
128     return -1;
129   GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size));
130   gcry_cipher_close (handle);
131   return size;
132 }
133
134
135 /**
136  * @brief Derive an IV
137  *
138  * @param iv initialization vector
139  * @param skey session key
140  * @param salt salt for the derivation
141  * @param salt_len size of the salt
142  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
143  */
144 void
145 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
146                              const struct GNUNET_CRYPTO_AesSessionKey *skey,
147                              const void *salt, size_t salt_len, ...)
148 {
149   va_list argp;
150
151   va_start (argp, salt_len);
152   GNUNET_CRYPTO_aes_derive_iv_v (iv, skey, salt, salt_len, argp);
153   va_end (argp);
154 }
155
156
157 /**
158  * @brief Derive an IV
159  *
160  * @param iv initialization vector
161  * @param skey session key
162  * @param salt salt for the derivation
163  * @param salt_len size of the salt
164  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
165  */
166 void
167 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
168                                const struct GNUNET_CRYPTO_AesSessionKey *skey,
169                                const void *salt, size_t salt_len, va_list argp)
170 {
171   GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key,
172                        sizeof (skey->key), argp);
173 }
174
175 /* end of crypto_aes.c */