2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file util/crypto_aes.c
23 * @brief Symmetric encryption services.
24 * @author Christian Grothoff
25 * @author Ioana Patrascu
29 #include "gnunet_common.h"
30 #include "gnunet_crypto_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36 * Create a new SessionKey (for AES-256).
38 * @param key session key to initialize
41 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
43 gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH,
46 htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH));
51 * Check that a new session key is well-formed.
53 * @return GNUNET_OK if the key is valid
56 GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
61 crc = GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH);
62 if (ntohl (key->crc32) != crc)
72 * Initialize AES cipher.
74 * @param handle handle to initialize
75 * @param sessionkey session key to use
76 * @param iv initialization vector to use
77 * @return GNUNET_OK on success, GNUNET_SYSERR on error
80 setup_cipher (gcry_cipher_hd_t *handle,
81 const struct GNUNET_CRYPTO_AesSessionKey *
83 const struct GNUNET_CRYPTO_AesInitializationVector *
89 GNUNET_CRYPTO_aes_check_session_key (sessionkey))
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,
101 GNUNET_CRYPTO_AesInitializationVector));
102 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
108 * Encrypt a block with the public key of another
109 * host that uses the same cyper.
111 * @param block the block to encrypt
112 * @param len the size of the block
113 * @param sessionkey the key used to encrypt
114 * @param iv the initialization vector to use, use INITVALUE
116 * @param result the output parameter in which to store the encrypted result
117 * @returns the size of the encrypted block, -1 for errors
120 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
121 const struct GNUNET_CRYPTO_AesSessionKey *
123 const struct GNUNET_CRYPTO_AesInitializationVector *
126 gcry_cipher_hd_t handle;
128 if (GNUNET_OK != setup_cipher (&handle, sessionkey, iv))
130 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len));
131 gcry_cipher_close (handle);
137 * Decrypt a given block with the sessionkey.
139 * @param block the data to decrypt, encoded as returned by encrypt
140 * @param size the size of the block to decrypt
141 * @param sessionkey the key used to decrypt
142 * @param iv the initialization vector to use, use INITVALUE
144 * @param result address to store the result at
145 * @return -1 on failure, size of decrypted block on success
148 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
149 const struct GNUNET_CRYPTO_AesSessionKey *
151 const struct GNUNET_CRYPTO_AesInitializationVector *
154 gcry_cipher_hd_t handle;
156 if (GNUNET_OK != setup_cipher (&handle, sessionkey, iv))
158 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size));
159 gcry_cipher_close (handle);
165 * @brief Derive an IV
167 * @param iv initialization vector
168 * @param skey session key
169 * @param salt salt for the derivation
170 * @param salt_len size of the salt
171 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
174 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
175 const struct GNUNET_CRYPTO_AesSessionKey *skey,
176 const void *salt, size_t salt_len, ...)
180 va_start (argp, salt_len);
181 GNUNET_CRYPTO_aes_derive_iv_v (iv, skey, salt, salt_len, argp);
187 * @brief Derive an IV
189 * @param iv initialization vector
190 * @param skey session key
191 * @param salt salt for the derivation
192 * @param salt_len size of the salt
193 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
196 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
197 const struct GNUNET_CRYPTO_AesSessionKey *skey,
198 const void *salt, size_t salt_len, va_list argp)
200 GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key,
201 sizeof (skey->key), argp);
204 /* end of crypto_aes.c */