diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-09-02 15:29:09 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-09-02 15:29:09 +0200 |
| commit | 4f13051f763cab3d431847305eff913e4bf9d77a (patch) | |
| tree | bb20393d714ede1a0200653e2bddbbb694b56f16 /libcmix-crypto/curve25519/sodium/libsodium_curve25519.c | |
| parent | dffdb1388991c5b5688139bfa093c8581a0f3a36 (diff) | |
| download | cmix-4f13051f763cab3d431847305eff913e4bf9d77a.tar.gz cmix-4f13051f763cab3d431847305eff913e4bf9d77a.tar.bz2 cmix-4f13051f763cab3d431847305eff913e4bf9d77a.zip | |
Changed to api to be less repetative in use.
Diffstat (limited to 'libcmix-crypto/curve25519/sodium/libsodium_curve25519.c')
| -rw-r--r-- | libcmix-crypto/curve25519/sodium/libsodium_curve25519.c | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c index 8fd087a..59e9258 100644 --- a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c +++ b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c @@ -12,9 +12,12 @@ void init() { } } -void sodium_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; } struct KeyPair curve25519_create_keypair() { @@ -26,5 +29,49 @@ struct KeyPair curve25519_create_keypair() { randombytes_buf(sec, crypto_box_SECRETKEYBYTES); crypto_scalarmult_base(pub, sec); - return (struct KeyPair){sec, pub, crypto_box_SECRETKEYBYTES, crypto_box_PUBLICKEYBYTES, &sodium_curve25519_keypair_deleter}; + return (struct KeyPair){ + sec, + pub, + crypto_box_SECRETKEYBYTES, + crypto_box_PUBLICKEYBYTES + }; +} + +void curve25519_shared_key_deleter(struct SharedKey s) { + sodium_free(s.shared); + + s.shared = NULL; +} + +struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char* pub_key, bool swap_pub_order) { + init(); + + 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) { + 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); + } else { + crypto_generichash_update(&h, pair.pub, crypto_box_PUBLICKEYBYTES); + crypto_generichash_update(&h, pub_key, crypto_box_PUBLICKEYBYTES); + } + crypto_generichash_final(&h, shared, crypto_generichash_BYTES); + + sodium_free(scalarmult_q); + + return (struct SharedKey){ + shared, + crypto_generichash_BYTES + }; } + |
