aboutsummaryrefslogtreecommitdiff
path: root/src/abe
diff options
context:
space:
mode:
authorSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>2018-01-04 16:07:55 +0100
committerSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>2018-01-04 16:07:55 +0100
commit056ca89d207cd1865a90fe3fcd430a4381097da5 (patch)
tree928a3f7bf520ec99ac1bdbceb423dd68d375acbb /src/abe
parentb4f6c5a8b521d4942d19f5a34929bebaa60a6210 (diff)
parent78705d5a49d7066a01b832546d2507456a3c5d2c (diff)
downloadgnunet-056ca89d207cd1865a90fe3fcd430a4381097da5.tar.gz
gnunet-056ca89d207cd1865a90fe3fcd430a4381097da5.zip
Merge remote-tracking branch 'origin/identity_abe'
Diffstat (limited to 'src/abe')
-rw-r--r--src/abe/Makefile.am50
-rw-r--r--src/abe/abe.c499
-rw-r--r--src/abe/test_cpabe.c87
3 files changed, 636 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..3f1f6dc5b
--- /dev/null
+++ b/src/abe/abe.c
@@ -0,0 +1,499 @@
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
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
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 */
155struct GNUNET_ABE_AbeMasterKey*
156GNUNET_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 */
173void
174GNUNET_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 */
191struct GNUNET_ABE_AbeKey*
192GNUNET_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 */
216void
217GNUNET_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
227static ssize_t
228write_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
255static ssize_t
256read_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 */
296ssize_t
297GNUNET_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 */
338ssize_t
339GNUNET_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 */
384ssize_t
385GNUNET_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 */
414struct GNUNET_ABE_AbeKey*
415GNUNET_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 */
446ssize_t
447GNUNET_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 */
476struct GNUNET_ABE_AbeMasterKey*
477GNUNET_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}
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 */