diff --git a/src/crypt.c b/src/crypt.c index 88594b3..ddd4ec0 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -60,6 +60,19 @@ void crypt_key_export_secret(crypt_key_t *k, char hex[CRYPT_SECKEY_HEXSIZE]) { sodium_bin2hex(hex+(crypto_kx_SECRETKEYBYTES * 2), crypto_sign_SECRETKEYBYTES * 2 + 1, k->sign_sec, crypto_sign_SECRETKEYBYTES); } +void crypt_store_public_key(crypt_key_t *k, FILE *out_file) { + char phex[CRYPT_PUBKEY_HEXSIZE+1]; + crypt_key_export_public(k, phex); + int o = fwrite(phex, sizeof(phex[0]), CRYPT_PUBKEY_HEXSIZE, out_file); + printf("%li %i | %i\n", sizeof(phex[0]), CRYPT_PUBKEY_HEXSIZE, o); +} + +void crypt_store_secret_key(crypt_key_t *k, FILE *out_file) { + char shex[CRYPT_SECKEY_HEXSIZE+1]; + crypt_key_export_secret(k, shex); + fwrite(shex, sizeof(shex[0]), CRYPT_SECKEY_HEXSIZE, out_file); +} + int crypt_session_init(crypt_session_t *s, crypt_key_t *own, crypt_key_t *remote, bool is_client) { if (is_client) { if (crypto_kx_client_session_keys(s->rx, s->tx, own->kx_pub, own->kx_sec, remote->kx_pub) != 0) { diff --git a/src/crypt.h b/src/crypt.h index e916c46..9a483ec 100644 --- a/src/crypt.h +++ b/src/crypt.h @@ -1,12 +1,13 @@ #ifndef _CRYPT_H_ #define _CRYPT_H_ +#include #include #include -#define CRYPT_PUBKEY_HEXSIZE (crypto_kx_PUBLICKEYBYTES + crypto_sign_PUBLICKEYBYTES) * 2 + 1 -#define CRYPT_SECKEY_HEXSIZE (crypto_kx_SECRETKEYBYTES + crypto_sign_SECRETKEYBYTES) * 2 + 1 +#define CRYPT_PUBKEY_HEXSIZE (crypto_kx_PUBLICKEYBYTES + crypto_sign_PUBLICKEYBYTES) * 2 +#define CRYPT_SECKEY_HEXSIZE (crypto_kx_SECRETKEYBYTES + crypto_sign_SECRETKEYBYTES) * 2 typedef struct crypt_key_t { unsigned char kx_pub[crypto_kx_PUBLICKEYBYTES]; @@ -23,6 +24,9 @@ int crypt_key_from_hex_public(crypt_key_t *k, char phex[CRYPT_PUBKEY_HEXSIZE]); void crypt_key_export_public(crypt_key_t *k, char hex[CRYPT_PUBKEY_HEXSIZE]); void crypt_key_export_secret(crypt_key_t *k, char hex[CRYPT_SECKEY_HEXSIZE]); +void crypt_store_public_key(crypt_key_t *k, FILE *out_file); +void crypt_store_secret_key(crypt_key_t *k, FILE *out_file); + typedef struct crypt_session_t { unsigned char rx[crypto_kx_SESSIONKEYBYTES]; unsigned char tx[crypto_kx_SESSIONKEYBYTES]; diff --git a/src/main.c b/src/main.c index 168bcaa..a834585 100644 --- a/src/main.c +++ b/src/main.c @@ -20,22 +20,21 @@ int main(int argc, char **argv) { return -1; } - crypt_key_t own, remote; + crypt_key_t own; if (crypt_key_gen(&own) < 0) { fprintf(stderr, "Failed to generate the keys."); return -1; } - if (crypt_key_gen(&remote) < 0) { - fprintf(stderr, "Failed to generate the keys."); - return -1; - } + FILE *fp = fopen("own_pub_key", "w"); + FILE *fs = fopen("own_sec_key", "w"); - crypt_session_t sess, sess_r; + crypt_store_public_key(&own, fp); + crypt_store_secret_key(&own, fs); - crypt_session_init(&sess, &own, &remote, true); - crypt_session_init(&sess_r, &remote, &own, false); + fclose(fp); + fclose(fs); // audio_t aud;