Compare commits
9 Commits
a5cbb82dd9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 04c0bfc5fe | |||
| bc46aabfc5 | |||
| e02326f35f | |||
| f4068f97b4 | |||
| b5f389264a | |||
| fd3700c960 | |||
| de292aba25 | |||
| bcb399deba | |||
| 5317f37fd7 |
9
helper.c
9
helper.c
@@ -2,7 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
uint32_t rand32() { return (rand() << 16) | (rand() & 0xFFFF); }
|
||||
uint32_t rand32() { return (rand() << 16) | (rand() & 0xFFFF) | 1; }
|
||||
|
||||
void print_uint128(unsigned __int128 n) {
|
||||
if (n == 0) {
|
||||
@@ -32,12 +32,14 @@ uint64_t rand64() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
r = (r << 16) | (rand() & 0xFFFF);
|
||||
}
|
||||
|
||||
// Force the bottom bit to 1, this will make all generated primes odd
|
||||
r = r | 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Stitch two 64-bit random numbers into a 128-bit number
|
||||
unsigned __int128 generate_stitched_128() {
|
||||
// Make sure to seed rand() in your main function with srand(time(NULL))
|
||||
uint64_t top_half = rand64();
|
||||
uint64_t bottom_half = rand64();
|
||||
|
||||
@@ -48,10 +50,9 @@ unsigned __int128 generate_stitched_128() {
|
||||
}
|
||||
|
||||
unsigned __int128 generate_prime_candidate() {
|
||||
// 1. Get the raw random bytes
|
||||
unsigned __int128 candidate = generate_stitched_128();
|
||||
|
||||
// 2. Force the bottom bit to 1 (Ensures it is odd)
|
||||
// Force the bottom bit to 1, this will make all generated primes odd
|
||||
candidate = candidate | 1;
|
||||
|
||||
// 3. Force the top bit to 1 (Ensures it is a full 128-bit number)
|
||||
|
||||
49
main.c
49
main.c
@@ -69,6 +69,7 @@ bool prime_test(uint64_t n, int a) {
|
||||
if (n % 2 == 0) {
|
||||
return false;
|
||||
}
|
||||
//
|
||||
|
||||
uint64_t d = n - 1;
|
||||
uint64_t S = 0;
|
||||
@@ -83,6 +84,7 @@ bool prime_test(uint64_t n, int a) {
|
||||
// round 1
|
||||
// 1: a^d =k 1 mod n
|
||||
uint64_t length = 0;
|
||||
// convert exponent to binary to use in quickpow
|
||||
uint64_t *d_binary = dec_to_bin(d, &length);
|
||||
unsigned __int128 first_qp_res = quick_pow(d_binary, a, n, length);
|
||||
|
||||
@@ -99,6 +101,7 @@ bool prime_test(uint64_t n, int a) {
|
||||
// printf("true\n");
|
||||
return true;
|
||||
} else {
|
||||
//^2 with mod n, for each step since the next is the previous's squared
|
||||
first_qp_res = (uint64_t)(((unsigned __int128)first_qp_res * first_qp_res) % n);
|
||||
}
|
||||
}
|
||||
@@ -116,10 +119,7 @@ void *prime_thread_worker(void *arg) {
|
||||
prime_test_t *result_ptr = (prime_test_t *)arg;
|
||||
|
||||
do {
|
||||
result_ptr->prime = rand64();
|
||||
// printf("\nGenerating a new prime number (%p). Candidate: ", result_ptr);
|
||||
// printf("%ju", result_ptr->prime);
|
||||
// printf("\n");
|
||||
result_ptr->prime = rand32();
|
||||
} while (!prime_test(result_ptr->prime, result_ptr->base));
|
||||
|
||||
return NULL;
|
||||
@@ -158,6 +158,7 @@ euklidian_result_t euklidian_algorigthm_extended(uint64_t a, uint64_t b) {
|
||||
__int128 x = k % 2 == 0 ? prev_xk : -prev_xk;
|
||||
__int128 y = k % 2 == 0 ? -prev_yk : prev_yk;
|
||||
|
||||
// the reason this can be casted is that two uint64_t-s cant have a lnko which is bigger than a uint64_t
|
||||
res.lnko = (uint64_t)prev_r;
|
||||
res.x = x;
|
||||
res.y = y;
|
||||
@@ -188,17 +189,6 @@ uint64_t kinai_maradek_tetel(uint64_t *m, uint64_t d, prime_test_t *p, prime_tes
|
||||
euklidian_result_t y = euklidian_algorigthm_extended(Mp, Mq); // in the struct the x will mean the y1 and y will mean the y2
|
||||
|
||||
// if either of them is less a negative number shift them into postive range with with hte modulo
|
||||
// y.x %= p->prime;
|
||||
// if (y.x < 0)
|
||||
// y.x += p->prime;
|
||||
//
|
||||
// y.y %= q->prime;
|
||||
// if (y.y < 0)
|
||||
// y.y += q->prime;
|
||||
//
|
||||
// unsigned __int128 s1 = (c1 * y.x * Mp) % M;
|
||||
// unsigned __int128 s2 = (c2 * y.y * Mq) % M;
|
||||
// return (s1 + s2) % M;
|
||||
uint64_t y1_pos;
|
||||
if (y.x < 0) {
|
||||
y1_pos = p->prime - (uint64_t)(-y.x % p->prime);
|
||||
@@ -226,13 +216,13 @@ uint64_t rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q, uint64_t *ou
|
||||
uint64_t fi_n = (uint64_t)(p->prime - 1) * (q->prime - 1);
|
||||
printf("n: %ju\n", fi_n);
|
||||
|
||||
// 2. kulcsgeneralas
|
||||
// keygen
|
||||
uint64_t e = 65537;
|
||||
// TODO: put this back after debug
|
||||
// do {
|
||||
// e = ran32() % fi_n;
|
||||
// } while (e <= 1 || !prime_test(e, p->base)); // the p and q base is used everywhere anyways, i wont pass in another arg
|
||||
do {
|
||||
e = rand32() % fi_n; // should this go back as a condition inside the while loop?
|
||||
} while (e <= 1 || !prime_test(e, p->base)); // the p and q base is used everywhere anyways, i wont pass in another arg
|
||||
|
||||
// calculate the d value, in eae it will be the y value
|
||||
euklidian_result_t calc_d = euklidian_algorigthm_extended(fi_n, e);
|
||||
|
||||
// if either of them is less a negative number shift them into postive range with with hte modulo
|
||||
@@ -247,6 +237,7 @@ uint64_t rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q, uint64_t *ou
|
||||
*out_d = d;
|
||||
|
||||
uint64_t length = 0;
|
||||
// m^e mod n
|
||||
uint64_t *nyenye = dec_to_bin(e, &length);
|
||||
uint64_t c = quick_pow(nyenye, *m, n, length);
|
||||
free(nyenye);
|
||||
@@ -263,8 +254,9 @@ int main() {
|
||||
printf("\n");
|
||||
|
||||
uint64_t m = 0;
|
||||
printf("give input for m: \n");
|
||||
printf("give input for m: ");
|
||||
scanf("%ju", &m);
|
||||
printf("\n");
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
@@ -280,8 +272,6 @@ int main() {
|
||||
pthread_join(thread_q, NULL);
|
||||
printf("\n");
|
||||
|
||||
p.prime = 3000000019;
|
||||
q.prime = 4000000007;
|
||||
printf("p: %u\n", p.prime);
|
||||
printf("q: %u\n", q.prime);
|
||||
|
||||
@@ -290,9 +280,9 @@ int main() {
|
||||
|
||||
if (!isSignature) {
|
||||
// rsa encryption
|
||||
rsa_encrypt(&m, &p, &q, &e, &d);
|
||||
uint64_t c = rsa_encrypt(&m, &p, &q, &e, &d);
|
||||
printf("\nkinai maradek tetel:\n");
|
||||
unsigned __int128 S = kinai_maradek_tetel(&m, d, &p, &q);
|
||||
unsigned __int128 S = kinai_maradek_tetel(&c, d, &p, &q);
|
||||
printf("S: ");
|
||||
print_uint128(S);
|
||||
printf("\n");
|
||||
@@ -300,15 +290,16 @@ int main() {
|
||||
// rsa signature
|
||||
|
||||
// generate keys
|
||||
uint64_t dummy = 2;
|
||||
rsa_encrypt(&dummy, &p, &q, &e, &d);
|
||||
rsa_encrypt(&m, &p, &q, &e, &d);
|
||||
printf("\n");
|
||||
|
||||
// c^d -> creates signature
|
||||
uint64_t signature = kinai_maradek_tetel(&m, d, &p, &q);
|
||||
printf("Alairas (Signature): ");
|
||||
printf("Signature: ");
|
||||
print_uint128(signature);
|
||||
|
||||
// key verifacation
|
||||
// S^e -> verifies the signature
|
||||
uint64_t e_length = 0;
|
||||
uint64_t *e_binary = dec_to_bin(e, &e_length);
|
||||
uint64_t n = (uint64_t)p.prime * q.prime;
|
||||
@@ -323,6 +314,8 @@ int main() {
|
||||
} else {
|
||||
printf("\nSignature not correct\n");
|
||||
}
|
||||
} else {
|
||||
printf("Why?\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user