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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 GNUNET_memcpy (key->aes_key,
63 GNUNET_CRYPTO_AES_KEY_LENGTH);
65 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
66 GCRY_CIPHER_MODE_CFB, 0));
67 rc = gcry_cipher_setkey (*handle,
69 sizeof(key->aes_key));
70 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
71 memset (iv, 0, 16); // TODO make reasonable
72 rc = gcry_cipher_setiv (*handle,
75 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
83 aes_128_cbc_encrypt (char*pt,
88 gcry_cipher_hd_t handle;
89 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
96 init_aes (k, 1, &handle, &skey, iv);
98 /* TODO make less crufty */
100 /* stuff in real length (big endian) before padding */
101 len[0] = (size & 0xff000000) >> 24;
102 len[1] = (size & 0xff0000) >> 16;
103 len[2] = (size & 0xff00) >> 8;
104 len[3] = (size & 0xff) >> 0;
105 padding = 16 - ((4 + size) % 16);
106 buf_size = 4 + size + padding;
107 buf = GNUNET_malloc (buf_size);
108 GNUNET_memcpy (buf, len, 4);
109 GNUNET_memcpy (buf + 4, pt, size);
110 *ct = GNUNET_malloc (buf_size);
112 GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf,
114 gcry_cipher_close (handle);
115 // AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
122 aes_128_cbc_decrypt (char*ct,
127 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
128 gcry_cipher_hd_t handle;
129 unsigned char iv[16];
133 init_aes (k, 1, &handle, &skey, iv);
135 tmp = GNUNET_malloc (size);
137 // AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
138 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
139 gcry_cipher_close (handle);
140 /* TODO make less crufty */
142 /* get real length */
145 | ((tmp[0]) << 24) | ((tmp[1]) << 16)
146 | ((tmp[2]) << 8) | ((tmp[3]) << 0);
147 /* truncate any garbage from the padding */
148 *pt = GNUNET_malloc (len);
149 GNUNET_memcpy (*pt, tmp + 4, len);
155 struct GNUNET_CRYPTO_AbeMasterKey*
156 GNUNET_CRYPTO_cpabe_create_master_key (void)
158 struct GNUNET_CRYPTO_AbeMasterKey*key;
160 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
161 gabe_setup (&key->pub, &key->msk);
162 GNUNET_assert (NULL != key->pub);
163 GNUNET_assert (NULL != key->msk);
169 GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key)
171 gabe_msk_free (key->msk);
172 gabe_pub_free (key->pub);
173 // GNUNET_free (key->msk);
174 // gabe_msk_free (key->msk); //For some reason free of pub implicit?
179 struct GNUNET_CRYPTO_AbeKey*
180 GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key,
183 struct GNUNET_CRYPTO_AbeKey *prv_key;
187 prv_key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
188 prv_key->prv = gabe_keygen (key->pub, key->msk, attrs);
189 size = gabe_pub_serialize (key->pub, &tmp);
190 prv_key->pub = gabe_pub_unserialize (tmp, size);
192 GNUNET_assert (NULL != prv_key->prv);
198 GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key,
201 // Memory management in gabe is buggy
202 gabe_prv_free (key->prv);
203 if (GNUNET_YES == delete_pub)
204 gabe_pub_free (key->pub);
210 write_cpabe (void **result,
220 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
222 len = (uint32_t *) ptr;
223 *len = htonl (file_len);
225 len = (uint32_t *) ptr;
226 *len = htonl (aes_buf_len);
228 GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
230 len = (uint32_t *) ptr;
231 *len = htonl (cph_buf_len);
233 GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
234 return 12 + cph_buf_len + aes_buf_len;
239 read_cpabe (const void *data,
250 len = (uint32_t *) ptr;
251 buf_len = ntohl (*len);
253 len = (uint32_t *) ptr;
254 *aes_buf_len = ntohl (*len);
256 *aes_buf = GNUNET_malloc (*aes_buf_len);
257 GNUNET_memcpy (*aes_buf, ptr, *aes_buf_len);
259 len = (uint32_t *) ptr;
260 *cph_buf_len = ntohl (*len);
262 *cph_buf = GNUNET_malloc (*cph_buf_len);
263 GNUNET_memcpy (*cph_buf, ptr, *cph_buf_len);
270 GNUNET_CRYPTO_cpabe_encrypt (const void *block,
273 const struct GNUNET_CRYPTO_AbeMasterKey *key,
285 if (! (cph = gabe_enc (key->pub, m, (char *) policy)))
286 return GNUNET_SYSERR;
287 cph_buf_len = gabe_cph_serialize (cph,
291 plt = GNUNET_memdup (block, size);
292 aes_buf_len = aes_128_cbc_encrypt (plt, size, m, &aes_buf);
295 result_len = write_cpabe (result, size, cph_buf, cph_buf_len, aes_buf,
297 GNUNET_free (cph_buf);
298 GNUNET_free (aes_buf);
304 GNUNET_CRYPTO_cpabe_decrypt (const void *block,
306 const struct GNUNET_CRYPTO_AbeKey *key,
317 read_cpabe (block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
318 cph = gabe_cph_unserialize (key->pub, cph_buf, cph_buf_size);
319 if (! gabe_dec (key->pub, key->prv, cph, m))
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "%s\n", gabe_error ());
323 GNUNET_free (aes_buf);
324 GNUNET_free (cph_buf);
328 return GNUNET_SYSERR;
332 plt_len = aes_128_cbc_decrypt (aes_buf, aes_buf_size, m, (char **) result);
333 GNUNET_free (cph_buf);
334 GNUNET_free (aes_buf);
336 // freeing is buggy in gabe
337 // gabe_prv_free (prv);
338 // gabe_pub_free (pub);
344 GNUNET_CRYPTO_cpabe_serialize_key (const struct GNUNET_CRYPTO_AbeKey *key,
353 pub_len = gabe_pub_serialize (key->pub, &pub);
354 prv_len = gabe_prv_serialize (key->prv, &prv);
356 len = pub_len + prv_len + 12;
357 write_cpabe (result, len, pub, pub_len, prv, prv_len);
366 struct GNUNET_CRYPTO_AbeKey*
367 GNUNET_CRYPTO_cpabe_deserialize_key (const void *data,
370 struct GNUNET_CRYPTO_AbeKey *key;
376 key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
382 key->pub = gabe_pub_unserialize (pub, pub_len);
383 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
392 GNUNET_CRYPTO_cpabe_serialize_master_key (const struct
393 GNUNET_CRYPTO_AbeMasterKey *key,
402 pub_len = gabe_pub_serialize (key->pub, &pub);
403 msk_len = gabe_msk_serialize (key->msk, &msk);
405 len = pub_len + msk_len + 12;
406 write_cpabe (result, len, pub, pub_len, msk, msk_len);
415 struct GNUNET_CRYPTO_AbeMasterKey*
416 GNUNET_CRYPTO_cpabe_deserialize_master_key (const void *data,
419 struct GNUNET_CRYPTO_AbeMasterKey *key;
425 key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
431 key->pub = gabe_pub_unserialize (pub, pub_len);
432 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);