Skip to content

Commit

Permalink
Fixed memory leaks in cpy06. Fixed 'all' not working in test as scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Chica committed Aug 28, 2024
1 parent 783ba68 commit 3419ebb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 81 deletions.
45 changes: 23 additions & 22 deletions src/groupsig/cpy06/join_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Note: cpy06_join_mem and cpy06_join_mgr compose an interactive protocol,
* as described in the CPY06 paper. Here, as in the paper, we assume that
* this protocol is run in a confidential and authenticated channel, with
* protection against replays. The user of this protocol should make sure of
* protection against replays. The user of this protocol should make sure of
* that.
*/
int cpy06_join_mem(message_t **mout,
Expand Down Expand Up @@ -84,7 +84,7 @@ int cpy06_join_mem(message_t **mout,
if (!(r = pbcext_element_Fr_init()))
GOTOENDRC(IERROR, cpy06_join_mem);
if (pbcext_element_Fr_random(r) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mem);
GOTOENDRC(IERROR, cpy06_join_mem);

/* I = yG1 + rQ */
if (!(g1 = pbcext_element_G1_init()))
Expand All @@ -110,30 +110,30 @@ int cpy06_join_mem(message_t **mout,
GOTOENDRC(IERROR, cpy06_join_mem);
if (pbcext_element_Fr_set(cpy06_memkey->_r, r) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mem);

/* First message is I */
if (pbcext_dump_element_G1_bytes(&bmsg, &len, I) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mem)

if(!*mout) {
if(!(_mout = message_from_bytes(bmsg, len)))
GOTOENDRC(IERROR, cpy06_join_mem);
GOTOENDRC(IERROR, cpy06_join_mem);
*mout = _mout;
} else {
_mout = *mout;
if(message_set_bytes(*mout, bmsg, len) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mem);
GOTOENDRC(IERROR, cpy06_join_mem);
}

}
/* 2nd step by member (seq 2/4): generate non-adaptive random xi with member

/* 2nd step by member (seq 2/4): generate non-adaptive random xi with member
committed randomness, and manager provided randomness */
else if (seq == 2) {

/* y and r values from seq = 0 are fetched from the memkey */

/* min = <u,v,I>
/* min = <u,v,I>
Read u, v and I values from input message */
if (!(u = pbcext_element_Fr_init()))
GOTOENDRC(IERROR, cpy06_join_mem);
Expand Down Expand Up @@ -190,7 +190,7 @@ int cpy06_join_mem(message_t **mout,
x[0] = cpy06_memkey->x;
x[1] = v;
x[2] = u;
x[3] = rr;
x[3] = rr;

i[0][0] = 0; i[0][1] = 0; // x*g1 (g[0],x[0])
i[1][0] = 1; i[1][1] = 0; // v*g1 (g[0],x[1])
Expand Down Expand Up @@ -219,9 +219,9 @@ int cpy06_join_mem(message_t **mout,
/* Output message is <I,pi,spk> */
if (pbcext_dump_element_G1_bytes(&bI, &Ilen, I) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mem);

len = Ilen + pilen + spklen;

if (!(bmsg = (byte_t *) mem_malloc(sizeof(byte_t)*len)))
GOTOENDRC(IERROR, cpy06_join_mem);
memcpy(bmsg, bI, Ilen);
Expand All @@ -230,14 +230,14 @@ int cpy06_join_mem(message_t **mout,

if(!*mout) {
if(!(_mout = message_from_bytes(bmsg, len)))
GOTOENDRC(IERROR, cpy06_join_mem);
GOTOENDRC(IERROR, cpy06_join_mem);
*mout = _mout;
} else {
_mout = *mout;
if(message_set_bytes(*mout, bmsg, len) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mem);
GOTOENDRC(IERROR, cpy06_join_mem);
}

}

/* 3rd step by member (seq 4/4): check cert */
Expand Down Expand Up @@ -292,6 +292,7 @@ int cpy06_join_mem(message_t **mout,
if (pbcext_element_Fr_set(cpy06_memkey->t, _cpy06_memkey->t) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mem);

if (_memkey) { cpy06_mem_key_free(_memkey); _memkey = NULL; }
}

else {
Expand Down Expand Up @@ -347,28 +348,28 @@ int cpy06_join_mem(message_t **mout,
pbcext_element_G1_free(cpy06_memkey->A); cpy06_memkey->A = NULL;
}
}

if (bkey) { mem_free(bkey); bkey = NULL; }
if (bmsg) { mem_free(bmsg); bmsg = NULL; }
if (bpi) { mem_free(bpi); bpi = NULL; }
if (bspk) { mem_free(bspk); bspk = NULL; }
if (spk) { spk_rep_free(spk); spk = NULL; }
if (bI) { mem_free(bI); bI = NULL; }
if (y) { pbcext_element_Fr_free(y); y = NULL; }
if (r) { pbcext_element_Fr_free(r); r = NULL; }
if (u) { pbcext_element_Fr_free(u); u = NULL; }
if (v) { pbcext_element_Fr_free(v); v = NULL; }
if (rr) { pbcext_element_Fr_free(rr); rr = NULL; }
if (g1) { pbcext_element_G1_free(g1); g1 = NULL; }
if (g1) { pbcext_element_G1_free(g1); g1 = NULL; }
if (I) { pbcext_element_G1_free(I); I = NULL; }
if (pi) { pbcext_element_G1_free(pi); pi = NULL; }
if (aux_g1) { pbcext_element_G1_free(aux_g1); aux_g1 = NULL; }
if (aux_g1) { pbcext_element_G1_free(aux_g1); aux_g1 = NULL; }
if (g2) { pbcext_element_G2_free(g2); g2 = NULL; }
if (aux_g2) { pbcext_element_G2_free(aux_g2); aux_g2 = NULL; }
if (aux_g2) { pbcext_element_G2_free(aux_g2); aux_g2 = NULL; }
if (aux_gt1) { pbcext_element_GT_free(aux_gt1); aux_gt1 = NULL; }
if (aux_gt2) { pbcext_element_GT_free(aux_gt2); aux_gt2 = NULL; }
if (aux_gt2) { pbcext_element_GT_free(aux_gt2); aux_gt2 = NULL; }

return rc;

}

/* join.c ends here */
82 changes: 38 additions & 44 deletions src/groupsig/cpy06/join_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int cpy06_join_mgr(message_t **mout,
int seq,
message_t *min,
groupsig_key_t *grpkey) {

groupsig_key_t *memkey;
cpy06_mem_key_t *cpy06_memkey;
cpy06_mgr_key_t *cpy06_mgrkey;
Expand Down Expand Up @@ -110,37 +110,33 @@ int cpy06_join_mgr(message_t **mout,
if (!(v = pbcext_element_Fr_init()))
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_Fr_random(v) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);

/* Send u, v, I to member */
if (pbcext_dump_element_Fr_bytes(&bu, &ulen, u) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_dump_element_Fr_bytes(&bv, &vlen, v) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_dump_element_G1_bytes(&bI, &Ilen, I) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
len = ulen + vlen + Ilen;

if (!(bmsg = (byte_t *) mem_malloc(sizeof(byte_t)*len)))
GOTOENDRC(IERROR, cpy06_join_mgr);
memcpy(bmsg, bu, ulen);
memcpy(&bmsg[ulen], bv, vlen);
memcpy(&bmsg[ulen+vlen], bI, Ilen);
if(!*mout) {

if(!*mout) {
if(!(_mout = message_from_bytes(bmsg, len))) {
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
}

*mout = _mout;

} else {

_mout = *mout;
if(message_set_bytes(*mout, bmsg, len) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);

}
GOTOENDRC(IERROR, cpy06_join_mgr);
}

}

Expand All @@ -155,15 +151,15 @@ int cpy06_join_mgr(message_t **mout,
if (pbcext_get_element_G1_bytes(I, &Ilen, min->bytes) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_get_element_G1_bytes(pi, &pilen, min->bytes + Ilen) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
if (!(spk = spk_rep_import(min->bytes + Ilen + pilen, &len)))
GOTOENDRC(IERROR, cpy06_join_mgr);

if (pbcext_dump_element_G1_bytes(&bpi, &pilen, pi) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
if (!(g1 = pbcext_element_G1_init())) GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_G1_from_string(&g1, BLS12_381_P, 10) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);

/* Verify spk */
Y[0] = pi;
Expand All @@ -180,7 +176,7 @@ int cpy06_join_mgr(message_t **mout,

prods[0] = 1;
prods[1] = 3;

if (spk_rep_verify(&ok,
Y, 2,
G, 3,
Expand All @@ -190,7 +186,7 @@ int cpy06_join_mgr(message_t **mout,
bpi, pilen) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);


if (!(memkey = cpy06_mem_key_init())) GOTOENDRC(IERROR, cpy06_join_mgr);
cpy06_memkey = (cpy06_mem_key_t *) memkey->key;

Expand All @@ -199,18 +195,15 @@ int cpy06_join_mgr(message_t **mout,
/* GOTOENDRC(IERROR, cpy06_join_mgr); */
/* if (pbcext_element_Fr_random(cpy06_memkey->x) == IERROR) */
/* GOTOENDRC(IERROR, cpy06_join_mgr); */

/* t \in_R Z^*_p */
if (!(cpy06_memkey->t = pbcext_element_Fr_init()))
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_Fr_random(cpy06_memkey->t) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);

// /* A = (q*g_1^x)^(1/t+\gamma) */
/* A = (q*pi)^(1/t+\gamma) */
if (!(g1 = pbcext_element_G1_init())) GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_G1_from_string(&g1, BLS12_381_P, 10) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
if (!(gammat = pbcext_element_Fr_init())) GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_Fr_add(gammat,
cpy06_mgrkey->gamma,
Expand All @@ -232,65 +225,65 @@ int cpy06_join_mgr(message_t **mout,
cpy06_memkey->A,
gammat) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);

/* Set memkey->x to 0 for export to work */
if (!(cpy06_memkey->x = pbcext_element_Fr_init()))
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_Fr_clear(cpy06_memkey->x) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);

/* Write the memkey into mout */
bkey = NULL;
if (cpy06_mem_key_export(&bkey, &size, memkey) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);

if(!*mout) {
if(!(_mout = message_from_bytes(bkey, size)))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
*mout = _mout;
} else {
_mout = *mout;
if(message_set_bytes(_mout, bkey, size) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
}

/* Update the gml, if any */
if(gml) {

/* Initialize the GML entry */
if(!(gml_entry = cpy06_gml_entry_init()))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);

cpy06_data = gml_entry->data;
if (!(cpy06_data->trapdoor = trapdoor_init(GROUPSIG_CPY06_CODE)))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
cpy06_trap = (cpy06_trapdoor_t *) cpy06_data->trapdoor->trap;

/* Open trapdoor */
if (!(cpy06_trap->open = pbcext_element_G1_init()))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_G1_set(cpy06_trap->open, cpy06_memkey->A))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);

/* Trace trapdoor */
if (!(cpy06_trap->trace = pbcext_element_G1_init()))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
if (pbcext_element_G1_set(cpy06_trap->trace, pi))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
/* if (pbcext_element_G1_mul(cpy06_trap->trace, */
/* g1, */
/* cpy06_memkey->x)) */
/* GOTOENDRC(IERROR, cpy06_join_mgr); */

/* Currently, CPY06 identities are just uint64_t's */
if (!(cpy06_data->id = identity_init(GROUPSIG_CPY06_CODE)))
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
*(cpy06_identity_t *) cpy06_data->id->id = gml->n;

if(gml_insert(gml, gml_entry) == IERROR)
GOTOENDRC(IERROR, cpy06_join_mgr);
GOTOENDRC(IERROR, cpy06_join_mgr);
}

}

else {
Expand All @@ -316,9 +309,10 @@ int cpy06_join_mgr(message_t **mout,
if (v) { pbcext_element_Fr_free(v); v = NULL; }
if (g1) { pbcext_element_G1_free(g1); g1 = NULL; }
if (I) { pbcext_element_G1_free(I); I = NULL; }
if (pi) { pbcext_element_G1_free(pi); pi = NULL; }
if (pi) { pbcext_element_G1_free(pi); pi = NULL; }
if (memkey) { cpy06_mem_key_free(memkey); memkey = NULL; }
if (bkey) { mem_free(bkey); bkey = NULL; }
if (spk) { spk_rep_free(spk); spk = NULL; }
if (bpi) { mem_free(bpi); bpi = NULL; }
if (bmsg) { mem_free(bmsg); bmsg = NULL; }
if (bu) { mem_free(bu); bu = NULL; }
Expand Down
Loading

0 comments on commit 3419ebb

Please sign in to comment.