From 4f13051f763cab3d431847305eff913e4bf9d77a Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Fri, 2 Sep 2016 15:29:09 +0200 Subject: Changed to api to be less repetative in use. --- libcmix-crypto/CMakeLists.txt | 21 +++++++-- libcmix-crypto/api.h | 26 ++++++++++ libcmix-crypto/curve25519/CMakeLists.txt | 12 +++-- libcmix-crypto/curve25519/curve25519.c | 12 +++++ libcmix-crypto/curve25519/curve25519.h | 9 +++- libcmix-crypto/curve25519/null/CMakeLists.txt | 17 +++++++ libcmix-crypto/curve25519/null/null_curve25519.c | 25 ++++++++++ libcmix-crypto/curve25519/sodium/CMakeLists.txt | 9 ++-- .../curve25519/sodium/libsodium_curve25519.c | 55 ++++++++++++++++++++-- libcmix-crypto/keymanagement.h | 21 --------- libcmix-crypto/keypair.h | 11 ----- libcmix-crypto/rsa/null/CMakeLists.txt | 0 libcmix-crypto/sharedkey.h | 14 ++++++ 13 files changed, 184 insertions(+), 48 deletions(-) create mode 100644 libcmix-crypto/api.h create mode 100644 libcmix-crypto/curve25519/curve25519.c create mode 100644 libcmix-crypto/curve25519/null/CMakeLists.txt create mode 100644 libcmix-crypto/curve25519/null/null_curve25519.c delete mode 100644 libcmix-crypto/keymanagement.h create mode 100644 libcmix-crypto/rsa/null/CMakeLists.txt create mode 100644 libcmix-crypto/sharedkey.h (limited to 'libcmix-crypto') diff --git a/libcmix-crypto/CMakeLists.txt b/libcmix-crypto/CMakeLists.txt index d40d44e..f039946 100644 --- a/libcmix-crypto/CMakeLists.txt +++ b/libcmix-crypto/CMakeLists.txt @@ -1,13 +1,14 @@ -add_library(cmix-crypto INTERFACE) +add_library(cmix-crypto-interface INTERFACE) -target_include_directories(cmix-crypto +target_include_directories(cmix-crypto-interface INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} ) -target_sources(cmix-crypto +target_sources(cmix-crypto-interface + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/api.h INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/keypair.h - INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/keymanagement.h + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/sharedkey.h INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/message.h ) @@ -17,6 +18,18 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/curve25519/) include(rsa_implementations) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/rsa) +add_library(cmix-crypto INTERFACE) + +target_include_directories(cmix-crypto + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} +) + +target_sources(cmix-crypto + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/api.h + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/keypair.h + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/sharedkey.h + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/message.h +) option(UseEC "Use curve25519 instead of RSA" ON) diff --git a/libcmix-crypto/api.h b/libcmix-crypto/api.h new file mode 100644 index 0000000..a798876 --- /dev/null +++ b/libcmix-crypto/api.h @@ -0,0 +1,26 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#include "keypair.h" +#include "sharedkey.h" + +typedef struct KeyPair (*KeyPairCreator)(); +typedef void (*KeyPairDeleter)(struct KeyPair); +typedef struct SharedKey (*SharedKeyDeriver)(struct KeyPair, unsigned char*, bool); +typedef void (*SharedKeyDeleter)(struct SharedKey); + +struct Api { + KeyPairCreator create_key_pair; + KeyPairDeleter free_key_pair; + SharedKeyDeriver derive_shared_key; + SharedKeyDeleter free_shared_key; +}; + +#ifdef __cplusplus +} +#endif diff --git a/libcmix-crypto/curve25519/CMakeLists.txt b/libcmix-crypto/curve25519/CMakeLists.txt index dc29c4d..ba79d87 100644 --- a/libcmix-crypto/curve25519/CMakeLists.txt +++ b/libcmix-crypto/curve25519/CMakeLists.txt @@ -10,17 +10,23 @@ target_sources(curve25519-interface ) target_link_libraries(curve25519-interface - INTERFACE cmix-crypto + INTERFACE cmix-crypto-interface ) foreach(impl ${curve25519_implementations}) add_subdirectory(${impl}) endforeach() -add_library(curve25519-implementation INTERFACE) +add_library(curve25519-implementation + curve25519.h curve25519.c +) + +target_include_directories(curve25519-implementation + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} +) target_link_libraries(curve25519-implementation - INTERFACE curve25519-${curve25519_implementation} + PUBLIC curve25519-${curve25519_implementation} ) diff --git a/libcmix-crypto/curve25519/curve25519.c b/libcmix-crypto/curve25519/curve25519.c new file mode 100644 index 0000000..824daf9 --- /dev/null +++ b/libcmix-crypto/curve25519/curve25519.c @@ -0,0 +1,12 @@ + +#include "curve25519.h" + +struct Api get_curve25519_implementation() +{ + return (struct Api) { + &curve25519_create_keypair, + &curve25519_keypair_deleter, + &curve25519_derive_shared_key, + &curve25519_shared_key_deleter + }; +} diff --git a/libcmix-crypto/curve25519/curve25519.h b/libcmix-crypto/curve25519/curve25519.h index 319e693..ef466a3 100644 --- a/libcmix-crypto/curve25519/curve25519.h +++ b/libcmix-crypto/curve25519/curve25519.h @@ -4,11 +4,18 @@ extern "C" { #endif -#include "keypair.h" +#include "api.h" #include + extern struct KeyPair curve25519_create_keypair(); +extern void curve25519_keypair_deleter(struct KeyPair p); + +extern struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char* pub_key, bool swap_pub_order); +extern void curve25519_shared_key_deleter(struct SharedKey s); + +struct Api get_curve25519_implementation(); #ifdef __cplusplus } diff --git a/libcmix-crypto/curve25519/null/CMakeLists.txt b/libcmix-crypto/curve25519/null/CMakeLists.txt new file mode 100644 index 0000000..bf30adc --- /dev/null +++ b/libcmix-crypto/curve25519/null/CMakeLists.txt @@ -0,0 +1,17 @@ +include(get_target_name) + +get_target_name(target_name) + +add_library(${target_name} SHARED + null_curve25519.c +) + +target_compile_options(${target_name} + PRIVATE -std=c99 +) + +target_link_libraries(${target_name} + INTERFACE cmix-crypto + PRIVATE curve25519-interface + PUBLIC sodium +) diff --git a/libcmix-crypto/curve25519/null/null_curve25519.c b/libcmix-crypto/curve25519/null/null_curve25519.c new file mode 100644 index 0000000..317455f --- /dev/null +++ b/libcmix-crypto/curve25519/null/null_curve25519.c @@ -0,0 +1,25 @@ + +#include "api.h" + +#include +#include + +void curve25519_keypair_deleter(struct KeyPair p) {} + +struct KeyPair curve25519_create_keypair() { + return (struct KeyPair){ + NULL, + NULL, + 0, + 0 + }; +} + +void curve25519_shared_key_deleter(struct SharedKey s) {} + +struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char* pub_key, bool swap_pub_order) { + return (struct SharedKey){ + NULL, + 0 + }; +} diff --git a/libcmix-crypto/curve25519/sodium/CMakeLists.txt b/libcmix-crypto/curve25519/sodium/CMakeLists.txt index 213a49f..7679a88 100644 --- a/libcmix-crypto/curve25519/sodium/CMakeLists.txt +++ b/libcmix-crypto/curve25519/sodium/CMakeLists.txt @@ -2,9 +2,9 @@ include(get_target_name) get_target_name(target_name) -include(ImportSodium) +find_package(sodium REQUIRED CONFIG) -add_library(${target_name} +add_library(${target_name} SHARED libsodium_curve25519.c ) @@ -13,6 +13,7 @@ target_compile_options(${target_name} ) target_link_libraries(${target_name} - PUBLIC curve25519-interface - PUBLIC Sodium + INTERFACE cmix-crypto + PRIVATE curve25519-interface + PUBLIC sodium ) 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 + }; } + diff --git a/libcmix-crypto/keymanagement.h b/libcmix-crypto/keymanagement.h deleted file mode 100644 index 18b578a..0000000 --- a/libcmix-crypto/keymanagement.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include "keypair.h" - -#include - -typedef struct KeyPair(*CmixKeyPairCreator)(); -typedef struct KeyPair(*CmixKeyPairLoader)(char*, char*); - -struct CmixKeyManagementImpl { - CmixKeyPairCreator create_new_keypair; - CmixKeyPairLoader load_keypair; -}; - -#ifdef __cplusplus -} // extern "C" -#endif diff --git a/libcmix-crypto/keypair.h b/libcmix-crypto/keypair.h index df5ec88..9587db3 100644 --- a/libcmix-crypto/keypair.h +++ b/libcmix-crypto/keypair.h @@ -6,24 +6,13 @@ extern "C" { #include -struct KeyPair; - -typedef void(*KeyPairDeleter)(struct KeyPair*); - -void keypair_deleter(struct KeyPair* p) ; - struct KeyPair { unsigned char* sec; unsigned char* pub; unsigned int sec_len; unsigned int pub_len; - KeyPairDeleter deleter; }; -void keypair_deleter(struct KeyPair* p) { - free(p->sec); - free(p->pub); -} #ifdef __cplusplus } diff --git a/libcmix-crypto/rsa/null/CMakeLists.txt b/libcmix-crypto/rsa/null/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/libcmix-crypto/sharedkey.h b/libcmix-crypto/sharedkey.h new file mode 100644 index 0000000..9c959fe --- /dev/null +++ b/libcmix-crypto/sharedkey.h @@ -0,0 +1,14 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +struct SharedKey { + unsigned char* shared; + unsigned int shared_len; +}; + +#ifdef __cplusplus +} // extern "C" +#endif -- cgit v1.2.3-70-g09d2