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