2 This file is part of GNUnet. Copyright (C) 2001-2018 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
24 * @brief functions for Attribute-Based Encryption
25 * @author Martin Schanzenbach
33 #include "gnunet_crypto_lib.h"
34 #include "gnunet_abe_lib.h"
36 struct GNUNET_ABE_AbeMasterKey
42 struct GNUNET_ABE_AbeKey
49 init_aes (element_t k, int enc,
50 gcry_cipher_hd_t*handle,
51 struct GNUNET_CRYPTO_SymmetricSessionKey *key,
56 unsigned char*key_buf;
58 key_len = element_length_in_bytes (k) < 33 ? 3 : element_length_in_bytes (k);
59 key_buf = (unsigned char *) malloc (key_len);
60 element_to_bytes (key_buf, k);
62 GNUNET_memcpy (key->aes_key, key_buf, GNUNET_CRYPTO_AES_KEY_LENGTH);
64 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
65 GCRY_CIPHER_MODE_CFB, 0));
66 rc = gcry_cipher_setkey (*handle,
68 sizeof(key->aes_key));
69 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
70 memset (iv, 0, 16); // TODO make reasonable
71 rc = gcry_cipher_setiv (*handle,
74 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
82 aes_128_cbc_encrypt (char*pt,
87 gcry_cipher_hd_t handle;
88 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
95 init_aes (k, 1, &handle, &skey, iv);
97 /* TODO make less crufty */
99 /* stuff in real length (big endian) before padding */
100 len[0] = (size & 0xff000000) >> 24;
101 len[1] = (size & 0xff0000) >> 16;
102 len[2] = (size & 0xff00) >> 8;
103 len[3] = (size & 0xff) >> 0;
104 padding = 16 - ((4 + size) % 16);
105 buf_size = 4 + size + padding;
106 buf = GNUNET_malloc (buf_size);
107 GNUNET_memcpy (buf, len, 4);
108 GNUNET_memcpy (buf + 4, pt, size);
109 *ct = GNUNET_malloc (buf_size);
111 GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf,
113 gcry_cipher_close (handle);
114 // AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
121 aes_128_cbc_decrypt (char*ct,
126 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
127 gcry_cipher_hd_t handle;
128 unsigned char iv[16];
132 init_aes (k, 1, &handle, &skey, iv);
134 tmp = GNUNET_malloc (size);
136 // AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
137 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
138 gcry_cipher_close (handle);
139 /* TODO make less crufty */
141 /* get real length */
144 | ((tmp[0]) << 24) | ((tmp[1]) << 16)
145 | ((tmp[2]) << 8) | ((tmp[3]) << 0);
146 /* truncate any garbage from the padding */
147 *pt = GNUNET_malloc (len);
148 GNUNET_memcpy (*pt, tmp + 4, len);
156 * Create a new CP-ABE master key. Caller must free return value.
158 * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_master_key
160 struct GNUNET_ABE_AbeMasterKey*
161 GNUNET_ABE_cpabe_create_master_key (void)
163 struct GNUNET_ABE_AbeMasterKey*key;
165 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
166 gabe_setup (&key->pub, &key->msk);
167 GNUNET_assert (NULL != key->pub);
168 GNUNET_assert (NULL != key->msk);
175 * Delete a CP-ABE master key.
177 * @param key the master key
178 * @return fresh private key; free using #GNUNET_free
181 GNUNET_ABE_cpabe_delete_master_key (struct GNUNET_ABE_AbeMasterKey *key)
183 gabe_msk_free (key->msk);
184 gabe_pub_free (key->pub);
185 // GNUNET_free (key->msk);
186 // gabe_msk_free (key->msk); //For some reason free of pub implicit?
193 * Create a new CP-ABE key. Caller must free return value.
195 * @param key the master key
196 * @param attrs the attributes to append to the key
197 * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_key
199 struct GNUNET_ABE_AbeKey*
200 GNUNET_ABE_cpabe_create_key (struct GNUNET_ABE_AbeMasterKey *key,
203 struct GNUNET_ABE_AbeKey *prv_key;
207 prv_key = GNUNET_new (struct GNUNET_ABE_AbeKey);
208 prv_key->prv = gabe_keygen (key->pub, key->msk, attrs);
209 size = gabe_pub_serialize (key->pub, &tmp);
210 prv_key->pub = gabe_pub_unserialize (tmp, size);
212 GNUNET_assert (NULL != prv_key->prv);
219 * Delete a CP-ABE key.
221 * @param key the key to delete
222 * @param delete_pub GNUNE_YES if the public key should also be freed (bug in gabe)
223 * @return fresh private key; free using #GNUNET_free
226 GNUNET_ABE_cpabe_delete_key (struct GNUNET_ABE_AbeKey *key,
229 // Memory management in gabe is buggy
230 gabe_prv_free (key->prv);
231 if (GNUNET_YES == delete_pub)
232 gabe_pub_free (key->pub);
238 write_cpabe (void **result,
248 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
250 len = (uint32_t *) ptr;
251 *len = htonl (file_len);
253 len = (uint32_t *) ptr;
254 *len = htonl (aes_buf_len);
256 GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
258 len = (uint32_t *) ptr;
259 *len = htonl (cph_buf_len);
261 GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
262 return 12 + cph_buf_len + aes_buf_len;
267 read_cpabe (const void *data,
278 len = (uint32_t *) ptr;
279 buf_len = ntohl (*len);
281 len = (uint32_t *) ptr;
282 *aes_buf_len = ntohl (*len);
284 *aes_buf = GNUNET_malloc (*aes_buf_len);
285 GNUNET_memcpy (*aes_buf, ptr, *aes_buf_len);
287 len = (uint32_t *) ptr;
288 *cph_buf_len = ntohl (*len);
290 *cph_buf = GNUNET_malloc (*cph_buf_len);
291 GNUNET_memcpy (*cph_buf, ptr, *cph_buf_len);
299 * Encrypt a block using sessionkey.
301 * @param block the block to encrypt
302 * @param size the size of the @a block
303 * @param policy the ABE policy
304 * @param key the key used to encrypt
305 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
306 * @return the size of the encrypted block, -1 for errors
309 GNUNET_ABE_cpabe_encrypt (const void *block,
312 const struct GNUNET_ABE_AbeMasterKey *key,
324 if (! (cph = gabe_enc (key->pub, m, (char *) policy)))
325 return GNUNET_SYSERR;
326 cph_buf_len = gabe_cph_serialize (cph,
330 plt = GNUNET_memdup (block, size);
331 aes_buf_len = aes_128_cbc_encrypt (plt, size, m, &aes_buf);
334 result_len = write_cpabe (result, size, cph_buf, cph_buf_len, aes_buf,
336 GNUNET_free (cph_buf);
337 GNUNET_free (aes_buf);
344 * Decrypt a block using the ABE key.
346 * @param block the block to encrypt
347 * @param size the size of the @a block
348 * @param key the key used to decrypt
349 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
350 * @return the size of the encrypted block, -1 for errors
353 GNUNET_ABE_cpabe_decrypt (const void *block,
355 const struct GNUNET_ABE_AbeKey *key,
366 read_cpabe (block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
367 cph = gabe_cph_unserialize (key->pub, cph_buf, cph_buf_size);
368 if (! gabe_dec (key->pub, key->prv, cph, m))
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "%s\n", gabe_error ());
372 GNUNET_free (aes_buf);
373 GNUNET_free (cph_buf);
377 return GNUNET_SYSERR;
381 plt_len = aes_128_cbc_decrypt (aes_buf, aes_buf_size, m, (char **) result);
382 GNUNET_free (cph_buf);
383 GNUNET_free (aes_buf);
385 // freeing is buggy in gabe
386 // gabe_prv_free (prv);
387 // gabe_pub_free (pub);
394 * Serialize an ABE key.
396 * @param key the key to serialize
397 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
398 * @return the size of the encrypted block, -1 for errors
401 GNUNET_ABE_cpabe_serialize_key (const struct GNUNET_ABE_AbeKey *key,
410 pub_len = gabe_pub_serialize (key->pub, &pub);
411 prv_len = gabe_prv_serialize (key->prv, &prv);
413 len = pub_len + prv_len + 12;
414 write_cpabe (result, len, pub, pub_len, prv, prv_len);
425 * Deserialize a serialized ABE key.
427 * @param data the data to deserialize
428 * @param len the length of the data.
429 * @return the ABE key. NULL of unsuccessful
431 struct GNUNET_ABE_AbeKey*
432 GNUNET_ABE_cpabe_deserialize_key (const void *data,
435 struct GNUNET_ABE_AbeKey *key;
441 key = GNUNET_new (struct GNUNET_ABE_AbeKey);
447 key->pub = gabe_pub_unserialize (pub, pub_len);
448 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
458 * Serialize an ABE master key.
460 * @param key the key to serialize
461 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
462 * @return the size of the encrypted block, -1 for errors
465 GNUNET_ABE_cpabe_serialize_master_key (const struct
466 GNUNET_ABE_AbeMasterKey *key,
475 pub_len = gabe_pub_serialize (key->pub, &pub);
476 msk_len = gabe_msk_serialize (key->msk, &msk);
478 len = pub_len + msk_len + 12;
479 write_cpabe (result, len, pub, pub_len, msk, msk_len);
490 * Deserialize an ABE master key.
492 * @param data the data to deserialize
493 * @param len the length of the data.
494 * @return the ABE key. NULL of unsuccessful
496 struct GNUNET_ABE_AbeMasterKey*
497 GNUNET_ABE_cpabe_deserialize_master_key (const void *data,
500 struct GNUNET_ABE_AbeMasterKey *key;
506 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
512 key->pub = gabe_pub_unserialize (pub, pub_len);
513 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);