-
Notifications
You must be signed in to change notification settings - Fork 1
/
tbfe-bbg.c
2678 lines (2302 loc) · 86.4 KB
/
tbfe-bbg.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
/*
* This file contains the implementation of the TBFE scheme.
* BBG HIBE is used for key delegation in a binary tree structure.
* Forward secrecy is achieved by puncturing the secret key for specific ciphertexts (via bloom
* filter) or after some time interval (via tree).
*
* The implementation utilizes EVERY node (in the tree) as distinct time interval (not only the
* leaves). Index-To-Identity mapping is done via indexing the nodes in a 'pre-order traversal'
* manner.
*
* To achieche CCA security the CHK compiler is added as additional layer to the HIBE.
*
*/
/*
* The code contains different definitions of the tree height (or depth), which are explained here:
* - total_depth : Height of the tree including bloom filter keys and CHK signature
* - num_delegatable_levels : Number of levels for further key delegation of subtree with ID as
* root --> equal to 'total_depth - ID.depth'. This variable refers to the number of b_i (basis)
* elements as part of some BBG secret key (cmp. BBG HIBE key generation).
*/
#include <config.h>
#include "include/tbfe-bbg.h"
#include "bloom.h"
#include "core.h"
#include "utils.h"
#include "vector.h"
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <omp.h>
#include <sodium/crypto_sign.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
// ##################################################
// ##### ADDITIONAL STRUCTS AND DEFINES FOR BBG #####
// ##################################################
/* BBG ciphertext consists of three group elemnts */
#define BBG_CIPHERTEXT_SIZE (G1_SIZE_COMPRESSED + G2_SIZE_COMPRESSED + GT_SIZE_COMPRESSED)
/* BBG public key consists of one group element*/
#define BBG_PUBLIC_KEY_SIZE GT_SIZE_COMPRESSED
/* BBG bloom filter key consists of three group elements */
#define BBG_BF_KEY_SIZE (G1_SIZE_COMPRESSED + G2_SIZE_COMPRESSED + G1_SIZE_COMPRESSED)
/* Set arity N of the interval tree */
#define ARITY TBFE_ARITY
/* Add offset to bloom filter index --> BF identities start at (ARITY + 1) */
#define BF_POS_TO_BF_ID(A) (A + ARITY + 1)
/* States if some BBG secret key is a bloom filter key or not */
typedef enum {
BLOOM_FILTER_KEY = 0, /**< Key is of type bbg bloom filter key */
SECRET_KEY = 1, /**< Key is of type bbg secret key */
} key_type_t;
/**
* EdDSA secret key
*/
typedef struct {
unsigned char key[crypto_sign_SECRETKEYBYTES];
} eddsa_sk_t;
/**
* Represents a BBG HIBE identity in the tree.
* An identity is defined by the its depth in the tree and the corresponding 0/1 (left/right child)
* path from the root.
*/
typedef struct {
unsigned depth; /**< Depth of the identity or distance from root. */
unsigned* id; /**< Array which uniquely defines the identity as a path from root. */
} bbg_identity_t;
/**
* Generated symmetric Key which is encapsulated and shared by this protocol.
*/
typedef struct {
gt_t k; /**< The key is represented by a G_t group element. */
} bbg_key_t;
/**
* Represents a BBG HIBE master key.
*/
typedef struct {
g1_t mk; /**< The master key is represented by a G_1 group element. */
} bbg_master_key_t;
/**
* Secret key of BBG HIBE (cmp. with BBG HIBE key generation)
* sk = [a0, a1, b_{k+1} ... b_l] where:
* - a0 = g_2^alpha * (h_1^I_1 * ... * h_k^I_k * g_3)^r
* - a1 = g_hat^r
* - b = [h_k^r, ..., h_l^r]
*/
typedef struct {
unsigned num_delegatable_levels; /**< Number of elements in array b */
bbg_identity_t identity; /**< Identity (or node of the tree) corresponding to the secret key */
g1_t a0; /**< a0 part of the secret key - G_1 element */
g2_t a1; /**< a1 part of the secret key - G_2 element */
g1_t* b; /**< b_k to b_l of the secret key - G_1 elements*/
g1_t associated_id; /**< This field stores the product (g_3 * h_1^I_1 * h_2^I_2 * ... * h_k^I_k)
for further key delegation. */
} bbg_secret_key_t;
/**
* Same as bbg_secret_key_t but stripped down to hold only essential elements.
*/
typedef struct {
g1_t a0; /**< a0 part of the secret key - G_1 element */
g2_t a1; /**< a1 part of the secret key - G_2 element */
g1_t b; /**< b part of the secret key (only one element) - G_1 element*/
} bbg_bf_key_t;
/**
* Ciphertext of BBG HIBE.
* C = [a,b,c] where:
* - a = e(g_2, g_1)^s * M
* - b = g_hat^s
* - c =(h_1^I_1 * ... * h_k^I_k * g_3)^s
*/
typedef struct {
gt_t a; /**< a part of the ciphertext - G_t element */
g2_t b; /**< b part of the ciphertext - G_2 element */
g1_t c; /**< c part of the ciphertext - G_1 element */
} bbg_ciphertext_t;
/* Prefixes for hash function to achieve domain separation */
static const uint8_t SIGNATURE_PREFIX = 3;
static const uint8_t VERIFICATION_PREFIX = 4;
// ##################################################
// ############## FUNCTION PROTOTYPES ###############
// ##################################################
// ## EdDSA SIGNATURE
static void eddsa_clear_sk(eddsa_sk_t* eddsa_sk);
static void eddsa_clear_pk(eddsa_pk_t* eddsa_pk);
static void eddsa_clear_sig(eddsa_sig_t* eddsa);
static int eddsa_keygen(eddsa_sk_t* eddsa_sk, eddsa_pk_t* eddsa_pk);
static int eddsa_sign(eddsa_sig_t* eddsa, vector_t* ciphertexts, eddsa_sk_t* eddsa_sk,
tbfe_bbg_public_key_t* pk);
static int eddsa_verify(vector_t* ciphertexts, eddsa_sig_t* eddsa, eddsa_pk_t* eddsa_pk,
tbfe_bbg_public_key_t* pk);
// ## SIZE
static unsigned int bbg_get_identity_size(const bbg_identity_t* identity);
static unsigned bbg_get_secret_key_size(const bbg_secret_key_t* secret_key);
static unsigned bbg_get_public_params_size(const bbg_public_params_t* public_params);
// ## INIT
static int bbg_init_identity(bbg_identity_t* identity, unsigned int id_depth);
static int bbg_init_public_key(bbg_public_key_t* pk);
static int bbg_init_secret_key(bbg_secret_key_t* sk, unsigned int delegatable_levels,
unsigned int id_depth);
static int bbg_init_bf_key(bbg_bf_key_t* bf_key);
static int bbg_init_ciphertext(bbg_ciphertext_t* ciphertext);
static int bbg_init_public_params(bbg_public_params_t* params, unsigned int depth);
static int bbg_init_public_params_from_serialized(bbg_public_params_t* params,
const uint8_t* serialized);
static int bbg_init_master_key(bbg_master_key_t* mk);
static int bbg_init_key(bbg_key_t* key);
static int bbg_init_identity_from(bbg_identity_t* dst, unsigned int depth,
const bbg_identity_t* src);
static int bbg_init_bf_key_from_secret_key(bbg_bf_key_t* bf_key, bbg_secret_key_t* sk);
// ## SERIALIZE AND DESERIALIZE
static void bbg_serialize_identity(uint8_t* dst, const bbg_identity_t* identity);
static void bbg_serialize_public_key(uint8_t* serialized, bbg_public_key_t* public_key);
static void bbg_serialize_secret_key(uint8_t* serialized, bbg_secret_key_t* secret_key);
static void bbg_serialize_bf_key(uint8_t* serialized, bbg_bf_key_t* bf_key);
static void bbg_serialize_ciphertext(uint8_t* serialized, bbg_ciphertext_t* ciphertext);
static void bbg_serialize_public_params(uint8_t* serialized, bbg_public_params_t* public_params);
static void bbg_deserialize_identity(bbg_identity_t* identity, const uint8_t* src);
static void bbg_deserialize_public_key(bbg_public_key_t* public_key, const uint8_t* serialized);
static void bbg_deserialize_secret_key(bbg_secret_key_t* secret_key, const uint8_t* serialized);
static void bbg_deserialize_bf_key(bbg_bf_key_t* bf_key, const uint8_t* serialized);
static void bbg_deserialize_ciphertext(bbg_ciphertext_t* ciphertext, const uint8_t* serialized);
// ## CLEAR AND FREE
static void bbg_clear_identity(bbg_identity_t* identity);
static void bbg_clear_public_key(bbg_public_key_t* pk);
static void bbg_clear_secret_key(bbg_secret_key_t* sk);
static void bbg_clear_bf_key(bbg_bf_key_t* bf_key);
static void bbg_clear_ciphertext(bbg_ciphertext_t* ciphertext);
static void bbg_clear_public_params(bbg_public_params_t* params);
static void bbg_clear_master_key(bbg_master_key_t* mk);
static void bbg_clear_key(bbg_key_t* key);
// ## HASHING
static void bbg_hash_eddsa_pk(bn_t hash, eddsa_pk_t* eddsa_pk);
static void hash_update_u32(Keccak_HashInstance* ctx, uint32_t v);
static void hash_update_tbfe_public_key(Keccak_HashInstance* ctx,
tbfe_bbg_public_key_t* public_key);
static void hash_update_bbg_ciphertext(Keccak_HashInstance* ctx, bbg_ciphertext_t* ciphertext);
static void hash_update_bbg_ciphertexts(Keccak_HashInstance* ctx, vector_t* ciphertexts);
// ## BBG HIBE
static int bbg_convert_identity_to_zp_vector(bn_t* identity_zp_vector,
const bbg_identity_t* identity);
static int bbg_setup(bbg_master_key_t* master_key, bbg_public_key_t* public_key,
bbg_public_params_t* public_params);
static int bbg_decapsulate(bbg_key_t* key, bbg_ciphertext_t* ciphertext, bbg_bf_key_t* bf_key,
eddsa_pk_t* eddsa_pk, bbg_public_params_t* public_params,
const bbg_identity_t* identity);
static int bbg_encapsulate(bbg_ciphertext_t* ciphertext, gt_t message, bbg_public_key_t* public_key,
eddsa_pk_t* eddsa_pk, bbg_public_params_t* public_params,
const bbg_identity_t* identity);
static int bbg_copy_identity(bbg_identity_t* dest, const bbg_identity_t* src);
static bool bbg_identities_are_equal(const bbg_identity_t* l, const bbg_identity_t* r);
static int bbg_sample_key(bbg_key_t* key);
static int bbg_key_generation_from_master_key(bbg_secret_key_t* secret_key,
bbg_master_key_t* master_key,
const bbg_identity_t* identity,
bbg_public_params_t* public_params);
static int bbg_key_generation_from_parent(bbg_secret_key_t* secret_key,
bbg_secret_key_t* parent_secret_key,
const bbg_identity_t* identity,
bbg_public_params_t* public_params);
static int bbg_convert_key_to_bit_string(uint8_t* bit_string, bbg_key_t* key);
// ## TBFE
// ### Commented functions define the public TBFE interface, declared in './include/tbfe-bbg.h'
/* int tbfe_bbg_init_public_key(tbfe_bbg_public_key_t* public_key, unsigned int total_depth); */
/* int tbfe_bbg_public_key_deserialize(tbfe_bbg_public_key_t* public_key, const uint8_t* src); */
/* void tbfe_bbg_clear_public_key(tbfe_bbg_public_key_t* public_key); */
/* int tbfe_bbg_init_secret_key(tbfe_bbg_secret_key_t* secret_key, unsigned int bloom_filter_size,
double false_positive_prob); */
/* int tbfe_bbg_secret_key_deserialize(tbfe_bbg_secret_key_t* secret_key, const uint8_t* src); */
static void tbfe_bbg_vector_secret_key_free(vector_t* vector_secret_key);
static void tbfe_bbg_vector_bf_key_free(vector_t* vector_bf_key);
/* void tbfe_bbg_clear_secret_key(tbfe_bbg_secret_key_t* secret_key); */
/* int tbfe_bbg_init_ciphertext(tbfe_bbg_ciphertext_t* ciphertext); */
/* int tbfe_bbg_ciphertext_deserialize(tbfe_bbg_ciphertext_t* ciphertext, const uint8_t* src); */
/* void tbfe_bbg_clear_ciphertext(tbfe_bbg_ciphertext_t* ciphertext); */
static unsigned long compute_tree_size(const unsigned h);
static int tbfe_bbg_index_to_identity(bbg_identity_t* identity, const unsigned long index,
const unsigned height);
/* void tbfe_bbg_public_key_serialize(uint8_t* serialized, tbfe_bbg_public_key_t* public_key); */
/* void tbfe_bbg_secret_key_serialize(uint8_t* serialized, tbfe_bbg_secret_key_t* secret_key); */
/* void tbfe_bbg_ciphertext_serialize(uint8_t* serialized, tbfe_bbg_ciphertext_t* ciphertext); */
/* unsigned tbfe_bbg_public_key_size(const tbfe_bbg_public_key_t* public_key); */
/* unsigned tbfe_bbg_secret_key_size(const tbfe_bbg_secret_key_t* secret_key); */
/* unsigned tbfe_bbg_ciphertext_size(const tbfe_bbg_ciphertext_t* ciphertext); */
static int generate_one_identity_with_last_component(bbg_identity_t* identity, unsigned int depth,
unsigned int last_component);
static int derive_key_and_add(vector_t* dst, bbg_public_params_t* params, bbg_master_key_t* msk,
const bbg_identity_t* identity, key_type_t key_type);
/* int tbfe_bbg_keygen(tbfe_bbg_public_key_t* public_key, tbfe_bbg_secret_key_t* secret_key); */
/* int tbfe_bbg_encaps(uint8_t* key, tbfe_bbg_ciphertext_t* ciphertext,
tbfe_bbg_public_key_t* public_key, unsigned int time_interval); */
/* int tbfe_bbg_decaps(uint8_t* key, tbfe_bbg_ciphertext_t* ciphertext,
tbfe_bbg_secret_key_t* secret_key, tbfe_bbg_public_key_t* public_key); */
/* int tbfe_bbg_puncture_ciphertext(tbfe_bbg_secret_key_t* secret_key,
tbfe_bbg_ciphertext_t* ciphertext); */
static int puncture_derive_key_and_add(vector_t* dst, bbg_public_params_t* params,
bbg_secret_key_t* sk, const bbg_identity_t* identity,
key_type_t key_type);
/* int tbfe_bbg_puncture_interval(tbfe_bbg_secret_key_t* secret_key, tbfe_bbg_public_key_t*
public_key, unsigned int time_interval); */
// ## COMPARE
#if defined(BFE_STATIC)
static bool bbg_public_keys_are_equal(bbg_public_key_t* l, bbg_public_key_t* r);
static bool bbg_public_params_are_equal(bbg_public_params_t* l, bbg_public_params_t* r);
static bool bbg_secret_keys_are_equal(bbg_secret_key_t* l, bbg_secret_key_t* r);
static bool bbg_bf_keys_are_equal(bbg_bf_key_t* l, bbg_bf_key_t* r);
static bool bbg_ciphertexts_are_equal(bbg_ciphertext_t* l, bbg_ciphertext_t* r);
bool tbfe_bbg_public_keys_are_equal(tbfe_bbg_public_key_t* l, tbfe_bbg_public_key_t* r);
bool tbfe_bbg_secret_keys_are_equal(tbfe_bbg_secret_key_t* l, tbfe_bbg_secret_key_t* r);
bool tbfe_bbg_ciphertexts_are_equal(tbfe_bbg_ciphertext_t* l, tbfe_bbg_ciphertext_t* r);
bool tbfe_bbg_eddsa_sig_are_equal(tbfe_bbg_ciphertext_t* l, tbfe_bbg_ciphertext_t* r);
#endif
// ##################################################
// ############## FUNCTION DEFINITIONS ##############
// ##################################################
/* >> EdDSA Signatures << */
/**
* The following functions provide the interface to create and verify
* signatures with EdDSA.
*/
///@{
static_assert(crypto_sign_BYTES == Ed25519_SIG_BYTES);
static_assert(crypto_sign_PUBLICKEYBYTES == Ed25519_KEY_BYTES);
static void eddsa_clear_sk(eddsa_sk_t* eddsa_sk) {
if (eddsa_sk) {
explicit_bzero(eddsa_sk->key, crypto_sign_SECRETKEYBYTES);
}
}
static void eddsa_clear_pk(eddsa_pk_t* eddsa_pk) {
if (eddsa_pk) {
memset(eddsa_pk->key, 0, crypto_sign_PUBLICKEYBYTES);
}
}
static void eddsa_clear_sig(eddsa_sig_t* eddsa) {
if (eddsa) {
memset(eddsa->sig, 0, crypto_sign_BYTES);
}
}
/**
* Generates a new EdDSA key pair.
* The keys are returned in RAW binary format.
*
* @param eddsa_sk[out] - the generated secret signature key
* @param eddsa_pk[out] - the generated public verification key
*
* @return - BFE_SUCCESS when no errors occured, BFE_ERROR otherwise
*/
static int eddsa_keygen(eddsa_sk_t* eddsa_sk, eddsa_pk_t* eddsa_pk) {
if (!eddsa_pk || !eddsa_sk) {
return BFE_ERROR_INVALID_PARAM;
}
crypto_sign_keypair(eddsa_pk->key, eddsa_sk->key);
return BFE_SUCCESS;
}
/**
* Sign the given ciphertext vector together with the corresponding tbfe public key.
* The public key is serialized and hashed together with the ciphertext.
*
* @param[out] eddsa - the generated signature
* @param[in] ciphertexts - the given ciphertexts that shall be signed
* @param[in] eddsa_sk - the secret verification key
* @param[in] pk - the corresponding tbfe public key used to generate the ciphertexts
*
* @return - BFE_SUCCESS if no error occured, BFE_ERROR otherwise
*/
static int eddsa_sign(eddsa_sig_t* eddsa, vector_t* ciphertexts, eddsa_sk_t* eddsa_sk,
tbfe_bbg_public_key_t* pk) {
if (!eddsa || !ciphertexts || !eddsa_sk || !pk) {
return BFE_ERROR_INVALID_PARAM;
}
int result_status = BFE_SUCCESS;
// Hash (ciphertexts || pk)
Keccak_HashInstance ctx;
Keccak_HashInitialize_SHAKE256(&ctx);
Keccak_HashUpdate(&ctx, &SIGNATURE_PREFIX, sizeof(SIGNATURE_PREFIX) * 8);
hash_update_bbg_ciphertexts(&ctx, ciphertexts);
hash_update_tbfe_public_key(&ctx, pk); // Add public key to hash
Keccak_HashFinal(&ctx, NULL);
uint8_t hash_buf[64];
Keccak_HashSqueeze(&ctx, hash_buf, sizeof(hash_buf) * 8);
// Sign the hash
crypto_sign_detached(eddsa->sig, NULL, hash_buf, sizeof(hash_buf), eddsa_sk->key);
return result_status;
}
/**
* Verifies the given EdDSA signature.
*
* @param[in] ciphertexts - the ciphertext vector which was signed
* @param[in] eddsa - the corresponding signature
* @param[in] eddsa_pk - the public verification key
* @param[in] pk - the tbfe public key used to generate the ciphertexts
*
* @return - BFE_SUCCESS if the signature could be verified, BFE_ERROR otherwise
*/
static int eddsa_verify(vector_t* ciphertexts, eddsa_sig_t* eddsa, eddsa_pk_t* eddsa_pk,
tbfe_bbg_public_key_t* pk) {
if (!ciphertexts || !eddsa || !eddsa_pk || !pk) {
return BFE_ERROR_INVALID_PARAM;
}
int result_status = BFE_SUCCESS;
// Hash (ciphertexts || pk)
Keccak_HashInstance ctx;
Keccak_HashInitialize_SHAKE256(&ctx);
Keccak_HashUpdate(&ctx, &SIGNATURE_PREFIX, sizeof(SIGNATURE_PREFIX) * 8);
hash_update_bbg_ciphertexts(&ctx, ciphertexts);
hash_update_tbfe_public_key(&ctx, pk); // Add public key to hash
Keccak_HashFinal(&ctx, NULL);
uint8_t hash_buf[64];
Keccak_HashSqueeze(&ctx, hash_buf, sizeof(hash_buf) * 8);
// Verify signature
if (crypto_sign_verify_detached(eddsa->sig, hash_buf, sizeof(hash_buf), eddsa_pk->key) != 0) {
result_status = BFE_ERROR;
}
return result_status;
}
///@}
/* >> SIZE << */
/**
* Return the size in bytes of the given element.
*/
///@{
static unsigned int bbg_get_identity_size(const bbg_identity_t* identity) {
return (identity->depth + 1) * sizeof(uint32_t);
}
static unsigned bbg_get_secret_key_size(const bbg_secret_key_t* secret_key) {
return G1_SIZE_COMPRESSED + G2_SIZE_COMPRESSED + G1_SIZE_COMPRESSED + sizeof(uint32_t) +
(secret_key->num_delegatable_levels * G1_SIZE_COMPRESSED) +
bbg_get_identity_size(&secret_key->identity);
}
static unsigned bbg_get_public_params_size(const bbg_public_params_t* public_params) {
return G2_SIZE_COMPRESSED + 2 * G1_SIZE_COMPRESSED + sizeof(uint32_t) +
(public_params->total_depth) * G1_SIZE_COMPRESSED;
}
///@}
/* >> INIT << */
/**
* The following functions initialize (already allocted - either heap or stack) structures with
* initial values. Some structures use RELIC specific datatypes (e.g. bn_t, gt_t, ...), that are
* created and defined in this functions.
*/
///@{
/**
* Initializes the given identity with the provided depth
*
* @param[out] identity - the identity element which shall be initialized
* @param[in] id_depth - the depth of the identity
*
* @return BFE_SUCCESS if no error occurs, an error code otherwise.
*/
static int bbg_init_identity(bbg_identity_t* identity, unsigned int id_depth) {
if (!identity) {
return BFE_ERROR_INVALID_PARAM;
}
identity->id = calloc(id_depth, sizeof(*identity->id));
if (!identity->id) {
return BFE_ERROR;
}
identity->depth = id_depth;
return BFE_SUCCESS;
}
static int bbg_init_public_key(bbg_public_key_t* pk) {
if (!pk) {
return BFE_ERROR_INVALID_PARAM;
}
int ret = BFE_SUCCESS;
gt_null(pk->pk);
RLC_TRY {
gt_new(pk->pk);
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
/**
* Initializes the given BBG secret key.
*
* @param[out] sk - the initialized secret key
* @param[in] delegatable_levels - the number of delegatable levels of the corresponding identity
* @param[in] id_depth - the depth of the corresponding identity
*
* @return BFE_SUCCESS if no error occurs, an error code otherwise.
*/
static int bbg_init_secret_key(bbg_secret_key_t* sk, unsigned int delegatable_levels,
unsigned int id_depth) {
if (!sk) {
return BFE_ERROR_INVALID_PARAM;
}
int ret = bbg_init_identity(&sk->identity, id_depth);
if (ret != BFE_SUCCESS) {
return ret;
}
g1_null(sk->a0);
g2_null(sk->a1);
g1_null(sk->associated_id);
sk->b = calloc(delegatable_levels, sizeof(*sk->b));
if (!sk->b) {
return ret;
}
sk->num_delegatable_levels = delegatable_levels;
for (size_t idx = 0; idx < delegatable_levels; ++idx) {
g1_null(sk->b[idx]);
}
RLC_TRY {
g1_new(sk->a0);
g2_new(sk->a1);
g1_new(sk->associated_id);
for (size_t idx = 0; idx < delegatable_levels; ++idx) {
g1_new(sk->b[idx]);
}
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
static int bbg_init_bf_key(bbg_bf_key_t* bf_key) {
if (!bf_key) {
return BFE_ERROR_INVALID_PARAM;
}
int ret = BFE_SUCCESS;
g1_null(bf_key->a0);
g2_null(bf_key->a1);
g1_null(bf_key->b);
RLC_TRY {
g1_new(bf_key->a0);
g2_new(bf_key->a1);
g1_new(bf_key->b);
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
static int bbg_init_ciphertext(bbg_ciphertext_t* ciphertext) {
if (!ciphertext) {
return BFE_ERROR_INVALID_PARAM;
}
int ret = BFE_SUCCESS;
gt_null(ciphertext->a);
g2_null(ciphertext->b);
g1_null(ciphertext->c);
RLC_TRY {
gt_new(ciphertext->a);
g2_new(ciphertext->b);
g1_new(ciphertext->c);
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
/**
* Initializes public parameters.
* The total depth determines the number of basis elements h.
*
* @param[out] params - the initialized parameter set
* @param[in] depth - the total depth of the tree
*
* @return BFE_SUCCESS if no error occurs, an error code otherwise.
*/
static int bbg_init_public_params(bbg_public_params_t* params, unsigned int depth) {
if (!params || depth < 3) {
return BFE_ERROR_INVALID_PARAM;
}
params->total_depth = depth;
params->h = calloc(depth, sizeof(*params->h));
params->h_precomputation_tables =
calloc(depth * RLC_EP_TABLE, sizeof(*params->h_precomputation_tables));
if (!params->h || !params->h_precomputation_tables) {
free(params->h);
return BFE_ERROR;
}
g2_null(params->g_hat);
g1_null(params->g2);
g1_null(params->g3);
for (size_t i = 0; i < depth; ++i) {
g1_null(params->h[i]);
}
for (size_t i = 0; i < depth * RLC_EP_TABLE; ++i) {
g1_null(params->h_precomputation_tables[i]);
}
int ret = BFE_SUCCESS;
RLC_TRY {
g2_new(params->g_hat);
g1_new(params->g2);
g1_new(params->g3);
for (size_t i = 0; i < depth; ++i) {
g1_new(params->h[i]);
}
for (size_t i = 0; i < depth * RLC_EP_TABLE; ++i) {
g1_new(params->h_precomputation_tables[i]);
}
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
static int bbg_init_public_params_from_serialized(bbg_public_params_t* params,
const uint8_t* serialized) {
if (!params || !serialized) {
return BFE_ERROR_INVALID_PARAM;
}
const unsigned int depth = read_u32(&serialized);
if (bbg_init_public_params(params, depth) != BFE_SUCCESS) {
return BFE_ERROR;
}
int ret = BFE_SUCCESS;
RLC_TRY {
read_g2(params->g_hat, &serialized);
read_g1(params->g2, &serialized);
read_g1(params->g3, &serialized);
for (size_t i = 0; i < params->total_depth; ++i) {
read_g1(params->h[i], &serialized);
g1_mul_pre(params->h_precomputation_tables + i * RLC_EP_TABLE, params->h[i]);
}
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
static int bbg_init_master_key(bbg_master_key_t* mk) {
if (!mk) {
return BFE_ERROR_INVALID_PARAM;
}
int ret = BFE_SUCCESS;
g1_null(mk->mk);
RLC_TRY {
g1_new(mk->mk);
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
static int bbg_init_key(bbg_key_t* key) {
if (!key) {
return BFE_ERROR_INVALID_PARAM;
}
int ret = BFE_SUCCESS;
gt_null(key->k);
RLC_TRY {
gt_new(key->k);
}
RLC_CATCH_ANY {
ret = BFE_ERROR;
}
return ret;
}
static int bbg_init_identity_from(bbg_identity_t* dst, unsigned int depth,
const bbg_identity_t* src) {
if (src == NULL) {
return BFE_ERROR;
}
int ret = bbg_init_identity(dst, depth);
if (ret != BFE_SUCCESS) {
return ret;
}
memcpy(dst->id, src->id, sizeof(dst->id[0]) * MIN(dst->depth, src->depth));
return BFE_SUCCESS;
}
static int bbg_init_bf_key_from_secret_key(bbg_bf_key_t* bf_key, bbg_secret_key_t* sk) {
if (!bf_key || !sk) {
return BFE_ERROR_INVALID_PARAM;
}
int ret = bbg_init_bf_key(bf_key);
if (ret != BFE_SUCCESS) {
return ret;
}
g1_copy(bf_key->a0, sk->a0);
g2_copy(bf_key->a1, sk->a1);
g1_copy(bf_key->b, sk->b[0]);
return BFE_SUCCESS;
}
///@}
/* >> SERIALIZE AND DESERIALIZE << */
/**
* The following functions provides primitives to write (serialize) or
* read (deserialize) structures to/from memory in a predefined order.
*/
///@{
static void bbg_serialize_identity(uint8_t* dst, const bbg_identity_t* identity) {
write_u32(&dst, identity->depth);
for (size_t i = 0; i < identity->depth; ++i) {
write_u32(&dst, identity->id[i]);
}
}
static void bbg_serialize_public_key(uint8_t* serialized, bbg_public_key_t* public_key) {
write_gt(&serialized, public_key->pk);
}
static void bbg_serialize_secret_key(uint8_t* serialized, bbg_secret_key_t* secret_key) {
write_u32(&serialized, secret_key->num_delegatable_levels);
bbg_serialize_identity(serialized, &secret_key->identity);
serialized += bbg_get_identity_size(&secret_key->identity);
write_g1(&serialized, secret_key->a0);
write_g2(&serialized, secret_key->a1);
write_g1(&serialized, secret_key->associated_id);
for (size_t i = 0; i < secret_key->num_delegatable_levels; ++i) {
write_g1(&serialized, secret_key->b[i]);
}
}
static void bbg_serialize_bf_key(uint8_t* serialized, bbg_bf_key_t* bf_key) {
write_g1(&serialized, bf_key->a0);
write_g2(&serialized, bf_key->a1);
write_g1(&serialized, bf_key->b);
}
static void bbg_serialize_ciphertext(uint8_t* serialized, bbg_ciphertext_t* ciphertext) {
write_gt(&serialized, ciphertext->a);
write_g2(&serialized, ciphertext->b);
write_g1(&serialized, ciphertext->c);
}
static void bbg_serialize_public_params(uint8_t* serialized, bbg_public_params_t* public_params) {
write_u32(&serialized, public_params->total_depth);
write_g2(&serialized, public_params->g_hat);
write_g1(&serialized, public_params->g2);
write_g1(&serialized, public_params->g3);
for (size_t i = 0; i < public_params->total_depth; ++i) {
write_g1(&serialized, public_params->h[i]);
}
}
static void bbg_deserialize_identity(bbg_identity_t* identity, const uint8_t* src) {
identity->depth = read_u32(&src);
if (identity->id) {
free(identity->id);
}
// TODO: add error check
identity->id = calloc(identity->depth, sizeof(identity->id[0]));
for (size_t i = 0; i < identity->depth; ++i) {
identity->id[i] = read_u32(&src);
}
}
static void bbg_deserialize_public_key(bbg_public_key_t* public_key, const uint8_t* serialized) {
read_gt(public_key->pk, &serialized);
}
static void bbg_deserialize_secret_key(bbg_secret_key_t* secret_key, const uint8_t* serialized) {
unsigned int num_delegatable_levels = read_u32(&serialized);
bbg_init_secret_key(secret_key, num_delegatable_levels, 0);
bbg_deserialize_identity(&secret_key->identity, serialized);
serialized += bbg_get_identity_size(&secret_key->identity);
read_g1(secret_key->a0, &serialized);
read_g2(secret_key->a1, &serialized);
read_g1(secret_key->associated_id, &serialized);
for (size_t i = 0; i < num_delegatable_levels; ++i) {
read_g1(secret_key->b[i], &serialized);
}
}
static void bbg_deserialize_bf_key(bbg_bf_key_t* bf_key, const uint8_t* serialized) {
bbg_init_bf_key(bf_key);
read_g1(bf_key->a0, &serialized);
read_g2(bf_key->a1, &serialized);
read_g1(bf_key->b, &serialized);
}
static void bbg_deserialize_ciphertext(bbg_ciphertext_t* ciphertext, const uint8_t* serialized) {
read_gt(ciphertext->a, &serialized);
read_g2(ciphertext->b, &serialized);
read_g1(ciphertext->c, &serialized);
}
///@}
/* >> CLEAR AND FREE << */
/**
* The following functions safely clear unsued strcutures.
* Senstive data is overwritten and subsequently the memory is freed.
*/
///@{
static void bbg_clear_identity(bbg_identity_t* identity) {
if (identity) {
free(identity->id);
identity->id = NULL;
identity->depth = 0;
}
}
static void bbg_clear_public_key(bbg_public_key_t* pk) {
if (pk) {
gt_free(pk->pk);
}
}
static void bbg_clear_secret_key(bbg_secret_key_t* sk) {
if (sk) {
for (size_t idx = sk->num_delegatable_levels; idx; --idx) {
g1_set_infty(sk->b[idx - 1]);
g1_free(sk->b[idx - 1]);
}
free(sk->b);
sk->b = NULL;
g1_set_infty(sk->associated_id);
g1_free(sk->associated_id);
g2_set_infty(sk->a1);
g2_free(sk->a1);
g1_set_infty(sk->a0);
g1_free(sk->a0);
bbg_clear_identity(&sk->identity);
}
}
static void bbg_clear_bf_key(bbg_bf_key_t* bf_key) {
if (bf_key) {
g2_set_infty(bf_key->a1);
g2_free(bf_key->a1);
g1_set_infty(bf_key->a0);
g1_free(bf_key->a0);
g1_set_infty(bf_key->b);
g1_free(bf_key->b);
}
}
static void bbg_clear_ciphertext(bbg_ciphertext_t* ciphertext) {
if (ciphertext) {
g1_free(ciphertext->c);
g2_free(ciphertext->b);
gt_free(ciphertext->a);
}
}
static void bbg_clear_public_params(bbg_public_params_t* params) {
if (params) {
g2_free(params->g_hat);
g1_free(params->g2);
g1_free(params->g3);
if (params->h_precomputation_tables) {
for (size_t i = 0; i < (params->total_depth) * RLC_EP_TABLE; ++i) {
g1_free(params->h_precomputation_tables[i]);
}
free(params->h_precomputation_tables);
params->h_precomputation_tables = NULL;
}
if (params->h) {
for (size_t i = 0; i < params->total_depth; ++i) {
g1_free(params->h[i]);
}
free(params->h);
params->h = NULL;
}
params->total_depth = 0;
}
}
static void bbg_clear_master_key(bbg_master_key_t* mk) {
if (mk) {
g1_set_infty(mk->mk);
g1_free(mk->mk);
}
}
static void bbg_clear_key(bbg_key_t* key) {
if (key) {
gt_zero(key->k);
gt_free(key->k);
}
}
///@}
/* >> HASHING << */
/**
* The following functions provide an interface to hash different kinds of data and
* combine multpile data items into a single hash.
* To create domain separation different prefixes shall be used for different data.
*/
///@{
/**
* Generates the SHA3 hash of the public key of the given EdDSA key pair
*
* @param[out] hash - the generated hash
* @param[in] eddsa_pk - the EdDSA public key
*/
static void bbg_hash_eddsa_pk(bn_t hash, eddsa_pk_t* eddsa_pk) {
Keccak_HashInstance ctx;
Keccak_HashInitialize_SHAKE256(&ctx);
Keccak_HashUpdate(&ctx, &VERIFICATION_PREFIX, sizeof(VERIFICATION_PREFIX) * 8);
Keccak_HashUpdate(&ctx, eddsa_pk->key, crypto_sign_PUBLICKEYBYTES * 8);
Keccak_HashFinal(&ctx, NULL);
hash_squeeze_zp(hash, &ctx);
}
/**
* Updates the hash instance with the given integer.
*
* @param[out] ctx - the hash instance
* @param[in] v - the input integer
*/
static void hash_update_u32(Keccak_HashInstance* ctx, uint32_t v) {
v = htole32(v);
Keccak_HashUpdate(ctx, (const uint8_t*)&v, sizeof(v) * 8);
}
/**
* Updates the hash instance with the given tbfe public key.
* The order of the hash updates follows the serialization order
*
* @param[out] ctx - the hash instance
* @param[in] public_key - the tbfe public key that shall be hashed
*/
static void hash_update_tbfe_public_key(Keccak_HashInstance* ctx,
tbfe_bbg_public_key_t* public_key) {
// Bloom filter parameter
hash_update_u32(ctx, public_key->bloom_filter_hashes);
hash_update_u32(ctx, public_key->bloom_filter_size);
// Public key
hash_update_gt(ctx, public_key->pk.pk);
// Public parameter
hash_update_u32(ctx, public_key->params.total_depth);
hash_update_g2(ctx, public_key->params.g_hat);
hash_update_g1(ctx, public_key->params.g2);
hash_update_g1(ctx, public_key->params.g3);
for (size_t i = 0; i < public_key->params.total_depth; i++) {
hash_update_g1(ctx, public_key->params.h[i]);
}
}
/**
* Generates the hash of the given BBG ciphertext c = [a,b,c]
*
* @param[out] ctx - the hash instance
* @param[in] ciphertext - the input bbg ciphertext to be hashed
*/
static void hash_update_bbg_ciphertext(Keccak_HashInstance* ctx, bbg_ciphertext_t* ciphertext) {
hash_update_gt(ctx, ciphertext->a);
hash_update_g2(ctx, ciphertext->b);
hash_update_g1(ctx, ciphertext->c);
}
/**
* Generates the hash of an vector of BBG ciphertexts
*
* @param[out] ctx - the hash instance
* @param[in] ciphertexts - the input vector conatining multiple bbg ciphertexts
*/
static void hash_update_bbg_ciphertexts(Keccak_HashInstance* ctx, vector_t* ciphertexts) {
const unsigned int count = vector_size(ciphertexts);
for (size_t i = 0; i < count; ++i) {
// Apply hash function on every ciphertext and add it to old hash
hash_update_bbg_ciphertext(ctx, vector_get(ciphertexts, i));
}
}
///@}
/* >> BBG HIBE << */
/**
* The following functions provide the interface to the BBG HIBE scheme,
* as well as some utility functions.
*/
///@{