paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / abe / abe.c
1 /*
2      This file is part of GNUnet.  Copyright (C) 2001-2018 Christian Grothoff
3      (and other contributing authors)
4
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.
9
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.
14     
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/>.
17
18 */
19
20 /**
21  * @file abe/abe.c
22  * @brief functions for Attribute-Based Encryption
23  * @author Martin Schanzenbach
24  */
25
26
27 #include "platform.h"
28 #include <pbc/pbc.h>
29 #include <gabe.h>
30
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_abe_lib.h"
33
34 struct GNUNET_ABE_AbeMasterKey
35 {
36   gabe_pub_t* pub;
37   gabe_msk_t* msk;
38 };
39
40 struct GNUNET_ABE_AbeKey
41 {
42   gabe_pub_t* pub;
43   gabe_prv_t* prv;
44 };
45
46 static int
47 init_aes( element_t k, int enc,
48           gcry_cipher_hd_t* handle,
49           struct GNUNET_CRYPTO_SymmetricSessionKey *key,
50           unsigned char* iv)
51 {
52   int rc;
53   int key_len;
54   unsigned char* key_buf;
55
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);
59
60   GNUNET_memcpy (key->aes_key, key_buf, GNUNET_CRYPTO_AES_KEY_LENGTH);
61   GNUNET_assert (0 ==
62                  gcry_cipher_open (handle, GCRY_CIPHER_AES256,
63                                    GCRY_CIPHER_MODE_CFB, 0));
64   rc = gcry_cipher_setkey (*handle,
65                            key->aes_key,
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,
70                           iv,
71                           16);
72   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
73
74   free(key_buf);
75   return rc;
76 }
77
78 static int
79 aes_128_cbc_encrypt( char* pt,
80                      int size,
81                      element_t k,
82                      char **ct )
83 {
84   gcry_cipher_hd_t handle;
85   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
86   unsigned char iv[16];
87   char* buf;
88   int padding;
89   int buf_size;
90   uint8_t len[4];
91   init_aes(k, 1, &handle, &skey, iv);
92
93   /* TODO make less crufty */
94
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);
106
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);
110   GNUNET_free (buf);
111   return buf_size;
112 }
113
114 static int
115 aes_128_cbc_decrypt( char* ct,
116                      int size,
117                      element_t k,
118                      char **pt )
119 {
120   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
121   gcry_cipher_hd_t handle;
122   unsigned char iv[16];
123   char* tmp;
124   uint32_t len;
125
126   init_aes(k, 1, &handle, &skey, iv);
127
128   tmp = GNUNET_malloc (size);
129
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 */
134
135   /* get real length */
136   len = 0;
137   len = len
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);
143   GNUNET_free (tmp);
144   return len;
145 }
146
147 /**
148  * @ingroup abe
149  * Create a new CP-ABE master key. Caller must free return value.
150  *
151  * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_master_key
152  */
153 struct GNUNET_ABE_AbeMasterKey*
154 GNUNET_ABE_cpabe_create_master_key (void)
155 {
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);
161   return key;
162 }
163
164 /**
165  * @ingroup abe
166  * Delete a CP-ABE master key.
167  *
168  * @param key the master key
169  * @return fresh private key; free using #GNUNET_free
170  */
171 void
172 GNUNET_ABE_cpabe_delete_master_key (struct GNUNET_ABE_AbeMasterKey *key)
173 {
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?
178   GNUNET_free (key);
179 }
180
181 /**
182  * @ingroup abe
183  * Create a new CP-ABE key. Caller must free return value.
184  *
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
188  */
189 struct GNUNET_ABE_AbeKey*
190 GNUNET_ABE_cpabe_create_key (struct GNUNET_ABE_AbeMasterKey *key,
191                              char **attrs)
192 {
193   struct GNUNET_ABE_AbeKey *prv_key;
194   int size;
195   char *tmp;
196
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);
201   GNUNET_free (tmp);
202   GNUNET_assert (NULL != prv_key->prv);
203   return prv_key;
204 }
205
206 /**
207  * @ingroup abe
208  * Delete a CP-ABE key.
209  *
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
213  */
214 void
215 GNUNET_ABE_cpabe_delete_key (struct GNUNET_ABE_AbeKey *key,
216                                 int delete_pub)
217 {
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);
222   GNUNET_free (key);
223 }
224
225 static ssize_t
226 write_cpabe (void **result,
227              uint32_t file_len,
228              char* cph_buf,
229              int cph_buf_len,
230              char* aes_buf,
231              int aes_buf_len)
232 {
233   char *ptr;
234   uint32_t *len;
235
236   *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
237   ptr = *result;
238   len = (uint32_t*) ptr;
239   *len = htonl (file_len);
240   ptr += 4;
241   len = (uint32_t*) ptr;
242   *len = htonl (aes_buf_len);
243   ptr += 4;
244   GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
245   ptr += aes_buf_len;
246   len = (uint32_t*) ptr;
247   *len = htonl (cph_buf_len);
248   ptr += 4;
249   GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
250   return 12 + cph_buf_len + aes_buf_len;
251 }
252
253 static ssize_t
254 read_cpabe (const void *data,
255             char** cph_buf,
256             int *cph_buf_len,
257             char** aes_buf,
258             int *aes_buf_len)
259 {
260   int buf_len;
261   char *ptr;
262   uint32_t *len;
263
264   ptr = (char*)data;
265   len = (uint32_t*)ptr;
266   buf_len = ntohl (*len);
267   ptr += 4;
268   len = (uint32_t*)ptr;
269   *aes_buf_len = ntohl (*len);
270   ptr += 4;
271   *aes_buf = GNUNET_malloc (*aes_buf_len);
272   GNUNET_memcpy(*aes_buf, ptr, *aes_buf_len);
273   ptr += *aes_buf_len;
274   len = (uint32_t*)ptr;
275   *cph_buf_len = ntohl (*len);
276   ptr += 4;
277   *cph_buf = GNUNET_malloc (*cph_buf_len);
278   GNUNET_memcpy(*cph_buf, ptr, *cph_buf_len);
279
280   return buf_len;
281 }
282
283 /**
284  * @ingroup abe
285  * Encrypt a block using  sessionkey.
286  *
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
293  */
294 ssize_t
295 GNUNET_ABE_cpabe_encrypt (const void *block,
296                              size_t size,
297                              const char *policy,
298                              const struct GNUNET_ABE_AbeMasterKey *key,
299                              void **result)
300 {
301   gabe_cph_t* cph;
302   char* plt;
303   char* cph_buf;
304   char* aes_buf;
305   element_t m;
306   int cph_buf_len;
307   int aes_buf_len;
308   ssize_t result_len;
309
310   if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
311     return GNUNET_SYSERR;
312   cph_buf_len = gabe_cph_serialize(cph,
313                                 &cph_buf);
314   gabe_cph_free(cph);
315   GNUNET_free (cph);
316   plt = GNUNET_memdup (block, size);
317   aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
318   GNUNET_free (plt);
319   element_clear(m);
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);
323   return result_len;
324 }
325
326 /**
327  * @ingroup abe
328  * Decrypt a block using the ABE key.
329  *
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
335  */
336 ssize_t
337 GNUNET_ABE_cpabe_decrypt (const void *block,
338                              size_t size,
339                              const struct GNUNET_ABE_AbeKey *key,
340                              void **result)
341 {
342   char* aes_buf;
343   char* cph_buf;
344   gabe_cph_t* cph;
345   element_t m;
346   int cph_buf_size;
347   int aes_buf_size;
348   int plt_len;
349
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);
357     gabe_cph_free(cph);
358     GNUNET_free (cph);
359     element_clear (m);
360     return GNUNET_SYSERR;
361   }
362   gabe_cph_free(cph);
363   GNUNET_free (cph);
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);
367   element_clear (m);
368   //freeing is buggy in gabe
369   //gabe_prv_free (prv);
370   //gabe_pub_free (pub);
371   return plt_len;
372 }
373
374 /**
375  * @ingroup abe
376  * Serialize an ABE key.
377  *
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
381  */
382 ssize_t
383 GNUNET_ABE_cpabe_serialize_key (const struct GNUNET_ABE_AbeKey *key,
384                                    void **result)
385 {
386   ssize_t len;
387   char *pub;
388   char *prv;
389   int pub_len;
390   int prv_len;
391
392   pub_len = gabe_pub_serialize (key->pub, &pub);
393   prv_len = gabe_prv_serialize (key->prv, &prv);
394
395   len = pub_len + prv_len + 12;
396   write_cpabe (result, len, pub, pub_len, prv, prv_len);
397
398   GNUNET_free (pub);
399   GNUNET_free (prv);
400
401   return len;
402 }
403
404 /**
405  * @ingroup abe
406  * Deserialize a serialized ABE key.
407  *
408  * @param data the data to deserialize
409  * @param len the length of the data.
410  * @return the ABE key. NULL of unsuccessful
411  */
412 struct GNUNET_ABE_AbeKey*
413 GNUNET_ABE_cpabe_deserialize_key (const void *data,
414                                      size_t len)
415 {
416   struct GNUNET_ABE_AbeKey *key;
417   char *pub;
418   char *prv;
419   int prv_len;
420   int pub_len;
421
422   key = GNUNET_new (struct GNUNET_ABE_AbeKey);
423   read_cpabe (data,
424               &pub,
425               &pub_len,
426               &prv,
427               &prv_len);
428   key->pub = gabe_pub_unserialize (pub, pub_len);
429   key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
430
431   GNUNET_free (pub);
432   GNUNET_free (prv);
433   return key;
434 }
435
436 /**
437  * @ingroup abe
438  * Serialize an ABE master key.
439  *
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
443  */
444 ssize_t
445 GNUNET_ABE_cpabe_serialize_master_key (const struct GNUNET_ABE_AbeMasterKey *key,
446                                           void **result)
447 {
448   ssize_t len;
449   char *pub;
450   char *msk;
451   int pub_len;
452   int msk_len;
453
454   pub_len = gabe_pub_serialize (key->pub, &pub);
455   msk_len = gabe_msk_serialize (key->msk, &msk);
456
457   len = pub_len + msk_len + 12;
458   write_cpabe (result, len, pub, pub_len, msk, msk_len);
459
460   GNUNET_free (pub);
461   GNUNET_free (msk);
462
463   return len;
464 }
465
466 /**
467  * @ingroup abe
468  * Deserialize an ABE master key.
469  *
470  * @param data the data to deserialize
471  * @param len the length of the data.
472  * @return the ABE key. NULL of unsuccessful
473  */
474 struct GNUNET_ABE_AbeMasterKey*
475 GNUNET_ABE_cpabe_deserialize_master_key (const void *data,
476                                             size_t len)
477 {
478   struct GNUNET_ABE_AbeMasterKey *key;
479   char *msk;
480   char *pub;
481   int msk_len;
482   int pub_len;
483
484   key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
485   read_cpabe (data,
486               &pub,
487               &pub_len,
488               &msk,
489               &msk_len);
490   key->pub = gabe_pub_unserialize (pub, pub_len);
491   key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);
492
493   GNUNET_free (pub);
494   GNUNET_free (msk);
495
496   return key;
497 }