Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / util / crypto_abe.c
1 /*
2      This file is part of GNUnet.  Copyright (C) 2001-2014 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 util/crypto_random.c
24  * @brief functions to gather random numbers
25  * @author Christian Grothoff
26  */
27
28
29 #include "platform.h"
30 #include <pbc/pbc.h>
31 #include <gabe.h>
32
33 #include "gnunet_crypto_lib.h"
34
35 struct GNUNET_CRYPTO_AbeMasterKey
36 {
37   gabe_pub_t* pub;
38   gabe_msk_t* msk;
39 };
40
41 struct GNUNET_CRYPTO_AbeKey
42 {
43   gabe_pub_t* pub;
44   gabe_prv_t* prv;
45 };
46
47 static int
48 init_aes( element_t k, int enc,
49           gcry_cipher_hd_t* handle,
50           struct GNUNET_CRYPTO_SymmetricSessionKey *key,
51           unsigned char* iv)
52 {
53   int rc;
54   int key_len;
55   unsigned char* key_buf;
56   
57   key_len = element_length_in_bytes(k) < 33 ? 3 : element_length_in_bytes(k);
58   key_buf = (unsigned char*) malloc(key_len);
59   element_to_bytes(key_buf, k);
60
61   memcpy (key->aes_key, key_buf, GNUNET_CRYPTO_AES_KEY_LENGTH); 
62   GNUNET_assert (0 ==
63                  gcry_cipher_open (handle, GCRY_CIPHER_AES256,
64                                    GCRY_CIPHER_MODE_CFB, 0));
65   rc = gcry_cipher_setkey (*handle,
66                            key->aes_key,
67                            sizeof (key->aes_key));
68   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
69   memset (iv, 0, 16); //TODO make reasonable
70   rc = gcry_cipher_setiv (*handle,
71                           iv,
72                           16);
73   GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
74
75   free(key_buf);
76   return rc;
77 }
78
79 static int
80 aes_128_cbc_encrypt( char* pt,
81                      int size,
82                      element_t k,
83                      char **ct )
84 {
85   gcry_cipher_hd_t handle;
86   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
87   unsigned char iv[16];
88   char* buf;
89   int padding;
90   int buf_size;
91   uint8_t len[4];
92   init_aes(k, 1, &handle, &skey, iv);
93
94   /* TODO make less crufty */
95
96   /* stuff in real length (big endian) before padding */
97   len[0] = (size & 0xff000000)>>24;
98   len[1] = (size & 0xff0000)>>16;
99   len[2] = (size & 0xff00)>>8;
100   len[3] = (size & 0xff)>>0;
101   padding = 16 - ((4+size) % 16);
102   buf_size = 4 + size + padding;
103   buf = GNUNET_malloc (buf_size);
104   GNUNET_memcpy (buf, len, 4);
105   GNUNET_memcpy (buf+4, pt, size);
106   *ct = GNUNET_malloc (buf_size);
107
108   GNUNET_assert (0 == gcry_cipher_encrypt (handle, *ct, buf_size, buf, buf_size));
109   gcry_cipher_close (handle);
110   //AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
111   GNUNET_free (buf);
112   return buf_size;
113 }
114
115 static int
116 aes_128_cbc_decrypt( char* ct,
117                      int size,
118                      element_t k,
119                      char **pt )
120 {
121   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
122   gcry_cipher_hd_t handle;
123   unsigned char iv[16];
124   char* tmp;
125   uint32_t len;
126   
127   init_aes(k, 1, &handle, &skey, iv);
128
129   tmp = GNUNET_malloc (size);
130
131   //AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
132   GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size)); 
133   gcry_cipher_close (handle);
134   /* TODO make less crufty */
135   
136   /* get real length */
137   len = 0;
138   len = len
139     | ((tmp[0])<<24) | ((tmp[1])<<16)
140     | ((tmp[2])<<8)  | ((tmp[3])<<0);
141   /* truncate any garbage from the padding */
142   *pt = GNUNET_malloc (len);
143   GNUNET_memcpy (*pt, tmp+4, len);
144   GNUNET_free (tmp);
145   return len;
146 }
147
148 struct GNUNET_CRYPTO_AbeMasterKey*
149 GNUNET_CRYPTO_cpabe_create_master_key (void)
150 {
151   struct GNUNET_CRYPTO_AbeMasterKey* key;
152   key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
153   gabe_setup(&key->pub, &key->msk);
154   GNUNET_assert (NULL != key->pub);
155   GNUNET_assert (NULL != key->msk);
156   return key;
157 }
158
159 void
160 GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key)
161 {
162   gabe_msk_free (key->msk);
163   gabe_pub_free (key->pub);
164   //GNUNET_free (key->msk);
165   //gabe_msk_free (key->msk); //For some reason free of pub implicit?
166   GNUNET_free (key);
167 }
168
169 struct GNUNET_CRYPTO_AbeKey*
170 GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key,
171                              char **attrs)
172 {
173   struct GNUNET_CRYPTO_AbeKey *prv_key;
174   int size;
175   char *tmp;
176   
177   prv_key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
178   prv_key->prv = gabe_keygen(key->pub, key->msk, attrs);
179   size = gabe_pub_serialize(key->pub, &tmp);
180   prv_key->pub = gabe_pub_unserialize(tmp, size);
181   GNUNET_free (tmp);
182   GNUNET_assert (NULL != prv_key->prv);
183   return prv_key;
184 }
185
186 void
187 GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key,
188                                 int delete_pub)
189 {
190   //Memory management in gabe is buggy
191   gabe_prv_free (key->prv);
192   if (GNUNET_YES == delete_pub)
193     gabe_pub_free (key->pub);
194   GNUNET_free (key);
195 }
196
197 ssize_t
198 write_cpabe (void **result,
199              uint32_t file_len,
200              char* cph_buf,
201              int cph_buf_len,
202              char* aes_buf,
203              int aes_buf_len)
204 {
205   char *ptr;
206   uint32_t *len;
207   
208   *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
209   ptr = *result;
210   len = (uint32_t*) ptr;
211   *len = htonl (file_len);
212   ptr += 4;
213   len = (uint32_t*) ptr;
214   *len = htonl (aes_buf_len);
215   ptr += 4;
216   memcpy (ptr, aes_buf, aes_buf_len);
217   ptr += aes_buf_len;
218   len = (uint32_t*) ptr;
219   *len = htonl (cph_buf_len);
220   ptr += 4;
221   memcpy (ptr, cph_buf, cph_buf_len);
222   return 12 + cph_buf_len + aes_buf_len;
223 }
224
225 ssize_t
226 read_cpabe (const void *data,
227             char** cph_buf,
228             int *cph_buf_len,
229             char** aes_buf,
230             int *aes_buf_len)
231 {
232   int buf_len;
233   char *ptr;
234   uint32_t *len;
235
236   ptr = (char*)data;
237   len = (uint32_t*)ptr;
238   buf_len = ntohl (*len);
239   ptr += 4;
240   len = (uint32_t*)ptr;
241   *aes_buf_len = ntohl (*len);
242   ptr += 4;
243   *aes_buf = GNUNET_malloc (*aes_buf_len);
244   memcpy(*aes_buf, ptr, *aes_buf_len);
245   ptr += *aes_buf_len;
246   len = (uint32_t*)ptr;
247   *cph_buf_len = ntohl (*len);
248   ptr += 4;
249   *cph_buf = GNUNET_malloc (*cph_buf_len);
250   memcpy(*cph_buf, ptr, *cph_buf_len);
251
252   return buf_len;
253 }
254
255 ssize_t
256 GNUNET_CRYPTO_cpabe_encrypt (const void *block,
257                              size_t size,
258                              const char *policy,
259                              const struct GNUNET_CRYPTO_AbeMasterKey *key,
260                              void **result)
261 {
262   gabe_cph_t* cph;
263   char* plt;
264   char* cph_buf;
265   char* aes_buf;
266   element_t m;
267   int cph_buf_len;
268   int aes_buf_len;
269   ssize_t result_len;
270
271   if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
272     return GNUNET_SYSERR;
273   cph_buf_len = gabe_cph_serialize(cph,
274                                 &cph_buf);
275   gabe_cph_free(cph);
276   GNUNET_free (cph);
277   plt = GNUNET_memdup (block, size);
278   aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
279   GNUNET_free (plt);
280   element_clear(m);
281   result_len = write_cpabe(result, size, cph_buf, cph_buf_len, aes_buf, aes_buf_len);
282   GNUNET_free(cph_buf);
283   GNUNET_free(aes_buf);
284   return result_len;
285 }
286
287 ssize_t
288 GNUNET_CRYPTO_cpabe_decrypt (const void *block,
289                              size_t size,
290                              const struct GNUNET_CRYPTO_AbeKey *key,
291                              void **result)
292 {
293   char* aes_buf;
294   char* cph_buf;
295   gabe_cph_t* cph;
296   element_t m;
297   int cph_buf_size;
298   int aes_buf_size;
299   int plt_len;
300
301   read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
302   cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size);
303   if( !gabe_dec(key->pub, key->prv, cph, m) ) {
304     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305                 "%s\n", gabe_error());
306     GNUNET_free (aes_buf);
307     GNUNET_free (cph_buf);
308     gabe_cph_free(cph);
309     GNUNET_free (cph);
310     element_clear (m);
311     return GNUNET_SYSERR;
312   }
313   gabe_cph_free(cph);
314   GNUNET_free (cph);
315   plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result);
316   GNUNET_free (cph_buf);
317   GNUNET_free (aes_buf);
318   element_clear (m);
319   //freeing is buggy in gabe
320   //gabe_prv_free (prv);
321   //gabe_pub_free (pub);
322   return plt_len;
323 }
324
325 ssize_t
326 GNUNET_CRYPTO_cpabe_serialize_key (const struct GNUNET_CRYPTO_AbeKey *key,
327                                    void **result)
328 {
329   ssize_t len;
330   char *pub;
331   char *prv;
332   int pub_len;
333   int prv_len;
334
335   pub_len = gabe_pub_serialize (key->pub, &pub);
336   prv_len = gabe_prv_serialize (key->prv, &prv);
337
338   len = pub_len + prv_len + 12;
339   write_cpabe (result, len, pub, pub_len, prv, prv_len);
340
341   GNUNET_free (pub);
342   GNUNET_free (prv);
343
344   return len;
345 }
346
347 struct GNUNET_CRYPTO_AbeKey*
348 GNUNET_CRYPTO_cpabe_deserialize_key (const void *data,
349                                      size_t len)
350 {
351   struct GNUNET_CRYPTO_AbeKey *key;
352   char *pub;
353   char *prv;
354   int prv_len;
355   int pub_len;
356
357   key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
358   read_cpabe (data,
359               &pub,
360               &pub_len,
361               &prv,
362               &prv_len);
363   key->pub = gabe_pub_unserialize (pub, pub_len);
364   key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
365   
366   GNUNET_free (pub);
367   GNUNET_free (prv);
368   return key;
369 }
370
371 ssize_t
372 GNUNET_CRYPTO_cpabe_serialize_master_key (const struct GNUNET_CRYPTO_AbeMasterKey *key,
373                                           void **result)
374 {
375   ssize_t len;
376   char *pub;
377   char *msk;
378   int pub_len;
379   int msk_len;
380
381   pub_len = gabe_pub_serialize (key->pub, &pub);
382   msk_len = gabe_msk_serialize (key->msk, &msk);
383
384   len = pub_len + msk_len + 12;
385   write_cpabe (result, len, pub, pub_len, msk, msk_len);
386
387   GNUNET_free (pub);
388   GNUNET_free (msk);
389
390   return len;
391 }
392
393 struct GNUNET_CRYPTO_AbeMasterKey*
394 GNUNET_CRYPTO_cpabe_deserialize_master_key (const void *data,
395                                             size_t len)
396 {
397   struct GNUNET_CRYPTO_AbeMasterKey *key;
398   char *msk;
399   char *pub;
400   int msk_len;
401   int pub_len;
402
403   key = GNUNET_new (struct GNUNET_CRYPTO_AbeMasterKey);
404   read_cpabe (data,
405               &pub,
406               &pub_len,
407               &msk,
408               &msk_len);
409   key->pub = gabe_pub_unserialize (pub, pub_len);
410   key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);
411   
412   GNUNET_free (pub);
413   GNUNET_free (msk);
414
415   return key;
416 }