added key dir check into why2_chat_generate_keys

This commit is contained in:
Václav Šmejkal 2024-02-23 10:59:38 +01:00
parent 0e2da6ccb7
commit 0c03ed3785
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -19,8 +19,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/chat/crypto.h> #include <why2/chat/crypto.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/random.h> #include <sys/random.h>
#include <why2/memory.h>
#include <why2/misc.h> #include <why2/misc.h>
#include <gmp.h> #include <gmp.h>
@ -50,28 +54,43 @@ void generate_prime(mpz_t x)
//GLOBAL //GLOBAL
void why2_chat_generate_keys(void) void why2_chat_generate_keys(void)
{ {
//VARIABLES //GET PATH TO KEY DIR
mpz_t p, q, e, d, n, phi_n, buffer_1, buffer_2; char *path = why2_replace(WHY2_CHAT_PUB_KEY_LOCATION, "{USER}", getenv("USER"));
mpz_inits(p, q, e, d, n, phi_n, buffer_1, buffer_2, NULL);
//GENERATE PRIMES //CHECK IF KEYS EXIST
generate_prime(p); if (access(path, R_OK) != 0)
generate_prime(q); {
mkdir(path, 0700);
//SET e //SOME USER OUTPUT
mpz_set_ui(e, 65537); printf("You are probably running WHY2-Chat for the first time now.\nGenerating RSA keys...\n");
//GET n //VARIABLES
mpz_mul(n, p, q); mpz_t p, q, e, d, n, phi_n, buffer_1, buffer_2;
mpz_inits(p, q, e, d, n, phi_n, buffer_1, buffer_2, NULL);
//GET phi //GENERATE PRIMES
mpz_sub_ui(buffer_1, p, 1); generate_prime(p);
mpz_sub_ui(buffer_2, q, 1); generate_prime(q);
mpz_mul(phi_n, buffer_1, buffer_2);
//COUNT d //SET e
mpz_invert(d, e, phi_n); mpz_set_ui(e, 65537);
//GET n
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);
//KEY DEALLOCATION
mpz_clears(p, q, e, d, n, phi_n, buffer_1, buffer_2, NULL);
}
//DEALLOCATION //DEALLOCATION
mpz_clears(p, q, e, d, n, phi_n, buffer_1, buffer_2, NULL); why2_deallocate(path);
} }