aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-09-01 13:07:57 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-09-01 14:05:40 +0200
commitdffdb1388991c5b5688139bfa093c8581a0f3a36 (patch)
treeaf0d82089d335e8b9eda6d6f754da4b2905eb901
parent9d7701c370f06be663f2a485507d388ab5194ca8 (diff)
downloadcmix-dffdb1388991c5b5688139bfa093c8581a0f3a36.tar.gz
cmix-dffdb1388991c5b5688139bfa093c8581a0f3a36.tar.bz2
cmix-dffdb1388991c5b5688139bfa093c8581a0f3a36.zip
Added a sodium implementation for creating private and public keys.
-rw-r--r--CMakeModules/ImportSodium.cmake10
-rw-r--r--libcmix-crypto/CMakeLists.txt29
-rw-r--r--libcmix-crypto/curve25519/CMakeLists.txt6
-rw-r--r--libcmix-crypto/curve25519/sodium/CMakeLists.txt3
-rw-r--r--libcmix-crypto/curve25519/sodium/libsodium_curve25519.c21
-rw-r--r--libcmix-crypto/keymanagement.c1
-rw-r--r--libcmix-crypto/keypair.h4
-rw-r--r--libcmix-crypto/message.c0
8 files changed, 49 insertions, 25 deletions
diff --git a/CMakeModules/ImportSodium.cmake b/CMakeModules/ImportSodium.cmake
new file mode 100644
index 0000000..a8b939c
--- /dev/null
+++ b/CMakeModules/ImportSodium.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 907c601..d40d44e 100644
--- a/libcmix-crypto/CMakeLists.txt
+++ b/libcmix-crypto/CMakeLists.txt
@@ -1,10 +1,16 @@
-add_library(cmix-crypto-interface INTERFACE)
+add_library(cmix-crypto INTERFACE)
-target_include_directories(cmix-crypto-interface
+target_include_directories(cmix-crypto
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
+target_sources(cmix-crypto
+ INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/keypair.h
+ INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/keymanagement.h
+ INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/message.h
+)
+
include(curve25519_implementations)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/curve25519/)
@@ -12,24 +18,9 @@ include(rsa_implementations)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/rsa)
-add_library(cmix-crypto
- message.h message.c
- keymanagement.h keymanagement.c
- keypair.h
-)
-
-target_include_directories(cmix-crypto
- PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
-)
-
-target_compile_options(cmix-crypto
- PRIVATE "-std=c99"
-)
-
option(UseEC "Use curve25519 instead of RSA" ON)
-
target_link_libraries(cmix-crypto
- PUBLIC curve25519-implementation
-# PUBLIC rsa-implementation
+ INTERFACE curve25519-implementation
+# INTERFACE rsa-implementation
)
diff --git a/libcmix-crypto/curve25519/CMakeLists.txt b/libcmix-crypto/curve25519/CMakeLists.txt
index 7885e7d..dc29c4d 100644
--- a/libcmix-crypto/curve25519/CMakeLists.txt
+++ b/libcmix-crypto/curve25519/CMakeLists.txt
@@ -5,8 +5,12 @@ target_include_directories(curve25519-interface
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
+target_sources(curve25519-interface
+ INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/curve25519.h
+)
+
target_link_libraries(curve25519-interface
- INTERFACE cmix-crypto-interface
+ INTERFACE cmix-crypto
)
foreach(impl ${curve25519_implementations})
diff --git a/libcmix-crypto/curve25519/sodium/CMakeLists.txt b/libcmix-crypto/curve25519/sodium/CMakeLists.txt
index 9fbf010..213a49f 100644
--- a/libcmix-crypto/curve25519/sodium/CMakeLists.txt
+++ b/libcmix-crypto/curve25519/sodium/CMakeLists.txt
@@ -2,6 +2,8 @@ include(get_target_name)
get_target_name(target_name)
+include(ImportSodium)
+
add_library(${target_name}
libsodium_curve25519.c
)
@@ -12,4 +14,5 @@ target_compile_options(${target_name}
target_link_libraries(${target_name}
PUBLIC curve25519-interface
+ PUBLIC Sodium
)
diff --git a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c
index 23dbf12..8fd087a 100644
--- a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c
+++ b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c
@@ -1,13 +1,30 @@
#include "curve25519.h"
+#include <sodium.h>
+
#include <stddef.h>
+#include <stdlib.h>
+
+void init() {
+ if(sodium_init() == -1) {
+ exit(-1);
+ }
+}
void sodium_curve25519_keypair_deleter(struct KeyPair* p) {
- free(p->sec);
+ sodium_free(p->sec);
free(p->pub);
}
struct KeyPair curve25519_create_keypair() {
- return (struct KeyPair){NULL, NULL, 0, 0, &sodium_curve25519_keypair_deleter};
+ init();
+
+ 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, crypto_box_SECRETKEYBYTES, crypto_box_PUBLICKEYBYTES, &sodium_curve25519_keypair_deleter};
}
diff --git a/libcmix-crypto/keymanagement.c b/libcmix-crypto/keymanagement.c
deleted file mode 100644
index 49e8316..0000000
--- a/libcmix-crypto/keymanagement.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "keymanagement.h"
diff --git a/libcmix-crypto/keypair.h b/libcmix-crypto/keypair.h
index 90cf16b..df5ec88 100644
--- a/libcmix-crypto/keypair.h
+++ b/libcmix-crypto/keypair.h
@@ -13,8 +13,8 @@ typedef void(*KeyPairDeleter)(struct KeyPair*);
void keypair_deleter(struct KeyPair* p) ;
struct KeyPair {
- char* sec;
- char* pub;
+ unsigned char* sec;
+ unsigned char* pub;
unsigned int sec_len;
unsigned int pub_len;
KeyPairDeleter deleter;
diff --git a/libcmix-crypto/message.c b/libcmix-crypto/message.c
deleted file mode 100644
index e69de29..0000000
--- a/libcmix-crypto/message.c
+++ /dev/null