diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-27 09:25:53 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-27 09:25:53 +0200 |
| commit | 25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c (patch) | |
| tree | 079ea63fcc874506072a91b13d2612b510cf158e /libcmix-crypto/curve25519 | |
| parent | 9eaf47d5dfa56ca79ae903aabfc2cf52bdfb981e (diff) | |
| download | cmix-25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c.tar.gz cmix-25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c.tar.bz2 cmix-25db9ff8a4cfb4b98aeeaae360e8c718b9c5e20c.zip | |
Adds libgcrypt implementation for elgamal in multiplicative group.
Also adapts the API to both handle sodium and gcrypt libraries.
Diffstat (limited to 'libcmix-crypto/curve25519')
| -rw-r--r-- | libcmix-crypto/curve25519/curve25519.c | 7 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/curve25519.h | 34 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/null/null_curve25519.c | 18 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/sodium/libsodium_curve25519.c | 47 |
4 files changed, 73 insertions, 33 deletions
diff --git a/libcmix-crypto/curve25519/curve25519.c b/libcmix-crypto/curve25519/curve25519.c index 4d53ddf..c4fae8d 100644 --- a/libcmix-crypto/curve25519/curve25519.c +++ b/libcmix-crypto/curve25519/curve25519.c @@ -3,11 +3,16 @@ struct Api get_curve25519_implementation() { + curve25519_initialize(); return (struct Api) { + &curve25519_initialize, &curve25519_create_keypair, &curve25519_keypair_deleter, + &curve25519_get_key_array, + &curve25519_add_public_share, &curve25519_derive_shared_key, - &curve25519_shared_key_deleter + &curve25519_shared_key_deleter, + &curve25519_deinitialize }; } diff --git a/libcmix-crypto/curve25519/curve25519.h b/libcmix-crypto/curve25519/curve25519.h index fde317a..5d004cf 100644 --- a/libcmix-crypto/curve25519/curve25519.h +++ b/libcmix-crypto/curve25519/curve25519.h @@ -11,15 +11,38 @@ extern "C" { */ /*! + * \brief curve25519_initialize initilalize curve25519 library + */ +extern void curve25519_initialize(void); + +/*! * \brief curve25519_create_keypair * \return A curve25519 keypair. */ -extern struct KeyPair curve25519_create_keypair(); +extern struct KeyPair curve25519_create_keypair(void); + /*! * \brief curve25519_keypair_deleter * \param p The keypair to free. */ -extern void curve25519_keypair_deleter(struct KeyPair p); +extern void curve25519_keypair_deleter(struct KeyPair* p); + +/*! + * \brief curve25519_get_pubkey_array + * \param pubkey + * \param buffer + * \param len + */ +extern void curve25519_get_key_array(char** buffer, size_t* len, void* pubkey); + +/*! + * \brief curve25519_add_public_share + * \param buffer + * \param out_len + * \param share + * \param pubkey + */ +extern void curve25519_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey); /*! * \brief curve25519_derive_shared_key @@ -28,12 +51,15 @@ extern void curve25519_keypair_deleter(struct KeyPair p); * \param swap_pub_order Should we swap the order in which we feed the public keys to the hash function. * \return A Shared key */ -extern struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* pub_key, bool swap_pub_order); +extern struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, void const* pub_key, bool swap_pub_order); + /*! * \brief curve25519_shared_key_deleter * \param s the Shared key to free. */ -extern void curve25519_shared_key_deleter(struct SharedKey s); +extern void curve25519_shared_key_deleter(struct SharedKey* s); + +extern void curve25519_deinitialize(void); /*! * \brief get_curve25519_implementation diff --git a/libcmix-crypto/curve25519/null/null_curve25519.c b/libcmix-crypto/curve25519/null/null_curve25519.c index 63280ec..33fb963 100644 --- a/libcmix-crypto/curve25519/null/null_curve25519.c +++ b/libcmix-crypto/curve25519/null/null_curve25519.c @@ -4,22 +4,28 @@ #include <stddef.h> #include <stdbool.h> -void curve25519_keypair_deleter(struct KeyPair p) {} +void curve25519_initialize(void){} -struct KeyPair curve25519_create_keypair() { +struct KeyPair curve25519_create_keypair(void) { return (struct KeyPair){ NULL, NULL, - 0, - 0 }; } -void curve25519_shared_key_deleter(struct SharedKey s) {} +void curve25519_keypair_deleter(struct KeyPair* p) {} + +void curve25519_get_key_array(char** buffer, size_t* len, void* key) {} + +void curve25519_add_public_share(char** buffer, size_t* len_out, char const* share, size_t in_len, void* pubkey) {} + +void curve25519_shared_key_deleter(struct SharedKey* s) {} + struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* pub_key, bool swap_pub_order) { return (struct SharedKey){ NULL, - 0 }; } + +void curve25519_deinitialize(void){}
\ No newline at end of file diff --git a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c index e86ec09..06453c5 100644 --- a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c +++ b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c @@ -6,23 +6,21 @@ #include <stddef.h> #include <stdlib.h> -void init() { +void curve25519_initialize(void) { if(sodium_init() == -1) { exit(-1); } } -void curve25519_keypair_deleter(struct KeyPair p) { - sodium_free(p.sec); - free(p.pub); +void curve25519_keypair_deleter(struct KeyPair* p) { + sodium_free(p->sec); + free(p->pub); - p.sec = NULL; - p.pub = NULL; + p->sec = NULL; + p->pub = NULL; } -struct KeyPair curve25519_create_keypair() { - init(); - +struct KeyPair curve25519_create_keypair(void) { unsigned char* sec = (unsigned char*) sodium_malloc(crypto_box_SECRETKEYBYTES); unsigned char* pub = (unsigned char*) malloc(crypto_box_PUBLICKEYBYTES); @@ -32,38 +30,42 @@ struct KeyPair curve25519_create_keypair() { return (struct KeyPair){ sec, pub, - crypto_box_SECRETKEYBYTES, - crypto_box_PUBLICKEYBYTES }; } -void curve25519_shared_key_deleter(struct SharedKey s) { - sodium_free(s.shared); +void curve25519_shared_key_deleter(struct SharedKey* s) { + sodium_free(s->shared); + + s->shared = NULL; +} + +void curve25519_get_key_array(char** buffer, size_t* len, void* key) { - s.shared = NULL; } -struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* pub_key, bool swap_pub_order) { - init(); +void curve25519_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey) { +} + +struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, void const* pub_key, bool swap_pub_order) { unsigned char* shared = (unsigned char*) sodium_malloc(crypto_generichash_BYTES); crypto_generichash_state h; unsigned char* scalarmult_q = (unsigned char*) sodium_malloc(crypto_scalarmult_BYTES); - if (crypto_scalarmult(scalarmult_q, pair.sec, pub_key) != 0) { + if (crypto_scalarmult(scalarmult_q, (unsigned char const*) pair.sec, (unsigned char const*) pub_key) != 0) { exit(-1); } crypto_generichash_init(&h, NULL, 0U, crypto_generichash_BYTES); crypto_generichash_update(&h, scalarmult_q, crypto_scalarmult_BYTES); if(swap_pub_order) { - crypto_generichash_update(&h, pub_key, crypto_box_PUBLICKEYBYTES); - crypto_generichash_update(&h, pair.pub, crypto_box_PUBLICKEYBYTES); + crypto_generichash_update(&h, (unsigned char const*) pub_key, crypto_box_PUBLICKEYBYTES); + crypto_generichash_update(&h, (unsigned char const*) pair.pub, crypto_box_PUBLICKEYBYTES); } else { - crypto_generichash_update(&h, pair.pub, crypto_box_PUBLICKEYBYTES); - crypto_generichash_update(&h, pub_key, crypto_box_PUBLICKEYBYTES); + crypto_generichash_update(&h, (unsigned char const*) pair.pub, crypto_box_PUBLICKEYBYTES); + crypto_generichash_update(&h, (unsigned char const*) pub_key, crypto_box_PUBLICKEYBYTES); } crypto_generichash_final(&h, shared, crypto_generichash_BYTES); @@ -71,7 +73,8 @@ struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char return (struct SharedKey){ shared, - crypto_generichash_BYTES }; } +void curve25519_deinitialize(void) {} + |
