diff --git a/lib/include/chiaki/ecdh.h b/lib/include/chiaki/ecdh.h index b96907e..440fcb8 100644 --- a/lib/include/chiaki/ecdh.h +++ b/lib/include/chiaki/ecdh.h @@ -27,6 +27,8 @@ extern "C" { #endif +#define CHIAKI_ECDH_SECRET_SIZE 32 + typedef struct chiaki_ecdh_t { struct ec_group_st *group; @@ -36,7 +38,7 @@ typedef struct chiaki_ecdh_t 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, const uint8_t *handshake_key, uint8_t *sig_out, size_t *sig_out_size); -CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_t *secret_out, size_t *secret_out_size, const uint8_t *remote_key, size_t remote_key_size, const uint8_t *handshake_key, const uint8_t *remote_sig, size_t remote_sig_size); +CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_t *secret_out, const uint8_t *remote_key, size_t remote_key_size, const uint8_t *handshake_key, const 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 ae84c23..786e3c7 100644 --- a/lib/src/ecdh.c +++ b/lib/src/ecdh.c @@ -108,7 +108,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_get_local_pub_key(ChiakiECDH *ecdh, ui return CHIAKI_ERR_SUCCESS; } -CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_t *secret_out, size_t *secret_out_size, const uint8_t *remote_key, size_t remote_key_size, const uint8_t *handshake_key, const uint8_t *remote_sig, size_t remote_sig_size) +CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_t *secret_out, const uint8_t *remote_key, size_t remote_key_size, const uint8_t *handshake_key, const uint8_t *remote_sig, size_t remote_sig_size) { EC_POINT *remote_public_key = EC_POINT_new(ecdh->group); if(!remote_public_key) @@ -120,14 +120,12 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_ return CHIAKI_ERR_UNKNOWN; } - int r = ECDH_compute_key(secret_out, *secret_out_size, remote_public_key, ecdh->key_local, NULL); + int r = ECDH_compute_key(secret_out, CHIAKI_ECDH_SECRET_SIZE, remote_public_key, ecdh->key_local, NULL); EC_POINT_free(remote_public_key); - if(r <= 0) + if(r != CHIAKI_ECDH_SECRET_SIZE) return CHIAKI_ERR_UNKNOWN; - *secret_out_size = (size_t)r; - return CHIAKI_ERR_SUCCESS; } \ No newline at end of file diff --git a/lib/src/nagare.c b/lib/src/nagare.c index c2067db..94ac8cf 100644 --- a/lib/src/nagare.c +++ b/lib/src/nagare.c @@ -200,10 +200,12 @@ static void nagare_takion_data_expect_bang(ChiakiNagare *nagare, uint8_t *buf, s CHIAKI_LOGI(nagare->log, "Nagare bang looks good so far\n"); -// chiaki_ecdh_derive_secret(&nagare->session->ecdh, -// ecdh_pub_key_buf.buf, ecdh_pub_key_buf.size, -// nagare->session->handshake_key, -// ecdh_sig_buf.buf, ecdh_sig_buf.size); + uint8_t secret[CHIAKI_ECDH_SECRET_SIZE]; + chiaki_ecdh_derive_secret(&nagare->session->ecdh, + secret, + ecdh_pub_key_buf.buf, ecdh_pub_key_buf.size, + nagare->session->handshake_key, + ecdh_sig_buf.buf, ecdh_sig_buf.size); error: chiaki_mirai_signal(&nagare->bang_mirai, true); diff --git a/test/gkcrypt.c b/test/gkcrypt.c index 30a9d6f..57751ce 100644 --- a/test/gkcrypt.c +++ b/test/gkcrypt.c @@ -53,13 +53,12 @@ static MunitResult test_ecdh(const MunitParameter params[], void *user) munit_assert_memory_equal(sizeof(local_public_key_sig), local_public_key_sig_result, local_public_key_sig); uint8_t secret_result[128]; - size_t secret_result_size = sizeof(secret_result); - chiaki_ecdh_derive_secret(&ecdh, secret_result, &secret_result_size, + chiaki_ecdh_derive_secret(&ecdh, secret_result, remote_public_key, sizeof(remote_public_key), handshake_key, remote_public_key_sig, sizeof(remote_public_key_sig)); - munit_assert_size(secret_result_size, ==, sizeof(secret)); + munit_assert_size(CHIAKI_ECDH_SECRET_SIZE, ==, sizeof(secret)); munit_assert_memory_equal(sizeof(secret), secret_result, secret); chiaki_ecdh_fini(&ecdh);