#include "curve25519.h" #include #include #include void sodium_curve25519_initialize(void) { if(sodium_init() == -1) { exit(-1); } } void sodium_curve25519_delete_keypair(struct KeyPair* p) { sodium_free(p->sec); free(p->pub); p->sec = NULL; p->pub = NULL; } struct KeyPair sodium_curve25519_create_keypair(void) { unsigned char* sec = (unsigned char*) sodium_malloc(crypto_box_SECRETKEYBYTES); unsigned char* pub = (unsigned char*) malloc(crypto_box_PUBLICKEYBYTES); randombytes_buf(sec, crypto_box_SECRETKEYBYTES); crypto_scalarmult_base(pub, sec); return (struct KeyPair){ sec, pub, }; } void sodium_curve25519_shared_key_deleter(struct SharedKey* s) { sodium_free(s->shared); s->shared = NULL; } void sodium_curve25519_key_to_array(unsigned char** buffer, size_t* len, void* key) { (void)buffer; (void)len; (void)key; } void sodium_curve25519_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey) { (void) buffer; (void) out_len; (void) share; (void) in_len; (void) pubkey; } GroupElement sodium_curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* other_pub, size_t pub_len, unsigned char const* value, size_t value_len, void* priv_value, bool swap_pub_order) { (void) pub_len; (void) value_len; 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, (unsigned char const*) priv_value, (unsigned char const*) value) != 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, (unsigned char const*) other_pub, crypto_box_PUBLICKEYBYTES); crypto_generichash_update(&h, (unsigned char const*) pair.pub, crypto_box_PUBLICKEYBYTES); } else { crypto_generichash_update(&h, (unsigned char const*) pair.pub, crypto_box_PUBLICKEYBYTES); crypto_generichash_update(&h, (unsigned char const*) other_pub, crypto_box_PUBLICKEYBYTES); } crypto_generichash_final(&h, shared, crypto_generichash_BYTES); sodium_free(scalarmult_q); return shared; } void sodium_curve25519_deinitialize(void) {} Initializer curve25519_initialize = &sodium_curve25519_initialize; KeyPairCreator curve25519_create_keypair = &sodium_curve25519_create_keypair; KeyPairDeleter curve25519_delete_keypair = &sodium_curve25519_delete_keypair; ElementToArray curve25519_element_to_array = NULL;//&sodium_curve25519_element_to_array; BufferDeleter curve25519_free_buffer = NULL;//&sodium_curve25519_free_buffer; ArrayToElement curve25519_array_to_element = NULL;//&sodium_curve25519_array_to_element; GroupElementGetter curve25519_get_group_element = NULL;//&sodium_curve25519_get_group_element; GroupElementDeleter curve25519_delete_group_element = NULL;//&sodium_curve25519_delete_group_element; KeyExchangeValueGetter curve25519_get_key_exchange_value = NULL;//&sodium_curve25519_get_key_exchange_value; GroupElementMultiplier curve25519_multiply = NULL;//&sodium_curve25519_multiply; DecryptionShareGetter curve25519_get_decryption_share = NULL;//&sodium_curve25519_get_decryption_share; GroupElementArraySizeGetter curve25519_get_group_element_array_size = NULL;//&sodium_curve25519_get_group_element_array_size; PublicShareAdder curve25519_add_public_share = NULL;//&sodium_curve25519_add_public_share; SharedKeyDeriver curve25519_derive_shared_key = &sodium_curve25519_derive_shared_key; SharedKeyDeleter curve25519_delete_shared_key = NULL;//&sodium_curve25519_delete_shared_key; Encrypter curve25519_encrypt = NULL;//&sodium_curve25519_encrypt; Deinitializer curve25519_deinitialize = &sodium_curve25519_deinitialize;