Change structure of keypair_t

This commit is contained in:
Night Kaly 2024-09-20 17:04:16 +01:00
parent 47e50fcf8e
commit 2801fad655
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
2 changed files with 61 additions and 58 deletions

View file

@ -7,33 +7,35 @@
#define TIME_SIZE sizeof(time_t)
#define SIGN_SIZE crypto_sign_BYTES
#define PK_BIN_SIZE crypto_kx_PUBLICKEYBYTES
#define SK_BIN_SIZE crypto_sign_SECRETKEYBYTES
#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_BIN_SIZE + METADATA_SIZE + SIGN_SIZE
#define SK_SIZE SK_BIN_SIZE + METADATA_SIZE + SIGN_SIZE
#define SHARED_SIZE crypto_kx_SESSIONKEYBYTES
#define PK_SIZE PK_RAW_SIZE + METADATA_SIZE + SIGN_SIZE
#define SK_SIZE SK_RAW_SIZE + METADATA_SIZE + SIGN_SIZE
#define SHARED_KEY_SIZE crypto_kx_SESSIONKEYBYTES
typedef struct public_key {
uint8_t bin[PK_BIN_SIZE];
uint8_t raw[PK_RAW_SIZE];
uint8_t username[MAX_NAME];
time_t creation;
uint8_t signature[SIGN_SIZE];
uint8_t full[PK_SIZE];
} public_key;
typedef struct secret_key {
uint8_t bin[SK_BIN_SIZE];
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 key_pair {
typedef struct keypair_t {
public_key pk;
secret_key sk;
} key_pair;
} keypair_t;
key_pair *create_key_pair(char *username);
key_pair *get_key_pair(char *username);
keypair_t *create_keypair(char *username);
keypair_t *get_keypair(char *username);
#endif

View file

@ -2,96 +2,97 @@
#include "key.h"
#include "util.h"
key_pair *create_key_pair(char *username)
keypair_t *create_keypair(char *username)
{
uint8_t cl_pk_bin[PK_BIN_SIZE], cl_sk_bin[SK_BIN_SIZE];
crypto_sign_keypair(cl_pk_bin, cl_sk_bin);
char pk_path[PATH_MAX], sk_path[PATH_MAX];
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);
/* USE DB INSTEAD OF FILES */
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+");
uint8_t pk_content[PK_SIZE], sk_content[SK_SIZE], metadata[METADATA_SIZE];
time_t current_time = time(NULL);
uint8_t *username_padded = memalloc(MAX_NAME * sizeof(uint8_t));
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);
uint8_t *hash = memalloc(HASH_SIZE * sizeof(uint8_t));
uint8_t *sign = memalloc(SIGN_SIZE * sizeof(uint8_t));
crypto_generichash(hash, HASH_SIZE, metadata, METADATA_SIZE, NULL, 0);
crypto_sign_detached(sign, NULL, hash, HASH_SIZE, cl_sk_bin);
memcpy(pk_content, cl_pk_bin, PK_BIN_SIZE);
memcpy(pk_content + PK_BIN_SIZE, metadata, METADATA_SIZE);
memcpy(pk_content + PK_BIN_SIZE + METADATA_SIZE, sign, SIGN_SIZE);
memcpy(sk_content, cl_sk_bin, SK_BIN_SIZE);
memcpy(sk_content + SK_BIN_SIZE, metadata, METADATA_SIZE);
memcpy(sk_content + SK_BIN_SIZE + METADATA_SIZE, sign, SIGN_SIZE);
free(hash);
crypto_sign_detached(sign, NULL, hash, HASH_SIZE, sk_raw);
fwrite(pk_content, 1, PK_SIZE, pkf);
fwrite(sk_content, 1, SK_SIZE, skf);
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);
key_pair *kp = memalloc(sizeof(key_pair));
memcpy(kp->pk.bin, cl_pk_bin, PK_BIN_SIZE);
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.bin, cl_sk_bin, SK_BIN_SIZE);
memcpy(kp->sk.username, username_padded, MAX_NAME);
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);
free(username_padded);
free(sign);
return kp;
}
key_pair *get_key_pair(char *username)
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_content[PK_SIZE], sk_content[SK_SIZE];
fread(pk_content, 1, PK_SIZE, pkf);
fread(sk_content, 1, SK_SIZE, skf);
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);
key_pair *kp = memalloc(sizeof(key_pair));
keypair_t *kp = memalloc(sizeof(keypair_t));
memcpy(kp->pk.bin, pk_content, PK_BIN_SIZE);
memcpy(kp->pk.username, pk_content + PK_BIN_SIZE, MAX_NAME);
memcpy(&kp->pk.creation, pk_content + PK_BIN_SIZE + MAX_NAME, TIME_SIZE);
memcpy(kp->pk.signature, pk_content + PK_BIN_SIZE + MAX_NAME + TIME_SIZE, SIGN_SIZE);
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.bin, sk_content, SK_BIN_SIZE);
memcpy(kp->sk.username, sk_content + SK_BIN_SIZE, MAX_NAME);
memcpy(&kp->sk.creation, sk_content + SK_BIN_SIZE + MAX_NAME, TIME_SIZE);
memcpy(kp->sk.signature, sk_content + SK_BIN_SIZE + MAX_NAME + TIME_SIZE, SIGN_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;
}