diff options
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | CMakeModules/ImportSodium.cmake | 10 | ||||
| -rw-r--r-- | CMakeModules/sodiumConfig.cmake | 10 | ||||
| -rw-r--r-- | libcmix-crypto/CMakeLists.txt | 21 | ||||
| -rw-r--r-- | libcmix-crypto/api.h | 26 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/curve25519.c | 12 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/curve25519.h | 9 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/null/CMakeLists.txt | 17 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/null/null_curve25519.c | 25 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/sodium/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/sodium/libsodium_curve25519.c | 55 | ||||
| -rw-r--r-- | libcmix-crypto/keymanagement.h | 21 | ||||
| -rw-r--r-- | libcmix-crypto/keypair.h | 11 | ||||
| -rw-r--r-- | libcmix-crypto/rsa/null/CMakeLists.txt | 0 | ||||
| -rw-r--r-- | libcmix-crypto/sharedkey.h | 14 | ||||
| -rw-r--r-- | scratchpad/scratchpad.c | 46 |
17 files changed, 230 insertions, 69 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 48bd387..0483633 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.6.1) project(cmix) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules) +list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules) find_package(Doxygen) if(DOXYGEN_FOUND) diff --git a/CMakeModules/ImportSodium.cmake b/CMakeModules/ImportSodium.cmake deleted file mode 100644 index a8b939c..0000000 --- a/CMakeModules/ImportSodium.cmake +++ /dev/null @@ -1,10 +0,0 @@ - -add_library(Sodium UNKNOWN IMPORTED) - -find_library(Sodium_LIBRARY NAMES "sodium") - -set_property(TARGET Sodium PROPERTY IMPORTED_LOCATION "${Sodium_LIBRARY}") - -find_path(Sodium_INCLUDE_DIR NAMES "sodium.h") - -set_property(TARGET Sodium PROPERTY INCLUDE_DIRECTORIES ${Sodium_INCLUDE_DIR}) diff --git a/CMakeModules/sodiumConfig.cmake b/CMakeModules/sodiumConfig.cmake new file mode 100644 index 0000000..f1c0760 --- /dev/null +++ b/CMakeModules/sodiumConfig.cmake @@ -0,0 +1,10 @@ + +add_library(sodium UNKNOWN IMPORTED) + +find_library(sodium_LIBRARY NAMES "sodium") + +set_property(TARGET sodium PROPERTY IMPORTED_LOCATION "${sodium_LIBRARY}") + +find_path(sodium_INCLUDE_DIR NAMES "sodium.h") + +set_property(TARGET sodium PROPERTY INCLUDE_DIRECTORIES ${sodium_INCLUDE_DIR}) 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 <stdbool.h> + +#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 <stdlib.h> + 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 <stddef.h> +#include <stdbool.h> + +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 <stddef.h> - -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 <stdlib.h> -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 --- /dev/null +++ b/libcmix-crypto/rsa/null/CMakeLists.txt 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 diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c index 848493d..c0730fd 100644 --- a/scratchpad/scratchpad.c +++ b/scratchpad/scratchpad.c @@ -2,25 +2,49 @@ #include "cmix.h" #include "message.h" +#include "curve25519.h" + #include <stdio.h> +#include <string.h> int main(int argc, char* argv[]) { - struct CmixBufferImpl buffer_impl = get_cmix_Curve25519_buffer_implementation(); - char* buffer = buffer_impl.allocate_cmix_buffer(3); + struct Api api = get_curve25519_implementation(); + + struct KeyPair pair1 = api.create_key_pair(); + struct KeyPair pair2 = api.create_key_pair(); + + struct SharedKey sk1 = api.derive_shared_key(pair1, pair2.pub, false); + struct SharedKey sk2 = api.derive_shared_key(pair2, pair1.pub, true); + + if(memcmp(sk1.shared, sk2.shared, sk1.shared_len) != 0) { + exit(-1); + } else { + printf("Yoepie!"); + } + + api.free_key_pair(pair1); + api.free_key_pair(pair2); + + api.free_shared_key(sk1); + api.free_shared_key(sk2); + + +// struct CmixBufferImpl buffer_impl = get_cmix_Curve25519_buffer_implementation(); +// char* buffer = buffer_impl.allocate_cmix_buffer(3); - int message_size = buffer_impl.message_length(); +// int message_size = buffer_impl.message_length(); - for(int i=0; i < 3; i++) { - buffer[i*message_size] = 'h'; - } +// for(int i=0; i < 3; i++) { +// buffer[i*message_size] = 'h'; +// } - for(int i=0; i < 3; i++) { - putc(buffer[i*message_size], stdout); - putc('\n', stdout); - } +// for(int i=0; i < 3; i++) { +// putc(buffer[i*message_size], stdout); +// putc('\n', stdout); +// } - buffer_impl.deallocate_cmix_buffer(buffer); +// buffer_impl.deallocate_cmix_buffer(buffer); return 0; } |
