From 8fef6fe05ce3e9c0f6d29bb318b90b72a71db51f Mon Sep 17 00:00:00 2001 From: Christoph Egger Date: Mon, 6 Jan 2020 12:23:22 +0100 Subject: [PATCH] Fixes, working now, kind of base scheme --- include/DIPE.h | 6 +++--- src/DIPE.cxx | 36 ++++++++++++++++++------------------ tests/testDIPE.cpp | 25 +++++++++++-------------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/include/DIPE.h b/include/DIPE.h index 87ebab8..fa493a3 100644 --- a/include/DIPE.h +++ b/include/DIPE.h @@ -7,13 +7,13 @@ extern "C" { typedef struct dipe_master_secretkey* dipe_master_secretkey_t; typedef struct dipe_secretkey* dipe_secretkey_t; typedef struct dipe_ctxt* dipe_ctxt_t; - + void dipe_init(FILE* configfp, dipe_param_t* param); pairing_t* dipe_get_pairing(dipe_param_t param); - + void dipe_master_keygen(dipe_param_t param, size_t dimension, dipe_master_publickey_t* pk, dipe_master_secretkey_t* sk); void dipe_keygen(dipe_param_t param, dipe_master_secretkey_t msk, char* cid, element_t* y, dipe_secretkey_t* sk); - + void dipe_encrypt(dipe_param_t param, dipe_master_publickey_t mpk, element_t* x, element_t ptxt, dipe_ctxt_t* ctxt); void dipe_decrypt(dipe_param_t param, dipe_secretkey_t sk, char* cid, element_t* y, dipe_ctxt_t ctxt, element_t ptxt); diff --git a/src/DIPE.cxx b/src/DIPE.cxx index 321372d..5b39bb7 100644 --- a/src/DIPE.cxx +++ b/src/DIPE.cxx @@ -37,7 +37,7 @@ namespace { size_t dipe_h_length(element_t elem, size_t dimension) { return 16 /*cid*/ + dimension * element_length_in_bytes(elem); } - + void dipe_generate_h(char* cid, element_t* y, size_t dimension, unsigned char* result) { memcpy(result, cid, 16); unsigned char* next_element = result+16; @@ -52,13 +52,13 @@ void dipe_init(FILE* configfp, dipe_param_t* param) { char buffer[2<<16]; (*param) = (dipe_param_t)malloc(sizeof(dipe_param)); fread(buffer, 1, 2<<16, configfp); - + pairing_init_set_buf((*param)->pairing, buffer, 2<<16); element_init_G1((*param)->g1, (*param)->pairing); element_init_G2((*param)->g2, (*param)->pairing); element_init_GT((*param)->gt, (*param)->pairing); - + element_random((*param)->g1); element_random((*param)->g2); pairing_apply((*param)->gt, (*param)->g1, (*param)->g2, (*param)->pairing); @@ -85,7 +85,7 @@ void dipe_master_keygen(dipe_param_t param, size_t dimension, dipe_master_public element_init_Zr((*sk)->k[i], param->pairing); element_init_G1((*pk)->k[i], param->pairing); element_random((*sk)->k[i]); - element_pow_zn((*pk)->k[i], param->gt, (*sk)->k[i]); + element_pow_zn((*pk)->k[i], param->g1, (*sk)->k[i]); } } @@ -93,24 +93,24 @@ void dipe_keygen(dipe_param_t param, dipe_master_secretkey_t msk, char* cid, ele *sk = (dipe_secretkey_t)malloc(sizeof(dipe_secretkey)); (*sk)->dimension = msk->dimension; element_init_G2((*sk)->d, param->pairing); - + size_t hash_input_length = dipe_h_length(y[0], msk->dimension); unsigned char hash_input[hash_input_length]; element_t h; element_init_G2(h, param->pairing); - + dipe_generate_h(cid, y, msk->dimension, hash_input); element_from_hash(h, hash_input, hash_input_length); element_t innerp; element_t tmp; - + element_init_Zr(innerp, param->pairing); element_init_Zr(tmp, param->pairing); - element_set1(innerp); + element_set0(innerp); element_from_hash(h, hash_input, hash_input_length); - + for (size_t i = 0; i < msk->dimension; ++i) { element_mul(tmp, y[i], msk->k[i]); element_add(innerp, innerp, tmp); @@ -121,7 +121,7 @@ void dipe_keygen(dipe_param_t param, dipe_master_secretkey_t msk, char* cid, ele element_pow_zn(h, h, innerp); element_pow_zn(a2, param->g2, msk->a); element_mul((*sk)->d, a2, h); - + element_clear(h); element_clear(innerp); element_clear(tmp); @@ -130,8 +130,8 @@ void dipe_keygen(dipe_param_t param, dipe_master_secretkey_t msk, char* cid, ele void dipe_encrypt(dipe_param_t param, dipe_master_publickey_t mpk, element_t* x, element_t ptxt, dipe_ctxt_t* ctxt) { *ctxt = (dipe_ctxt_t)malloc(sizeof(dipe_ctxt)); - (*ctxt)->dimension = mpk->dimension; - + (*ctxt)->dimension = mpk->dimension; + element_t r; element_t s; element_init_Zr(r, param->pairing); @@ -142,19 +142,19 @@ void dipe_encrypt(dipe_param_t param, dipe_master_publickey_t mpk, element_t* x, element_init_G1((*ctxt)->s, param->pairing); element_pow_zn((*ctxt)->s, param->g1, s); - + element_t k1; element_t x1; element_init_G1(k1, param->pairing); element_init_G1(x1, param->pairing); - + (*ctxt)->cx = (element_t*)calloc(mpk->dimension, sizeof(element_t)); for (size_t i = 0; i < mpk->dimension; ++i){ element_pow_zn(x1, param->g1, x[i]); element_pow_zn(x1, x1, r); element_pow_zn(k1, mpk->k[i], s); - + element_init_G1((*ctxt)->cx[i], param->pairing); element_mul((*ctxt)->cx[i], k1, x1); } @@ -178,10 +178,10 @@ void dipe_decrypt(dipe_param_t param, dipe_secretkey_t sk, char* cid, element_t* unsigned char hash_input[hash_input_length]; element_t h; element_init_G2(h, param->pairing); - + dipe_generate_h(cid, y, sk->dimension, hash_input); element_from_hash(h, hash_input, hash_input_length); - + element_t tmp; element_t innerp; element_t hy; @@ -189,7 +189,7 @@ void dipe_decrypt(dipe_param_t param, dipe_secretkey_t sk, char* cid, element_t* element_init_GT(innerp, param->pairing); element_init_G2(hy, param->pairing); - element_set1(innerp); + element_set1(innerp); for (size_t i = 0; i < sk->dimension; ++i) { element_pow_zn(hy, h, y[i]); pairing_apply(tmp, ctxt->cx[i], hy, param->pairing); diff --git a/tests/testDIPE.cpp b/tests/testDIPE.cpp index 0a3e209..c335d48 100644 --- a/tests/testDIPE.cpp +++ b/tests/testDIPE.cpp @@ -24,10 +24,10 @@ TEST(DipeTest, DipeKeygen) { element_init_Zr(y[i], *dipe_get_pairing(param)); element_random(y[i]); } - + dipe_master_keygen(param, 5, &pk, &msk); dipe_keygen(param, msk, "1234567890abcdef", y, &sk); - + dipe_free_master_secretkey(msk); dipe_free_master_publickey(pk); dipe_free_secretkey(sk); @@ -42,13 +42,13 @@ TEST(DipeTest, DipeDecryptFail) { dipe_master_publickey_t pk; dipe_master_secretkey_t msk; dipe_secretkey_t sk; - + element_t y[5]; for (size_t i = 0; i < 5; ++i) { element_init_Zr(y[i], *dipe_get_pairing(param)); element_random(y[i]); } - + dipe_master_keygen(param, 5, &pk, &msk); dipe_keygen(param, msk, "1234567890abcdef", y, &sk); @@ -57,7 +57,7 @@ TEST(DipeTest, DipeDecryptFail) { element_init_GT(ptxtp, *dipe_get_pairing(param)); element_random(ptxt); - + dipe_encrypt(param, pk, y, ptxt, &ctxt); dipe_decrypt(param, sk, "1234567890abcdef", y, ctxt, ptxtp); @@ -66,7 +66,7 @@ TEST(DipeTest, DipeDecryptFail) { for (size_t i = 0; i < 5; ++i) { element_clear(y[i]); } - + element_clear(ptxt); element_clear(ptxtp); @@ -81,13 +81,13 @@ TEST(DipeTest, DipeDecryptSuccess) { dipe_master_publickey_t pk; dipe_master_secretkey_t msk; dipe_secretkey_t sk; - + element_t y[5]; for (size_t i = 0; i < 5; ++i) { element_init_Zr(y[i], *dipe_get_pairing(param)); element_set0(y[i]); } - + dipe_master_keygen(param, 5, &pk, &msk); dipe_keygen(param, msk, "1234567890abcdef", y, &sk); @@ -96,18 +96,16 @@ TEST(DipeTest, DipeDecryptSuccess) { element_init_GT(ptxtp, *dipe_get_pairing(param)); element_random(ptxt); - + dipe_encrypt(param, pk, y, ptxt, &ctxt); dipe_decrypt(param, sk, "1234567890abcdef", y, ctxt, ptxtp); - element_printf("%B\n", ptxt); - element_printf("%B\n", ptxtp); EXPECT_EQ(element_cmp(ptxt, ptxtp), 0); for (size_t i = 0; i < 5; ++i) { element_clear(y[i]); } - + element_clear(ptxt); element_clear(ptxtp); @@ -119,7 +117,7 @@ TEST(DipeTest, DipeDecryptSuccess) { int main(int argc, char **argv) { - ::testing::InitGoogleTest(&argc, argv); + ::testing::InitGoogleTest(&argc, argv); dipe_init(fopen("../tests/a.param", "r"), ¶m); @@ -127,4 +125,3 @@ int main(int argc, char **argv) { dipe_free_param(param); return result; } - -- 2.39.5