1
0

crypt_store_*_key funcs was implemented. Also a test code in a main func comes along.

This commit is contained in:
Alexander Andreev 2024-03-22 04:16:43 +04:00
parent 026d8376e7
commit fb9e62fc8b
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
3 changed files with 26 additions and 10 deletions

View File

@ -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) {

View File

@ -1,12 +1,13 @@
#ifndef _CRYPT_H_
#define _CRYPT_H_
#include <stdio.h>
#include <stdbool.h>
#include <sodium.h>
#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];

View File

@ -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;