diff --git a/.gitignore b/.gitignore index 1fde0fe..f3265eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +rsa/rsa +*.swp *.o *.a woody_woodpacker -woody \ No newline at end of file +woody diff --git a/rsa/ft_itoa.c b/rsa/ft_itoa.c new file mode 100644 index 0000000..6df4a87 --- /dev/null +++ b/rsa/ft_itoa.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:56:59 by gbrochar #+# #+# */ +/* Updated: 2024/02/14 13:40:32 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rsa.h" + +char *ft_itoa(int n) +{ + char *str; + size_t str_size; + int n_mem; + + n_mem = n; + str_size = (n < 0) ? 3 : 2; + while ((n > 9 || n < -9) && str_size++) + n /= 10; + str = (char *)malloc((str_size--) * sizeof(char)); + if (!str) + return (NULL); + str[str_size--] = '\0'; + while (n_mem > 9 || n_mem < -9) + { + str[str_size--] = (n_mem < 0) ? -(n_mem % 10) + '0' : n_mem % 10 + '0'; + n_mem = n_mem / 10; + } + str[0] = (n_mem < 0) ? '-' : (n_mem + '0'); + str[1] = (n_mem < 0) ? (-n_mem + '0') : str[1]; + return (str); +} diff --git a/rsa/generate_keys.c b/rsa/generate_keys.c new file mode 100644 index 0000000..e7f1431 --- /dev/null +++ b/rsa/generate_keys.c @@ -0,0 +1,93 @@ +#include "rsa.h" + +int *random_bits(int n) { + int fd = open("/dev/urandom", O_RDONLY); + ft_log(INFO, "fd"); + ft_log(INFO, ft_itoa(fd)); + int *random_bits = (int *)malloc(n >> 4); + if (!random_bits) { + ft_log(ERROR, "allocation failed on random_bits"); + exit(1); + } + ft_log(INFO, "before read"); + read(fd, random_bits, n >> 3, 0); + ft_log(INFO, "after read"); + // set n bits to random values + // for (int i = 0; i < n >> 5; i++) { + // random_bits[i] = rand(); + // } + // set LSB and MSB to 1 + random_bits[0] |= 1; + printf("last %ud\n", random_bits[(n / sizeof(int) >> 3) - 1]); + random_bits[(n / sizeof(int) >> 3) - 1] |= 0x80000000; + printf("last %ud\n", random_bits[(n / sizeof(int) >> 3) - 1]); + return random_bits; +} + +void bitshift_array(int *a, int len) { + for (int n = 0; n < len - 1; n++) { + a[n] = a[n] >> 1 | (a[n + 1] & 1) << 31; + } + a[len - 1] >>= 1; +} + +int *generate_a(int *n) { + int *a = (int *)malloc(RSA_SIZE_BYTES >> 1); + memcpy(a, n, RSA_SIZE_BYTES >> 1); + a[0] -= 1; + int cursor = 0; + // a = n - 2 + while (true) { + a[cursor / 32] = a ^ (1 << (cursor % 32) + if ((a[cursor / 32] >> (cursor % 32)) & 1 == 0) + break; + cursor++; + } +} + +int *large_mod(int *a, int *b) { + int len = RSA_SIZE_BYTES / sizeof(int) >> 1; + //for (int i = len - 1; i > -1; i--) +} + +void generate_prime(int *n) { + ft_log(INFO, "Generating random RSA_SIZE / 2 bits number"); + int *prime = random_bits(RSA_SIZE >> 1); + ft_log(INFO, "After random_bits"); + printf("sizeof int : %d\n", sizeof(int)); + for (int i = ((RSA_SIZE_BYTES / sizeof(int)) >> 1) - 1; i > -1; i--) { + printf("%ud\n", prime[i]); + } + int *d; + int s = 0; + ft_log(INFO, "Creating n - 1"); + d = (int *)malloc(RSA_SIZE_BYTES >> 1); + memcpy(d, prime, RSA_SIZE_BYTES >> 1); + // d = n - 1 (n's LSB is guarrenteed to be 1) + d[0] -= 1; + for (int i = (RSA_SIZE_BYTES / sizeof(int) >> 1) - 1; i > -1; i--) { + printf("%ud\n", d[i]); + } + ft_log(INFO, "Factoriizing n - 1 as 2^s*d"); + while (!(d[0] & 1)) { + s += 1; + bitshift_array(d, RSA_SIZE_BYTES / sizeof(int) >> 1); + } + for (int k = 0; k < 128; k++) { +// int *a = generate_a(prime); + } +} + +int *ft_phi(int *p, int *q) { + return (int *)malloc(sizeof(int)); +} + +void generate_keys(int *p, int *q, int *e) { + ft_log(INFO, "Generating primes..."); + p = 0; + q = 0; + e = 0; + generate_prime(p); + generate_prime(q); + int *phi = ft_phi(p, q); +} diff --git a/rsa/lib.c b/rsa/lib.c new file mode 100644 index 0000000..70b4378 --- /dev/null +++ b/rsa/lib.c @@ -0,0 +1,15 @@ +#include "rsa.h" + +void ft_log(int level, char *s) { + switch (level) { + case ERROR: + printf("error: %s\n", s); + break; + case WARNING: + printf("warning: %s\n", s); + break; + case INFO: + printf("info: %s\n", s); + break; + } +} diff --git a/rsa/main.c b/rsa/main.c new file mode 100644 index 0000000..0ea238b --- /dev/null +++ b/rsa/main.c @@ -0,0 +1,26 @@ +#include "rsa.h" + +/* +int decrypt(int c) { +} + +int encrypt(int m) { +} +*/ + +int main(int ac, char **av) { + if (ac == 2) { + ft_log(INFO, ft_itoa(RAND_MAX)); + int m = atoi(av[1]); + srand(time(NULL)); + int *p; + int *q; + int *e; + ft_log(INFO, "Generating keys..."); + generate_keys(p, q, e); + } else { + ft_log(WARNING, "Need to pass message as argument"); + return 1; + } + return 0; +} diff --git a/rsa/rsa.h b/rsa/rsa.h new file mode 100644 index 0000000..477a9f5 --- /dev/null +++ b/rsa/rsa.h @@ -0,0 +1,29 @@ +#ifndef _RSA_H +#define _RSA_H 1 + +#include +#include +#include +#include +#include +#include +#include + +// TODO remove bytes bits helper +#define RSA_SIZE 1024 +#define RSA_SIZE_BYTES 1024 / 8 + +#define ERROR 0 +#define WARNING 1 +#define INFO 2 + +void ft_log(int level, char *s); + +char *ft_itoa(int n); + +void generate_keys(int *p, int *q, int *e); +int *random_bits(int n); +void generate_prime(int *p); +int *phi(int *p, int *q); + +#endif