initialize response_code, always
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19
20 */
21
22 /**
23  * @file abe/abe.c
24  * @brief functions for Attribute-Based Encryption
25  * @author Martin Schanzenbach
26  */
27
28
29 #include "platform.h"
30 #include <pbc/pbc.h>
31 #include <gabe.h>
32
33 #include "gnunet_crypto_lib.h"
34 #include "gnunet_abe_lib.h"
35
36 struct GNUNET_ABE_AbeMasterKey
37 {
38   gabe_pub_t* pub;
39   gabe_msk_t* msk;
40 };
41
42 struct GNUNET_ABE_AbeKey
43 {
44   gabe_pub_t* pub;
45   gabe_prv_t* prv;
46 };
47
48 static int
49 init_aes( element_t k, int enc,
50           gcry_cipher_hd_t* handle,
51           struct GNUNET_CRYPTO_SymmetricSessionKey *key,
52           unsigned char* iv)
53 {
54   int rc;
55   int key_len;
56   unsigned char* key_buf;
57   
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);
61
62   memcpy (key->aes_key, key_buf, GNUNET_CRYPTO_AES_KEY_LENGTH); 
63   GNUNET_assert (0 ==
64                  gcry_cipher_open (handle, GCRY_CIPHER_AES256,
65                                    GCRY_CIPHER_MODE_CFB, 0));
66   rc = gcry_cipher_setkey (*handle,
67                            key->aes_key,
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,
72                           iv,
73                           16);
74   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
75
76   free(key_buf);
77   return rc;
78 }
79
80 static int
81 aes_128_cbc_encrypt( char* pt,
82                      int size,
83                      element_t k,
84                      char **ct )
85 {
86   gcry_cipher_hd_t handle;
87   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
88   unsigned char iv[16];
89   char* buf;
90   int padding;
91   int buf_size;
92   uint8_t len[4];
93   init_aes(k, 1, &handle, &skey, iv);
94
95   /* TODO make less crufty */
96
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);
108
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);
112   GNUNET_free (buf);
113   return buf_size;
114 }
115
116 static int
117 aes_128_cbc_decrypt( char* ct,
118                      int size,
119                      element_t k,
120                      char **pt )
121 {
122   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
123   gcry_cipher_hd_t handle;
124   unsigned char iv[16];
125   char* tmp;
126   uint32_t len;
127   
128   init_aes(k, 1, &handle, &skey, iv);
129
130   tmp = GNUNET_malloc (size);
131
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 */
136   
137   /* get real length */
138   len = 0;
139   len = len
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);
145   GNUNET_free (tmp);
146   return len;
147 }
148
149 /**
150  * @ingroup abe
151  * Create a new CP-ABE master key. Caller must free return value.
152  *
153  * @return fresh private key; free using #GNUNET_ABE_cpabe_delete_master_key
154  */
155 struct GNUNET_ABE_AbeMasterKey*
156 GNUNET_ABE_cpabe_create_master_key (void)
157 {
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);
163   return key;
164 }
165
166 /**
167  * @ingroup abe
168  * Delete a CP-ABE master key.
169  *
170  * @param key the master key
171  * @return fresh private key; free using #GNUNET_free
172  */
173 void
174 GNUNET_ABE_cpabe_delete_master_key (struct GNUNET_ABE_AbeMasterKey *key)
175 {
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?
180   GNUNET_free (key);
181 }
182
183 /**
184  * @ingroup abe
185  * Create a new CP-ABE key. Caller must free return value.
186  *
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
190  */
191 struct GNUNET_ABE_AbeKey*
192 GNUNET_ABE_cpabe_create_key (struct GNUNET_ABE_AbeMasterKey *key,
193                              char **attrs)
194 {
195   struct GNUNET_ABE_AbeKey *prv_key;
196   int size;
197   char *tmp;
198   
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);
203   GNUNET_free (tmp);
204   GNUNET_assert (NULL != prv_key->prv);
205   return prv_key;
206 }
207
208 /**
209  * @ingroup abe
210  * Delete a CP-ABE key.
211  *
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
215  */
216 void
217 GNUNET_ABE_cpabe_delete_key (struct GNUNET_ABE_AbeKey *key,
218                                 int delete_pub)
219 {
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);
224   GNUNET_free (key);
225 }
226
227 static ssize_t
228 write_cpabe (void **result,
229              uint32_t file_len,
230              char* cph_buf,
231              int cph_buf_len,
232              char* aes_buf,
233              int aes_buf_len)
234 {
235   char *ptr;
236   uint32_t *len;
237   
238   *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
239   ptr = *result;
240   len = (uint32_t*) ptr;
241   *len = htonl (file_len);
242   ptr += 4;
243   len = (uint32_t*) ptr;
244   *len = htonl (aes_buf_len);
245   ptr += 4;
246   memcpy (ptr, aes_buf, aes_buf_len);
247   ptr += aes_buf_len;
248   len = (uint32_t*) ptr;
249   *len = htonl (cph_buf_len);
250   ptr += 4;
251   memcpy (ptr, cph_buf, cph_buf_len);
252   return 12 + cph_buf_len + aes_buf_len;
253 }
254
255 static ssize_t
256 read_cpabe (const void *data,
257             char** cph_buf,
258             int *cph_buf_len,
259             char** aes_buf,
260             int *aes_buf_len)
261 {
262   int buf_len;
263   char *ptr;
264   uint32_t *len;
265
266   ptr = (char*)data;
267   len = (uint32_t*)ptr;
268   buf_len = ntohl (*len);
269   ptr += 4;
270   len = (uint32_t*)ptr;
271   *aes_buf_len = ntohl (*len);
272   ptr += 4;
273   *aes_buf = GNUNET_malloc (*aes_buf_len);
274   memcpy(*aes_buf, ptr, *aes_buf_len);
275   ptr += *aes_buf_len;
276   len = (uint32_t*)ptr;
277   *cph_buf_len = ntohl (*len);
278   ptr += 4;
279   *cph_buf = GNUNET_malloc (*cph_buf_len);
280   memcpy(*cph_buf, ptr, *cph_buf_len);
281
282   return buf_len;
283 }
284
285 /**
286  * @ingroup abe
287  * Encrypt a block using  sessionkey.
288  *
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
295  */
296 ssize_t
297 GNUNET_ABE_cpabe_encrypt (const void *block,
298                              size_t size,
299                              const char *policy,
300                              const struct GNUNET_ABE_AbeMasterKey *key,
301                              void **result)
302 {
303   gabe_cph_t* cph;
304   char* plt;
305   char* cph_buf;
306   char* aes_buf;
307   element_t m;
308   int cph_buf_len;
309   int aes_buf_len;
310   ssize_t result_len;
311
312   if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
313     return GNUNET_SYSERR;
314   cph_buf_len = gabe_cph_serialize(cph,
315                                 &cph_buf);
316   gabe_cph_free(cph);
317   GNUNET_free (cph);
318   plt = GNUNET_memdup (block, size);
319   aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
320   GNUNET_free (plt);
321   element_clear(m);
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);
325   return result_len;
326 }
327
328 /**
329  * @ingroup abe
330  * Decrypt a block using the ABE key.
331  *
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
337  */
338 ssize_t
339 GNUNET_ABE_cpabe_decrypt (const void *block,
340                              size_t size,
341                              const struct GNUNET_ABE_AbeKey *key,
342                              void **result)
343 {
344   char* aes_buf;
345   char* cph_buf;
346   gabe_cph_t* cph;
347   element_t m;
348   int cph_buf_size;
349   int aes_buf_size;
350   int plt_len;
351
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);
359     gabe_cph_free(cph);
360     GNUNET_free (cph);
361     element_clear (m);
362     return GNUNET_SYSERR;
363   }
364   gabe_cph_free(cph);
365   GNUNET_free (cph);
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);
369   element_clear (m);
370   //freeing is buggy in gabe
371   //gabe_prv_free (prv);
372   //gabe_pub_free (pub);
373   return plt_len;
374 }
375
376 /**
377  * @ingroup abe
378  * Serialize an ABE key.
379  *
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
383  */
384 ssize_t
385 GNUNET_ABE_cpabe_serialize_key (const struct GNUNET_ABE_AbeKey *key,
386                                    void **result)
387 {
388   ssize_t len;
389   char *pub;
390   char *prv;
391   int pub_len;
392   int prv_len;
393
394   pub_len = gabe_pub_serialize (key->pub, &pub);
395   prv_len = gabe_prv_serialize (key->prv, &prv);
396
397   len = pub_len + prv_len + 12;
398   write_cpabe (result, len, pub, pub_len, prv, prv_len);
399
400   GNUNET_free (pub);
401   GNUNET_free (prv);
402
403   return len;
404 }
405
406 /**
407  * @ingroup abe
408  * Deserialize a serialized ABE key.
409  *
410  * @param data the data to deserialize
411  * @param len the length of the data.
412  * @return the ABE key. NULL of unsuccessful
413  */
414 struct GNUNET_ABE_AbeKey*
415 GNUNET_ABE_cpabe_deserialize_key (const void *data,
416                                      size_t len)
417 {
418   struct GNUNET_ABE_AbeKey *key;
419   char *pub;
420   char *prv;
421   int prv_len;
422   int pub_len;
423
424   key = GNUNET_new (struct GNUNET_ABE_AbeKey);
425   read_cpabe (data,
426               &pub,
427               &pub_len,
428               &prv,
429               &prv_len);
430   key->pub = gabe_pub_unserialize (pub, pub_len);
431   key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
432   
433   GNUNET_free (pub);
434   GNUNET_free (prv);
435   return key;
436 }
437
438 /**
439  * @ingroup abe
440  * Serialize an ABE master key.
441  *
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
445  */
446 ssize_t
447 GNUNET_ABE_cpabe_serialize_master_key (const struct GNUNET_ABE_AbeMasterKey *key,
448                                           void **result)
449 {
450   ssize_t len;
451   char *pub;
452   char *msk;
453   int pub_len;
454   int msk_len;
455
456   pub_len = gabe_pub_serialize (key->pub, &pub);
457   msk_len = gabe_msk_serialize (key->msk, &msk);
458
459   len = pub_len + msk_len + 12;
460   write_cpabe (result, len, pub, pub_len, msk, msk_len);
461
462   GNUNET_free (pub);
463   GNUNET_free (msk);
464
465   return len;
466 }
467
468 /**
469  * @ingroup abe
470  * Deserialize an ABE master key.
471  *
472  * @param data the data to deserialize
473  * @param len the length of the data.
474  * @return the ABE key. NULL of unsuccessful
475  */
476 struct GNUNET_ABE_AbeMasterKey*
477 GNUNET_ABE_cpabe_deserialize_master_key (const void *data,
478                                             size_t len)
479 {
480   struct GNUNET_ABE_AbeMasterKey *key;
481   char *msk;
482   char *pub;
483   int msk_len;
484   int pub_len;
485
486   key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
487   read_cpabe (data,
488               &pub,
489               &pub_len,
490               &msk,
491               &msk_len);
492   key->pub = gabe_pub_unserialize (pub, pub_len);
493   key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);
494   
495   GNUNET_free (pub);
496   GNUNET_free (msk);
497
498   return key;
499 }