diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-06-14 19:36:55 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-06-14 19:36:55 +0200 |
commit | b6b11a25946bc2756be5c4a1f691e04ca9d92d5f (patch) | |
tree | adb5d04f45f7719a142f60830ce3d50e81cb84c9 | |
parent | af15e7f597ad03d541a43199b29e422971e48ed8 (diff) |
libsodium support for asymmetric cryptofeature/libsodium-integration
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/crypto-sodium.c | 1 | ||||
-rw-r--r-- | src/crypto-sodium.h | 39 |
3 files changed, 42 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 5786a01..2db50bc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ bin_PROGRAMS = ptunnel-ng man8_MANS = ptunnel-ng.8 ptunnel_ng_CFLAGS = -Wall -Wextra -ptunnel_ng_LDADD = +ptunnel_ng_LDADD = -lsodium CLEANFILES = EXTTRA_DIST = @@ -39,6 +39,7 @@ ptunnel_ng_CFLAGS += -fsanitize=address -fsanitize=leak -fsanitize=undefined endif ptunnel_ng_SOURCES = \ + crypto-sodium.c \ md5.c \ challenge.c \ options.c \ diff --git a/src/crypto-sodium.c b/src/crypto-sodium.c new file mode 100644 index 0000000..c023ef4 --- /dev/null +++ b/src/crypto-sodium.c @@ -0,0 +1 @@ +#include "crypto-sodium.h" diff --git a/src/crypto-sodium.h b/src/crypto-sodium.h new file mode 100644 index 0000000..5994056 --- /dev/null +++ b/src/crypto-sodium.h @@ -0,0 +1,39 @@ +#ifndef CRYPTO_SODIUM_H +#define CRYPTO_SODIUM_H 1 + +#include <sodium.h> +#include <stdint.h> + +struct longterm_keypair { + uint8_t publickey[crypto_kx_PUBLICKEYBYTES]; + uint8_t secretkey[crypto_kx_SECRETKEYBYTES]; +}; + +struct longterm_keypair * generate_keypair_from_secretkey_hexstr_sodium(char const * const secretkey_hexstr, + size_t secretkey_hexstr_len) +{ + struct longterm_keypair * keypair = (struct longterm_keypair *)malloc(sizeof(*keypair)); + + if (keypair == NULL) { + return NULL; + } + + if (sodium_hex2bin(keypair->secretkey, sizeof(keypair->secretkey), + secretkey_hexstr, secretkey_hexstr_len, NULL, NULL, NULL) != 0) + { + goto error; + } + + if (crypto_scalarmult_base(keypair->publickey, keypair->secretkey) != 0) { + goto error; + } + + sodium_mlock(keypair, sizeof(*keypair)); + + return keypair; +error: + free(keypair); + return NULL; +} + +#endif |