add sm2_standard

This commit is contained in:
[GGSuchao]
2017-07-06 11:41:39 +08:00
committed by GGSuchao
parent ad1fb8a46e
commit 6a598cbdb8
15 changed files with 10990 additions and 3 deletions

View File

@@ -1,3 +1,7 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=sm2_err.c sm2_asn1.c sm2_id.c sm2_sign.c sm2_enc.c \
sm2_oct.c sm2_exch.c sm2_kmeth.c
LIBS=../../libcrypto
SOURCE[../../libcrypto]=sm2_err.c sm2_asn1.c sm2_id.c sm2_sign.c sm2_enc.c \
sm2_oct.c sm2_exch.c sm2_kmeth.c sm2_standard_enc.c sm2_standard_exch.c \
sm2_standard_sign.c ./miracl/mralloc.c ./miracl/mrarth0.c \
./miracl/mrarth1.c ./miracl/mrarth2.c ./miracl/mrarth3.c ./miracl/mrbits.c \
./miracl/mrcore.c ./miracl/mrcurve.c ./miracl/mrjack.c ./miracl/mrlucas.c\
./miracl/mrmonty.c ./miracl/mrmuldv.c ./miracl/mrsroot.c ./miracl/mrxgcd.c

View File

@@ -0,0 +1,85 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL memory allocation routines
* mralloc.c
*
* MIRACL C Memory allocation/deallocation
* Can be replaced with special user-defined routines
* Default is to standard system routines
*
* NOTE: uses calloc() which initialises memory to Zero, so make sure
* any substituted routine does the same!
*/
#include <openssl/miracl.h>
#include <stdlib.h>
#ifndef MR_STATIC
miracl *mr_first_alloc()
{
return (miracl *)calloc(1,sizeof(miracl));
}
void *mr_alloc(_MIPD_ int num,int size)
{
char *p;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip==NULL)
{
p=(char *)calloc(num,size);
return (void *)p;
}
if (mr_mip->ERNUM) return NULL;
p=(char *)calloc(num,size);
if (p==NULL) mr_berror(_MIPP_ MR_ERR_OUT_OF_MEMORY);
return (void *)p;
}
void mr_free(void *addr)
{
if (addr==NULL) return;
free(addr);
return;
}
#endif

320
crypto/sm2/miracl/mrarth0.c Normal file
View File

@@ -0,0 +1,320 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL arithmetic routines 0 - Add and subtract routines
* mrarth0.c
*
*/
#include <openssl/miracl.h>
void mr_padd(_MIPD_ big x,big y,big z)
{ /* add two big numbers, z=x+y where *
* x and y are positive */
int i,lx,ly,lz,la;
mr_small carry,psum;
mr_small *gx,*gy,*gz;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
lx = (int)x->len;
ly = (int)y->len;
if (ly>lx)
{
lz=ly;
la=lx;
if (x!=z) copy(y,z);
else la=ly;
}
else
{
lz=lx;
la=ly;
if (y!=z) copy(x,z);
else la=lx;
}
carry=0;
z->len=lz;
gx=x->w; gy=y->w; gz=z->w;
if (lz<mr_mip->nib || !mr_mip->check) z->len++;
#ifndef MR_SIMPLE_BASE
if (mr_mip->base==0)
{
#endif
for (i=0;i<la;i++)
{ /* add by columns to length of the smaller number */
psum=gx[i]+gy[i]+carry;
if (psum>gx[i]) carry=0;
else if (psum<gx[i]) carry=1;
gz[i]=psum;
}
for (;i<lz && carry>0;i++ )
{ /* add by columns to the length of larger number (if there is a carry) */
psum=gx[i]+gy[i]+carry;
if (psum>gx[i]) carry=0;
else if (psum<gx[i]) carry=1;
gz[i]=psum;
}
if (carry)
{ /* carry left over - possible overflow */
if (mr_mip->check && i>=mr_mip->nib)
{
mr_berror(_MIPP_ MR_ERR_OVERFLOW);
return;
}
gz[i]=carry;
}
#ifndef MR_SIMPLE_BASE
}
else
{
for (i=0;i<la;i++)
{ /* add by columns */
psum=gx[i]+gy[i]+carry;
carry=0;
if (psum>=mr_mip->base)
{ /* set carry */
carry=1;
psum-=mr_mip->base;
}
gz[i]=psum;
}
for (;i<lz && carry>0;i++)
{
psum=gx[i]+gy[i]+carry;
carry=0;
if (psum>=mr_mip->base)
{ /* set carry */
carry=1;
psum-=mr_mip->base;
}
gz[i]=psum;
}
if (carry)
{ /* carry left over - possible overflow */
if (mr_mip->check && i>=mr_mip->nib)
{
mr_berror(_MIPP_ MR_ERR_OVERFLOW);
return;
}
gz[i]=carry;
}
}
#endif
if (gz[z->len-1]==0) z->len--;
}
void mr_psub(_MIPD_ big x,big y,big z)
{ /* subtract two big numbers z=x-y *
* where x and y are positive and x>y */
int i,lx,ly;
mr_small borrow,pdiff;
mr_small *gx,*gy,*gz;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
lx = (int)x->len;
ly = (int)y->len;
if (ly>lx)
{
mr_berror(_MIPP_ MR_ERR_NEG_RESULT);
return;
}
if (y!=z) copy(x,z);
else ly=lx;
z->len=lx;
gx=x->w; gy=y->w; gz=z->w;
borrow=0;
#ifndef MR_SIMPLE_BASE
if (mr_mip->base==0)
{
#endif
for (i=0;i<ly || borrow>0;i++)
{ /* subtract by columns */
if (i>lx)
{
mr_berror(_MIPP_ MR_ERR_NEG_RESULT);
return;
}
pdiff=gx[i]-gy[i]-borrow;
if (pdiff<gx[i]) borrow=0;
else if (pdiff>gx[i]) borrow=1;
gz[i]=pdiff;
}
#ifndef MR_SIMPLE_BASE
}
else for (i=0;i<ly || borrow>0;i++)
{ /* subtract by columns */
if (i>lx)
{
mr_berror(_MIPP_ MR_ERR_NEG_RESULT);
return;
}
pdiff=gy[i]+borrow;
borrow=0;
if (gx[i]>=pdiff) pdiff=gx[i]-pdiff;
else
{ /* set borrow */
pdiff=mr_mip->base+gx[i]-pdiff;
borrow=1;
}
gz[i]=pdiff;
}
#endif
mr_lzero(z);
}
static void mr_select(_MIPD_ big x,int d,big y,big z)
{ /* perform required add or subtract operation */
int sx,sy,sz,jf,xgty;
#ifdef MR_FLASH
if (mr_notint(x) || mr_notint(y))
{
mr_berror(_MIPP_ MR_ERR_INT_OP);
return;
}
#endif
sx=exsign(x);
sy=exsign(y);
sz=0;
x->len&=MR_OBITS; /* force operands to be positive */
y->len&=MR_OBITS;
xgty=mr_compare(x,y);
jf=(1+sx)+(1+d*sy)/2;
switch (jf)
{ /* branch according to signs of operands */
case 0:
if (xgty>=0)
mr_padd(_MIPP_ x,y,z);
else
mr_padd(_MIPP_ y,x,z);
sz=MINUS;
break;
case 1:
if (xgty<=0)
{
mr_psub(_MIPP_ y,x,z);
sz=PLUS;
}
else
{
mr_psub(_MIPP_ x,y,z);
sz=MINUS;
}
break;
case 2:
if (xgty>=0)
{
mr_psub(_MIPP_ x,y,z);
sz=PLUS;
}
else
{
mr_psub(_MIPP_ y,x,z);
sz=MINUS;
}
break;
case 3:
if (xgty>=0)
mr_padd(_MIPP_ x,y,z);
else
mr_padd(_MIPP_ y,x,z);
sz=PLUS;
break;
}
if (sz<0) z->len^=MR_MSBIT; /* set sign of result */
if (x!=z && sx<0) x->len^=MR_MSBIT; /* restore signs to operands */
if (y!=z && y!=x && sy<0) y->len^=MR_MSBIT;
}
void add(_MIPD_ big x,big y,big z)
{ /* add two signed big numbers together z=x+y */
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
MR_IN(27)
mr_select(_MIPP_ x,PLUS,y,z);
MR_OUT
}
void subtract(_MIPD_ big x,big y,big z)
{ /* subtract two big signed numbers z=x-y */
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
MR_IN(28)
mr_select(_MIPP_ x,MINUS,y,z);
MR_OUT
}
void incr(_MIPD_ big x,int n,big z)
{ /* add int to big number: z=x+n */
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
MR_IN(7)
convert(_MIPP_ n,mr_mip->w0);
mr_select(_MIPP_ x,PLUS,mr_mip->w0,z);
MR_OUT
}
void decr(_MIPD_ big x,int n,big z)
{ /* subtract int from big number: z=x-n */
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
MR_IN(8)
convert(_MIPP_ n,mr_mip->w0);
mr_select(_MIPP_ x,MINUS,mr_mip->w0,z);
MR_OUT
}

1068
crypto/sm2/miracl/mrarth1.c Normal file

File diff suppressed because it is too large Load Diff

1584
crypto/sm2/miracl/mrarth2.c Normal file

File diff suppressed because it is too large Load Diff

231
crypto/sm2/miracl/mrarth3.c Normal file
View File

@@ -0,0 +1,231 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL arithmetic routines 3 - simple powers and roots
* mrarth3.c
*/
#include <stdlib.h>
#include <openssl/miracl.h>
void expint(_MIPD_ int b,int n,big x)
{ /* sets x=b^n */
unsigned int bit,un;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
convert(_MIPP_ 1,x);
if (n==0) return;
MR_IN(50)
if (n<0)
{
mr_berror(_MIPP_ MR_ERR_NEG_POWER);
MR_OUT
return;
}
if (b==2) expb2(_MIPP_ n,x);
else
{
bit=1;
un=(unsigned int)n;
while (un>=bit) bit<<=1;
bit>>=1;
while (bit>0)
{ /* ltr method */
multiply(_MIPP_ x,x,x);
if ((bit&un)!=0) premult(_MIPP_ x,b,x);
bit>>=1;
}
}
MR_OUT
}
void power(_MIPD_ big x,long n,big z,big w)
{ /* raise big number to int power w=x^n *
* (mod z if z and w distinct) */
mr_small norm;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
copy(x,mr_mip->w5);
zero(w);
if(mr_mip->ERNUM || size(mr_mip->w5)==0) return;
convert(_MIPP_ 1,w);
if (n==0L) return;
MR_IN(17)
if (n<0L)
{
mr_berror(_MIPP_ MR_ERR_NEG_POWER);
MR_OUT
return;
}
if (w==z) forever
{ /* "Russian peasant" exponentiation */
if (n%2!=0L)
multiply(_MIPP_ w,mr_mip->w5,w);
n/=2L;
if (mr_mip->ERNUM || n==0L) break;
multiply(_MIPP_ mr_mip->w5,mr_mip->w5,mr_mip->w5);
}
else
{
norm=normalise(_MIPP_ z,z);
divide(_MIPP_ mr_mip->w5,z,z);
forever
{
if (mr_mip->user!=NULL) (*mr_mip->user)();
if (n%2!=0L) mad(_MIPP_ w,mr_mip->w5,mr_mip->w5,z,z,w);
n/=2L;
if (mr_mip->ERNUM || n==0L) break;
mad(_MIPP_ mr_mip->w5,mr_mip->w5,mr_mip->w5,z,z,mr_mip->w5);
}
if (norm!=1)
{
#ifdef MR_FP_ROUNDING
mr_sdiv(_MIPP_ z,norm,mr_invert(norm),z);
#else
mr_sdiv(_MIPP_ z,norm,z);
#endif
divide(_MIPP_ w,z,z);
}
}
MR_OUT
}
BOOL nroot(_MIPD_ big x,int n,big w)
{ /* extract lower approximation to nth root *
* w=x^(1/n) returns TRUE for exact root *
* uses Newtons method */
int sx,dif,s,p,d,lg2,lgx,rem;
BOOL full;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return FALSE;
if (size(x)==0 || n==1)
{
copy(x,w);
return TRUE;
}
MR_IN(16)
if (n<1) mr_berror(_MIPP_ MR_ERR_BAD_ROOT);
sx=exsign(x);
if (n%2==0 && sx==MINUS) mr_berror(_MIPP_ MR_ERR_NEG_ROOT);
if (mr_mip->ERNUM)
{
MR_OUT
return FALSE;
}
insign(PLUS,x);
lgx=logb2(_MIPP_ x);
if (n>=lgx)
{ /* root must be 1 */
insign(sx,x);
convert(_MIPP_ sx,w);
MR_OUT
if (lgx==1) return TRUE;
else return FALSE;
}
expb2(_MIPP_ 1+(lgx-1)/n,mr_mip->w2); /* guess root as 2^(log2(x)/n) */
s=(-(((int)x->len-1)/n)*n);
mr_shift(_MIPP_ mr_mip->w2,s/n,mr_mip->w2);
lg2=logb2(_MIPP_ mr_mip->w2)-1;
full=FALSE;
if (s==0) full=TRUE;
d=0;
p=1;
while (!mr_mip->ERNUM)
{ /* Newtons method */
copy(mr_mip->w2,mr_mip->w3);
mr_shift(_MIPP_ x,s,mr_mip->w4);
mr_mip->check=OFF;
power(_MIPP_ mr_mip->w2,n-1,mr_mip->w6,mr_mip->w6);
mr_mip->check=ON;
divide(_MIPP_ mr_mip->w4,mr_mip->w6,mr_mip->w2);
rem=size(mr_mip->w4);
subtract(_MIPP_ mr_mip->w2,mr_mip->w3,mr_mip->w2);
dif=size(mr_mip->w2);
subdiv(_MIPP_ mr_mip->w2,n,mr_mip->w2);
add(_MIPP_ mr_mip->w2,mr_mip->w3,mr_mip->w2);
p*=2;
if(p<lg2+d*mr_mip->lg2b) continue;
if (full && mr_abs(dif)<n)
{ /* test for finished */
while (dif<0)
{
rem=0;
decr(_MIPP_ mr_mip->w2,1,mr_mip->w2);
mr_mip->check=OFF;
power(_MIPP_ mr_mip->w2,n,mr_mip->w6,mr_mip->w6);
mr_mip->check=ON;
dif=mr_compare(x,mr_mip->w6);
}
copy(mr_mip->w2,w);
insign(sx,w);
insign(sx,x);
MR_OUT
if (rem==0 && dif==0) return TRUE;
else return FALSE;
}
else
{ /* adjust precision */
d*=2;
if (d==0) d=1;
s+=d*n;
if (s>=0)
{
d-=s/n;
s=0;
full=TRUE;
}
mr_shift(_MIPP_ mr_mip->w2,d,mr_mip->w2);
}
p/=2;
}
MR_OUT
return FALSE;
}

245
crypto/sm2/miracl/mrbits.c Normal file
View File

@@ -0,0 +1,245 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL bit manipulation routines
* mrbits.c
*/
#include <stdlib.h>
#include <openssl/miracl.h>
#ifdef MR_FP
#include <math.h>
#endif
int logb2(_MIPD_ big x)
{ /* returns number of bits in x */
int xl,lg2;
mr_small top;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM || size(x)==0) return 0;
MR_IN(49)
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
xl=(int)(x->len&MR_OBITS);
lg2=mr_mip->lg2b*(xl-1);
top=x->w[xl-1];
while (top>=1)
{
lg2++;
top/=2;
}
#ifndef MR_ALWAYS_BINARY
}
else
{
copy(x,mr_mip->w0);
insign(PLUS,mr_mip->w0);
lg2=0;
while (mr_mip->w0->len>1)
{
#ifdef MR_FP_ROUNDING
mr_sdiv(_MIPP_ mr_mip->w0,mr_mip->base2,mr_invert(mr_mip->base2),mr_mip->w0);
#else
mr_sdiv(_MIPP_ mr_mip->w0,mr_mip->base2,mr_mip->w0);
#endif
lg2+=mr_mip->lg2b;
}
while (mr_mip->w0->w[0]>=1)
{
lg2++;
mr_mip->w0->w[0]/=2;
}
}
#endif
MR_OUT
return lg2;
}
void sftbit(_MIPD_ big x,int n,big z)
{ /* shift x by n bits */
int m;
mr_small sm;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
copy(x,z);
if (n==0) return;
MR_IN(47)
m=mr_abs(n);
sm=mr_shiftbits((mr_small)1,m%mr_mip->lg2b);
if (n>0)
{ /* shift left */
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
mr_shift(_MIPP_ z,n/mr_mip->lg2b,z);
mr_pmul(_MIPP_ z,sm,z);
#ifndef MR_ALWAYS_BINARY
}
else
{
expb2(_MIPP_ m,mr_mip->w1);
multiply(_MIPP_ z,mr_mip->w1,z);
}
#endif
}
else
{ /* shift right */
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
mr_shift(_MIPP_ z,n/mr_mip->lg2b,z);
#ifdef MR_FP_ROUNDING
mr_sdiv(_MIPP_ z,sm,mr_invert(sm),z);
#else
mr_sdiv(_MIPP_ z,sm,z);
#endif
#ifndef MR_ALWAYS_BINARY
}
else
{
expb2(_MIPP_ m,mr_mip->w1);
divide(_MIPP_ z,mr_mip->w1,z);
}
#endif
}
MR_OUT
}
void expb2(_MIPD_ int n,big x)
{ /* sets x=2^n */
int r,p;
#ifndef MR_ALWAYS_BINARY
int i;
#endif
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
convert(_MIPP_ 1,x);
if (n==0) return;
MR_IN(149)
if (n<0)
{
mr_berror(_MIPP_ MR_ERR_NEG_POWER);
MR_OUT
return;
}
r=n/mr_mip->lg2b;
p=n%mr_mip->lg2b;
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
mr_shift(_MIPP_ x,r,x);
x->w[x->len-1]=mr_shiftbits(x->w[x->len-1],p);
#ifndef MR_ALWAYS_BINARY
}
else
{
for (i=1;i<=r;i++)
mr_pmul(_MIPP_ x,mr_mip->base2,x);
mr_pmul(_MIPP_ x,mr_shiftbits((mr_small)1,p),x);
}
#endif
MR_OUT
}
#ifndef MR_NO_RAND
void bigbits(_MIPD_ int n,big x)
{ /* sets x as random < 2^n */
mr_small r;
mr_lentype wlen;
#ifdef MR_FP
mr_small dres;
#endif
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
zero(x);
if (mr_mip->ERNUM || n<=0) return;
MR_IN(150)
expb2(_MIPP_ n,mr_mip->w1);
wlen=mr_mip->w1->len;
do
{
r=brand(_MIPPO_ );
if (mr_mip->base==0) x->w[x->len++]=r;
else x->w[x->len++]=MR_REMAIN(r,mr_mip->base);
} while (x->len<wlen);
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
x->w[wlen-1]=MR_REMAIN(x->w[wlen-1],mr_mip->w1->w[wlen-1]);
mr_lzero(x);
#ifndef MR_ALWAYS_BINARY
}
else
{
divide(_MIPP_ x,mr_mip->w1,mr_mip->w1);
}
#endif
MR_OUT
}
#endif

2290
crypto/sm2/miracl/mrcore.c Normal file

File diff suppressed because it is too large Load Diff

2507
crypto/sm2/miracl/mrcurve.c Normal file

File diff suppressed because it is too large Load Diff

342
crypto/sm2/miracl/mrjack.c Normal file
View File

@@ -0,0 +1,342 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL Jacobi symbol routine
* mrjack.c
*
* See "A binary algorithm for the Jacobi symbol"
* Shallit and Sorenson
*/
#include <stdlib.h>
#include <openssl/miracl.h>
int jack(_MIPD_ big a,big n)
{ /* find jacobi symbol (a/n), for positive odd n */
big w;
int nm8,onm8,t;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM || size(a)==0 || size(n) <1) return 0;
MR_IN(3)
t=1;
copy(n,mr_mip->w2);
nm8=remain(_MIPP_ mr_mip->w2,8);
if (nm8%2==0)
{
MR_OUT
return 0;
}
if (size(a)<0)
{
if (nm8%4==3) t=-1;
negify(a,mr_mip->w1);
}
else copy(a,mr_mip->w1);
while (size(mr_mip->w1)!=0)
{
while (remain(_MIPP_ mr_mip->w1,2)==0)
{
subdiv(_MIPP_ mr_mip->w1,2,mr_mip->w1);
if (nm8==3 || nm8==5) t=-t;
}
if (mr_compare(mr_mip->w1,mr_mip->w2)<0)
{
onm8=nm8;
w=mr_mip->w1; mr_mip->w1=mr_mip->w2; mr_mip->w2=w;
nm8=remain(_MIPP_ mr_mip->w2,8);
if (onm8%4==3 && nm8%4==3) t=-t;
}
mr_psub(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w1);
subdiv(_MIPP_ mr_mip->w1,2,mr_mip->w1);
if (nm8==3 || nm8==5) t=-t;
}
MR_OUT
if (size(mr_mip->w2)==1) return t;
return 0;
}
/*
* See "Efficient Algorithms for Computing the Jacobi Symbol"
* Eikenberry & Sorenson
*
* Its turns out this is slower than the binary method above for reasonable sizes
* of parameters (and takes up a lot more space!)
#ifdef MR_FP
#include <math.h>
#endif
static void rfind(mr_small u,mr_small v,mr_small k,mr_small sk,mr_utype *a,mr_utype *b)
{
mr_utype x2,y2,r;
mr_small w,q,x1,y1,sr;
#ifdef MR_FP
mr_small dres;
#endif
w=invers(v,k);
w=smul(u,w,k);
x1=k; x2=0;
y1=w; y2=1;
// NOTE: x1 and y1 are always +ve. x2 and y2 are always small
while (y1>=sk)
{
#ifndef MR_NOFULLWIDTH
if (x1==0) q=muldvm((mr_small)1,(mr_small)0,y1,&sr);
else
#endif
q=MR_DIV(x1,y1);
r= x1-q*y1; x1=y1; y1=r;
sr=x2-q*y2; x2=y2; y2=sr;
}
if (y2>=0) { *a=y2; *b=0-y1; }
else { *a=-y2; *b=y1; }
}
int jack(_MIPD_ big U,big V)
{ // find jacobi symbol for U wrt V. Only defined for
// positive V, V odd. Otherwise returns 0
int i,e,r,m,t,v8,u4;
mr_utype a,b;
mr_small u,v,d,g,k,sk,s;
#ifdef MR_FP
mr_small dres;
#endif
big w;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
#ifdef MR_FP_ROUNDING
mr_large ik,id;
#endif
if (mr_mip->ERNUM || size(U)==0 || size(V) <1) return 0;
copy(U,mr_mip->w1);
copy(V,mr_mip->w2);
a=0;
MR_IN(3)
if (remain(_MIPP_ mr_mip->w2,2)==0)
{ // V is even
MR_OUT
return 0;
}
if (mr_mip->base!=0)
{
k=1;
for (m=1;;m++)
{
k*=2;
if (k==MAXBASE) break;
}
if (m%2==1) {m--; k=MR_DIV(k,2);}
#ifdef MR_FP_ROUNDING
ik=mr_invert(k);
#endif
}
else
{
m=MIRACL;
k=0;
}
r=m/2;
sk=1;
for (i=0;i<r;i++) sk*=2;
t=1;
v8=remain(_MIPP_ mr_mip->w2,8);
while (!mr_mip->ERNUM && size(mr_mip->w1)!=0)
{
if (size(mr_mip->w1)<0)
{
negify(mr_mip->w1,mr_mip->w1);
if (v8%4==3) t=-t;
}
do { // oddify
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
if (mr_mip->base==k) u=mr_mip->w1->w[0];
else u=MR_REMAIN(mr_mip->w1->w[0],k);
#ifndef MR_ALWAYS_BINARY
}
#ifdef MR_FP_ROUNDING
else u=mr_sdiv(_MIPP_ mr_mip->w1,k,ik,mr_mip->w3);
#else
else u=mr_sdiv(_MIPP_ mr_mip->w1,k,mr_mip->w3);
#endif
#endif
if (u==0) {s=k; e=0;}
else
{
s=1; e=0;
while (MR_REMAIN(u,2)==0) {s*=2; e++; u=MR_DIV(u,2);}
}
if (s==mr_mip->base) mr_shift(_MIPP_ mr_mip->w1,-1,mr_mip->w1);
#ifdef MR_FP_ROUNDING
else if (s>1)
{
mr_sdiv(_MIPP_ mr_mip->w1,s,mr_invert(s),mr_mip->w1);
}
#else
else if (s>1) mr_sdiv(_MIPP_ mr_mip->w1,s,mr_mip->w1);
#endif
} while (u==0);
if (e%2!=0 && (v8==3 || v8==5)) t=-t;
if (mr_compare(mr_mip->w1,mr_mip->w2)<0)
{
if (mr_mip->base==mr_mip->base2) u4=(int)MR_REMAIN(mr_mip->w1->w[0],4);
else u4=remain(_MIPP_ mr_mip->w1,4);
if (v8%4==3 && u4==3) t=-t;
w=mr_mip->w1; mr_mip->w1=mr_mip->w2; mr_mip->w2=w;
}
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
if (k==mr_mip->base)
{
u=mr_mip->w1->w[0];
v=mr_mip->w2->w[0];
}
else
{
u=MR_REMAIN(mr_mip->w1->w[0],k);
v=MR_REMAIN(mr_mip->w2->w[0],k);
}
#ifndef MR_ALWAYS_BINARY
}
else
{
#ifdef MR_FP_ROUNDING
u=mr_sdiv(_MIPP_ mr_mip->w1,k,ik,mr_mip->w3);
v=mr_sdiv(_MIPP_ mr_mip->w2,k,ik,mr_mip->w3);
#else
u=mr_sdiv(_MIPP_ mr_mip->w1,k,mr_mip->w3);
v=mr_sdiv(_MIPP_ mr_mip->w2,k,mr_mip->w3);
#endif
}
#endif
rfind(u,v,k,sk,&a,&b);
if (a>1)
{
#ifdef MR_FP_ROUNDING
d=mr_sdiv(_MIPP_ mr_mip->w2,a,mr_invert(a),mr_mip->w3);
#else
d=mr_sdiv(_MIPP_ mr_mip->w2,a,mr_mip->w3);
#endif
d=sgcd(d,a);
a=MR_DIV(a,d);
}
else d=1;
if (d>1)
{
#ifdef MR_FP_ROUNDING
id=mr_invert(d);
mr_sdiv(_MIPP_ mr_mip->w2,d,id,mr_mip->w2);
u=mr_sdiv(_MIPP_ mr_mip->w1,d,id,mr_mip->w3);
#else
mr_sdiv(_MIPP_ mr_mip->w2,d,mr_mip->w2);
u=mr_sdiv(_MIPP_ mr_mip->w1,d,mr_mip->w3);
#endif
}
else u=0;
g=a;
if (mr_mip->base==mr_mip->base2) v8=(int)MR_REMAIN(mr_mip->w2->w[0],8);
else v8=remain(_MIPP_ mr_mip->w2,8);
while (MR_REMAIN(g,2)==0)
{
g=MR_DIV(g,2);
if (v8==3 || v8==5) t=-t;
}
if (MR_REMAIN(g,4)==3 && v8%4==3) t=-t;
#ifdef MR_FP_ROUNDING
v=mr_sdiv(_MIPP_ mr_mip->w2,g,mr_invert(g),mr_mip->w3);
#else
v=mr_sdiv(_MIPP_ mr_mip->w2,g,mr_mip->w3);
#endif
t*=jac(v,g)*jac(u,d);
if (t==0)
{
MR_OUT
return 0;
}
// printf("a= %I64d b=%I64d %d\n",a,b,(int)b);
if (a>1) mr_pmul(_MIPP_ mr_mip->w1,a,mr_mip->w1);
if (b>=0)
mr_pmul(_MIPP_ mr_mip->w2,b,mr_mip->w3);
else
{
b=-b;
mr_pmul(_MIPP_ mr_mip->w2,b,mr_mip->w3);
negify(mr_mip->w3,mr_mip->w3);
}
// premult(_MIPP_ mr_mip->w2,(int)b,mr_mip->w3); <- nasty bug - potential loss of precision in b
add(_MIPP_ mr_mip->w1,mr_mip->w3,mr_mip->w1);
if (k==mr_mip->base) mr_shift(_MIPP_ mr_mip->w1,-1,mr_mip->w1);
#ifdef MR_FP_ROUNDING
else mr_sdiv(_MIPP_ mr_mip->w1,k,ik,mr_mip->w1);
#else
else mr_sdiv(_MIPP_ mr_mip->w1,k,mr_mip->w1);
#endif
}
MR_OUT
if (size(mr_mip->w2)==1) return t;
return 0;
}
*/

157
crypto/sm2/miracl/mrlucas.c Normal file
View File

@@ -0,0 +1,157 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL methods for evaluating lucas V function
* mrlucas.c (Postl's algorithm)
*/
#include <stdlib.h>
#include <openssl/miracl.h>
void nres_lucas(_MIPD_ big p,big r,big vp,big v)
{
int i,nb;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
MR_IN(107)
if (size(r)==0)
{
zero(vp);
convert(_MIPP_ 2,v);
nres(_MIPP_ v,v);
MR_OUT
return;
}
if (size(r)==1 || size(r)==(-1))
{ /* note - sign of r doesn't matter */
convert(_MIPP_ 2,vp);
nres(_MIPP_ vp,vp);
copy(p,v);
MR_OUT
return;
}
copy(p,mr_mip->w3);
convert(_MIPP_ 2,mr_mip->w4);
nres(_MIPP_ mr_mip->w4,mr_mip->w4); /* w4=2 */
copy(mr_mip->w4,mr_mip->w8);
copy(mr_mip->w3,mr_mip->w9);
copy(r,mr_mip->w1);
insign(PLUS,mr_mip->w1);
decr(_MIPP_ mr_mip->w1,1,mr_mip->w1);
#ifndef MR_ALWAYS_BINARY
if (mr_mip->base==mr_mip->base2)
{
#endif
nb=logb2(_MIPP_ mr_mip->w1);
for (i=nb-1;i>=0;i--)
{
if (mr_mip->user!=NULL) (*mr_mip->user)();
if (mr_testbit(_MIPP_ mr_mip->w1,i))
{
nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w9,mr_mip->w8);
nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w3,mr_mip->w8);
nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w9,mr_mip->w9);
nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w4,mr_mip->w9);
}
else
{
nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w8,mr_mip->w9);
nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w3,mr_mip->w9);
nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w8,mr_mip->w8);
nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w4,mr_mip->w8);
}
}
#ifndef MR_ALWAYS_BINARY
}
else
{
expb2(_MIPP_ logb2(_MIPP_ mr_mip->w1)-1,mr_mip->w2);
while (!mr_mip->ERNUM && size(mr_mip->w2)!=0)
{ /* use binary method */
if (mr_compare(mr_mip->w1,mr_mip->w2)>=0)
{ /* vp=v*vp-p, v=v*v-2 */
nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w9,mr_mip->w8);
nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w3,mr_mip->w8);
nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w9,mr_mip->w9);
nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w4,mr_mip->w9);
subtract(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w1);
}
else
{ /* v=v*vp-p, vp=vp*vp-2 */
nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w8,mr_mip->w9);
nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w3,mr_mip->w9);
nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w8,mr_mip->w8);
nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w4,mr_mip->w8);
}
subdiv(_MIPP_ mr_mip->w2,2,mr_mip->w2);
}
}
#endif
copy(mr_mip->w9,v);
if (v!=vp) copy(mr_mip->w8,vp);
MR_OUT
}
void lucas(_MIPD_ big p,big r,big n,big vp,big v)
{
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return;
MR_IN(108)
prepare_monty(_MIPP_ n);
nres(_MIPP_ p,mr_mip->w3);
nres_lucas(_MIPP_ mr_mip->w3,r,mr_mip->w8,mr_mip->w9);
redc(_MIPP_ mr_mip->w9,v);
if (v!=vp) redc(_MIPP_ mr_mip->w8,vp);
MR_OUT
}

1414
crypto/sm2/miracl/mrmonty.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,57 @@
/*
* Borland C++ 32-bit compiler (BCC32). Use with mirdef.h32
* Uses inline assembly feature. Suitable for Win32 Apps
* Also compatible with Microsoft Visual C++ 32-bit compiler
*/
#define ASM __asm__
int muldiv(a,b,c,m,rp)
int a,b,c,m,*rp;
{
ASM ("mov eax,DWORD PTR a");
ASM ("mul DWORD PTR b");
ASM ("add eax,DWORD PTR c");
ASM ("adc edx,0h");
ASM ("div DWORD PTR m");
ASM ("mov ebx,DWORD PTR rp");
ASM ("mov [ebx],edx");
}
int muldvm(a,c,m,rp)
int a,c,m,*rp;
{
ASM ("mov edx,DWORD PTR a");
ASM ("mov eax,DWORD PTR c");
ASM ("div DWORD PTR m");
ASM ("mov ebx,DWORD PTR rp");
ASM ("mov [ebx],edx");
}
int muldvd(a,b,c,rp)
int a,b,c,*rp;
{
ASM ("mov eax,DWORD PTR a");
ASM ("mul DWORD PTR b");
ASM ("add eax,DWORD PTR c");
ASM ("adc edx,0h");
ASM ("mov ebx,DWORD PTR rp");
ASM ("mov [ebx],eax");
ASM ("mov eax,edx");
}
void muldvd2(a,b,c,rp)
int a,b,*c,*rp;
{
ASM ("mov eax,DWORD PTR a");
ASM ("mul DWORD PTR b");
ASM ("mov ebx,DWORD PTR c");
ASM ("add eax,[ebx]");
ASM ("adc edx,0h");
ASM ("mov esi,DWORD PTR rp");
ASM ("add eax,[esi]");
ASM ("adc edx,0h");
ASM ("mov [esi],eax");
ASM ("mov [ebx],edx");
}

188
crypto/sm2/miracl/mrsroot.c Normal file
View File

@@ -0,0 +1,188 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL method for modular square root
* mrsroot.c
*
* Siguna Mueller's O(lg(p)^3) algorithm, Designs Codes and Cryptography, 2004
*
* This is a little slower for p=1 mod 4 primes, but its not time critical, and
* more importantly it doesn't pull in the large powmod code into elliptic curve programs
* It does require code from mrjack.c and mrlucas.c
*
* If p=3 mod 4, then sqrt(a)=a^[(p+1)/4] mod p. Note that for many elliptic curves
* (p+1)/4 has very low hamming weight.
*
* (was sqrt(a) = V_{(p+1)/4}(a+1/a,1)/(1+1/a))
*
* Mueller's method is also very simple, uses very little memory, and it works just fine for p=1 mod 8 primes
* (for example the "annoying" NIST modulus 2^224-2^96+1)
* Also doesn't waste time on non-squares, as a jacobi test is done first
*
* If you know that the prime is 3 mod 4, and you know that x is almost certainly a QR
* then the jacobi-dependent code can be deleted with some space savings.
*
* NOTE - IF p IS NOT PRIME, THIS CODE WILL FAIL SILENTLY!
*
*/
#include <stdlib.h>
#include <openssl/miracl.h>
BOOL nres_sqroot(_MIPD_ big x,big w)
{ /* w=sqrt(x) mod p. This depends on p being prime! */
int t,js;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return FALSE;
copy(x,w);
if (size(w)==0) return TRUE;
MR_IN(100)
redc(_MIPP_ w,w); /* get it back into normal form */
if (size(w)==1) /* square root of 1 is 1 */
{
nres(_MIPP_ w,w);
MR_OUT
return TRUE;
}
if (size(w)==4) /* square root of 4 is 2 */
{
convert(_MIPP_ 2,w);
nres(_MIPP_ w,w);
MR_OUT
return TRUE;
}
if (jack(_MIPP_ w,mr_mip->modulus)!=1)
{ /* Jacobi test */
zero(w);
MR_OUT
return FALSE;
}
js=mr_mip->pmod8%4-2; /* 1 mod 4 or 3 mod 4 prime? */
incr(_MIPP_ mr_mip->modulus,js,mr_mip->w10);
subdiv(_MIPP_ mr_mip->w10,4,mr_mip->w10); /* (p+/-1)/4 */
if (js==1)
{ /* 3 mod 4 primes - do a quick and dirty sqrt(x)=x^(p+1)/4 mod p */
nres(_MIPP_ w,mr_mip->w2);
copy(mr_mip->one,w);
forever
{ /* Simple Right-to-Left exponentiation */
if (mr_mip->user!=NULL) (*mr_mip->user)();
if (subdiv(_MIPP_ mr_mip->w10,2,mr_mip->w10)!=0)
nres_modmult(_MIPP_ w,mr_mip->w2,w);
if (mr_mip->ERNUM || size(mr_mip->w10)==0) break;
nres_modmult(_MIPP_ mr_mip->w2,mr_mip->w2,mr_mip->w2);
}
/* nres_moddiv(_MIPP_ mr_mip->one,w,mr_mip->w11);
nres_modadd(_MIPP_ mr_mip->w11,w,mr_mip->w3);
nres_lucas(_MIPP_ mr_mip->w3,mr_mip->w10,w,w);
nres_modadd(_MIPP_ mr_mip->w11,mr_mip->one,mr_mip->w11);
nres_moddiv(_MIPP_ w,mr_mip->w11,w); */
}
else
{ /* 1 mod 4 primes */
for (t=1; ;t++)
{ /* t=1.5 on average */
if (t==1) copy(w,mr_mip->w4);
else
{
premult(_MIPP_ w,t,mr_mip->w4);
divide(_MIPP_ mr_mip->w4,mr_mip->modulus,mr_mip->modulus);
premult(_MIPP_ mr_mip->w4,t,mr_mip->w4);
divide(_MIPP_ mr_mip->w4,mr_mip->modulus,mr_mip->modulus);
}
decr(_MIPP_ mr_mip->w4,4,mr_mip->w1);
if (jack(_MIPP_ mr_mip->w1,mr_mip->modulus)==js) break;
if (mr_mip->ERNUM) break;
}
decr(_MIPP_ mr_mip->w4,2,mr_mip->w3);
nres(_MIPP_ mr_mip->w3,mr_mip->w3);
nres_lucas(_MIPP_ mr_mip->w3,mr_mip->w10,w,w); /* heavy lifting done here */
if (t!=1)
{
convert(_MIPP_ t,mr_mip->w11);
nres(_MIPP_ mr_mip->w11,mr_mip->w11);
nres_moddiv(_MIPP_ w,mr_mip->w11,w);
}
}
MR_OUT
return TRUE;
}
BOOL sqroot(_MIPD_ big x,big p,big w)
{ /* w = sqrt(x) mod p */
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return FALSE;
MR_IN(101)
if (subdivisible(_MIPP_ p,2))
{ /* p must be odd */
zero(w);
MR_OUT
return FALSE;
}
prepare_monty(_MIPP_ p);
nres(_MIPP_ x,w);
if (nres_sqroot(_MIPP_ w,w))
{
redc(_MIPP_ w,w);
MR_OUT
return TRUE;
}
zero(w);
MR_OUT
return FALSE;
}

495
crypto/sm2/miracl/mrxgcd.c Normal file
View File

@@ -0,0 +1,495 @@
/***************************************************************************
*
Copyright 2013 CertiVox IOM Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
/*
* MIRACL Extended Greatest Common Divisor module.
* mrxgcd.c
*/
#include <openssl/miracl.h>
#ifdef MR_FP
#include <math.h>
#endif
#ifdef MR_COUNT_OPS
extern int fpx;
#endif
#ifndef MR_USE_BINARY_XGCD
#ifdef mr_dltype
static mr_small qdiv(mr_large u,mr_large v)
{ /* fast division - small quotient expected. */
mr_large lq,x=u;
#ifdef MR_FP
mr_small dres;
#endif
x-=v;
if (x<v) return 1;
x-=v;
if (x<v) return 2;
x-=v;
if (x<v) return 3;
x-=v;
if (x<v) return 4;
x-=v;
if (x<v) return 5;
x-=v;
if (x<v) return 6;
x-=v;
if (x<v) return 7;
x-=v;
if (x<v) return 8;
/* do it the hard way! */
lq=8+MR_DIV(x,v);
if (lq>=MAXBASE) return 0;
return (mr_small)lq;
}
#else
static mr_small qdiv(mr_small u,mr_small v)
{ /* fast division - small quotient expected */
mr_small x=u;
x-=v;
if (x<v) return 1;
x-=v;
if (x<v) return 2;
return MR_DIV(u,v);
}
#endif
int xgcd(_MIPD_ big x,big y,big xd,big yd,big z)
{ /* greatest common divisor by Euclids method *
* extended to also calculate xd and yd where *
* z = x.xd + y.yd = gcd(x,y) *
* if xd, yd not distinct, only xd calculated *
* z only returned if distinct from xd and yd *
* xd will always be positive, yd negative */
int s,n,iter;
mr_small r,a,b,c,d;
mr_small q,m,sr;
#ifdef MR_FP
mr_small dres;
#endif
#ifdef mr_dltype
union doubleword uu,vv;
mr_large u,v,lr;
#else
mr_small u,v,lr;
#endif
BOOL last,dplus=TRUE;
big t;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (mr_mip->ERNUM) return 0;
MR_IN(30)
#ifdef MR_COUNT_OPS
fpx++;
#endif
copy(x,mr_mip->w1);
copy(y,mr_mip->w2);
s=exsign(mr_mip->w1);
insign(PLUS,mr_mip->w1);
insign(PLUS,mr_mip->w2);
convert(_MIPP_ 1,mr_mip->w3);
zero(mr_mip->w4);
last=FALSE;
a=b=c=d=0;
iter=0;
while (size(mr_mip->w2)!=0)
{
if (b==0)
{ /* update mr_mip->w1 and mr_mip->w2 */
divide(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w5);
t=mr_mip->w1,mr_mip->w1=mr_mip->w2,mr_mip->w2=t; /* swap(mr_mip->w1,mr_mip->w2) */
multiply(_MIPP_ mr_mip->w4,mr_mip->w5,mr_mip->w0);
add(_MIPP_ mr_mip->w3,mr_mip->w0,mr_mip->w3);
t=mr_mip->w3,mr_mip->w3=mr_mip->w4,mr_mip->w4=t; /* swap(xd,yd) */
iter++;
}
else
{
/* printf("a= %I64u b= %I64u c= %I64u d= %I64u \n",a,b,c,d); */
mr_pmul(_MIPP_ mr_mip->w1,c,mr_mip->w5); /* c*w1 */
mr_pmul(_MIPP_ mr_mip->w1,a,mr_mip->w1); /* a*w1 */
mr_pmul(_MIPP_ mr_mip->w2,b,mr_mip->w0); /* b*w2 */
mr_pmul(_MIPP_ mr_mip->w2,d,mr_mip->w2); /* d*w2 */
if (!dplus)
{
mr_psub(_MIPP_ mr_mip->w0,mr_mip->w1,mr_mip->w1); /* b*w2-a*w1 */
mr_psub(_MIPP_ mr_mip->w5,mr_mip->w2,mr_mip->w2); /* c*w1-d*w2 */
}
else
{
mr_psub(_MIPP_ mr_mip->w1,mr_mip->w0,mr_mip->w1); /* a*w1-b*w2 */
mr_psub(_MIPP_ mr_mip->w2,mr_mip->w5,mr_mip->w2); /* d*w2-c*w1 */
}
mr_pmul(_MIPP_ mr_mip->w3,c,mr_mip->w5);
mr_pmul(_MIPP_ mr_mip->w3,a,mr_mip->w3);
mr_pmul(_MIPP_ mr_mip->w4,b,mr_mip->w0);
mr_pmul(_MIPP_ mr_mip->w4,d,mr_mip->w4);
if (a==0) copy(mr_mip->w0,mr_mip->w3);
else mr_padd(_MIPP_ mr_mip->w3,mr_mip->w0,mr_mip->w3);
mr_padd(_MIPP_ mr_mip->w4,mr_mip->w5,mr_mip->w4);
}
if (mr_mip->ERNUM || size(mr_mip->w2)==0) break;
n=(int)mr_mip->w1->len;
if (n==1)
{
last=TRUE;
u=mr_mip->w1->w[0];
v=mr_mip->w2->w[0];
}
else
{
m=mr_mip->w1->w[n-1]+1;
#ifndef MR_SIMPLE_BASE
if (mr_mip->base==0)
{
#endif
#ifndef MR_NOFULLWIDTH
#ifdef mr_dltype
/* use double length type if available */
if (n>2 && m!=0)
{ /* squeeze out as much significance as possible */
uu.h[MR_TOP]=muldvm(mr_mip->w1->w[n-1],mr_mip->w1->w[n-2],m,&sr);
uu.h[MR_BOT]=muldvm(sr,mr_mip->w1->w[n-3],m,&sr);
vv.h[MR_TOP]=muldvm(mr_mip->w2->w[n-1],mr_mip->w2->w[n-2],m,&sr);
vv.h[MR_BOT]=muldvm(sr,mr_mip->w2->w[n-3],m,&sr);
}
else
{
uu.h[MR_TOP]=mr_mip->w1->w[n-1];
uu.h[MR_BOT]=mr_mip->w1->w[n-2];
vv.h[MR_TOP]=mr_mip->w2->w[n-1];
vv.h[MR_BOT]=mr_mip->w2->w[n-2];
if (n==2) last=TRUE;
}
u=uu.d;
v=vv.d;
#else
if (m==0)
{
u=mr_mip->w1->w[n-1];
v=mr_mip->w2->w[n-1];
}
else
{
u=muldvm(mr_mip->w1->w[n-1],mr_mip->w1->w[n-2],m,&sr);
v=muldvm(mr_mip->w2->w[n-1],mr_mip->w2->w[n-2],m,&sr);
}
#endif
#endif
#ifndef MR_SIMPLE_BASE
}
else
{
#ifdef mr_dltype
if (n>2)
{ /* squeeze out as much significance as possible */
u=muldiv(mr_mip->w1->w[n-1],mr_mip->base,mr_mip->w1->w[n-2],m,&sr);
u=u*mr_mip->base+muldiv(sr,mr_mip->base,mr_mip->w1->w[n-3],m,&sr);
v=muldiv(mr_mip->w2->w[n-1],mr_mip->base,mr_mip->w2->w[n-2],m,&sr);
v=v*mr_mip->base+muldiv(sr,mr_mip->base,mr_mip->w2->w[n-3],m,&sr);
}
else
{
u=(mr_large)mr_mip->base*mr_mip->w1->w[n-1]+mr_mip->w1->w[n-2];
v=(mr_large)mr_mip->base*mr_mip->w2->w[n-1]+mr_mip->w2->w[n-2];
last=TRUE;
}
#else
u=muldiv(mr_mip->w1->w[n-1],mr_mip->base,mr_mip->w1->w[n-2],m,&sr);
v=muldiv(mr_mip->w2->w[n-1],mr_mip->base,mr_mip->w2->w[n-2],m,&sr);
#endif
}
#endif
}
dplus=TRUE;
a=1; b=0; c=0; d=1;
forever
{ /* work only with most significant piece */
if (last)
{
if (v==0) break;
q=qdiv(u,v);
if (q==0) break;
}
else
{
if (dplus)
{
if ((mr_small)(v-c)==0 || (mr_small)(v+d)==0) break;
q=qdiv(u+a,v-c);
if (q==0) break;
if (q!=qdiv(u-b,v+d)) break;
}
else
{
if ((mr_small)(v+c)==0 || (mr_small)(v-d)==0) break;
q=qdiv(u-a,v+c);
if (q==0) break;
if (q!=qdiv(u+b,v-d)) break;
}
}
if (q==1)
{
if ((mr_small)(b+d) >= MAXBASE) break;
r=a+c; a=c; c=r;
r=b+d; b=d; d=r;
lr=u-v; u=v; v=lr;
}
else
{
if (q>=MR_DIV(MAXBASE-b,d)) break;
r=a+q*c; a=c; c=r;
r=b+q*d; b=d; d=r;
lr=u-q*v; u=v; v=lr;
}
iter++;
dplus=!dplus;
}
iter%=2;
}
if (s==MINUS) iter++;
if (iter%2==1) subtract(_MIPP_ y,mr_mip->w3,mr_mip->w3);
if (xd!=yd)
{
negify(x,mr_mip->w2);
mad(_MIPP_ mr_mip->w2,mr_mip->w3,mr_mip->w1,y,mr_mip->w4,mr_mip->w4);
copy(mr_mip->w4,yd);
}
copy(mr_mip->w3,xd);
if (z!=xd && z!=yd) copy(mr_mip->w1,z);
MR_OUT
return (size(mr_mip->w1));
}
int invmodp(_MIPD_ big x,big y,big z)
{
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
int gcd;
MR_IN(213);
gcd=xgcd(_MIPP_ x,y,z,z,z);
MR_OUT
return gcd;
}
#else
/* much smaller, much slower binary inversion algorithm */
/* fails silently if a is not co-prime to p */
/* experimental! At least 3 times slower than standard method.. */
int invmodp(_MIPD_ big a,big p,big z)
{
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
big u,v,x1,x2;
MR_IN(213);
u=mr_mip->w1; v=mr_mip->w2; x1=mr_mip->w3; x2=mr_mip->w4;
copy(a,u);
copy(p,v);
convert(_MIPP_ 1,x1);
zero(x2);
while (size(u)!=1 && size(v)!=1)
{
while (remain(_MIPP_ u,2)==0)
{
subdiv(_MIPP_ u,2,u);
if (remain(_MIPP_ x1,2)!=0) add(_MIPP_ x1,p,x1);
subdiv(_MIPP_ x1,2,x1);
}
while (remain(_MIPP_ v,2)==0)
{
subdiv(_MIPP_ v,2,v);
if (remain(_MIPP_ x2,2)!=0) add(_MIPP_ x2,p,x2);
subdiv(_MIPP_ x2,2,x2);
}
if (compare(u,v)>=0)
{
mr_psub(_MIPP_ u,v,u);
subtract(_MIPP_ x1,x2,x1);
}
else
{
mr_psub(_MIPP_ v,u,v);
subtract(_MIPP_ x2,x1,x2);
}
}
if (size(u)==1) copy(x1,z);
else copy(x2,z);
if (size(z)<0) add(_MIPP_ z,p,z);
MR_OUT
return 1; /* note - no checking that gcd=1 */
}
#endif
#ifndef MR_STATIC
/* Montgomery's method for multiple
simultaneous modular inversions */
BOOL double_inverse(_MIPD_ big n,big x,big y,big w,big z)
{
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
MR_IN(146)
mad(_MIPP_ x,w,w,n,n,mr_mip->w6);
if (size(mr_mip->w6)==0)
{
mr_berror(_MIPP_ MR_ERR_DIV_BY_ZERO);
MR_OUT
return FALSE;
}
invmodp(_MIPP_ mr_mip->w6,n,mr_mip->w6);
mad(_MIPP_ w,mr_mip->w6,w,n,n,y);
mad(_MIPP_ x,mr_mip->w6,x,n,n,z);
MR_OUT
return TRUE;
}
BOOL multi_inverse(_MIPD_ int m,big *x,big n,big *w)
{ /* find w[i]=1/x[i] mod n, for i=0 to m-1 *
* x and w MUST be distinct */
int i;
#ifdef MR_OS_THREADS
miracl *mr_mip=get_mip();
#endif
if (m==0) return TRUE;
if (m<0) return FALSE;
MR_IN(25)
if (x==w)
{
mr_berror(_MIPP_ MR_ERR_BAD_PARAMETERS);
MR_OUT
return FALSE;
}
if (m==1)
{
invmodp(_MIPP_ x[0],n,w[0]);
MR_OUT
return TRUE;
}
convert(_MIPP_ 1,w[0]);
copy(x[0],w[1]);
for (i=2;i<m;i++)
mad(_MIPP_ w[i-1],x[i-1],x[i-1],n,n,w[i]);
mad(_MIPP_ w[m-1],x[m-1],x[m-1],n,n,mr_mip->w6); /* y=x[0]*x[1]*x[2]....x[m-1] */
if (size(mr_mip->w6)==0)
{
mr_berror(_MIPP_ MR_ERR_DIV_BY_ZERO);
MR_OUT
return FALSE;
}
invmodp(_MIPP_ mr_mip->w6,n,mr_mip->w6);
/* Now y=1/y */
copy(x[m-1],mr_mip->w5);
mad(_MIPP_ w[m-1],mr_mip->w6,mr_mip->w6,n,n,w[m-1]);
for (i=m-2;;i--)
{
if (i==0)
{
mad(_MIPP_ mr_mip->w5,mr_mip->w6,mr_mip->w6,n,n,w[0]);
break;
}
mad(_MIPP_ w[i],mr_mip->w5,w[i],n,n,w[i]);
mad(_MIPP_ w[i],mr_mip->w6,w[i],n,n,w[i]);
mad(_MIPP_ mr_mip->w5,x[i],x[i],n,n,mr_mip->w5);
}
MR_OUT
return TRUE;
}
#endif