-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibfenc_WatersCP.c
1693 lines (1437 loc) · 62.9 KB
/
libfenc_WatersCP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file libfenc_WatersCP.c
*
* \brief Routines for the Waters CP-ABE scheme.
*
* Copyright 2009 Matthew Green. All rights reserved.
*/
#include <stdlib.h>
#include <string.h>
#include <pbc/pbc.h>
#include "libfenc.h"
#include "libfenc_group_params.h"
#include "libfenc_ABE_common.h"
#include "libfenc_utils.h"
#include "libfenc_WatersCP.h"
#include "libfenc_LSSS.h"
#include "libfenc_LSW.h"
#include <time.h>
/********************************************************************************
* Waters Ciphertext-Policy Implementation
********************************************************************************/
/*!
* Initialize a fenc_context data structure for use with the Waters scheme.
* Any number of fenc_context structures may be simultaneously used, with the same
* or different schemes. The caller assumes responsible for allocating the context
* buffer.
*
* @param context Pre-allocated buffer for the fenc_context data structure.
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_create_context_WatersCP(fenc_context *context)
{
CHECK_LIBRARY_STATE;
FENC_ERROR result = FENC_ERROR_UNKNOWN;
/* Allocate a scheme-specific context. */
context->scheme_context = SAFE_MALLOC( sizeof(fenc_scheme_context_WatersCP) );
if (context->scheme_context != NULL) {
/* Set up the scheme context. */
memset(context->scheme_context, 0, sizeof(fenc_scheme_context_WatersCP) );
/* TODO */
result = FENC_ERROR_NONE;
} else {
/* Couldn't allocate scheme context. */
result = FENC_ERROR_OUT_OF_MEMORY;
}
/* Configure function pointers within the fenc_context to point to
* LSW scheme-specific routines. */
if (result == FENC_ERROR_NONE) {
context->gen_params = libfenc_gen_params_WatersCP;
context->set_params = libfenc_set_params_WatersCP;
context->extract_key = libfenc_extract_key_WatersCP;
context->encrypt = libfenc_encrypt_WatersCP;
context->kem_encrypt = libfenc_kem_encrypt_WatersCP;
context->decrypt = libfenc_decrypt_WatersCP;
context->destroy_context = libfenc_destroy_context_WatersCP;
context->generate_global_params = libfenc_generate_global_params_COMMON;
context->destroy_global_params = libfenc_destroy_global_params_COMMON;
context->export_public_params = libfenc_export_public_params_WatersCP;
context->export_secret_params = libfenc_export_secret_params_WatersCP;
context->import_public_params = libfenc_import_public_params_WatersCP;
context->import_secret_params = libfenc_import_secret_params_WatersCP;
context->export_global_params = libfenc_export_global_params_WatersCP;
context->import_global_params = libfenc_import_global_params_WatersCP;
context->export_secret_key = libfenc_export_secret_key_WatersCP;
context->import_secret_key = libfenc_import_secret_key_WatersCP;
}
/* Return success/error. */
return result;
}
/*!
* Generate public and secret parameters.
*
* @param context The fenc_context data structure
* @param global_params Global params (scheme-specific).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_gen_params_WatersCP(fenc_context *context, fenc_global_params *global_params)
{
FENC_ERROR result = FENC_ERROR_UNKNOWN, err_code = FENC_ERROR_NONE;
element_t eggT, aZ;
fenc_scheme_context_WatersCP* scheme_context;
Bool elements_initialized = FALSE;
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
result = FENC_ERROR_INVALID_CONTEXT;
goto cleanup;
}
/* Validate the global parameters. */
err_code = libfenc_validate_global_params_WatersCP(global_params);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not validate global params, error: %s", __func__, libfenc_error_to_string(err_code));
result = err_code;
goto cleanup;
}
/* Global parameters check out ok. Copy them and generate the scheme-specific parameters. The NULL
* parameter causes the structure to be allocated. */
scheme_context->global_params = initialize_global_params_WatersCP(global_params->group_params, NULL);
/* Initialize the elements in the public and secret parameters, along with some temporary variables. */
public_params_initialize_WatersCP(&(scheme_context->public_params), scheme_context->global_params->pairing);
secret_params_initialize_WatersCP(&(scheme_context->secret_params), scheme_context->global_params->pairing);
element_init_GT(eggT, scheme_context->global_params->pairing);
element_init_Zr(aZ, scheme_context->global_params->pairing);
elements_initialized = TRUE;
/* Select randoms generators g1 \in G1, g2 \in G2, a, \alpha */
element_random(scheme_context->public_params.gONE);
element_random(scheme_context->public_params.gTWO);
element_random(scheme_context->secret_params.alphaZ);
element_random(aZ);
element_random(scheme_context->secret_params.alphaZ);
/* Compute g^a, */
element_pow_zn(scheme_context->public_params.gaONE, scheme_context->public_params.gONE, aZ); /* gaONE = gONE^a */
element_pow_zn(scheme_context->public_params.gaTWO, scheme_context->public_params.gTWO, aZ); /* gaTWO = gaTWO^a */
/* Compute eggalphaT = e(gONE,gTWO)^(alpha) */
pairing_apply(eggT, scheme_context->public_params.gONE, scheme_context->public_params.gTWO, scheme_context->global_params->pairing); /* eggT = e(gONE, gTWO) */
element_pow_zn(scheme_context->public_params.eggalphaT, eggT, scheme_context->secret_params.alphaZ); /* eggalphaT = eggT^alpha */
/* Success */
result = FENC_ERROR_NONE;
cleanup:
if (elements_initialized == TRUE) {
/* Destroy any temporary elements. */
element_clear(eggT);
element_clear(aZ);
}
return result;
}
/*!
* Load public and (optionally) secret parameters into the context. All relevant global
* parameters are embedded within the public_params data structure.
*
* @param context The fenc_context data structure
* @param public_params Public scheme parameters.
* @param secret_params Secret scheme parameters (optional).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_set_params_WatersCP(fenc_context *context, fenc_public_params *public_params, fenc_secret_params *secret_params)
{
return FENC_ERROR_NOT_IMPLEMENTED;
}
/*!
* Extract a secret key representing a given function input, which is defined as an access structure.
* Note that this function will only be called if the secret parameters (MSK) are available within
* the context.
*
* @param context The fenc_context data structure
* @param input The function input from which this key will be built.
* @param key A pre-allocated buffer for the resulting key
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_extract_key_WatersCP(fenc_context *context, fenc_function_input *input, fenc_key *key)
{
FENC_ERROR result = FENC_ERROR_UNKNOWN, err_code = FENC_ERROR_NONE;
fenc_key_WatersCP *key_WatersCP = NULL;
fenc_attribute_list *attribute_list = NULL;
fenc_scheme_context_WatersCP* scheme_context;
int i;
element_t tZ, tempONE, tempTWO, temp2TWO;
Bool elements_initialized = FALSE;
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
result = FENC_ERROR_INVALID_CONTEXT;
goto cleanup;
}
/* Parse the function input as an attribute list. This will allocate memory
* that will ultimately be released when the key is cleared. */
attribute_list = SAFE_MALLOC(sizeof(fenc_attribute_list));
err_code = libfenc_parse_input_as_attribute_list(input, attribute_list, scheme_context->global_params->pairing);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not parse function input as an attribute list", __func__);
result = FENC_ERROR_INVALID_INPUT;
goto cleanup;
}
/* Initialize the LSW-specific key data structure and allocate some temporary variables. */
key_WatersCP = fenc_key_WatersCP_initialize(attribute_list, FALSE, scheme_context->global_params);
if (key_WatersCP == NULL) {
LOG_ERROR("%s: could not initialize key structure", __func__);
result = FENC_ERROR_INVALID_INPUT;
goto cleanup;
}
element_init_Zr(tZ, scheme_context->global_params->pairing);
element_init_G1(tempONE, scheme_context->global_params->pairing);
element_init_G2(tempTWO, scheme_context->global_params->pairing);
element_init_G2(temp2TWO, scheme_context->global_params->pairing);
elements_initialized = TRUE;
/* Select a random tZ. */
element_random(tZ);
/* Compute KTWO = gTWO^{\alpha} * gTWO^{at}. */
element_pow_zn(tempTWO, scheme_context->public_params.gaTWO, tZ); /* tempTWO = gTWO^{at} */
element_pow_zn(temp2TWO, scheme_context->public_params.gTWO, scheme_context->secret_params.alphaZ); /* temp2TWO = gTWO^\alpha */
element_mul(key_WatersCP->KTWO, tempTWO, temp2TWO); /* KONE = tempTWO * temp2TWO */
/* Compute LTWO = gTWO^t. */
element_pow_zn(key_WatersCP->LTWO, scheme_context->public_params.gTWO, tZ); /* LTWO = gTWO^{t} */
/* For every share/attribute, create one component of the secret key. */
for (i = 0; i < (signed int)key_WatersCP->attribute_list.num_attributes; i++) {
/* Hash the attribute string to Zr, if it hasn't already been. */
hash_attribute_string_to_Zr(&(key_WatersCP->attribute_list.attribute[i]), scheme_context->global_params->pairing);
/* Now hash the result into an element of G1 (tempONE). */
err_code = hash2_attribute_element_to_G1(&(key_WatersCP->attribute_list.attribute[i].attribute_hash), &tempONE); /* result in tempONE */
/* Compute KXONE[i] = tempONE^{t}. */
element_pow_zn(key_WatersCP->KXONE[i], tempONE, tZ); /* KXONE[i] = tempONE^{t} */
}
/* Stash the key_WatersCP structure inside of the fenc_key. */
memset(key, 0, sizeof(fenc_key));
key->scheme_type = FENC_SCHEME_WATERSCP;
key->valid = TRUE;
key->scheme_key = (void*)key_WatersCP;
/* Success! */
result = FENC_ERROR_NONE;
cleanup:
/* If there was an error, clean up after ourselves. */
if (result != FENC_ERROR_NONE) {
if (key_WatersCP != NULL) {
fenc_attribute_list_clear(&(key_WatersCP->attribute_list));
/* Clear out the key internals. */
if (elements_initialized == TRUE) {
fenc_key_WatersCP_clear(key_WatersCP);
}
}
}
/* Wipe out temporary variables. */
if (elements_initialized == TRUE) {
element_clear(tZ);
element_clear(tempONE);
element_clear(tempTWO);
element_clear(temp2TWO);
}
return result;
}
/*!
* Encrypt a plaintext, return a ciphertext.
*
* @param context The fenc_context data structure
* @param input The function input under which which the ciphertext will be encrypted.
* @param plaintext The plaintext message.
* @param ciphertext A pre-allocated buffer for the returned fenc_ciphertext.
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_encrypt_WatersCP(fenc_context *context, fenc_function_input *input, fenc_plaintext *plaintext,
fenc_ciphertext *ciphertext)
{
return encrypt_WatersCP_internal(context, input, plaintext, FALSE, NULL, 0, ciphertext);
}
/*!
* Key encapsulation variant of encryption. Generate an encryption key and encapsulate it under
* a given function input. Returns the encapsulated key as well as the ciphertext.
*
* @param context The fenc_context data structure
* @param input The function input under which which the ciphertext will be encrypted.
* @param key_len Desired key size (in bytes). Will be overwritten with the actual key size.
* @param key Pointer to an initialized buffer into which the key will be written.
* @param ciphertext A pre-allocated buffer for the returned fenc_ciphertext.
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_kem_encrypt_WatersCP(fenc_context *context, fenc_function_input *input, size_t key_len,
uint8* key, fenc_ciphertext *ciphertext)
{
return encrypt_WatersCP_internal(context, input, NULL, TRUE, key, key_len, ciphertext);
}
/*!
* Decrypt a ciphertext using a specified secret key.
*
* @param context The fenc_context data structure
* @param ciphertext The ciphertext to decrypt.
* @param key The secret key to use.
* @param plaintext A pre-allocated buffer for the resulting plaintext.
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_decrypt_WatersCP(fenc_context *context, fenc_ciphertext *ciphertext, fenc_key *key,
fenc_plaintext *plaintext)
{
FENC_ERROR result = FENC_ERROR_UNKNOWN, err_code;
fenc_ciphertext_WatersCP ciphertext_WatersCP;
fenc_scheme_context_WatersCP *scheme_context = NULL;
fenc_key_WatersCP *key_WatersCP = NULL;
fenc_attribute_list attribute_list;
fenc_attribute_policy policy;
fenc_lsss_coefficient_list coefficient_list;
element_t tempGT, temp2GT, tempONE, temp2ONE, temp3ONE, tempZ, temp2Z, prodONE;
element_t temp3GT, temp4GT, prodT, finalT;
uint32 i;
int32 index_ciph, index_key;
Bool elements_initialized = FALSE, coefficients_initialized = FALSE;
Bool attribute_list_N_initialized = FALSE;
// char test_str[MAX_POLICY_STR];
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
return FENC_ERROR_INVALID_CONTEXT;
}
/* Obtain the WatersCP-specific key data structure and make sure it's correct. */
if (key->scheme_key == NULL) {
LOG_ERROR("%s: could not obtain scheme-specific decryption key", __func__);
return FENC_ERROR_INVALID_KEY;
}
key_WatersCP = (fenc_key_WatersCP*)key->scheme_key;
err_code = attribute_list_compute_hashes(&(key_WatersCP->attribute_list), scheme_context->global_params->pairing);
/* Deserialize the ciphertext. */
err_code = libfenc_deserialize_ciphertext_WatersCP(ciphertext->data, ciphertext->data_len, &ciphertext_WatersCP, scheme_context);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: unable to deserialize ciphertext", __func__);
result = err_code;
goto cleanup;
}
#ifdef FENC_DEBUG
// libfenc_fprint_ciphertext_WatersCP(&ciphertext_WatersCP, stdout);
#endif
/* Now deserialize the policy string into a data structure and make sure all attributes are hashed. */
fenc_policy_from_string(&policy, ciphertext_WatersCP.policy_str);
#ifdef FENC_DEBUG
// strcpy(test_str, "");
// fenc_attribute_policy_to_string(policy.root, test_str, MAX_POLICY_STR);
// printf("PARSED ATTRIBUTE STRING: %s\n", test_str);
#endif
err_code = attribute_tree_compute_hashes(policy.root, scheme_context->global_params->pairing);
//libfenc_fprint_ciphertext_WatersCP(&ciphertext_WatersCP, stdout);
/* Use the LSSS-associated procedure to recover the coefficients. This gives us a list of coefficients
* that should match up in a 1-to-1 fashion with the components of the decryption key.
* First, initialize a coefficients list: */
err_code = LSSS_allocate_coefficient_list(&coefficient_list, ciphertext_WatersCP.attribute_list.num_attributes, scheme_context->global_params->pairing);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not allocate coefficients", __func__);
result = err_code;
goto cleanup;
}
coefficients_initialized = TRUE;
/* Now calculate the actual coefficients. */
err_code = fenc_LSSS_calculate_coefficients_from_policy(&policy, &(key_WatersCP->attribute_list), &coefficient_list,
scheme_context->global_params->pairing);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: unable to compute LSSS coefficients", __func__);
result = FENC_ERROR_INVALID_CIPHERTEXT;
goto cleanup;
}
/* Allocate some temporary work variables. */
elements_initialized = TRUE;
element_init_GT(tempGT, scheme_context->global_params->pairing);
element_init_GT(temp2GT, scheme_context->global_params->pairing);
element_init_GT(temp3GT, scheme_context->global_params->pairing);
element_init_GT(temp4GT, scheme_context->global_params->pairing);
element_init_GT(prodT, scheme_context->global_params->pairing);
element_init_GT(finalT, scheme_context->global_params->pairing);
element_init_G1(prodONE, scheme_context->global_params->pairing);
element_init_G1(tempONE, scheme_context->global_params->pairing);
element_init_G1(temp2ONE, scheme_context->global_params->pairing);
element_init_G1(temp3ONE, scheme_context->global_params->pairing);
element_init_Zr(tempZ, scheme_context->global_params->pairing);
element_init_Zr(temp2Z, scheme_context->global_params->pairing);
/* Now compute the product of the sub-components raised to the coefficients:
* prodT = \prod_{i=0}^num_attributes (Z_i^{coefficient[i]}).
* We compute one of these for every value in attribute_list_N. */
element_set1(prodT);
element_set1(prodONE);
for (i = 0; i < coefficient_list.num_coefficients; i++) {
if (coefficient_list.coefficients[i].is_set == TRUE) {
/*
* Let index_ciph be the attribute's index in the ciphertext structure.
* Let index_key be the attribute's index in the key structure.
*
* Compute finalT = e(CONE[index_ciph] , LTWO) * e(DTWO[index_ciph] , KXONE[index_key]). */
/* Find the corresponding indices in the ciphertext attribute list and in the key. */
DEBUG_ELEMENT_PRINTF("running libfenc_get_attribute_index_in_list\n");
index_key = libfenc_get_attribute_index_in_list(&(ciphertext_WatersCP.attribute_list.attribute[i]), &(key_WatersCP->attribute_list));
index_ciph = i;/*libfenc_get_attribute_index_in_list(&(attribute_list.attribute[i]), &(key_WatersCP.attribute_list));*/
if (index_ciph < 0 || index_key < 0) {
DEBUG_ELEMENT_PRINTF("ciphertext element %d=%B\n", index_ciph, ciphertext_WatersCP.attribute_list.attribute[i].attribute_hash);
DEBUG_ELEMENT_PRINTF("ciphertext element %d=%s\n", index_ciph, ciphertext_WatersCP.attribute_list.attribute[i].attribute_str);
LOG_ERROR("%s: could not find attribute in key and ciphertext (k=%d,c=%d)", __func__, index_key, index_ciph);
/*DEBUG_ELEMENT_PRINTF("attribute(%s)=%B\n", attribute_list.attribute[i].attribute_str, attribute_list.attribute[i].attribute_hash);*/
result = FENC_ERROR_INVALID_INPUT;
goto cleanup;
}
/* Compute prodONE *= CONE[index_ciph]^{coefficient[i]} */
element_pow_zn(tempONE, ciphertext_WatersCP.CONE[index_ciph], coefficient_list.coefficients[i].coefficient);
element_mul(temp2ONE, tempONE, prodONE);
element_set(prodONE, temp2ONE);
/* Compute finalT = e(KXONE[index_key]^{coefficient[i]}, DTWO[index_ciph]). */
element_pow_zn(tempONE, key_WatersCP->KXONE[index_key], coefficient_list.coefficients[i].coefficient);
pairing_apply(finalT, tempONE, ciphertext_WatersCP.DTWO[index_ciph], scheme_context->global_params->pairing);
/* We computed the intermediate value as "finalT".
* Now compute prodT *= finalT. */
element_mul(temp2GT, finalT, prodT);
element_set(prodT, temp2GT);
} /* end of if coefficient.is_set clause */
} /* end of for clause */
/* Now compute prodT *= e(prodONE, LTWO). */
pairing_apply(finalT, prodONE, key_WatersCP->LTWO, scheme_context->global_params->pairing);
element_mul(temp2GT, finalT, prodT);
element_set(prodT, temp2GT);
/* Final computation: prodT = e(CprimeONE, KTWO) / prodT. */
pairing_apply(tempGT, ciphertext_WatersCP.CprimeONE, key_WatersCP->KTWO, scheme_context->global_params->pairing);
element_invert(temp2GT, prodT);
element_mul(prodT, tempGT, temp2GT);
/* Finally, hash this result to obtain the KEM decryption. Full encryption is for the future. */
if (ciphertext_WatersCP.type == FENC_CIPHERTEXT_TYPE_KEM_CPA) {
/* If its a KEM, hash prodT and that's the resulting session key. */
err_code = derive_session_key_from_element(plaintext, prodT, ciphertext_WatersCP.kem_key_len, scheme_context->global_params->pairing);
if (err_code != FENC_ERROR_NONE) {
result = err_code;
goto cleanup;
}
} else {
LOG_ERROR("%s: only KEM mode is supported at this point", __func__);
result = FENC_ERROR_NOT_IMPLEMENTED;
goto cleanup;
}
/* Success! */
result = FENC_ERROR_NONE;
cleanup:
/* If the coefficient list was allocated/initialized, we have to clear it. */
if (coefficients_initialized == TRUE) {
LSSS_clear_coefficients_list(&coefficient_list);
}
/* Clear temporary variables. */
if (elements_initialized == TRUE) {
element_clear(finalT);
element_clear(prodT);
element_clear(tempGT);
element_clear(temp2GT);
element_clear(temp3GT);
element_clear(temp4GT);
element_clear(prodONE);
element_clear(tempONE);
element_clear(temp2ONE);
element_clear(temp3ONE);
element_clear(tempZ);
element_clear(temp2Z);
}
/* Clear out the attribute_list structure. */
if (attribute_list_N_initialized == TRUE) {
fenc_attribute_list_clear(&(attribute_list));
}
return result;
}
FENC_ERROR
libfenc_retrieve_attribute_policy(fenc_context *context, fenc_ciphertext *ciphertext, uint8 *buffer, size_t buf_len)
{
FENC_ERROR result = FENC_ERROR_NONE, err_code;
fenc_ciphertext_WatersCP ciphertext_WatersCP;
fenc_scheme_context_WatersCP *scheme_context = NULL;
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
return FENC_ERROR_INVALID_CONTEXT;
}
/* Deserialize the ciphertext. */
err_code = libfenc_deserialize_ciphertext_WatersCP(ciphertext->data, ciphertext->data_len, &ciphertext_WatersCP, scheme_context);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: unable to deserialize ciphertext", __func__);
result = err_code;
return FENC_ERROR_INVALID_CIPHERTEXT;
}
if(strlen(ciphertext_WatersCP.policy_str) < buf_len) {
memcpy(buffer, ciphertext_WatersCP.policy_str, strlen(ciphertext_WatersCP.policy_str));
}
return FENC_ERROR_NONE;
}
/*!
* Internal function for computing a ciphertext. In key-encapsulation mode this function
* returns a key and a buffer. In standard mode it encrypts a given plaintext.
*
* @param context The fenc_context data structure
* @param input The function input under which which the ciphertext will be encrypted.
* @param plaintext The plaintext message.
* @param kem_mode Set to "TRUE" if using KEM mode, false for normal encryption.
* @param kem_key_buf Buffer for the returned session key (KEM mode only).
* @param kem_key_len Pointer to a key length; input is desired, overwritten with actual length.
* @param ciphertext A pre-allocated buffer for the returned fenc_ciphertext.
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
encrypt_WatersCP_internal(fenc_context *context, fenc_function_input *input, fenc_plaintext *plaintext,
Bool kem_mode, uint8* kem_key_buf, size_t kem_key_len, fenc_ciphertext *ciphertext)
{
FENC_ERROR result = FENC_ERROR_UNKNOWN, err_code = FENC_ERROR_NONE;
fenc_attribute_policy *policy = NULL;
fenc_scheme_context_WatersCP* scheme_context = NULL;
fenc_ciphertext_WatersCP ciphertext_WatersCP;
element_t rZ, sZ, eggalphasT;
element_t tempONE, temp2ONE;
uint32 i;
Bool elements_initialized = FALSE;
size_t serialized_len = 0;
fenc_attribute_list attribute_list;
// char temp_policy_str[MAX_POLICY_STR];
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
result = FENC_ERROR_INVALID_CONTEXT;
goto cleanup;
}
/* Parse the function input as an attribute policy. This will allocate memory
* that will ultimately be released when the key is cleared. */
policy = (fenc_attribute_policy*)SAFE_MALLOC(sizeof(fenc_attribute_policy));
err_code = libfenc_parse_input_as_attribute_policy(input, policy);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not parse function input as policy", __func__);
result = FENC_ERROR_INVALID_INPUT;
goto cleanup;
}
/* Initialize temporary elements. */
element_init_Zr(rZ, scheme_context->global_params->pairing);
element_init_Zr(sZ, scheme_context->global_params->pairing);
element_init_GT(eggalphasT, scheme_context->global_params->pairing);
element_init_G1(tempONE, scheme_context->global_params->pairing);
element_init_G1(temp2ONE, scheme_context->global_params->pairing);
elements_initialized = TRUE;
/* Select sZ and compute eggalphaZT = eggalphaT^{sZ}. */
element_random(sZ);
element_pow_zn(eggalphasT, scheme_context->public_params.eggalphaT, sZ);
/* Export the policy to a string and draw it back in again. This clears up some issues in the way */
//strcpy(temp_policy_str, "");
//err_code = fenc_attribute_policy_to_string(policy->root, temp_policy_str, MAX_POLICY_STR);
//if (err_code != FENC_ERROR_NONE) {
// LOG_ERROR("%s: could not serialize policy", __func__);
// result = err_code;
// goto cleanup;
//}
// fenc_policy_from_string(policy, temp_policy_str);
// printf("Original policy string: '%s'\n", policy->string);
//strcpy(temp_policy_str, "");
//err_code = fenc_attribute_policy_to_string(policy->root, temp_policy_str, MAX_POLICY_STR);
//printf("Revised policy string: %s\n", temp_policy_str);
/* Use the Linear Secret Sharing Scheme (LSSS) to compute an enumerated list of all
* attributes and corresponding secret shares of sZ. The shares will be placed into
* a fenc_attribute_list structure that we'll embed within the fenc_key_WatersCP struct. */
memset(&attribute_list, 0, sizeof(fenc_attribute_list));
err_code = fenc_LSSS_calculate_shares_from_policy(&(sZ), policy, &attribute_list,
scheme_context->global_params->pairing);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not calculate shares", __func__);
result = FENC_ERROR_INVALID_INPUT;
goto cleanup;
}
/* Initialize the WatersCP-specific ciphertext data structure and allocate some temporary variables. */
err_code = fenc_ciphertext_WatersCP_initialize(&ciphertext_WatersCP, &attribute_list, policy, FENC_CIPHERTEXT_TYPE_KEM_CPA,
scheme_context);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not initialize ciphertext structure", __func__);
result = FENC_ERROR_UNKNOWN;
goto cleanup;
}
//strcpy(ciphertext_WatersCP.policy_str, temp_policy_str);
strcpy(ciphertext_WatersCP.policy_str, fenc_get_policy_string(policy));
/* If we're in KEM mode, the returned key is the hash of eggalphasT. */
if (kem_mode == TRUE) {
err_code = fenc_derive_key_from_element(eggalphasT, kem_key_len, kem_key_buf);
if (err_code != FENC_ERROR_NONE) {
result = err_code;
goto cleanup;
}
ciphertext_WatersCP.type = FENC_CIPHERTEXT_TYPE_KEM_CPA;
ciphertext_WatersCP.kem_key_len = kem_key_len;
} else {
/* Not in KEM mode. */
LOG_ERROR("%s: KEM mode is currently the only supported form of encryption.", __func__);
result = FENC_ERROR_UNKNOWN;
goto cleanup;
}
/* Compute CprimeONE = gONE^{sZ}. */
element_pow_zn(ciphertext_WatersCP.CprimeONE, scheme_context->public_params.gONE, sZ);
/* For every share/attribute, create one component of the secret key. */
for (i = 0; i < ciphertext_WatersCP.attribute_list.num_attributes; i++) {
/* Hash the attribute string to Zr, if it hasn't already been. */
hash_attribute_string_to_Zr(&(ciphertext_WatersCP.attribute_list.attribute[i]), scheme_context->global_params->pairing);
/* Pick a random value r_i (rZ). */
element_random(rZ);
/* Set DTWO[i] = gTWO^{rZ}. */ // very expensive
element_pow_zn(ciphertext_WatersCP.DTWO[i], scheme_context->public_params.gTWO, rZ);
/* Hash the attribute (already hashed to Zr) into an element of G1 (tempONE). */
err_code = hash2_attribute_element_to_G1(&(ciphertext_WatersCP.attribute_list.attribute[i].attribute_hash), &tempONE); /* result in tempONE */
element_pow_zn(temp2ONE, tempONE, rZ); /* temp2ONE = H(attribute)^{rZ} */
element_invert(tempONE, temp2ONE); /* tempONE = H(attribute)^{-rZ} */
/* Set CONE[i] = gaONE^{share_i} * H(attribute)^{-rZ}. */
DEBUG_ELEMENT_PRINTF("share %d is %B\n", i, attribute_list.attribute[i].share);
element_pow_zn(temp2ONE, scheme_context->public_params.gaONE, attribute_list.attribute[i].share); /* temp2ONE = gaONE^{share_i} */
element_mul(ciphertext_WatersCP.CONE[i], tempONE, temp2ONE); /* CONE = tempONE * temp2ONE. */
}
/* DEBUG: Print out the ciphertext. */
// libfenc_fprint_ciphertext_WatersCP(&ciphertext_WatersCP, stdout);
/* Serialize the WatersCP ciphertext structure into a fenc_ciphertext container
* (which is essentially just a binary buffer). First we get the length, then we
* allocate the ciphertext buffer, then we serialize. */
libfenc_serialize_ciphertext_WatersCP(&ciphertext_WatersCP, NULL, 0, &serialized_len); /* This gets the serialized length. */
libfenc_ciphertext_initialize(ciphertext, serialized_len, FENC_SCHEME_WATERSCP);
if (err_code != FENC_ERROR_NONE) { result = err_code; goto cleanup; }
err_code = libfenc_serialize_ciphertext_WatersCP(&ciphertext_WatersCP, ciphertext->data, ciphertext->max_len, &ciphertext->data_len); /* Serialization. */
if (err_code != FENC_ERROR_NONE) { result = err_code; goto cleanup; }
/* Success! */
result = FENC_ERROR_NONE;
cleanup:
/* If there was an error, clean up after ourselves. */
/* Wipe out temporary variables. */
if (elements_initialized == TRUE) {
element_clear(rZ);
element_clear(sZ);
element_clear(eggalphasT);
element_clear(tempONE);
element_clear(temp2ONE);
}
return result;
}
/*!
* Export the public parameters (MPK) to a binary buffer. Calling this function with buffer
* set to NULL will return the length of the exported material.
*
* @param context The fenc_context data structure
* @param buffer A pre-allocated buffer for the resulting export.
* @param max_len The maximum allocated size of the buffer (in bytes).
* @param result_len The size of the resulting export (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR libfenc_export_public_params_WatersCP(fenc_context *context, uint8 *buffer, size_t max_len, size_t *result_len)
{
FENC_ERROR err_code;
fenc_scheme_context_WatersCP* scheme_context;
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
/* Export the elements to the buffer. Note that if buffer is NULL this routine will
* just compute the necessary buffer length. */
err_code = export_components_to_buffer(buffer, max_len, result_len, "%C%C%C%C%E",
&(scheme_context->public_params.gONE),
&(scheme_context->public_params.gTWO),
&(scheme_context->public_params.gaONE),
&(scheme_context->public_params.gaTWO),
&(scheme_context->public_params.eggalphaT));
return err_code;
}
/*!
* Export a context's secret parameters (MSK) to a binary buffer. Calling this function with buffer
* set to NULL will return the length of the exported material.
*
* @param context The fenc_context data structure
* @param buffer A pre-allocated buffer for the resulting export.
* @param max_len The maximum allocated size of the buffer (in bytes).
* @param result_len The size of the resulting export (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_export_secret_params_WatersCP(fenc_context *context, uint8 *buffer, size_t max_len, size_t *result_len)
{
fenc_scheme_context_WatersCP* scheme_context;
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
/* Export the elements to the buffer. Note that if buffer is NULL this routine will
* just compute the necessary buffer length. */
return export_components_to_buffer(buffer, max_len, result_len, "%E",
&(scheme_context->secret_params.alphaZ));
}
/*!
* Import the public parameters (MPK) from a binary buffer.
*
* @param context The fenc_context data structure
* @param buffer The buffer.
* @param max_len The size of the buffer (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_import_public_params_WatersCP(fenc_context *context, uint8 *buffer, size_t buf_len, fenc_global_params *global_params)
{
//FENC_ERROR err_code;
fenc_scheme_context_WatersCP* scheme_context;
//size_t bytes_read = 0;
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
return FENC_ERROR_INVALID_CONTEXT;
}
/* Sanity check: Make sure that we have initialized group/global parameters. */
if (scheme_context->global_params == NULL) {
LOG_ERROR("%s: global/group parameters are not set", __func__);
return FENC_ERROR_INVALID_GLOBAL_PARAMS;
}
/* Initialize the public parameters, allocating group elements. */
public_params_initialize_WatersCP(&(scheme_context->public_params), scheme_context->global_params->pairing);
/* Import the elements from the buffer. */
return import_components_from_buffer(buffer, buf_len, NULL, "%C%C%C%C%E",
&(scheme_context->public_params.gONE),
&(scheme_context->public_params.gTWO),
&(scheme_context->public_params.gaONE),
&(scheme_context->public_params.gaTWO),
&(scheme_context->public_params.eggalphaT));
}
/*!
* Import the secret parameters (MPK) from a binary buffer.
*
* @param context The fenc_context data structure
* @param buffer The buffer.
* @param max_len The size of the buffer (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_import_secret_params_WatersCP(fenc_context *context, uint8 *buffer, size_t buf_len)
{
// FENC_ERROR err_code;
fenc_scheme_context_WatersCP* scheme_context;
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
return FENC_ERROR_INVALID_CONTEXT;
}
/* Initialize the secret parameters, allocating group elements. */
secret_params_initialize_WatersCP(&(scheme_context->secret_params), scheme_context->global_params->pairing);
return import_components_from_buffer(buffer, buf_len, NULL, "%E",
&(scheme_context->secret_params.alphaZ));
}
/*!
* Import the global parameters (MPK) from a binary buffer.
*
* @param context The fenc_context data structure
* @param buffer The buffer.
* @param max_len The size of the buffer (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_import_global_params_WatersCP(fenc_context *context, uint8 *buffer, size_t buf_len)
{
FENC_ERROR err_code;
fenc_scheme_context_WatersCP* scheme_context;
fenc_group_params group_params;
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
return FENC_ERROR_INVALID_CONTEXT;
}
/* Read the global parameters out of the buffer, if they're in there. */
err_code = libfenc_load_group_params_from_buf(&(group_params), buffer, buf_len);
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not read group params", __func__);
return FENC_ERROR_INVALID_GLOBAL_PARAMS;
}
/* Initialize the scheme's global parameters. */
scheme_context->global_params = initialize_global_params_WatersCP(&group_params, scheme_context->global_params);
return err_code;
}
/*!
* Export the global parameters to a binary buffer. Calling this function with buffer
* set to NULL will return the length of the exported material.
*
* @param context The fenc_context data structure
* @param buffer A pre-allocated buffer for the resulting export.
* @param max_len The maximum allocated size of the buffer (in bytes).
* @param result_len The size of the resulting export (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_export_global_params_WatersCP(fenc_context *context, uint8 *buffer, size_t max_len, size_t *result_len)
{
FENC_ERROR err_code;
fenc_scheme_context_WatersCP* scheme_context;
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
/* Export the group parameters to the buffer. If th buffer is NULL this only compute the length. */
err_code = libfenc_export_group_params(&(scheme_context->global_params->group_params), buffer, max_len, result_len);
return err_code;
}
/*!
* Serialize an ABE key structure.
*
* @param context The fenc_context data structure
* @param key The fenc_key data structure.
* @param buffer A pre-allocated buffer for the resulting export.
* @param buf_len The maximum allocated size of the buffer (in bytes).
* @param result_len The size of the resulting export (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_export_secret_key_WatersCP(fenc_context *context, fenc_key *key, uint8 *buffer, size_t buf_len, size_t *result_len)
{
FENC_ERROR err_code = FENC_ERROR_NONE;
/* retrieve the key for the WatersCP context */
fenc_key_WatersCP *key_WatersCP = (fenc_key_WatersCP *) key->scheme_key;
if(key_WatersCP == NULL) {
err_code = FENC_ERROR_INVALID_INPUT;
LOG_ERROR("%s: fenc_key not existent.", __func__);
goto cleanup;
}
/* serialize key structure to buffer */
err_code = libfenc_serialize_key_WatersCP(key_WatersCP, buffer, buf_len, result_len);
if (err_code != FENC_ERROR_NONE) {
err_code = FENC_ERROR_INVALID_INPUT;
LOG_ERROR("%s: cannot serialize key to buffer. has key been constructed?", __func__);
goto cleanup;
}
cleanup:
return err_code;
}
/*!
* Deserialize an ABE key structure.
*
* @param context The fenc_context data structure
* @param key The fenc_key data structure (pre-allocated), but not initialized.
* @param buffer The buffer which contains the binary contents of key?
* @param buf_len The size of the buffer (in bytes).
* @return FENC_ERROR_NONE or an error code.
*/
FENC_ERROR
libfenc_import_secret_key_WatersCP(fenc_context *context, fenc_key *key, uint8 *buffer, size_t buf_len)
{
FENC_ERROR err_code = FENC_ERROR_NONE;
fenc_key_WatersCP *key_WatersCP = NULL;
fenc_attribute_list *attribute_list;
uint32 num_components;
size_t import_len;
fenc_scheme_context_WatersCP *scheme_context;
/* Get the scheme-specific context. */
scheme_context = (fenc_scheme_context_WatersCP*)context->scheme_context;
if (scheme_context == NULL) {
return FENC_ERROR_INVALID_CONTEXT;
}
/* Allocate an attribute list data structure. */
attribute_list = (fenc_attribute_list*) SAFE_MALLOC(sizeof(fenc_attribute_list));
if (attribute_list == NULL) {
LOG_ERROR("%s: could not allocate attribute list", __func__);
return FENC_ERROR_OUT_OF_MEMORY;
}
/* import attributes only -- should be first in buffer */
err_code = import_components_from_buffer(buffer, buf_len, &import_len, "%A%d",
attribute_list,
&(num_components));
/* sanity check */
if(num_components != attribute_list->num_attributes) {
LOG_ERROR("%s: mis-match in attributes found in key", __func__);
err_code = FENC_ERROR_INVALID_INPUT;
goto cleanup;
}
// printf("import_len => '%zu'\n", import_len);
/* Initialize the WatersCP-specific key data structure and allocate some temporary variables. */
key_WatersCP = fenc_key_WatersCP_initialize(attribute_list, FALSE, scheme_context->global_params);
if (key_WatersCP == NULL) {
LOG_ERROR("%s: could not initialize key structure", __func__);
return FENC_ERROR_INVALID_INPUT;
}
/* deserialize remaining buffer into key_watersCP structure -- KTWO, LONE, and KXONE (num_component times) */
err_code = libfenc_deserialize_key_WatersCP(key_WatersCP, (uint8 *) (buffer + import_len), (buf_len - import_len));
if (err_code != FENC_ERROR_NONE) {
LOG_ERROR("%s: could not deserialize into key structure", __func__);
err_code = FENC_ERROR_INVALID_INPUT;
goto cleanup;
}
/* Stash the key_WatersCP structure inside of the fenc_key. */
memset(key, 0, sizeof(fenc_key));
key->scheme_type = FENC_SCHEME_WATERSCP;
key->valid = TRUE;
key->scheme_key = (void*)key_WatersCP;
return err_code;
cleanup:
/* clean up here */
free(attribute_list);
return FENC_ERROR_NONE;