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/>.
22 * @brief functions for Attribute-Based Encryption
23 * @author Martin Schanzenbach
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_abe_lib.h"
34 struct GNUNET_ABE_AbeMasterKey
40 struct GNUNET_ABE_AbeKey
47 init_aes( element_t k, int enc,
48 gcry_cipher_hd_t* handle,
49 struct GNUNET_CRYPTO_SymmetricSessionKey *key,
54 unsigned char* key_buf;
56 key_len = element_length_in_bytes(k) < 33 ? 3 : element_length_in_bytes(k);
57 key_buf = (unsigned char*) malloc(key_len);
58 element_to_bytes(key_buf, k);
60 GNUNET_memcpy (key->aes_key, key_buf, GNUNET_CRYPTO_AES_KEY_LENGTH);
62 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
63 GCRY_CIPHER_MODE_CFB, 0));
64 rc = gcry_cipher_setkey (*handle,
66 sizeof (key->aes_key));
67 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
68 memset (iv, 0, 16); //TODO make reasonable
69 rc = gcry_cipher_setiv (*handle,
72 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
79 aes_128_cbc_encrypt( char* pt,
84 gcry_cipher_hd_t handle;
85 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
91 init_aes(k, 1, &handle, &skey, iv);
93 /* TODO make less crufty */
95 /* stuff in real length (big endian) before padding */
96 len[0] = (size & 0xff000000)>>24;
97 len[1] = (size & 0xff0000)>>16;
98 len[2] = (size & 0xff00)>>8;
99 len[3] = (size & 0xff)>>0;
100 padding = 16 - ((4+size) % 16);
101 buf_size = 4 + size + padding;
102 buf = GNUNET_malloc (buf_size);
103 GNUNET_memcpy (buf, len, 4);
104 GNUNET_memcpy (buf+4, pt, size);
105 *ct = GNUNET_malloc (buf_size);
107 GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf, buf_size));
108 gcry_cipher_close (handle);
109 //AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
115 aes_128_cbc_decrypt( char* ct,
120 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
121 gcry_cipher_hd_t handle;
122 unsigned char iv[16];
126 init_aes(k, 1, &handle, &skey, iv);
128 tmp = GNUNET_malloc (size);
130 //AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
131 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
132 gcry_cipher_close (handle);
133 /* TODO make less crufty */
135 /* get real length */
138 | ((tmp[0])<<24) | ((tmp[1])<<16)
139 | ((tmp[2])<<8) | ((tmp[3])<<0);
140 /* truncate any garbage from the padding */
141 *pt = GNUNET_malloc (len);
142 GNUNET_memcpy (*pt, tmp+4, len);
149 * Create a new CP-ABE master key. Caller must free return value.
151 * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_master_key
153 struct GNUNET_ABE_AbeMasterKey*
154 GNUNET_ABE_cpabe_create_master_key (void)
156 struct GNUNET_ABE_AbeMasterKey* key;
157 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
158 gabe_setup(&key->pub, &key->msk);
159 GNUNET_assert (NULL != key->pub);
160 GNUNET_assert (NULL != key->msk);
166 * Delete a CP-ABE master key.
168 * @param key the master key
169 * @return fresh private key; free using #GNUNET_free
172 GNUNET_ABE_cpabe_delete_master_key (struct GNUNET_ABE_AbeMasterKey *key)
174 gabe_msk_free (key->msk);
175 gabe_pub_free (key->pub);
176 //GNUNET_free (key->msk);
177 //gabe_msk_free (key->msk); //For some reason free of pub implicit?
183 * Create a new CP-ABE key. Caller must free return value.
185 * @param key the master key
186 * @param attrs the attributes to append to the key
187 * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_key
189 struct GNUNET_ABE_AbeKey*
190 GNUNET_ABE_cpabe_create_key (struct GNUNET_ABE_AbeMasterKey *key,
193 struct GNUNET_ABE_AbeKey *prv_key;
197 prv_key = GNUNET_new (struct GNUNET_ABE_AbeKey);
198 prv_key->prv = gabe_keygen(key->pub, key->msk, attrs);
199 size = gabe_pub_serialize(key->pub, &tmp);
200 prv_key->pub = gabe_pub_unserialize(tmp, size);
202 GNUNET_assert (NULL != prv_key->prv);
208 * Delete a CP-ABE key.
210 * @param key the key to delete
211 * @param delete_pub GNUNE_YES if the public key should also be freed (bug in gabe)
212 * @return fresh private key; free using #GNUNET_free
215 GNUNET_ABE_cpabe_delete_key (struct GNUNET_ABE_AbeKey *key,
218 //Memory management in gabe is buggy
219 gabe_prv_free (key->prv);
220 if (GNUNET_YES == delete_pub)
221 gabe_pub_free (key->pub);
226 write_cpabe (void **result,
236 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
238 len = (uint32_t*) ptr;
239 *len = htonl (file_len);
241 len = (uint32_t*) ptr;
242 *len = htonl (aes_buf_len);
244 GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
246 len = (uint32_t*) ptr;
247 *len = htonl (cph_buf_len);
249 GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
250 return 12 + cph_buf_len + aes_buf_len;
254 read_cpabe (const void *data,
265 len = (uint32_t*)ptr;
266 buf_len = ntohl (*len);
268 len = (uint32_t*)ptr;
269 *aes_buf_len = ntohl (*len);
271 *aes_buf = GNUNET_malloc (*aes_buf_len);
272 GNUNET_memcpy(*aes_buf, ptr, *aes_buf_len);
274 len = (uint32_t*)ptr;
275 *cph_buf_len = ntohl (*len);
277 *cph_buf = GNUNET_malloc (*cph_buf_len);
278 GNUNET_memcpy(*cph_buf, ptr, *cph_buf_len);
285 * Encrypt a block using sessionkey.
287 * @param block the block to encrypt
288 * @param size the size of the @a block
289 * @param policy the ABE policy
290 * @param key the key used to encrypt
291 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
292 * @return the size of the encrypted block, -1 for errors
295 GNUNET_ABE_cpabe_encrypt (const void *block,
298 const struct GNUNET_ABE_AbeMasterKey *key,
310 if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
311 return GNUNET_SYSERR;
312 cph_buf_len = gabe_cph_serialize(cph,
316 plt = GNUNET_memdup (block, size);
317 aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
320 result_len = write_cpabe(result, size, cph_buf, cph_buf_len, aes_buf, aes_buf_len);
321 GNUNET_free(cph_buf);
322 GNUNET_free(aes_buf);
328 * Decrypt a block using the ABE key.
330 * @param block the block to encrypt
331 * @param size the size of the @a block
332 * @param key the key used to decrypt
333 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
334 * @return the size of the encrypted block, -1 for errors
337 GNUNET_ABE_cpabe_decrypt (const void *block,
339 const struct GNUNET_ABE_AbeKey *key,
350 read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
351 cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size);
352 if( !gabe_dec(key->pub, key->prv, cph, m) ) {
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "%s\n", gabe_error());
355 GNUNET_free (aes_buf);
356 GNUNET_free (cph_buf);
360 return GNUNET_SYSERR;
364 plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result);
365 GNUNET_free (cph_buf);
366 GNUNET_free (aes_buf);
368 //freeing is buggy in gabe
369 //gabe_prv_free (prv);
370 //gabe_pub_free (pub);
376 * Serialize an ABE key.
378 * @param key the key to serialize
379 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
380 * @return the size of the encrypted block, -1 for errors
383 GNUNET_ABE_cpabe_serialize_key (const struct GNUNET_ABE_AbeKey *key,
392 pub_len = gabe_pub_serialize (key->pub, &pub);
393 prv_len = gabe_prv_serialize (key->prv, &prv);
395 len = pub_len + prv_len + 12;
396 write_cpabe (result, len, pub, pub_len, prv, prv_len);
406 * Deserialize a serialized ABE key.
408 * @param data the data to deserialize
409 * @param len the length of the data.
410 * @return the ABE key. NULL of unsuccessful
412 struct GNUNET_ABE_AbeKey*
413 GNUNET_ABE_cpabe_deserialize_key (const void *data,
416 struct GNUNET_ABE_AbeKey *key;
422 key = GNUNET_new (struct GNUNET_ABE_AbeKey);
428 key->pub = gabe_pub_unserialize (pub, pub_len);
429 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
438 * Serialize an ABE master key.
440 * @param key the key to serialize
441 * @param result the result buffer. Will be allocated. Free using #GNUNET_free
442 * @return the size of the encrypted block, -1 for errors
445 GNUNET_ABE_cpabe_serialize_master_key (const struct GNUNET_ABE_AbeMasterKey *key,
454 pub_len = gabe_pub_serialize (key->pub, &pub);
455 msk_len = gabe_msk_serialize (key->msk, &msk);
457 len = pub_len + msk_len + 12;
458 write_cpabe (result, len, pub, pub_len, msk, msk_len);
468 * Deserialize an ABE master key.
470 * @param data the data to deserialize
471 * @param len the length of the data.
472 * @return the ABE key. NULL of unsuccessful
474 struct GNUNET_ABE_AbeMasterKey*
475 GNUNET_ABE_cpabe_deserialize_master_key (const void *data,
478 struct GNUNET_ABE_AbeMasterKey *key;
484 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
490 key->pub = gabe_pub_unserialize (pub, pub_len);
491 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);