int main() { big p,q,dp,dq,m,c,m1; int i,romptr; miracl instance; /* sizeof(miracl)= 2124 bytes from the stack */ #ifndef MR_STATIC miracl *mr_mip=mirsys(&instance,WORDS*8,16); char *mem=memalloc(_MIPP_ ,7); #else miracl *mr_mip=mirsys(&instance,MR_STATIC*8,16); /* size of bigs is fixed */ char mem[MR_BIG_RESERVE(7)]; /* reserve space on the stack for 7 bigs */ memset(mem,0,MR_BIG_RESERVE(7)); /* clear this memory */ #endif /* Initialise bigs */ p=mirvar_mem(_MIPP_ mem,0); q=mirvar_mem(_MIPP_ mem,1); dp=mirvar_mem(_MIPP_ mem,2); dq=mirvar_mem(_MIPP_ mem,3); m=mirvar_mem(_MIPP_ mem,4); c=mirvar_mem(_MIPP_ mem,5); m1=mirvar_mem(_MIPP_ mem,6); romptr=0; init_big_from_rom(p,WORDS,rom,256,&romptr); init_big_from_rom(q,WORDS,rom,256,&romptr); init_big_from_rom(dp,WORDS,rom,256,&romptr); init_big_from_rom(dq,WORDS,rom,256,&romptr); bigbits(_MIPP_ 512,c); /* count clocks, instructions and CPI from here.. */ //for (i=0;i<100000;i++) //{ powmod(_MIPP_ c,dp,p,m); powmod(_MIPP_ c,dq,q,m1); //} /* to here... */ #ifndef MR_NO_STANDARD_IO otnum(_MIPP_ m,stdout); otnum(_MIPP_ m1,stdout); #endif #ifndef MR_STATIC memkill(_MIPP_ mem,7); #else memset(mem,0,MR_BIG_RESERVE(6)); /* clear this stack memory */ #endif mirexit(_MIPPO_ ); /* clears workspace memory */ return 0; }
int main() { int ia,ib,promptr; epoint *PA,*PB; big A,B,a,b,q,pa,pb,key,x,y; ebrick2 binst; miracl instance; /* create miracl workspace on the stack */ /* Specify base 16 here so that HEX can be read in directly without a base-change */ miracl *mip=mirsys(&instance,WORDS*HEXDIGS,16); /* size of bigs is fixed */ char mem_big[MR_BIG_RESERVE(10)]; /* we need 10 bigs... */ char mem_ecp[MR_ECP_RESERVE(2)]; /* ..and two elliptic curve points */ memset(mem_big, 0, MR_BIG_RESERVE(10)); /* clear the memory */ memset(mem_ecp, 0, MR_ECP_RESERVE(2)); A=mirvar_mem(mip, mem_big, 0); /* Initialise big numbers */ B=mirvar_mem(mip, mem_big, 1); pa=mirvar_mem(mip, mem_big, 2); pb=mirvar_mem(mip, mem_big, 3); key=mirvar_mem(mip, mem_big, 4); x=mirvar_mem(mip, mem_big, 5); y=mirvar_mem(mip, mem_big, 6); q=mirvar_mem(mip,mem_big,7); a=mirvar_mem(mip, mem_big, 8); b=mirvar_mem(mip, mem_big, 9); PA=epoint_init_mem(mip, mem_ecp, 0); /* initialise Elliptic Curve points */ PB=epoint_init_mem(mip, mem_ecp, 1); irand(mip, 3L); /* change parameter for different random numbers */ promptr=0; init_big_from_rom(B,WORDS,rom,WORDS*4,&promptr); /* Read in curve parameter B from ROM */ /* don't need q or G(x,y) (we have precomputed table from it) */ init_big_from_rom(q,WORDS,rom,WORDS*4,&promptr); init_big_from_rom(x,WORDS,rom,WORDS*4,&promptr); init_big_from_rom(y,WORDS,rom,WORDS*4,&promptr); convert(mip,1,A); /* set A=1 */ /* Create precomputation instance from precomputed table in ROM */ ebrick2_init(&binst,prom,A,B,CURVE_M,CURVE_A,CURVE_B,CURVE_C,WINDOW,CURVE_M); /* offline calculations */ bigbits(mip,CURVE_M,a); /* A's random number */ ia=mul2_brick(mip,&binst,a,pa,pa); /* a*G =(pa,ya), ia is sign of ya */ bigbits(mip,CURVE_M,b); /* B's random number */ ib=mul2_brick(mip,&binst,b,pb,pb); /* b*G =(pb,yb), ib is sign of yb */ /* online calculations */ ecurve2_init(mip,CURVE_M,CURVE_A,CURVE_B,CURVE_C,A,B,FALSE,MR_PROJECTIVE); epoint2_set(mip,pb,pb,ib,PB); /* decompress PB */ ecurve2_mult(mip,a,PB,PB); epoint2_get(mip,PB,key,key); /* since internal base is HEX, can use otnum instead of cotnum - avoiding a base change */ printf("Alice's Key= "); otnum(mip,key,stdout); epoint2_set(mip,pa,pa,ia,PB); /* decompress PA */ ecurve2_mult(mip,b,PB,PB); epoint2_get(mip,PB,key,key); printf("Bob's Key= "); otnum(mip,key,stdout); /* clear the memory */ memset(mem_big, 0, MR_BIG_RESERVE(10)); memset(mem_ecp, 0, MR_ECP_RESERVE(2)); return 0; }
int main() { #ifdef MR_GENERIC_MT miracl instance; #endif big p,A,B,fr,q,delta,t,T; zzn2 res,Qx,Qy; epoint *P; int i,romptr; #ifndef MR_STATIC #ifdef MR_GENERIC_MT miracl *mr_mip=mirsys(&instance,WORDS*NPW,16); #else miracl *mr_mip=mirsys(WORDS*NPW,16); #endif char *mem=memalloc(_MIPP_ 14); char *mem1=ecp_memalloc(_MIPP_ 1); #else #ifdef MR_GENERIC_MT miracl *mr_mip=mirsys(&instance,MR_STATIC*NPW,16); #else miracl *mr_mip=mirsys(MR_STATIC*NPW,16); #endif char mem[MR_BIG_RESERVE(14)]; /* reserve space on the stack for 14 bigs */ char mem1[MR_ECP_RESERVE(1)]; /* reserve space on stack for 1 curve points */ memset(mem,0,MR_BIG_RESERVE(14)); /* clear this memory */ memset(mem1,0,MR_ECP_RESERVE(1)); #endif p=mirvar_mem(_MIPP_ mem,0); A=mirvar_mem(_MIPP_ mem,1); B=mirvar_mem(_MIPP_ mem,2); T=mirvar_mem(_MIPP_ mem,3); q=mirvar_mem(_MIPP_ mem,4); fr=mirvar_mem(_MIPP_ mem,5); delta=mirvar_mem(_MIPP_ mem,6); res.a=mirvar_mem(_MIPP_ mem,7); res.b=mirvar_mem(_MIPP_ mem,8); t=mirvar_mem(_MIPP_ mem,9); Qx.a=mirvar_mem(_MIPP_ mem,10); Qx.b=mirvar_mem(_MIPP_ mem,11); Qy.a=mirvar_mem(_MIPP_ mem,12); Qy.b=mirvar_mem(_MIPP_ mem,13); P=epoint_init_mem(_MIPP_ mem1,0); convert(_MIPP_ -3,A); romptr=0; init_big_from_rom(p,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(B,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(q,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(delta,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(fr,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(T,WORDS,romp,ROMSZ,&romptr); #ifndef MR_NO_STANDARD_IO printf("ROM size= %d\n",sizeof(romp)+sizeof(Prom)); printf("sizeof(miracl)= %d\n",sizeof(miracl)); #endif ecurve_init(_MIPP_ A,B,p,MR_PROJECTIVE); romptr=0; init_point_from_rom(P,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qx.a,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qx.b,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qy.a,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qy.b,WORDS,Prom,PROMSZ,&romptr); #ifdef MR_COUNT_OPS fpa=fpc=fpx=0; #endif ecap(_MIPP_ &Qx,&Qy,P,T,fr,delta,&res); /* #ifdef MR_COUNT_OPS printf("fpc= %d\n",fpc); printf("fpa= %d\n",fpa); printf("fpx= %d\n",fpx); fpa=fpc=fpx=0; #endif */ bigbits(_MIPP_ 160,t); zzn2_powl(_MIPP_ &res,t,&res); #ifndef MR_NO_STANDARD_IO zzn2_out(_MIPP_ "res= ",&res); #endif ecurve_mult(_MIPP_ t,P,P); ecap(_MIPP_ &Qx,&Qy,P,T,fr,delta,&res); #ifndef MR_NO_STANDARD_IO zzn2_out(_MIPP_ "res= ",&res); #endif #ifndef MR_STATIC memkill(_MIPP_ mem,14); ecp_memkill(_MIPP_ mem1,1); #else memset(mem,0,MR_BIG_RESERVE(14)); /* clear this stack memory */ memset(mem1,0,MR_ECP_RESERVE(1)); #endif return 0; }
int crypto_dh(unsigned char *s,const unsigned char* pk,const unsigned char *sk) { int i,promptr; ecn2 P; big A,B,p,a[2]; zzn2 x,y,psi[2]; miracl instance; /* create miracl workspace on the stack */ /* Specify base 16 here so that HEX can be read in directly without a base-change */ miracl *mip=mirsys(&instance,WORDS*16,16); /* size of bigs is fixed */ char mem_big[MR_BIG_RESERVE(17)]; /* we need 17 bigs... */ memset(mem_big, 0, MR_BIG_RESERVE(17)); /* clear the memory */ A=mirvar_mem(mip, mem_big, 0); /* Initialise big numbers */ B=mirvar_mem(mip, mem_big, 1); x.a=mirvar_mem(mip, mem_big, 2); x.b=mirvar_mem(mip, mem_big, 3); #ifndef COMPRESSED y.a=mirvar_mem(mip, mem_big, 4); y.b=mirvar_mem(mip, mem_big, 5); #endif a[0]=mirvar_mem(mip, mem_big, 6); a[1]=mirvar_mem(mip, mem_big, 7); p=mirvar_mem(mip, mem_big, 8); P.x.a=mirvar_mem(mip, mem_big, 9); P.x.b=mirvar_mem(mip, mem_big, 10); P.y.a=mirvar_mem(mip, mem_big, 11); P.y.b=mirvar_mem(mip, mem_big, 12); P.z.a=mirvar_mem(mip, mem_big, 13); P.z.b=mirvar_mem(mip, mem_big, 14); P.marker=MR_EPOINT_INFINITY; psi[0].a=mirvar_mem(mip, mem_big, 15); psi[0].b=mirvar_mem(mip, mem_big, 16); promptr=0; init_big_from_rom(p,WORDS,rom,16,&promptr); /* Read in prime modulus p from ROM */ init_big_from_rom(B,WORDS,rom,16,&promptr); /* Read in curve parameter B from ROM */ init_big_from_rom(psi[0].a,WORDS,rom,16,&promptr); init_big_from_rom(psi[0].b,WORDS,rom,16,&promptr); init_big_from_rom(x.a,WORDS,rom,16,&promptr); init_big_from_rom(x.b,WORDS,rom,16,&promptr); #ifndef COMPRESSED init_big_from_rom(y.a,WORDS,rom,16,&promptr); init_big_from_rom(y.b,WORDS,rom,16,&promptr); #endif convert(mip,1,A); /* Fix A=1 */ /* offline calculations */ ecurve_init(mip,A,B,p,MR_PROJECTIVE); mip->TWIST=TRUE; /* Alice calculates secret key */ bytes_to_big(mip,16,pk,x.a); bytes_to_big(mip,16,&pk[16],x.b); bytes_to_big(mip,16,sk,a[0]); bytes_to_big(mip,16,&sk[16],a[1]); #ifndef COMPRESSED bytes_to_big(mip,16,&pk[32],y.a); bytes_to_big(mip,16,&pk[48],y.b); if (!ecn2_set(mip,&x,&y,&P)) { memset(mem_big, 0, MR_BIG_RESERVE(17)); mirexit(mip); return -1; } #else if (!ecn2_setx(mip,&x,&P)) { memset(mem_big, 0, MR_BIG_RESERVE(17)); mirexit(mip); return -1; } #endif ecn2_mul2_gls(mip,a,&P,psi,&P); ecn2_getx(&P,&x); #ifdef COMPRESSED zzn2_sqr(mip,&x,&x); /* I tossed y, so I might have wrong sign.. */ #endif big_to_bytes(mip,16,x.a,s,TRUE); big_to_bytes(mip,16,x.b,&s[16],TRUE); memset(mem_big, 0, MR_BIG_RESERVE(17)); mirexit(mip); return 0; }
int crypto_dh_keypair(unsigned char* pk,unsigned char *sk) { int i,promptr; big A,B,p,a[2]; zzn2 x,y,psi[2]; miracl instance; /* create miracl workspace on the stack */ ebrick binst; /* Specify base 16 here so that HEX can be read in directly without a base-change */ miracl *mip=mirsys(&instance,WORDS*16,16); /* size of bigs is fixed */ char mem_big[MR_BIG_RESERVE(11)]; /* we need 10 bigs... */ memset(mem_big, 0, MR_BIG_RESERVE(11)); /* clear the memory */ A=mirvar_mem(mip, mem_big, 0); /* Initialise big numbers */ B=mirvar_mem(mip, mem_big, 1); x.a=mirvar_mem(mip, mem_big, 2); x.b=mirvar_mem(mip, mem_big, 3); y.a=mirvar_mem(mip, mem_big, 4); y.b=mirvar_mem(mip, mem_big, 5); a[0]=mirvar_mem(mip, mem_big, 6); a[1]=mirvar_mem(mip, mem_big, 7); p=mirvar_mem(mip, mem_big, 8); psi[0].a=mirvar_mem(mip, mem_big, 9); psi[0].b=mirvar_mem(mip, mem_big, 10); promptr=0; init_big_from_rom(p,WORDS,rom,16,&promptr); /* Read in prime modulus p from ROM */ init_big_from_rom(B,WORDS,rom,16,&promptr); /* Read in curve parameter B from ROM */ init_big_from_rom(psi[0].a,WORDS,rom,16,&promptr); init_big_from_rom(psi[0].b,WORDS,rom,16,&promptr); convert(mip,1,A); /* Fix A=1 */ ecn2_brick_init(&binst,prom,A,B,p,6,128); /* Alices key gen calculation */ #ifdef HAVE_MAIN for (i=0;i<32;i++) sk[i]=rand(); #else randombytes(sk,32); #endif sk[15]&=0x3f; sk[31]&=0x3f; bytes_to_big(mip,16,sk,a[0]); bytes_to_big(mip,16,&sk[16],a[1]); ecn2_mul_brick_gls(mip,&binst,a,psi,&x,&y); big_to_bytes(mip,16,x.a,pk,TRUE); big_to_bytes(mip,16,x.b,&pk[16],TRUE); #ifndef COMPRESSED big_to_bytes(mip,16,y.a,&pk[32],TRUE); big_to_bytes(mip,16,y.b,&pk[48],TRUE); #endif memset(mem_big, 0, MR_BIG_RESERVE(11)); mirexit(mip); return 0; }
int main() { int promptr; epoint *PB; big A,B,p,a,b,pa,pb,key; ebrick binst; miracl instance; /* create miracl workspace on the stack */ /* Specify base 16 here so that HEX can be read in directly without a base-change */ miracl *mip=mirsys(&instance,WORDS*HEXDIGS,16); /* size of bigs is fixed */ char mem_big[MR_BIG_RESERVE(8)]; /* we need 8 bigs... */ char mem_ecp[MR_ECP_RESERVE(1)]; /* ..and 1 elliptic curve points */ memset(mem_big, 0, MR_BIG_RESERVE(8)); /* clear the memory */ memset(mem_ecp, 0, MR_ECP_RESERVE(1)); A=mirvar_mem(mip, mem_big, 0); /* Initialise big numbers */ B=mirvar_mem(mip, mem_big, 1); pa=mirvar_mem(mip, mem_big, 2); pb=mirvar_mem(mip, mem_big, 3); key=mirvar_mem(mip, mem_big, 4); a=mirvar_mem(mip, mem_big, 5); b=mirvar_mem(mip, mem_big, 6); p=mirvar_mem(mip, mem_big, 7); PB=epoint_init_mem(mip, mem_ecp, 0); /* initialise Elliptic Curve points */ irand(mip, 3L); /* change parameter for different random numbers */ promptr=0; init_big_from_rom(p,WORDS,rom,WORDS*5,&promptr); /* Read in prime modulus p from ROM */ init_big_from_rom(B,WORDS,rom,WORDS*5,&promptr); /* Read in curve parameter B from ROM */ /* don't need q or G(x,y) (we have precomputed table from it) */ convert(mip,-3,A); /* set A=-3 */ /* Create precomputation instance from precomputed table in ROM */ ebrick_init(&binst,prom,A,B,p,WINDOW,CURVE_BITS); /* offline calculations */ bigbits(mip,CURVE_BITS,a); /* A's random number */ mul_brick(mip,&binst,a,pa,pa); /* a*G =(pa,ya) */ bigbits(mip,CURVE_BITS,b); /* B's random number */ mul_brick(mip,&binst,b,pb,pb); /* b*G =(pb,yb) */ /* swap X values of point */ /* online calculations */ ecurve_init(mip,A,B,p,MR_PROJECTIVE); epoint_set(mip,pb,pb,0,PB); /* decompress PB */ ecurve_mult(mip,a,PB,PB); epoint_get(mip,PB,key,key); /* since internal base is HEX, can use otnum instead of cotnum - avoiding a base change */ #ifndef MR_NO_STANDARD_IO printf("Alice's Key= "); otnum(mip,key,stdout); #endif epoint_set(mip,pa,pa,0,PB); /* decompress PA */ ecurve_mult(mip,b,PB,PB); epoint_get(mip,PB,key,key); #ifndef MR_NO_STANDARD_IO printf("Bob's Key= "); otnum(mip,key,stdout); #endif /* clear the memory */ memset(mem_big, 0, MR_BIG_RESERVE(8)); memset(mem_ecp, 0, MR_ECP_RESERVE(1)); return 0; }