Move lib to src/lib and change secret key strcuture

This commit is contained in:
Night Kaly 2024-09-24 13:52:19 +01:00
parent 74597b6361
commit 4d3bb95e78
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
6 changed files with 97 additions and 115 deletions

View file

@ -9,9 +9,9 @@
#define SIGN_SIZE crypto_sign_BYTES
#define PK_RAW_SIZE crypto_kx_PUBLICKEYBYTES
#define SK_RAW_SIZE crypto_sign_SECRETKEYBYTES
#define METADATA_SIZE MAX_NAME + TIME_SIZE
#define PK_SIZE PK_RAW_SIZE + METADATA_SIZE + SIGN_SIZE
#define SK_SIZE SK_RAW_SIZE + METADATA_SIZE + SIGN_SIZE
#define PK_DATA_SIZE PK_RAW_SIZE + MAX_NAME + TIME_SIZE
#define PK_SIZE PK_DATA_SIZE + SIGN_SIZE /* Size with signature */
#define SK_SIZE SK_DATA_SIZE
#define SHARED_KEY_SIZE crypto_kx_SESSIONKEYBYTES
typedef struct public_key {
@ -22,17 +22,9 @@ typedef struct public_key {
uint8_t full[PK_SIZE];
} public_key;
typedef struct secret_key {
uint8_t raw[SK_RAW_SIZE];
uint8_t username[MAX_NAME];
time_t creation;
uint8_t signature[SIGN_SIZE];
uint8_t full[SK_SIZE];
} secret_key;
typedef struct keypair_t {
public_key pk;
secret_key sk;
uint8_t sk[SK_SIZE];
} keypair_t;
keypair_t *create_keypair(char *username);

View file

@ -1,98 +0,0 @@
#include "packet.h"
#include "key.h"
#include "util.h"
keypair_t *create_keypair(char *username)
{
uint8_t pk_raw[PK_RAW_SIZE], sk_raw[SK_RAW_SIZE], metadata[METADATA_SIZE],
username_padded[MAX_NAME], hash[HASH_SIZE], sign[SIGN_SIZE],
pk_full[PK_SIZE], sk_full[SK_SIZE];
crypto_sign_keypair(pk_raw, sk_raw);
time_t current_time = time(NULL);
strcpy(username_padded, username);
size_t length = strlen(username);
if (length < MAX_NAME) {
/* Pad with null characters up to max length */
memset(username_padded + length, 0, MAX_NAME - length);
} else {
error(0, "Username must be shorter than MAX_NAME");
return NULL;
}
memcpy(metadata, username_padded, MAX_NAME);
memcpy(metadata + MAX_NAME, &current_time, TIME_SIZE);
crypto_generichash(hash, HASH_SIZE, metadata, METADATA_SIZE, NULL, 0);
crypto_sign_detached(sign, NULL, hash, HASH_SIZE, sk_raw);
memcpy(pk_full, pk_raw, PK_RAW_SIZE);
memcpy(pk_full + PK_RAW_SIZE, metadata, METADATA_SIZE);
memcpy(pk_full + PK_RAW_SIZE + METADATA_SIZE, sign, SIGN_SIZE);
memcpy(sk_full, sk_raw, SK_RAW_SIZE);
memcpy(sk_full + SK_RAW_SIZE, metadata, METADATA_SIZE);
memcpy(sk_full + SK_RAW_SIZE + METADATA_SIZE, sign, SIGN_SIZE);
/* USE DB INSTEAD OF FILES */
char pk_path[PATH_MAX], sk_path[PATH_MAX];
sprintf(pk_path, "/home/night/%s_pk", username);
sprintf(sk_path, "/home/night/%s_sk", username);
FILE *pkf = fopen(pk_path, "w+");
FILE *skf = fopen(sk_path, "w+");
fwrite(pk_full, 1, PK_SIZE, pkf);
fwrite(sk_full, 1, SK_SIZE, skf);
fclose(pkf);
fclose(skf);
keypair_t *kp = memalloc(sizeof(keypair_t));
memcpy(kp->pk.raw, pk_raw, PK_RAW_SIZE);
memcpy(kp->pk.username, username_padded, MAX_NAME);
kp->pk.creation = current_time;
memcpy(kp->pk.signature, sign, SIGN_SIZE);
memcpy(kp->pk.full, pk_full, PK_SIZE);
memcpy(kp->sk.raw, sk_raw, SK_RAW_SIZE);
memcpy(kp->sk.username, username_padded, MAX_NAME);
kp->sk.creation = current_time;
memcpy(kp->sk.signature, sign, SIGN_SIZE);
memcpy(kp->sk.full, sk_full, PK_SIZE);
return kp;
}
keypair_t *get_keypair(char *username)
{
/* REPLACE WITH DB */
char pk_path[PATH_MAX], sk_path[PATH_MAX];
sprintf(pk_path, "/home/night/%s_pk", username);
sprintf(sk_path, "/home/night/%s_sk", username);
FILE *pkf = fopen(pk_path, "r");
FILE *skf = fopen(sk_path, "r");
if (!pkf || !skf) {
printf("Error opening key files.\n");
return NULL;
}
uint8_t pk_full[PK_SIZE], sk_full[SK_SIZE];
fread(pk_full, 1, PK_SIZE, pkf);
fread(sk_full, 1, SK_SIZE, skf);
fclose(pkf);
fclose(skf);
keypair_t *kp = memalloc(sizeof(keypair_t));
memcpy(kp->pk.raw, pk_full, PK_RAW_SIZE);
memcpy(kp->pk.username, pk_full + PK_RAW_SIZE, MAX_NAME);
memcpy(&kp->pk.creation, pk_full + PK_RAW_SIZE + MAX_NAME, TIME_SIZE);
memcpy(kp->pk.signature, pk_full + PK_RAW_SIZE + MAX_NAME + TIME_SIZE, SIGN_SIZE);
memcpy(kp->pk.full, pk_full, PK_SIZE);
memcpy(kp->sk.raw, sk_full, SK_RAW_SIZE);
memcpy(kp->sk.username, sk_full + SK_RAW_SIZE, MAX_NAME);
memcpy(&kp->sk.creation, sk_full + SK_RAW_SIZE + MAX_NAME, TIME_SIZE);
memcpy(kp->sk.signature, sk_full + SK_RAW_SIZE + MAX_NAME + TIME_SIZE, SIGN_SIZE);
memcpy(kp->sk.full, sk_full, SK_SIZE);
return kp;
}

90
src/lib/key.c Normal file
View file

@ -0,0 +1,90 @@
#include "packet.h"
#include "key.h"
#include "util.h"
keypair_t *create_keypair(char *username)
{
uint8_t pk_raw[PK_RAW_SIZE], sk[SK_SIZE], pk_data[PK_DATA_SIZE],
username_padded[MAX_NAME], pk_hash[HASH_SIZE], pk_sign[SIGN_SIZE],
pk[PK_SIZE];
crypto_sign_keypair(pk_raw, sk);
time_t current_time = time(NULL);
strcpy(username_padded, username);
size_t length = strlen(username);
if (length < MAX_NAME) {
/* Pad with null characters up to max length */
memset(username_padded + length, 0, MAX_NAME - length);
} else {
error(0, "Username must be shorter than MAX_NAME");
return NULL;
}
memcpy(public_key, pk_raw, PK_RAW_SIZE);
memcpy(public_key + PK_RAW_SIZE, username_padded, MAX_NAME);
memcpy(public_key + PK_RAW_SIZE + MAX_NAME, &current_time, TIME_SIZE);
crypto_generichash(pk_hash, HASH_SIZE, pk_data, PK_DATA_SIZE, NULL, 0);
crypto_sign_detached(pk_sign, NULL, pk_hash, HASH_SIZE, sk_raw);
memcpy(pk, pk_raw, PK_RAW_SIZE);
memcpy(pk + PK_RAW_SIZE, metadata, METADATA_SIZE);
memcpy(pk + PK_RAW_SIZE + METADATA_SIZE, sign, SIGN_SIZE);
memcpy(sk, sk_raw, SK_RAW_SIZE);
/* USE DB INSTEAD OF FILES */
char pk_path[PATH_MAX], sk_path[PATH_MAX];
sprintf(pk_path, "/home/night/%s_pk", username);
sprintf(sk_path, "/home/night/%s_sk", username);
FILE *pkf = fopen(pk_path, "w+");
FILE *skf = fopen(sk_path, "w+");
fwrite(pk, 1, PK_SIZE, pkf);
fwrite(sk, 1, SK_SIZE, skf);
fclose(pkf);
fclose(skf);
keypair_t *kp = memalloc(sizeof(keypair_t));
memcpy(kp->pk.raw, pk_raw, PK_RAW_SIZE);
memcpy(kp->pk.username, username_padded, MAX_NAME);
kp->pk.creation = current_time;
memcpy(kp->pk.signature, sign, SIGN_SIZE);
memcpy(kp->pk.full, pk, PK_SIZE);
memcpy(kp->sk, sk_raw, SK_SIZE);
return kp;
}
keypair_t *get_keypair(char *username)
{
/* REPLACE WITH DB */
char pk_path[PATH_MAX], sk_path[PATH_MAX];
sprintf(pk_path, "/home/night/%s_pk", username);
sprintf(sk_path, "/home/night/%s_sk", username);
FILE *pkf = fopen(pk_path, "r");
FILE *skf = fopen(sk_path, "r");
if (!pkf || !skf) {
printf("Error opening key files.\n");
return NULL;
}
uint8_t pk[PK_SIZE], sk[SK_SIZE];
fread(pk, 1, PK_SIZE, pkf);
fread(sk, 1, SK_SIZE, skf);
fclose(pkf);
fclose(skf);
keypair_t *kp = memalloc(sizeof(keypair_t));
memcpy(kp->pk.raw, pk, PK_RAW_SIZE);
memcpy(kp->pk.username, pk + PK_RAW_SIZE, MAX_NAME);
memcpy(&kp->pk.creation, pk + PK_RAW_SIZE + MAX_NAME, TIME_SIZE);
memcpy(kp->pk.signature, pk + PK_RAW_SIZE + MAX_NAME + TIME_SIZE, SIGN_SIZE);
memcpy(kp->pk.full, pk, PK_SIZE);
memcpy(kp->sk, sk, SK_SIZE);
return kp;
}

View file

@ -3,8 +3,6 @@
#include "util.h"
#include "server/server.h"
int debug;
/*
* Requires manually free packet data
* pkt: packet to fill data in (must be created via create_packet)
@ -209,17 +207,17 @@ void free_packet(packet_t *pkt)
/*
* Wrapper for recv_packet to verify packet
* Reads packet from fd, stores in pkt
* TODO: pkt is unncessary
*/
int verify_packet(packet_t *pkt, int fd)
int verify_packet(int fd)
{
packet pkt;
int status = recv_packet(pkt, fd, ZSM_TYP_MESSAGE);
if (status != ZSM_STA_SUCCESS) {
close(fd);
return status;
}
uint8_t from[MAX_NAME], to[MAX_NAME];
uint8_t from[MAX_NAME];
memcpy(from, pkt->data, MAX_NAME);
/* TODO: replace with db operations */