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));
81 aes_128_cbc_encrypt( char* pt,
86 gcry_cipher_hd_t handle;
87 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
93 init_aes(k, 1, &handle, &skey, iv);
95 /* TODO make less crufty */
97 /* stuff in real length (big endian) before padding */
98 len[0] = (size & 0xff000000)>>24;
99 len[1] = (size & 0xff0000)>>16;
100 len[2] = (size & 0xff00)>>8;
101 len[3] = (size & 0xff)>>0;
102 padding = 16 - ((4+size) % 16);
103 buf_size = 4 + size + padding;
104 buf = GNUNET_malloc (buf_size);
105 GNUNET_memcpy (buf, len, 4);
106 GNUNET_memcpy (buf+4, pt, size);
107 *ct = GNUNET_malloc (buf_size);
109 GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf, buf_size));
110 gcry_cipher_close (handle);
111 //AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
117 aes_128_cbc_decrypt( char* ct,
122 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
123 gcry_cipher_hd_t handle;
124 unsigned char iv[16];
128 init_aes(k, 1, &handle, &skey, iv);
130 tmp = GNUNET_malloc (size);
132 //AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
133 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
134 gcry_cipher_close (handle);
135 /* TODO make less crufty */
137 /* get real length */
140 | ((tmp[0])<<24) | ((tmp[1])<<16)
141 | ((tmp[2])<<8) | ((tmp[3])<<0);
142 /* truncate any garbage from the padding */
143 *pt = GNUNET_malloc (len);
144 GNUNET_memcpy (*pt, tmp+4, len);
151 * Create a new CP-ABE master key. Caller must free return value.
153 * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_master_key
155 struct GNUNET_ABE_AbeMasterKey*
156 GNUNET_ABE_cpabe_create_master_key (void)
158 struct GNUNET_ABE_AbeMasterKey* key;
159 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
160 gabe_setup(&key->pub, &key->msk);
161 GNUNET_assert (NULL != key->pub);
162 GNUNET_assert (NULL != key->msk);
168 * Delete a CP-ABE master key.
170 * @param key the master key
171 * @return fresh private key; free using #GNUNET_free
174 GNUNET_ABE_cpabe_delete_master_key (struct GNUNET_ABE_AbeMasterKey *key)
176 gabe_msk_free (key->msk);
177 gabe_pub_free (key->pub);
178 //GNUNET_free (key->msk);
179 //gabe_msk_free (key->msk); //For some reason free of pub implicit?
185 * Create a new CP-ABE key. Caller must free return value.
187 * @param key the master key
188 * @param attrs the attributes to append to the key
189 * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_key
191 struct GNUNET_ABE_AbeKey*
192 GNUNET_ABE_cpabe_create_key (struct GNUNET_ABE_AbeMasterKey *key,
195 struct GNUNET_ABE_AbeKey *prv_key;
199 prv_key = GNUNET_new (struct GNUNET_ABE_AbeKey);
200 prv_key->prv = gabe_keygen(key->pub, key->msk, attrs);
201 size = gabe_pub_serialize(key->pub, &tmp);
202 prv_key->pub = gabe_pub_unserialize(tmp, size);
204 GNUNET_assert (NULL != prv_key->prv);
210 * Delete a CP-ABE key.
212 * @param key the key to delete
213 * @param delete_pub GNUNE_YES if the public key should also be freed (bug in gabe)
214 * @return fresh private key; free using #GNUNET_free
217 GNUNET_ABE_cpabe_delete_key (struct GNUNET_ABE_AbeKey *key,
220 //Memory management in gabe is buggy
221 gabe_prv_free (key->prv);
222 if (GNUNET_YES == delete_pub)
223 gabe_pub_free (key->pub);
228 write_cpabe (void **result,
238 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
240 len = (uint32_t*) ptr;
241 *len = htonl (file_len);
243 len = (uint32_t*) ptr;
244 *len = htonl (aes_buf_len);
246 GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
248 len = (uint32_t*) ptr;
249 *len = htonl (cph_buf_len);
251 GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
252 return 12 + cph_buf_len + aes_buf_len;
256 read_cpabe (const void *data,
267 len = (uint32_t*)ptr;
268 buf_len = ntohl (*len);
270 len = (uint32_t*)ptr;
271 *aes_buf_len = ntohl (*len);
273 *aes_buf = GNUNET_malloc (*aes_buf_len);
274 GNUNET_memcpy(*aes_buf, ptr, *aes_buf_len);
276 len = (uint32_t*)ptr;
277 *cph_buf_len = ntohl (*len);
279 *cph_buf = GNUNET_malloc (*cph_buf_len);
280 GNUNET_memcpy(*cph_buf, ptr, *cph_buf_len);
287 * Encrypt a block using sessionkey.
289 * @param block the block to encrypt
290 * @param size the size of the @a block
291 * @param policy the ABE policy
292 * @param key the key used to encrypt
293 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
294 * @return the size of the encrypted block, -1 for errors
297 GNUNET_ABE_cpabe_encrypt (const void *block,
300 const struct GNUNET_ABE_AbeMasterKey *key,
312 if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
313 return GNUNET_SYSERR;
314 cph_buf_len = gabe_cph_serialize(cph,
318 plt = GNUNET_memdup (block, size);
319 aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
322 result_len = write_cpabe(result, size, cph_buf, cph_buf_len, aes_buf, aes_buf_len);
323 GNUNET_free(cph_buf);
324 GNUNET_free(aes_buf);
330 * Decrypt a block using the ABE key.
332 * @param block the block to encrypt
333 * @param size the size of the @a block
334 * @param key the key used to decrypt
335 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
336 * @return the size of the encrypted block, -1 for errors
339 GNUNET_ABE_cpabe_decrypt (const void *block,
341 const struct GNUNET_ABE_AbeKey *key,
352 read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
353 cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size);
354 if( !gabe_dec(key->pub, key->prv, cph, m) ) {
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "%s\n", gabe_error());
357 GNUNET_free (aes_buf);
358 GNUNET_free (cph_buf);
362 return GNUNET_SYSERR;
366 plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result);
367 GNUNET_free (cph_buf);
368 GNUNET_free (aes_buf);
370 //freeing is buggy in gabe
371 //gabe_prv_free (prv);
372 //gabe_pub_free (pub);
378 * Serialize an ABE key.
380 * @param key the key to serialize
381 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
382 * @return the size of the encrypted block, -1 for errors
385 GNUNET_ABE_cpabe_serialize_key (const struct GNUNET_ABE_AbeKey *key,
394 pub_len = gabe_pub_serialize (key->pub, &pub);
395 prv_len = gabe_prv_serialize (key->prv, &prv);
397 len = pub_len + prv_len + 12;
398 write_cpabe (result, len, pub, pub_len, prv, prv_len);
408 * Deserialize a serialized ABE key.
410 * @param data the data to deserialize
411 * @param len the length of the data.
412 * @return the ABE key. NULL of unsuccessful
414 struct GNUNET_ABE_AbeKey*
415 GNUNET_ABE_cpabe_deserialize_key (const void *data,
418 struct GNUNET_ABE_AbeKey *key;
424 key = GNUNET_new (struct GNUNET_ABE_AbeKey);
430 key->pub = gabe_pub_unserialize (pub, pub_len);
431 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
440 * Serialize an ABE master key.
442 * @param key the key to serialize
443 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
444 * @return the size of the encrypted block, -1 for errors
447 GNUNET_ABE_cpabe_serialize_master_key (const struct GNUNET_ABE_AbeMasterKey *key,
456 pub_len = gabe_pub_serialize (key->pub, &pub);
457 msk_len = gabe_msk_serialize (key->msk, &msk);
459 len = pub_len + msk_len + 12;
460 write_cpabe (result, len, pub, pub_len, msk, msk_len);
470 * Deserialize an ABE master key.
472 * @param data the data to deserialize
473 * @param len the length of the data.
474 * @return the ABE key. NULL of unsuccessful
476 struct GNUNET_ABE_AbeMasterKey*
477 GNUNET_ABE_cpabe_deserialize_master_key (const void *data,
480 struct GNUNET_ABE_AbeMasterKey *key;
486 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
492 key->pub = gabe_pub_unserialize (pub, pub_len);
493 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);