diff options
author | WuKong <rebirthmonkey@gmail.com> | 2015-09-04 09:25:34 +0200 |
---|---|---|
committer | WuKong <rebirthmonkey@gmail.com> | 2015-09-04 09:25:34 +0200 |
commit | 3baeb11a8fbcfcdbc31976d421f17b85503b3ecd (patch) | |
tree | 04891d88c1127148f1b390b5a24414e85b270aee /moon-abe/libbswabe-0.9/misc.c | |
parent | 67c5b73910f5fc437429c356978081b252a59480 (diff) |
init attribute-based encryption
Change-Id: Iba1a3d722110abf747a0fba366f3ebc911d25b25
Diffstat (limited to 'moon-abe/libbswabe-0.9/misc.c')
-rw-r--r-- | moon-abe/libbswabe-0.9/misc.c | 532 |
1 files changed, 532 insertions, 0 deletions
diff --git a/moon-abe/libbswabe-0.9/misc.c b/moon-abe/libbswabe-0.9/misc.c new file mode 100644 index 00000000..d6574ee8 --- /dev/null +++ b/moon-abe/libbswabe-0.9/misc.c @@ -0,0 +1,532 @@ +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <glib.h> +#include <pbc.h> + +#include "bswabe.h" +#include "private.h" +/* Serialize a 32 bit int + is used to serialize the lenght of the element + for each element first there its lenght on 32 bits then the element */ +void +serialize_uint32( GByteArray* b, uint32_t k ) +{ + int i; + guint8 byte; + + for( i = 3; i >= 0; i-- ) + { + byte = (k & 0xff<<(i*8))>>(i*8); + g_byte_array_append(b, &byte, 1); + } +} + +uint32_t +unserialize_uint32( GByteArray* b, int* offset ) +{ + int i; + uint32_t r; + + r = 0; + for( i = 3; i >= 0; i-- ) + r |= (b->data[(*offset)++])<<(i*8); + + return r; +} + +/* First stores the length of the element then the bytes of the element + Knowing the length of the element is necessary in order to unserialize it */ +void +serialize_element( GByteArray* b, element_t e ) +{ + uint32_t len; + unsigned char* buf; + + len = element_length_in_bytes(e); + serialize_uint32(b, len); + + buf = (unsigned char*) malloc(len); + element_to_bytes(buf, e); + g_byte_array_append(b, buf, len); + free(buf); +} + +void +unserialize_element( GByteArray* b, int* offset, element_t e ) +{ + uint32_t len; + unsigned char* buf; + + len = unserialize_uint32(b, offset); + + buf = (unsigned char*) malloc(len); + memcpy(buf, b->data + *offset, len); + *offset += len; + + element_from_bytes(e, buf); + free(buf); +} + +void +serialize_string( GByteArray* b, char* s ) +{ + g_byte_array_append(b, (unsigned char*) s, strlen(s) + 1); +} + +char* +unserialize_string( GByteArray* b, int* offset ) +{ + GString* s; + char* r; + char c; + + s = g_string_sized_new(32); + while( 1 ) + { + c = b->data[(*offset)++]; + if( c && c != EOF ) + g_string_append_c(s, c); + else + break; + } + + r = s->str; + g_string_free(s, 0); + + return r; +} + +GByteArray* +bswabe_pub_serialize( bswabe_pub_t* pub ) +{ + GByteArray* b; + + b = g_byte_array_new(); + serialize_string(b, pub->pairing_desc); + serialize_element(b, pub->g); + serialize_element(b, pub->h); + serialize_element(b, pub->gp); + serialize_element(b, pub->g_hat_alpha); + + return b; +} + +bswabe_pub_t* +bswabe_pub_unserialize( GByteArray* b, int free ) +{ + bswabe_pub_t* pub; + int offset; + + pub = (bswabe_pub_t*) malloc(sizeof(bswabe_pub_t)); + offset = 0; + + pub->pairing_desc = unserialize_string(b, &offset); + pairing_init_set_buf(pub->p, pub->pairing_desc, strlen(pub->pairing_desc)); + + element_init_G1(pub->g, pub->p); + element_init_G1(pub->h, pub->p); + element_init_G2(pub->gp, pub->p); + element_init_GT(pub->g_hat_alpha, pub->p); + + unserialize_element(b, &offset, pub->g); + unserialize_element(b, &offset, pub->h); + unserialize_element(b, &offset, pub->gp); + unserialize_element(b, &offset, pub->g_hat_alpha); + + if( free ) + g_byte_array_free(b, 1); + + return pub; +} + +GByteArray* +bswabe_msk_serialize( bswabe_msk_t* msk ) +{ + GByteArray* b; + + b = g_byte_array_new(); + serialize_element(b, msk->beta); + serialize_element(b, msk->g_alpha); + + return b; +} + +bswabe_msk_t* +bswabe_msk_unserialize( bswabe_pub_t* pub, GByteArray* b, int free ) +{ + bswabe_msk_t* msk; + int offset; + + msk = (bswabe_msk_t*) malloc(sizeof(bswabe_msk_t)); + offset = 0; + + element_init_Zr(msk->beta, pub->p); + element_init_G2(msk->g_alpha, pub->p); + + unserialize_element(b, &offset, msk->beta); + unserialize_element(b, &offset, msk->g_alpha); + + if( free ) + g_byte_array_free(b, 1); + + return msk; +} + +GByteArray* +bswabe_prv_serialize( bswabe_prv_t* prv ) +{ + GByteArray* b; + int i; + + b = g_byte_array_new(); + + serialize_element(b, prv->d); + serialize_uint32( b, prv->comps->len); + + for( i = 0; i < prv->comps->len; i++ ) + { + serialize_string( b, g_array_index(prv->comps, bswabe_prv_comp_t, i).attr); + serialize_element(b, g_array_index(prv->comps, bswabe_prv_comp_t, i).d); + serialize_element(b, g_array_index(prv->comps, bswabe_prv_comp_t, i).dp); + } + + return b; +} + +bswabe_prv_t* +bswabe_prv_unserialize( bswabe_pub_t* pub, GByteArray* b, int free ) +{ + bswabe_prv_t* prv; + int i; + int len; + int offset; + + prv = (bswabe_prv_t*) malloc(sizeof(bswabe_prv_t)); + offset = 0; + + element_init_G2(prv->d, pub->p); + unserialize_element(b, &offset, prv->d); + + prv->comps = g_array_new(0, 1, sizeof(bswabe_prv_comp_t)); + len = unserialize_uint32(b, &offset); + + for( i = 0; i < len; i++ ) + { + bswabe_prv_comp_t c; + + c.attr = unserialize_string(b, &offset); + + element_init_G2(c.d, pub->p); + element_init_G2(c.dp, pub->p); + + unserialize_element(b, &offset, c.d); + unserialize_element(b, &offset, c.dp); + + g_array_append_val(prv->comps, c); + } + + if( free ) + g_byte_array_free(b, 1); + + return prv; +} +/* +GByteArray* +peks_trap_serialize( peks_trap_t* trap ) +{ + GByteArray* b; + + b = g_byte_array_new(); + + serialize_element(b, trap->T); + + return b; +} +peks_trap_t* +peks_trap_unserialize( bswabe_pub_t* pub, GByteArray* b, int free ) +{ + peks_trap_t* trap; + int offset; + + trap = (peks_trap_t*) malloc(sizeof(peks_trap_t)); + offset = 0; + + unserialize_element(b, &offset, trap->T); + + if( free ) + g_byte_array_free(b, 1); + + return trap; +} + +*/ +GByteArray* +peks_ind_serialize( peks_ind_t* ind ) +{ + GByteArray* b; + int i; + + b = g_byte_array_new(); + + /* First serialize the number of components */ + serialize_uint32( b, ind->comps->len); + + for( i = 0; i < ind->comps->len; i++ ) + { + serialize_element(b, g_array_index(ind->comps, peks_sew_t, i).A ); + serialize_element(b, g_array_index(ind->comps, peks_sew_t, i).B ); + } + + return b; +} + +peks_ind_t* +peks_ind_unserialize( bswabe_pub_t* pub, GByteArray* b, int free ) +{ + peks_ind_t* ind; + int i; + int len; + int offset; + + ind = (peks_ind_t*) malloc(sizeof(peks_ind_t)); + offset = 0; + + ind->comps = g_array_new(0, 1, sizeof(peks_sew_t)); + len = unserialize_uint32(b, &offset); + + for( i = 0; i < len; i++ ) + { + peks_sew_t sew; + + + element_init_G1(sew.A, pub->p); + element_init_GT(sew.B, pub->p); + + unserialize_element(b, &offset, sew.A); + unserialize_element(b, &offset, sew.B); + + g_array_append_val(ind->comps, sew); + } + + if( free ) + g_byte_array_free(b, 1); + + return ind; +} + +void +serialize_policy( GByteArray* b, bswabe_policy_t* p ) +{ + int i; + + serialize_uint32(b, (uint32_t) p->k); + + serialize_uint32(b, (uint32_t) p->children->len); + if( p->children->len == 0 ) + { + serialize_string( b, p->attr); + serialize_element(b, p->c); + serialize_element(b, p->cp); + } + else + for( i = 0; i < p->children->len; i++ ) + serialize_policy(b, g_ptr_array_index(p->children, i)); +} + +bswabe_policy_t* +unserialize_policy( bswabe_pub_t* pub, GByteArray* b, int* offset ) +{ + int i; + int n; + bswabe_policy_t* p; + + p = (bswabe_policy_t*) malloc(sizeof(bswabe_policy_t)); + + p->k = (int) unserialize_uint32(b, offset); + p->attr = 0; + p->children = g_ptr_array_new(); + + n = unserialize_uint32(b, offset); + if( n == 0 ) + { + p->attr = unserialize_string(b, offset); + element_init_G1(p->c, pub->p); + element_init_G1(p->cp, pub->p); + unserialize_element(b, offset, p->c); + unserialize_element(b, offset, p->cp); + } + else + for( i = 0; i < n; i++ ) + g_ptr_array_add(p->children, unserialize_policy(pub, b, offset)); + + return p; +} + +GByteArray* +bswabe_cph_serialize( bswabe_cph_t* cph ) +{ + GByteArray* b; + + b = g_byte_array_new(); + serialize_element(b, cph->cs); + serialize_element(b, cph->c); + serialize_policy( b, cph->p); + serialize_string( b, cph->policy); + + return b; +} + +bswabe_cph_t* +bswabe_cph_unserialize( bswabe_pub_t* pub, GByteArray* b, int free ) +{ + bswabe_cph_t* cph; + int offset; + + cph = (bswabe_cph_t*) malloc(sizeof(bswabe_cph_t)); + offset = 0; + + element_init_GT(cph->cs, pub->p); + element_init_G1(cph->c, pub->p); + unserialize_element(b, &offset, cph->cs); + unserialize_element(b, &offset, cph->c); + cph->p = unserialize_policy(pub, b, &offset); + cph->policy = unserialize_string(b, &offset); + if( free ) + g_byte_array_free(b, 1); + + return cph; +} + +GByteArray* +peks_sew_serialize( peks_sew_t* sew ) +{ + GByteArray* b; + + b = g_byte_array_new(); + serialize_element(b, sew->A); + serialize_element(b, sew->B); + + return b; +} + +peks_sew_t* +peks_sew_unserialize( bswabe_pub_t* pub, GByteArray* b, int free ) +{ + peks_sew_t* sew; + int offset; + + sew = (peks_sew_t*) malloc(sizeof(peks_sew_t)); + offset = 0; + + element_init_G1(sew->A, pub->p); + element_init_GT(sew->B, pub->p); + + unserialize_element(b, &offset, sew->A); + unserialize_element(b, &offset, sew->B); + + if( free ) + g_byte_array_free(b, 1); + + return sew; +} + +GByteArray* +peks_trap_serialize( peks_trap_t* trap ) +{ + GByteArray* b; + + b = g_byte_array_new(); + serialize_element(b, trap->T); + + return b; +} + +peks_trap_t* +peks_trap_unserialize( bswabe_pub_t* pub, GByteArray* b, int free ) +{ + peks_trap_t* trap; + int offset; + + trap = (peks_trap_t*) malloc(sizeof(peks_trap_t)); + offset = 0; + + element_init_G1(trap->T, pub->p); + + unserialize_element(b, &offset, trap->T); + + if( free ) + g_byte_array_free(b, 1); + + return trap; +} + +void +bswabe_pub_free( bswabe_pub_t* pub ) +{ + element_clear(pub->g); + element_clear(pub->h); + element_clear(pub->gp); + element_clear(pub->g_hat_alpha); + pairing_clear(pub->p); + free(pub->pairing_desc); + free(pub); +} + +void +bswabe_msk_free( bswabe_msk_t* msk ) +{ + element_clear(msk->beta); + element_clear(msk->g_alpha); + free(msk); +} + +void +bswabe_prv_free( bswabe_prv_t* prv ) +{ + int i; + + element_clear(prv->d); + + for( i = 0; i < prv->comps->len; i++ ) + { + bswabe_prv_comp_t c; + + c = g_array_index(prv->comps, bswabe_prv_comp_t, i); + free(c.attr); + element_clear(c.d); + element_clear(c.dp); + } + + g_array_free(prv->comps, 1); + + free(prv); +} + +void +bswabe_policy_free( bswabe_policy_t* p ) +{ + int i; + + if( p->attr ) + { + free(p->attr); + element_clear(p->c); + element_clear(p->cp); + } + + for( i = 0; i < p->children->len; i++ ) + bswabe_policy_free(g_ptr_array_index(p->children, i)); + + g_ptr_array_free(p->children, 1); + + free(p); +} + +void +bswabe_cph_free( bswabe_cph_t* cph ) +{ + element_clear(cph->cs); + element_clear(cph->c); + bswabe_policy_free(cph->p); +} |