diff --git a/lib/include/chiaki/ecdh.h b/lib/include/chiaki/ecdh.h
index 01e5c93..d6cbdfc 100644
--- a/lib/include/chiaki/ecdh.h
+++ b/lib/include/chiaki/ecdh.h
@@ -37,6 +37,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_init(ChiakiECDH *ecdh);
CHIAKI_EXPORT void chiaki_ecdh_fini(ChiakiECDH *ecdh);
CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_get_local_pub_key(ChiakiECDH *ecdh, uint8_t *key_out, size_t *key_out_size, uint8_t *handshake_key, uint8_t *sig_out, size_t *sig_out_size);
CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_t *remote_key, size_t remote_key_size, uint8_t *handshake_key, uint8_t *remote_sig, size_t remote_sig_size);
+CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_set_local_key(ChiakiECDH *ecdh, const uint8_t *private_key, size_t private_key_size, const uint8_t *public_key, size_t public_key_size);
#ifdef __cplusplus
}
diff --git a/lib/src/ecdh.c b/lib/src/ecdh.c
index 715dec1..481d124 100644
--- a/lib/src/ecdh.c
+++ b/lib/src/ecdh.c
@@ -52,6 +52,46 @@ CHIAKI_EXPORT void chiaki_ecdh_fini(ChiakiECDH *ecdh)
}
+CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_set_local_key(ChiakiECDH *ecdh, const uint8_t *private_key, size_t private_key_size, const uint8_t *public_key, size_t public_key_size)
+{
+ ChiakiErrorCode err = CHIAKI_ERR_SUCCESS;
+
+ BIGNUM *private_key_bn = BN_bin2bn(private_key, (int)private_key_size, NULL);
+ if(!private_key_bn)
+ return CHIAKI_ERR_UNKNOWN;
+
+ EC_POINT *public_key_point = EC_POINT_new(ecdh->group);
+ if(!public_key_point)
+ {
+ err = CHIAKI_ERR_UNKNOWN;
+ goto error_priv;
+ }
+
+ if(!EC_POINT_oct2point(ecdh->group, public_key_point, public_key, public_key_size, NULL))
+ {
+ err = CHIAKI_ERR_UNKNOWN;
+ goto error_pub;
+ }
+
+ if(!EC_KEY_set_private_key(ecdh->key_local, private_key_bn))
+ {
+ err = CHIAKI_ERR_UNKNOWN;
+ goto error_pub;
+ }
+
+ if(!EC_KEY_set_public_key(ecdh->key_local, public_key_point))
+ {
+ err = CHIAKI_ERR_UNKNOWN;
+ goto error_pub;
+ }
+
+error_pub:
+ EC_POINT_free(public_key_point);
+error_priv:
+ BN_free(private_key_bn);
+ return err;
+}
+
CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_get_local_pub_key(ChiakiECDH *ecdh, uint8_t *key_out, size_t *key_out_size, uint8_t *handshake_key, uint8_t *sig_out, size_t *sig_out_size)
{
const EC_POINT *point = EC_KEY_get0_public_key(ecdh->key_local);
@@ -65,6 +105,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_get_local_pub_key(ChiakiECDH *ecdh, ui
if(!HMAC(EVP_sha256(), handshake_key, CHIAKI_HANDSHAKE_KEY_SIZE, key_out, *key_out_size, sig_out, (unsigned int *)sig_out_size))
return CHIAKI_ERR_UNKNOWN;
+ // TODO: set *sig_out_size?
+
return CHIAKI_ERR_SUCCESS;
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 2247a30..445d9a4 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -5,7 +5,8 @@ target_include_directories(munit PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/munit")
add_executable(chiaki-unit
main.c
http.c
- rpcrypt.c)
+ rpcrypt.c
+ gkcrypt.c)
target_link_libraries(chiaki-unit chiaki-lib munit)
diff --git a/test/gkcrypt.c b/test/gkcrypt.c
new file mode 100644
index 0000000..0e6fce1
--- /dev/null
+++ b/test/gkcrypt.c
@@ -0,0 +1,60 @@
+/*
+ * This file is part of Chiaki.
+ *
+ * Chiaki is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chiaki is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Chiaki. If not, see .
+ */
+
+#include
+
+#include
+
+
+static MunitResult test_ecdh(const MunitParameter params[], void *user)
+{
+ static const uint8_t handshake_key[] = { 0xfc, 0x5d, 0x4b, 0xa0, 0x3a, 0x35, 0x3a, 0xbb, 0x6a, 0x7f, 0xac, 0x79, 0x1b, 0x17, 0xbb, 0x34 };
+ static const uint8_t local_private_key[] = { 0x16, 0xe7, 0x5d, 0xcb, 0xda, 0x98, 0x55, 0xfb, 0x6b, 0xef, 0xdd, 0x8a, 0xa5, 0xf1, 0x6e, 0x7f, 0x46, 0xfd, 0xe1, 0xd2, 0x27, 0x97, 0x3, 0x60, 0x18, 0x72, 0xd8, 0x4b, 0x15, 0x38, 0xd9, 0x0 };
+ static const uint8_t local_public_key[] = { 0x4, 0xf4, 0xa, 0xf1, 0x35, 0xa4, 0x88, 0x94, 0x36, 0xce, 0xe5, 0x2b, 0x5c, 0x73, 0xa3, 0x3e, 0xc5, 0xad, 0xb, 0xe0, 0x95, 0x2f, 0x57, 0xf4, 0xf0, 0xed, 0xc, 0x80, 0xb0, 0xbe, 0xda, 0x7c, 0xa6, 0x43, 0x78, 0x93, 0x93, 0xa5, 0x94, 0x7e, 0x9f, 0xaa, 0x3f, 0x67, 0x95, 0xc9, 0xaa, 0x9, 0xa9, 0x63, 0x25, 0xdf, 0xe8, 0x50, 0xbf, 0xc3, 0xf1, 0xdb, 0x62, 0xa5, 0xa, 0xbf, 0xb0, 0xff, 0xf7 };
+ static const uint8_t local_public_key_sig[] = { 0x99, 0xb5, 0xcb, 0xb5, 0x37, 0x18, 0xb, 0xfc, 0x55, 0xda, 0x43, 0x7f, 0x44, 0x76, 0xa8, 0x17, 0xc9, 0x37, 0xfe, 0x56, 0x1b, 0x8a, 0xbe, 0xc, 0x41, 0x12, 0xab, 0x71, 0xf5, 0xa6, 0x8d, 0x29 };
+ static const uint8_t remote_public_key[] = { 0x4, 0xdf, 0xef, 0x8, 0xbb, 0xa8, 0x56, 0xf2, 0xb4, 0x4b, 0x8a, 0xe, 0x4f, 0x44, 0x20, 0x3f, 0x8e, 0x49, 0x3f, 0xee, 0xd4, 0x3c, 0xe9, 0x3a, 0xfe, 0x5c, 0x64, 0x67, 0x77, 0x20, 0x15, 0x7c, 0x59, 0x10, 0x15, 0x67, 0x94, 0xae, 0x5f, 0x2, 0x4a, 0xad, 0xc, 0xce, 0xfa, 0x14, 0x15, 0xa, 0xab, 0xee, 0x8, 0xb, 0x14, 0x12, 0x76, 0xea, 0x3e, 0xc0, 0xd5, 0x65, 0xf4, 0x68, 0x77, 0xa3, 0xca };
+ static const uint8_t remote_public_key_sig[] = { 0x13, 0xc5, 0x89, 0xe2, 0x3b, 0x72, 0x85, 0x24, 0xa9, 0x9f, 0x96, 0x80, 0x3, 0xa1, 0x81, 0x30, 0x59, 0x68, 0xf1, 0xbb, 0xb6, 0x4d, 0xc4, 0xa7, 0x6c, 0xce, 0xf6, 0x79, 0x4c, 0xeb, 0x2d, 0x98 };
+ static const uint8_t secret[] = { 0xb8, 0x1c, 0x61, 0x46, 0xe7, 0x49, 0x73, 0x8c, 0x96, 0x30, 0xca, 0x13, 0xff, 0x71, 0xe5, 0x9b, 0x3b, 0xf9, 0x41, 0x98, 0xd4, 0x67, 0xa5, 0xa2, 0xbc, 0x78, 0x4, 0x92, 0x81, 0x43, 0xec, 0x1d };
+
+ ChiakiECDH ecdh;
+ ChiakiErrorCode err = chiaki_ecdh_init(&ecdh);
+ if(err != CHIAKI_ERR_SUCCESS)
+ return MUNIT_ERROR;
+
+ err = chiaki_ecdh_set_local_key(&ecdh, local_private_key, sizeof(local_private_key), local_public_key, sizeof(local_public_key));
+ if(err != CHIAKI_ERR_SUCCESS)
+ return MUNIT_ERROR;
+
+ //uint8_t local_public_key_result[128];
+ //uint8_t local_public_key_sig_result[32];
+ //chiaki_ecdh_get_local_pub_key()
+
+ return MUNIT_OK;
+}
+
+
+MunitTest tests_gkcrypt[] = {
+ {
+ "/ecdh",
+ test_ecdh,
+ NULL,
+ NULL,
+ MUNIT_TEST_OPTION_NONE,
+ NULL
+ },
+ { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
+};
\ No newline at end of file
diff --git a/test/main.c b/test/main.c
index 1639126..a2a68a2 100644
--- a/test/main.c
+++ b/test/main.c
@@ -19,6 +19,7 @@
extern MunitTest tests_http[];
extern MunitTest tests_rpcrypt[];
+extern MunitTest tests_gkcrypt[];
static MunitSuite suites[] = {
{
@@ -35,6 +36,13 @@ static MunitSuite suites[] = {
1,
MUNIT_SUITE_OPTION_NONE
},
+ {
+ "/gkcrypt",
+ tests_gkcrypt,
+ NULL,
+ 1,
+ MUNIT_SUITE_OPTION_NONE
+ },
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE }
};