Compare commits

...

28 Commits
master ... rsa

Author SHA1 Message Date
gbrochar 032dd53abf merge: update rsa 2024-04-18 08:00:02 +02:00
gbrochar 6dcf29d9b8 feat: rsa 32bits (16 bits msg no padding) 2024-04-11 14:39:27 +02:00
gbrochar 17cd4fde5b feat: rsa 64bits prime gen 2024-04-09 16:55:13 +02:00
gbrochar ee899b8c8c clean: miller-rabin sep fn + set e=65537 2024-02-19 15:15:02 +01:00
gbrochar b4a0432d33 feat: check first primes before miller rabin 2024-02-19 13:01:51 +01:00
gbrochar 0bc6bf62a4 fix: unused return value 2024-02-19 13:00:55 +01:00
gbrochar badc3fd399 opti: remove many memcpy 2024-02-18 18:06:18 +01:00
gbrochar 1d9e2936ac opti: rm malloc in mul and useless cmp in modulo 2024-02-18 17:14:48 +01:00
gbrochar 365c98be7c clean: remove comments 2024-02-18 16:40:16 +01:00
gbrochar 81f8fb3c1f opti: remove many mallocs 2024-02-18 16:38:30 +01:00
gbrochar d368c925fb opti: is_not_zero 2024-02-18 15:55:05 +01:00
gbrochar 2f7d2922c7 fix: substraction 2024-02-18 15:37:37 +01:00
gbrochar c8e5e6bf67 opti: cache left_shifts in multiplication 2024-02-18 14:16:28 +01:00
gbrochar fa5c3d7f96 refacto: make more numbers same length 2024-02-18 02:41:48 +01:00
gbrochar ed45a04df2 chore: PHONY rule 2024-02-18 02:17:52 +01:00
gbrochar 585f9750df fix: put correct flag for opti................. 2024-02-18 00:46:50 +01:00
gbrochar 4c53350bd5 refacto: faster 2024-02-18 00:46:21 +01:00
gbrochar 7d0c774cb7 refacto: speed x10 2024-02-17 20:50:05 +01:00
gbrochar 4a3cbd75b5 fix: leaks 2024-02-16 17:33:46 +01:00
gbrochar 52f65086c2 remove unused func and comments 2024-02-16 16:48:25 +01:00
gbrochar 6806db1c6f feat: prime working rly slow and leaks 2024-02-16 15:57:11 +01:00
gbrochar a000c56ce5 clean: remove comments 2024-02-16 13:33:59 +01:00
gbrochar 3883287e8d feat: square and multiply, a^e mod n 2024-02-16 13:32:06 +01:00
gbrochar 5028b0dd9f rsa reborn mod working 2024-02-15 21:26:40 +01:00
gbrochar 6a077bca3f rsa branch backup 2024-02-15 21:25:35 +01:00
gbrochar 038719bb26 fix: malloc size was too small 2024-02-14 17:18:09 +01:00
gbrochar adf7a34fa6 feat: rsa compiles 2024-02-14 17:17:11 +01:00
gbrochar 5cf2bc357b feat: rsa doesnt compile lol 2024-02-14 17:14:03 +01:00
17 changed files with 4582 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
rsa/rsa
*.swp
*.o
*.a
woody_woodpacker

44
rsa/Makefile Normal file
View File

@ -0,0 +1,44 @@
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

42
rsa/array.c Normal file
View File

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

342
rsa/bigint.c Normal file
View File

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

13
rsa/main.c Normal file
View File

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

45
rsa/rsa.c Normal file
View File

@ -0,0 +1,45 @@
#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;
}

57
rsa/rsa.h Normal file
View File

@ -0,0 +1,57 @@
#ifndef _RSA_H
#define _RSA_H 1
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#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

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

45
rsa64/Makefile Normal file
View File

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

42
rsa64/array.c Normal file
View File

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

342
rsa64/bigint.c Normal file
View File

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

13
rsa64/main.c Normal file
View File

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

3245
rsa64/primes.0000 Normal file

File diff suppressed because it is too large Load Diff

78
rsa64/primes.c Normal file
View File

@ -0,0 +1,78 @@
#include "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);
// a &= 0xFFFF;
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);
//a &= 0xFFFF;
}
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;
//n &= 0xFFFF;
while (/*n % 3 == 0 ||*/ !is_prime(n, 16, fd)) {
n = get_random_bytes(fd);
n |= 1 << 15;
n |= 1;
//n &= 0xFFFF;
}
return n;
}
uint16_t generate_prime() {
int fd = open("/dev/urandom", O_RDONLY);
uint16_t n = generate_prime_fd(fd);
close(fd);
return n;
}

192
rsa64/rsa.c Normal file
View File

@ -0,0 +1,192 @@
#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;
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));
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);
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;
}

62
rsa64/rsa.h Normal file
View File

@ -0,0 +1,62 @@
#ifndef _RSA_H
#define _RSA_H 1
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#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);
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

9
rsa64/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;
}