aboutsummaryrefslogtreecommitdiff
path: root/src/abe
diff options
context:
space:
mode:
authorSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>2018-01-03 10:11:40 +0100
committerSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>2018-01-03 10:11:40 +0100
commitfb85cf602c67994646c156aa9e05d2b9aa10816c (patch)
treeed14e694bb1ce9c11e7cebd0aa445ad41f7c2c7e /src/abe
parent7c1f035ed971e12882cd7a65c7d36883842945b1 (diff)
downloadgnunet-fb85cf602c67994646c156aa9e05d2b9aa10816c.tar.gz
gnunet-fb85cf602c67994646c156aa9e05d2b9aa10816c.zip
-move abe functionality out of util; prepare for release
Diffstat (limited to 'src/abe')
-rw-r--r--src/abe/Makefile.am50
-rw-r--r--src/abe/abe.c417
-rw-r--r--src/abe/test_cpabe.c87
3 files changed, 554 insertions, 0 deletions
diff --git a/src/abe/Makefile.am b/src/abe/Makefile.am
new file mode 100644
index 000000000..308e6c67c
--- /dev/null
+++ b/src/abe/Makefile.am
@@ -0,0 +1,50 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4plugindir = $(libdir)/gnunet
5
6libexecdir= $(pkglibdir)/libexec/
7
8pkgcfgdir= $(pkgdatadir)/config.d/
9
10dist_pkgcfg_DATA = \
11 abe.conf
12
13if USE_COVERAGE
14 AM_CFLAGS = --coverage -O0
15 XLIB = -lgcov
16endif
17
18libgnunetabe_la_SOURCES = abe.c
19
20libgnunetabe_la_LIBADD = \
21 $(GCLIBADD)\
22 $(LIBGCRYPT_LIBS) \
23 $(LTLIBICONV) \
24 $(LTLIBINTL) \
25 $(ABE_LIBADD) \
26 -lgabe \
27 -lpbc \
28 -lglib-2.0 \
29 -lltdl $(Z_LIBS) -lunistring $(XLIB)
30
31libgnunetabe_la_LDFLAGS = \
32 $(GN_LIB_LDFLAGS) \
33 -version-info 1:0:0
34
35lib_LTLIBRARIES = libgnunetabe.la
36
37if ENABLE_TEST_RUN
38AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME;
39TESTS = $(check_PROGRAMS)
40endif
41
42check_PROGRAMS = test_cpabe
43
44test_cpabe_SOURCES = \
45 test_cpabe.c
46test_cpabe_LDADD = \
47 libgnunetabe.la \
48 $(top_builddir)/src/util/libgnunetutil.la
49check_PROGRAMS += \
50 test_cpabe
diff --git a/src/abe/abe.c b/src/abe/abe.c
new file mode 100644
index 000000000..d008cc522
--- /dev/null
+++ b/src/abe/abe.c
@@ -0,0 +1,417 @@
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#include "gnunet_abe_lib.h"
35
36struct GNUNET_ABE_AbeMasterKey
37{
38 gabe_pub_t* pub;
39 gabe_msk_t* msk;
40};
41
42struct GNUNET_ABE_AbeKey
43{
44 gabe_pub_t* pub;
45 gabe_prv_t* prv;
46};
47
48static int
49init_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
80static int
81aes_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
116static int
117aes_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
149struct GNUNET_ABE_AbeMasterKey*
150GNUNET_ABE_cpabe_create_master_key (void)
151{
152 struct GNUNET_ABE_AbeMasterKey* key;
153 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
154 gabe_setup(&key->pub, &key->msk);
155 GNUNET_assert (NULL != key->pub);
156 GNUNET_assert (NULL != key->msk);
157 return key;
158}
159
160void
161GNUNET_ABE_cpabe_delete_master_key (struct GNUNET_ABE_AbeMasterKey *key)
162{
163 gabe_msk_free (key->msk);
164 gabe_pub_free (key->pub);
165 //GNUNET_free (key->msk);
166 //gabe_msk_free (key->msk); //For some reason free of pub implicit?
167 GNUNET_free (key);
168}
169
170struct GNUNET_ABE_AbeKey*
171GNUNET_ABE_cpabe_create_key (struct GNUNET_ABE_AbeMasterKey *key,
172 char **attrs)
173{
174 struct GNUNET_ABE_AbeKey *prv_key;
175 int size;
176 char *tmp;
177
178 prv_key = GNUNET_new (struct GNUNET_ABE_AbeKey);
179 prv_key->prv = gabe_keygen(key->pub, key->msk, attrs);
180 size = gabe_pub_serialize(key->pub, &tmp);
181 prv_key->pub = gabe_pub_unserialize(tmp, size);
182 GNUNET_free (tmp);
183 GNUNET_assert (NULL != prv_key->prv);
184 return prv_key;
185}
186
187void
188GNUNET_ABE_cpabe_delete_key (struct GNUNET_ABE_AbeKey *key,
189 int delete_pub)
190{
191 //Memory management in gabe is buggy
192 gabe_prv_free (key->prv);
193 if (GNUNET_YES == delete_pub)
194 gabe_pub_free (key->pub);
195 GNUNET_free (key);
196}
197
198ssize_t
199write_cpabe (void **result,
200 uint32_t file_len,
201 char* cph_buf,
202 int cph_buf_len,
203 char* aes_buf,
204 int aes_buf_len)
205{
206 char *ptr;
207 uint32_t *len;
208
209 *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
210 ptr = *result;
211 len = (uint32_t*) ptr;
212 *len = htonl (file_len);
213 ptr += 4;
214 len = (uint32_t*) ptr;
215 *len = htonl (aes_buf_len);
216 ptr += 4;
217 memcpy (ptr, aes_buf, aes_buf_len);
218 ptr += aes_buf_len;
219 len = (uint32_t*) ptr;
220 *len = htonl (cph_buf_len);
221 ptr += 4;
222 memcpy (ptr, cph_buf, cph_buf_len);
223 return 12 + cph_buf_len + aes_buf_len;
224}
225
226ssize_t
227read_cpabe (const void *data,
228 char** cph_buf,
229 int *cph_buf_len,
230 char** aes_buf,
231 int *aes_buf_len)
232{
233 int buf_len;
234 char *ptr;
235 uint32_t *len;
236
237 ptr = (char*)data;
238 len = (uint32_t*)ptr;
239 buf_len = ntohl (*len);
240 ptr += 4;
241 len = (uint32_t*)ptr;
242 *aes_buf_len = ntohl (*len);
243 ptr += 4;
244 *aes_buf = GNUNET_malloc (*aes_buf_len);
245 memcpy(*aes_buf, ptr, *aes_buf_len);
246 ptr += *aes_buf_len;
247 len = (uint32_t*)ptr;
248 *cph_buf_len = ntohl (*len);
249 ptr += 4;
250 *cph_buf = GNUNET_malloc (*cph_buf_len);
251 memcpy(*cph_buf, ptr, *cph_buf_len);
252
253 return buf_len;
254}
255
256ssize_t
257GNUNET_ABE_cpabe_encrypt (const void *block,
258 size_t size,
259 const char *policy,
260 const struct GNUNET_ABE_AbeMasterKey *key,
261 void **result)
262{
263 gabe_cph_t* cph;
264 char* plt;
265 char* cph_buf;
266 char* aes_buf;
267 element_t m;
268 int cph_buf_len;
269 int aes_buf_len;
270 ssize_t result_len;
271
272 if( !(cph = gabe_enc(key->pub, m, (char*)policy)) )
273 return GNUNET_SYSERR;
274 cph_buf_len = gabe_cph_serialize(cph,
275 &cph_buf);
276 gabe_cph_free(cph);
277 GNUNET_free (cph);
278 plt = GNUNET_memdup (block, size);
279 aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
280 GNUNET_free (plt);
281 element_clear(m);
282 result_len = write_cpabe(result, size, cph_buf, cph_buf_len, aes_buf, aes_buf_len);
283 GNUNET_free(cph_buf);
284 GNUNET_free(aes_buf);
285 return result_len;
286}
287
288ssize_t
289GNUNET_ABE_cpabe_decrypt (const void *block,
290 size_t size,
291 const struct GNUNET_ABE_AbeKey *key,
292 void **result)
293{
294 char* aes_buf;
295 char* cph_buf;
296 gabe_cph_t* cph;
297 element_t m;
298 int cph_buf_size;
299 int aes_buf_size;
300 int plt_len;
301
302 read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
303 cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size);
304 if( !gabe_dec(key->pub, key->prv, cph, m) ) {
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "%s\n", gabe_error());
307 GNUNET_free (aes_buf);
308 GNUNET_free (cph_buf);
309 gabe_cph_free(cph);
310 GNUNET_free (cph);
311 element_clear (m);
312 return GNUNET_SYSERR;
313 }
314 gabe_cph_free(cph);
315 GNUNET_free (cph);
316 plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result);
317 GNUNET_free (cph_buf);
318 GNUNET_free (aes_buf);
319 element_clear (m);
320 //freeing is buggy in gabe
321 //gabe_prv_free (prv);
322 //gabe_pub_free (pub);
323 return plt_len;
324}
325
326ssize_t
327GNUNET_ABE_cpabe_serialize_key (const struct GNUNET_ABE_AbeKey *key,
328 void **result)
329{
330 ssize_t len;
331 char *pub;
332 char *prv;
333 int pub_len;
334 int prv_len;
335
336 pub_len = gabe_pub_serialize (key->pub, &pub);
337 prv_len = gabe_prv_serialize (key->prv, &prv);
338
339 len = pub_len + prv_len + 12;
340 write_cpabe (result, len, pub, pub_len, prv, prv_len);
341
342 GNUNET_free (pub);
343 GNUNET_free (prv);
344
345 return len;
346}
347
348struct GNUNET_ABE_AbeKey*
349GNUNET_ABE_cpabe_deserialize_key (const void *data,
350 size_t len)
351{
352 struct GNUNET_ABE_AbeKey *key;
353 char *pub;
354 char *prv;
355 int prv_len;
356 int pub_len;
357
358 key = GNUNET_new (struct GNUNET_ABE_AbeKey);
359 read_cpabe (data,
360 &pub,
361 &pub_len,
362 &prv,
363 &prv_len);
364 key->pub = gabe_pub_unserialize (pub, pub_len);
365 key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
366
367 GNUNET_free (pub);
368 GNUNET_free (prv);
369 return key;
370}
371
372ssize_t
373GNUNET_ABE_cpabe_serialize_master_key (const struct GNUNET_ABE_AbeMasterKey *key,
374 void **result)
375{
376 ssize_t len;
377 char *pub;
378 char *msk;
379 int pub_len;
380 int msk_len;
381
382 pub_len = gabe_pub_serialize (key->pub, &pub);
383 msk_len = gabe_msk_serialize (key->msk, &msk);
384
385 len = pub_len + msk_len + 12;
386 write_cpabe (result, len, pub, pub_len, msk, msk_len);
387
388 GNUNET_free (pub);
389 GNUNET_free (msk);
390
391 return len;
392}
393
394struct GNUNET_ABE_AbeMasterKey*
395GNUNET_ABE_cpabe_deserialize_master_key (const void *data,
396 size_t len)
397{
398 struct GNUNET_ABE_AbeMasterKey *key;
399 char *msk;
400 char *pub;
401 int msk_len;
402 int pub_len;
403
404 key = GNUNET_new (struct GNUNET_ABE_AbeMasterKey);
405 read_cpabe (data,
406 &pub,
407 &pub_len,
408 &msk,
409 &msk_len);
410 key->pub = gabe_pub_unserialize (pub, pub_len);
411 key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);
412
413 GNUNET_free (pub);
414 GNUNET_free (msk);
415
416 return key;
417}
diff --git a/src/abe/test_cpabe.c b/src/abe/test_cpabe.c
new file mode 100644
index 000000000..9b2062b23
--- /dev/null
+++ b/src/abe/test_cpabe.c
@@ -0,0 +1,87 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2002, 2003, 2004, 2006 GNUnet e.V.
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 * @author Martin Schanzenbach
23 * @file util/test_crypto_abe.c
24 * @brief test for ABE ciphers
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_abe_lib.h"
29
30#define TESTSTRING "Hello World!"
31
32static int
33testAbecipher ()
34{
35 struct GNUNET_ABE_AbeMasterKey *msk;
36 struct GNUNET_ABE_AbeKey *key;
37 char *result;
38 char **attrs;
39 int size;
40 char *res;
41 msk = GNUNET_ABE_cpabe_create_master_key ();
42 size = GNUNET_ABE_cpabe_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
43 "testattr", //Policy
44 msk,
45 (void*)&result);
46 GNUNET_assert (-1 != size);
47 attrs = GNUNET_malloc (2 * sizeof (char*));
48 attrs[0] = "testattr";
49 attrs[1] = NULL;
50 key = GNUNET_ABE_cpabe_create_key (msk,
51 attrs);
52
53 size = GNUNET_ABE_cpabe_decrypt (result, size,
54 key,
55 (void*)&res);
56 if (strlen (TESTSTRING) + 1 != size)
57 {
58 printf ("abeciphertest failed: decryptBlock returned %d\n", size);
59 return 1;
60 }
61 if (0 != strcmp (res, TESTSTRING))
62 {
63 printf ("abeciphertest failed: %s != %s\n", res, TESTSTRING);
64 return 1;
65 }
66 else
67 return 0;
68}
69
70
71int
72main (int argc, char *argv[])
73{
74 int failureCount = 0;
75
76 GNUNET_log_setup ("test-crypto-abe", "WARNING", NULL);
77 failureCount += testAbecipher ();
78
79 if (failureCount != 0)
80 {
81 printf ("%d TESTS FAILED!\n", failureCount);
82 return -1;
83 }
84 return 0;
85}
86
87/* end of test_crypto_aes.c */