From 5cf2bc357b8263f242d7872eda64d4f2732434bc Mon Sep 17 00:00:00 2001 From: gbrochar Date: Wed, 14 Feb 2024 17:14:03 +0100 Subject: [PATCH 01/34] feat: rsa doesnt compile lol --- .gitignore | 4 +- rsa/ft_itoa.c | 37 ++++++++++++++++++ rsa/generate_keys.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ rsa/lib.c | 15 ++++++++ rsa/main.c | 26 +++++++++++++ rsa/rsa.h | 29 ++++++++++++++ 6 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 rsa/ft_itoa.c create mode 100644 rsa/generate_keys.c create mode 100644 rsa/lib.c create mode 100644 rsa/main.c create mode 100644 rsa/rsa.h 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 From adf7a34fa61c9c5798fa1190622167dbf3334ace Mon Sep 17 00:00:00 2001 From: gbrochar Date: Wed, 14 Feb 2024 17:17:11 +0100 Subject: [PATCH 02/34] feat: rsa compiles --- rsa/generate_keys.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rsa/generate_keys.c b/rsa/generate_keys.c index e7f1431..e095e37 100644 --- a/rsa/generate_keys.c +++ b/rsa/generate_keys.c @@ -37,8 +37,8 @@ int *generate_a(int *n) { a[0] -= 1; int cursor = 0; // a = n - 2 - while (true) { - a[cursor / 32] = a ^ (1 << (cursor % 32) + while (1) { + a[cursor / 32] = a[cursor / 32] ^ (1 << (cursor % 32)); if ((a[cursor / 32] >> (cursor % 32)) & 1 == 0) break; cursor++; From 038719bb26738e09f9a754709c00b34f718d8022 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Wed, 14 Feb 2024 17:18:09 +0100 Subject: [PATCH 03/34] fix: malloc size was too small --- rsa/generate_keys.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsa/generate_keys.c b/rsa/generate_keys.c index e095e37..a20f9a0 100644 --- a/rsa/generate_keys.c +++ b/rsa/generate_keys.c @@ -4,7 +4,7 @@ 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); + int *random_bits = (int *)malloc(n >> 3); if (!random_bits) { ft_log(ERROR, "allocation failed on random_bits"); exit(1); From 6a077bca3f1b384fb9e62b58b549a21a8508314e Mon Sep 17 00:00:00 2001 From: gbrochar Date: Thu, 15 Feb 2024 21:25:35 +0100 Subject: [PATCH 04/34] 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 From 5028b0dd9f3b25f9cb5cf0c97e04aea636dabc64 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Thu, 15 Feb 2024 21:26:40 +0100 Subject: [PATCH 05/34] rsa reborn mod working --- rsa/Makefile | 18 +++ rsa/array.c | 40 +++++++ rsa/bigint.c | 240 ++++++++++++++++++++++++++++++++++++++++ rsa/ft_itoa.c | 37 ------- rsa/ft_strjoin.c | 36 ------ rsa/ft_strlen.c | 23 ---- rsa/generate_keys.c | 261 -------------------------------------------- rsa/guide.txt | 30 ----- rsa/lib.c | 23 ---- rsa/main.c | 22 +--- rsa/rsa.c | 84 ++++++++++++++ rsa/rsa.h | 69 ++++++------ rsa/utils.c | 9 ++ 13 files changed, 434 insertions(+), 458 deletions(-) create mode 100644 rsa/Makefile create mode 100644 rsa/array.c create mode 100644 rsa/bigint.c delete mode 100644 rsa/ft_itoa.c delete mode 100644 rsa/ft_strjoin.c delete mode 100644 rsa/ft_strlen.c delete mode 100644 rsa/generate_keys.c delete mode 100644 rsa/guide.txt delete mode 100644 rsa/lib.c create mode 100644 rsa/rsa.c create mode 100644 rsa/utils.c diff --git a/rsa/Makefile b/rsa/Makefile new file mode 100644 index 0000000..63baf89 --- /dev/null +++ b/rsa/Makefile @@ -0,0 +1,18 @@ +NAME = rsa + +SRC = \ + main.c \ + rsa.c \ + bigint.c \ + array.c \ + utils.c \ + +all: $(NAME) + +$(NAME): + gcc -Wall -Wextra -Werror $(SRC) -o $(NAME) + +fclean: + rm -rf $(NAME) + +re: fclean all diff --git a/rsa/array.c b/rsa/array.c new file mode 100644 index 0000000..ebd09d0 --- /dev/null +++ b/rsa/array.c @@ -0,0 +1,40 @@ +#include "rsa.h" + +void array_set_random_bytes(uint32_t *n, size_t size) { + int fd = open("/dev/urandom", O_RDONLY); + read(fd, n, size); +} + +void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size) { + n[0] |= 1; + n[size / sizeof(uint32_t) - 1] |= 1 << 31; +} + +void array_bitwise_right_shift(uint32_t *a, size_t len) { + size_t size = sizeof(uint32_t) * 8 - 1; + for (size_t n = 0; n < len - 1; n++) { + a[n] = a[n] >> 1 | (a[n + 1] & 1) << size; + } + a[len - 1] >>= 1; +} + +void array_bitwise_left_shift(uint32_t *a, size_t len) { + size_t size = sizeof(uint32_t) * 8 - 1; + for (size_t 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(uint32_t *a, size_t len) { + size_t cursor = 0; + size_t size = sizeof(uint32_t) * 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; + } +} diff --git a/rsa/bigint.c b/rsa/bigint.c new file mode 100644 index 0000000..4b0f692 --- /dev/null +++ b/rsa/bigint.c @@ -0,0 +1,240 @@ +#include "rsa.h" + +void bigint_set_random_bytes(bigint_t n) { + int fd = open("/dev/urandom", O_RDONLY); + read(fd, n.data, n.len * sizeof(uint32_t)); + close(fd); +} + +void bigint_set_msb_and_lsb_to_one(bigint_t n) { + n.data[0] |= 1; + n.data[n.len - 1] |= 1 << 31; +} + +void bigint_bitwise_right_shift(bigint_t n) { + size_t size = sizeof(uint32_t) * 8 - 1; + for (size_t i = 0; i < n.len - 1; i++) { + n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << size; + } + n.data[n.len - 1] >>= 1; +} + +void bigint_bitwise_left_shift(bigint_t n) { + size_t size = sizeof(uint32_t) * 8 - 1; + for (int i = n.len - 1; i > 0; i--) { + n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << size)) >> size); + } + n.data[0] <<= 1; +} + +bigint_t assignable_bigint_bitwise_left_shift(bigint_t n) { + bigint_t result = bigint_clone(n); + size_t size = sizeof(uint32_t) * 8 - 1; + for (int i = result.len - 1; i > 0; i--) { + result.data[i] = result.data[i] << 1 | ((result.data[i - 1] & (1 << size)) >> size); + } + result.data[0] <<= 1; + return result; +} + +// Will underflow +void bigint_decrement(bigint_t n) { + size_t cursor = 0; + size_t size = sizeof(uint32_t) * 8; + while (cursor < size * n.len) { + n.data[cursor / size] = n.data[cursor / size] ^ (1 << (cursor % size)); + if (((n.data[cursor / size] >> (cursor % size)) & 1) == 0) { + return; + } + cursor += 1; + } +} + +// TODO refactor/clean assume same length ? +int bigint_cmp(bigint_t a, bigint_t b) { + uint32_t size = sizeof(uint32_t) * 8; + uint32_t acursor = size * a.len - 1; + uint32_t bcursor = size * b.len - 1; + printf("cursors a and b %d %d\n", acursor, bcursor); + while (acursor > bcursor) { + if (a.data[acursor / size] & (1 << acursor % size)) { + return 1; + } + acursor -= 1; + } + while (bcursor > acursor) { + if (b.data[bcursor / size] & (1 << bcursor % size)) { + return -1; + } + bcursor -= 1; + } + int cursor = acursor; + while (cursor >= 0) { + uint32_t abit = a.data[cursor / size] & (1 << (cursor % size)); + uint32_t bbit = b.data[cursor / size] & (1 << (cursor % size)); + //printf("cursor %d abit %ud bbit %ud\n", cursor, abit, bbit); + if (abit > bbit) { + return 1; + } + if (bbit > abit) { + return -1; + } + cursor -= 1; + } + return 0; +} + +bigint_t assignable_bigint_substraction(bigint_t a, bigint_t b) { + if (a.len != b.len) { + printf("error: attempting to substract numbers of different length\n"); + exit(1); + } + + bigint_t result = bigint_clone(a); + bigint_t borrow = bigint_clone(b); + bigint_t y = bigint_clone(b); + bigint_t zero = bigint_zero(a.len); + while (bigint_cmp(borrow, zero)) { + for (size_t i = 0; i < a.len; i++) { + borrow.data[i] = ~result.data[i] & y.data[i]; + result.data[i] = result.data[i] ^ y.data[i]; + } + bigint_destroy(y); + y = assignable_bigint_bitwise_left_shift(borrow); + } + bigint_destroy(y); + bigint_destroy(borrow); + bigint_destroy(zero); + return result; +} + +void bigint_substraction(bigint_t a, bigint_t b) { + if (a.len != b.len) { + printf("error: attempting to substract numbers of different length\n"); + exit(1); + } + + bigint_t borrow = bigint_clone(b); + bigint_t y = bigint_clone(b); + bigint_t zero = bigint_zero(a.len); + while (bigint_cmp(borrow, zero)) { + for (size_t i = 0; i < a.len; i++) { + borrow.data[i] = ~a.data[i] & y.data[i]; + a.data[i] = a.data[i] ^ y.data[i]; + } + bigint_destroy(y); + y = assignable_bigint_bitwise_left_shift(borrow); + } + bigint_destroy(y); + bigint_destroy(borrow); + bigint_destroy(zero); +} + +bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b) { + bigint_t result = bigint_clone(a); + bigint_t mod = bigint_clone(b); + printf("a = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + if (bigint_cmp(result, b) == -1) { + bigint_destroy(mod); + return result; + } + bigint_bitwise_left_shift(mod); + printf("after bitwise_shift\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + while (bigint_cmp(b, mod) == -1) { + while (bigint_cmp(result, mod) == 1) { + bigint_bitwise_left_shift(mod); + printf("DOUBLE after bitwise_shift\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + } + bigint_bitwise_right_shift(mod); + printf("before sub \na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + if (bigint_cmp(result, mod) == 1) { + bigint_substraction(result, mod); + + printf("subbed\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + } + bigint_bitwise_right_shift(mod); + } + while (bigint_cmp(result, b) == 1) { + bigint_substraction(result, b); + + printf("subbed\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + } + return result; +} + +void bigint_print(bigint_t n) { + for (int i = n.len - 1; i >= 0; i--) { + printf("bigint %ud\n", n.data[i]); + } +} + +bigint_t bigint_clone(bigint_t src) { + bigint_t dst; + + dst.len = src.len; + dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t)); + memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); + + return dst; +} + +bigint_t bigint_prime(size_t len) { + bigint_t n = bigint_new(len); + + printf("new\n"); + bigint_print(n); + bigint_set_random_bytes(n); + printf("random bytes\n"); + bigint_print(n); + bigint_set_msb_and_lsb_to_one(n); + printf("msb and lsb set to tone\n"); + bigint_print(n); + + bigint_t d = bigint_clone(n); + d.data[0] -= 1; + uint32_t s = 0; + while (!d.data[0] & 1) { + bigint_bitwise_right_shift(d); + s += 1; + } + + bigint_t two = bigint_zero(len); + two.data[0] = 2; + bigint_t n_minus_two = bigint_clone(d); + bigint_decrement(n_minus_two); + + for (uint32_t k = 0; k < 128; k++) { + bigint_t a = bigint_zero(len); + while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) { + printf("this is good %d\n", k); + bigint_set_random_bytes(a); + } + bigint_destroy(a); + } + return n; +} + +bigint_t bigint_new(size_t len) { + bigint_t bigint; + + bigint.len = len; + bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t)); + + return bigint; +} + +bigint_t bigint_zero(size_t len) { + bigint_t bigint; + + bigint = bigint_new(len); + for (size_t i = 0; i < len; i++) { + bigint.data[i] = 0; + } + + return bigint; +} + +void bigint_destroy(bigint_t n) { + free(n.data); + n.data = NULL; +} diff --git a/rsa/ft_itoa.c b/rsa/ft_itoa.c deleted file mode 100644 index 6df4a87..0000000 --- a/rsa/ft_itoa.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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/ft_strjoin.c b/rsa/ft_strjoin.c deleted file mode 100644 index 3534c10..0000000 --- a/rsa/ft_strjoin.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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 deleted file mode 100644 index a4df623..0000000 --- a/rsa/ft_strlen.c +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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 deleted file mode 100644 index 1fa87ae..0000000 --- a/rsa/generate_keys.c +++ /dev/null @@ -1,261 +0,0 @@ -#include "rsa.h" - -void set_random_bits(int *n, int size) { - int fd = open("/dev/urandom", O_RDONLY); - read(fd, n, size); -} - -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) << size; - } - a[len - 1] >>= 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; - 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 *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) { - 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)); -} - -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; - q = 0; - e = 0; - generate_prime(p); - generate_prime(q); - int *phi = ft_phi(p, q); -} diff --git a/rsa/guide.txt b/rsa/guide.txt deleted file mode 100644 index 444889d..0000000 --- a/rsa/guide.txt +++ /dev/null @@ -1,30 +0,0 @@ -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 deleted file mode 100644 index 4247a25..0000000 --- a/rsa/lib.c +++ /dev/null @@ -1,23 +0,0 @@ -#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: - 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 index 39025d5..c1df254 100644 --- a/rsa/main.c +++ b/rsa/main.c @@ -1,23 +1,13 @@ #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)); - ft_log(INFO, "Generating keys..."); - t_rsa rsa = rsa_new(RSA_SIZE); - } else { - ft_log(WARNING, "Need to pass message as argument"); - return 1; + (void)av; + rsa_t rsa = rsa_generate_keys(RSA_BLOCK_SIZE); + (void)rsa; + } + else { + printf("usage: ./rsa message\n"); } return 0; } diff --git a/rsa/rsa.c b/rsa/rsa.c new file mode 100644 index 0000000..358e3f8 --- /dev/null +++ b/rsa/rsa.c @@ -0,0 +1,84 @@ +#include "rsa.h" + +rsa_t rsa_init(size_t len) { + rsa_t rsa; + +// rsa.p = bigint_prime(len / 2); +// rsa.q = bigint_prime(len / 2); + + rsa.p = bigint_zero(len / 2); + rsa.q = bigint_zero(len / 2); + return rsa; +} + +rsa_t rsa_generate_keys(size_t block_size) { + size_t len = block_size / sizeof(uint32_t) / 8; + rsa_t rsa = rsa_init(len); + + bigint_t a = bigint_zero(4); + bigint_t b = bigint_zero(4); + + a.data[0] = 1234567890; + b.data[0] = 234567; + + printf("cmp a and b %d\n", bigint_cmp(a, b)); + + a.data[0] = 1234567890; +// b.data[0] = 1921572864; + + printf("cmp a and b %d\n", bigint_cmp(a, b)); + +/* + printf("cmp a and b %d\n", bigint_cmp(a, b)); + printf("cmp b and a %d\n", bigint_cmp(b, a)); + + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); + bigint_bitwise_left_shift(b); + printf("b %ud\n", b.data[0]); + printf("cmp a and b %d\n", bigint_cmp(a, b)); +*/ + bigint_t result = assignable_bigint_modulo(a, b); + + printf("result is %ud\n", result.data[0]); + + return rsa; +} diff --git a/rsa/rsa.h b/rsa/rsa.h index b1951a2..148169f 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -1,46 +1,51 @@ #ifndef _RSA_H #define _RSA_H 1 -#include +#include #include +#include #include -#include -#include -#include #include +#include +#include -// TODO remove bytes bits helper -#define RSA_SIZE 1024 -#define RSA_SIZE_BYTES 1024 / 8 +#define RSA_BLOCK_SIZE 2048 -#define ERROR 0 -#define WARNING 1 -#define INFO 2 +typedef struct bigint_s { + uint32_t *data; + size_t len; +} bigint_t; -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); +typedef struct rsa_s { + bigint_t p; + bigint_t q; +} rsa_t; -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); +void *protected_malloc(size_t size); -t_rsa rsa_new(int block_size); +rsa_t rsa_generate_keys(size_t block_size); + + +void bigint_set_random_bytes(bigint_t n); +void bigint_set_msb_and_lsb_to_one(bigint_t n); +void bigint_bitwise_left_shift(bigint_t n); +void bigint_bitwise_right_shift(bigint_t n); +void bigint_decrement(bigint_t n); +int bigint_cmp(bigint_t a, bigint_t b); +bigint_t bigint_prime(size_t len); +void bigint_print(bigint_t n); +bigint_t bigint_new(size_t len); +bigint_t bigint_zero(size_t len); +bigint_t bigint_clone(bigint_t src); +bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); + +void bigint_destroy(bigint_t n); + +void array_set_random_bytes(uint32_t *n, size_t size); +void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size); +void array_bitwise_right_shift(uint32_t *a, size_t len); +void array_bitwise_right_shift(uint32_t *a, size_t len); +void array_decrement(uint32_t *a, size_t len); #endif diff --git a/rsa/utils.c b/rsa/utils.c new file mode 100644 index 0000000..cc99207 --- /dev/null +++ b/rsa/utils.c @@ -0,0 +1,9 @@ +#include "rsa.h" + +void *protected_malloc(size_t size) { + void *ptr = malloc(size); + if (!ptr) { + printf("malloc returned NULL"); + } + return ptr; +} From 3883287e8d83ba885efcf92a25955fa9d6213587 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 16 Feb 2024 13:32:06 +0100 Subject: [PATCH 06/34] feat: square and multiply, a^e mod n --- rsa/bigint.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++----- rsa/rsa.c | 19 +++++++ rsa/rsa.h | 3 ++ 3 files changed, 159 insertions(+), 12 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 4b0f692..db812d5 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -55,7 +55,6 @@ int bigint_cmp(bigint_t a, bigint_t b) { uint32_t size = sizeof(uint32_t) * 8; uint32_t acursor = size * a.len - 1; uint32_t bcursor = size * b.len - 1; - printf("cursors a and b %d %d\n", acursor, bcursor); while (acursor > bcursor) { if (a.data[acursor / size] & (1 << acursor % size)) { return 1; @@ -84,6 +83,7 @@ int bigint_cmp(bigint_t a, bigint_t b) { return 0; } +// TODO check opti bigint_t assignable_bigint_substraction(bigint_t a, bigint_t b) { if (a.len != b.len) { printf("error: attempting to substract numbers of different length\n"); @@ -108,7 +108,13 @@ bigint_t assignable_bigint_substraction(bigint_t a, bigint_t b) { return result; } -void bigint_substraction(bigint_t a, bigint_t b) { +// TODO check opti +void bigint_substraction(bigint_t a, bigint_t bb) { + bigint_t b = bigint_clone(bb); + if (a.len > bb.len) { + b = bigint_zero(a.len); + memcpy(b.data, bb.data, b.len * sizeof(uint32_t)); + } if (a.len != b.len) { printf("error: attempting to substract numbers of different length\n"); exit(1); @@ -130,35 +136,154 @@ void bigint_substraction(bigint_t a, bigint_t b) { bigint_destroy(zero); } +// TODO check opti bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b) { bigint_t result = bigint_clone(a); bigint_t mod = bigint_clone(b); - printf("a = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + if (a.len > b.len) { + mod = bigint_zero(a.len); + memcpy(mod.data, b.data, b.len * sizeof(uint32_t)); + } if (bigint_cmp(result, b) == -1) { bigint_destroy(mod); return result; } bigint_bitwise_left_shift(mod); - printf("after bitwise_shift\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); while (bigint_cmp(b, mod) == -1) { while (bigint_cmp(result, mod) == 1) { bigint_bitwise_left_shift(mod); - printf("DOUBLE after bitwise_shift\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); } bigint_bitwise_right_shift(mod); - printf("before sub \na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); if (bigint_cmp(result, mod) == 1) { - bigint_substraction(result, mod); - - printf("subbed\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); + bigint_substraction(result, mod); } - bigint_bitwise_right_shift(mod); } - while (bigint_cmp(result, b) == 1) { + while (bigint_cmp(result, b) == 1) { bigint_substraction(result, b); + } + bigint_destroy(mod); + return result; +} - printf("subbed\na = %ud\nb = %ud\nresult = %ud\nmod = %ud\n", a.data[0], b.data[0], result.data[0], mod.data[0]); +void bigint_add(bigint_t a, bigint_t b) { + bigint_t result = bigint_zero(a.len); + size_t size = sizeof(uint32_t) * 8; + size_t width = a.len * size; + uint32_t carriage = 0; + +// printf("hello add\n"); + for (size_t cursor = 0; cursor < width; cursor++) { +// printf("hahaha %ld %ld\n", cursor, width); + uint32_t a_bit = a.data[cursor / size] >> (cursor % size) & 1; + uint32_t b_bit = b.data[cursor / size] >> (cursor % size) & 1; + result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size); + carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage); + } +// printf("im out\n"); + bigint_destroy(a); + a = bigint_clone(result); + bigint_destroy(result); +} + +void bigint_set_zeros(bigint_t n) { +// printf("hello set zeros\n"); + for (size_t i = 0; i < n.len; i++) { + n.data[i] = 0; + } +// printf("goodbye set zeros\n"); +} + +bigint_t assignable_bigint_mul(bigint_t a, bigint_t b) { + bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + /*if (a.len > b.len) { + result = bigint_zero(a.len); + b_tool = bigint_zero(a.len); + } else { + result = bigint_zero(a.len + b.len); + b_tool = bigint_zero(a.len + b.len); + }*/ + size_t size = sizeof(uint32_t) * 8; + size_t width = a.len * size; + + printf("multiplying %d and %d\n", a.data[0], b.data[0]); + +// printf("hello mul\n"); + for (size_t cursor = 0; cursor < width; cursor++) { +// printf("hello BIG LOOP ls %ld %ld\n", cursor, width); + if (a.data[cursor / 32] >> (cursor % 32) & 1) { + bigint_set_zeros(b_tool); + printf("bef %d\n", b_tool.data[0]); +// printf("hello memcpy\n"); + memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t)); + printf("aft %d\n", b_tool.data[0]); +// printf("goodbye memcpy\n"); + for (size_t i = 0; i < cursor; i++) { +// printf("hello bitwise ls %ld %ld\n", i, cursor); + bigint_bitwise_left_shift(b_tool); +// printf("goodbye bitwise ls\n"); + } +// printf("before hello add\n"); + bigint_add(result, b_tool); } + } +// printf("GOODBYE BIG LOOP ls \n"); + bigint_destroy(b_tool); + return result; +} + +// a^e mod n +// clean memory tricks !!! +bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { + printf("print a\n"); + bigint_print(a); + printf("print e\n"); + bigint_print(e); + printf("print n\n"); + bigint_print(n); + bigint_t result = bigint_clone(a); + size_t size = sizeof(uint32_t) * 8; + int cursor = e.len * size - 1; + while (!(e.data[cursor / 32] & 1 << (cursor % 32))) { + cursor--; + } + cursor--; + /* + printf("SQUARE\n"); + bigint_t tmp_result2 = assignable_bigint_mul(result, result); + bigint_destroy(result); + result = bigint_clone(tmp_result2); + bigint_destroy(tmp_result2); + tmp_result2 = assignable_bigint_modulo(result, n); + bigint_destroy(result); + result = bigint_clone(tmp_result2); + bigint_destroy(tmp_result2); + */ + printf("cursor %d\n", cursor); + while (cursor >= 0) { + printf("SQUARE\n"); + bigint_t tmp_result2 = assignable_bigint_mul(result, result); + bigint_destroy(result); + result = bigint_clone(tmp_result2); + bigint_destroy(tmp_result2); + tmp_result2 = assignable_bigint_modulo(result, n); + bigint_destroy(result); + result = bigint_clone(tmp_result2); + bigint_destroy(tmp_result2); + if (e.data[cursor / 32] & 1 << (cursor % 32)) { + printf("MULTIPLY\n"); + bigint_t tmp_result = assignable_bigint_mul(result, a); + bigint_destroy(result); + result = bigint_clone(tmp_result); + bigint_destroy(tmp_result); + tmp_result = assignable_bigint_modulo(result, n); + bigint_destroy(result); + result = bigint_clone(tmp_result); + bigint_destroy(tmp_result); + } + cursor -= 1; + } + printf("this time its over\n"); return result; } diff --git a/rsa/rsa.c b/rsa/rsa.c index 358e3f8..66c254b 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -80,5 +80,24 @@ rsa_t rsa_generate_keys(size_t block_size) { printf("result is %ud\n", result.data[0]); + a = bigint_clone(result); + b.data[0] = 5764; + + printf("length\na: %lu e: %lu n: %lu\n", result.len, a.len, b.len); + + bigint_t result2 = assignable_bigint_pow_mod(result, a, b); + printf("bigpowmod is %u \n", result2.data[0]); +/* result.data[0] = 8; + a.data[0] = 4; + result2 = assignable_bigint_mul(result, a); + printf("result2 is %u \n", result2.data[0]); + result.data[0] = 84; + a.data[0] = 463; + result2 = assignable_bigint_mul(result, a); + printf("result2 is %u \n", result2.data[0]); + bigint_add(result, a); + + printf("result2 is %u \n", result.data[0]); +*/ return rsa; } diff --git a/rsa/rsa.h b/rsa/rsa.h index 148169f..02860e4 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -38,7 +38,10 @@ void bigint_print(bigint_t n); bigint_t bigint_new(size_t len); bigint_t bigint_zero(size_t len); bigint_t bigint_clone(bigint_t src); +void bigint_add(bigint_t a, bigint_t b); +bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); +bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); void bigint_destroy(bigint_t n); From a000c56ce5d7b6e388926c090b1cfa49b6725332 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 16 Feb 2024 13:33:52 +0100 Subject: [PATCH 07/34] clean: remove comments --- rsa/bigint.c | 48 +----------------------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index db812d5..a48ac09 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -71,7 +71,6 @@ int bigint_cmp(bigint_t a, bigint_t b) { while (cursor >= 0) { uint32_t abit = a.data[cursor / size] & (1 << (cursor % size)); uint32_t bbit = b.data[cursor / size] & (1 << (cursor % size)); - //printf("cursor %d abit %ud bbit %ud\n", cursor, abit, bbit); if (abit > bbit) { return 1; } @@ -171,63 +170,39 @@ void bigint_add(bigint_t a, bigint_t b) { size_t width = a.len * size; uint32_t carriage = 0; -// printf("hello add\n"); for (size_t cursor = 0; cursor < width; cursor++) { -// printf("hahaha %ld %ld\n", cursor, width); uint32_t a_bit = a.data[cursor / size] >> (cursor % size) & 1; uint32_t b_bit = b.data[cursor / size] >> (cursor % size) & 1; result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size); carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage); } -// printf("im out\n"); bigint_destroy(a); a = bigint_clone(result); bigint_destroy(result); } void bigint_set_zeros(bigint_t n) { -// printf("hello set zeros\n"); for (size_t i = 0; i < n.len; i++) { n.data[i] = 0; } -// printf("goodbye set zeros\n"); } bigint_t assignable_bigint_mul(bigint_t a, bigint_t b) { bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - /*if (a.len > b.len) { - result = bigint_zero(a.len); - b_tool = bigint_zero(a.len); - } else { - result = bigint_zero(a.len + b.len); - b_tool = bigint_zero(a.len + b.len); - }*/ size_t size = sizeof(uint32_t) * 8; size_t width = a.len * size; - printf("multiplying %d and %d\n", a.data[0], b.data[0]); - -// printf("hello mul\n"); for (size_t cursor = 0; cursor < width; cursor++) { -// printf("hello BIG LOOP ls %ld %ld\n", cursor, width); if (a.data[cursor / 32] >> (cursor % 32) & 1) { bigint_set_zeros(b_tool); - printf("bef %d\n", b_tool.data[0]); -// printf("hello memcpy\n"); memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t)); - printf("aft %d\n", b_tool.data[0]); -// printf("goodbye memcpy\n"); for (size_t i = 0; i < cursor; i++) { -// printf("hello bitwise ls %ld %ld\n", i, cursor); bigint_bitwise_left_shift(b_tool); -// printf("goodbye bitwise ls\n"); } -// printf("before hello add\n"); bigint_add(result, b_tool); } } -// printf("GOODBYE BIG LOOP ls \n"); bigint_destroy(b_tool); return result; } @@ -235,12 +210,6 @@ bigint_t assignable_bigint_mul(bigint_t a, bigint_t b) { // a^e mod n // clean memory tricks !!! bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { - printf("print a\n"); - bigint_print(a); - printf("print e\n"); - bigint_print(e); - printf("print n\n"); - bigint_print(n); bigint_t result = bigint_clone(a); size_t size = sizeof(uint32_t) * 8; int cursor = e.len * size - 1; @@ -248,20 +217,7 @@ bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { cursor--; } cursor--; - /* - printf("SQUARE\n"); - bigint_t tmp_result2 = assignable_bigint_mul(result, result); - bigint_destroy(result); - result = bigint_clone(tmp_result2); - bigint_destroy(tmp_result2); - tmp_result2 = assignable_bigint_modulo(result, n); - bigint_destroy(result); - result = bigint_clone(tmp_result2); - bigint_destroy(tmp_result2); - */ - printf("cursor %d\n", cursor); while (cursor >= 0) { - printf("SQUARE\n"); bigint_t tmp_result2 = assignable_bigint_mul(result, result); bigint_destroy(result); result = bigint_clone(tmp_result2); @@ -271,7 +227,6 @@ bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { result = bigint_clone(tmp_result2); bigint_destroy(tmp_result2); if (e.data[cursor / 32] & 1 << (cursor % 32)) { - printf("MULTIPLY\n"); bigint_t tmp_result = assignable_bigint_mul(result, a); bigint_destroy(result); result = bigint_clone(tmp_result); @@ -283,7 +238,6 @@ bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { } cursor -= 1; } - printf("this time its over\n"); return result; } @@ -312,7 +266,7 @@ bigint_t bigint_prime(size_t len) { printf("random bytes\n"); bigint_print(n); bigint_set_msb_and_lsb_to_one(n); - printf("msb and lsb set to tone\n"); + printf("msb and lsb set to one\n"); bigint_print(n); bigint_t d = bigint_clone(n); From 6806db1c6fda092f263f80e104ccbe6b6d6c8114 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 16 Feb 2024 15:57:11 +0100 Subject: [PATCH 08/34] feat: prime working rly slow and leaks --- rsa/Makefile | 3 + rsa/bigint.c | 191 ++++++++++++++++++++++++++++++++++++++++++--------- rsa/rsa.c | 85 ++++------------------- rsa/rsa.h | 3 +- 4 files changed, 177 insertions(+), 105 deletions(-) diff --git a/rsa/Makefile b/rsa/Makefile index 63baf89..ea760a3 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -12,6 +12,9 @@ all: $(NAME) $(NAME): gcc -Wall -Wextra -Werror $(SRC) -o $(NAME) +fast: + gcc -Wall -Wextra -Werror -o3 $(SRC) -o $(NAME) + fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index a48ac09..370796a 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -164,6 +164,35 @@ bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b) { return result; } +// TODO check opti +void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) { + bigint_set_zeros(result); + memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + bigint_t mod = bigint_clone(b); + if (a.len > b.len) { + mod = bigint_zero(a.len); + memcpy(mod.data, b.data, b.len * sizeof(uint32_t)); + } + if (bigint_cmp(result, b) == -1) { + bigint_destroy(mod); + return ; + } + bigint_bitwise_left_shift(mod); + while (bigint_cmp(b, mod) == -1) { + while (bigint_cmp(result, mod) == 1) { + bigint_bitwise_left_shift(mod); + } + bigint_bitwise_right_shift(mod); + if (bigint_cmp(result, mod) == 1) { + bigint_substraction(result, mod); + } + } + while (bigint_cmp(result, b) == 1) { + bigint_substraction(result, b); + } + bigint_destroy(mod); +} + void bigint_add(bigint_t a, bigint_t b) { bigint_t result = bigint_zero(a.len); size_t size = sizeof(uint32_t) * 8; @@ -207,10 +236,31 @@ bigint_t assignable_bigint_mul(bigint_t a, bigint_t b) { return result; } +void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { + bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + size_t size = sizeof(uint32_t) * 8; + size_t width = a.len * size; + bigint_set_zeros(result); + for (size_t cursor = 0; cursor < width; cursor++) { + if (a.data[cursor / 32] >> (cursor % 32) & 1) { + bigint_set_zeros(b_tool); + memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t)); + for (size_t i = 0; i < cursor; i++) { + bigint_bitwise_left_shift(b_tool); + } + bigint_add(result, b_tool); + } + } + bigint_destroy(b_tool); +} + // a^e mod n // clean memory tricks !!! -bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { - bigint_t result = bigint_clone(a); +void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2) { + bigint_set_zeros(result); + bigint_set_zeros(custom); + bigint_set_zeros(custom2); + memcpy(result.data, a.data, a.len * sizeof(uint32_t)); size_t size = sizeof(uint32_t) * 8; int cursor = e.len * size - 1; while (!(e.data[cursor / 32] & 1 << (cursor % 32))) { @@ -218,26 +268,45 @@ bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { } cursor--; while (cursor >= 0) { - bigint_t tmp_result2 = assignable_bigint_mul(result, result); - bigint_destroy(result); - result = bigint_clone(tmp_result2); - bigint_destroy(tmp_result2); - tmp_result2 = assignable_bigint_modulo(result, n); - bigint_destroy(result); - result = bigint_clone(tmp_result2); - bigint_destroy(tmp_result2); + custom_bigint_mul(result, result, custom); + custom_bigint_modulo(custom, n, custom2); + bigint_set_zeros(result); + memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); if (e.data[cursor / 32] & 1 << (cursor % 32)) { - bigint_t tmp_result = assignable_bigint_mul(result, a); - bigint_destroy(result); - result = bigint_clone(tmp_result); - bigint_destroy(tmp_result); - tmp_result = assignable_bigint_modulo(result, n); - bigint_destroy(result); - result = bigint_clone(tmp_result); - bigint_destroy(tmp_result); + custom_bigint_mul(result, a, custom); + custom_bigint_modulo(custom, n, custom2); + memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } cursor -= 1; } +} +// a^e mod n +// clean memory tricks !!! +bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { + bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + size_t size = sizeof(uint32_t) * 8; + int cursor = e.len * size - 1; + while (!(e.data[cursor / 32] & 1 << (cursor % 32))) { + cursor--; + } + cursor--; + while (cursor >= 0) { + custom_bigint_mul(result, result, custom); + custom_bigint_modulo(custom, n, custom2); + bigint_set_zeros(result); + memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + if (e.data[cursor / 32] & 1 << (cursor % 32)) { + custom_bigint_mul(result, a, custom); + custom_bigint_modulo(custom, n, custom2); + memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + } + cursor -= 1; + } + bigint_destroy(custom); + bigint_destroy(custom2); return result; } @@ -257,39 +326,97 @@ bigint_t bigint_clone(bigint_t src) { return dst; } -bigint_t bigint_prime(size_t len) { - bigint_t n = bigint_new(len); +void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t one, bigint_t n_minus_two, bigint_t n_minus_one) { + bigint_destroy(x); + bigint_destroy(y); + bigint_destroy(n); + bigint_destroy(d); + bigint_destroy(two); + bigint_destroy(one); + bigint_destroy(n_minus_two); + bigint_destroy(n_minus_one); +} +/* + bigint_t bigint_random_range(bigint_t low, bigint_t high) { + + } + */ +bigint_t bigint_prime(size_t len) { + bigint_t n = bigint_zero(len); - printf("new\n"); - bigint_print(n); bigint_set_random_bytes(n); - printf("random bytes\n"); - bigint_print(n); bigint_set_msb_and_lsb_to_one(n); - printf("msb and lsb set to one\n"); - bigint_print(n); + // printf("msb and lsb set to one N IS: \n"); + // bigint_print(n); bigint_t d = bigint_clone(n); d.data[0] -= 1; uint32_t s = 0; - while (!d.data[0] & 1) { + while (!(d.data[0] & 1)) { bigint_bitwise_right_shift(d); s += 1; } + // printf("D IS:\n"); + // bigint_print(d); + + bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t two = bigint_zero(len); two.data[0] = 2; - bigint_t n_minus_two = bigint_clone(d); + bigint_t one = bigint_zero(len); + one.data[0] = 1; + bigint_t n_minus_two = bigint_clone(n); + bigint_t n_minus_one = bigint_clone(n); + n_minus_two.data[0] -= 1; + n_minus_one.data[0] -= 1; bigint_decrement(n_minus_two); - + bigint_t a = bigint_zero(len); + // printf("s is %d\n", s); for (uint32_t k = 0; k < 128; k++) { - bigint_t a = bigint_zero(len); + // printf("this is good %d\n", k); + //bigint_t a = bigint_zero(len); + bigint_set_zeros(a); while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) { - printf("this is good %d\n", k); bigint_set_random_bytes(a); } - bigint_destroy(a); + //printf("A IS: \n"); + //bigint_print(a); + //printf("a %d\n", k); + //bigint_t x = custom_bigint_pow_mod(a, d, n, x, custom, custom2); + custom_bigint_pow_mod(a, d, n, x, custom, custom2); + //printf("b %d\n", k); + for (uint32_t i = 0; i < s; i++) { + //bigint_destroy(y); + custom_bigint_pow_mod(x, two, n, y, custom, custom2); + //y = assignable_bigint_pow_mod(x, two, n); + //printf("X IS: \n"); + //bigint_print(x); + //printf("Y IS: \n"); + //bigint_print(y); + if (bigint_cmp(y, one) == 0 && bigint_cmp(x, one) != 0 && bigint_cmp(x, n_minus_one) != 0) { + bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); + bigint_destroy(a); + // printf("failed first test\n"); + return bigint_prime(len); + } + bigint_destroy(x); + x = bigint_clone(y); + } + if (bigint_cmp(y, one) != 0) { + // printf("y is equal to %d and %d", y.data[1], y.data[0]); + // printf("failed second test\n"); + bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); + bigint_destroy(a); + return bigint_prime(len); + } } + bigint_destroy(a); + // for (int i = 0; i < 10000; i++) { + // printf("CHU A MON PRIME!!!\n"); + // } return n; } diff --git a/rsa/rsa.c b/rsa/rsa.c index 66c254b..623b244 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -3,11 +3,15 @@ rsa_t rsa_init(size_t len) { rsa_t rsa; -// rsa.p = bigint_prime(len / 2); -// rsa.q = bigint_prime(len / 2); + printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); + printf("Generating p...\n"); + rsa.p = bigint_prime(len / 2); + printf("p = %u\n", rsa.p.data[0]); + printf("Generating q...\n"); + rsa.q = bigint_prime(len / 2); + printf("q = %u\n", rsa.q.data[0]); + - rsa.p = bigint_zero(len / 2); - rsa.q = bigint_zero(len / 2); return rsa; } @@ -20,84 +24,21 @@ rsa_t rsa_generate_keys(size_t block_size) { a.data[0] = 1234567890; b.data[0] = 234567; - - printf("cmp a and b %d\n", bigint_cmp(a, b)); - - a.data[0] = 1234567890; -// b.data[0] = 1921572864; - - printf("cmp a and b %d\n", bigint_cmp(a, b)); - -/* - printf("cmp a and b %d\n", bigint_cmp(a, b)); - printf("cmp b and a %d\n", bigint_cmp(b, a)); - - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); - bigint_bitwise_left_shift(b); - printf("b %ud\n", b.data[0]); - printf("cmp a and b %d\n", bigint_cmp(a, b)); -*/ bigint_t result = assignable_bigint_modulo(a, b); printf("result is %ud\n", result.data[0]); + bigint_destroy(a); a = bigint_clone(result); b.data[0] = 5764; printf("length\na: %lu e: %lu n: %lu\n", result.len, a.len, b.len); bigint_t result2 = assignable_bigint_pow_mod(result, a, b); + bigint_destroy(a); + bigint_destroy(b); + bigint_destroy(result); printf("bigpowmod is %u \n", result2.data[0]); -/* result.data[0] = 8; - a.data[0] = 4; - result2 = assignable_bigint_mul(result, a); - printf("result2 is %u \n", result2.data[0]); - result.data[0] = 84; - a.data[0] = 463; - result2 = assignable_bigint_mul(result, a); - printf("result2 is %u \n", result2.data[0]); - bigint_add(result, a); - - printf("result2 is %u \n", result.data[0]); -*/ + bigint_destroy(result2); return rsa; } diff --git a/rsa/rsa.h b/rsa/rsa.h index 02860e4..fc29fca 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -9,7 +9,7 @@ #include #include -#define RSA_BLOCK_SIZE 2048 +#define RSA_BLOCK_SIZE 64 typedef struct bigint_s { uint32_t *data; @@ -42,6 +42,7 @@ void bigint_add(bigint_t a, bigint_t b); bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); +void bigint_set_zeros(bigint_t n); void bigint_destroy(bigint_t n); From 52f65086c2768f06672e31cbab61c7342ad2df93 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 16 Feb 2024 16:48:25 +0100 Subject: [PATCH 09/34] remove unused func and comments --- rsa/Makefile | 4 +- rsa/bigint.c | 174 +++++++++++---------------------------------------- rsa/rsa.c | 21 ------- 3 files changed, 40 insertions(+), 159 deletions(-) diff --git a/rsa/Makefile b/rsa/Makefile index ea760a3..16b5441 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -10,10 +10,10 @@ SRC = \ all: $(NAME) $(NAME): - gcc -Wall -Wextra -Werror $(SRC) -o $(NAME) + gcc -Wall -Wextra -Werror -Wunused-function $(SRC) -o $(NAME) fast: - gcc -Wall -Wextra -Werror -o3 $(SRC) -o $(NAME) + gcc -Wall -Wextra -Werror Wunused-function -o3 $(SRC) -o $(NAME) fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index 370796a..ab4d131 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -135,35 +135,6 @@ void bigint_substraction(bigint_t a, bigint_t bb) { bigint_destroy(zero); } -// TODO check opti -bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b) { - bigint_t result = bigint_clone(a); - bigint_t mod = bigint_clone(b); - if (a.len > b.len) { - mod = bigint_zero(a.len); - memcpy(mod.data, b.data, b.len * sizeof(uint32_t)); - } - if (bigint_cmp(result, b) == -1) { - bigint_destroy(mod); - return result; - } - bigint_bitwise_left_shift(mod); - while (bigint_cmp(b, mod) == -1) { - while (bigint_cmp(result, mod) == 1) { - bigint_bitwise_left_shift(mod); - } - bigint_bitwise_right_shift(mod); - if (bigint_cmp(result, mod) == 1) { - bigint_substraction(result, mod); - } - } - while (bigint_cmp(result, b) == 1) { - bigint_substraction(result, b); - } - bigint_destroy(mod); - return result; -} - // TODO check opti void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) { bigint_set_zeros(result); @@ -193,6 +164,41 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) { bigint_destroy(mod); } +bigint_t bigint_new(size_t len) { + bigint_t bigint; + + bigint.len = len; + bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t)); + + return bigint; +} + +bigint_t bigint_zero(size_t len) { + bigint_t bigint; + + bigint = bigint_new(len); + for (size_t i = 0; i < len; i++) { + bigint.data[i] = 0; + } + + return bigint; +} + +bigint_t bigint_clone(bigint_t src) { + bigint_t dst; + + dst.len = src.len; + dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t)); + memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); + + return dst; +} + +void bigint_destroy(bigint_t n) { + free(n.data); + n.data = NULL; +} + void bigint_add(bigint_t a, bigint_t b) { bigint_t result = bigint_zero(a.len); size_t size = sizeof(uint32_t) * 8; @@ -205,6 +211,7 @@ void bigint_add(bigint_t a, bigint_t b) { result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size); carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage); } +// memcpy(a.data, result.data, a.len * sizeof(uint32_t)); bigint_destroy(a); a = bigint_clone(result); bigint_destroy(result); @@ -216,28 +223,9 @@ void bigint_set_zeros(bigint_t n) { } } -bigint_t assignable_bigint_mul(bigint_t a, bigint_t b) { - bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - size_t size = sizeof(uint32_t) * 8; - size_t width = a.len * size; - - for (size_t cursor = 0; cursor < width; cursor++) { - if (a.data[cursor / 32] >> (cursor % 32) & 1) { - bigint_set_zeros(b_tool); - memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t)); - for (size_t i = 0; i < cursor; i++) { - bigint_bitwise_left_shift(b_tool); - } - bigint_add(result, b_tool); - } - } - bigint_destroy(b_tool); - return result; -} - void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { - bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + //bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t b_tool = bigint_zero(a.len + b.len); size_t size = sizeof(uint32_t) * 8; size_t width = a.len * size; bigint_set_zeros(result); @@ -280,35 +268,6 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, cursor -= 1; } } -// a^e mod n -// clean memory tricks !!! -bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) { - bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - size_t size = sizeof(uint32_t) * 8; - int cursor = e.len * size - 1; - while (!(e.data[cursor / 32] & 1 << (cursor % 32))) { - cursor--; - } - cursor--; - while (cursor >= 0) { - custom_bigint_mul(result, result, custom); - custom_bigint_modulo(custom, n, custom2); - bigint_set_zeros(result); - memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - if (e.data[cursor / 32] & 1 << (cursor % 32)) { - custom_bigint_mul(result, a, custom); - custom_bigint_modulo(custom, n, custom2); - memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - } - cursor -= 1; - } - bigint_destroy(custom); - bigint_destroy(custom2); - return result; -} void bigint_print(bigint_t n) { for (int i = n.len - 1; i >= 0; i--) { @@ -316,15 +275,6 @@ void bigint_print(bigint_t n) { } } -bigint_t bigint_clone(bigint_t src) { - bigint_t dst; - - dst.len = src.len; - dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t)); - memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); - - return dst; -} void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t one, bigint_t n_minus_two, bigint_t n_minus_one) { bigint_destroy(x); @@ -346,8 +296,6 @@ bigint_t bigint_prime(size_t len) { bigint_set_random_bytes(n); bigint_set_msb_and_lsb_to_one(n); - // printf("msb and lsb set to one N IS: \n"); - // bigint_print(n); bigint_t d = bigint_clone(n); d.data[0] -= 1; @@ -356,8 +304,6 @@ bigint_t bigint_prime(size_t len) { bigint_bitwise_right_shift(d); s += 1; } - // printf("D IS:\n"); - // bigint_print(d); bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); @@ -374,73 +320,29 @@ bigint_t bigint_prime(size_t len) { n_minus_one.data[0] -= 1; bigint_decrement(n_minus_two); bigint_t a = bigint_zero(len); - // printf("s is %d\n", s); for (uint32_t k = 0; k < 128; k++) { - // printf("this is good %d\n", k); - //bigint_t a = bigint_zero(len); bigint_set_zeros(a); while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) { bigint_set_random_bytes(a); } - //printf("A IS: \n"); - //bigint_print(a); - //printf("a %d\n", k); - //bigint_t x = custom_bigint_pow_mod(a, d, n, x, custom, custom2); custom_bigint_pow_mod(a, d, n, x, custom, custom2); - //printf("b %d\n", k); for (uint32_t i = 0; i < s; i++) { - //bigint_destroy(y); custom_bigint_pow_mod(x, two, n, y, custom, custom2); - //y = assignable_bigint_pow_mod(x, two, n); - //printf("X IS: \n"); - //bigint_print(x); - //printf("Y IS: \n"); - //bigint_print(y); if (bigint_cmp(y, one) == 0 && bigint_cmp(x, one) != 0 && bigint_cmp(x, n_minus_one) != 0) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(a); - // printf("failed first test\n"); return bigint_prime(len); } bigint_destroy(x); x = bigint_clone(y); } if (bigint_cmp(y, one) != 0) { - // printf("y is equal to %d and %d", y.data[1], y.data[0]); - // printf("failed second test\n"); bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(a); return bigint_prime(len); } } bigint_destroy(a); - // for (int i = 0; i < 10000; i++) { - // printf("CHU A MON PRIME!!!\n"); - // } return n; } -bigint_t bigint_new(size_t len) { - bigint_t bigint; - - bigint.len = len; - bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t)); - - return bigint; -} - -bigint_t bigint_zero(size_t len) { - bigint_t bigint; - - bigint = bigint_new(len); - for (size_t i = 0; i < len; i++) { - bigint.data[i] = 0; - } - - return bigint; -} - -void bigint_destroy(bigint_t n) { - free(n.data); - n.data = NULL; -} diff --git a/rsa/rsa.c b/rsa/rsa.c index 623b244..8a07142 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -19,26 +19,5 @@ rsa_t rsa_generate_keys(size_t block_size) { size_t len = block_size / sizeof(uint32_t) / 8; rsa_t rsa = rsa_init(len); - bigint_t a = bigint_zero(4); - bigint_t b = bigint_zero(4); - - a.data[0] = 1234567890; - b.data[0] = 234567; - bigint_t result = assignable_bigint_modulo(a, b); - - printf("result is %ud\n", result.data[0]); - - bigint_destroy(a); - a = bigint_clone(result); - b.data[0] = 5764; - - printf("length\na: %lu e: %lu n: %lu\n", result.len, a.len, b.len); - - bigint_t result2 = assignable_bigint_pow_mod(result, a, b); - bigint_destroy(a); - bigint_destroy(b); - bigint_destroy(result); - printf("bigpowmod is %u \n", result2.data[0]); - bigint_destroy(result2); return rsa; } From 4a3cbd75b57b2046721dd3f83b14c74f0644a13b Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 16 Feb 2024 17:33:46 +0100 Subject: [PATCH 10/34] fix: leaks --- rsa/bigint.c | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index ab4d131..dda3504 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -82,35 +82,11 @@ int bigint_cmp(bigint_t a, bigint_t b) { return 0; } -// TODO check opti -bigint_t assignable_bigint_substraction(bigint_t a, bigint_t b) { - if (a.len != b.len) { - printf("error: attempting to substract numbers of different length\n"); - exit(1); - } - - bigint_t result = bigint_clone(a); - bigint_t borrow = bigint_clone(b); - bigint_t y = bigint_clone(b); - bigint_t zero = bigint_zero(a.len); - while (bigint_cmp(borrow, zero)) { - for (size_t i = 0; i < a.len; i++) { - borrow.data[i] = ~result.data[i] & y.data[i]; - result.data[i] = result.data[i] ^ y.data[i]; - } - bigint_destroy(y); - y = assignable_bigint_bitwise_left_shift(borrow); - } - bigint_destroy(y); - bigint_destroy(borrow); - bigint_destroy(zero); - return result; -} - // TODO check opti void bigint_substraction(bigint_t a, bigint_t bb) { bigint_t b = bigint_clone(bb); if (a.len > bb.len) { + bigint_destroy(b); b = bigint_zero(a.len); memcpy(b.data, bb.data, b.len * sizeof(uint32_t)); } @@ -130,6 +106,7 @@ void bigint_substraction(bigint_t a, bigint_t bb) { bigint_destroy(y); y = assignable_bigint_bitwise_left_shift(borrow); } + bigint_destroy(b); bigint_destroy(y); bigint_destroy(borrow); bigint_destroy(zero); @@ -141,6 +118,7 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) { memcpy(result.data, a.data, a.len * sizeof(uint32_t)); bigint_t mod = bigint_clone(b); if (a.len > b.len) { + bigint_destroy(mod); mod = bigint_zero(a.len); memcpy(mod.data, b.data, b.len * sizeof(uint32_t)); } @@ -211,9 +189,7 @@ void bigint_add(bigint_t a, bigint_t b) { result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size); carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage); } -// memcpy(a.data, result.data, a.len * sizeof(uint32_t)); - bigint_destroy(a); - a = bigint_clone(result); + memcpy(a.data, result.data, a.len * sizeof(uint32_t)); bigint_destroy(result); } @@ -330,6 +306,8 @@ bigint_t bigint_prime(size_t len) { custom_bigint_pow_mod(x, two, n, y, custom, custom2); if (bigint_cmp(y, one) == 0 && bigint_cmp(x, one) != 0 && bigint_cmp(x, n_minus_one) != 0) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); + bigint_destroy(custom); + bigint_destroy(custom2); bigint_destroy(a); return bigint_prime(len); } @@ -338,10 +316,14 @@ bigint_t bigint_prime(size_t len) { } if (bigint_cmp(y, one) != 0) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); + bigint_destroy(custom); + bigint_destroy(custom2); bigint_destroy(a); return bigint_prime(len); } } + bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); + bigint_destroy(custom2); bigint_destroy(a); return n; } From 7d0c774cb76ffc8f34c17bd4e962748a027fea24 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sat, 17 Feb 2024 20:50:05 +0100 Subject: [PATCH 11/34] refacto: speed x10 --- rsa/Makefile | 5 ++++- rsa/bigint.c | 46 +++++++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/rsa/Makefile b/rsa/Makefile index 16b5441..dec020e 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -13,7 +13,10 @@ $(NAME): gcc -Wall -Wextra -Werror -Wunused-function $(SRC) -o $(NAME) fast: - gcc -Wall -Wextra -Werror Wunused-function -o3 $(SRC) -o $(NAME) + gcc -Wall -Wextra -Werror -Wunused-function -o3 $(SRC) -o $(NAME) + +profile: + gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index dda3504..1f78f25 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -55,12 +55,20 @@ int bigint_cmp(bigint_t a, bigint_t b) { uint32_t size = sizeof(uint32_t) * 8; uint32_t acursor = size * a.len - 1; uint32_t bcursor = size * b.len - 1; + while (acursor >= bcursor + size && a.data[acursor / size] == 0) { + acursor -= size; + } + while (acursor > bcursor) { if (a.data[acursor / size] & (1 << acursor % size)) { return 1; } acursor -= 1; } + while (bcursor >= acursor + size && b.data[bcursor / size] == 0) { + bcursor -= size; + } + while (bcursor > acursor) { if (b.data[bcursor / size] & (1 << bcursor % size)) { return -1; @@ -68,6 +76,9 @@ int bigint_cmp(bigint_t a, bigint_t b) { bcursor -= 1; } int cursor = acursor; + while (cursor >= 0 && a.data[cursor / size] == b.data[cursor / size]) { + cursor -= size; + } while (cursor >= 0) { uint32_t abit = a.data[cursor / size] & (1 << (cursor % size)); uint32_t bbit = b.data[cursor / size] & (1 << (cursor % size)); @@ -177,20 +188,20 @@ void bigint_destroy(bigint_t n) { n.data = NULL; } -void bigint_add(bigint_t a, bigint_t b) { - bigint_t result = bigint_zero(a.len); - size_t size = sizeof(uint32_t) * 8; - size_t width = a.len * size; +void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) { + //bigint_t result = bigint_zero(a.len); + bigint_set_zeros(result); + //size_t size = sizeof(uint32_t) * 8; + //size_t width = a.len * size; uint32_t carriage = 0; - for (size_t cursor = 0; cursor < width; cursor++) { - uint32_t a_bit = a.data[cursor / size] >> (cursor % size) & 1; - uint32_t b_bit = b.data[cursor / size] >> (cursor % size) & 1; - result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size); - carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage); + for (size_t cursor = 0; cursor < a.len; cursor++) { + uint64_t tmp = (uint64_t)a.data[cursor] + (uint64_t)b.data[cursor] + carriage; + memcpy(result.data + cursor, &tmp, sizeof(uint32_t)); + carriage = tmp >> 32; } memcpy(a.data, result.data, a.len * sizeof(uint32_t)); - bigint_destroy(result); + //bigint_destroy(result); } void bigint_set_zeros(bigint_t n) { @@ -199,7 +210,7 @@ void bigint_set_zeros(bigint_t n) { } } -void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { +void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t custom) { //bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t b_tool = bigint_zero(a.len + b.len); size_t size = sizeof(uint32_t) * 8; @@ -212,7 +223,7 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { for (size_t i = 0; i < cursor; i++) { bigint_bitwise_left_shift(b_tool); } - bigint_add(result, b_tool); + custom_bigint_add(result, b_tool, custom); } } bigint_destroy(b_tool); @@ -220,7 +231,7 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { // a^e mod n // clean memory tricks !!! -void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2) { +void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t custom3) { bigint_set_zeros(result); bigint_set_zeros(custom); bigint_set_zeros(custom2); @@ -232,12 +243,12 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, } cursor--; while (cursor >= 0) { - custom_bigint_mul(result, result, custom); + custom_bigint_mul(result, result, custom, custom3); custom_bigint_modulo(custom, n, custom2); bigint_set_zeros(result); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); if (e.data[cursor / 32] & 1 << (cursor % 32)) { - custom_bigint_mul(result, a, custom); + custom_bigint_mul(result, a, custom, custom3); custom_bigint_modulo(custom, n, custom2); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } @@ -285,6 +296,7 @@ bigint_t bigint_prime(size_t len) { bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t two = bigint_zero(len); two.data[0] = 2; @@ -301,9 +313,9 @@ bigint_t bigint_prime(size_t len) { while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) { bigint_set_random_bytes(a); } - custom_bigint_pow_mod(a, d, n, x, custom, custom2); + custom_bigint_pow_mod(a, d, n, x, custom, custom2, custom3); for (uint32_t i = 0; i < s; i++) { - custom_bigint_pow_mod(x, two, n, y, custom, custom2); + custom_bigint_pow_mod(x, two, n, y, custom, custom2, custom3); if (bigint_cmp(y, one) == 0 && bigint_cmp(x, one) != 0 && bigint_cmp(x, n_minus_one) != 0) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom); From 4c53350bd562147e9bf1f816a51b9d7fb3864799 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 00:46:21 +0100 Subject: [PATCH 12/34] refacto: faster --- rsa/Makefile | 3 +++ rsa/bigint.c | 47 +++++++++++++++++++++-------------------------- rsa/rsa.c | 19 ++++++++++++++----- rsa/rsa.h | 4 +++- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/rsa/Makefile b/rsa/Makefile index dec020e..08a0c23 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -18,6 +18,9 @@ fast: profile: gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) +profile-fast: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) + fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index 1f78f25..e27ed6a 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -55,12 +55,12 @@ int bigint_cmp(bigint_t a, bigint_t b) { uint32_t size = sizeof(uint32_t) * 8; uint32_t acursor = size * a.len - 1; uint32_t bcursor = size * b.len - 1; - while (acursor >= bcursor + size && a.data[acursor / size] == 0) { + while (acursor >= bcursor + size && a.data[acursor >> 5] == 0) { acursor -= size; } while (acursor > bcursor) { - if (a.data[acursor / size] & (1 << acursor % size)) { + if (a.data[acursor >> 5] & (1 << acursor % size)) { return 1; } acursor -= 1; @@ -70,26 +70,22 @@ int bigint_cmp(bigint_t a, bigint_t b) { } while (bcursor > acursor) { - if (b.data[bcursor / size] & (1 << bcursor % size)) { + if (b.data[bcursor >> 5] & (1 << bcursor % size)) { return -1; } bcursor -= 1; } int cursor = acursor; - while (cursor >= 0 && a.data[cursor / size] == b.data[cursor / size]) { - cursor -= size; - } while (cursor >= 0) { - uint32_t abit = a.data[cursor / size] & (1 << (cursor % size)); - uint32_t bbit = b.data[cursor / size] & (1 << (cursor % size)); - if (abit > bbit) { + if (a.data[cursor >> 5] > b.data[cursor >> 5]) { return 1; } - if (bbit > abit) { + if (b.data[cursor >> 5] > a.data[cursor >> 5]) { return -1; } - cursor -= 1; + cursor -= size; } + return 0; } @@ -189,10 +185,7 @@ void bigint_destroy(bigint_t n) { } void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) { - //bigint_t result = bigint_zero(a.len); bigint_set_zeros(result); - //size_t size = sizeof(uint32_t) * 8; - //size_t width = a.len * size; uint32_t carriage = 0; for (size_t cursor = 0; cursor < a.len; cursor++) { @@ -201,7 +194,6 @@ void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) { carriage = tmp >> 32; } memcpy(a.data, result.data, a.len * sizeof(uint32_t)); - //bigint_destroy(result); } void bigint_set_zeros(bigint_t n) { @@ -213,15 +205,18 @@ void bigint_set_zeros(bigint_t n) { void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t custom) { //bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t b_tool = bigint_zero(a.len + b.len); + //memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t)); size_t size = sizeof(uint32_t) * 8; - size_t width = a.len * size; + int width = a.len * size; bigint_set_zeros(result); - for (size_t cursor = 0; cursor < width; cursor++) { - if (a.data[cursor / 32] >> (cursor % 32) & 1) { + for (int cursor = 0; cursor < width; cursor++) { + if (a.data[cursor >> 5] >> (cursor % 32) & 1) { bigint_set_zeros(b_tool); - memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t)); - for (size_t i = 0; i < cursor; i++) { + memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t)); + int i = cursor - cursor % 32; + while (i < cursor) { bigint_bitwise_left_shift(b_tool); + i++; } custom_bigint_add(result, b_tool, custom); } @@ -277,7 +272,7 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t bigint_random_range(bigint_t low, bigint_t high) { } - */ + */ bigint_t bigint_prime(size_t len) { bigint_t n = bigint_zero(len); @@ -292,11 +287,11 @@ bigint_t bigint_prime(size_t len) { s += 1; } - bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t two = bigint_zero(len); two.data[0] = 2; diff --git a/rsa/rsa.c b/rsa/rsa.c index 8a07142..94b47eb 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -4,12 +4,14 @@ rsa_t rsa_init(size_t len) { rsa_t rsa; printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); - printf("Generating p...\n"); + //printf("Generating p...\n"); rsa.p = bigint_prime(len / 2); - printf("p = %u\n", rsa.p.data[0]); - printf("Generating q...\n"); + printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); + //printf("p = %u\n", rsa.p.data[0]); + //printf("Generating q...\n"); rsa.q = bigint_prime(len / 2); - printf("q = %u\n", rsa.q.data[0]); + printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); + //printf("q = %u\n", rsa.q.data[0]); return rsa; @@ -18,6 +20,13 @@ rsa_t rsa_init(size_t len) { rsa_t rsa_generate_keys(size_t block_size) { size_t len = block_size / sizeof(uint32_t) / 8; rsa_t rsa = rsa_init(len); - + bigint_destroy(rsa.p); + bigint_destroy(rsa.q); + for (int i = 0; i < 18; i++) { + bigint_t p = bigint_prime(len / 2); + printf("%lu\n", ((uint64_t)p.data[1] << 32) + (uint64_t)p.data[0]); + bigint_destroy(p); + } return rsa; } + diff --git a/rsa/rsa.h b/rsa/rsa.h index fc29fca..b4b4485 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -9,7 +9,7 @@ #include #include -#define RSA_BLOCK_SIZE 64 +#define RSA_BLOCK_SIZE 128 typedef struct bigint_s { uint32_t *data; @@ -39,6 +39,7 @@ bigint_t bigint_new(size_t len); bigint_t bigint_zero(size_t len); bigint_t bigint_clone(bigint_t src); void bigint_add(bigint_t a, bigint_t b); +void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result); bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); @@ -53,3 +54,4 @@ void array_bitwise_right_shift(uint32_t *a, size_t len); void array_decrement(uint32_t *a, size_t len); #endif + From 585f9750dfbc26e790bb78e082adb8bfa2a0fb7f Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 00:46:50 +0100 Subject: [PATCH 13/34] fix: put correct flag for opti................. --- rsa/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsa/Makefile b/rsa/Makefile index 08a0c23..a280052 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -13,7 +13,7 @@ $(NAME): gcc -Wall -Wextra -Werror -Wunused-function $(SRC) -o $(NAME) fast: - gcc -Wall -Wextra -Werror -Wunused-function -o3 $(SRC) -o $(NAME) + gcc -Wall -Wextra -Werror -Wunused-function -O3 $(SRC) -o $(NAME) profile: gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) From ed45a04df246469b4b90b5b05ff13de483e9b751 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 02:17:52 +0100 Subject: [PATCH 14/34] chore: PHONY rule --- rsa/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rsa/Makefile b/rsa/Makefile index a280052..1d3819d 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -25,3 +25,5 @@ fclean: rm -rf $(NAME) re: fclean all + +.PHONY: all fast profile profile-fast fclean re From fa5c3d7f96383a42fa9c4b6331e41dbff980a306 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 02:41:48 +0100 Subject: [PATCH 15/34] refacto: make more numbers same length --- rsa/bigint.c | 102 +++++++++++++++++++++------------------------------ rsa/rsa.h | 6 +-- 2 files changed, 45 insertions(+), 63 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index e27ed6a..9fbc592 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -1,14 +1,14 @@ #include "rsa.h" -void bigint_set_random_bytes(bigint_t n) { +void bigint_set_random_bytes(bigint_t n, size_t len) { int fd = open("/dev/urandom", O_RDONLY); - read(fd, n.data, n.len * sizeof(uint32_t)); + read(fd, n.data, len * sizeof(uint32_t)); close(fd); } -void bigint_set_msb_and_lsb_to_one(bigint_t n) { +void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) { n.data[0] |= 1; - n.data[n.len - 1] |= 1 << 31; + n.data[len - 1] |= 1 << 31; } void bigint_bitwise_right_shift(bigint_t n) { @@ -51,61 +51,39 @@ void bigint_decrement(bigint_t n) { } // TODO refactor/clean assume same length ? -int bigint_cmp(bigint_t a, bigint_t b) { - uint32_t size = sizeof(uint32_t) * 8; - uint32_t acursor = size * a.len - 1; - uint32_t bcursor = size * b.len - 1; - while (acursor >= bcursor + size && a.data[acursor >> 5] == 0) { - acursor -= size; - } - - while (acursor > bcursor) { - if (a.data[acursor >> 5] & (1 << acursor % size)) { - return 1; - } - acursor -= 1; - } - while (bcursor >= acursor + size && b.data[bcursor / size] == 0) { - bcursor -= size; - } - - while (bcursor > acursor) { - if (b.data[bcursor >> 5] & (1 << bcursor % size)) { - return -1; - } - bcursor -= 1; - } - int cursor = acursor; +int64_t bigint_cmp(bigint_t a, bigint_t b) { + int cursor = a.len - 1; while (cursor >= 0) { - if (a.data[cursor >> 5] > b.data[cursor >> 5]) { + if (a.data[cursor] > b.data[cursor]) { return 1; } - if (b.data[cursor >> 5] > a.data[cursor >> 5]) { + if (b.data[cursor] > a.data[cursor]) { return -1; } - cursor -= size; + cursor -= 1; } return 0; } -// TODO check opti -void bigint_substraction(bigint_t a, bigint_t bb) { - bigint_t b = bigint_clone(bb); - if (a.len > bb.len) { - bigint_destroy(b); - b = bigint_zero(a.len); - memcpy(b.data, bb.data, b.len * sizeof(uint32_t)); - } - if (a.len != b.len) { - printf("error: attempting to substract numbers of different length\n"); - exit(1); +// TODO refactor/clean assume same length ? +int bigint_dif(bigint_t a, bigint_t b) { + int cursor = a.len - 1; + while (cursor >= 0) { + if (a.data[cursor] ^ b.data[cursor]) { + return 1; + } + cursor -= 1; } + return 0; +} +// TODO check opti +void bigint_substraction(bigint_t a, bigint_t b) { bigint_t borrow = bigint_clone(b); bigint_t y = bigint_clone(b); bigint_t zero = bigint_zero(a.len); - while (bigint_cmp(borrow, zero)) { + while (bigint_dif(borrow, zero)) { for (size_t i = 0; i < a.len; i++) { borrow.data[i] = ~a.data[i] & y.data[i]; a.data[i] = a.data[i] ^ y.data[i]; @@ -113,7 +91,6 @@ void bigint_substraction(bigint_t a, bigint_t bb) { bigint_destroy(y); y = assignable_bigint_bitwise_left_shift(borrow); } - bigint_destroy(b); bigint_destroy(y); bigint_destroy(borrow); bigint_destroy(zero); @@ -129,21 +106,21 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) { mod = bigint_zero(a.len); memcpy(mod.data, b.data, b.len * sizeof(uint32_t)); } - if (bigint_cmp(result, b) == -1) { + if (bigint_cmp(result, b) < 0) { bigint_destroy(mod); return ; } bigint_bitwise_left_shift(mod); - while (bigint_cmp(b, mod) == -1) { - while (bigint_cmp(result, mod) == 1) { + while (bigint_cmp(b, mod) < 0) { + while (bigint_cmp(result, mod) > 0) { bigint_bitwise_left_shift(mod); } bigint_bitwise_right_shift(mod); - if (bigint_cmp(result, mod) == 1) { + if (bigint_cmp(result, mod) > 0) { bigint_substraction(result, mod); } } - while (bigint_cmp(result, b) == 1) { + while (bigint_cmp(result, b) > 0) { bigint_substraction(result, b); } bigint_destroy(mod); @@ -274,10 +251,11 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, } */ bigint_t bigint_prime(size_t len) { - bigint_t n = bigint_zero(len); + //bigint_t n = bigint_zero(len); + bigint_t n = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_set_random_bytes(n); - bigint_set_msb_and_lsb_to_one(n); + bigint_set_random_bytes(n, len); + bigint_set_msb_and_lsb_to_one(n, len); bigint_t d = bigint_clone(n); d.data[0] -= 1; @@ -293,44 +271,48 @@ bigint_t bigint_prime(size_t len) { bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t two = bigint_zero(len); + bigint_t two = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t one = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); two.data[0] = 2; - bigint_t one = bigint_zero(len); one.data[0] = 1; bigint_t n_minus_two = bigint_clone(n); bigint_t n_minus_one = bigint_clone(n); n_minus_two.data[0] -= 1; n_minus_one.data[0] -= 1; bigint_decrement(n_minus_two); - bigint_t a = bigint_zero(len); + //bigint_t a = bigint_zero(len); + bigint_t a = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); for (uint32_t k = 0; k < 128; k++) { bigint_set_zeros(a); - while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) { - bigint_set_random_bytes(a); + while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { + bigint_set_random_bytes(a, len); } custom_bigint_pow_mod(a, d, n, x, custom, custom2, custom3); for (uint32_t i = 0; i < s; i++) { custom_bigint_pow_mod(x, two, n, y, custom, custom2, custom3); - if (bigint_cmp(y, one) == 0 && bigint_cmp(x, one) != 0 && bigint_cmp(x, n_minus_one) != 0) { + if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom); bigint_destroy(custom2); + bigint_destroy(custom3); bigint_destroy(a); return bigint_prime(len); } bigint_destroy(x); x = bigint_clone(y); } - if (bigint_cmp(y, one) != 0) { + if (bigint_dif(y, one)) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom); bigint_destroy(custom2); + bigint_destroy(custom3); bigint_destroy(a); return bigint_prime(len); } } bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom2); + bigint_destroy(custom3); bigint_destroy(a); return n; } diff --git a/rsa/rsa.h b/rsa/rsa.h index b4b4485..0847a2b 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -27,12 +27,12 @@ void *protected_malloc(size_t size); rsa_t rsa_generate_keys(size_t block_size); -void bigint_set_random_bytes(bigint_t n); -void bigint_set_msb_and_lsb_to_one(bigint_t n); +void bigint_set_random_bytes(bigint_t n, size_t len); +void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len); void bigint_bitwise_left_shift(bigint_t n); void bigint_bitwise_right_shift(bigint_t n); void bigint_decrement(bigint_t n); -int bigint_cmp(bigint_t a, bigint_t b); +int64_t bigint_cmp(bigint_t a, bigint_t b); bigint_t bigint_prime(size_t len); void bigint_print(bigint_t n); bigint_t bigint_new(size_t len); From c8e5e6bf671118a566482c92ae7fda898a47eb50 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 14:16:28 +0100 Subject: [PATCH 16/34] opti: cache left_shifts in multiplication --- rsa/bigint.c | 65 +++++++++++++++++++++++++++------------------------- rsa/rsa.h | 4 ++-- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 9fbc592..cfae50d 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -161,16 +161,17 @@ void bigint_destroy(bigint_t n) { n.data = NULL; } -void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) { - bigint_set_zeros(result); - uint32_t carriage = 0; +void custom_bigint_add(bigint_t a, bigint_t b, int index) { + uint64_t carriage = 0; for (size_t cursor = 0; cursor < a.len; cursor++) { - uint64_t tmp = (uint64_t)a.data[cursor] + (uint64_t)b.data[cursor] + carriage; - memcpy(result.data + cursor, &tmp, sizeof(uint32_t)); + uint64_t tmp = (uint64_t)a.data[cursor] + carriage; + if ((int)cursor - index >= 0) { + tmp += (uint64_t)b.data[cursor - index]; + } + a.data[cursor] = (uint32_t)tmp; carriage = tmp >> 32; } - memcpy(a.data, result.data, a.len * sizeof(uint32_t)); } void bigint_set_zeros(bigint_t n) { @@ -179,31 +180,37 @@ void bigint_set_zeros(bigint_t n) { } } -void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t custom) { - //bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t b_tool = bigint_zero(a.len + b.len); - //memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t)); - size_t size = sizeof(uint32_t) * 8; - int width = a.len * size; +void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { + int width = a.len * 32; bigint_set_zeros(result); + bigint_t *b_tool = (bigint_t *)malloc(32 * sizeof(bigint_t)); + + b_tool[0] = bigint_zero(a.len + b.len); + bigint_set_zeros(b_tool[0]); + memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); + + for (int i = 1; i < 32; i++) { + b_tool[i] = bigint_zero(a.len + b.len); + bigint_set_zeros(b_tool[i]); + memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); + bigint_bitwise_left_shift(b_tool[i]); + } + for (int cursor = 0; cursor < width; cursor++) { - if (a.data[cursor >> 5] >> (cursor % 32) & 1) { - bigint_set_zeros(b_tool); - memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t)); - int i = cursor - cursor % 32; - while (i < cursor) { - bigint_bitwise_left_shift(b_tool); - i++; - } - custom_bigint_add(result, b_tool, custom); + int offset = cursor % 32; + int index = cursor >> 5; + if (a.data[index] >> offset & 1) { + custom_bigint_add(result, b_tool[offset], index); } } - bigint_destroy(b_tool); + for (int i = 0; i < 32; i++) { + bigint_destroy(b_tool[i]); + } } // a^e mod n // clean memory tricks !!! -void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t custom3) { +void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2) { bigint_set_zeros(result); bigint_set_zeros(custom); bigint_set_zeros(custom2); @@ -215,12 +222,12 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, } cursor--; while (cursor >= 0) { - custom_bigint_mul(result, result, custom, custom3); + custom_bigint_mul(result, result, custom); custom_bigint_modulo(custom, n, custom2); bigint_set_zeros(result); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); if (e.data[cursor / 32] & 1 << (cursor % 32)) { - custom_bigint_mul(result, a, custom, custom3); + custom_bigint_mul(result, a, custom); custom_bigint_modulo(custom, n, custom2); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } @@ -269,7 +276,6 @@ bigint_t bigint_prime(size_t len) { bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t two = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t one = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); @@ -287,14 +293,13 @@ bigint_t bigint_prime(size_t len) { while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { bigint_set_random_bytes(a, len); } - custom_bigint_pow_mod(a, d, n, x, custom, custom2, custom3); + custom_bigint_pow_mod(a, d, n, x, custom, custom2); for (uint32_t i = 0; i < s; i++) { - custom_bigint_pow_mod(x, two, n, y, custom, custom2, custom3); + custom_bigint_pow_mod(x, two, n, y, custom, custom2); if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom); bigint_destroy(custom2); - bigint_destroy(custom3); bigint_destroy(a); return bigint_prime(len); } @@ -305,14 +310,12 @@ bigint_t bigint_prime(size_t len) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom); bigint_destroy(custom2); - bigint_destroy(custom3); bigint_destroy(a); return bigint_prime(len); } } bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom2); - bigint_destroy(custom3); bigint_destroy(a); return n; } diff --git a/rsa/rsa.h b/rsa/rsa.h index 0847a2b..ce55a92 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -9,7 +9,7 @@ #include #include -#define RSA_BLOCK_SIZE 128 +#define RSA_BLOCK_SIZE 256 typedef struct bigint_s { uint32_t *data; @@ -39,7 +39,7 @@ bigint_t bigint_new(size_t len); bigint_t bigint_zero(size_t len); bigint_t bigint_clone(bigint_t src); void bigint_add(bigint_t a, bigint_t b); -void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result); +void custom_bigint_add(bigint_t a, bigint_t b, int index); bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); From 2f7d2922c7cd68db18170c3da06a67ec19c0ab7f Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 15:37:37 +0100 Subject: [PATCH 17/34] fix: substraction --- rsa/Makefile | 6 ++++++ rsa/bigint.c | 28 +++++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/rsa/Makefile b/rsa/Makefile index 1d3819d..80cf2bf 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -18,9 +18,15 @@ fast: profile: gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) +profile-clang: + clang -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) + profile-fast: gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) +profile-fast-clang: + clang -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) + fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index cfae50d..6d75a7d 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -68,29 +68,35 @@ int64_t bigint_cmp(bigint_t a, bigint_t b) { // TODO refactor/clean assume same length ? int bigint_dif(bigint_t a, bigint_t b) { - int cursor = a.len - 1; - while (cursor >= 0) { + int cursor = a.len; + while (--cursor >= 0) { if (a.data[cursor] ^ b.data[cursor]) { return 1; } - cursor -= 1; + //cursor -= 1; } return 0; } + +void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) { + while (bigint_dif(*y, zero)) { + for (size_t i = 0; i < a.len; i++) { + borrow.data[i] = ~a.data[i] & y->data[i]; + a.data[i] = a.data[i] ^ y->data[i]; + } + bigint_destroy(*y); + *y = assignable_bigint_bitwise_left_shift(borrow); + } + +} + // TODO check opti void bigint_substraction(bigint_t a, bigint_t b) { bigint_t borrow = bigint_clone(b); bigint_t y = bigint_clone(b); bigint_t zero = bigint_zero(a.len); - while (bigint_dif(borrow, zero)) { - for (size_t i = 0; i < a.len; i++) { - borrow.data[i] = ~a.data[i] & y.data[i]; - a.data[i] = a.data[i] ^ y.data[i]; - } - bigint_destroy(y); - y = assignable_bigint_bitwise_left_shift(borrow); - } + tool(borrow, &y, zero, a); bigint_destroy(y); bigint_destroy(borrow); bigint_destroy(zero); From d368c925fb4588dd8227217322a250be28b20360 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 15:55:05 +0100 Subject: [PATCH 18/34] opti: is_not_zero --- rsa/bigint.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 6d75a7d..1f45b5a 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -79,8 +79,17 @@ int bigint_dif(bigint_t a, bigint_t b) { return 0; } -void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) { - while (bigint_dif(*y, zero)) { +int is_not_zero(bigint_t n) { + for (size_t i = 0; i < n.len; i++) { + if (n.data[i]) { + return 1; + } + } + return 0; +} + +void tool(bigint_t borrow, bigint_t *y, bigint_t a) { + while (is_not_zero(*y)) { for (size_t i = 0; i < a.len; i++) { borrow.data[i] = ~a.data[i] & y->data[i]; a.data[i] = a.data[i] ^ y->data[i]; @@ -88,18 +97,15 @@ void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) { bigint_destroy(*y); *y = assignable_bigint_bitwise_left_shift(borrow); } - } // TODO check opti void bigint_substraction(bigint_t a, bigint_t b) { bigint_t borrow = bigint_clone(b); bigint_t y = bigint_clone(b); - bigint_t zero = bigint_zero(a.len); - tool(borrow, &y, zero, a); + tool(borrow, &y, a); bigint_destroy(y); bigint_destroy(borrow); - bigint_destroy(zero); } // TODO check opti From 81f8fb3c1f5998dcca01f0a64969ed2de7c0e5a6 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 16:38:30 +0100 Subject: [PATCH 19/34] opti: remove many mallocs --- rsa/bigint.c | 65 ++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 1f45b5a..f78c5cd 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -27,14 +27,13 @@ void bigint_bitwise_left_shift(bigint_t n) { n.data[0] <<= 1; } -bigint_t assignable_bigint_bitwise_left_shift(bigint_t n) { - bigint_t result = bigint_clone(n); +void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) { + memcpy(result.data, n.data, n.len * sizeof(uint32_t)); size_t size = sizeof(uint32_t) * 8 - 1; for (int i = result.len - 1; i > 0; i--) { result.data[i] = result.data[i] << 1 | ((result.data[i - 1] & (1 << size)) >> size); } result.data[0] <<= 1; - return result; } // Will underflow @@ -88,38 +87,36 @@ int is_not_zero(bigint_t n) { return 0; } -void tool(bigint_t borrow, bigint_t *y, bigint_t a) { - while (is_not_zero(*y)) { +// TODO check opti +void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { + //bigint_t borrow = bigint_clone(b); + //bigint_t y = bigint_clone(b); + memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); + memcpy(y.data, b.data, b.len * sizeof(uint32_t)); + while (is_not_zero(y)) { for (size_t i = 0; i < a.len; i++) { - borrow.data[i] = ~a.data[i] & y->data[i]; - a.data[i] = a.data[i] ^ y->data[i]; + borrow.data[i] = ~a.data[i] & y.data[i]; + a.data[i] = a.data[i] ^ y.data[i]; } - bigint_destroy(*y); - *y = assignable_bigint_bitwise_left_shift(borrow); + move_bigint_bitwise_left_shift(borrow, y); } + //bigint_destroy(y); + //bigint_destroy(borrow); } // TODO check opti -void bigint_substraction(bigint_t a, bigint_t b) { - bigint_t borrow = bigint_clone(b); - bigint_t y = bigint_clone(b); - tool(borrow, &y, a); - bigint_destroy(y); - bigint_destroy(borrow); -} - -// TODO check opti -void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) { +void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { bigint_set_zeros(result); memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - bigint_t mod = bigint_clone(b); - if (a.len > b.len) { - bigint_destroy(mod); + //bigint_t mod = bigint_clone(b); + /*if (a.len > b.len) { + printf("useless\n"); + //bigint_destroy(mod); mod = bigint_zero(a.len); memcpy(mod.data, b.data, b.len * sizeof(uint32_t)); - } + }*/ if (bigint_cmp(result, b) < 0) { - bigint_destroy(mod); + //bigint_destroy(mod); return ; } bigint_bitwise_left_shift(mod); @@ -129,13 +126,13 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) { } bigint_bitwise_right_shift(mod); if (bigint_cmp(result, mod) > 0) { - bigint_substraction(result, mod); + bigint_substraction(result, mod, borrow_sub, y_sub); } } while (bigint_cmp(result, b) > 0) { - bigint_substraction(result, b); + bigint_substraction(result, b, borrow_sub, y_sub); } - bigint_destroy(mod); + //bigint_destroy(mod); } bigint_t bigint_new(size_t len) { @@ -222,7 +219,7 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { // a^e mod n // clean memory tricks !!! -void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2) { +void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { bigint_set_zeros(result); bigint_set_zeros(custom); bigint_set_zeros(custom2); @@ -235,12 +232,12 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, cursor--; while (cursor >= 0) { custom_bigint_mul(result, result, custom); - custom_bigint_modulo(custom, n, custom2); + custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); bigint_set_zeros(result); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); if (e.data[cursor / 32] & 1 << (cursor % 32)) { custom_bigint_mul(result, a, custom); - custom_bigint_modulo(custom, n, custom2); + custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } cursor -= 1; @@ -270,12 +267,14 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, } */ bigint_t bigint_prime(size_t len) { - //bigint_t n = bigint_zero(len); bigint_t n = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_set_random_bytes(n, len); bigint_set_msb_and_lsb_to_one(n, len); + bigint_t mod = bigint_clone(n); + bigint_t borrow_sub = bigint_clone(n); + bigint_t y_sub = bigint_clone(n); bigint_t d = bigint_clone(n); d.data[0] -= 1; uint32_t s = 0; @@ -305,9 +304,9 @@ bigint_t bigint_prime(size_t len) { while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { bigint_set_random_bytes(a, len); } - custom_bigint_pow_mod(a, d, n, x, custom, custom2); + custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub); for (uint32_t i = 0; i < s; i++) { - custom_bigint_pow_mod(x, two, n, y, custom, custom2); + custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub); if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom); From 365c98be7c269b7479a8f38b94daf3a61740f082 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 16:40:16 +0100 Subject: [PATCH 20/34] clean: remove comments --- rsa/bigint.c | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index f78c5cd..58e2e19 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -72,7 +72,6 @@ int bigint_dif(bigint_t a, bigint_t b) { if (a.data[cursor] ^ b.data[cursor]) { return 1; } - //cursor -= 1; } return 0; @@ -89,8 +88,6 @@ int is_not_zero(bigint_t n) { // TODO check opti void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { - //bigint_t borrow = bigint_clone(b); - //bigint_t y = bigint_clone(b); memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); memcpy(y.data, b.data, b.len * sizeof(uint32_t)); while (is_not_zero(y)) { @@ -100,23 +97,13 @@ void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { } move_bigint_bitwise_left_shift(borrow, y); } - //bigint_destroy(y); - //bigint_destroy(borrow); } // TODO check opti void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { bigint_set_zeros(result); memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - //bigint_t mod = bigint_clone(b); - /*if (a.len > b.len) { - printf("useless\n"); - //bigint_destroy(mod); - mod = bigint_zero(a.len); - memcpy(mod.data, b.data, b.len * sizeof(uint32_t)); - }*/ if (bigint_cmp(result, b) < 0) { - //bigint_destroy(mod); return ; } bigint_bitwise_left_shift(mod); @@ -132,7 +119,6 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, while (bigint_cmp(result, b) > 0) { bigint_substraction(result, b, borrow_sub, y_sub); } - //bigint_destroy(mod); } bigint_t bigint_new(size_t len) { @@ -261,11 +247,7 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_destroy(n_minus_two); bigint_destroy(n_minus_one); } -/* - bigint_t bigint_random_range(bigint_t low, bigint_t high) { - } - */ bigint_t bigint_prime(size_t len) { bigint_t n = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); @@ -297,7 +279,6 @@ bigint_t bigint_prime(size_t len) { n_minus_two.data[0] -= 1; n_minus_one.data[0] -= 1; bigint_decrement(n_minus_two); - //bigint_t a = bigint_zero(len); bigint_t a = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); for (uint32_t k = 0; k < 128; k++) { bigint_set_zeros(a); From 1d9e2936ac4c00b6227fa3c04e694049b104c67e Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 17:14:48 +0100 Subject: [PATCH 21/34] opti: rm malloc in mul and useless cmp in modulo --- rsa/bigint.c | 53 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 58e2e19..4ecc0d6 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -106,11 +106,10 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, if (bigint_cmp(result, b) < 0) { return ; } - bigint_bitwise_left_shift(mod); + while (bigint_cmp(result, mod) > 0) { + bigint_bitwise_left_shift(mod); + } while (bigint_cmp(b, mod) < 0) { - while (bigint_cmp(result, mod) > 0) { - bigint_bitwise_left_shift(mod); - } bigint_bitwise_right_shift(mod); if (bigint_cmp(result, mod) > 0) { bigint_substraction(result, mod, borrow_sub, y_sub); @@ -175,17 +174,13 @@ void bigint_set_zeros(bigint_t n) { } } -void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { +void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t *b_tool) { int width = a.len * 32; bigint_set_zeros(result); - bigint_t *b_tool = (bigint_t *)malloc(32 * sizeof(bigint_t)); - - b_tool[0] = bigint_zero(a.len + b.len); bigint_set_zeros(b_tool[0]); memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); for (int i = 1; i < 32; i++) { - b_tool[i] = bigint_zero(a.len + b.len); bigint_set_zeros(b_tool[i]); memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); bigint_bitwise_left_shift(b_tool[i]); @@ -198,14 +193,11 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) { custom_bigint_add(result, b_tool[offset], index); } } - for (int i = 0; i < 32; i++) { - bigint_destroy(b_tool[i]); - } } // a^e mod n // clean memory tricks !!! -void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { +void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool) { bigint_set_zeros(result); bigint_set_zeros(custom); bigint_set_zeros(custom2); @@ -217,12 +209,12 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, } cursor--; while (cursor >= 0) { - custom_bigint_mul(result, result, custom); + custom_bigint_mul(result, result, custom, b_tool); custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); bigint_set_zeros(result); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); if (e.data[cursor / 32] & 1 << (cursor % 32)) { - custom_bigint_mul(result, a, custom); + custom_bigint_mul(result, a, custom, b_tool); custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } @@ -269,6 +261,12 @@ bigint_t bigint_prime(size_t len) { bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); + + for (int i = 0; i < 32; i++) { + b_tool[i] = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + } + bigint_t two = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t one = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); @@ -285,14 +283,21 @@ bigint_t bigint_prime(size_t len) { while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { bigint_set_random_bytes(a, len); } - custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub); + custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool); for (uint32_t i = 0; i < s; i++) { - custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub); + custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool); if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom); bigint_destroy(custom2); bigint_destroy(a); + bigint_destroy(mod); + bigint_destroy(borrow_sub); + bigint_destroy(y_sub); + for (int i = 0; i < 32; i++) { + bigint_destroy(b_tool[i]); + } + free(b_tool); return bigint_prime(len); } bigint_destroy(x); @@ -303,12 +308,26 @@ bigint_t bigint_prime(size_t len) { bigint_destroy(custom); bigint_destroy(custom2); bigint_destroy(a); + bigint_destroy(mod); + bigint_destroy(borrow_sub); + bigint_destroy(y_sub); + for (int i = 0; i < 32; i++) { + bigint_destroy(b_tool[i]); + } + free(b_tool); return bigint_prime(len); } } bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom2); bigint_destroy(a); + bigint_destroy(mod); + bigint_destroy(borrow_sub); + bigint_destroy(y_sub); + for (int i = 0; i < 32; i++) { + bigint_destroy(b_tool[i]); + } + free(b_tool); return n; } From badc3fd39998660b1baed708614794adb3e098ce Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 18:06:18 +0100 Subject: [PATCH 22/34] opti: remove many memcpy --- rsa/bigint.c | 69 ++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 4ecc0d6..25496a4 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -1,5 +1,9 @@ #include "rsa.h" +void my_memcpy(void *dst, void *src, size_t n) { + memcpy(dst, src, n); +} + void bigint_set_random_bytes(bigint_t n, size_t len) { int fd = open("/dev/urandom", O_RDONLY); read(fd, n.data, len * sizeof(uint32_t)); @@ -12,37 +16,33 @@ void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) { } void bigint_bitwise_right_shift(bigint_t n) { - size_t size = sizeof(uint32_t) * 8 - 1; for (size_t i = 0; i < n.len - 1; i++) { - n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << size; + n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << 31; } n.data[n.len - 1] >>= 1; } void bigint_bitwise_left_shift(bigint_t n) { - size_t size = sizeof(uint32_t) * 8 - 1; for (int i = n.len - 1; i > 0; i--) { - n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << size)) >> size); + n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); } n.data[0] <<= 1; } void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) { - memcpy(result.data, n.data, n.len * sizeof(uint32_t)); - size_t size = sizeof(uint32_t) * 8 - 1; +// my_memcpy(result.data, n.data, n.len * sizeof(uint32_t)); for (int i = result.len - 1; i > 0; i--) { - result.data[i] = result.data[i] << 1 | ((result.data[i - 1] & (1 << size)) >> size); + result.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); } - result.data[0] <<= 1; + result.data[0] = n.data[0] << 1; } // Will underflow void bigint_decrement(bigint_t n) { size_t cursor = 0; - size_t size = sizeof(uint32_t) * 8; - while (cursor < size * n.len) { - n.data[cursor / size] = n.data[cursor / size] ^ (1 << (cursor % size)); - if (((n.data[cursor / size] >> (cursor % size)) & 1) == 0) { + while (cursor < n.len << 5) { + n.data[cursor >> 32] = n.data[cursor >> 5] ^ (1 << (cursor % 32)); + if (((n.data[cursor >> 5] >> (cursor % 32)) & 1) == 0) { return; } cursor += 1; @@ -88,8 +88,8 @@ int is_not_zero(bigint_t n) { // TODO check opti void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { - memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); - memcpy(y.data, b.data, b.len * sizeof(uint32_t)); + //my_memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); + my_memcpy(y.data, b.data, b.len * sizeof(uint32_t)); while (is_not_zero(y)) { for (size_t i = 0; i < a.len; i++) { borrow.data[i] = ~a.data[i] & y.data[i]; @@ -102,7 +102,7 @@ void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { // TODO check opti void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { bigint_set_zeros(result); - memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); if (bigint_cmp(result, b) < 0) { return ; } @@ -145,7 +145,7 @@ bigint_t bigint_clone(bigint_t src) { dst.len = src.len; dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t)); - memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); + my_memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); return dst; } @@ -178,11 +178,11 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t *b_tool int width = a.len * 32; bigint_set_zeros(result); bigint_set_zeros(b_tool[0]); - memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); + my_memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); for (int i = 1; i < 32; i++) { bigint_set_zeros(b_tool[i]); - memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); + my_memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); bigint_bitwise_left_shift(b_tool[i]); } @@ -201,10 +201,9 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_set_zeros(result); bigint_set_zeros(custom); bigint_set_zeros(custom2); - memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - size_t size = sizeof(uint32_t) * 8; - int cursor = e.len * size - 1; - while (!(e.data[cursor / 32] & 1 << (cursor % 32))) { + my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + int cursor = (e.len << 5) - 1; + while (!(e.data[cursor >> 5] & 1 << (cursor % 32))) { cursor--; } cursor--; @@ -212,11 +211,11 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, custom_bigint_mul(result, result, custom, b_tool); custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); bigint_set_zeros(result); - memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - if (e.data[cursor / 32] & 1 << (cursor % 32)) { + my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + if (e.data[cursor >> 5] & 1 << (cursor % 32)) { custom_bigint_mul(result, a, custom, b_tool); custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); - memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); } cursor -= 1; } @@ -241,7 +240,9 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, } bigint_t bigint_prime(size_t len) { - bigint_t n = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + size_t my_size = len * 2; + + bigint_t n = bigint_zero(my_size); bigint_set_random_bytes(n, len); bigint_set_msb_and_lsb_to_one(n, len); @@ -257,19 +258,19 @@ bigint_t bigint_prime(size_t len) { s += 1; } - bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t x = bigint_zero(my_size); + bigint_t y = bigint_zero(my_size); + bigint_t custom = bigint_zero(my_size); + bigint_t custom2 = bigint_zero(my_size); bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); for (int i = 0; i < 32; i++) { - b_tool[i] = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + b_tool[i] = bigint_zero(my_size * 2); } - bigint_t two = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); - bigint_t one = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t two = bigint_zero(my_size); + bigint_t one = bigint_zero(my_size); two.data[0] = 2; one.data[0] = 1; bigint_t n_minus_two = bigint_clone(n); @@ -277,7 +278,7 @@ bigint_t bigint_prime(size_t len) { n_minus_two.data[0] -= 1; n_minus_one.data[0] -= 1; bigint_decrement(n_minus_two); - bigint_t a = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t a = bigint_zero(my_size); for (uint32_t k = 0; k < 128; k++) { bigint_set_zeros(a); while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { From 0bc6bf62a4ffade4295a58bd62b5026b321c4b71 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Mon, 19 Feb 2024 13:00:55 +0100 Subject: [PATCH 23/34] fix: unused return value --- rsa/array.c | 4 +++- rsa/bigint.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/rsa/array.c b/rsa/array.c index ebd09d0..66947aa 100644 --- a/rsa/array.c +++ b/rsa/array.c @@ -2,7 +2,9 @@ void array_set_random_bytes(uint32_t *n, size_t size) { int fd = open("/dev/urandom", O_RDONLY); - read(fd, n, size); + if (read(fd, n, size) == -1) { + exit(1); + } } void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size) { diff --git a/rsa/bigint.c b/rsa/bigint.c index 25496a4..5585c5a 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -6,7 +6,9 @@ void my_memcpy(void *dst, void *src, size_t n) { void bigint_set_random_bytes(bigint_t n, size_t len) { int fd = open("/dev/urandom", O_RDONLY); - read(fd, n.data, len * sizeof(uint32_t)); + if (read(fd, n.data, len * sizeof(uint32_t)) == -1) { + exit(1); + } close(fd); } From b4a0432d33b74de13a9e6897ecfcc70c93be9224 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Mon, 19 Feb 2024 13:01:51 +0100 Subject: [PATCH 24/34] feat: check first primes before miller rabin --- rsa/bigint.c | 38 +++++++++++++++++++++++++++++--------- rsa/rsa.c | 27 ++++++++++++++++++++++----- rsa/rsa.h | 4 ++-- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 5585c5a..e44a3ac 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -79,20 +79,20 @@ int bigint_dif(bigint_t a, bigint_t b) { return 0; } -int is_not_zero(bigint_t n) { +int is_zero(bigint_t n) { for (size_t i = 0; i < n.len; i++) { if (n.data[i]) { - return 1; + return 0; } } - return 0; + return 1; } // TODO check opti void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { //my_memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); my_memcpy(y.data, b.data, b.len * sizeof(uint32_t)); - while (is_not_zero(y)) { + while (!is_zero(y)) { for (size_t i = 0; i < a.len; i++) { borrow.data[i] = ~a.data[i] & y.data[i]; a.data[i] = a.data[i] ^ y.data[i]; @@ -117,7 +117,7 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_substraction(result, mod, borrow_sub, y_sub); } } - while (bigint_cmp(result, b) > 0) { + while (bigint_cmp(result, b) > -1) { bigint_substraction(result, b, borrow_sub, y_sub); } } @@ -241,14 +241,26 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_destroy(n_minus_one); } -bigint_t bigint_prime(size_t len) { +int prime_division(bigint_t *primes, bigint_t n, bigint_t mod, bigint_t custom2, bigint_t borrow_sub, bigint_t y_sub) { + //printf("testing %u\n", n.data[0]); + bigint_set_zeros(mod); + for (int i = 0; i < 3245; i++) { + mod.data[0] = primes[i].data[0]; + custom_bigint_modulo(n, primes[i], custom2, mod, borrow_sub, y_sub); + //printf("i %d prime %d mod %d\n", i, primes[i].data[0], custom2.data[0]); + if (is_zero(custom2)) { + return 1; + } + } + return 0; +} +bigint_t bigint_prime(size_t len, bigint_t *primes) { size_t my_size = len * 2; bigint_t n = bigint_zero(my_size); bigint_set_random_bytes(n, len); bigint_set_msb_and_lsb_to_one(n, len); - bigint_t mod = bigint_clone(n); bigint_t borrow_sub = bigint_clone(n); bigint_t y_sub = bigint_clone(n); @@ -266,6 +278,14 @@ bigint_t bigint_prime(size_t len) { bigint_t custom2 = bigint_zero(my_size); bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); + + if (prime_division(primes, n, mod, custom2, borrow_sub, y_sub)) { + bulk_destroy(x, y, n, d, custom, custom2, mod, borrow_sub); + bigint_destroy(y_sub); + return bigint_prime(len, primes); + } + memcpy(mod.data, n.data, n.len * sizeof(uint32_t)); + for (int i = 0; i < 32; i++) { b_tool[i] = bigint_zero(my_size * 2); } @@ -301,7 +321,7 @@ bigint_t bigint_prime(size_t len) { bigint_destroy(b_tool[i]); } free(b_tool); - return bigint_prime(len); + return bigint_prime(len, primes); } bigint_destroy(x); x = bigint_clone(y); @@ -318,7 +338,7 @@ bigint_t bigint_prime(size_t len) { bigint_destroy(b_tool[i]); } free(b_tool); - return bigint_prime(len); + return bigint_prime(len, primes); } } bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); diff --git a/rsa/rsa.c b/rsa/rsa.c index 94b47eb..e83421e 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -1,15 +1,15 @@ #include "rsa.h" -rsa_t rsa_init(size_t len) { +rsa_t rsa_init(size_t len, bigint_t *primes) { rsa_t rsa; printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); //printf("Generating p...\n"); - rsa.p = bigint_prime(len / 2); + rsa.p = bigint_prime(len / 2, primes); printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); //printf("p = %u\n", rsa.p.data[0]); //printf("Generating q...\n"); - rsa.q = bigint_prime(len / 2); + rsa.q = bigint_prime(len / 2, primes); printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); //printf("q = %u\n", rsa.q.data[0]); @@ -19,11 +19,28 @@ rsa_t rsa_init(size_t len) { rsa_t rsa_generate_keys(size_t block_size) { size_t len = block_size / sizeof(uint32_t) / 8; - rsa_t rsa = rsa_init(len); + bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t)); + for (int i = 0; i < 3245; i++) { + primes[i] = bigint_zero(len); + } + int fd = open("primes.0000", O_RDONLY); + char *buf = (char *)malloc(21290 * sizeof(char)); + int ret = read(fd, buf, 21290); + char *tok = strtok(buf, "\n"); + int i = 0; + while (tok) { + primes[i].data[0] = (uint32_t)atoi(tok); + tok = strtok(NULL, "\n"); + i += 1; + } + printf("ret %d\n", ret); + + + rsa_t rsa = rsa_init(len, primes); bigint_destroy(rsa.p); bigint_destroy(rsa.q); for (int i = 0; i < 18; i++) { - bigint_t p = bigint_prime(len / 2); + bigint_t p = bigint_prime(len / 2, primes); printf("%lu\n", ((uint64_t)p.data[1] << 32) + (uint64_t)p.data[0]); bigint_destroy(p); } diff --git a/rsa/rsa.h b/rsa/rsa.h index ce55a92..4a0661e 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -9,7 +9,7 @@ #include #include -#define RSA_BLOCK_SIZE 256 +#define RSA_BLOCK_SIZE 256 typedef struct bigint_s { uint32_t *data; @@ -33,7 +33,7 @@ void bigint_bitwise_left_shift(bigint_t n); void bigint_bitwise_right_shift(bigint_t n); void bigint_decrement(bigint_t n); int64_t bigint_cmp(bigint_t a, bigint_t b); -bigint_t bigint_prime(size_t len); +bigint_t bigint_prime(size_t len, bigint_t *primes); void bigint_print(bigint_t n); bigint_t bigint_new(size_t len); bigint_t bigint_zero(size_t len); From ee899b8c8cf963e585f3611b9c3a59e748a79f39 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Mon, 19 Feb 2024 15:15:02 +0100 Subject: [PATCH 25/34] clean: miller-rabin sep fn + set e=65537 --- rsa/Makefile | 9 ++++++ rsa/bigint.c | 82 ++++++++++++++++++++++------------------------------ rsa/rsa.c | 8 ++--- rsa/rsa.h | 2 +- 4 files changed, 46 insertions(+), 55 deletions(-) diff --git a/rsa/Makefile b/rsa/Makefile index 80cf2bf..6cc1a78 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -15,6 +15,15 @@ $(NAME): fast: gcc -Wall -Wextra -Werror -Wunused-function -O3 $(SRC) -o $(NAME) +fast-info: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -fopt-info $(SRC) -o $(NAME) + +really-fast: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native $(SRC) -o $(NAME) + +really-fast-info: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native -fopt-info $(SRC) -o $(NAME) + profile: gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index e44a3ac..415a604 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -32,7 +32,6 @@ void bigint_bitwise_left_shift(bigint_t n) { } void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) { -// my_memcpy(result.data, n.data, n.len * sizeof(uint32_t)); for (int i = result.len - 1; i > 0; i--) { result.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); } @@ -90,7 +89,6 @@ int is_zero(bigint_t n) { // TODO check opti void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { - //my_memcpy(borrow.data, b.data, b.len * sizeof(uint32_t)); my_memcpy(y.data, b.data, b.len * sizeof(uint32_t)); while (!is_zero(y)) { for (size_t i = 0; i < a.len; i++) { @@ -242,18 +240,39 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, } int prime_division(bigint_t *primes, bigint_t n, bigint_t mod, bigint_t custom2, bigint_t borrow_sub, bigint_t y_sub) { - //printf("testing %u\n", n.data[0]); bigint_set_zeros(mod); - for (int i = 0; i < 3245; i++) { + for (int i = 0; i < 200; i++) { mod.data[0] = primes[i].data[0]; custom_bigint_modulo(n, primes[i], custom2, mod, borrow_sub, y_sub); - //printf("i %d prime %d mod %d\n", i, primes[i].data[0], custom2.data[0]); if (is_zero(custom2)) { return 1; } } return 0; } + +int miller_rabin(size_t len, bigint_t a, bigint_t two, bigint_t n_minus_two, bigint_t d, bigint_t n, bigint_t x, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool, bigint_t n_minus_one, uint32_t s, bigint_t y, bigint_t one) { + for (uint32_t k = 0; k < 20; k++) { + bigint_set_zeros(a); + while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { + bigint_set_random_bytes(a, len); + } + custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool); + for (uint32_t i = 0; i < s; i++) { + custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool); + if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { + return 0; + } + bigint_destroy(x); + x = bigint_clone(y); + } + if (bigint_dif(y, one)) { + return 0; + } + } + return 1; +} + bigint_t bigint_prime(size_t len, bigint_t *primes) { size_t my_size = len * 2; @@ -276,7 +295,6 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) { bigint_t y = bigint_zero(my_size); bigint_t custom = bigint_zero(my_size); bigint_t custom2 = bigint_zero(my_size); - bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); if (prime_division(primes, n, mod, custom2, borrow_sub, y_sub)) { @@ -286,11 +304,11 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) { } memcpy(mod.data, n.data, n.len * sizeof(uint32_t)); + bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); for (int i = 0; i < 32; i++) { b_tool[i] = bigint_zero(my_size * 2); } - bigint_t two = bigint_zero(my_size); bigint_t one = bigint_zero(my_size); two.data[0] = 2; @@ -301,46 +319,9 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) { n_minus_one.data[0] -= 1; bigint_decrement(n_minus_two); bigint_t a = bigint_zero(my_size); - for (uint32_t k = 0; k < 128; k++) { - bigint_set_zeros(a); - while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { - bigint_set_random_bytes(a, len); - } - custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool); - for (uint32_t i = 0; i < s; i++) { - custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool); - if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { - bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); - bigint_destroy(custom); - bigint_destroy(custom2); - bigint_destroy(a); - bigint_destroy(mod); - bigint_destroy(borrow_sub); - bigint_destroy(y_sub); - for (int i = 0; i < 32; i++) { - bigint_destroy(b_tool[i]); - } - free(b_tool); - return bigint_prime(len, primes); - } - bigint_destroy(x); - x = bigint_clone(y); - } - if (bigint_dif(y, one)) { - bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one); - bigint_destroy(custom); - bigint_destroy(custom2); - bigint_destroy(a); - bigint_destroy(mod); - bigint_destroy(borrow_sub); - bigint_destroy(y_sub); - for (int i = 0; i < 32; i++) { - bigint_destroy(b_tool[i]); - } - free(b_tool); - return bigint_prime(len, primes); - } - } + + int is_prime = miller_rabin(len, a, two, n_minus_two, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool, n_minus_one, s, y, one); + bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); bigint_destroy(custom2); bigint_destroy(a); @@ -351,6 +332,11 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) { bigint_destroy(b_tool[i]); } free(b_tool); - return n; + + if (is_prime) { + return n; + } + bigint_destroy(n); + return bigint_prime(len, primes); } diff --git a/rsa/rsa.c b/rsa/rsa.c index e83421e..22dccbf 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -33,17 +33,13 @@ rsa_t rsa_generate_keys(size_t block_size) { tok = strtok(NULL, "\n"); i += 1; } + primes[0].data[0] = 65537; printf("ret %d\n", ret); - rsa_t rsa = rsa_init(len, primes); bigint_destroy(rsa.p); bigint_destroy(rsa.q); - for (int i = 0; i < 18; i++) { - bigint_t p = bigint_prime(len / 2, primes); - printf("%lu\n", ((uint64_t)p.data[1] << 32) + (uint64_t)p.data[0]); - bigint_destroy(p); - } + return rsa; } diff --git a/rsa/rsa.h b/rsa/rsa.h index 4a0661e..5537b35 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -9,7 +9,7 @@ #include #include -#define RSA_BLOCK_SIZE 256 +#define RSA_BLOCK_SIZE 128 typedef struct bigint_s { uint32_t *data; From 17cd4fde5b4b61217dc49bcc73759a3bfee07bf8 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Tue, 9 Apr 2024 16:55:13 +0200 Subject: [PATCH 26/34] feat: rsa 64bits prime gen --- rsa64/Makefile | 45 + rsa64/array.c | 42 + rsa64/bigint.c | 342 +++++ rsa64/main.c | 13 + rsa64/primes.0000 | 3245 +++++++++++++++++++++++++++++++++++++++++++++ rsa64/primes.c | 77 ++ rsa64/rsa.c | 49 + rsa64/rsa.h | 60 + rsa64/utils.c | 9 + 9 files changed, 3882 insertions(+) create mode 100644 rsa64/Makefile create mode 100644 rsa64/array.c create mode 100644 rsa64/bigint.c create mode 100644 rsa64/main.c create mode 100644 rsa64/primes.0000 create mode 100644 rsa64/primes.c create mode 100644 rsa64/rsa.c create mode 100644 rsa64/rsa.h create mode 100644 rsa64/utils.c diff --git a/rsa64/Makefile b/rsa64/Makefile new file mode 100644 index 0000000..5cbb4bb --- /dev/null +++ b/rsa64/Makefile @@ -0,0 +1,45 @@ +NAME = rsa + +SRC = \ + main.c \ + rsa.c \ + bigint.c \ + array.c \ + utils.c \ + primes.c \ + +all: $(NAME) + +$(NAME): + gcc -Wall -Wextra -Werror -Wunused-function $(SRC) -o $(NAME) + +fast: + gcc -Wall -Wextra -Werror -Wunused-function -O3 $(SRC) -o $(NAME) + +fast-info: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -fopt-info $(SRC) -o $(NAME) + +really-fast: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native $(SRC) -o $(NAME) + +really-fast-info: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native -fopt-info $(SRC) -o $(NAME) + +profile: + gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) + +profile-clang: + clang -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) + +profile-fast: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) + +profile-fast-clang: + clang -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) + +fclean: + rm -rf $(NAME) + +re: fclean all + +.PHONY: all fast profile profile-fast fclean re diff --git a/rsa64/array.c b/rsa64/array.c new file mode 100644 index 0000000..66947aa --- /dev/null +++ b/rsa64/array.c @@ -0,0 +1,42 @@ +#include "rsa.h" + +void array_set_random_bytes(uint32_t *n, size_t size) { + int fd = open("/dev/urandom", O_RDONLY); + if (read(fd, n, size) == -1) { + exit(1); + } +} + +void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size) { + n[0] |= 1; + n[size / sizeof(uint32_t) - 1] |= 1 << 31; +} + +void array_bitwise_right_shift(uint32_t *a, size_t len) { + size_t size = sizeof(uint32_t) * 8 - 1; + for (size_t n = 0; n < len - 1; n++) { + a[n] = a[n] >> 1 | (a[n + 1] & 1) << size; + } + a[len - 1] >>= 1; +} + +void array_bitwise_left_shift(uint32_t *a, size_t len) { + size_t size = sizeof(uint32_t) * 8 - 1; + for (size_t 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(uint32_t *a, size_t len) { + size_t cursor = 0; + size_t size = sizeof(uint32_t) * 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; + } +} diff --git a/rsa64/bigint.c b/rsa64/bigint.c new file mode 100644 index 0000000..415a604 --- /dev/null +++ b/rsa64/bigint.c @@ -0,0 +1,342 @@ +#include "rsa.h" + +void my_memcpy(void *dst, void *src, size_t n) { + memcpy(dst, src, n); +} + +void bigint_set_random_bytes(bigint_t n, size_t len) { + int fd = open("/dev/urandom", O_RDONLY); + if (read(fd, n.data, len * sizeof(uint32_t)) == -1) { + exit(1); + } + close(fd); +} + +void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) { + n.data[0] |= 1; + n.data[len - 1] |= 1 << 31; +} + +void bigint_bitwise_right_shift(bigint_t n) { + for (size_t i = 0; i < n.len - 1; i++) { + n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << 31; + } + n.data[n.len - 1] >>= 1; +} + +void bigint_bitwise_left_shift(bigint_t n) { + for (int i = n.len - 1; i > 0; i--) { + n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); + } + n.data[0] <<= 1; +} + +void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) { + for (int i = result.len - 1; i > 0; i--) { + result.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); + } + result.data[0] = n.data[0] << 1; +} + +// Will underflow +void bigint_decrement(bigint_t n) { + size_t cursor = 0; + while (cursor < n.len << 5) { + n.data[cursor >> 32] = n.data[cursor >> 5] ^ (1 << (cursor % 32)); + if (((n.data[cursor >> 5] >> (cursor % 32)) & 1) == 0) { + return; + } + cursor += 1; + } +} + +// TODO refactor/clean assume same length ? +int64_t bigint_cmp(bigint_t a, bigint_t b) { + int cursor = a.len - 1; + while (cursor >= 0) { + if (a.data[cursor] > b.data[cursor]) { + return 1; + } + if (b.data[cursor] > a.data[cursor]) { + return -1; + } + cursor -= 1; + } + + return 0; +} + +// TODO refactor/clean assume same length ? +int bigint_dif(bigint_t a, bigint_t b) { + int cursor = a.len; + while (--cursor >= 0) { + if (a.data[cursor] ^ b.data[cursor]) { + return 1; + } + } + + return 0; +} + +int is_zero(bigint_t n) { + for (size_t i = 0; i < n.len; i++) { + if (n.data[i]) { + return 0; + } + } + return 1; +} + +// TODO check opti +void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { + my_memcpy(y.data, b.data, b.len * sizeof(uint32_t)); + while (!is_zero(y)) { + for (size_t i = 0; i < a.len; i++) { + borrow.data[i] = ~a.data[i] & y.data[i]; + a.data[i] = a.data[i] ^ y.data[i]; + } + move_bigint_bitwise_left_shift(borrow, y); + } +} + +// TODO check opti +void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { + bigint_set_zeros(result); + my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + if (bigint_cmp(result, b) < 0) { + return ; + } + while (bigint_cmp(result, mod) > 0) { + bigint_bitwise_left_shift(mod); + } + while (bigint_cmp(b, mod) < 0) { + bigint_bitwise_right_shift(mod); + if (bigint_cmp(result, mod) > 0) { + bigint_substraction(result, mod, borrow_sub, y_sub); + } + } + while (bigint_cmp(result, b) > -1) { + bigint_substraction(result, b, borrow_sub, y_sub); + } +} + +bigint_t bigint_new(size_t len) { + bigint_t bigint; + + bigint.len = len; + bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t)); + + return bigint; +} + +bigint_t bigint_zero(size_t len) { + bigint_t bigint; + + bigint = bigint_new(len); + for (size_t i = 0; i < len; i++) { + bigint.data[i] = 0; + } + + return bigint; +} + +bigint_t bigint_clone(bigint_t src) { + bigint_t dst; + + dst.len = src.len; + dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t)); + my_memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); + + return dst; +} + +void bigint_destroy(bigint_t n) { + free(n.data); + n.data = NULL; +} + +void custom_bigint_add(bigint_t a, bigint_t b, int index) { + uint64_t carriage = 0; + + for (size_t cursor = 0; cursor < a.len; cursor++) { + uint64_t tmp = (uint64_t)a.data[cursor] + carriage; + if ((int)cursor - index >= 0) { + tmp += (uint64_t)b.data[cursor - index]; + } + a.data[cursor] = (uint32_t)tmp; + carriage = tmp >> 32; + } +} + +void bigint_set_zeros(bigint_t n) { + for (size_t i = 0; i < n.len; i++) { + n.data[i] = 0; + } +} + +void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t *b_tool) { + int width = a.len * 32; + bigint_set_zeros(result); + bigint_set_zeros(b_tool[0]); + my_memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); + + for (int i = 1; i < 32; i++) { + bigint_set_zeros(b_tool[i]); + my_memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); + bigint_bitwise_left_shift(b_tool[i]); + } + + for (int cursor = 0; cursor < width; cursor++) { + int offset = cursor % 32; + int index = cursor >> 5; + if (a.data[index] >> offset & 1) { + custom_bigint_add(result, b_tool[offset], index); + } + } +} + +// a^e mod n +// clean memory tricks !!! +void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool) { + bigint_set_zeros(result); + bigint_set_zeros(custom); + bigint_set_zeros(custom2); + my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); + int cursor = (e.len << 5) - 1; + while (!(e.data[cursor >> 5] & 1 << (cursor % 32))) { + cursor--; + } + cursor--; + while (cursor >= 0) { + custom_bigint_mul(result, result, custom, b_tool); + custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); + bigint_set_zeros(result); + my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + if (e.data[cursor >> 5] & 1 << (cursor % 32)) { + custom_bigint_mul(result, a, custom, b_tool); + custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); + my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); + } + cursor -= 1; + } +} + +void bigint_print(bigint_t n) { + for (int i = n.len - 1; i >= 0; i--) { + printf("bigint %ud\n", n.data[i]); + } +} + + +void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t one, bigint_t n_minus_two, bigint_t n_minus_one) { + bigint_destroy(x); + bigint_destroy(y); + bigint_destroy(n); + bigint_destroy(d); + bigint_destroy(two); + bigint_destroy(one); + bigint_destroy(n_minus_two); + bigint_destroy(n_minus_one); +} + +int prime_division(bigint_t *primes, bigint_t n, bigint_t mod, bigint_t custom2, bigint_t borrow_sub, bigint_t y_sub) { + bigint_set_zeros(mod); + for (int i = 0; i < 200; i++) { + mod.data[0] = primes[i].data[0]; + custom_bigint_modulo(n, primes[i], custom2, mod, borrow_sub, y_sub); + if (is_zero(custom2)) { + return 1; + } + } + return 0; +} + +int miller_rabin(size_t len, bigint_t a, bigint_t two, bigint_t n_minus_two, bigint_t d, bigint_t n, bigint_t x, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool, bigint_t n_minus_one, uint32_t s, bigint_t y, bigint_t one) { + for (uint32_t k = 0; k < 20; k++) { + bigint_set_zeros(a); + while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { + bigint_set_random_bytes(a, len); + } + custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool); + for (uint32_t i = 0; i < s; i++) { + custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool); + if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { + return 0; + } + bigint_destroy(x); + x = bigint_clone(y); + } + if (bigint_dif(y, one)) { + return 0; + } + } + return 1; +} + +bigint_t bigint_prime(size_t len, bigint_t *primes) { + size_t my_size = len * 2; + + bigint_t n = bigint_zero(my_size); + + bigint_set_random_bytes(n, len); + bigint_set_msb_and_lsb_to_one(n, len); + bigint_t mod = bigint_clone(n); + bigint_t borrow_sub = bigint_clone(n); + bigint_t y_sub = bigint_clone(n); + bigint_t d = bigint_clone(n); + d.data[0] -= 1; + uint32_t s = 0; + while (!(d.data[0] & 1)) { + bigint_bitwise_right_shift(d); + s += 1; + } + + bigint_t x = bigint_zero(my_size); + bigint_t y = bigint_zero(my_size); + bigint_t custom = bigint_zero(my_size); + bigint_t custom2 = bigint_zero(my_size); + + + if (prime_division(primes, n, mod, custom2, borrow_sub, y_sub)) { + bulk_destroy(x, y, n, d, custom, custom2, mod, borrow_sub); + bigint_destroy(y_sub); + return bigint_prime(len, primes); + } + memcpy(mod.data, n.data, n.len * sizeof(uint32_t)); + + bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); + for (int i = 0; i < 32; i++) { + b_tool[i] = bigint_zero(my_size * 2); + } + + bigint_t two = bigint_zero(my_size); + bigint_t one = bigint_zero(my_size); + two.data[0] = 2; + one.data[0] = 1; + bigint_t n_minus_two = bigint_clone(n); + bigint_t n_minus_one = bigint_clone(n); + n_minus_two.data[0] -= 1; + n_minus_one.data[0] -= 1; + bigint_decrement(n_minus_two); + bigint_t a = bigint_zero(my_size); + + int is_prime = miller_rabin(len, a, two, n_minus_two, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool, n_minus_one, s, y, one); + + bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); + bigint_destroy(custom2); + bigint_destroy(a); + bigint_destroy(mod); + bigint_destroy(borrow_sub); + bigint_destroy(y_sub); + for (int i = 0; i < 32; i++) { + bigint_destroy(b_tool[i]); + } + free(b_tool); + + if (is_prime) { + return n; + } + bigint_destroy(n); + return bigint_prime(len, primes); +} + diff --git a/rsa64/main.c b/rsa64/main.c new file mode 100644 index 0000000..c1df254 --- /dev/null +++ b/rsa64/main.c @@ -0,0 +1,13 @@ +#include "rsa.h" + +int main(int ac, char **av) { + if (ac == 2) { + (void)av; + rsa_t rsa = rsa_generate_keys(RSA_BLOCK_SIZE); + (void)rsa; + } + else { + printf("usage: ./rsa message\n"); + } + return 0; +} diff --git a/rsa64/primes.0000 b/rsa64/primes.0000 new file mode 100644 index 0000000..e4be11b --- /dev/null +++ b/rsa64/primes.0000 @@ -0,0 +1,3245 @@ +2 +3 +5 +7 +11 +13 +17 +19 +23 +29 +31 +37 +41 +43 +47 +53 +59 +61 +67 +71 +73 +79 +83 +89 +97 +101 +103 +107 +109 +113 +127 +131 +137 +139 +149 +151 +157 +163 +167 +173 +179 +181 +191 +193 +197 +199 +211 +223 +227 +229 +233 +239 +241 +251 +257 +263 +269 +271 +277 +281 +283 +293 +307 +311 +313 +317 +331 +337 +347 +349 +353 +359 +367 +373 +379 +383 +389 +397 +401 +409 +419 +421 +431 +433 +439 +443 +449 +457 +461 +463 +467 +479 +487 +491 +499 +503 +509 +521 +523 +541 +547 +557 +563 +569 +571 +577 +587 +593 +599 +601 +607 +613 +617 +619 +631 +641 +643 +647 +653 +659 +661 +673 +677 +683 +691 +701 +709 +719 +727 +733 +739 +743 +751 +757 +761 +769 +773 +787 +797 +809 +811 +821 +823 +827 +829 +839 +853 +857 +859 +863 +877 +881 +883 +887 +907 +911 +919 +929 +937 +941 +947 +953 +967 +971 +977 +983 +991 +997 +1009 +1013 +1019 +1021 +1031 +1033 +1039 +1049 +1051 +1061 +1063 +1069 +1087 +1091 +1093 +1097 +1103 +1109 +1117 +1123 +1129 +1151 +1153 +1163 +1171 +1181 +1187 +1193 +1201 +1213 +1217 +1223 +1229 +1231 +1237 +1249 +1259 +1277 +1279 +1283 +1289 +1291 +1297 +1301 +1303 +1307 +1319 +1321 +1327 +1361 +1367 +1373 +1381 +1399 +1409 +1423 +1427 +1429 +1433 +1439 +1447 +1451 +1453 +1459 +1471 +1481 +1483 +1487 +1489 +1493 +1499 +1511 +1523 +1531 +1543 +1549 +1553 +1559 +1567 +1571 +1579 +1583 +1597 +1601 +1607 +1609 +1613 +1619 +1621 +1627 +1637 +1657 +1663 +1667 +1669 +1693 +1697 +1699 +1709 +1721 +1723 +1733 +1741 +1747 +1753 +1759 +1777 +1783 +1787 +1789 +1801 +1811 +1823 +1831 +1847 +1861 +1867 +1871 +1873 +1877 +1879 +1889 +1901 +1907 +1913 +1931 +1933 +1949 +1951 +1973 +1979 +1987 +1993 +1997 +1999 +2003 +2011 +2017 +2027 +2029 +2039 +2053 +2063 +2069 +2081 +2083 +2087 +2089 +2099 +2111 +2113 +2129 +2131 +2137 +2141 +2143 +2153 +2161 +2179 +2203 +2207 +2213 +2221 +2237 +2239 +2243 +2251 +2267 +2269 +2273 +2281 +2287 +2293 +2297 +2309 +2311 +2333 +2339 +2341 +2347 +2351 +2357 +2371 +2377 +2381 +2383 +2389 +2393 +2399 +2411 +2417 +2423 +2437 +2441 +2447 +2459 +2467 +2473 +2477 +2503 +2521 +2531 +2539 +2543 +2549 +2551 +2557 +2579 +2591 +2593 +2609 +2617 +2621 +2633 +2647 +2657 +2659 +2663 +2671 +2677 +2683 +2687 +2689 +2693 +2699 +2707 +2711 +2713 +2719 +2729 +2731 +2741 +2749 +2753 +2767 +2777 +2789 +2791 +2797 +2801 +2803 +2819 +2833 +2837 +2843 +2851 +2857 +2861 +2879 +2887 +2897 +2903 +2909 +2917 +2927 +2939 +2953 +2957 +2963 +2969 +2971 +2999 +3001 +3011 +3019 +3023 +3037 +3041 +3049 +3061 +3067 +3079 +3083 +3089 +3109 +3119 +3121 +3137 +3163 +3167 +3169 +3181 +3187 +3191 +3203 +3209 +3217 +3221 +3229 +3251 +3253 +3257 +3259 +3271 +3299 +3301 +3307 +3313 +3319 +3323 +3329 +3331 +3343 +3347 +3359 +3361 +3371 +3373 +3389 +3391 +3407 +3413 +3433 +3449 +3457 +3461 +3463 +3467 +3469 +3491 +3499 +3511 +3517 +3527 +3529 +3533 +3539 +3541 +3547 +3557 +3559 +3571 +3581 +3583 +3593 +3607 +3613 +3617 +3623 +3631 +3637 +3643 +3659 +3671 +3673 +3677 +3691 +3697 +3701 +3709 +3719 +3727 +3733 +3739 +3761 +3767 +3769 +3779 +3793 +3797 +3803 +3821 +3823 +3833 +3847 +3851 +3853 +3863 +3877 +3881 +3889 +3907 +3911 +3917 +3919 +3923 +3929 +3931 +3943 +3947 +3967 +3989 +4001 +4003 +4007 +4013 +4019 +4021 +4027 +4049 +4051 +4057 +4073 +4079 +4091 +4093 +4099 +4111 +4127 +4129 +4133 +4139 +4153 +4157 +4159 +4177 +4201 +4211 +4217 +4219 +4229 +4231 +4241 +4243 +4253 +4259 +4261 +4271 +4273 +4283 +4289 +4297 +4327 +4337 +4339 +4349 +4357 +4363 +4373 +4391 +4397 +4409 +4421 +4423 +4441 +4447 +4451 +4457 +4463 +4481 +4483 +4493 +4507 +4513 +4517 +4519 +4523 +4547 +4549 +4561 +4567 +4583 +4591 +4597 +4603 +4621 +4637 +4639 +4643 +4649 +4651 +4657 +4663 +4673 +4679 +4691 +4703 +4721 +4723 +4729 +4733 +4751 +4759 +4783 +4787 +4789 +4793 +4799 +4801 +4813 +4817 +4831 +4861 +4871 +4877 +4889 +4903 +4909 +4919 +4931 +4933 +4937 +4943 +4951 +4957 +4967 +4969 +4973 +4987 +4993 +4999 +5003 +5009 +5011 +5021 +5023 +5039 +5051 +5059 +5077 +5081 +5087 +5099 +5101 +5107 +5113 +5119 +5147 +5153 +5167 +5171 +5179 +5189 +5197 +5209 +5227 +5231 +5233 +5237 +5261 +5273 +5279 +5281 +5297 +5303 +5309 +5323 +5333 +5347 +5351 +5381 +5387 +5393 +5399 +5407 +5413 +5417 +5419 +5431 +5437 +5441 +5443 +5449 +5471 +5477 +5479 +5483 +5501 +5503 +5507 +5519 +5521 +5527 +5531 +5557 +5563 +5569 +5573 +5581 +5591 +5623 +5639 +5641 +5647 +5651 +5653 +5657 +5659 +5669 +5683 +5689 +5693 +5701 +5711 +5717 +5737 +5741 +5743 +5749 +5779 +5783 +5791 +5801 +5807 +5813 +5821 +5827 +5839 +5843 +5849 +5851 +5857 +5861 +5867 +5869 +5879 +5881 +5897 +5903 +5923 +5927 +5939 +5953 +5981 +5987 +6007 +6011 +6029 +6037 +6043 +6047 +6053 +6067 +6073 +6079 +6089 +6091 +6101 +6113 +6121 +6131 +6133 +6143 +6151 +6163 +6173 +6197 +6199 +6203 +6211 +6217 +6221 +6229 +6247 +6257 +6263 +6269 +6271 +6277 +6287 +6299 +6301 +6311 +6317 +6323 +6329 +6337 +6343 +6353 +6359 +6361 +6367 +6373 +6379 +6389 +6397 +6421 +6427 +6449 +6451 +6469 +6473 +6481 +6491 +6521 +6529 +6547 +6551 +6553 +6563 +6569 +6571 +6577 +6581 +6599 +6607 +6619 +6637 +6653 +6659 +6661 +6673 +6679 +6689 +6691 +6701 +6703 +6709 +6719 +6733 +6737 +6761 +6763 +6779 +6781 +6791 +6793 +6803 +6823 +6827 +6829 +6833 +6841 +6857 +6863 +6869 +6871 +6883 +6899 +6907 +6911 +6917 +6947 +6949 +6959 +6961 +6967 +6971 +6977 +6983 +6991 +6997 +7001 +7013 +7019 +7027 +7039 +7043 +7057 +7069 +7079 +7103 +7109 +7121 +7127 +7129 +7151 +7159 +7177 +7187 +7193 +7207 +7211 +7213 +7219 +7229 +7237 +7243 +7247 +7253 +7283 +7297 +7307 +7309 +7321 +7331 +7333 +7349 +7351 +7369 +7393 +7411 +7417 +7433 +7451 +7457 +7459 +7477 +7481 +7487 +7489 +7499 +7507 +7517 +7523 +7529 +7537 +7541 +7547 +7549 +7559 +7561 +7573 +7577 +7583 +7589 +7591 +7603 +7607 +7621 +7639 +7643 +7649 +7669 +7673 +7681 +7687 +7691 +7699 +7703 +7717 +7723 +7727 +7741 +7753 +7757 +7759 +7789 +7793 +7817 +7823 +7829 +7841 +7853 +7867 +7873 +7877 +7879 +7883 +7901 +7907 +7919 +7927 +7933 +7937 +7949 +7951 +7963 +7993 +8009 +8011 +8017 +8039 +8053 +8059 +8069 +8081 +8087 +8089 +8093 +8101 +8111 +8117 +8123 +8147 +8161 +8167 +8171 +8179 +8191 +8209 +8219 +8221 +8231 +8233 +8237 +8243 +8263 +8269 +8273 +8287 +8291 +8293 +8297 +8311 +8317 +8329 +8353 +8363 +8369 +8377 +8387 +8389 +8419 +8423 +8429 +8431 +8443 +8447 +8461 +8467 +8501 +8513 +8521 +8527 +8537 +8539 +8543 +8563 +8573 +8581 +8597 +8599 +8609 +8623 +8627 +8629 +8641 +8647 +8663 +8669 +8677 +8681 +8689 +8693 +8699 +8707 +8713 +8719 +8731 +8737 +8741 +8747 +8753 +8761 +8779 +8783 +8803 +8807 +8819 +8821 +8831 +8837 +8839 +8849 +8861 +8863 +8867 +8887 +8893 +8923 +8929 +8933 +8941 +8951 +8963 +8969 +8971 +8999 +9001 +9007 +9011 +9013 +9029 +9041 +9043 +9049 +9059 +9067 +9091 +9103 +9109 +9127 +9133 +9137 +9151 +9157 +9161 +9173 +9181 +9187 +9199 +9203 +9209 +9221 +9227 +9239 +9241 +9257 +9277 +9281 +9283 +9293 +9311 +9319 +9323 +9337 +9341 +9343 +9349 +9371 +9377 +9391 +9397 +9403 +9413 +9419 +9421 +9431 +9433 +9437 +9439 +9461 +9463 +9467 +9473 +9479 +9491 +9497 +9511 +9521 +9533 +9539 +9547 +9551 +9587 +9601 +9613 +9619 +9623 +9629 +9631 +9643 +9649 +9661 +9677 +9679 +9689 +9697 +9719 +9721 +9733 +9739 +9743 +9749 +9767 +9769 +9781 +9787 +9791 +9803 +9811 +9817 +9829 +9833 +9839 +9851 +9857 +9859 +9871 +9883 +9887 +9901 +9907 +9923 +9929 +9931 +9941 +9949 +9967 +9973 +10007 +10009 +10037 +10039 +10061 +10067 +10069 +10079 +10091 +10093 +10099 +10103 +10111 +10133 +10139 +10141 +10151 +10159 +10163 +10169 +10177 +10181 +10193 +10211 +10223 +10243 +10247 +10253 +10259 +10267 +10271 +10273 +10289 +10301 +10303 +10313 +10321 +10331 +10333 +10337 +10343 +10357 +10369 +10391 +10399 +10427 +10429 +10433 +10453 +10457 +10459 +10463 +10477 +10487 +10499 +10501 +10513 +10529 +10531 +10559 +10567 +10589 +10597 +10601 +10607 +10613 +10627 +10631 +10639 +10651 +10657 +10663 +10667 +10687 +10691 +10709 +10711 +10723 +10729 +10733 +10739 +10753 +10771 +10781 +10789 +10799 +10831 +10837 +10847 +10853 +10859 +10861 +10867 +10883 +10889 +10891 +10903 +10909 +10937 +10939 +10949 +10957 +10973 +10979 +10987 +10993 +11003 +11027 +11047 +11057 +11059 +11069 +11071 +11083 +11087 +11093 +11113 +11117 +11119 +11131 +11149 +11159 +11161 +11171 +11173 +11177 +11197 +11213 +11239 +11243 +11251 +11257 +11261 +11273 +11279 +11287 +11299 +11311 +11317 +11321 +11329 +11351 +11353 +11369 +11383 +11393 +11399 +11411 +11423 +11437 +11443 +11447 +11467 +11471 +11483 +11489 +11491 +11497 +11503 +11519 +11527 +11549 +11551 +11579 +11587 +11593 +11597 +11617 +11621 +11633 +11657 +11677 +11681 +11689 +11699 +11701 +11717 +11719 +11731 +11743 +11777 +11779 +11783 +11789 +11801 +11807 +11813 +11821 +11827 +11831 +11833 +11839 +11863 +11867 +11887 +11897 +11903 +11909 +11923 +11927 +11933 +11939 +11941 +11953 +11959 +11969 +11971 +11981 +11987 +12007 +12011 +12037 +12041 +12043 +12049 +12071 +12073 +12097 +12101 +12107 +12109 +12113 +12119 +12143 +12149 +12157 +12161 +12163 +12197 +12203 +12211 +12227 +12239 +12241 +12251 +12253 +12263 +12269 +12277 +12281 +12289 +12301 +12323 +12329 +12343 +12347 +12373 +12377 +12379 +12391 +12401 +12409 +12413 +12421 +12433 +12437 +12451 +12457 +12473 +12479 +12487 +12491 +12497 +12503 +12511 +12517 +12527 +12539 +12541 +12547 +12553 +12569 +12577 +12583 +12589 +12601 +12611 +12613 +12619 +12637 +12641 +12647 +12653 +12659 +12671 +12689 +12697 +12703 +12713 +12721 +12739 +12743 +12757 +12763 +12781 +12791 +12799 +12809 +12821 +12823 +12829 +12841 +12853 +12889 +12893 +12899 +12907 +12911 +12917 +12919 +12923 +12941 +12953 +12959 +12967 +12973 +12979 +12983 +13001 +13003 +13007 +13009 +13033 +13037 +13043 +13049 +13063 +13093 +13099 +13103 +13109 +13121 +13127 +13147 +13151 +13159 +13163 +13171 +13177 +13183 +13187 +13217 +13219 +13229 +13241 +13249 +13259 +13267 +13291 +13297 +13309 +13313 +13327 +13331 +13337 +13339 +13367 +13381 +13397 +13399 +13411 +13417 +13421 +13441 +13451 +13457 +13463 +13469 +13477 +13487 +13499 +13513 +13523 +13537 +13553 +13567 +13577 +13591 +13597 +13613 +13619 +13627 +13633 +13649 +13669 +13679 +13681 +13687 +13691 +13693 +13697 +13709 +13711 +13721 +13723 +13729 +13751 +13757 +13759 +13763 +13781 +13789 +13799 +13807 +13829 +13831 +13841 +13859 +13873 +13877 +13879 +13883 +13901 +13903 +13907 +13913 +13921 +13931 +13933 +13963 +13967 +13997 +13999 +14009 +14011 +14029 +14033 +14051 +14057 +14071 +14081 +14083 +14087 +14107 +14143 +14149 +14153 +14159 +14173 +14177 +14197 +14207 +14221 +14243 +14249 +14251 +14281 +14293 +14303 +14321 +14323 +14327 +14341 +14347 +14369 +14387 +14389 +14401 +14407 +14411 +14419 +14423 +14431 +14437 +14447 +14449 +14461 +14479 +14489 +14503 +14519 +14533 +14537 +14543 +14549 +14551 +14557 +14561 +14563 +14591 +14593 +14621 +14627 +14629 +14633 +14639 +14653 +14657 +14669 +14683 +14699 +14713 +14717 +14723 +14731 +14737 +14741 +14747 +14753 +14759 +14767 +14771 +14779 +14783 +14797 +14813 +14821 +14827 +14831 +14843 +14851 +14867 +14869 +14879 +14887 +14891 +14897 +14923 +14929 +14939 +14947 +14951 +14957 +14969 +14983 +15013 +15017 +15031 +15053 +15061 +15073 +15077 +15083 +15091 +15101 +15107 +15121 +15131 +15137 +15139 +15149 +15161 +15173 +15187 +15193 +15199 +15217 +15227 +15233 +15241 +15259 +15263 +15269 +15271 +15277 +15287 +15289 +15299 +15307 +15313 +15319 +15329 +15331 +15349 +15359 +15361 +15373 +15377 +15383 +15391 +15401 +15413 +15427 +15439 +15443 +15451 +15461 +15467 +15473 +15493 +15497 +15511 +15527 +15541 +15551 +15559 +15569 +15581 +15583 +15601 +15607 +15619 +15629 +15641 +15643 +15647 +15649 +15661 +15667 +15671 +15679 +15683 +15727 +15731 +15733 +15737 +15739 +15749 +15761 +15767 +15773 +15787 +15791 +15797 +15803 +15809 +15817 +15823 +15859 +15877 +15881 +15887 +15889 +15901 +15907 +15913 +15919 +15923 +15937 +15959 +15971 +15973 +15991 +16001 +16007 +16033 +16057 +16061 +16063 +16067 +16069 +16073 +16087 +16091 +16097 +16103 +16111 +16127 +16139 +16141 +16183 +16187 +16189 +16193 +16217 +16223 +16229 +16231 +16249 +16253 +16267 +16273 +16301 +16319 +16333 +16339 +16349 +16361 +16363 +16369 +16381 +16411 +16417 +16421 +16427 +16433 +16447 +16451 +16453 +16477 +16481 +16487 +16493 +16519 +16529 +16547 +16553 +16561 +16567 +16573 +16603 +16607 +16619 +16631 +16633 +16649 +16651 +16657 +16661 +16673 +16691 +16693 +16699 +16703 +16729 +16741 +16747 +16759 +16763 +16787 +16811 +16823 +16829 +16831 +16843 +16871 +16879 +16883 +16889 +16901 +16903 +16921 +16927 +16931 +16937 +16943 +16963 +16979 +16981 +16987 +16993 +17011 +17021 +17027 +17029 +17033 +17041 +17047 +17053 +17077 +17093 +17099 +17107 +17117 +17123 +17137 +17159 +17167 +17183 +17189 +17191 +17203 +17207 +17209 +17231 +17239 +17257 +17291 +17293 +17299 +17317 +17321 +17327 +17333 +17341 +17351 +17359 +17377 +17383 +17387 +17389 +17393 +17401 +17417 +17419 +17431 +17443 +17449 +17467 +17471 +17477 +17483 +17489 +17491 +17497 +17509 +17519 +17539 +17551 +17569 +17573 +17579 +17581 +17597 +17599 +17609 +17623 +17627 +17657 +17659 +17669 +17681 +17683 +17707 +17713 +17729 +17737 +17747 +17749 +17761 +17783 +17789 +17791 +17807 +17827 +17837 +17839 +17851 +17863 +17881 +17891 +17903 +17909 +17911 +17921 +17923 +17929 +17939 +17957 +17959 +17971 +17977 +17981 +17987 +17989 +18013 +18041 +18043 +18047 +18049 +18059 +18061 +18077 +18089 +18097 +18119 +18121 +18127 +18131 +18133 +18143 +18149 +18169 +18181 +18191 +18199 +18211 +18217 +18223 +18229 +18233 +18251 +18253 +18257 +18269 +18287 +18289 +18301 +18307 +18311 +18313 +18329 +18341 +18353 +18367 +18371 +18379 +18397 +18401 +18413 +18427 +18433 +18439 +18443 +18451 +18457 +18461 +18481 +18493 +18503 +18517 +18521 +18523 +18539 +18541 +18553 +18583 +18587 +18593 +18617 +18637 +18661 +18671 +18679 +18691 +18701 +18713 +18719 +18731 +18743 +18749 +18757 +18773 +18787 +18793 +18797 +18803 +18839 +18859 +18869 +18899 +18911 +18913 +18917 +18919 +18947 +18959 +18973 +18979 +19001 +19009 +19013 +19031 +19037 +19051 +19069 +19073 +19079 +19081 +19087 +19121 +19139 +19141 +19157 +19163 +19181 +19183 +19207 +19211 +19213 +19219 +19231 +19237 +19249 +19259 +19267 +19273 +19289 +19301 +19309 +19319 +19333 +19373 +19379 +19381 +19387 +19391 +19403 +19417 +19421 +19423 +19427 +19429 +19433 +19441 +19447 +19457 +19463 +19469 +19471 +19477 +19483 +19489 +19501 +19507 +19531 +19541 +19543 +19553 +19559 +19571 +19577 +19583 +19597 +19603 +19609 +19661 +19681 +19687 +19697 +19699 +19709 +19717 +19727 +19739 +19751 +19753 +19759 +19763 +19777 +19793 +19801 +19813 +19819 +19841 +19843 +19853 +19861 +19867 +19889 +19891 +19913 +19919 +19927 +19937 +19949 +19961 +19963 +19973 +19979 +19991 +19993 +19997 +20011 +20021 +20023 +20029 +20047 +20051 +20063 +20071 +20089 +20101 +20107 +20113 +20117 +20123 +20129 +20143 +20147 +20149 +20161 +20173 +20177 +20183 +20201 +20219 +20231 +20233 +20249 +20261 +20269 +20287 +20297 +20323 +20327 +20333 +20341 +20347 +20353 +20357 +20359 +20369 +20389 +20393 +20399 +20407 +20411 +20431 +20441 +20443 +20477 +20479 +20483 +20507 +20509 +20521 +20533 +20543 +20549 +20551 +20563 +20593 +20599 +20611 +20627 +20639 +20641 +20663 +20681 +20693 +20707 +20717 +20719 +20731 +20743 +20747 +20749 +20753 +20759 +20771 +20773 +20789 +20807 +20809 +20849 +20857 +20873 +20879 +20887 +20897 +20899 +20903 +20921 +20929 +20939 +20947 +20959 +20963 +20981 +20983 +21001 +21011 +21013 +21017 +21019 +21023 +21031 +21059 +21061 +21067 +21089 +21101 +21107 +21121 +21139 +21143 +21149 +21157 +21163 +21169 +21179 +21187 +21191 +21193 +21211 +21221 +21227 +21247 +21269 +21277 +21283 +21313 +21317 +21319 +21323 +21341 +21347 +21377 +21379 +21383 +21391 +21397 +21401 +21407 +21419 +21433 +21467 +21481 +21487 +21491 +21493 +21499 +21503 +21517 +21521 +21523 +21529 +21557 +21559 +21563 +21569 +21577 +21587 +21589 +21599 +21601 +21611 +21613 +21617 +21647 +21649 +21661 +21673 +21683 +21701 +21713 +21727 +21737 +21739 +21751 +21757 +21767 +21773 +21787 +21799 +21803 +21817 +21821 +21839 +21841 +21851 +21859 +21863 +21871 +21881 +21893 +21911 +21929 +21937 +21943 +21961 +21977 +21991 +21997 +22003 +22013 +22027 +22031 +22037 +22039 +22051 +22063 +22067 +22073 +22079 +22091 +22093 +22109 +22111 +22123 +22129 +22133 +22147 +22153 +22157 +22159 +22171 +22189 +22193 +22229 +22247 +22259 +22271 +22273 +22277 +22279 +22283 +22291 +22303 +22307 +22343 +22349 +22367 +22369 +22381 +22391 +22397 +22409 +22433 +22441 +22447 +22453 +22469 +22481 +22483 +22501 +22511 +22531 +22541 +22543 +22549 +22567 +22571 +22573 +22613 +22619 +22621 +22637 +22639 +22643 +22651 +22669 +22679 +22691 +22697 +22699 +22709 +22717 +22721 +22727 +22739 +22741 +22751 +22769 +22777 +22783 +22787 +22807 +22811 +22817 +22853 +22859 +22861 +22871 +22877 +22901 +22907 +22921 +22937 +22943 +22961 +22963 +22973 +22993 +23003 +23011 +23017 +23021 +23027 +23029 +23039 +23041 +23053 +23057 +23059 +23063 +23071 +23081 +23087 +23099 +23117 +23131 +23143 +23159 +23167 +23173 +23189 +23197 +23201 +23203 +23209 +23227 +23251 +23269 +23279 +23291 +23293 +23297 +23311 +23321 +23327 +23333 +23339 +23357 +23369 +23371 +23399 +23417 +23431 +23447 +23459 +23473 +23497 +23509 +23531 +23537 +23539 +23549 +23557 +23561 +23563 +23567 +23581 +23593 +23599 +23603 +23609 +23623 +23627 +23629 +23633 +23663 +23669 +23671 +23677 +23687 +23689 +23719 +23741 +23743 +23747 +23753 +23761 +23767 +23773 +23789 +23801 +23813 +23819 +23827 +23831 +23833 +23857 +23869 +23873 +23879 +23887 +23893 +23899 +23909 +23911 +23917 +23929 +23957 +23971 +23977 +23981 +23993 +24001 +24007 +24019 +24023 +24029 +24043 +24049 +24061 +24071 +24077 +24083 +24091 +24097 +24103 +24107 +24109 +24113 +24121 +24133 +24137 +24151 +24169 +24179 +24181 +24197 +24203 +24223 +24229 +24239 +24247 +24251 +24281 +24317 +24329 +24337 +24359 +24371 +24373 +24379 +24391 +24407 +24413 +24419 +24421 +24439 +24443 +24469 +24473 +24481 +24499 +24509 +24517 +24527 +24533 +24547 +24551 +24571 +24593 +24611 +24623 +24631 +24659 +24671 +24677 +24683 +24691 +24697 +24709 +24733 +24749 +24763 +24767 +24781 +24793 +24799 +24809 +24821 +24841 +24847 +24851 +24859 +24877 +24889 +24907 +24917 +24919 +24923 +24943 +24953 +24967 +24971 +24977 +24979 +24989 +25013 +25031 +25033 +25037 +25057 +25073 +25087 +25097 +25111 +25117 +25121 +25127 +25147 +25153 +25163 +25169 +25171 +25183 +25189 +25219 +25229 +25237 +25243 +25247 +25253 +25261 +25301 +25303 +25307 +25309 +25321 +25339 +25343 +25349 +25357 +25367 +25373 +25391 +25409 +25411 +25423 +25439 +25447 +25453 +25457 +25463 +25469 +25471 +25523 +25537 +25541 +25561 +25577 +25579 +25583 +25589 +25601 +25603 +25609 +25621 +25633 +25639 +25643 +25657 +25667 +25673 +25679 +25693 +25703 +25717 +25733 +25741 +25747 +25759 +25763 +25771 +25793 +25799 +25801 +25819 +25841 +25847 +25849 +25867 +25873 +25889 +25903 +25913 +25919 +25931 +25933 +25939 +25943 +25951 +25969 +25981 +25997 +25999 +26003 +26017 +26021 +26029 +26041 +26053 +26083 +26099 +26107 +26111 +26113 +26119 +26141 +26153 +26161 +26171 +26177 +26183 +26189 +26203 +26209 +26227 +26237 +26249 +26251 +26261 +26263 +26267 +26293 +26297 +26309 +26317 +26321 +26339 +26347 +26357 +26371 +26387 +26393 +26399 +26407 +26417 +26423 +26431 +26437 +26449 +26459 +26479 +26489 +26497 +26501 +26513 +26539 +26557 +26561 +26573 +26591 +26597 +26627 +26633 +26641 +26647 +26669 +26681 +26683 +26687 +26693 +26699 +26701 +26711 +26713 +26717 +26723 +26729 +26731 +26737 +26759 +26777 +26783 +26801 +26813 +26821 +26833 +26839 +26849 +26861 +26863 +26879 +26881 +26891 +26893 +26903 +26921 +26927 +26947 +26951 +26953 +26959 +26981 +26987 +26993 +27011 +27017 +27031 +27043 +27059 +27061 +27067 +27073 +27077 +27091 +27103 +27107 +27109 +27127 +27143 +27179 +27191 +27197 +27211 +27239 +27241 +27253 +27259 +27271 +27277 +27281 +27283 +27299 +27329 +27337 +27361 +27367 +27397 +27407 +27409 +27427 +27431 +27437 +27449 +27457 +27479 +27481 +27487 +27509 +27527 +27529 +27539 +27541 +27551 +27581 +27583 +27611 +27617 +27631 +27647 +27653 +27673 +27689 +27691 +27697 +27701 +27733 +27737 +27739 +27743 +27749 +27751 +27763 +27767 +27773 +27779 +27791 +27793 +27799 +27803 +27809 +27817 +27823 +27827 +27847 +27851 +27883 +27893 +27901 +27917 +27919 +27941 +27943 +27947 +27953 +27961 +27967 +27983 +27997 +28001 +28019 +28027 +28031 +28051 +28057 +28069 +28081 +28087 +28097 +28099 +28109 +28111 +28123 +28151 +28163 +28181 +28183 +28201 +28211 +28219 +28229 +28277 +28279 +28283 +28289 +28297 +28307 +28309 +28319 +28349 +28351 +28387 +28393 +28403 +28409 +28411 +28429 +28433 +28439 +28447 +28463 +28477 +28493 +28499 +28513 +28517 +28537 +28541 +28547 +28549 +28559 +28571 +28573 +28579 +28591 +28597 +28603 +28607 +28619 +28621 +28627 +28631 +28643 +28649 +28657 +28661 +28663 +28669 +28687 +28697 +28703 +28711 +28723 +28729 +28751 +28753 +28759 +28771 +28789 +28793 +28807 +28813 +28817 +28837 +28843 +28859 +28867 +28871 +28879 +28901 +28909 +28921 +28927 +28933 +28949 +28961 +28979 +29009 +29017 +29021 +29023 +29027 +29033 +29059 +29063 +29077 +29101 +29123 +29129 +29131 +29137 +29147 +29153 +29167 +29173 +29179 +29191 +29201 +29207 +29209 +29221 +29231 +29243 +29251 +29269 +29287 +29297 +29303 +29311 +29327 +29333 +29339 +29347 +29363 +29383 +29387 +29389 +29399 +29401 +29411 +29423 +29429 +29437 +29443 +29453 +29473 +29483 +29501 +29527 +29531 +29537 +29567 +29569 +29573 +29581 +29587 +29599 +29611 +29629 +29633 +29641 +29663 +29669 +29671 +29683 +29717 +29723 +29741 +29753 +29759 +29761 +29789 +29803 +29819 +29833 +29837 +29851 +29863 +29867 +29873 +29879 +29881 +29917 +29921 +29927 +29947 +29959 +29983 +29989 diff --git a/rsa64/primes.c b/rsa64/primes.c new file mode 100644 index 0000000..6194b23 --- /dev/null +++ b/rsa64/primes.c @@ -0,0 +1,77 @@ +#include "rsa.h" + +uint32_t get_random_bytes(int fd) { + uint32_t ret; + if (read(fd, &ret, sizeof(uint32_t)) == -1) { + exit(1); + } + return ret; +} + +// nn pow e mod mm +uint32_t pow_mod(uint32_t nn, uint32_t e, uint32_t mm) { + uint64_t x = nn; + uint64_t m = mm; + uint64_t y = 1; + + while (e > 1) { + if (e % 2) { + y *= x; + e -= 1; + y = y % m; + } + x *= x; + e /= 2; + x = x % m; + } + return (uint32_t)(x * y % m); +} + +bool is_prime(uint32_t n, uint32_t k_max, int fd) { + uint32_t a = get_random_bytes(fd); + uint32_t d = n - 1; + uint32_t s = 0; + + while ((d & 1) == 0) { + s++; + d = d >> 1; + } + + for (uint32_t k = 0; k < k_max; k++) { + while (a < 2 || a > (n - 2)) { + a = get_random_bytes(fd); + } + uint32_t x = pow_mod(a, d, n); + uint32_t y; + for (uint32_t i = 0; i < s; i++) { + y = pow_mod(x, 2, n); + if (y == 1 && x != 1 && x != n - 1) + return false; + x = y; + } + if (y != 1) { + return false; + } + } + return true; +} + +uint32_t generate_prime_fd(int fd) { + uint32_t n = get_random_bytes(fd); + n |= 1 << 31; + n |= 1; + + while (n % 65537 == 0 || !is_prime(n, 128, fd)) { + n = get_random_bytes(fd); + n |= 1 << 31; + n |= 1; + } + return n; +} + +uint32_t generate_prime() { + int fd = open("/dev/urandom", O_RDONLY); + uint32_t n = generate_prime_fd(fd); + close(fd); + return n; +} diff --git a/rsa64/rsa.c b/rsa64/rsa.c new file mode 100644 index 0000000..2c4c85b --- /dev/null +++ b/rsa64/rsa.c @@ -0,0 +1,49 @@ +#include "rsa.h" + +rsa_t rsa_init(size_t len, bigint_t *primes) { + rsa_t rsa; + + printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); + //printf("Generating p...\n"); + rsa.p = bigint_prime(len / 2, primes); + printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); +//printf("p = %u\n", rsa.p.data[0]); + //printf("Generating q...\n"); + rsa.q = bigint_prime(len / 2, primes); + printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); + //printf("q = %u\n", rsa.q.data[0]); + + + return rsa; +} + +rsa_t rsa_generate_keys(size_t block_size) { + size_t len = block_size / sizeof(uint32_t) / 8; + bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t)); + for (int i = 0; i < 3245; i++) { + primes[i] = bigint_zero(len); + } + int fd = open("primes.0000", O_RDONLY); + char *buf = (char *)malloc(21290 * sizeof(char)); + int ret = read(fd, buf, 21290); + char *tok = strtok(buf, "\n"); + int i = 0; + while (tok) { + primes[i].data[0] = (uint32_t)atoi(tok); + tok = strtok(NULL, "\n"); + i += 1; + } + primes[0].data[0] = 65537; + printf("ret %d\n", ret); + + rsa_t rsa = rsa_init(len, primes); + bigint_destroy(rsa.p); + bigint_destroy(rsa.q); + + for (int i = 0; i < 100; i++) { + printf("%u\n", generate_prime()); + } + + return rsa; +} + diff --git a/rsa64/rsa.h b/rsa64/rsa.h new file mode 100644 index 0000000..bd58963 --- /dev/null +++ b/rsa64/rsa.h @@ -0,0 +1,60 @@ +#ifndef _RSA_H +#define _RSA_H 1 + +#include +#include +#include +#include +#include +#include +#include +#include + +#define RSA_BLOCK_SIZE 128 + +typedef struct bigint_s { + uint32_t *data; + size_t len; +} bigint_t; + +typedef struct rsa_s { + bigint_t p; + bigint_t q; +} rsa_t; + + +void *protected_malloc(size_t size); + +rsa_t rsa_generate_keys(size_t block_size); + + +void bigint_set_random_bytes(bigint_t n, size_t len); +void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len); +void bigint_bitwise_left_shift(bigint_t n); +void bigint_bitwise_right_shift(bigint_t n); +void bigint_decrement(bigint_t n); +int64_t bigint_cmp(bigint_t a, bigint_t b); +bigint_t bigint_prime(size_t len, bigint_t *primes); +void bigint_print(bigint_t n); +bigint_t bigint_new(size_t len); +bigint_t bigint_zero(size_t len); +bigint_t bigint_clone(bigint_t src); +void bigint_add(bigint_t a, bigint_t b); +void custom_bigint_add(bigint_t a, bigint_t b, int index); +bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); +bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); +bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); +void bigint_set_zeros(bigint_t n); + +void bigint_destroy(bigint_t n); + +void array_set_random_bytes(uint32_t *n, size_t size); +void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size); +void array_bitwise_right_shift(uint32_t *a, size_t len); +void array_bitwise_right_shift(uint32_t *a, size_t len); +void array_decrement(uint32_t *a, size_t len); + +uint32_t generate_prime(); + +#endif + diff --git a/rsa64/utils.c b/rsa64/utils.c new file mode 100644 index 0000000..cc99207 --- /dev/null +++ b/rsa64/utils.c @@ -0,0 +1,9 @@ +#include "rsa.h" + +void *protected_malloc(size_t size) { + void *ptr = malloc(size); + if (!ptr) { + printf("malloc returned NULL"); + } + return ptr; +} From 6dcf29d9b822f155d813dc242b11cdba68ddd2cc Mon Sep 17 00:00:00 2001 From: gbrochar Date: Thu, 11 Apr 2024 14:39:27 +0200 Subject: [PATCH 27/34] feat: rsa 32bits (16 bits msg no padding) --- rsa64/primes.c | 61 ++++++++++---------- rsa64/rsa.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++-- rsa64/rsa.h | 4 +- 3 files changed, 182 insertions(+), 36 deletions(-) diff --git a/rsa64/primes.c b/rsa64/primes.c index 6194b23..3a4a584 100644 --- a/rsa64/primes.c +++ b/rsa64/primes.c @@ -1,49 +1,47 @@ #include "rsa.h" -uint32_t get_random_bytes(int fd) { - uint32_t ret; - if (read(fd, &ret, sizeof(uint32_t)) == -1) { +uint16_t get_random_bytes(int fd) { + uint16_t ret; + if (read(fd, &ret, sizeof(uint16_t)) == -1) { exit(1); } return ret; } -// nn pow e mod mm -uint32_t pow_mod(uint32_t nn, uint32_t e, uint32_t mm) { - uint64_t x = nn; - uint64_t m = mm; +// n pow e mod m +uint64_t pow_mod(uint64_t n, uint64_t e, uint64_t m) { uint64_t y = 1; while (e > 1) { - if (e % 2) { - y *= x; - e -= 1; - y = y % m; + if (e & 1) { + y = (y * n) % m; } - x *= x; - e /= 2; - x = x % m; + n = (n * n) % m; + e = e >> 1; } - return (uint32_t)(x * y % m); + return (n * y) % m; } -bool is_prime(uint32_t n, uint32_t k_max, int fd) { - uint32_t a = get_random_bytes(fd); - uint32_t d = n - 1; - uint32_t s = 0; +bool is_prime(uint16_t n, size_t k_max, int fd) { + uint16_t a = get_random_bytes(fd); + // a &= 0xFFFF; + uint16_t d = n - 1; + uint16_t s = 0; while ((d & 1) == 0) { s++; d = d >> 1; } - for (uint32_t k = 0; k < k_max; k++) { + for (size_t k = 0; k < k_max; k++) { + a = 0; while (a < 2 || a > (n - 2)) { a = get_random_bytes(fd); + //a &= 0xFFFF; } - uint32_t x = pow_mod(a, d, n); - uint32_t y; - for (uint32_t i = 0; i < s; i++) { + uint16_t x = pow_mod(a, d, n); + uint16_t y; + for (uint16_t i = 0; i < s; i++) { y = pow_mod(x, 2, n); if (y == 1 && x != 1 && x != n - 1) return false; @@ -56,22 +54,25 @@ bool is_prime(uint32_t n, uint32_t k_max, int fd) { return true; } -uint32_t generate_prime_fd(int fd) { - uint32_t n = get_random_bytes(fd); - n |= 1 << 31; +uint16_t generate_prime_fd(int fd) { + uint16_t n = get_random_bytes(fd); + n |= 1 << 15; n |= 1; + //n &= 0xFFFF; - while (n % 65537 == 0 || !is_prime(n, 128, fd)) { + while (/*n % 3 == 0 ||*/ !is_prime(n, 16, fd)) { n = get_random_bytes(fd); - n |= 1 << 31; + n |= 1 << 15; n |= 1; + //n &= 0xFFFF; } return n; } -uint32_t generate_prime() { +uint16_t generate_prime() { int fd = open("/dev/urandom", O_RDONLY); - uint32_t n = generate_prime_fd(fd); + uint16_t n = generate_prime_fd(fd); close(fd); return n; } + diff --git a/rsa64/rsa.c b/rsa64/rsa.c index 2c4c85b..45ae45c 100644 --- a/rsa64/rsa.c +++ b/rsa64/rsa.c @@ -3,20 +3,73 @@ rsa_t rsa_init(size_t len, bigint_t *primes) { rsa_t rsa; - printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); +// printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); //printf("Generating p...\n"); rsa.p = bigint_prime(len / 2, primes); - printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); +// printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); //printf("p = %u\n", rsa.p.data[0]); //printf("Generating q...\n"); rsa.q = bigint_prime(len / 2, primes); - printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); +// printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); //printf("q = %u\n", rsa.q.data[0]); return rsa; } +int64_t euler(int64_t r0, int64_t r1) { + int64_t s0 = 1; + int64_t s1 = 0; + int64_t t0 = 0; + int64_t t1 = 1; + int64_t q0 = 0; + + //printf("" + //printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r0, s0, t0); + //printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r1, s1, t1); + + while (r1 != 0) { + q0 = r0 / r1; + int64_t tmp = r0 % r1; + r0 = r1; + r1 = tmp; + tmp = s0 - q0 * s1; + s0 = s1; + s1 = tmp; + tmp = t0 - q0 * t1; + t0 = t1; + t1 = tmp; + //printf("|% 20ld|% 20ld|% 20ld|% 20ld|\n", q0, r1, s1, t1); + } + return s0; +} + +int64_t euler2(int64_t r0, int64_t r1) { + int64_t s0 = 1; + int64_t s1 = 0; + int64_t t0 = 0; + int64_t t1 = 1; + int64_t q0 = 0; + + //printf("" + printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r0, s0, t0); + printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r1, s1, t1); + + while (r1 != 0) { + q0 = r0 / r1; + int64_t tmp = r0 % r1; + r0 = r1; + r1 = tmp; + tmp = s0 - q0 * s1; + s0 = s1; + s1 = tmp; + tmp = t0 - q0 * t1; + t0 = t1; + t1 = tmp; + printf("|% 20ld|% 20ld|% 20ld|% 20ld|\n", q0, r1, s1, t1); + } + return t0; +} rsa_t rsa_generate_keys(size_t block_size) { size_t len = block_size / sizeof(uint32_t) / 8; bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t)); @@ -40,10 +93,100 @@ rsa_t rsa_generate_keys(size_t block_size) { bigint_destroy(rsa.p); bigint_destroy(rsa.q); - for (int i = 0; i < 100; i++) { - printf("%u\n", generate_prime()); + //int64_t p = 56843;//(uint64_t)generate_prime(); + //int64_t q = 61861;//(uint64_t)generate_prime(); + //int64_t p = 36671; + //int64_t q = 53939; + //int64_t p = 57313; + //int64_t q = 51329; + //int64_t p = 39901; + //int64_t q = 43391; + +// int fd2 = open("/dev/urandom", O_RDONLY); + for (int try = 0; try < 1000; try++) { + + if (try % 100 == 0) + printf("try: %d\n", try); + int64_t p = (uint64_t)generate_prime(); + int64_t q = (uint64_t)generate_prime(); + //p = 63761; + //q = 65003; + int64_t ln = (p - 1) * (q - 1); + int64_t e = 11317; + //e = 11; + + while (ln % e == 0 || p == q) { + p = generate_prime(); + q = generate_prime(); + ln = (p - 1) * (q - 1); } + if (q > p) { + uint64_t tmp = p; + p = q; + q = tmp; + } + int64_t n = p * q; + + int64_t r0 = e; + int64_t r1 = ln; + int64_t s0 = 1; + int64_t s1 = 0; + int64_t t0 = 0; + int64_t t1 = 1; + int64_t q0 = 0; + + while (r1 != 0) { + q0 = r0 / r1; + int64_t tmp = r0 % r1; + r0 = r1; + r1 = tmp; + tmp = s0 - q0 * s1; + s0 = s1; + s1 = tmp; + tmp = t0 - q0 * t1; + t0 = t1; + t1 = tmp; + } + + int64_t d = euler(e, ln) + ln; + if (d > n) { + d -= ln; + } + +/* printf("p: %ld\n", p); + printf("q: %ld\n", q); + printf("ln: %ld\n", ln); + printf("n: %ld\n", n); + printf("d: %ld\n", d); + printf("e: %ld\n", e); + printf("d * e %% ln = %ld\n", (d*e)%ln);*/ + for (uint64_t m = 0; m < 16384; m++) { + //uint64_t m = get_random_bytes(fd2); + uint64_t c = pow_mod(m, e, n); + uint64_t m2 = pow_mod(c, d, n); + if (m != m2) { + printf("ERROR try: %d\nround: n/a\nmsg: %ld\ncypher: %ld\ndecrypted: %ld\nd: %ld\ne: %ld\np: %lu\nq: %lu\nn: %lu\n", try, m, c, m2, d, e, p, q, n); + break; + } + } + //int64_t m = 42; + /* + for (int64_t m = 41; m < 43; m++) { + int64_t c = pow_mod(m, e, n); + int64_t m2 = pow_mod(c, d, n); + if (d < 0) { + int64_t c2 = euler(c, n); + printf("c2: %ld\n", c2); + printf("c2 * c %% n = %ld\n", ((c2 + n)*c)%n); + printf("c2 * c %% n = %ld\n", ((c2)*c)%n); + m2 = pow_mod(c2 + n, -d, n); + } + printf("message: %ld\n", m); + printf("cypher: %ld\n", c); + printf("decrypted: %ld\n", m2); + }*/ + } return rsa; } diff --git a/rsa64/rsa.h b/rsa64/rsa.h index bd58963..8c6b6f6 100644 --- a/rsa64/rsa.h +++ b/rsa64/rsa.h @@ -54,7 +54,9 @@ void array_bitwise_right_shift(uint32_t *a, size_t len); void array_bitwise_right_shift(uint32_t *a, size_t len); void array_decrement(uint32_t *a, size_t len); -uint32_t generate_prime(); +uint16_t generate_prime(); +uint64_t pow_mod(uint64_t nn, uint64_t e, uint64_t mm); +uint16_t get_random_bytes(int fd); #endif From d429e2921da63b10efd9e6f750fc0a0f6edc552d Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 19 May 2024 08:37:43 +0200 Subject: [PATCH 28/34] clean: rsa64 --- .gitignore | 1 + rsa64/Makefile | 2 - rsa64/array.c | 42 ------ rsa64/bigint.c | 342 ------------------------------------------------- rsa64/rsa.c | 51 ++++---- 5 files changed, 28 insertions(+), 410 deletions(-) delete mode 100644 rsa64/array.c delete mode 100644 rsa64/bigint.c diff --git a/.gitignore b/.gitignore index fd55dfd..604f567 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ rsa/rsa +rsa64/rsa *.swp *.o *.a diff --git a/rsa64/Makefile b/rsa64/Makefile index 5cbb4bb..eb4559a 100644 --- a/rsa64/Makefile +++ b/rsa64/Makefile @@ -3,8 +3,6 @@ NAME = rsa SRC = \ main.c \ rsa.c \ - bigint.c \ - array.c \ utils.c \ primes.c \ diff --git a/rsa64/array.c b/rsa64/array.c deleted file mode 100644 index 66947aa..0000000 --- a/rsa64/array.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "rsa.h" - -void array_set_random_bytes(uint32_t *n, size_t size) { - int fd = open("/dev/urandom", O_RDONLY); - if (read(fd, n, size) == -1) { - exit(1); - } -} - -void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size) { - n[0] |= 1; - n[size / sizeof(uint32_t) - 1] |= 1 << 31; -} - -void array_bitwise_right_shift(uint32_t *a, size_t len) { - size_t size = sizeof(uint32_t) * 8 - 1; - for (size_t n = 0; n < len - 1; n++) { - a[n] = a[n] >> 1 | (a[n + 1] & 1) << size; - } - a[len - 1] >>= 1; -} - -void array_bitwise_left_shift(uint32_t *a, size_t len) { - size_t size = sizeof(uint32_t) * 8 - 1; - for (size_t 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(uint32_t *a, size_t len) { - size_t cursor = 0; - size_t size = sizeof(uint32_t) * 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; - } -} diff --git a/rsa64/bigint.c b/rsa64/bigint.c deleted file mode 100644 index 415a604..0000000 --- a/rsa64/bigint.c +++ /dev/null @@ -1,342 +0,0 @@ -#include "rsa.h" - -void my_memcpy(void *dst, void *src, size_t n) { - memcpy(dst, src, n); -} - -void bigint_set_random_bytes(bigint_t n, size_t len) { - int fd = open("/dev/urandom", O_RDONLY); - if (read(fd, n.data, len * sizeof(uint32_t)) == -1) { - exit(1); - } - close(fd); -} - -void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) { - n.data[0] |= 1; - n.data[len - 1] |= 1 << 31; -} - -void bigint_bitwise_right_shift(bigint_t n) { - for (size_t i = 0; i < n.len - 1; i++) { - n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << 31; - } - n.data[n.len - 1] >>= 1; -} - -void bigint_bitwise_left_shift(bigint_t n) { - for (int i = n.len - 1; i > 0; i--) { - n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); - } - n.data[0] <<= 1; -} - -void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) { - for (int i = result.len - 1; i > 0; i--) { - result.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); - } - result.data[0] = n.data[0] << 1; -} - -// Will underflow -void bigint_decrement(bigint_t n) { - size_t cursor = 0; - while (cursor < n.len << 5) { - n.data[cursor >> 32] = n.data[cursor >> 5] ^ (1 << (cursor % 32)); - if (((n.data[cursor >> 5] >> (cursor % 32)) & 1) == 0) { - return; - } - cursor += 1; - } -} - -// TODO refactor/clean assume same length ? -int64_t bigint_cmp(bigint_t a, bigint_t b) { - int cursor = a.len - 1; - while (cursor >= 0) { - if (a.data[cursor] > b.data[cursor]) { - return 1; - } - if (b.data[cursor] > a.data[cursor]) { - return -1; - } - cursor -= 1; - } - - return 0; -} - -// TODO refactor/clean assume same length ? -int bigint_dif(bigint_t a, bigint_t b) { - int cursor = a.len; - while (--cursor >= 0) { - if (a.data[cursor] ^ b.data[cursor]) { - return 1; - } - } - - return 0; -} - -int is_zero(bigint_t n) { - for (size_t i = 0; i < n.len; i++) { - if (n.data[i]) { - return 0; - } - } - return 1; -} - -// TODO check opti -void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { - my_memcpy(y.data, b.data, b.len * sizeof(uint32_t)); - while (!is_zero(y)) { - for (size_t i = 0; i < a.len; i++) { - borrow.data[i] = ~a.data[i] & y.data[i]; - a.data[i] = a.data[i] ^ y.data[i]; - } - move_bigint_bitwise_left_shift(borrow, y); - } -} - -// TODO check opti -void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { - bigint_set_zeros(result); - my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - if (bigint_cmp(result, b) < 0) { - return ; - } - while (bigint_cmp(result, mod) > 0) { - bigint_bitwise_left_shift(mod); - } - while (bigint_cmp(b, mod) < 0) { - bigint_bitwise_right_shift(mod); - if (bigint_cmp(result, mod) > 0) { - bigint_substraction(result, mod, borrow_sub, y_sub); - } - } - while (bigint_cmp(result, b) > -1) { - bigint_substraction(result, b, borrow_sub, y_sub); - } -} - -bigint_t bigint_new(size_t len) { - bigint_t bigint; - - bigint.len = len; - bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t)); - - return bigint; -} - -bigint_t bigint_zero(size_t len) { - bigint_t bigint; - - bigint = bigint_new(len); - for (size_t i = 0; i < len; i++) { - bigint.data[i] = 0; - } - - return bigint; -} - -bigint_t bigint_clone(bigint_t src) { - bigint_t dst; - - dst.len = src.len; - dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t)); - my_memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); - - return dst; -} - -void bigint_destroy(bigint_t n) { - free(n.data); - n.data = NULL; -} - -void custom_bigint_add(bigint_t a, bigint_t b, int index) { - uint64_t carriage = 0; - - for (size_t cursor = 0; cursor < a.len; cursor++) { - uint64_t tmp = (uint64_t)a.data[cursor] + carriage; - if ((int)cursor - index >= 0) { - tmp += (uint64_t)b.data[cursor - index]; - } - a.data[cursor] = (uint32_t)tmp; - carriage = tmp >> 32; - } -} - -void bigint_set_zeros(bigint_t n) { - for (size_t i = 0; i < n.len; i++) { - n.data[i] = 0; - } -} - -void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t *b_tool) { - int width = a.len * 32; - bigint_set_zeros(result); - bigint_set_zeros(b_tool[0]); - my_memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); - - for (int i = 1; i < 32; i++) { - bigint_set_zeros(b_tool[i]); - my_memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); - bigint_bitwise_left_shift(b_tool[i]); - } - - for (int cursor = 0; cursor < width; cursor++) { - int offset = cursor % 32; - int index = cursor >> 5; - if (a.data[index] >> offset & 1) { - custom_bigint_add(result, b_tool[offset], index); - } - } -} - -// a^e mod n -// clean memory tricks !!! -void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool) { - bigint_set_zeros(result); - bigint_set_zeros(custom); - bigint_set_zeros(custom2); - my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - int cursor = (e.len << 5) - 1; - while (!(e.data[cursor >> 5] & 1 << (cursor % 32))) { - cursor--; - } - cursor--; - while (cursor >= 0) { - custom_bigint_mul(result, result, custom, b_tool); - custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); - bigint_set_zeros(result); - my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - if (e.data[cursor >> 5] & 1 << (cursor % 32)) { - custom_bigint_mul(result, a, custom, b_tool); - custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); - my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - } - cursor -= 1; - } -} - -void bigint_print(bigint_t n) { - for (int i = n.len - 1; i >= 0; i--) { - printf("bigint %ud\n", n.data[i]); - } -} - - -void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t one, bigint_t n_minus_two, bigint_t n_minus_one) { - bigint_destroy(x); - bigint_destroy(y); - bigint_destroy(n); - bigint_destroy(d); - bigint_destroy(two); - bigint_destroy(one); - bigint_destroy(n_minus_two); - bigint_destroy(n_minus_one); -} - -int prime_division(bigint_t *primes, bigint_t n, bigint_t mod, bigint_t custom2, bigint_t borrow_sub, bigint_t y_sub) { - bigint_set_zeros(mod); - for (int i = 0; i < 200; i++) { - mod.data[0] = primes[i].data[0]; - custom_bigint_modulo(n, primes[i], custom2, mod, borrow_sub, y_sub); - if (is_zero(custom2)) { - return 1; - } - } - return 0; -} - -int miller_rabin(size_t len, bigint_t a, bigint_t two, bigint_t n_minus_two, bigint_t d, bigint_t n, bigint_t x, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool, bigint_t n_minus_one, uint32_t s, bigint_t y, bigint_t one) { - for (uint32_t k = 0; k < 20; k++) { - bigint_set_zeros(a); - while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { - bigint_set_random_bytes(a, len); - } - custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool); - for (uint32_t i = 0; i < s; i++) { - custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool); - if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { - return 0; - } - bigint_destroy(x); - x = bigint_clone(y); - } - if (bigint_dif(y, one)) { - return 0; - } - } - return 1; -} - -bigint_t bigint_prime(size_t len, bigint_t *primes) { - size_t my_size = len * 2; - - bigint_t n = bigint_zero(my_size); - - bigint_set_random_bytes(n, len); - bigint_set_msb_and_lsb_to_one(n, len); - bigint_t mod = bigint_clone(n); - bigint_t borrow_sub = bigint_clone(n); - bigint_t y_sub = bigint_clone(n); - bigint_t d = bigint_clone(n); - d.data[0] -= 1; - uint32_t s = 0; - while (!(d.data[0] & 1)) { - bigint_bitwise_right_shift(d); - s += 1; - } - - bigint_t x = bigint_zero(my_size); - bigint_t y = bigint_zero(my_size); - bigint_t custom = bigint_zero(my_size); - bigint_t custom2 = bigint_zero(my_size); - - - if (prime_division(primes, n, mod, custom2, borrow_sub, y_sub)) { - bulk_destroy(x, y, n, d, custom, custom2, mod, borrow_sub); - bigint_destroy(y_sub); - return bigint_prime(len, primes); - } - memcpy(mod.data, n.data, n.len * sizeof(uint32_t)); - - bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); - for (int i = 0; i < 32; i++) { - b_tool[i] = bigint_zero(my_size * 2); - } - - bigint_t two = bigint_zero(my_size); - bigint_t one = bigint_zero(my_size); - two.data[0] = 2; - one.data[0] = 1; - bigint_t n_minus_two = bigint_clone(n); - bigint_t n_minus_one = bigint_clone(n); - n_minus_two.data[0] -= 1; - n_minus_one.data[0] -= 1; - bigint_decrement(n_minus_two); - bigint_t a = bigint_zero(my_size); - - int is_prime = miller_rabin(len, a, two, n_minus_two, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool, n_minus_one, s, y, one); - - bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); - bigint_destroy(custom2); - bigint_destroy(a); - bigint_destroy(mod); - bigint_destroy(borrow_sub); - bigint_destroy(y_sub); - for (int i = 0; i < 32; i++) { - bigint_destroy(b_tool[i]); - } - free(b_tool); - - if (is_prime) { - return n; - } - bigint_destroy(n); - return bigint_prime(len, primes); -} - diff --git a/rsa64/rsa.c b/rsa64/rsa.c index 45ae45c..ce066ff 100644 --- a/rsa64/rsa.c +++ b/rsa64/rsa.c @@ -1,22 +1,22 @@ #include "rsa.h" - +/* rsa_t rsa_init(size_t len, bigint_t *primes) { rsa_t rsa; // printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); //printf("Generating p...\n"); - rsa.p = bigint_prime(len / 2, primes); +// rsa.p = bigint_prime(len / 2, primes); // printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); //printf("p = %u\n", rsa.p.data[0]); //printf("Generating q...\n"); - rsa.q = bigint_prime(len / 2, primes); +// rsa.q = bigint_prime(len / 2, primes); // printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); //printf("q = %u\n", rsa.q.data[0]); return rsa; } - +*/ int64_t euler(int64_t r0, int64_t r1) { int64_t s0 = 1; int64_t s1 = 0; @@ -71,27 +71,28 @@ int64_t euler2(int64_t r0, int64_t r1) { return t0; } rsa_t rsa_generate_keys(size_t block_size) { - size_t len = block_size / sizeof(uint32_t) / 8; - bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t)); - for (int i = 0; i < 3245; i++) { - primes[i] = bigint_zero(len); - } - int fd = open("primes.0000", O_RDONLY); - char *buf = (char *)malloc(21290 * sizeof(char)); - int ret = read(fd, buf, 21290); - char *tok = strtok(buf, "\n"); - int i = 0; - while (tok) { - primes[i].data[0] = (uint32_t)atoi(tok); - tok = strtok(NULL, "\n"); - i += 1; - } - primes[0].data[0] = 65537; - printf("ret %d\n", ret); + (void)block_size; +// size_t len = block_size / sizeof(uint32_t) / 8; +// bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t)); +// for (int i = 0; i < 3245; i++) { +// primes[i] = bigint_zero(len); +// } +// int fd = open("primes.0000", O_RDONLY); +// char *buf = (char *)malloc(21290 * sizeof(char)); +// int ret = read(fd, buf, 21290); +// char *tok = strtok(buf, "\n"); +// int i = 0; +// while (tok) { +// primes[i].data[0] = (uint32_t)atoi(tok); +// tok = strtok(NULL, "\n"); +// i += 1; +// } +// primes[0].data[0] = 65537; +// printf("ret %d\n", ret); - rsa_t rsa = rsa_init(len, primes); - bigint_destroy(rsa.p); - bigint_destroy(rsa.q); +// rsa_t rsa = rsa_init(len, primes); +// bigint_destroy(rsa.p); +// bigint_destroy(rsa.q); //int64_t p = 56843;//(uint64_t)generate_prime(); //int64_t q = 61861;//(uint64_t)generate_prime(); @@ -187,6 +188,8 @@ rsa_t rsa_generate_keys(size_t block_size) { printf("decrypted: %ld\n", m2); }*/ } + rsa_t rsa; + rsa.p.len = 42; return rsa; } From 2303d0cb2c2c3dd694df5f92e6408440262620fe Mon Sep 17 00:00:00 2001 From: gbrochar Date: Thu, 23 May 2024 13:14:37 +0200 Subject: [PATCH 29/34] clean: remove old rsa --- rsa/Makefile | 44 ------- rsa/array.c | 42 ------- rsa/bigint.c | 342 --------------------------------------------------- rsa/main.c | 13 -- rsa/rsa.c | 45 ------- rsa/rsa.h | 57 --------- rsa/utils.c | 9 -- 7 files changed, 552 deletions(-) delete mode 100644 rsa/Makefile delete mode 100644 rsa/array.c delete mode 100644 rsa/bigint.c delete mode 100644 rsa/main.c delete mode 100644 rsa/rsa.c delete mode 100644 rsa/rsa.h delete mode 100644 rsa/utils.c diff --git a/rsa/Makefile b/rsa/Makefile deleted file mode 100644 index 6cc1a78..0000000 --- a/rsa/Makefile +++ /dev/null @@ -1,44 +0,0 @@ -NAME = rsa - -SRC = \ - main.c \ - rsa.c \ - bigint.c \ - array.c \ - utils.c \ - -all: $(NAME) - -$(NAME): - gcc -Wall -Wextra -Werror -Wunused-function $(SRC) -o $(NAME) - -fast: - gcc -Wall -Wextra -Werror -Wunused-function -O3 $(SRC) -o $(NAME) - -fast-info: - gcc -Wall -Wextra -Werror -Wunused-function -O3 -fopt-info $(SRC) -o $(NAME) - -really-fast: - gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native $(SRC) -o $(NAME) - -really-fast-info: - gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native -fopt-info $(SRC) -o $(NAME) - -profile: - gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) - -profile-clang: - clang -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) - -profile-fast: - gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) - -profile-fast-clang: - clang -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) - -fclean: - rm -rf $(NAME) - -re: fclean all - -.PHONY: all fast profile profile-fast fclean re diff --git a/rsa/array.c b/rsa/array.c deleted file mode 100644 index 66947aa..0000000 --- a/rsa/array.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "rsa.h" - -void array_set_random_bytes(uint32_t *n, size_t size) { - int fd = open("/dev/urandom", O_RDONLY); - if (read(fd, n, size) == -1) { - exit(1); - } -} - -void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size) { - n[0] |= 1; - n[size / sizeof(uint32_t) - 1] |= 1 << 31; -} - -void array_bitwise_right_shift(uint32_t *a, size_t len) { - size_t size = sizeof(uint32_t) * 8 - 1; - for (size_t n = 0; n < len - 1; n++) { - a[n] = a[n] >> 1 | (a[n + 1] & 1) << size; - } - a[len - 1] >>= 1; -} - -void array_bitwise_left_shift(uint32_t *a, size_t len) { - size_t size = sizeof(uint32_t) * 8 - 1; - for (size_t 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(uint32_t *a, size_t len) { - size_t cursor = 0; - size_t size = sizeof(uint32_t) * 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; - } -} diff --git a/rsa/bigint.c b/rsa/bigint.c deleted file mode 100644 index 415a604..0000000 --- a/rsa/bigint.c +++ /dev/null @@ -1,342 +0,0 @@ -#include "rsa.h" - -void my_memcpy(void *dst, void *src, size_t n) { - memcpy(dst, src, n); -} - -void bigint_set_random_bytes(bigint_t n, size_t len) { - int fd = open("/dev/urandom", O_RDONLY); - if (read(fd, n.data, len * sizeof(uint32_t)) == -1) { - exit(1); - } - close(fd); -} - -void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) { - n.data[0] |= 1; - n.data[len - 1] |= 1 << 31; -} - -void bigint_bitwise_right_shift(bigint_t n) { - for (size_t i = 0; i < n.len - 1; i++) { - n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << 31; - } - n.data[n.len - 1] >>= 1; -} - -void bigint_bitwise_left_shift(bigint_t n) { - for (int i = n.len - 1; i > 0; i--) { - n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); - } - n.data[0] <<= 1; -} - -void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) { - for (int i = result.len - 1; i > 0; i--) { - result.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31); - } - result.data[0] = n.data[0] << 1; -} - -// Will underflow -void bigint_decrement(bigint_t n) { - size_t cursor = 0; - while (cursor < n.len << 5) { - n.data[cursor >> 32] = n.data[cursor >> 5] ^ (1 << (cursor % 32)); - if (((n.data[cursor >> 5] >> (cursor % 32)) & 1) == 0) { - return; - } - cursor += 1; - } -} - -// TODO refactor/clean assume same length ? -int64_t bigint_cmp(bigint_t a, bigint_t b) { - int cursor = a.len - 1; - while (cursor >= 0) { - if (a.data[cursor] > b.data[cursor]) { - return 1; - } - if (b.data[cursor] > a.data[cursor]) { - return -1; - } - cursor -= 1; - } - - return 0; -} - -// TODO refactor/clean assume same length ? -int bigint_dif(bigint_t a, bigint_t b) { - int cursor = a.len; - while (--cursor >= 0) { - if (a.data[cursor] ^ b.data[cursor]) { - return 1; - } - } - - return 0; -} - -int is_zero(bigint_t n) { - for (size_t i = 0; i < n.len; i++) { - if (n.data[i]) { - return 0; - } - } - return 1; -} - -// TODO check opti -void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) { - my_memcpy(y.data, b.data, b.len * sizeof(uint32_t)); - while (!is_zero(y)) { - for (size_t i = 0; i < a.len; i++) { - borrow.data[i] = ~a.data[i] & y.data[i]; - a.data[i] = a.data[i] ^ y.data[i]; - } - move_bigint_bitwise_left_shift(borrow, y); - } -} - -// TODO check opti -void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) { - bigint_set_zeros(result); - my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - if (bigint_cmp(result, b) < 0) { - return ; - } - while (bigint_cmp(result, mod) > 0) { - bigint_bitwise_left_shift(mod); - } - while (bigint_cmp(b, mod) < 0) { - bigint_bitwise_right_shift(mod); - if (bigint_cmp(result, mod) > 0) { - bigint_substraction(result, mod, borrow_sub, y_sub); - } - } - while (bigint_cmp(result, b) > -1) { - bigint_substraction(result, b, borrow_sub, y_sub); - } -} - -bigint_t bigint_new(size_t len) { - bigint_t bigint; - - bigint.len = len; - bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t)); - - return bigint; -} - -bigint_t bigint_zero(size_t len) { - bigint_t bigint; - - bigint = bigint_new(len); - for (size_t i = 0; i < len; i++) { - bigint.data[i] = 0; - } - - return bigint; -} - -bigint_t bigint_clone(bigint_t src) { - bigint_t dst; - - dst.len = src.len; - dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t)); - my_memcpy(dst.data, src.data, src.len * sizeof(uint32_t)); - - return dst; -} - -void bigint_destroy(bigint_t n) { - free(n.data); - n.data = NULL; -} - -void custom_bigint_add(bigint_t a, bigint_t b, int index) { - uint64_t carriage = 0; - - for (size_t cursor = 0; cursor < a.len; cursor++) { - uint64_t tmp = (uint64_t)a.data[cursor] + carriage; - if ((int)cursor - index >= 0) { - tmp += (uint64_t)b.data[cursor - index]; - } - a.data[cursor] = (uint32_t)tmp; - carriage = tmp >> 32; - } -} - -void bigint_set_zeros(bigint_t n) { - for (size_t i = 0; i < n.len; i++) { - n.data[i] = 0; - } -} - -void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t *b_tool) { - int width = a.len * 32; - bigint_set_zeros(result); - bigint_set_zeros(b_tool[0]); - my_memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t)); - - for (int i = 1; i < 32; i++) { - bigint_set_zeros(b_tool[i]); - my_memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t)); - bigint_bitwise_left_shift(b_tool[i]); - } - - for (int cursor = 0; cursor < width; cursor++) { - int offset = cursor % 32; - int index = cursor >> 5; - if (a.data[index] >> offset & 1) { - custom_bigint_add(result, b_tool[offset], index); - } - } -} - -// a^e mod n -// clean memory tricks !!! -void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool) { - bigint_set_zeros(result); - bigint_set_zeros(custom); - bigint_set_zeros(custom2); - my_memcpy(result.data, a.data, a.len * sizeof(uint32_t)); - int cursor = (e.len << 5) - 1; - while (!(e.data[cursor >> 5] & 1 << (cursor % 32))) { - cursor--; - } - cursor--; - while (cursor >= 0) { - custom_bigint_mul(result, result, custom, b_tool); - custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); - bigint_set_zeros(result); - my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - if (e.data[cursor >> 5] & 1 << (cursor % 32)) { - custom_bigint_mul(result, a, custom, b_tool); - custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub); - my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t)); - } - cursor -= 1; - } -} - -void bigint_print(bigint_t n) { - for (int i = n.len - 1; i >= 0; i--) { - printf("bigint %ud\n", n.data[i]); - } -} - - -void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t one, bigint_t n_minus_two, bigint_t n_minus_one) { - bigint_destroy(x); - bigint_destroy(y); - bigint_destroy(n); - bigint_destroy(d); - bigint_destroy(two); - bigint_destroy(one); - bigint_destroy(n_minus_two); - bigint_destroy(n_minus_one); -} - -int prime_division(bigint_t *primes, bigint_t n, bigint_t mod, bigint_t custom2, bigint_t borrow_sub, bigint_t y_sub) { - bigint_set_zeros(mod); - for (int i = 0; i < 200; i++) { - mod.data[0] = primes[i].data[0]; - custom_bigint_modulo(n, primes[i], custom2, mod, borrow_sub, y_sub); - if (is_zero(custom2)) { - return 1; - } - } - return 0; -} - -int miller_rabin(size_t len, bigint_t a, bigint_t two, bigint_t n_minus_two, bigint_t d, bigint_t n, bigint_t x, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool, bigint_t n_minus_one, uint32_t s, bigint_t y, bigint_t one) { - for (uint32_t k = 0; k < 20; k++) { - bigint_set_zeros(a); - while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) { - bigint_set_random_bytes(a, len); - } - custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool); - for (uint32_t i = 0; i < s; i++) { - custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool); - if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) { - return 0; - } - bigint_destroy(x); - x = bigint_clone(y); - } - if (bigint_dif(y, one)) { - return 0; - } - } - return 1; -} - -bigint_t bigint_prime(size_t len, bigint_t *primes) { - size_t my_size = len * 2; - - bigint_t n = bigint_zero(my_size); - - bigint_set_random_bytes(n, len); - bigint_set_msb_and_lsb_to_one(n, len); - bigint_t mod = bigint_clone(n); - bigint_t borrow_sub = bigint_clone(n); - bigint_t y_sub = bigint_clone(n); - bigint_t d = bigint_clone(n); - d.data[0] -= 1; - uint32_t s = 0; - while (!(d.data[0] & 1)) { - bigint_bitwise_right_shift(d); - s += 1; - } - - bigint_t x = bigint_zero(my_size); - bigint_t y = bigint_zero(my_size); - bigint_t custom = bigint_zero(my_size); - bigint_t custom2 = bigint_zero(my_size); - - - if (prime_division(primes, n, mod, custom2, borrow_sub, y_sub)) { - bulk_destroy(x, y, n, d, custom, custom2, mod, borrow_sub); - bigint_destroy(y_sub); - return bigint_prime(len, primes); - } - memcpy(mod.data, n.data, n.len * sizeof(uint32_t)); - - bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t)); - for (int i = 0; i < 32; i++) { - b_tool[i] = bigint_zero(my_size * 2); - } - - bigint_t two = bigint_zero(my_size); - bigint_t one = bigint_zero(my_size); - two.data[0] = 2; - one.data[0] = 1; - bigint_t n_minus_two = bigint_clone(n); - bigint_t n_minus_one = bigint_clone(n); - n_minus_two.data[0] -= 1; - n_minus_one.data[0] -= 1; - bigint_decrement(n_minus_two); - bigint_t a = bigint_zero(my_size); - - int is_prime = miller_rabin(len, a, two, n_minus_two, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool, n_minus_one, s, y, one); - - bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one); - bigint_destroy(custom2); - bigint_destroy(a); - bigint_destroy(mod); - bigint_destroy(borrow_sub); - bigint_destroy(y_sub); - for (int i = 0; i < 32; i++) { - bigint_destroy(b_tool[i]); - } - free(b_tool); - - if (is_prime) { - return n; - } - bigint_destroy(n); - return bigint_prime(len, primes); -} - diff --git a/rsa/main.c b/rsa/main.c deleted file mode 100644 index c1df254..0000000 --- a/rsa/main.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "rsa.h" - -int main(int ac, char **av) { - if (ac == 2) { - (void)av; - rsa_t rsa = rsa_generate_keys(RSA_BLOCK_SIZE); - (void)rsa; - } - else { - printf("usage: ./rsa message\n"); - } - return 0; -} diff --git a/rsa/rsa.c b/rsa/rsa.c deleted file mode 100644 index 22dccbf..0000000 --- a/rsa/rsa.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "rsa.h" - -rsa_t rsa_init(size_t len, bigint_t *primes) { - rsa_t rsa; - - printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); - //printf("Generating p...\n"); - rsa.p = bigint_prime(len / 2, primes); - printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); - //printf("p = %u\n", rsa.p.data[0]); - //printf("Generating q...\n"); - rsa.q = bigint_prime(len / 2, primes); - printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); - //printf("q = %u\n", rsa.q.data[0]); - - - return rsa; -} - -rsa_t rsa_generate_keys(size_t block_size) { - size_t len = block_size / sizeof(uint32_t) / 8; - bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t)); - for (int i = 0; i < 3245; i++) { - primes[i] = bigint_zero(len); - } - int fd = open("primes.0000", O_RDONLY); - char *buf = (char *)malloc(21290 * sizeof(char)); - int ret = read(fd, buf, 21290); - char *tok = strtok(buf, "\n"); - int i = 0; - while (tok) { - primes[i].data[0] = (uint32_t)atoi(tok); - tok = strtok(NULL, "\n"); - i += 1; - } - primes[0].data[0] = 65537; - printf("ret %d\n", ret); - - rsa_t rsa = rsa_init(len, primes); - bigint_destroy(rsa.p); - bigint_destroy(rsa.q); - - return rsa; -} - diff --git a/rsa/rsa.h b/rsa/rsa.h deleted file mode 100644 index 5537b35..0000000 --- a/rsa/rsa.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef _RSA_H -#define _RSA_H 1 - -#include -#include -#include -#include -#include -#include -#include - -#define RSA_BLOCK_SIZE 128 - -typedef struct bigint_s { - uint32_t *data; - size_t len; -} bigint_t; - -typedef struct rsa_s { - bigint_t p; - bigint_t q; -} rsa_t; - - -void *protected_malloc(size_t size); - -rsa_t rsa_generate_keys(size_t block_size); - - -void bigint_set_random_bytes(bigint_t n, size_t len); -void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len); -void bigint_bitwise_left_shift(bigint_t n); -void bigint_bitwise_right_shift(bigint_t n); -void bigint_decrement(bigint_t n); -int64_t bigint_cmp(bigint_t a, bigint_t b); -bigint_t bigint_prime(size_t len, bigint_t *primes); -void bigint_print(bigint_t n); -bigint_t bigint_new(size_t len); -bigint_t bigint_zero(size_t len); -bigint_t bigint_clone(bigint_t src); -void bigint_add(bigint_t a, bigint_t b); -void custom_bigint_add(bigint_t a, bigint_t b, int index); -bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); -bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); -bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); -void bigint_set_zeros(bigint_t n); - -void bigint_destroy(bigint_t n); - -void array_set_random_bytes(uint32_t *n, size_t size); -void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size); -void array_bitwise_right_shift(uint32_t *a, size_t len); -void array_bitwise_right_shift(uint32_t *a, size_t len); -void array_decrement(uint32_t *a, size_t len); - -#endif - diff --git a/rsa/utils.c b/rsa/utils.c deleted file mode 100644 index cc99207..0000000 --- a/rsa/utils.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "rsa.h" - -void *protected_malloc(size_t size) { - void *ptr = malloc(size); - if (!ptr) { - printf("malloc returned NULL"); - } - return ptr; -} From 48020cc5dace0bfb421b7424760c36707d42ab52 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Thu, 23 May 2024 13:21:06 +0200 Subject: [PATCH 30/34] clean: rsa64 comments --- rsa64/main.c | 3 +- rsa64/primes.c | 6 +--- rsa64/rsa.c | 86 +------------------------------------------------- rsa64/rsa.h | 30 +----------------- 4 files changed, 5 insertions(+), 120 deletions(-) diff --git a/rsa64/main.c b/rsa64/main.c index c1df254..7b16f6d 100644 --- a/rsa64/main.c +++ b/rsa64/main.c @@ -3,7 +3,7 @@ int main(int ac, char **av) { if (ac == 2) { (void)av; - rsa_t rsa = rsa_generate_keys(RSA_BLOCK_SIZE); + rsa_t rsa = rsa_generate_keys(); (void)rsa; } else { @@ -11,3 +11,4 @@ int main(int ac, char **av) { } return 0; } + diff --git a/rsa64/primes.c b/rsa64/primes.c index 3a4a584..eba141d 100644 --- a/rsa64/primes.c +++ b/rsa64/primes.c @@ -24,7 +24,6 @@ uint64_t pow_mod(uint64_t n, uint64_t e, uint64_t m) { bool is_prime(uint16_t n, size_t k_max, int fd) { uint16_t a = get_random_bytes(fd); - // a &= 0xFFFF; uint16_t d = n - 1; uint16_t s = 0; @@ -37,7 +36,6 @@ bool is_prime(uint16_t n, size_t k_max, int fd) { a = 0; while (a < 2 || a > (n - 2)) { a = get_random_bytes(fd); - //a &= 0xFFFF; } uint16_t x = pow_mod(a, d, n); uint16_t y; @@ -58,13 +56,11 @@ uint16_t generate_prime_fd(int fd) { uint16_t n = get_random_bytes(fd); n |= 1 << 15; n |= 1; - //n &= 0xFFFF; - while (/*n % 3 == 0 ||*/ !is_prime(n, 16, fd)) { + while (!is_prime(n, 16, fd)) { n = get_random_bytes(fd); n |= 1 << 15; n |= 1; - //n &= 0xFFFF; } return n; } diff --git a/rsa64/rsa.c b/rsa64/rsa.c index ce066ff..1fe8fed 100644 --- a/rsa64/rsa.c +++ b/rsa64/rsa.c @@ -1,22 +1,5 @@ #include "rsa.h" -/* -rsa_t rsa_init(size_t len, bigint_t *primes) { - rsa_t rsa; -// printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); - //printf("Generating p...\n"); -// rsa.p = bigint_prime(len / 2, primes); -// printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); -//printf("p = %u\n", rsa.p.data[0]); - //printf("Generating q...\n"); -// rsa.q = bigint_prime(len / 2, primes); -// printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); - //printf("q = %u\n", rsa.q.data[0]); - - - return rsa; -} -*/ int64_t euler(int64_t r0, int64_t r1) { int64_t s0 = 1; int64_t s1 = 0; @@ -24,10 +7,6 @@ int64_t euler(int64_t r0, int64_t r1) { int64_t t1 = 1; int64_t q0 = 0; - //printf("" - //printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r0, s0, t0); - //printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r1, s1, t1); - while (r1 != 0) { q0 = r0 / r1; int64_t tmp = r0 % r1; @@ -39,82 +18,19 @@ int64_t euler(int64_t r0, int64_t r1) { tmp = t0 - q0 * t1; t0 = t1; t1 = tmp; - //printf("|% 20ld|% 20ld|% 20ld|% 20ld|\n", q0, r1, s1, t1); } return s0; } -int64_t euler2(int64_t r0, int64_t r1) { - int64_t s0 = 1; - int64_t s1 = 0; - int64_t t0 = 0; - int64_t t1 = 1; - int64_t q0 = 0; - - //printf("" - printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r0, s0, t0); - printf("|% 20d|% 20ld|% 20ld|% 20ld|\n", 0, r1, s1, t1); - - while (r1 != 0) { - q0 = r0 / r1; - int64_t tmp = r0 % r1; - r0 = r1; - r1 = tmp; - tmp = s0 - q0 * s1; - s0 = s1; - s1 = tmp; - tmp = t0 - q0 * t1; - t0 = t1; - t1 = tmp; - printf("|% 20ld|% 20ld|% 20ld|% 20ld|\n", q0, r1, s1, t1); - } - return t0; -} -rsa_t rsa_generate_keys(size_t block_size) { - (void)block_size; -// size_t len = block_size / sizeof(uint32_t) / 8; -// bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t)); -// for (int i = 0; i < 3245; i++) { -// primes[i] = bigint_zero(len); -// } -// int fd = open("primes.0000", O_RDONLY); -// char *buf = (char *)malloc(21290 * sizeof(char)); -// int ret = read(fd, buf, 21290); -// char *tok = strtok(buf, "\n"); -// int i = 0; -// while (tok) { -// primes[i].data[0] = (uint32_t)atoi(tok); -// tok = strtok(NULL, "\n"); -// i += 1; -// } -// primes[0].data[0] = 65537; -// printf("ret %d\n", ret); - -// rsa_t rsa = rsa_init(len, primes); -// bigint_destroy(rsa.p); -// bigint_destroy(rsa.q); - - //int64_t p = 56843;//(uint64_t)generate_prime(); - //int64_t q = 61861;//(uint64_t)generate_prime(); - //int64_t p = 36671; - //int64_t q = 53939; - //int64_t p = 57313; - //int64_t q = 51329; - //int64_t p = 39901; - //int64_t q = 43391; - -// int fd2 = open("/dev/urandom", O_RDONLY); +rsa_t rsa_generate_keys(void) { for (int try = 0; try < 1000; try++) { if (try % 100 == 0) printf("try: %d\n", try); int64_t p = (uint64_t)generate_prime(); int64_t q = (uint64_t)generate_prime(); - //p = 63761; - //q = 65003; int64_t ln = (p - 1) * (q - 1); int64_t e = 11317; - //e = 11; while (ln % e == 0 || p == q) { p = generate_prime(); diff --git a/rsa64/rsa.h b/rsa64/rsa.h index 8c6b6f6..38c67ad 100644 --- a/rsa64/rsa.h +++ b/rsa64/rsa.h @@ -22,37 +22,9 @@ typedef struct rsa_s { bigint_t q; } rsa_t; - void *protected_malloc(size_t size); -rsa_t rsa_generate_keys(size_t block_size); - - -void bigint_set_random_bytes(bigint_t n, size_t len); -void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len); -void bigint_bitwise_left_shift(bigint_t n); -void bigint_bitwise_right_shift(bigint_t n); -void bigint_decrement(bigint_t n); -int64_t bigint_cmp(bigint_t a, bigint_t b); -bigint_t bigint_prime(size_t len, bigint_t *primes); -void bigint_print(bigint_t n); -bigint_t bigint_new(size_t len); -bigint_t bigint_zero(size_t len); -bigint_t bigint_clone(bigint_t src); -void bigint_add(bigint_t a, bigint_t b); -void custom_bigint_add(bigint_t a, bigint_t b, int index); -bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); -bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); -bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); -void bigint_set_zeros(bigint_t n); - -void bigint_destroy(bigint_t n); - -void array_set_random_bytes(uint32_t *n, size_t size); -void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size); -void array_bitwise_right_shift(uint32_t *a, size_t len); -void array_bitwise_right_shift(uint32_t *a, size_t len); -void array_decrement(uint32_t *a, size_t len); +rsa_t rsa_generate_keys(); uint16_t generate_prime(); uint64_t pow_mod(uint64_t nn, uint64_t e, uint64_t mm); From c9d07e22a9ad6c18fd3891e2b30ac6cb7371aaf0 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sat, 15 Jun 2024 12:43:59 +0200 Subject: [PATCH 31/34] feat(print.s): decypher blocks --- Makefile | 10 ++-- includes/rsa.h | 29 ++++++++++++ includes/woody.h | 7 ++- print.s | 118 ++++++++++++++++++++++++++++++++++++++--------- srcs/encrypt.c | 33 +++++++++++-- srcs/primes.c | 74 +++++++++++++++++++++++++++++ srcs/rsa.c | 53 +++++++++++++++++++++ srcs/woody.c | 35 +++++++++++--- zreset_woody.sh | 1 + 9 files changed, 319 insertions(+), 41 deletions(-) create mode 100644 includes/rsa.h create mode 100644 srcs/primes.c create mode 100644 srcs/rsa.c create mode 100755 zreset_woody.sh diff --git a/Makefile b/Makefile index 6ecb028..f51ff5f 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,9 @@ SRCS_PATH = srcs/ SRCS = $(SRCS_PATH)main.c \ $(SRCS_PATH)utils.c \ $(SRCS_PATH)woody.c \ - $(SRCS_PATH)encrypt.c + $(SRCS_PATH)encrypt.c \ + $(SRCS_PATH)rsa.c \ + $(SRCS_PATH)primes.c OBJS = ${SRCS:.c=.o} @@ -23,7 +25,7 @@ all: ${NAME} .c.o: ${CC} ${INCLUDES} ${DEFINES} ${CFLAGS} -c $< -o $@ -$(NAME): ${OBJS} +$(NAME): ${OBJS} includes/woody.h make -C ft_printf ${CC} ${OBJS} ${LIBFT_FLAGS} -o ${NAME} @@ -31,11 +33,11 @@ clean: make -C ft_printf clean ${RM} ${OBJS} -fclean: +fclean: clean make -C ft_printf fclean ${RM} ${NAME} re: fclean make all -.PHONY : all clean fclean re \ No newline at end of file +.PHONY : all clean fclean re diff --git a/includes/rsa.h b/includes/rsa.h new file mode 100644 index 0000000..4131693 --- /dev/null +++ b/includes/rsa.h @@ -0,0 +1,29 @@ +#ifndef _RSA_H +#define _RSA_H 1 + +#include +#include +#include +#include +#include +#include +#include +#include + +#define RSA_BLOCK_SIZE 128 + +typedef struct rsa_s { + uint64_t n; + uint64_t d; +} rsa_t; + +void *protected_malloc(size_t size); + +rsa_t rsa_generate_keys(); + +uint16_t generate_prime(); +uint64_t pow_mod(uint64_t nn, uint64_t e, uint64_t mm); +uint16_t get_random_bytes(int fd); + +#endif + diff --git a/includes/woody.h b/includes/woody.h index 0833ae4..01d898e 100644 --- a/includes/woody.h +++ b/includes/woody.h @@ -1,6 +1,8 @@ #ifndef WOODY_H #define WOODY_H +#include "rsa.h" + #include "../ft_printf/includes/ft_printf.h" #include #include @@ -17,10 +19,11 @@ #define JUMP "\xe9" #define WOODY "....WOODY...." -#define JUMP_VALUE "\xda\xda\xda" +#define JUMP_VALUE "\xda\xda" #define TEXT_OFFSET "\xba\xba\xba\xba\xba\xba\xba\xba" #define SECTION_SIZE "\xca\xca\xca\xca\xca\xca\xca\xca" +#define PRIVATE_KEY "\xcd\xab\xef\xcd\xab\xef\xcd\xab" typedef struct payload { @@ -49,7 +52,7 @@ int get_symbols_count(int sh_size, int sh_entsize); int prepare_injection(t_elf_content *woody); // encrypt.c -void encrypt(char *file, unsigned long int offset, unsigned long int size); +unsigned long encrypt(char *file, unsigned long int offset, unsigned long int size, rsa_t rsa); #endif diff --git a/print.s b/print.s index 0b5475f..1c0c26e 100644 --- a/print.s +++ b/print.s @@ -2,36 +2,108 @@ bits 64 global _start _start: - push rax - push rdi - push rsi - push rdx + push rbp + push rsp + push rbx + push r12 + push r13 + push r14 + push r15 mov rdi, 1 lea rsi, [rel msg] - mov rax, rsi - sub rax, qword [rel text_section] ;text_section address - mov r8, qword [rel section_sisze] ;text_section size + mov rbx, rsi + sub rbx, qword [rel text_section] ;text_section address because of this and that + mov r8, qword [rel section_size] ;text_section size mov r9, 0 ;increment register + mov r10, 0 ;increment register xor r10, r10 - encrypt: - cmp r8, r9 - je end_encrypt - movzx r10, byte[rax + r9] - inc r10b ;rot + 1 - mov byte[rax + r9], r10b - inc r9 - jmp encrypt - end_encrypt: + xor r13, r13 + mov r13d, dword [rel private_key] + xor r12, r12 + mov r12d, dword [rel private_key + 4] + ;shr r12, 32 + push r13 ; push rsa.d + push r12 ; push rsa.n + jmp decrypt_loop + + ; rbx is adress of text(encrypted) section + ; r8 is section size + ; r9 is index + ; rax is cypher that needs to be converted to message + ; dword [rsp + 16] is rsa.d + ; dword [rsp + 8] is rsa.n + ; qword [rsp] is cypher backup + decrypt_once: + mov r11, 0x100000000 + sq_mul_bit_index: + shr r11, 1 + mov r12, r11 + and r12, qword [rsp + 16] + jz sq_mul_bit_index + sq_mul_loop: + shr r11, 1 + cmp r11, 0 + je decrypt_loop2 + mul rax, + ; modulo n ... + mov r13, qword [rsp + 8] + xor rdx, rdx + div r13 + mov rax, rdx + ; modulo n ... + mov r12, r11 + and r12, qword [rsp + 16] + cmp r12, 0 + je sq_mul_loop + mov r13, qword [rsp] + mul r13 + ; modulo n ... + mov r13, qword [rsp + 8] + xor rdx, rdx + div r13 + mov rax, rdx + ; modulo n ... + jmp sq_mul_loop + + decrypt_loop: + cmp r8, r10 + je end_decrypt + xor rax, rax + mov eax, dword [rbx + r9] + push rax + ;push r10 + jmp decrypt_once + decrypt_loop2: + sub rax, 42 ; remove 42 of result (avoid 0 values) + sub rax, r10 ; remove index of result (caesar like cypher so 0/42 values are differents) + ; unpadding and write back here + ;mov [rbx + r9], rax + ; unpadding and write back here + pop rax + add r9, 4 + inc r10 + jmp decrypt_loop + + end_decrypt: mov rdx, 14 mov rax, 1 syscall - pop rdx - pop rsi - pop rdi - pop rax - jmp 0xdadadada - msg db "....WOODY....",10 + pop r12 ; pop rsa.n + pop r12 ; pop rsa.d + + + pop r15 + pop r14 + pop r13 + pop r12 + pop rbx + pop rsp + pop rbp + + jmp 0xdadadada ; this needs to be just before that + msg db "....WOODY....",10 ; that needs to be just after this text_section dq 0xbabababababababa - section_sisze dq 0xcacacacacacacaca + section_size dq 0xcacacacacacacaca + private_key dq 0xabcdefabcdefabcd diff --git a/srcs/encrypt.c b/srcs/encrypt.c index c57c8f1..abbe67f 100644 --- a/srcs/encrypt.c +++ b/srcs/encrypt.c @@ -1,15 +1,38 @@ #include "../includes/woody.h" +#include "../includes/rsa.h" -void encrypt(char *file, unsigned long int offset, unsigned long int size) +unsigned long encrypt(char *file, unsigned long int offset, unsigned long int size, rsa_t rsa) { + size_t padded_len = size * sizeof(char) * 33 / sizeof(uint32_t) / 32 + 1; // every 32 octet one padding octet, plus one for the remainder (uses too much memory for size % 128 == 0 but fuck you) + uint32_t *padded = (uint32_t *)malloc(sizeof(uint32_t) * padded_len); + for (size_t i = 0; i < padded_len; i++) { + padded[padded_len] = 0; + } + (void)rsa; size_t i = 0; - while (i < size) - { - file[offset + i] = file[offset + i] - 1; + while (i < size) { + size_t j = 0; + while (j < 8) { + size_t bit_index = i * 8 * sizeof(char) + j; + //printf("bit_index : %ld\n", bit_index); + padded[bit_index / 31] += (1 & (file[bit_index / 8] >> j)) << (bit_index % 31); + j++; + } + //file[offset + i] = file[offset + i] - 1; ++i; } + for (size_t i = 0; i < padded_len; i++) { + printf("block : %x\n", padded[i]);//, padded[i]); + padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n); + printf("encrypted block : %x\n\n", padded[i]);//, padded[i]); + //printf("decipher block : %lu (%lx)\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i, pow_mod(padded[i], rsa.d, rsa.n) - 42 - i); + } + memcpy(&file[offset], padded, padded_len * sizeof(uint32_t)); printf("\nENCRYPTION : \n"); printf(" File encrypted from %ld (%lx) to %ld (%lx)\n", offset, offset, offset + size, offset + size); printf(" Size of encryption = %ld (%lx)\n", size, size); + printf(" Size of padded encryption = %ld (%lx)\n", padded_len * sizeof(uint32_t), padded_len * sizeof(uint32_t)); printf("\n"); -} \ No newline at end of file + return offset + padded_len * sizeof(uint32_t); +} + diff --git a/srcs/primes.c b/srcs/primes.c new file mode 100644 index 0000000..a95ac06 --- /dev/null +++ b/srcs/primes.c @@ -0,0 +1,74 @@ +#include "../includes/rsa.h" + +uint16_t get_random_bytes(int fd) { + uint16_t ret; + if (read(fd, &ret, sizeof(uint16_t)) == -1) { + exit(1); + } + return ret; +} + +// n pow e mod m +uint64_t pow_mod(uint64_t n, uint64_t e, uint64_t m) { + uint64_t y = 1; + + while (e > 1) { + if (e & 1) { + y = (y * n) % m; + } + n = (n * n) % m; + e = e >> 1; + } + return (n * y) % m; +} + +bool is_prime(uint16_t n, size_t k_max, int fd) { + uint16_t a = get_random_bytes(fd); + uint16_t d = n - 1; + uint16_t s = 0; + + while ((d & 1) == 0) { + s++; + d = d >> 1; + } + + for (size_t k = 0; k < k_max; k++) { + a = 0; + while (a < 2 || a > (n - 2)) { + a = get_random_bytes(fd); + } + uint16_t x = pow_mod(a, d, n); + uint16_t y; + for (uint16_t i = 0; i < s; i++) { + y = pow_mod(x, 2, n); + if (y == 1 && x != 1 && x != n - 1) + return false; + x = y; + } + if (y != 1) { + return false; + } + } + return true; +} + +uint16_t generate_prime_fd(int fd) { + uint16_t n = get_random_bytes(fd); + n |= 1 << 15; + n |= 1; + + while (!is_prime(n, 16, fd)) { + n = get_random_bytes(fd); + n |= 1 << 15; + n |= 1; + } + return n; +} + +uint16_t generate_prime() { + int fd = open("/dev/urandom", O_RDONLY); + uint16_t n = generate_prime_fd(fd); + close(fd); + return n; +} + diff --git a/srcs/rsa.c b/srcs/rsa.c new file mode 100644 index 0000000..b1db1e3 --- /dev/null +++ b/srcs/rsa.c @@ -0,0 +1,53 @@ +#include "../includes/rsa.h" + +int64_t euler(int64_t r0, int64_t r1) { + int64_t s0 = 1; + int64_t s1 = 0; + int64_t t0 = 0; + int64_t t1 = 1; + int64_t q0 = 0; + + while (r1 != 0) { + q0 = r0 / r1; + int64_t tmp = r0 % r1; + r0 = r1; + r1 = tmp; + tmp = s0 - q0 * s1; + s0 = s1; + s1 = tmp; + tmp = t0 - q0 * t1; + t0 = t1; + t1 = tmp; + } + return s0; +} + +rsa_t rsa_generate_keys(void) { + int64_t p = (uint64_t)generate_prime(); + int64_t q = (uint64_t)generate_prime(); + int64_t ln = (p - 1) * (q - 1); + int64_t e = 11317; + + while (ln % e == 0 || p == q) { + p = generate_prime(); + q = generate_prime(); + ln = (p - 1) * (q - 1); + } + + if (q > p) { + uint64_t tmp = p; + p = q; + q = tmp; + } + + int64_t n = p * q; + int64_t d = euler(e, ln) + ln; + if (d > n) { + d -= ln; + } + rsa_t rsa; + rsa.d = d; + rsa.n = n; + return rsa; +} + diff --git a/srcs/woody.c b/srcs/woody.c index 580758c..83722d5 100644 --- a/srcs/woody.c +++ b/srcs/woody.c @@ -1,4 +1,5 @@ #include "../includes/woody.h" +#include "../includes/rsa.h" int elf_magic_numbers(char *str) { @@ -92,18 +93,25 @@ t_payload *get_payload() exit(1); } payload->len = read(fd, buffer, 1024); + printf("payload len%ld\n", payload->len); payload->payload = malloc(sizeof(char) * payload->len); ft_memcpy(payload->payload, buffer, payload->len); return payload; } -int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position, int load_segment_index) +int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_position, int load_segment_index, rsa_t rsa) { + (void)rsa; + //printf("salut %s\n", JUMP_VALUE); + for (size_t i = 0; i < payload->len; i++) { + printf("%c", *(payload->payload + i)); + } char *ptr_jmp_value = ft_strnstr_nullterminated(payload->payload, JUMP_VALUE, payload->len); char *ptr_woody = ft_strnstr_nullterminated(payload->payload, WOODY, payload->len); char *ptr_text_section = ft_strnstr_nullterminated(payload->payload, TEXT_OFFSET, payload->len); + char *ptr_private_key = ft_strnstr_nullterminated(payload->payload, PRIVATE_KEY, payload->len); char *ptr_section_size = ft_strnstr_nullterminated(payload->payload, SECTION_SIZE, payload->len); - if (ptr_jmp_value && ptr_woody && ptr_text_section && ptr_section_size) + if (ptr_jmp_value && ptr_woody && ptr_text_section && ptr_section_size && ptr_private_key) { int32_t woody_index = ptr_woody - payload->payload; int32_t jmp_index = ptr_jmp_value - sizeof(JUMP) - payload->payload; @@ -118,14 +126,24 @@ int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_posi int64_t section_value = woody->Phdr[load_segment_index].p_memsz; //woody->text_section->sh_size; ft_memcpy(&payload->payload[section_index], §ion_value, sizeof(section_value)); + int64_t private_key_index = ptr_private_key - payload->payload; + int64_t private_key_value = (rsa.n << 32) + rsa.d; + ft_memcpy(&payload->payload[private_key_index], &private_key_value, sizeof(uint64_t)); + + ft_memcpy(woody->file + payload_position, payload->payload, payload->len); - printf("Old entry : %ld (%lx)\n", woody->Ehdr->e_entry, woody->Ehdr->e_entry); printf("Code cave start = %ld (%lx)\n", payload_position, payload_position); printf("Payload size = %ld (%lx)\n", payload->len, payload->len); printf("Backward offset = %d (%x)(%x)\n", jump_value, jump_value, -jump_value); return EXIT_SUCCESS; } + printf("c'est la merde\n"); + printf("ptr_woody : %p\n", ptr_woody); + printf("ptr_section_size: %p\n", ptr_section_size); + printf("ptr_text_section : %p\n", ptr_text_section); + printf("ptr_jmp_value : %p\n", ptr_jmp_value); + printf("ptr_private_key: %p\n", ptr_private_key); return EXIT_FAILURE; } @@ -148,12 +166,15 @@ void inject(t_elf_content *woody) { payload_position = create_codecave(woody, &woody->Phdr[i], payload); } - encrypt(woody->file, woody->Phdr[i].p_offset, woody->Phdr[i].p_memsz); - insert_payload(woody, payload, payload_position, i); + rsa_t rsa = rsa_generate_keys(); + printf("key n : %ld (%lx) key d %ld (%lx), key total : %ld (%lx)\n", rsa.n, rsa.n, rsa.d, rsa.d, (rsa.n << 32) + rsa.d, (rsa.n << 32) + rsa.d); + payload_position = encrypt(woody->file, woody->Phdr[i].p_offset, woody->Phdr[i].p_memsz, rsa); + printf("Payload position : %ld (%lx)\n", payload_position, payload_position); + insert_payload(woody, payload, payload_position, i, rsa); woody->Ehdr->e_entry = payload_position; - woody->Phdr[i].p_filesz += payload->len; - woody->Phdr[i].p_memsz += payload->len; + woody->Phdr[i].p_filesz += payload->len + 15; + woody->Phdr[i].p_memsz += payload->len + 15; woody->Phdr[i].p_flags = PF_X | PF_W | PF_R; printf("New entry = %ld (%lx)\n", woody->Ehdr->e_entry, woody->Ehdr->e_entry); } diff --git a/zreset_woody.sh b/zreset_woody.sh new file mode 100755 index 0000000..8837fa8 --- /dev/null +++ b/zreset_woody.sh @@ -0,0 +1 @@ +./gen_payload.sh && rm -f woody && ./woody_woodpacker resources/sample64 | less From 78ff534aee21a00e3dc6a6a79911fc27b1dc86a2 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 16 Jun 2024 16:27:37 +0200 Subject: [PATCH 32/34] feat(print.s): unpadding WIP --- print.s | 26 ++++++++++++++++++++++++-- srcs/encrypt.c | 28 +++++++++++++++++++++------- srcs/woody.c | 16 +++++++++++----- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/print.s b/print.s index 1c0c26e..5fcfbd4 100644 --- a/print.s +++ b/print.s @@ -15,6 +15,11 @@ _start: mov rbx, rsi sub rbx, qword [rel text_section] ;text_section address because of this and that mov r8, qword [rel section_size] ;text_section size + shr r8, 2 + inc r8 + inc r8 + inc r8 + inc r8 mov r9, 0 ;increment register mov r10, 0 ;increment register xor r10, r10 @@ -78,7 +83,25 @@ _start: sub rax, 42 ; remove 42 of result (avoid 0 values) sub rax, r10 ; remove index of result (caesar like cypher so 0/42 values are differents) ; unpadding and write back here - ;mov [rbx + r9], rax + mov dword [rbx + r9], 0 + mov rcx, r10 + mov r15, r10 + shr r15, 5 + shl r15, 2 + inc rcx + shl rcx, 59 + shr rcx, 59 + shl rax, cl + mov r14, r9 + sub r14, r15 + add [rbx + r14], eax + mov rcx, rax + shr rcx, 32 + cmp r9, 0 + je first_block_skip + add [rbx + r14 - 4], ecx + + first_block_skip: ; unpadding and write back here pop rax add r9, 4 @@ -93,7 +116,6 @@ _start: pop r12 ; pop rsa.n pop r12 ; pop rsa.d - pop r15 pop r14 pop r13 diff --git a/srcs/encrypt.c b/srcs/encrypt.c index abbe67f..39b3c9b 100644 --- a/srcs/encrypt.c +++ b/srcs/encrypt.c @@ -10,21 +10,35 @@ unsigned long encrypt(char *file, unsigned long int offset, unsigned long int si } (void)rsa; size_t i = 0; - while (i < size) { + while (i < (size + 4)) { + /*if (i < 8) { + printf("%x\n", file[offset+i]); + }*/ size_t j = 0; + size_t tool = i % 4; + int tool2 = 0; + if (tool == 0) { + tool2 = 3; + } else if (tool == 1) { + tool2 = 1; + } else if (tool == 2) { + tool2 = -1; + } else { + tool2 = -3; + } + while (j < 8) { - size_t bit_index = i * 8 * sizeof(char) + j; - //printf("bit_index : %ld\n", bit_index); - padded[bit_index / 31] += (1 & (file[bit_index / 8] >> j)) << (bit_index % 31); + size_t bit_index = i * 8 + j; + //printf("gonna encrypt index %lu\n", offset + bit_index / 8); + padded[bit_index / 31] += (1 & (file[offset + bit_index / 8 + tool2] >> (7 - j))) << (30 - bit_index % 31); j++; } - //file[offset + i] = file[offset + i] - 1; ++i; } for (size_t i = 0; i < padded_len; i++) { - printf("block : %x\n", padded[i]);//, padded[i]); + printf("block : %x\n", padded[i]); padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n); - printf("encrypted block : %x\n\n", padded[i]);//, padded[i]); + printf("encrypted block : %x\n\n", padded[i]); //printf("decipher block : %lu (%lx)\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i, pow_mod(padded[i], rsa.d, rsa.n) - 42 - i); } memcpy(&file[offset], padded, padded_len * sizeof(uint32_t)); diff --git a/srcs/woody.c b/srcs/woody.c index 83722d5..7f6f6e2 100644 --- a/srcs/woody.c +++ b/srcs/woody.c @@ -103,9 +103,9 @@ int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_posi { (void)rsa; //printf("salut %s\n", JUMP_VALUE); - for (size_t i = 0; i < payload->len; i++) { + /*for (size_t i = 0; i < payload->len; i++) { printf("%c", *(payload->payload + i)); - } + }*/ char *ptr_jmp_value = ft_strnstr_nullterminated(payload->payload, JUMP_VALUE, payload->len); char *ptr_woody = ft_strnstr_nullterminated(payload->payload, WOODY, payload->len); char *ptr_text_section = ft_strnstr_nullterminated(payload->payload, TEXT_OFFSET, payload->len); @@ -113,17 +113,23 @@ int insert_payload(t_elf_content *woody, t_payload *payload, size_t payload_posi char *ptr_section_size = ft_strnstr_nullterminated(payload->payload, SECTION_SIZE, payload->len); if (ptr_jmp_value && ptr_woody && ptr_text_section && ptr_section_size && ptr_private_key) { + printf("payload position %ld (%lx)\n", payload_position, payload_position); + printf("ptr_woody : %p\n", ptr_woody); + printf("ptr_section_size: %p\n", ptr_section_size); + printf("ptr_text_section : %p\n", ptr_text_section); + printf("ptr_jmp_value : %p\n", ptr_jmp_value); + printf("ptr_private_key: %p\n", ptr_private_key); int32_t woody_index = ptr_woody - payload->payload; int32_t jmp_index = ptr_jmp_value - sizeof(JUMP) - payload->payload; - int32_t jump_value = ((payload_position + jmp_index + 5) - woody->Ehdr->e_entry) * -1; // 5 = JUMP SIZE (OPCODE + 4 bytes operand) - ft_memcpy(&payload->payload[jmp_index + 1], &jump_value, sizeof(jump_value)); + int32_t jump_value = ((payload_position + jmp_index + 5 - 1) - woody->Ehdr->e_entry) * -1; // 5 = JUMP SIZE (OPCODE + 4 bytes operand) + ft_memcpy(&payload->payload[jmp_index + 1 - 1], &jump_value, sizeof(jump_value)); int64_t text_index = ptr_text_section - payload->payload; int64_t text_value = payload_position - woody->Phdr[load_segment_index].p_offset + woody_index; ft_memcpy(&payload->payload[text_index], &text_value, sizeof(text_value)); int64_t section_index = ptr_section_size - payload->payload; - int64_t section_value = woody->Phdr[load_segment_index].p_memsz; //woody->text_section->sh_size; + int64_t section_value = woody->Phdr[load_segment_index].p_memsz * 33/32 + 1; //woody->text_section->sh_size; ft_memcpy(&payload->payload[section_index], §ion_value, sizeof(section_value)); int64_t private_key_index = ptr_private_key - payload->payload; From 0f93258a883edeb6eba36e736720b66c9472cd17 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Thu, 20 Jun 2024 08:45:15 +0200 Subject: [PATCH 33/34] feat(print.s): payload should be OK --- print.s | 38 ++++++++++++++++++++++++++++---------- srcs/rsa.c | 2 ++ zreset_woody.sh | 2 +- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/print.s b/print.s index 5fcfbd4..74f57cf 100644 --- a/print.s +++ b/print.s @@ -10,6 +10,16 @@ _start: push r14 push r15 + push rax + push rcx + push rdx + push rsi + push rdi + push r8 + push r9 + push r10 + push r11 + mov rdi, 1 lea rsi, [rel msg] mov rbx, rsi @@ -17,9 +27,6 @@ _start: mov r8, qword [rel section_size] ;text_section size shr r8, 2 inc r8 - inc r8 - inc r8 - inc r8 mov r9, 0 ;increment register mov r10, 0 ;increment register xor r10, r10 @@ -47,16 +54,18 @@ _start: and r12, qword [rsp + 16] jz sq_mul_bit_index sq_mul_loop: + ; check if pow is zero shr r11, 1 cmp r11, 0 je decrypt_loop2 + ; square ... mul rax, ; modulo n ... mov r13, qword [rsp + 8] xor rdx, rdx div r13 mov rax, rdx - ; modulo n ... + ; ... and multiply mov r12, r11 and r12, qword [rsp + 16] cmp r12, 0 @@ -68,7 +77,7 @@ _start: xor rdx, rdx div r13 mov rax, rdx - ; modulo n ... + ; end of loop jmp sq_mul_loop decrypt_loop: @@ -84,22 +93,21 @@ _start: sub rax, r10 ; remove index of result (caesar like cypher so 0/42 values are differents) ; unpadding and write back here mov dword [rbx + r9], 0 - mov rcx, r10 mov r15, r10 shr r15, 5 shl r15, 2 - inc rcx + mov rcx, r10 shl rcx, 59 shr rcx, 59 + inc rcx shl rax, cl mov r14, r9 sub r14, r15 add [rbx + r14], eax - mov rcx, rax - shr rcx, 32 + shr rax, 32 cmp r9, 0 je first_block_skip - add [rbx + r14 - 4], ecx + add [rbx + r14 - 4], eax first_block_skip: ; unpadding and write back here @@ -116,6 +124,16 @@ _start: pop r12 ; pop rsa.n pop r12 ; pop rsa.d + pop r11 + pop r10 + pop r9 + pop r8 + pop rdi + pop rsi + pop rdx + pop rcx + pop rax + pop r15 pop r14 pop r13 diff --git a/srcs/rsa.c b/srcs/rsa.c index b1db1e3..ada2d73 100644 --- a/srcs/rsa.c +++ b/srcs/rsa.c @@ -48,6 +48,8 @@ rsa_t rsa_generate_keys(void) { rsa_t rsa; rsa.d = d; rsa.n = n; + //rsa.d = 104320933; + //rsa.n = 2959006679; return rsa; } diff --git a/zreset_woody.sh b/zreset_woody.sh index 8837fa8..96b5522 100755 --- a/zreset_woody.sh +++ b/zreset_woody.sh @@ -1 +1 @@ -./gen_payload.sh && rm -f woody && ./woody_woodpacker resources/sample64 | less +./gen_payload.sh && rm -f woody && ./woody_woodpacker resources/sample64 > log && xxd woody > dump From d72905c8876118639b9895070d672f923f0caa2f Mon Sep 17 00:00:00 2001 From: gbrochar Date: Tue, 13 Aug 2024 10:23:36 +0200 Subject: [PATCH 34/34] fix: rsa key is now always large enough --- srcs/encrypt.c | 4 ++-- srcs/rsa.c | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/srcs/encrypt.c b/srcs/encrypt.c index 39b3c9b..d5ba3b2 100644 --- a/srcs/encrypt.c +++ b/srcs/encrypt.c @@ -38,8 +38,8 @@ unsigned long encrypt(char *file, unsigned long int offset, unsigned long int si for (size_t i = 0; i < padded_len; i++) { printf("block : %x\n", padded[i]); padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n); - printf("encrypted block : %x\n\n", padded[i]); - //printf("decipher block : %lu (%lx)\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i, pow_mod(padded[i], rsa.d, rsa.n) - 42 - i); + printf("encrypted block : %x\n", padded[i]); + printf("decipher block : %lx\n\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i); } memcpy(&file[offset], padded, padded_len * sizeof(uint32_t)); printf("\nENCRYPTION : \n"); diff --git a/srcs/rsa.c b/srcs/rsa.c index ada2d73..b9e02de 100644 --- a/srcs/rsa.c +++ b/srcs/rsa.c @@ -23,15 +23,19 @@ int64_t euler(int64_t r0, int64_t r1) { } rsa_t rsa_generate_keys(void) { + + int64_t n = 0; int64_t p = (uint64_t)generate_prime(); int64_t q = (uint64_t)generate_prime(); int64_t ln = (p - 1) * (q - 1); int64_t e = 11317; - while (ln % e == 0 || p == q) { + + while (ln % e == 0 || p == q || !(n & (1 << 31))) { p = generate_prime(); q = generate_prime(); ln = (p - 1) * (q - 1); + n = p * q; } if (q > p) { @@ -40,7 +44,6 @@ rsa_t rsa_generate_keys(void) { q = tmp; } - int64_t n = p * q; int64_t d = euler(e, ln) + ln; if (d > n) { d -= ln;