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 it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
18 * @file util/crypto_random.c
19 * @brief functions to gather random numbers
20 * @author Christian Grothoff
28 #include "gnunet_crypto_lib.h"
30 struct GNUNET_CRYPTO_AbeMasterKey
36 struct GNUNET_CRYPTO_AbeKey
43 init_aes( element_t k, int enc,
44 gcry_cipher_hd_t* handle,
45 struct GNUNET_CRYPTO_SymmetricSessionKey *key,
50 unsigned char* key_buf;
52 key_len = element_length_in_bytes(k) < 33 ? 3 : element_length_in_bytes(k);
53 key_buf = (unsigned char*) malloc(key_len);
54 element_to_bytes(key_buf, k);
56 GNUNET_memcpy (key->aes_key,
58 GNUNET_CRYPTO_AES_KEY_LENGTH);
60 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
61 GCRY_CIPHER_MODE_CFB, 0));
62 rc = gcry_cipher_setkey (*handle,
64 sizeof (key->aes_key));
65 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
66 memset (iv, 0, 16); //TODO make reasonable
67 rc = gcry_cipher_setiv (*handle,
70 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
77 aes_128_cbc_encrypt( char* pt,
82 gcry_cipher_hd_t handle;
83 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
89 init_aes(k, 1, &handle, &skey, iv);
91 /* TODO make less crufty */
93 /* stuff in real length (big endian) before padding */
94 len[0] = (size & 0xff000000)>>24;
95 len[1] = (size & 0xff0000)>>16;
96 len[2] = (size & 0xff00)>>8;
97 len[3] = (size & 0xff)>>0;
98 padding = 16 - ((4+size) % 16);
99 buf_size = 4 + size + padding;
100 buf = GNUNET_malloc (buf_size);
101 GNUNET_memcpy (buf, len, 4);
102 GNUNET_memcpy (buf+4, pt, size);
103 *ct = GNUNET_malloc (buf_size);
105 GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf, buf_size));
106 gcry_cipher_close (handle);
107 //AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
113 aes_128_cbc_decrypt( char* ct,
118 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
119 gcry_cipher_hd_t handle;
120 unsigned char iv[16];
124 init_aes(k, 1, &handle, &skey, iv);
126 tmp = GNUNET_malloc (size);
128 //AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
129 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
130 gcry_cipher_close (handle);
131 /* TODO make less crufty */
133 /* get real length */
136 | ((tmp[0])<<24) | ((tmp[1])<<16)
137 | ((tmp[2])<<8) | ((tmp[3])<<0);
138 /* truncate any garbage from the padding */
139 *pt = GNUNET_malloc (len);
140 GNUNET_memcpy (*pt, tmp+4, len);
145 struct GNUNET_CRYPTO_AbeMasterKey*
146 GNUNET_CRYPTO_cpabe_create_master_key (void)
148 struct GNUNET_CRYPTO_AbeMasterKey* key;
149 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
150 gabe_setup(&key->pub, &key->msk);
151 GNUNET_assert (NULL != key->pub);
152 GNUNET_assert (NULL != key->msk);
157 GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key)
159 gabe_msk_free (key->msk);
160 gabe_pub_free (key->pub);
161 //GNUNET_free (key->msk);
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);
179 GNUNET_assert (NULL != prv_key->prv);
184 GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key,
187 //Memory management in gabe is buggy
188 gabe_prv_free (key->prv);
189 if (GNUNET_YES == delete_pub)
190 gabe_pub_free (key->pub);
195 write_cpabe (void **result,
205 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
207 len = (uint32_t*) ptr;
208 *len = htonl (file_len);
210 len = (uint32_t*) ptr;
211 *len = htonl (aes_buf_len);
213 GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
215 len = (uint32_t*) ptr;
216 *len = htonl (cph_buf_len);
218 GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
219 return 12 + cph_buf_len + aes_buf_len;
223 read_cpabe (const void *data,
234 len = (uint32_t*)ptr;
235 buf_len = ntohl (*len);
237 len = (uint32_t*)ptr;
238 *aes_buf_len = ntohl (*len);
240 *aes_buf = GNUNET_malloc (*aes_buf_len);
241 GNUNET_memcpy (*aes_buf, ptr, *aes_buf_len);
243 len = (uint32_t*)ptr;
244 *cph_buf_len = ntohl (*len);
246 *cph_buf = GNUNET_malloc (*cph_buf_len);
247 GNUNET_memcpy (*cph_buf, ptr, *cph_buf_len);
253 GNUNET_CRYPTO_cpabe_encrypt (const void *block,
256 const struct GNUNET_CRYPTO_AbeMasterKey *key,
268 if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
269 return GNUNET_SYSERR;
270 cph_buf_len = gabe_cph_serialize(cph,
274 plt = GNUNET_memdup (block, size);
275 aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
278 result_len = write_cpabe(result, size, cph_buf, cph_buf_len, aes_buf, aes_buf_len);
279 GNUNET_free(cph_buf);
280 GNUNET_free(aes_buf);
285 GNUNET_CRYPTO_cpabe_decrypt (const void *block,
287 const struct GNUNET_CRYPTO_AbeKey *key,
298 read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
299 cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size);
300 if( !gabe_dec(key->pub, key->prv, cph, m) ) {
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "%s\n", gabe_error());
303 GNUNET_free (aes_buf);
304 GNUNET_free (cph_buf);
308 return GNUNET_SYSERR;
312 plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result);
313 GNUNET_free (cph_buf);
314 GNUNET_free (aes_buf);
316 //freeing is buggy in gabe
317 //gabe_prv_free (prv);
318 //gabe_pub_free (pub);
323 GNUNET_CRYPTO_cpabe_serialize_key (const struct GNUNET_CRYPTO_AbeKey *key,
332 pub_len = gabe_pub_serialize (key->pub, &pub);
333 prv_len = gabe_prv_serialize (key->prv, &prv);
335 len = pub_len + prv_len + 12;
336 write_cpabe (result, len, pub, pub_len, prv, prv_len);
344 struct GNUNET_CRYPTO_AbeKey*
345 GNUNET_CRYPTO_cpabe_deserialize_key (const void *data,
348 struct GNUNET_CRYPTO_AbeKey *key;
354 key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
360 key->pub = gabe_pub_unserialize (pub, pub_len);
361 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
369 GNUNET_CRYPTO_cpabe_serialize_master_key (const struct GNUNET_CRYPTO_AbeMasterKey *key,
378 pub_len = gabe_pub_serialize (key->pub, &pub);
379 msk_len = gabe_msk_serialize (key->msk, &msk);
381 len = pub_len + msk_len + 12;
382 write_cpabe (result, len, pub, pub_len, msk, msk_len);
390 struct GNUNET_CRYPTO_AbeMasterKey*
391 GNUNET_CRYPTO_cpabe_deserialize_master_key (const void *data,
394 struct GNUNET_CRYPTO_AbeMasterKey *key;
400 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
406 key->pub = gabe_pub_unserialize (pub, pub_len);
407 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);