Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving main.c with CRT 1st version #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 52 additions & 82 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@

//https://e2e.ti.com/support/tools/ccs/f/81/t/470171?Error-Memory-map-prevented-reading-0x202020B0-How-to-definitely-solve
//debug stops at 74 variables do not show

#define CRT 1
#define SIZE 1 // 0:=256Bit 1:=512Bit 2:=1024Bit
#define SIZE 0 // 0:=256Bit 1:=512Bit 2:=1024Bit

//#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
//#include "inc/hw_memmap.h"
#include "flint.h"
//#include "flint.c"
#include <limits.h>
#include <string.h>
#include <time.h>
#include "tm4c123gh6pm.h"
//#include <time.h>

void squ_and_mul(clint* basis, clint* modulus, clint* erg, char* s);
clint* rsa(clint* base, clint* exp, clint* mod);
clint* rsa_crt(clint* base, clint* d, clint* p, clint* q);
void check_cipher(clint* c, char* c_ext);

void squ_and_mul(CLINT basis, CLINT modulus, CLINT erg, char* s);
//void squ_and_mul(CLINT base, CLINT mod, CLINT res, char* e_bin) ;
void rsa(CLINT base, CLINT exp, CLINT mod, CLINT res);
//void rsa_crt(CLINT base, CLINT d, CLINT p, CLINT q);
void rsa_crt(CLINT base, CLINT d, CLINT p, CLINT q, CLINT res);
void rsa_crt2();
void check_cipher(CLINT c, char* c_ext);

unsigned short B = (unsigned short)65536;
unsigned short B2 = (unsigned short)2;
unsigned short B10 = (unsigned short)10;

/*
// 1024 Bit
char ps_L[] = "11799940606765122157611277130045066959256062520371739560806392321926463280401194642712890850827847780551090208498805012553581203235294194860873223609015477";
char qs_L[] = "8957103306903606949408104239618127559192817675894527265487522259573418305677063719260073965144676083541693842217671539231745355197549057115339663240687569";
Expand All @@ -38,64 +38,69 @@ char ds_M[] = "65882036256492448979831223392265301430734775881134064445286581248
char es_M[] = "65537";
char ms_M[] = "9845036921172790349724138442481371302270379";
char cs_ext_M[] = "7301745151270516318623469090933370821244449555691537705291283029718157624242712959138659186919669630850613782800896611457929953588562350070003210682113769";

*/
// 256 Bit
char ps_S[] = "250325044798691409684083071440443880431";
char qs_S[] = "229770138261794308748025352098251066273";
char ds_S[] = "24585041856782950592719418770956785764319358874029574779981960494202322200513";
char es_S[] = "65537";
char ms_S[] = "9845036921172790349724138442481371302270379";
char cs_ext_S[] = "38222019833090413175408435136713720884590177436526349868410614951574203042546";

/*
char* ps[3] = { ps_S,ps_M,ps_L };
char* qs[3] = { qs_S,qs_M,qs_L };
char* ds[3] = { ds_S,ds_M,ds_L };
char* es[3] = { es_S,es_M,es_L };
char* ms[3] = { ms_S,ms_M,ms_L };
char* cs_ext[3] = { cs_ext_S,cs_ext_M,cs_ext_L };
*/
CLINT c, p, q, n, phi, e, d, m, m_post; // MAIN
CLINT p_dec, q_dec, dp, dq, bp, bq, u, v, n, gcd, t1, t2; // rsa_crt()
CLINT c_ext_l; // check_cipher()

int main()
{
clock_t start;
int diff_ms;
//clock_t start;
//int diff_ms;

char* ms_post;

clint* p = create_l();
clint* q = create_l();
clint* n = create_l();
clint* phi = create_l();
clint* e = create_l();
clint* d = create_l();
clint* m = create_l();
clint* c = create_l();
clint* m_post = create_l();

/*
str2clint_l(p, ps[SIZE], B10);
str2clint_l(q, qs[SIZE], B10);
str2clint_l(d, ds[SIZE], B10);
str2clint_l(e, es[SIZE], B10);
str2clint_l(m, ms[SIZE], B10);
*/

str2clint_l(p, ps_S, B10);
str2clint_l(q, qs_S, B10);
str2clint_l(d, ds_S, B10);
str2clint_l(e, es_S, B10);
str2clint_l(m, ms_S, B10);

str2clint_l(c, "0", 16);
mul_l(p, q, n);

c = rsa(m, e, n);
rsa(m, e, n, c);

check_cipher(c, cs_ext[SIZE]);
// check_cipher(c, cs_ext_S);
//check_cipher(c, cs_ext[SIZE]);

if (CRT) {
start = clock();
m_post = rsa_crt(c, d, p, q);
diff_ms = (int)(clock() - start) * 1000 / CLOCKS_PER_SEC;
//start = clock();
rsa_crt2();
//rsa_crt(c, d, p, q, m_post);
//diff_ms = (int)(clock() - start) * 1000 / CLOCKS_PER_SEC;
}
else {
start = clock();
m_post = rsa(c, d, n);
diff_ms = (int)(clock() - start) * 1000 / CLOCKS_PER_SEC;
//start = clock();
rsa(c, d, n, m_post);
//diff_ms = (int)(clock() - start) * 1000 / CLOCKS_PER_SEC;
}

ms_post = xclint2str_l(m_post, B10, 0);

/*
if (equ_l(m,m_post)) {
printf("\nInitial plain: %s \nPost plain: %s\nEquality: %s\n\n", ms[SIZE], ms_post, "TRUE");
}
Expand All @@ -104,11 +109,11 @@ int main()
}

printf("Decrypt resp. verify time: %dms\n\n", diff_ms);

*/
return 0;
}

void squ_and_mul(clint* base, clint* mod, clint* res, char* e_bin) {
void squ_and_mul(CLINT base, CLINT mod, CLINT res, char* e_bin) {
SETONE_L(res);
while (*e_bin) { // Start with MSB
msqr_l(res, res, mod); // Mod to keep numbers small
Expand All @@ -120,37 +125,20 @@ void squ_and_mul(clint* base, clint* mod, clint* res, char* e_bin) {
}
}

clint* rsa(clint* base, clint* exp, clint* mod) {
void rsa(CLINT base, CLINT exp, CLINT mod, CLINT res) {
// encrypt crypt(plain, e, n)
// decrypt crypt(cipher, d, n)
// sign crypt(message, d, n)
// verify crypt(signature, e, n)

clint* res = create_l();
char* e_bin = xclint2str_l(exp, 2, 0); // e_dez -> e_bin
squ_and_mul(base, mod, res, e_bin);
return res;
}

clint* rsa_crt(clint* base, clint* d, clint* p, clint* q) {
void rsa_crt(CLINT base, CLINT d, CLINT p, CLINT q, CLINT res) {
// decrypt rsa_crt(cipher, d, p, q)
// sign rsa_crt(message, d, p, q)

clint* res = create_l();
clint* p_dec = create_l();
clint* q_dec = create_l();
clint* dp = create_l();
clint* dq = create_l();
clint* bp = create_l();
clint* bq = create_l();
clint* u = create_l();
clint* v = create_l();
clint* n = create_l();
clint* gcd = create_l();

clint* t1 = create_l();
clint* t2 = create_l();

int sign_u, sign_v;
int* ptr_sign_u = &sign_u;
int* ptr_sign_v = &sign_v;
Expand All @@ -169,8 +157,8 @@ clint* rsa_crt(clint* base, clint* d, clint* p, clint* q) {
mod_l(d, p_dec, dp);
mod_l(d, q_dec, dq);

bp = rsa(base, dp, p);
bq = rsa(base, dq, q);
rsa(base, dp, p, bp);
rsa(base, dq, q, bq);

mmul_l(p, bq, bq, n);
mmul_l(u, bq, bq, n);
Expand All @@ -179,30 +167,12 @@ clint* rsa_crt(clint* base, clint* d, clint* p, clint* q) {

madd_l(bq, bp, res, n);

return res;
}

void check_cipher(clint* c, char* cs_ext) {
clint* c_ext_l = create_l();
void check_cipher(CLINT c, char* cs_ext) {

str2clint_l(c_ext_l, cs_ext, B10);
if (equ_l(c,c_ext_l)) printf("Check cipher: TRUE\n\n");
else printf("Check cipher: FALSE\n\n");
//if (equ_l(c,c_ext_l)) printf("Check cipher: TRUE\n\n");
//else printf("Check cipher: FALSE\n\n");
}

// HOMEWORK:
/*char as[] = "10000000000000000000";
char bs[] = "22222222222222222222";
char* cs;

clint* ap = create_l(); // Creates clint and returns pointer
clint* bp = create_l();
clint* cp = create_l();

str2clint_l(ap, as, B10);
str2clint_l(bp, bs, B10);

mul_l(ap, bp, cp); // Apply plus minus as well

cs = xclint2str_l(cp, B10, 0);

printf(cs);*/