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