diff --git a/lib/include/chiaki/ecdh.h b/lib/include/chiaki/ecdh.h index af9dc36..01e5c93 100644 --- a/lib/include/chiaki/ecdh.h +++ b/lib/include/chiaki/ecdh.h @@ -36,6 +36,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, 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); #ifdef __cplusplus } diff --git a/lib/src/ecdh.c b/lib/src/ecdh.c index 9aca9ea..715dec1 100644 --- a/lib/src/ecdh.c +++ b/lib/src/ecdh.c @@ -65,5 +65,46 @@ 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; + return CHIAKI_ERR_SUCCESS; +} + +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) +{ + EC_POINT *point = EC_POINT_new(ecdh->group); + if(!point) + return CHIAKI_ERR_UNKNOWN; + + if(!EC_POINT_oct2point(ecdh->group, point, remote_key, remote_key_size, NULL)) + { + EC_POINT_free(point); + return CHIAKI_ERR_UNKNOWN; + } + + EC_KEY *remote_ec_key = EC_KEY_new(); + if(!remote_ec_key) + { + EC_POINT_free(point); + return CHIAKI_ERR_UNKNOWN; + } + + if(!EC_KEY_set_group(remote_ec_key, ecdh->group)) + { + EC_KEY_free(remote_ec_key); + EC_POINT_free(point); + return CHIAKI_ERR_UNKNOWN; + } + + if(!EC_KEY_set_public_key(remote_ec_key, point)) + { + EC_KEY_free(remote_ec_key); + EC_POINT_free(point); + return CHIAKI_ERR_UNKNOWN; + } + EC_POINT_free(point); + + // TODO: do derivation + + EC_KEY_free(remote_ec_key); + return CHIAKI_ERR_SUCCESS; } \ No newline at end of file diff --git a/lib/src/nagare.c b/lib/src/nagare.c index a0b39e2..4e7a037 100644 --- a/lib/src/nagare.c +++ b/lib/src/nagare.c @@ -200,6 +200,11 @@ 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); + error: chiaki_mirai_signal(&nagare->bang_mirai, true); }