Compare commits
No commits in common. "d764002546419300d087994b4f1414daea4e235b" and "8b7aa98b70adf6baa34d5e9421704f89480a2629" have entirely different histories.
d764002546
...
8b7aa98b70
@ -29,13 +29,12 @@ extern "C" {
|
||||
#define WHY2_CHAT_ECC NID_secp521r1 //CURVE NAME
|
||||
|
||||
#define WHY2_CHAT_KEY_LOCATION WHY2_CONFIG_DIR "/keys" //KEYS LOCATION
|
||||
#define WHY2_CHAT_KEY "secp521r1.pem"
|
||||
#define WHY2_CHAT_PUB_KEY "pub"
|
||||
#define WHY2_CHAT_PRI_KEY "pri"
|
||||
|
||||
void why2_chat_init_keys(void); //INIT (POSSIBLY GENERATE) ECC KEYS
|
||||
void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE)
|
||||
|
||||
char *why2_chat_ecc_sign(char *message); //SIGN message WITH ECC KEY
|
||||
|
||||
char *why2_sha256(char *input); //HASH input USING SHA256 AND RETURN IN STRING
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -32,135 +32,100 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/ec.h>
|
||||
|
||||
EVP_PKEY *keypair = NULL; //KEYPAIR
|
||||
char *ecc_pub = NULL;
|
||||
char *ecc_pri = NULL;
|
||||
|
||||
//LOCAL
|
||||
char* base64_encode(char *message)
|
||||
void read_file(FILE *file, char **output)
|
||||
{
|
||||
//VARIABLES
|
||||
BIO *bio;
|
||||
BIO *b64;
|
||||
BUF_MEM *buffer_ptr;
|
||||
char* encoded_message;
|
||||
int buffer_size;
|
||||
char *buffer;
|
||||
|
||||
//INIT BIOs
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
bio = BIO_new(BIO_s_mem());
|
||||
bio = BIO_push(b64, bio);
|
||||
//GET LENGTH
|
||||
fseek(file, 0, SEEK_END);
|
||||
buffer_size = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
//ENCODE
|
||||
BIO_write(bio, message, strlen(message));
|
||||
BIO_flush(bio);
|
||||
BIO_get_mem_ptr(bio, &buffer_ptr);
|
||||
//READ
|
||||
buffer = why2_calloc(buffer_size + 1, sizeof(char));
|
||||
if (fread(buffer, buffer_size, 1, file) != 1) why2_die("Reading keyfile failed!");
|
||||
buffer[buffer_size] = '\0';
|
||||
|
||||
//COPY
|
||||
encoded_message = why2_malloc(buffer_ptr -> length + 1);
|
||||
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
|
||||
encoded_message[buffer_ptr -> length] = '\0';
|
||||
|
||||
//DEALLOCATION
|
||||
BIO_free_all(bio);
|
||||
|
||||
return encoded_message;
|
||||
}
|
||||
|
||||
char* base64_decode(char *encoded_message)
|
||||
{
|
||||
//VARIABLES
|
||||
BIO *bio;
|
||||
BIO *b64;
|
||||
size_t length = strlen(encoded_message);
|
||||
char* decoded_message = why2_malloc(length);
|
||||
|
||||
//INIT BIOs
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
bio = BIO_new_mem_buf(encoded_message, length);
|
||||
bio = BIO_push(b64, bio);
|
||||
|
||||
//NULL-TERM
|
||||
decoded_message[BIO_read(bio, decoded_message, length)] = '\0';
|
||||
|
||||
//DEALLOCATION
|
||||
BIO_free_all(bio);
|
||||
|
||||
return decoded_message;
|
||||
//ASSIGN OUTPUT
|
||||
*output = buffer;
|
||||
}
|
||||
|
||||
//GLOBAL
|
||||
void why2_chat_init_keys(void)
|
||||
{
|
||||
FILE *key; //KEY FILE
|
||||
//KEY FILES
|
||||
FILE *public;
|
||||
FILE *private;
|
||||
|
||||
char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME")); //GET PATH TO KEY DIR
|
||||
char *key_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_KEY) + 3); //ALLOCATE THE KEY PATH
|
||||
//GET PATH TO KEY DIR
|
||||
char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME"));
|
||||
|
||||
//GET THE ACTUAL KEY PATH
|
||||
sprintf(key_path, "%s/%s%c", path, WHY2_CHAT_KEY, '\0');
|
||||
//ALLOCATE THE KEY PATHS
|
||||
char *public_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_PUB_KEY) + 3);
|
||||
char *private_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_PRI_KEY) + 3);
|
||||
|
||||
//CHECK IF KEY EXIST
|
||||
if (access(path, R_OK) != 0) //NOT FOUND - CREATE IT
|
||||
//GET THE ACTUAL KEY PATHS
|
||||
sprintf(public_path, "%s/%s%c", path, WHY2_CHAT_PUB_KEY, '\0');
|
||||
sprintf(private_path, "%s/%s%c", path, WHY2_CHAT_PRI_KEY, '\0');
|
||||
|
||||
//CHECK IF KEYS EXIST
|
||||
if (access(path, R_OK) != 0)
|
||||
{
|
||||
mkdir(path, 0700);
|
||||
|
||||
//SOME USER OUTPUT
|
||||
printf("No ECC key found.\nGenerating...\n\n");
|
||||
printf("You are probably running WHY2-Chat for the first time now.\nGenerating ECC keys...\n");
|
||||
|
||||
//VARIABLES
|
||||
EVP_PKEY *pkey = NULL; //KEYPAIR
|
||||
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); //CREATE CTX
|
||||
|
||||
EVP_PKEY_keygen_init(ctx); //INIT KEYGEN
|
||||
|
||||
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, WHY2_CHAT_ECC); //SETUP ECC
|
||||
EVP_PKEY_keygen(ctx, &keypair); //GENERATE ECC KEYPAIR
|
||||
|
||||
//WRITE THE KEYS INTO KEY-FILE
|
||||
key = why2_fopen(key_path, "w+");
|
||||
PEM_write_PrivateKey(key, keypair, NULL, NULL, 0, NULL, NULL); //WRITE THE KEY
|
||||
EVP_PKEY_keygen(ctx, &pkey); //GENERATE ECC KEYPAIR
|
||||
|
||||
printf("Saving keys...\n");
|
||||
|
||||
//WRITE THE KEYS INTO KEY-FILES
|
||||
public = why2_fopen(public_path, "w+");
|
||||
private = why2_fopen(private_path, "w+");
|
||||
|
||||
PEM_write_PrivateKey(private, pkey, NULL, NULL, 0, NULL, NULL); //WRITE PRI KEY
|
||||
PEM_write_PUBKEY(public, pkey); //WRITE PUB KEY
|
||||
|
||||
//DEALLOCATION
|
||||
EVP_PKEY_CTX_free(ctx);
|
||||
EVP_PKEY_free(pkey);
|
||||
} else
|
||||
{
|
||||
key = why2_fopen(key_path, "r"); //OPEN KEY FILE
|
||||
keypair = PEM_read_PrivateKey(key, NULL, NULL, NULL); //LOAD KEYPAIR
|
||||
//OPEN FILES
|
||||
public = why2_fopen(public_path, "r");
|
||||
private = why2_fopen(private_path, "r");
|
||||
|
||||
//READ THE KEYS
|
||||
read_file(public, &ecc_pub);
|
||||
read_file(private, &ecc_pri);
|
||||
}
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(path);
|
||||
why2_deallocate(key_path);
|
||||
why2_deallocate(key);
|
||||
}
|
||||
|
||||
char *why2_chat_ecc_sign(char *message)
|
||||
{
|
||||
//VARIABLES
|
||||
EVP_MD_CTX *mdctx = NULL; //SIGNING CONTEXT
|
||||
size_t siglen;
|
||||
char *sig; //SIGNATURE
|
||||
char *encoded_sig; //FINAL (ENCODED) SIGNATURE
|
||||
|
||||
//INIT mdctx
|
||||
mdctx = EVP_MD_CTX_new();
|
||||
EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, keypair);
|
||||
|
||||
EVP_DigestSignUpdate(mdctx, message, strlen(message)); //UPDATE MESSAGE TO SIGN
|
||||
EVP_DigestSignFinal(mdctx, NULL, &siglen); //COUNT LENGTH
|
||||
|
||||
//GENERATE SIGNATURE
|
||||
sig = why2_malloc(siglen); //ALLOCATE SIGNATURE
|
||||
EVP_DigestSignFinal(mdctx, (unsigned char*) sig, &siglen);
|
||||
|
||||
encoded_sig = base64_encode(sig); //CONVERT sig TO BASE64
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(sig);
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
|
||||
return encoded_sig;
|
||||
why2_deallocate(public_path);
|
||||
why2_deallocate(private_path);
|
||||
why2_deallocate(public);
|
||||
why2_deallocate(private);
|
||||
}
|
||||
|
||||
void why2_chat_deallocate_keys(void)
|
||||
{
|
||||
//DEALLOCATE THE pkey
|
||||
EVP_PKEY_free(keypair);
|
||||
why2_deallocate(ecc_pub);
|
||||
why2_deallocate(ecc_pri);
|
||||
}
|
||||
|
||||
char *why2_sha256(char *input)
|
||||
|
Loading…
x
Reference in New Issue
Block a user