rsa reborn mod working

This commit is contained in:
gbrochar 2024-02-15 21:26:40 +01:00
parent 6a077bca3f
commit 5028b0dd9f
13 changed files with 434 additions and 458 deletions

18
rsa/Makefile Normal file
View File

@ -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

40
rsa/array.c Normal file
View File

@ -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;
}
}

240
rsa/bigint.c Normal file
View File

@ -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;
}

View File

@ -1,37 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -1,36 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -1,23 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}

84
rsa/rsa.c Normal file
View File

@ -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;
}

View File

@ -1,46 +1,51 @@
#ifndef _RSA_H
#define _RSA_H 1
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
// 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

9
rsa/utils.c Normal file
View File

@ -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;
}