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