From 8d1dbead20ae5fab5b2f63223c35ab6ebc957558 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 27 Nov 2019 19:41:38 +0900 Subject: [PATCH] Add set_libsodium_key_easy(), fixes/refactorings --- src/UDPConnection.cpp | 13 +++++++++++-- src/UDPConnection.h | 4 +++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 1d28083..d57cdac 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -2103,16 +2103,25 @@ UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining) { return UDPC::get_empty_pinfo(); } -void UDPC_set_libsodium_keys(UDPC_HContext ctx, unsigned char *sk, unsigned char *pk) { +int UDPC_set_libsodium_keys(UDPC_HContext ctx, unsigned char *sk, unsigned char *pk) { UDPC::Context *c = UDPC::verifyContext(ctx); if(!c || !c->flags.test(2)) { - return; + return 0; } std::lock_guard lock(c->mutex); std::memcpy(c->sk, sk, crypto_sign_SECRETKEYBYTES); std::memcpy(c->pk, pk, crypto_sign_PUBLICKEYBYTES); c->keysSet.store(true); + return 1; +} + +int UDPC_set_libsodium_key_easy(UDPC_HContext ctx, unsigned char *sk) { + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; + if(crypto_sign_ed25519_sk_to_pk(pk, sk) != 0) { + return 0; + } + return UDPC_set_libsodium_keys(ctx, sk, pk); } void UDPC_unset_libsodium_keys(UDPC_HContext ctx) { diff --git a/src/UDPConnection.h b/src/UDPConnection.h index 3180681..eb95ef6 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -184,7 +184,9 @@ UDPC_Event UDPC_get_event(UDPC_HContext ctx, unsigned long *remaining); UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining); -void UDPC_set_libsodium_keys(UDPC_HContext ctx, unsigned char *sk, unsigned char *pk); +int UDPC_set_libsodium_keys(UDPC_HContext ctx, unsigned char *sk, unsigned char *pk); + +int UDPC_set_libsodium_key_easy(UDPC_HContext ctx, unsigned char *sk); void UDPC_unset_libsodium_keys(UDPC_HContext ctx); -- 2.49.0