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"
34 * Create a new SessionKey (for AES-256).
37 GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
39 gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH,
42 htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH));
46 * Check that a new session key is well-formed.
48 * @return GNUNET_OK if the key is valid
51 GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
56 crc = GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH);
57 if (ntohl (key->crc32) == crc)
65 * Encrypt a block with the public key of another
66 * host that uses the same cyper.
67 * @param block the block to encrypt
68 * @param len the size of the block
69 * @param sessionkey the key used to encrypt
70 * @param iv the initialization vector to use, use INITVALUE
72 * @param result the output parameter in which to store the encrypted result
73 * @returns the size of the encrypted block, -1 for errors
76 GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
77 const struct GNUNET_CRYPTO_AesSessionKey *
79 const struct GNUNET_CRYPTO_AesInitializationVector *
82 gcry_cipher_hd_t handle;
85 if (sessionkey->crc32 !=
86 htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
92 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
93 GCRY_CIPHER_MODE_CFB, 0));
94 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
95 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
96 rc = gcry_cipher_setiv (handle, iv,
98 GNUNET_CRYPTO_AesInitializationVector));
99 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
100 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len));
101 gcry_cipher_close (handle);
106 * Decrypt a given block with the sessionkey.
108 * @param block the data to decrypt, encoded as returned by encrypt
109 * @param size the size of the block to decrypt
110 * @param sessionkey the key used to decrypt
111 * @param iv the initialization vector to use, use INITVALUE
113 * @param result address to store the result at
114 * @return -1 on failure, size of decrypted block on success
117 GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
118 const struct GNUNET_CRYPTO_AesSessionKey *
120 const struct GNUNET_CRYPTO_AesInitializationVector *
123 gcry_cipher_hd_t handle;
126 if (sessionkey->crc32 !=
127 htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
133 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
134 GCRY_CIPHER_MODE_CFB, 0));
135 rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
136 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
137 rc = gcry_cipher_setiv (handle, iv,
139 GNUNET_CRYPTO_AesInitializationVector));
140 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
141 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size));
142 gcry_cipher_close (handle);
147 * @brief Derive an IV
148 * @param iv initialization vector
149 * @param skey session key
150 * @param salt salt for the derivation
151 * @param salt_len size of the salt
152 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
155 GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
156 const struct GNUNET_CRYPTO_AesSessionKey *skey,
157 const void *salt, size_t salt_len, ...)
161 va_start (argp, salt_len);
162 GNUNET_CRYPTO_aes_derive_iv_v (iv, skey, salt, salt_len, argp);
167 * @brief Derive an IV
168 * @param iv initialization vector
169 * @param skey session key
170 * @param salt salt for the derivation
171 * @param salt_len size of the salt
172 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
175 GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
176 const struct GNUNET_CRYPTO_AesSessionKey *skey,
177 const void *salt, size_t salt_len, va_list argp)
179 GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key,
180 sizeof (skey->key), argp);
183 /* end of crypto_aes.c */