mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-19 21:13:12 -07:00
Create GKCrypts
This commit is contained in:
parent
1588e81b9e
commit
5f654dde02
4 changed files with 60 additions and 1 deletions
|
@ -24,6 +24,7 @@ extern "C" {
|
||||||
|
|
||||||
#define CHIAKI_EXPORT
|
#define CHIAKI_EXPORT
|
||||||
|
|
||||||
|
#define CHIAKI_NEW(t) (malloc(sizeof(t)))
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
|
|
|
@ -46,6 +46,28 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcry
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_decrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_decrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size);
|
||||||
static inline ChiakiErrorCode chiaki_gkcrypt_encrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size) { return chiaki_gkcrypt_decrypt(gkcrypt, key_pos, buf, buf_size); }
|
static inline ChiakiErrorCode chiaki_gkcrypt_encrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size) { return chiaki_gkcrypt_decrypt(gkcrypt, key_pos, buf, buf_size); }
|
||||||
|
|
||||||
|
static inline ChiakiGKCrypt *chiaki_gkcrypt_new(ChiakiLog *log, size_t key_buf_blocks, uint8_t index, const uint8_t *handshake_key, const uint8_t *ecdh_secret)
|
||||||
|
{
|
||||||
|
ChiakiGKCrypt *gkcrypt = CHIAKI_NEW(ChiakiGKCrypt);
|
||||||
|
if(!gkcrypt)
|
||||||
|
return NULL;
|
||||||
|
ChiakiErrorCode err = chiaki_gkcrypt_init(gkcrypt, log, key_buf_blocks, index, handshake_key, ecdh_secret);
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
{
|
||||||
|
free(gkcrypt);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return gkcrypt;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void chiaki_gkcrypt_free(ChiakiGKCrypt *gkcrypt)
|
||||||
|
{
|
||||||
|
if(!gkcrypt)
|
||||||
|
return;
|
||||||
|
chiaki_gkcrypt_fini(gkcrypt);
|
||||||
|
free(gkcrypt);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include "takion.h"
|
#include "takion.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ecdh.h"
|
#include "ecdh.h"
|
||||||
|
#include "gkcrypt.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
@ -35,7 +36,9 @@ typedef struct chiaki_nagare_t
|
||||||
ChiakiLog *log;
|
ChiakiLog *log;
|
||||||
ChiakiTakion takion;
|
ChiakiTakion takion;
|
||||||
ChiakiMirai bang_mirai;
|
ChiakiMirai bang_mirai;
|
||||||
uint8_t ecdh_secret[CHIAKI_ECDH_SECRET_SIZE];
|
uint8_t *ecdh_secret;
|
||||||
|
ChiakiGKCrypt *gkcrypt_a;
|
||||||
|
ChiakiGKCrypt *gkcrypt_b;
|
||||||
} ChiakiNagare;
|
} ChiakiNagare;
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(struct chiaki_session_t *session);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(struct chiaki_session_t *session);
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <takion.pb.h>
|
#include <takion.pb.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
|
@ -50,6 +51,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
|
||||||
nagare->session = session;
|
nagare->session = session;
|
||||||
nagare->log = &session->log;
|
nagare->log = &session->log;
|
||||||
|
|
||||||
|
nagare->ecdh_secret = NULL;
|
||||||
|
|
||||||
ChiakiErrorCode err = chiaki_mirai_init(&nagare->bang_mirai);
|
ChiakiErrorCode err = chiaki_mirai_init(&nagare->bang_mirai);
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
goto error_bang_mirai;
|
goto error_bang_mirai;
|
||||||
|
@ -106,6 +109,22 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
|
||||||
CHIAKI_LOGI(&session->log, "Nagare successfully received bang\n");
|
CHIAKI_LOGI(&session->log, "Nagare successfully received bang\n");
|
||||||
|
|
||||||
|
|
||||||
|
nagare->gkcrypt_a = chiaki_gkcrypt_new(&session->log, 0 /* TODO */, 2, session->handshake_key, nagare->ecdh_secret);
|
||||||
|
if(!nagare->gkcrypt_a)
|
||||||
|
{
|
||||||
|
CHIAKI_LOGE(&session->log, "Nagare failed to initialize GKCrypt with index 2\n");
|
||||||
|
goto error_takion;
|
||||||
|
}
|
||||||
|
nagare->gkcrypt_b = chiaki_gkcrypt_new(&session->log, 0 /* TODO */, 3, session->handshake_key, nagare->ecdh_secret);
|
||||||
|
if(!nagare->gkcrypt_b)
|
||||||
|
{
|
||||||
|
CHIAKI_LOGE(&session->log, "Nagare failed to initialize GKCrypt with index 3\n");
|
||||||
|
goto error_gkcrypt_a;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
|
||||||
CHIAKI_LOGI(&session->log, "Nagare is disconnecting\n");
|
CHIAKI_LOGI(&session->log, "Nagare is disconnecting\n");
|
||||||
|
@ -113,11 +132,15 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
|
||||||
nagare_send_disconnect(nagare);
|
nagare_send_disconnect(nagare);
|
||||||
|
|
||||||
err = CHIAKI_ERR_SUCCESS;
|
err = CHIAKI_ERR_SUCCESS;
|
||||||
|
chiaki_gkcrypt_free(nagare->gkcrypt_b);
|
||||||
|
error_gkcrypt_a:
|
||||||
|
chiaki_gkcrypt_free(nagare->gkcrypt_a);
|
||||||
error_takion:
|
error_takion:
|
||||||
chiaki_takion_close(&nagare->takion);
|
chiaki_takion_close(&nagare->takion);
|
||||||
CHIAKI_LOGI(&session->log, "Nagare closed takion\n");
|
CHIAKI_LOGI(&session->log, "Nagare closed takion\n");
|
||||||
error_bang_mirai:
|
error_bang_mirai:
|
||||||
chiaki_mirai_fini(&nagare->bang_mirai);
|
chiaki_mirai_fini(&nagare->bang_mirai);
|
||||||
|
free(nagare->ecdh_secret);
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
|
|
||||||
|
@ -200,6 +223,14 @@ static void nagare_takion_data_expect_bang(ChiakiNagare *nagare, uint8_t *buf, s
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(!nagare->ecdh_secret);
|
||||||
|
nagare->ecdh_secret = malloc(CHIAKI_ECDH_SECRET_SIZE);
|
||||||
|
if(!nagare->ecdh_secret)
|
||||||
|
{
|
||||||
|
CHIAKI_LOGE(nagare->log, "Nagare failed to alloc ECDH secret memory\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
ChiakiErrorCode err = chiaki_ecdh_derive_secret(&nagare->session->ecdh,
|
ChiakiErrorCode err = chiaki_ecdh_derive_secret(&nagare->session->ecdh,
|
||||||
nagare->ecdh_secret,
|
nagare->ecdh_secret,
|
||||||
ecdh_pub_key_buf.buf, ecdh_pub_key_buf.size,
|
ecdh_pub_key_buf.buf, ecdh_pub_key_buf.size,
|
||||||
|
@ -208,6 +239,8 @@ static void nagare_takion_data_expect_bang(ChiakiNagare *nagare, uint8_t *buf, s
|
||||||
|
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
{
|
{
|
||||||
|
free(nagare->ecdh_secret);
|
||||||
|
nagare->ecdh_secret = NULL;
|
||||||
CHIAKI_LOGE(nagare->log, "Nagare failed to derive secret from bang\n");
|
CHIAKI_LOGE(nagare->log, "Nagare failed to derive secret from bang\n");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue