Compare commits
3 Commits
94969b5588
...
8b7aa98b70
Author | SHA1 | Date | |
---|---|---|---|
8b7aa98b70 | |||
eff50818ea | |||
b833b56aef |
2
Makefile
2
Makefile
@ -59,7 +59,7 @@ TEST_LOGGER=./src/logger/lib/test/main.c
|
|||||||
LIBS_LOGGER=$(LIB_CORE)
|
LIBS_LOGGER=$(LIB_CORE)
|
||||||
LIB_LOGGER=-l$(PROJECT_NAME)-logger
|
LIB_LOGGER=-l$(PROJECT_NAME)-logger
|
||||||
|
|
||||||
LIBS_LIB_CHAT=$(LIB_CORE) -lpthread -lgmp -lcrypto
|
LIBS_LIB_CHAT=$(LIB_CORE) -lpthread -lcrypto
|
||||||
LIB_CHAT=-l$(PROJECT_NAME)-chat
|
LIB_CHAT=-l$(PROJECT_NAME)-chat
|
||||||
LIB_CHAT_CONFIG=$(LIB_CHAT)-config
|
LIB_CHAT_CONFIG=$(LIB_CHAT)-config
|
||||||
LIBS_CHAT=$(LIB_CHAT) $(LIBS_LIB_CHAT) $(LIB_CHAT_CONFIG)
|
LIBS_CHAT=$(LIB_CHAT) $(LIBS_LIB_CHAT) $(LIB_CHAT_CONFIG)
|
||||||
|
@ -30,7 +30,7 @@ then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
COMMON="gcc make tmux curl"
|
COMMON="gcc make tmux curl"
|
||||||
ARCH_GENTOO_COMMON="$COMMON json-c libgit2 gmp openssl"
|
ARCH_GENTOO_COMMON="$COMMON json-c libgit2 openssl"
|
||||||
|
|
||||||
# Get COMMAND
|
# Get COMMAND
|
||||||
if [[ $DISTRO == "arch" ]]
|
if [[ $DISTRO == "arch" ]]
|
||||||
@ -38,7 +38,7 @@ then
|
|||||||
COMMAND="pacman -S --needed --noconfirm $ARCH_GENTOO_COMMON"
|
COMMAND="pacman -S --needed --noconfirm $ARCH_GENTOO_COMMON"
|
||||||
elif [[ $DISTRO == "debian" ]]
|
elif [[ $DISTRO == "debian" ]]
|
||||||
then
|
then
|
||||||
COMMAND="apt-get install -y $COMMON libjson-c-dev libcurl4-openssl-dev libgit2-dev libgmp-dev libssl-dev"
|
COMMAND="apt-get install -y $COMMON libjson-c-dev libcurl4-openssl-dev libgit2-dev libssl-dev"
|
||||||
elif [[ $DISTRO == "gentoo" ]]
|
elif [[ $DISTRO == "gentoo" ]]
|
||||||
then
|
then
|
||||||
COMMAND="emerge -vn $ARCH_GENTOO_COMMON"
|
COMMAND="emerge -vn $ARCH_GENTOO_COMMON"
|
||||||
|
@ -26,24 +26,15 @@ extern "C" {
|
|||||||
#include <why2/chat/config.h>
|
#include <why2/chat/config.h>
|
||||||
|
|
||||||
//MACROS
|
//MACROS
|
||||||
#define WHY2_CHAT_KEY_BITS 4096 //BITS..
|
#define WHY2_CHAT_ECC NID_secp521r1 //CURVE NAME
|
||||||
#define WHY2_CHAT_PRIME_ITERS 100 //NUMBER OF ITERATIONS WHEN CHECKING PRIME NUMBER
|
|
||||||
#define WHY2_CHAT_RSA_EXPONENT "H33" //DEFAULT e IN BASE WHY2_CHAT_KEY_BASE
|
|
||||||
|
|
||||||
#define WHY2_CHAT_KEY_LOCATION WHY2_CONFIG_DIR "/keys" //KEYS LOCATION
|
#define WHY2_CHAT_KEY_LOCATION WHY2_CONFIG_DIR "/keys" //KEYS LOCATION
|
||||||
#define WHY2_CHAT_PUB_KEY "pub"
|
#define WHY2_CHAT_PUB_KEY "pub"
|
||||||
#define WHY2_CHAT_PRI_KEY "pri"
|
#define WHY2_CHAT_PRI_KEY "pri"
|
||||||
#define WHY2_CHAT_KEY_BASE 62 //BASE IN THE GENERATED KEYS ARE STORED IN WHY2_CHAT_KEY_LOCATION
|
|
||||||
|
|
||||||
void why2_chat_init_keys(void); //INIT (POSSIBLY GENERATE) RSA KEYS
|
void why2_chat_init_keys(void); //INIT (POSSIBLY GENERATE) ECC KEYS
|
||||||
void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE)
|
void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE)
|
||||||
|
|
||||||
char *why2_get_chat_modulus(void); //GET THE RSA MODULUS
|
|
||||||
char *why2_get_chat_d(void); //GET THE RSA d
|
|
||||||
|
|
||||||
char *why2_chat_rsa_pub_encrypt(char *to_encrypt); //RSA ENCRYPT USING PUBLIC KEY
|
|
||||||
char *why2_chat_rsa_pri_decrypt(char *to_decrypt); //RSA DECRYPT USING PRIVATE KEY
|
|
||||||
|
|
||||||
char *why2_sha256(char *input); //HASH input USING SHA256 AND RETURN IN STRING
|
char *why2_sha256(char *input); //HASH input USING SHA256 AND RETURN IN STRING
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -23,40 +23,17 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/random.h>
|
|
||||||
|
|
||||||
#include <why2/memory.h>
|
#include <why2/memory.h>
|
||||||
#include <why2/misc.h>
|
#include <why2/misc.h>
|
||||||
|
|
||||||
#include <gmp.h>
|
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/pem.h>
|
||||||
|
#include <openssl/ec.h>
|
||||||
|
|
||||||
//DO NOT TOUCH THESE PLS :3
|
char *ecc_pub = NULL;
|
||||||
char *rsa_modulus = NULL; //THE RSA MODULUS
|
char *ecc_pri = NULL;
|
||||||
char *rsa_d = NULL; //THE RSA d
|
|
||||||
|
|
||||||
//LOCAL
|
|
||||||
void generate_prime(mpz_t x)
|
|
||||||
{
|
|
||||||
//RANDOM
|
|
||||||
gmp_randstate_t state;
|
|
||||||
gmp_randinit_default(state);
|
|
||||||
unsigned long random_buffer; //SEED
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
if (getrandom(&random_buffer, sizeof(unsigned long), GRND_NONBLOCK) == -1) why2_die("getrandom fn failed!");
|
|
||||||
|
|
||||||
//GENERATE RANDOM PRIME USING random_buffer SEED
|
|
||||||
gmp_randseed_ui(state, random_buffer);
|
|
||||||
mpz_urandomb(x, state, WHY2_CHAT_KEY_BITS);
|
|
||||||
mpz_nextprime(x, x);
|
|
||||||
} while (mpz_probab_prime_p(x, WHY2_CHAT_PRIME_ITERS) == 0); //CHECK FOR PRIME PROBABILITY
|
|
||||||
|
|
||||||
//DEALLOCATION
|
|
||||||
gmp_randclear(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
void read_file(FILE *file, char **output)
|
void read_file(FILE *file, char **output)
|
||||||
{
|
{
|
||||||
@ -78,35 +55,11 @@ void read_file(FILE *file, char **output)
|
|||||||
*output = buffer;
|
*output = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *exp_mod(char *to_exp, char *exponent)
|
|
||||||
{
|
|
||||||
//VARIABLES
|
|
||||||
char *output;
|
|
||||||
mpz_t m, c, n, e;
|
|
||||||
mpz_init(c);
|
|
||||||
|
|
||||||
//GET ALL STUFF
|
|
||||||
mpz_init_set_str(m, to_exp, 10);
|
|
||||||
mpz_init_set_str(n, why2_get_chat_modulus(), WHY2_CHAT_KEY_BASE);
|
|
||||||
mpz_init_set_str(e, exponent, WHY2_CHAT_KEY_BASE);
|
|
||||||
|
|
||||||
//ENCRYPT MESSAGE
|
|
||||||
mpz_powm(c, m, e, n);
|
|
||||||
|
|
||||||
output = why2_malloc(mpz_sizeinbase(c, 10) + 2); //ALLOCATE OUTPUT
|
|
||||||
mpz_get_str(output, 10, c); //GET OUTPUT
|
|
||||||
|
|
||||||
//DEALLOCATION
|
|
||||||
mpz_clears(m, c, n, e, NULL);
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
//GLOBAL
|
//GLOBAL
|
||||||
void why2_chat_init_keys(void)
|
void why2_chat_init_keys(void)
|
||||||
{
|
{
|
||||||
//KEY FILES
|
//KEY FILES
|
||||||
FILE *public; //TECHNICALLY, PUBLIC KEY CONTAINS ONLY THE MODULUS AND PRIVATE CONTAINS ONLY THE d
|
FILE *public;
|
||||||
FILE *private;
|
FILE *private;
|
||||||
|
|
||||||
//GET PATH TO KEY DIR
|
//GET PATH TO KEY DIR
|
||||||
@ -126,29 +79,17 @@ void why2_chat_init_keys(void)
|
|||||||
mkdir(path, 0700);
|
mkdir(path, 0700);
|
||||||
|
|
||||||
//SOME USER OUTPUT
|
//SOME USER OUTPUT
|
||||||
printf("You are probably running WHY2-Chat for the first time now.\nGenerating RSA keys...\n");
|
printf("You are probably running WHY2-Chat for the first time now.\nGenerating ECC keys...\n");
|
||||||
|
|
||||||
//VARIABLES
|
//VARIABLES
|
||||||
mpz_t p, q, e, d, n, phi_n, buffer_1, buffer_2;
|
EVP_PKEY *pkey = NULL; //KEYPAIR
|
||||||
mpz_inits(p, q, e, d, n, phi_n, buffer_1, buffer_2, NULL);
|
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); //CREATE CTX
|
||||||
|
|
||||||
//GENERATE PRIMES
|
EVP_PKEY_keygen_init(ctx); //INIT KEYGEN
|
||||||
generate_prime(p);
|
|
||||||
generate_prime(q);
|
|
||||||
|
|
||||||
//SET e
|
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, WHY2_CHAT_ECC); //SETUP ECC
|
||||||
mpz_set_str(e, WHY2_CHAT_RSA_EXPONENT, 10);
|
|
||||||
|
|
||||||
//GET n
|
EVP_PKEY_keygen(ctx, &pkey); //GENERATE ECC KEYPAIR
|
||||||
mpz_mul(n, p, q);
|
|
||||||
|
|
||||||
//GET phi
|
|
||||||
mpz_sub_ui(buffer_1, p, 1);
|
|
||||||
mpz_sub_ui(buffer_2, q, 1);
|
|
||||||
mpz_mul(phi_n, buffer_1, buffer_2);
|
|
||||||
|
|
||||||
//COUNT d
|
|
||||||
mpz_invert(d, e, phi_n);
|
|
||||||
|
|
||||||
printf("Saving keys...\n");
|
printf("Saving keys...\n");
|
||||||
|
|
||||||
@ -156,11 +97,12 @@ void why2_chat_init_keys(void)
|
|||||||
public = why2_fopen(public_path, "w+");
|
public = why2_fopen(public_path, "w+");
|
||||||
private = why2_fopen(private_path, "w+");
|
private = why2_fopen(private_path, "w+");
|
||||||
|
|
||||||
mpz_out_str(public, WHY2_CHAT_KEY_BASE, n);
|
PEM_write_PrivateKey(private, pkey, NULL, NULL, 0, NULL, NULL); //WRITE PRI KEY
|
||||||
mpz_out_str(private, WHY2_CHAT_KEY_BASE, d);
|
PEM_write_PUBKEY(public, pkey); //WRITE PUB KEY
|
||||||
|
|
||||||
//KEYGEN DEALLOCATION
|
//DEALLOCATION
|
||||||
mpz_clears(p, q, e, d, n, phi_n, buffer_1, buffer_2, NULL);
|
EVP_PKEY_CTX_free(ctx);
|
||||||
|
EVP_PKEY_free(pkey);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
//OPEN FILES
|
//OPEN FILES
|
||||||
@ -168,8 +110,8 @@ void why2_chat_init_keys(void)
|
|||||||
private = why2_fopen(private_path, "r");
|
private = why2_fopen(private_path, "r");
|
||||||
|
|
||||||
//READ THE KEYS
|
//READ THE KEYS
|
||||||
read_file(public, &rsa_modulus);
|
read_file(public, &ecc_pub);
|
||||||
read_file(private, &rsa_d);
|
read_file(private, &ecc_pri);
|
||||||
}
|
}
|
||||||
|
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
@ -182,28 +124,8 @@ void why2_chat_init_keys(void)
|
|||||||
|
|
||||||
void why2_chat_deallocate_keys(void)
|
void why2_chat_deallocate_keys(void)
|
||||||
{
|
{
|
||||||
why2_deallocate(rsa_modulus);
|
why2_deallocate(ecc_pub);
|
||||||
why2_deallocate(rsa_d);
|
why2_deallocate(ecc_pri);
|
||||||
}
|
|
||||||
|
|
||||||
char *why2_get_chat_modulus(void)
|
|
||||||
{
|
|
||||||
return rsa_modulus;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *why2_get_chat_d(void)
|
|
||||||
{
|
|
||||||
return rsa_d;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *why2_chat_rsa_pub_encrypt(char *to_encrypt)
|
|
||||||
{
|
|
||||||
return exp_mod(to_encrypt, WHY2_CHAT_RSA_EXPONENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *why2_chat_rsa_pri_decrypt(char *to_decrypt)
|
|
||||||
{
|
|
||||||
return exp_mod(to_decrypt, why2_get_chat_d());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *why2_sha256(char *input)
|
char *why2_sha256(char *input)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user