From 6a077bca3f1b384fb9e62b58b549a21a8508314e Mon Sep 17 00:00:00 2001 From: gbrochar Date: Thu, 15 Feb 2024 21:25:35 +0100 Subject: [PATCH] rsa branch backup --- rsa/ft_strjoin.c | 36 +++++++ rsa/ft_strlen.c | 23 +++++ rsa/generate_keys.c | 242 +++++++++++++++++++++++++++++++++++++------- rsa/guide.txt | 30 ++++++ rsa/lib.c | 8 ++ rsa/main.c | 5 +- rsa/rsa.h | 17 ++++ 7 files changed, 320 insertions(+), 41 deletions(-) create mode 100644 rsa/ft_strjoin.c create mode 100644 rsa/ft_strlen.c create mode 100644 rsa/guide.txt diff --git a/rsa/ft_strjoin.c b/rsa/ft_strjoin.c new file mode 100644 index 0000000..3534c10 --- /dev/null +++ b/rsa/ft_strjoin.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/29 09:48:11 by gbrochar #+# #+# */ +/* Updated: 2024/02/15 15:38:50 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rsa.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + size_t len; + size_t i; + + i = 0; + len = ft_strlen(s1) + ft_strlen(s2); + str = (char *)malloc((len + 1) * sizeof(char)); + if (!str) + return (NULL); + while (i < len) + { + if (i < ft_strlen(s1)) + str[i] = s1[i]; + else + str[i] = s2[i - ft_strlen(s1)]; + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/rsa/ft_strlen.c b/rsa/ft_strlen.c new file mode 100644 index 0000000..a4df623 --- /dev/null +++ b/rsa/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2015/11/25 14:43:32 by gbrochar #+# #+# */ +/* Updated: 2024/02/15 15:39:11 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "rsa.h" + +size_t ft_strlen(const char *s) +{ + int i; + + i = 0; + while (s[i]) + i++; + return (i); +} diff --git a/rsa/generate_keys.c b/rsa/generate_keys.c index a20f9a0..1fa87ae 100644 --- a/rsa/generate_keys.c +++ b/rsa/generate_keys.c @@ -1,53 +1,187 @@ #include "rsa.h" -int *random_bits(int n) { +void set_random_bits(int *n, int size) { int fd = open("/dev/urandom", O_RDONLY); - ft_log(INFO, "fd"); - ft_log(INFO, ft_itoa(fd)); - int *random_bits = (int *)malloc(n >> 3); - 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; + read(fd, n, size); } -void bitshift_array(int *a, int len) { +void set_msb_and_lsb_to_one(int *n, int size) { + n[0] |= 1; + n[size / sizeof(int) - 1] |= 1 << 31; +} + +/* + 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 >> 3); + 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; + } + */ + +void array_bitwise_right_shift(int *a, int len) { + int size = sizeof(int) * 8 - 1; for (int n = 0; n < len - 1; n++) { - a[n] = a[n] >> 1 | (a[n + 1] & 1) << 31; + a[n] = a[n] >> 1 | (a[n + 1] & 1) << size; } 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; +void array_bitwise_left_shift(int *a, int len) { + int size = sizeof(int) * 8 - 1; + for (int n = len - 1; n > 0; n--) { + a[n] = a[n] << 1 | ((a[n - 1] & (1 << size)) >> size); + } + a[0] <<= 1; +} + +// Will underflow +void array_decrement(int *a, int len) { int cursor = 0; - // a = n - 2 - while (1) { - a[cursor / 32] = a[cursor / 32] ^ (1 << (cursor % 32)); - if ((a[cursor / 32] >> (cursor % 32)) & 1 == 0) - break; - cursor++; + int size = sizeof(int) * 8; + while (cursor < size * len) { + a[cursor / size] = a[cursor / size] ^ (1 << (cursor % size)); + if ((a[cursor / size] >> (cursor % size)) & 1 == 0) { + return; + } + cursor += 1; } } -int *large_mod(int *a, int *b) { - int len = RSA_SIZE_BYTES / sizeof(int) >> 1; - //for (int i = len - 1; i > -1; i--) +/* + 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 (1) { +a[cursor / 32] = a[cursor / 32] ^ (1 << (cursor % 32)); +if ((a[cursor / 32] >> (cursor % 32)) & 1 == 0) +break; +cursor++; +} +} + */ + +int large_cmp(t_bigint *a, t_bitint *b) { + int size = sizeof(int) * 8; + int alen = a.size / sizeof(int); + int blen = b.size / sizeof(int); + int acursor = size * alen - 1; + int bcursor = size * alen - 1; + while (acursor > bcursor) { + if (a[acursor / size] & (1 << acursor % size)) { + return 1; + } + acursor -= 1; + } + while (bcursor > acursor) { + if (b[bcursor / size] & (1 << bcursor % size)) { + return -1; + } + bcursor -= 1; + } + int cursor = acursor; + while (cursor >= 0) { + int abit = a[cursor / size] & (1 << cursor % size); + int bbit = b[cursor / size] & (1 << cursor % size); + if (abit > bbit) { + return 1; + } + if (bbit > abit) { + return -1; + } + } + return 0; +} + +t_bigint bigint_clone(t_bigint src) { + t_bitint dst; + bigint.size = src.size; + bigint.data = (int *)protected_malloc(bigint.size, "bigint.data in bigint_clone"); + memcpy(bigint.data, src.data, size); +} + +int *bigint_sub(t_bigint *a, t_bigint *b) { + int *tmp = (int *)protected_malloc(size) +} + +int *bigint_mod(t_bigint *a, t_bigint *b) { + int size = sizeof(int) * 8; + int alen = a.size / sizeof(int); + int blen = b.size / sizeof(int); + int acursor = size * alen - 1; + int bcursor = size * blen - 1; + t_bigint bclone = bigint_clone(b); + + int cmp = large_cmp(a, b); + if (cmp == 0) { + + } + while (!(a[acursor / size] & (1 << acursor % size))) { + acursor -= 1; + } + while (!(b[bcursor / size] & (1 << bcursor % size))) { + bcursor -= 1; + } + int bpow = 0; + if (acursor < bcursor) { + return a.data; + } + while (large_cmp(a, bclone) == 1) { + while (large_cmp(a, b) == 1) { + array_bitwise_left_shift(b, blen); + bcursor += 1; + bpow += 1; + } + array_bitwise_right_shift(b, blen); + bcursor -= 1; + bpow -= 1; + a.data = bigint_sub(a, b); + } +} + +void generate_prime(int *n, int size) { + set_random_bits(n, size); + set_msb_and_lsb_to_one(n, size); + + // n - 1 = 2^s*d + int *d = (int *)protected_malloc(size, "d in generate_prime"); + d[0] -= 1; + int s = 0; + while (!(d[0] & 1)) { + s += 1; + array_bitwise_right_shift(d, size / sizeof(int)); + } + + } void generate_prime(int *n) { @@ -74,7 +208,7 @@ void generate_prime(int *n) { bitshift_array(d, RSA_SIZE_BYTES / sizeof(int) >> 1); } for (int k = 0; k < 128; k++) { -// int *a = generate_a(prime); + // int *a = generate_a(prime); } } @@ -82,6 +216,40 @@ int *ft_phi(int *p, int *q) { return (int *)malloc(sizeof(int)); } +t_rsa rsa_allocate(int block_size) { + t_rsa rsa; + + rsa.p.size = block_size; + rsa.q.size = block_size; + rsa.n.size = 2 * block_size; + rsa.e.size = block_size; + + rsa.p.data = (int *)protected_malloc(rsa.p.size, "rsa.p.size"); + rsa.q.data = (int *)protected_malloc(rsa.q.size, "rsa.q.size"); + rsa.n.data = (int *)protected_malloc(rsa.n.size, "rsa.n.size"); + rsa.e.data = (int *)protected_malloc(rsa.e.size, "rsa.e.size"); + for (int i = 0; i < block_size / sizeof(int)) { + rsa.p.data[i] = 0; + rsa.q.data[i] = 0; + rsa.e.data[i] = 0; + } + for (int i = 0; i < 2 * block_size / sizeof(int)) { + rsa.n.data[i] = 0; + } +} + +t_rsa rsa_new(int block_size) { + t_rsa rsa; + + // Convert block_size to bytes + block_size /= 8; + + rsa = rsa_allocate(block_size); + generate_prime(rsa.p.data, block_size / 2); + generate_prime(rsa.q.data, block_size / 2); + return rsa; +} + void generate_keys(int *p, int *q, int *e) { ft_log(INFO, "Generating primes..."); p = 0; diff --git a/rsa/guide.txt b/rsa/guide.txt new file mode 100644 index 0000000..444889d --- /dev/null +++ b/rsa/guide.txt @@ -0,0 +1,30 @@ +Soit p et q deux nombres premiers + +n = p * q +phi(n) = (p - 1)(q - 1) +choisir e tel que gcd(phi(n), e) = 1 + +soit un message m +on veut creer un message chiffre c a partir de m + +c = m^e mod(n) + +ed = 1 mod(phi) + +m = c^d mod(n) + + +Les nombres doivent etre d'une taille arbitraire. +Ils seront stocker dans un tableau. + +Il faut definir une structure pour decrire un grand nombre. +Ainsi que plusieurs operations : addition, multiplication, soustraction et modulo. + +Il faut soit changer la taille des nombre quand necessaire, soit definir une taille fixe pour chacun des nombres, ce qui est plus simple. + +len == length of array + +s_bigint { + uint32_t *data; + size_t len; +} diff --git a/rsa/lib.c b/rsa/lib.c index 70b4378..4247a25 100644 --- a/rsa/lib.c +++ b/rsa/lib.c @@ -1,5 +1,13 @@ #include "rsa.h" +void *protected_malloc(size_t size, char *str) { + void *ptr = malloc(size); + if (!ptr) { + ft_log(ERROR, ft_strjoin(ft_strjoin("allocation of ", str), " failed")); + exit(1); + } +} + void ft_log(int level, char *s) { switch (level) { case ERROR: diff --git a/rsa/main.c b/rsa/main.c index 0ea238b..39025d5 100644 --- a/rsa/main.c +++ b/rsa/main.c @@ -13,11 +13,8 @@ int main(int ac, char **av) { 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); + t_rsa rsa = rsa_new(RSA_SIZE); } else { ft_log(WARNING, "Need to pass message as argument"); return 1; diff --git a/rsa/rsa.h b/rsa/rsa.h index 477a9f5..b1951a2 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -17,13 +17,30 @@ #define WARNING 1 #define INFO 2 +typedef struct s_rsa { + t_bigint p; + t_bigint q; + t_bigint n; + t_bigint e; +} t_rsa; + +typedef struct s_bigint { + int *data; + int size; +} t_bigint; + void ft_log(int level, char *s); char *ft_itoa(int n); +size_t ft_strlen(const char *s); +char *ft_strjoin(char const *s1, char const *s2); + 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); +t_rsa rsa_new(int block_size); + #endif