From: Schanzenbach, Martin Date: Sun, 9 Jul 2017 17:23:26 +0000 (+0200) Subject: -add free ABE functions X-Git-Tag: gnunet-0.11.0rc0~24^2~66^2~1 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=cfc492f5d6c7c5786bd5d033892238a6889b5d7d;p=oweals%2Fgnunet.git -add free ABE functions --- diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 4d00103ab..69f6ce5d4 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h @@ -2138,6 +2138,8 @@ GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash, */ struct GNUNET_CRYPTO_AbeMasterKey * GNUNET_CRYPTO_cpabe_create_master_key (void); +void +GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key); /** * @ingroup crypto @@ -2148,6 +2150,8 @@ GNUNET_CRYPTO_cpabe_create_master_key (void); struct GNUNET_CRYPTO_AbeKey * GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *msk, char **attrs); +void +GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key); /** diff --git a/src/util/crypto_abe.c b/src/util/crypto_abe.c index 38812b412..ddaec83eb 100644 --- a/src/util/crypto_abe.c +++ b/src/util/crypto_abe.c @@ -148,6 +148,14 @@ GNUNET_CRYPTO_cpabe_create_master_key (void) return key; } +void +GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key) +{ + g_byte_array_unref (key->msk); + g_byte_array_unref (key->pub); + GNUNET_free (key); +} + struct GNUNET_CRYPTO_AbeKey* GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key, char **attrs) @@ -156,7 +164,6 @@ GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key, bswabe_pub_t* pub; bswabe_msk_t* msk; bswabe_prv_t* prv; - gchar* pub_data; gsize len; pub = bswabe_pub_unserialize(key->pub, 0); @@ -164,73 +171,69 @@ GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key, prv = bswabe_keygen(pub, msk, attrs); prv_key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey); prv_key->prv = bswabe_prv_serialize(prv); - pub_data = g_strndup ((gchar*)key->pub->data, - key->pub->len); + len = key->pub->len; - prv_key->pub = g_byte_array_new_take ((guint8*)pub_data, len); + printf ("Keylen %lu\n", len); + prv_key->pub = bswabe_pub_serialize (pub); GNUNET_assert (NULL != prv_key->prv); return prv_key; } +void +GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key) +{ + g_byte_array_unref (key->prv); + g_byte_array_unref (key->pub); + GNUNET_free (key); +} + ssize_t write_cpabe (void **result, GByteArray* cph_buf, - int file_len, GByteArray* aes_buf) + uint32_t file_len, GByteArray* aes_buf) { char *ptr; - int i; - ssize_t size; - size = aes_buf->len + cph_buf->len + 12; - *result = GNUNET_malloc (size); + uint32_t *len; + + *result = GNUNET_malloc (12 + cph_buf->len + aes_buf->len); ptr = *result; - for(i=3; i >= 0; i--) { - *ptr = (file_len & 0xff<<(i*8))>>(i*8); - ptr++; - } - for(i=3; i >= 0; i--) { - *ptr = (aes_buf->len & 0xff<<(i*8))>>(i*8); - ptr++; - } + len = (uint32_t*) ptr; + *len = htonl (file_len); + ptr += 4; + len = (uint32_t*) ptr; + *len = htonl (aes_buf->len); + ptr += 4; memcpy (ptr, aes_buf->data, aes_buf->len); ptr += aes_buf->len; - for(i=3; i >= 0; i--) { - *ptr = (cph_buf->len & 0xff<<(i*8))>>(i*8); - ptr++; - } + len = (uint32_t*) ptr; + *len = htonl (cph_buf->len); + ptr += 4; memcpy (ptr, cph_buf->data, cph_buf->len); - return size; + return 12 + cph_buf->len + aes_buf->len; } ssize_t read_cpabe (const void *data, GByteArray** cph_buf, GByteArray** aes_buf) { - int i; - ssize_t buf_len; - ssize_t tmp_len; + int buf_len; + int tmp_len; char *ptr; + uint32_t *len; *cph_buf = g_byte_array_new(); *aes_buf = g_byte_array_new(); ptr = (char*)data; - - buf_len = 0; - for(i=3; i >= 0; i--) { - buf_len |= *ptr<<(i*8); - ptr++; - } - - tmp_len = 0; - for(i=3; i >= 0; i--) { - tmp_len |= *ptr<<(i*8); - ptr++; - } + len = (uint32_t*)ptr; + buf_len = ntohl (*len); + ptr += 4; + len = (uint32_t*)ptr; + tmp_len = ntohl (*len); + ptr += 4; g_byte_array_set_size(*aes_buf, tmp_len); memcpy((*aes_buf)->data, ptr, tmp_len); ptr += tmp_len; - tmp_len = 0; - for(i=3; i >= 0; i--) { - tmp_len |= *ptr<<(i*8); - ptr++; - } + len = (uint32_t*)ptr; + tmp_len = ntohl (*len); + ptr += 4; g_byte_array_set_size(*cph_buf, tmp_len); memcpy((*cph_buf)->data, ptr, tmp_len);