2 This file is part of GNUnet. Copyright (C) 2001-2014 Christian Grothoff
3 (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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @file util/crypto_random.c
24 * @brief functions to gather random numbers
25 * @author Christian Grothoff
33 #include "gnunet_crypto_lib.h"
35 struct GNUNET_CRYPTO_AbeMasterKey
41 struct GNUNET_CRYPTO_AbeKey
48 init_aes( element_t k, int enc,
49 gcry_cipher_hd_t* handle,
50 struct GNUNET_CRYPTO_SymmetricSessionKey *key,
55 unsigned char* key_buf;
57 key_len = element_length_in_bytes(k) < 33 ? 3 : element_length_in_bytes(k);
58 key_buf = (unsigned char*) malloc(key_len);
59 element_to_bytes(key_buf, k);
61 memcpy (key->aes_key, key_buf, GNUNET_CRYPTO_AES_KEY_LENGTH);
63 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
64 GCRY_CIPHER_MODE_CFB, 0));
65 rc = gcry_cipher_setkey (*handle,
67 sizeof (key->aes_key));
68 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
69 memset (iv, 0, 16); //TODO make reasonable
70 rc = gcry_cipher_setiv (*handle,
73 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
80 aes_128_cbc_encrypt( char* pt,
85 gcry_cipher_hd_t handle;
86 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
92 init_aes(k, 1, &handle, &skey, iv);
94 /* TODO make less crufty */
96 /* stuff in real length (big endian) before padding */
97 len[0] = (size & 0xff000000)>>24;
98 len[1] = (size & 0xff0000)>>16;
99 len[2] = (size & 0xff00)>>8;
100 len[3] = (size & 0xff)>>0;
101 padding = 16 - ((4+size) % 16);
102 buf_size = 4 + size + padding;
103 buf = GNUNET_malloc (buf_size);
104 GNUNET_memcpy (buf, len, 4);
105 GNUNET_memcpy (buf+4, pt, size);
106 *ct = GNUNET_malloc (buf_size);
108 GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf, buf_size));
109 gcry_cipher_close (handle);
110 //AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
116 aes_128_cbc_decrypt( char* ct,
121 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
122 gcry_cipher_hd_t handle;
123 unsigned char iv[16];
127 init_aes(k, 1, &handle, &skey, iv);
129 tmp = GNUNET_malloc (size);
131 //AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
132 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
133 gcry_cipher_close (handle);
134 /* TODO make less crufty */
136 /* get real length */
139 | ((tmp[0])<<24) | ((tmp[1])<<16)
140 | ((tmp[2])<<8) | ((tmp[3])<<0);
141 /* truncate any garbage from the padding */
142 *pt = GNUNET_malloc (len);
143 GNUNET_memcpy (*pt, tmp+4, len);
148 struct GNUNET_CRYPTO_AbeMasterKey*
149 GNUNET_CRYPTO_cpabe_create_master_key (void)
151 struct GNUNET_CRYPTO_AbeMasterKey* key;
152 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
153 gabe_setup(&key->pub, &key->msk);
154 GNUNET_assert (NULL != key->pub);
155 GNUNET_assert (NULL != key->msk);
160 GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key)
162 gabe_msk_free (key->msk); //For some reason free of pub implicit?
166 struct GNUNET_CRYPTO_AbeKey*
167 GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key,
170 struct GNUNET_CRYPTO_AbeKey *prv_key;
174 prv_key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
175 prv_key->prv = gabe_keygen(key->pub, key->msk, attrs);
176 size = gabe_pub_serialize(key->pub, &tmp);
177 prv_key->pub = gabe_pub_unserialize(tmp, size);
178 GNUNET_assert (NULL != prv_key->prv);
183 GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key)
185 //Memory management in gabe is buggy
186 //gabe_prv_free (prv);
191 write_cpabe (void **result,
201 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
203 len = (uint32_t*) ptr;
204 *len = htonl (file_len);
206 len = (uint32_t*) ptr;
207 *len = htonl (aes_buf_len);
209 memcpy (ptr, aes_buf, aes_buf_len);
211 len = (uint32_t*) ptr;
212 *len = htonl (cph_buf_len);
214 memcpy (ptr, cph_buf, cph_buf_len);
215 return 12 + cph_buf_len + aes_buf_len;
219 read_cpabe (const void *data,
230 len = (uint32_t*)ptr;
231 buf_len = ntohl (*len);
233 len = (uint32_t*)ptr;
234 *aes_buf_len = ntohl (*len);
236 *aes_buf = GNUNET_malloc (*aes_buf_len);
237 memcpy(*aes_buf, ptr, *aes_buf_len);
239 len = (uint32_t*)ptr;
240 *cph_buf_len = ntohl (*len);
242 *cph_buf = GNUNET_malloc (*cph_buf_len);
243 memcpy(*cph_buf, ptr, *cph_buf_len);
249 GNUNET_CRYPTO_cpabe_encrypt (const void *block,
252 const struct GNUNET_CRYPTO_AbeMasterKey *key,
264 if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
265 return GNUNET_SYSERR;
266 cph_buf_len = gabe_cph_serialize(cph,
269 plt = GNUNET_memdup (block, size);
270 aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
273 result_len = write_cpabe(result, size, cph_buf, cph_buf_len, aes_buf, aes_buf_len);
274 GNUNET_free(cph_buf);
275 GNUNET_free(aes_buf);
280 GNUNET_CRYPTO_cpabe_decrypt (const void *block,
282 const struct GNUNET_CRYPTO_AbeKey *key,
293 read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
294 cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size);
295 if( !gabe_dec(key->pub, key->prv, cph, m) ) {
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "%s\n", gabe_error());
299 return GNUNET_SYSERR;
302 plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result);
303 GNUNET_free (aes_buf);
304 //freeing is buggy in gabe
305 //gabe_prv_free (prv);
306 //gabe_pub_free (pub);
311 GNUNET_CRYPTO_cpabe_serialize_key (const struct GNUNET_CRYPTO_AbeKey *key,
320 pub_len = gabe_pub_serialize (key->pub, &pub);
321 prv_len = gabe_prv_serialize (key->prv, &prv);
323 len = pub_len + prv_len + 12;
324 write_cpabe (result, len, pub, pub_len, prv, prv_len);
332 struct GNUNET_CRYPTO_AbeKey*
333 GNUNET_CRYPTO_cpabe_deserialize_key (const void *data,
336 struct GNUNET_CRYPTO_AbeKey *key;
342 key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
348 key->pub = gabe_pub_unserialize (pub, pub_len);
349 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
357 GNUNET_CRYPTO_cpabe_serialize_master_key (const struct GNUNET_CRYPTO_AbeMasterKey *key,
366 pub_len = gabe_pub_serialize (key->pub, &pub);
367 msk_len = gabe_msk_serialize (key->msk, &msk);
369 len = pub_len + msk_len + 12;
370 write_cpabe (result, len, pub, pub_len, msk, msk_len);
378 struct GNUNET_CRYPTO_AbeMasterKey*
379 GNUNET_CRYPTO_cpabe_deserialize_master_key (const void *data,
382 struct GNUNET_CRYPTO_AbeMasterKey *key;
388 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
394 key->pub = gabe_pub_unserialize (pub, pub_len);
395 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);