1
0
tetatet/src/crypt.c

92 lines
3.3 KiB
C
Raw Normal View History

2024-03-21 17:50:55 +04:00
#include "crypt.h"
#include <stdlib.h>
int crypt_key_gen(crypt_key_t *k) {
if (crypto_kx_keypair(k->kx_pub, k->kx_sec) < 0) {
2024-03-21 17:50:55 +04:00
return -1;
}
if (crypto_sign_keypair(k->sign_pub, k->sign_sec) < 0) {
2024-03-21 17:50:55 +04:00
return -1;
}
k->hasSecKey = true;
return 0;
}
int crypt_key_from_hex(crypt_key_t *k, char phex[CRYPT_PUBKEY_HEXSIZE], char shex[CRYPT_SECKEY_HEXSIZE]) {
int res = 0;
res = sodium_hex2bin(k->kx_pub, crypto_kx_PUBLICKEYBYTES, phex, crypto_kx_PUBLICKEYBYTES * 2, NULL, NULL, NULL);
2024-03-21 17:50:55 +04:00
if (res < 0)
return -1;
res = sodium_hex2bin(k->kx_sec, crypto_kx_SECRETKEYBYTES, shex, crypto_kx_SECRETKEYBYTES * 2, NULL, NULL, NULL);
2024-03-21 17:50:55 +04:00
if (res < 0)
return -1;
res = sodium_hex2bin(k->sign_pub, crypto_sign_PUBLICKEYBYTES, phex+(crypto_kx_PUBLICKEYBYTES * 2), crypto_sign_PUBLICKEYBYTES * 2, NULL, NULL, NULL);
2024-03-21 17:50:55 +04:00
if (res < 0)
return -1;
res = sodium_hex2bin(k->sign_sec, crypto_sign_SECRETKEYBYTES, shex+(crypto_kx_SECRETKEYBYTES * 2), crypto_sign_SECRETKEYBYTES * 2, NULL, NULL, NULL);
2024-03-21 17:50:55 +04:00
if (res < 0)
return -1;
k->hasSecKey = true;
return 0;
}
int crypt_key_from_hex_public(crypt_key_t *k, char phex[CRYPT_PUBKEY_HEXSIZE]) {
int res = 0;
res = sodium_hex2bin(k->kx_pub, crypto_kx_PUBLICKEYBYTES, phex, crypto_kx_PUBLICKEYBYTES * 2, NULL, NULL, NULL);
2024-03-21 17:50:55 +04:00
if (res < 0)
return -1;
res = sodium_hex2bin(k->sign_pub, crypto_sign_PUBLICKEYBYTES, phex+(crypto_kx_PUBLICKEYBYTES * 2), crypto_sign_PUBLICKEYBYTES * 2, NULL, NULL, NULL);
2024-03-21 17:50:55 +04:00
if (res < 0)
return -1;
k->hasSecKey = false;
return 0;
}
void crypt_key_export_public(crypt_key_t *k, char hex[CRYPT_PUBKEY_HEXSIZE]) {
sodium_bin2hex(hex, crypto_kx_PUBLICKEYBYTES * 2 + 1, k->kx_pub, crypto_kx_PUBLICKEYBYTES);
sodium_bin2hex(hex+(crypto_kx_PUBLICKEYBYTES * 2), crypto_sign_PUBLICKEYBYTES * 2 + 1, k->sign_pub, crypto_sign_PUBLICKEYBYTES);
2024-03-21 17:50:55 +04:00
}
void crypt_key_export_secret(crypt_key_t *k, char hex[CRYPT_SECKEY_HEXSIZE]) {
sodium_bin2hex(hex, crypto_kx_SECRETKEYBYTES * 2 + 1, k->kx_sec, crypto_kx_SECRETKEYBYTES);
sodium_bin2hex(hex+(crypto_kx_SECRETKEYBYTES * 2), crypto_sign_SECRETKEYBYTES * 2 + 1, k->sign_sec, crypto_sign_SECRETKEYBYTES);
2024-03-21 17:50:55 +04:00
}
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) {
2024-03-21 17:50:55 +04:00
fprintf(stderr, "Failed to instantiate a client session.\n");
return -1;
}
} else {
if (crypto_kx_server_session_keys(s->rx, s->tx, own->kx_pub, own->kx_sec, remote->kx_pub) != 0) {
2024-03-21 17:50:55 +04:00
fprintf(stderr, "Failed to instantiate a server session.\n");
return -1;
}
}
s->remote_key = remote;
2024-03-21 17:50:55 +04:00
return 0;
}