├── Makefile ├── README.md ├── lib ├── libgmsdf.a └── libgmsdf.so ├── rule.mk ├── source ├── include │ ├── miracl.h │ ├── mirdef │ ├── mirdef.arm │ ├── mirdef.ash │ ├── mirdef.atm │ ├── mirdef.bfp │ ├── mirdef.bpp │ ├── mirdef.bs │ ├── mirdef.ccc │ ├── mirdef.cm │ ├── mirdef.dll │ ├── mirdef.gcc │ ├── mirdef.gen │ ├── mirdef.gfp │ ├── mirdef.h │ ├── mirdef.h16 │ ├── mirdef.h32 │ ├── mirdef.h64 │ ├── mirdef.haf │ ├── mirdef.hio │ ├── mirdef.hpc │ ├── mirdef.hpp │ ├── mirdef.ibe │ ├── mirdef.kep │ ├── mirdef.lnx │ ├── mirdef.mgw │ ├── mirdef.mik │ ├── mirdef.mip │ ├── mirdef.mmm │ ├── mirdef.ol │ ├── mirdef.pic │ ├── mirdef.ppc │ ├── mirdef.scr │ ├── mirdef.sjc │ ├── mirdef.tst │ ├── mirdef.w64 │ └── mirdef.wpp ├── mersenne.c ├── mraes.c ├── mralloc.c ├── mrarth0.c ├── mrarth1.c ├── mrarth2.c ├── mrarth3.c ├── mrbits.c ├── mrbrick.c ├── mrbuild.c ├── mrcore.c ├── mrcrt.c ├── mrcurve.c ├── mrdouble.c ├── mrebrick.c ├── mrec2m.c ├── mrecn2.c ├── mrfast.c ├── mrflash.c ├── mrflsh1.c ├── mrflsh2.c ├── mrflsh3.c ├── mrflsh4.c ├── mrfpe.c ├── mrfrnd.c ├── mrgcd.c ├── mrgcm.c ├── mrgf2m.c ├── mrio1.c ├── mrio2.c ├── mrjack.c ├── mrlucas.c ├── mrmonty.c ├── mrmuldv.c ├── mrmuldv.c32 ├── mrmuldv.ccc ├── mrmuldv.g64 ├── mrmuldv.gcc ├── mrmuldv.gpp ├── mrmuldv.ppc ├── mrmuldv.s ├── mrmuldv.s64 ├── mrmuldv.tcc ├── mrmuldv.w64 ├── mrpi.c ├── mrpower.c ├── mrprime.c ├── mrrand.c ├── mrround.c ├── mrscrt.c ├── mrsha3.c ├── mrshs.c ├── mrshs256.c ├── mrshs512.c ├── mrsmall.c ├── mrsroot.c ├── mrstrong.c ├── mrxgcd.c ├── mrzzn2.c ├── mrzzn2b.c ├── mrzzn3.c └── mrzzn4.c └── utils ├── Makefile ├── bmark ├── Makefile ├── bmark └── bmark.c ├── sm2 ├── .obj │ ├── kdf.o │ ├── main.o │ ├── sm2.o │ ├── sm2_enc.o │ ├── sm2_key_ex.o │ ├── sm2_sv.o │ └── sm3.o ├── Makefile ├── kdf.c ├── kdf.h ├── main.c ├── sm2 ├── sm2.c ├── sm2_enc.c ├── sm2_key_ex.c ├── sm2_key_ex.h ├── sm2_sv.c ├── sm3.c └── sm3.h ├── sm3 ├── Makefile ├── sm3 ├── sm3.c └── sm3.h └── sm4 ├── .obj ├── sm4.o └── sm4test.o ├── Makefile ├── sm4 ├── sm4.c ├── sm4.h └── sm4test.c /Makefile: -------------------------------------------------------------------------------- 1 | export IDIR LDIR DEFS 2 | 3 | IDIR += ./include 4 | IDIR += ./source/include 5 | 6 | CFLAGS := $(addprefix -I, $(IDIR)) 7 | CFLAGS += $(addprefix -L, $(LDIR)) 8 | CFLAGS += $(addprefix -D, $(DEFS)) 9 | CFLAGS += -shared -fPIC -O2 10 | 11 | 12 | include rule.mk 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gmalg 2 | 国密算法sm1,sm2,sm3,sm4算法源码 3 | -------------------------------------------------------------------------------- /lib/libgmsdf.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/lib/libgmsdf.a -------------------------------------------------------------------------------- /lib/libgmsdf.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/lib/libgmsdf.so -------------------------------------------------------------------------------- /rule.mk: -------------------------------------------------------------------------------- 1 | VERSION := 0.1 2 | BASENAME := libgmsdf 3 | STATICLIB := $(BASENAME).a 4 | SHAREDLIB := $(BASENAME).so 5 | 6 | DIR_OBJ = ./.obj 7 | SOURCES = $(wildcard source/*.c) 8 | OBJS = $(patsubst %.c,${DIR_OBJ}/%.o,$(notdir ${SOURCES})) 9 | 10 | export CC STRIP MAKE AR 11 | .PHONY: all clean 12 | 13 | all: $(OBJS) 14 | $(CC) $(CFLAGS) -o ${DIR_OBJ}/$(SHAREDLIB) $(OBJS) 15 | $(AR) -cr ${DIR_OBJ}/$(STATICLIB) $(OBJS) 16 | cp ${DIR_OBJ}/$(SHAREDLIB) lib/ 17 | cp ${DIR_OBJ}/$(STATICLIB) lib/ 18 | $(MAKE) -C utils 19 | 20 | ${DIR_OBJ}/%.o:source/%.c 21 | test -d $(DIR_OBJ) || mkdir -p $(DIR_OBJ) 22 | $(CC) $(CFLAGS) -c $< -o $@ 23 | 24 | clean: 25 | $(RM) ${DIR_OBJ}/* 26 | $(MAKE) -C utils clean 27 | 28 | mrproper: clean 29 | $(RM) tags *.tgz 30 | 31 | tarball: 32 | @git archive --prefix=$(BASENAME)-$(GIT_VER)/ --format=tar HEAD \ 33 | | gzip > $(BASENAME)-$(GIT_VER).tgz 34 | -------------------------------------------------------------------------------- /source/include/mirdef: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 32-bit computers 4 | * e.g. 80386+ PC, VAX, ARM etc. Assembly language versions of muldiv, 5 | * muldvm, muldvd and muldvd2 will be necessary. See mrmuldv.any 6 | * 7 | * Also suitable for DJGPP GNU C Compiler 8 | * ... but change __int64 to long long 9 | */ 10 | 11 | #define MIRACL 32 12 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 13 | #define mr_utype int 14 | /* the underlying type is usually int * 15 | * but see mrmuldv.any */ 16 | #define mr_unsign32 unsigned int 17 | /* 32 bit unsigned type */ 18 | #define MR_IBITS 32 /* bits in int */ 19 | #define MR_LBITS 32 /* bits in long */ 20 | #define MR_FLASH 52 21 | /* delete this definition if integer * 22 | * only version of MIRACL required */ 23 | /* Number of bits per double mantissa */ 24 | 25 | #define mr_dltype __int64 /* ... or long long for Unix/Linux */ 26 | #define mr_unsign64 unsigned __int64 27 | 28 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 29 | 30 | 31 | -------------------------------------------------------------------------------- /source/include/mirdef.arm: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | 6 | #define MIRACL 32 7 | #define MR_LITTLE_ENDIAN 8 | 9 | /* or perhaps 10 | #define MR_BIG_ENDIAN 11 | */ 12 | 13 | #define mr_utype int 14 | #define MR_IBITS 32 15 | #define MR_LBITS 32 16 | #define mr_unsign32 unsigned int 17 | #define mr_dltype long long 18 | #define mr_unsign64 unsigned long long 19 | #define MR_NOASM 20 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 21 | 22 | 23 | -------------------------------------------------------------------------------- /source/include/mirdef.ash: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 32 7 | #define mr_utype int 8 | #define MR_IBITS 32 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned int 11 | #define mr_dltype __int64 12 | #define mr_unsign64 unsigned __int64 13 | #define MR_STATIC 12 14 | #define MR_ALWAYS_BINARY 15 | #define MR_NOASM 16 | #define MR_STRIPPED_DOWN 17 | #define MR_GENERIC_MT 18 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 19 | #define MR_BITSINCHAR 8 20 | #define MR_SHORT_OF_MEMORY 21 | #define MR_AFFINE_ONLY 22 | #define MR_NOSUPPORT_COMPRESSION 23 | -------------------------------------------------------------------------------- /source/include/mirdef.atm: -------------------------------------------------------------------------------- 1 | /* 2 | mirdef.h file for Atmeg128 3 | 4 | */ 5 | 6 | #define MR_LITTLE_ENDIAN 7 | #define MIRACL 8 8 | #define mr_utype char /* wordlength of processor */ 9 | #define MR_IBITS 16 /* number of bits in int */ 10 | #define MR_LBITS 32 /* number of bits in long */ 11 | #define mr_unsign32 unsigned long /* unsigned 32-bit type */ 12 | #define mr_dltype short /* double-length type (twice the number of bits of mr_utype) */ 13 | #define MR_STATIC 21 /* 21*8 > 163 bits */ 14 | #define MR_ALWAYS_BINARY 15 | #define MR_NOASM /* no assembly language */ 16 | #define MR_STRIPPED_DOWN 17 | #define MR_GENERIC_MT /* multi-threaded */ 18 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 19 | #define MR_BITSINCHAR 8 20 | #define MR_SHORT_OF_MEMORY 21 | #define MR_NO_STANDARD_IO /* no printf support */ 22 | #define MR_NO_FILE_IO /* no file support */ 23 | 24 | /* 25 | 26 | Delete the lines 27 | 28 | printf("Alice's Key= "); 29 | otnum(mip,key,stdout); 30 | 31 | and 32 | 33 | printf("Bob's Key= "); 34 | otnum(mip,key,stdout); 35 | 36 | from ecdh2m8.c 37 | 38 | Build a library from only 39 | 40 | mrcore.c 41 | mrarth0.c 42 | mrarth1.c 43 | mrbits.c 44 | mrecgf2m.c 45 | 46 | To find out basic RAM requirement, watch sizeof(miracl) 47 | 48 | */ 49 | -------------------------------------------------------------------------------- /source/include/mirdef.bfp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * 4 | * This version of mirdef.h is for use with the Borland C compiler 5 | * It uses a double underlying type, and using fast floating-point 6 | * arithmetic (include mr87f.c in the library build) 7 | * This will be fast for 512-bit arithmetic 8 | */ 9 | 10 | #define MR_LITTLE_ENDIAN 11 | #define MIRACL 64 12 | #define mr_utype double 13 | #define mr_dltype long double 14 | #define MR_NOFULLWIDTH 15 | #define MR_FP 16 | #define MR_IBITS 32 17 | #define MR_LBITS 32 18 | #define mr_unsign32 unsigned int 19 | #define mr_unsign64 unsigned __int64 20 | #define MR_FP_ROUNDING 21 | #define MR_FLASH 52 22 | #define MR_PENTIUM 18 23 | #define MAXBASE 536870912.0 24 | 25 | -------------------------------------------------------------------------------- /source/include/mirdef.bpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MIRACL 32 6 | #define MR_LITTLE_ENDIAN 7 | 8 | /* or perhaps 9 | #define MR_BIG_ENDIAN 10 | */ 11 | 12 | #define mr_utype int 13 | #define MR_IBITS 32 14 | #define MR_LBITS 32 15 | #define mr_unsign32 unsigned int 16 | #define mr_dltype long long 17 | #define mr_unsign64 unsigned long long 18 | #define MR_NOASM 19 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 20 | 21 | 22 | -------------------------------------------------------------------------------- /source/include/mirdef.bs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 64 7 | #define mr_utype __int64 8 | #define mr_unsign64 unsigned __int64 9 | #define MR_IBITS 32 10 | #define MR_LBITS 32 11 | #define mr_unsign32 unsigned int 12 | #define MR_FLASH 52 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_BITSINCHAR 8 15 | -------------------------------------------------------------------------------- /source/include/mirdef.ccc: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * 4 | * For typical 32-bit C-Only MIRACL library 5 | */ 6 | 7 | 8 | #define MIRACL 32 9 | #define MR_LITTLE_ENDIAN 10 | 11 | /* or for most non-Intel processors 12 | #define MR_BIG_ENDIAN 13 | */ 14 | 15 | #define mr_utype int 16 | #define MR_IBITS 32 17 | #define MR_LBITS 32 18 | #define mr_unsign32 unsigned int 19 | #define mr_dltype __int64 20 | 21 | /* or for gcc and most Unix 22 | #define mr_dltype long long 23 | */ 24 | 25 | #define MR_NOASM 26 | #define MR_FLASH 52 27 | 28 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 29 | 30 | -------------------------------------------------------------------------------- /source/include/mirdef.cm: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 32 7 | #define mr_utype int 8 | #define MR_IBITS 32 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned int 11 | #define mr_dltype __int64 12 | #define mr_unsign64 unsigned __int64 13 | #define MR_FLASH 52 14 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 15 | #define MR_KCM 16 16 | #define MR_BITSINCHAR 8 17 | -------------------------------------------------------------------------------- /source/include/mirdef.dll: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MIRACL 32 6 | #define MR_LITTLE_ENDIAN 7 | #define mr_utype int 8 | #define MR_IBITS 32 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned int 11 | #define mr_dltype __int64 12 | #define mr_unsign64 unsigned __int64 13 | #define MR_STRIPPED_DOWN 14 | #define MR_GENERIC_MT 15 | #define MR_NO_STANDARD_IO 16 | -------------------------------------------------------------------------------- /source/include/mirdef.gcc: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * 4 | * This version is suitable for use with the GCC compiler 5 | * in a C/C++ only unix environment. 6 | * Remove mrmuldv.c from the build 7 | * 8 | * For MS C on a PC, change BIG_ENDIAN to LITTLE_ENDIAN 9 | * and "long long" to "__int64" 10 | * 11 | * Assembly language routines for the mrmuldv module will 12 | * probably speed things up, so in most cases the generic 13 | * mirdef.h32 file is appropriate 14 | * 15 | * NOT recommended for Linux on PCs - read linux.txt 16 | * 17 | * NOTE:- Read comments in miracl.mak unix make file 18 | */ 19 | 20 | 21 | #define MIRACL 32 22 | #define MR_BIG_ENDIAN /* This may need to be changed */ 23 | #define mr_utype int 24 | #define mr_unsign32 unsigned int 25 | #define mr_dltype long long 26 | #define mr_unsign64 unsigned long long 27 | #define MR_IBITS 32 28 | #define MR_LBITS 32 29 | #define MR_NOASM 30 | #define MR_FLASH 52 31 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 32 | 33 | 34 | -------------------------------------------------------------------------------- /source/include/mirdef.gen: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 32 7 | #define mr_utype int 8 | #define MR_IBITS 32 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned int 11 | #define mr_dltype __int64 12 | #define mr_unsign64 unsigned __int64 13 | #define MR_GENERIC_MT 14 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 15 | #define MR_COMBA 16 16 | #define MR_BITSINCHAR 8 17 | 18 | -------------------------------------------------------------------------------- /source/include/mirdef.gfp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 32 7 | #define mr_utype int 8 | #define MR_IBITS 32 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned int 11 | #define mr_dltype __int64 12 | #define mr_unsign64 unsigned __int64 13 | #define MR_STATIC 16 14 | #define MR_ALWAYS_BINARY 15 | #define MR_STRIPPED_DOWN 16 | #define MR_GENERIC_MT 17 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 18 | #define MR_NOASM 19 | #define MR_BITSINCHAR 8 20 | 21 | -------------------------------------------------------------------------------- /source/include/mirdef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 64 7 | #define mr_utype long 8 | #define mr_unsign64 unsigned long 9 | #define MR_IBITS 32 10 | #define MR_LBITS 64 11 | #define mr_unsign32 unsigned int 12 | #define MR_FLASH 52 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_BITSINCHAR 8 15 | -------------------------------------------------------------------------------- /source/include/mirdef.h16: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 16-bit computers 4 | * e.g. old IBM PCs. 5 | */ 6 | 7 | #define MIRACL 16 8 | 9 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 10 | #define mr_utype int 11 | /* the underlying type is usually int * 12 | * but see mrmuldv.any */ 13 | #define mr_dltype long 14 | /* double length type */ 15 | #define mr_unsign32 unsigned long 16 | /* 32 bit unsigned type */ 17 | #define MR_IBITS 16 /* bits in int */ 18 | #define MR_LBITS 32 /* bits in long */ 19 | #define MR_FLASH 52 20 | /* delete this definition if integer * 21 | * only version of MIRACL required */ 22 | /* Number of bits per double mantissa */ 23 | 24 | /* #define MR_NOASM * define this if using C code ONLY * 25 | * mr_dltype must be defined */ 26 | 27 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 28 | 29 | -------------------------------------------------------------------------------- /source/include/mirdef.h32: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 32-bit computers 4 | * e.g. 80386+ PC, VAX, ARM etc. Assembly language versions of muldiv, 5 | * muldvm, muldvd and muldvd2 will be necessary. See mrmuldv.any 6 | * 7 | * Also suitable for DJGPP GNU C Compiler 8 | * ... but change __int64 to long long 9 | */ 10 | 11 | #define MIRACL 32 12 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 13 | #define mr_utype int 14 | /* the underlying type is usually int * 15 | * but see mrmuldv.any */ 16 | #define mr_unsign32 unsigned int 17 | /* 32 bit unsigned type */ 18 | #define MR_IBITS 32 /* bits in int */ 19 | #define MR_LBITS 32 /* bits in long */ 20 | #define MR_FLASH 52 21 | /* delete this definition if integer * 22 | * only version of MIRACL required */ 23 | /* Number of bits per double mantissa */ 24 | 25 | #define mr_dltype __int64 /* ... or long long for Unix/Linux */ 26 | #define mr_unsign64 unsigned __int64 27 | 28 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 29 | 30 | 31 | -------------------------------------------------------------------------------- /source/include/mirdef.h64: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 64 7 | #define mr_utype long 8 | #define mr_unsign64 unsigned long 9 | #define MR_IBITS 32 10 | #define MR_LBITS 64 11 | #define mr_unsign32 unsigned int 12 | #define MR_FLASH 52 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_BITSINCHAR 8 15 | -------------------------------------------------------------------------------- /source/include/mirdef.haf: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use some 32-bit compilers 4 | * which don't have a 64-bit type and for which assembly language versions 5 | * of muldiv, muldvd, muldvm and muldvd2 are not available. 6 | * See mrmuldv.any for details. 7 | * This mode of operation is not recommended. 8 | * 9 | */ 10 | 11 | #define MIRACL 16 12 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 13 | 14 | #define mr_utype short 15 | /* the underlying type is usually int * 16 | * but see mrmuldv.any */ 17 | #define mr_dltype long 18 | /* double length type */ 19 | #define mr_unsign32 unsigned int 20 | /* 32 bit unsigned type */ 21 | #define MR_IBITS 32 /* bits in int */ 22 | #define MR_LBITS 32 /* bits in long */ 23 | #define MR_FLASH 52 24 | /* delete this definition if integer * 25 | * only version of MIRACL required */ 26 | /* Number of bits per double mantissa */ 27 | 28 | #define MR_NOASM /* define this if using C code only * 29 | * mr_dltype must be defined */ 30 | 31 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 32 | 33 | -------------------------------------------------------------------------------- /source/include/mirdef.hio: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 32-bit computers 4 | * e.g. 80386+ PC, VAX, ARM etc. Assembly language versions of muldiv, 5 | * muldvm, muldvd and muldvd2 will be necessary. See mrmuldv.any 6 | * 7 | * NOTE: This is for Integer-Only builds of the MIRACL library 8 | * This will be slightly faster if flash arithmetic is not needed. 9 | * 10 | * Also suitable for DJGPP GNU C Compiler 11 | * ... but change __int64 to long long 12 | */ 13 | 14 | #define MIRACL 32 15 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 16 | #define mr_utype int 17 | /* the underlying type is usually int * 18 | * but see mrmuldv.any */ 19 | #define mr_unsign32 unsigned int 20 | /* 32 bit unsigned type */ 21 | #define MR_IBITS 32 /* Bits in int */ 22 | #define MR_LBITS 32 /* Bits in long */ 23 | 24 | #define mr_dltype __int64 /* ... or long long */ 25 | #define mr_unsign64 unsigned __int64 26 | 27 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 28 | 29 | -------------------------------------------------------------------------------- /source/include/mirdef.hpc: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with computer such as IBM PC in which 4 | * 32-bit longs can be manipulated directly, typically if the PC is 5 | * 80386 based. 6 | * 7 | * Suitable assembly language versions of muldiv, muldvm, muldvd and muldvd2 8 | * will be necessary. See mrmuldv.any for details 9 | */ 10 | 11 | #define MIRACL 32 12 | /* pseudo 32-bit working */ 13 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 14 | #define mr_utype long 15 | /* the underlying type is usually int * 16 | * but see mrmuldv.any */ 17 | #define mr_unsign32 unsigned long 18 | /* 32-bit unsigned type */ 19 | #define MR_IBITS 16 /* bits in int */ 20 | #define MR_LBITS 32 /* bits in long */ 21 | #define MR_FLASH 52 22 | /* delete this definition if integer * 23 | * only version of MIRACL required */ 24 | /* Number of bits per double mantissa */ 25 | 26 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 27 | 28 | -------------------------------------------------------------------------------- /source/include/mirdef.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * For C++ build of library 4 | */ 5 | 6 | #define MR_LITTLE_ENDIAN 7 | #define MIRACL 64 8 | #define mr_utype long 9 | #define mr_unsign64 unsigned long 10 | #define MR_IBITS 32 11 | #define MR_LBITS 64 12 | #define mr_unsign32 unsigned int 13 | #define MR_FLASH 52 14 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 15 | #define MR_BITSINCHAR 8 16 | #define MR_CPP 17 | -------------------------------------------------------------------------------- /source/include/mirdef.ibe: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 32 7 | #define mr_utype int 8 | #define MR_IBITS 32 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned int 11 | #define mr_dltype __int64 12 | #define mr_unsign64 unsigned __int64 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_GENERIC_MT 15 | #define MR_COMBA 16 16 | #define MR_BITSINCHAR 8 17 | 18 | -------------------------------------------------------------------------------- /source/include/mirdef.kep: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 8 7 | #define mr_utype char 8 | #define MR_IBITS 16 /**/ 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned long 11 | #define mr_dltype int /**/ 12 | #define mr_qltype long 13 | #define MR_STATIC 128 14 | #define MR_ALWAYS_BINARY 15 | #define MR_NOASM 16 | #define MR_STRIPPED_DOWN 17 | #define MR_GENERIC_MT 18 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 19 | #define MR_KCM 16 20 | #define MR_BITSINCHAR 8 21 | #define MR_NO_STANDARD_IO 22 | #define MR_NO_FILE_IO 23 | 24 | -------------------------------------------------------------------------------- /source/include/mirdef.lnx: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 32-bit computers 4 | * e.g. 80386+ PC, VAX, ARM etc. Assembly language versions of muldiv, 5 | * muldvm, muldvd and muldvd2 will be necessary. See mrmuldv.any 6 | * 7 | * Suitable for Unix/Linux and for DJGPP GNU C Compiler 8 | */ 9 | 10 | #define MIRACL 32 11 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 12 | #define mr_utype int 13 | /* the underlying type is usually int * 14 | * but see mrmuldv.any */ 15 | #define mr_unsign32 unsigned int 16 | /* 32 bit unsigned type */ 17 | #define MR_IBITS 32 /* bits in int */ 18 | #define MR_LBITS 32 /* bits in long */ 19 | #define MR_FLASH 52 20 | /* delete this definition if integer * 21 | * only version of MIRACL required */ 22 | /* Number of bits per double mantissa */ 23 | 24 | #define mr_dltype long long /* ... or __int64 for Windows */ 25 | #define mr_unsign64 unsigned long long 26 | 27 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 28 | 29 | 30 | -------------------------------------------------------------------------------- /source/include/mirdef.mgw: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 64 7 | #define mr_utype long long 8 | #define mr_unsign64 unsigned long long 9 | #define MR_IBITS 32 10 | #define MR_LBITS 32 11 | #define mr_unsign32 unsigned int 12 | #define MR_FLASH 52 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_BITSINCHAR 8 15 | 16 | -------------------------------------------------------------------------------- /source/include/mirdef.mik: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | 6 | #define MIRACL 32 7 | #define MR_LITTLE_ENDIAN 8 | #define mr_utype int 9 | #define MR_IBITS 32 10 | #define MR_LBITS 32 11 | #define mr_dltype long long 12 | #define mr_unsign32 unsigned int 13 | #define mr_unsign64 unsigned long long 14 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 15 | #define MR_COMBA 6 16 | #define MR_STATIC 6 17 | #define MR_ALWAYS_BINARY 18 | #define MR_STRIPPED_DOWN 19 | #define MR_GENERIC_MT 20 | 21 | -------------------------------------------------------------------------------- /source/include/mirdef.mip: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * Mips Version 4 | */ 5 | 6 | #define MIRACL 32 7 | #define MR_BIG_ENDIAN /* This may need to be changed */ 8 | #define mr_utype int 9 | #define mr_unsign32 unsigned int 10 | #define MR_IBITS 32 11 | #define MR_LBITS 32 12 | #define mr_dltype long long 13 | #define mr_unsign64 unsigned long long 14 | #define MR_ALWAYS_BINARY 15 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 16 | #define MR_COMBA 16 17 | #define MR_BITSINCHAR 8 18 | -------------------------------------------------------------------------------- /source/include/mirdef.mmm: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 32-bit computers 4 | * e.g. 80386+ PC, VAX, ARM etc. Assembly language versions of muldiv, 5 | * muldvm, muldvd and muldvd2 will be necessary. See mrmuldv.any 6 | * 7 | * Also suitable for DJGPP GNU C Compiler 8 | * ... but change __int64 to long long 9 | */ 10 | 11 | #define MIRACL 32 12 | #define MR_BIG_ENDIAN /* This may need to be changed */ 13 | #define mr_utype int 14 | /* the underlying type is usually int * 15 | * but see mrmuldv.any */ 16 | #define mr_unsign32 unsigned int 17 | /* 32 bit unsigned type */ 18 | #define MR_IBITS 32 /* bits in int */ 19 | #define MR_LBITS 32 /* bits in long */ 20 | #define MR_ALWAYS_BINARY 21 | /* delete this definition if integer * 22 | * only version of MIRACL required */ 23 | /* Number of bits per double mantissa */ 24 | 25 | #define mr_dltype long long /* ... or long long for Unix/Linux */ 26 | #define mr_unsign64 unsigned long long 27 | #define MR_KCM 16 28 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 29 | 30 | 31 | -------------------------------------------------------------------------------- /source/include/mirdef.ol: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 32 7 | #define mr_utype int 8 | #define MR_IBITS 32 9 | #define MR_LBITS 32 10 | #define mr_unsign32 unsigned int 11 | #define MR_STRIPPED_DOWN 12 | #define mr_dltype __int64 13 | #define mr_unsign64 unsigned __int64 14 | #define MR_GENERIC_MT 15 | #define MR_NOASM 16 | #define MR_COMBA 16 17 | #define MR_BITSINCHAR 8 18 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 19 | 20 | -------------------------------------------------------------------------------- /source/include/mirdef.pic: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 32-bit computers 4 | * e.g. 80386+ PC, VAX, ARM etc. Assembly language versions of muldiv, 5 | * muldvm, muldvd and muldvd2 will be necessary. See mrmuldv.any 6 | * 7 | * Also suitable for DJGPP GNU C Compiler 8 | * ... but change __int64 to long long 9 | */ 10 | 11 | #define MIRACL 32 12 | #define MR_LITTLE_ENDIAN /* This may need to be changed */ 13 | #define mr_utype int 14 | /* the underlying type is usually int * 15 | * but see mrmuldv.any */ 16 | #define mr_unsign32 unsigned int 17 | /* 32 bit unsigned type */ 18 | #define MR_IBITS 32 /* bits in int */ 19 | #define MR_LBITS 32 /* bits in long */ 20 | 21 | #define mr_dltype long long /* ... or long long for Unix/Linux */ 22 | #define mr_unsign64 unsigned long long 23 | #define MR_NOASM 24 | 25 | 26 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 27 | 28 | 29 | -------------------------------------------------------------------------------- /source/include/mirdef.ppc: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_BIG_ENDIAN 6 | #define MIRACL 64 7 | #define mr_utype long 8 | #define MR_IBITS 32 9 | #define MR_LBITS 64 10 | #define mr_unsign32 unsigned int 11 | #define mr_unsign64 unsigned long 12 | #define MR_ALWAYS_BINARY 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_BITSINCHAR 8 15 | #define MR_COMBA 8 16 | -------------------------------------------------------------------------------- /source/include/mirdef.scr: -------------------------------------------------------------------------------- 1 | #define MIRACL 32 2 | #define MR_LITTLE_ENDIAN 3 | #define mr_utype int 4 | #define MR_IBITS 32 5 | #define MR_LBITS 32 6 | #define mr_unsign32 unsigned int 7 | #define mr_unsign64 unsigned __int64 8 | #define MR_STRIPPED_DOWN 9 | #define MR_GENERIC_MT 10 | #define MR_NO_STANDARD_IO 11 | #define MR_KCM 16 12 | #define MAXBASE ((mr_utype)1<<(MIRACL-1)) 13 | -------------------------------------------------------------------------------- /source/include/mirdef.sjc: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | * This version suitable for use with most 32-bit computers 4 | * e.g. 80386+ PC, VAX, ARM etc. Assembly language versions of muldiv, 5 | * muldvm, muldvd and muldvd2 will be necessary. See mrmuldv.any 6 | * 7 | * Also suitable for DJGPP GNU C Compiler 8 | * ... but change __int64 to long long 9 | */ 10 | 11 | #define MIRACL 32 12 | #define MR_BIG_ENDIAN /* This may need to be changed */ 13 | #define mr_utype int 14 | /* the underlying type is usually int * 15 | * but see mrmuldv.any */ 16 | #define mr_unsign32 unsigned int 17 | #define MR_NOASM 18 | /* 32 bit unsigned type */ 19 | #define MR_IBITS 32 /* bits in int */ 20 | #define MR_LBITS 32 /* bits in long */ 21 | #define MR_ALWAYS_BINARY 22 | /* delete this definition if integer * 23 | * only version of MIRACL required */ 24 | /* Number of bits per double mantissa */ 25 | 26 | #define mr_dltype long long /* ... or long long for Unix/Linux */ 27 | #define mr_unsign64 unsigned long long 28 | 29 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 30 | 31 | 32 | -------------------------------------------------------------------------------- /source/include/mirdef.tst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/source/include/mirdef.tst -------------------------------------------------------------------------------- /source/include/mirdef.w64: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 64 7 | #define mr_utype __int64 8 | #define mr_unsign64 unsigned __int64 9 | #define MR_IBITS 32 10 | #define MR_LBITS 32 11 | #define mr_unsign32 unsigned int 12 | #define MR_FLASH 52 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_BITSINCHAR 8 15 | 16 | -------------------------------------------------------------------------------- /source/include/mirdef.wpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIRACL compiler/hardware definitions - mirdef.h 3 | */ 4 | 5 | #define MR_LITTLE_ENDIAN 6 | #define MIRACL 64 7 | #define mr_utype __int64 8 | #define mr_unsign64 unsigned __int64 9 | #define MR_IBITS 32 10 | #define MR_LBITS 32 11 | #define mr_unsign32 unsigned int 12 | #define MR_FLASH 52 13 | #define MAXBASE ((mr_small)1<<(MIRACL-1)) 14 | #define MR_BITSINCHAR 8 15 | #define MR_CPP 16 | -------------------------------------------------------------------------------- /source/mersenne.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to calculate mersenne primes 3 | * using Lucas-Lehmer test - Knuth p.391 4 | * 5 | * Try this only in a 32-bit (or better!) environment 6 | * 7 | */ 8 | 9 | #include 10 | #include "miracl.h" 11 | #define LIMIT 100000 12 | 13 | int main() 14 | { /* calculate mersenne primes */ 15 | BOOL compo; 16 | big L,m,T; 17 | int i,k,q,p,r; 18 | miracl *mip=mirsys(5000,0); 19 | L=mirvar(0); 20 | m=mirvar(0); 21 | T=mirvar(0); 22 | gprime(LIMIT); 23 | for (k=1;;k++) 24 | { /* test only prime exponents */ 25 | q=mip->PRIMES[k]; 26 | if (q==0) break; 27 | expb2(q,m); 28 | decr(m,1,m); /* m=2^q-1 */ 29 | 30 | /* try to find a factor. Should perhaps keep trying over a bigger range... */ 31 | 32 | compo=FALSE; 33 | for(i=2;i<16*q;i+=2) 34 | { /* prime factors (if they exist) are always * 35 | * of the form i*q+1, and 1 or 7 mod 8 */ 36 | p=i*q+1; 37 | if ((p-1)%q!=0) break; /* check for overflow */ 38 | if (p%8!=1 && p%8!=7) continue; 39 | if (spmd(3,p-1,p)!=1) continue; /* check for prime p */ 40 | 41 | r=subdiv(m,p,T); 42 | if (r==0) 43 | { 44 | if (size(T)!=1) compo=TRUE; 45 | break; 46 | } 47 | } 48 | if (compo) 49 | { 50 | printf("2^%d-1 is NOT prime ; factor = %7d\n",q,p); 51 | continue; 52 | } 53 | 54 | convert(4,L); 55 | for(i=1;i<=q-2;i++) 56 | { /* Lucas-Lehmer test */ 57 | fft_mult(L,L,L); 58 | decr(L,2,L); 59 | 60 | sftbit(L,-q,T); 61 | add(L,T,L); 62 | sftbit(T,q,T); 63 | subtract(L,T,L); 64 | if (mr_compare(L,m)>=0) subtract(L,m,L); 65 | } 66 | if (size(L)==0) 67 | { /* mersenne prime found! */ 68 | printf("2^%d-1 is prime = \n",q); 69 | cotnum(m,stdout); 70 | } 71 | else printf("2^%d-1 is NOT prime\n",q); 72 | } 73 | return 0; 74 | } 75 | 76 | -------------------------------------------------------------------------------- /source/mralloc.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL memory allocation routines 37 | * mralloc.c 38 | * 39 | * MIRACL C Memory allocation/deallocation 40 | * Can be replaced with special user-defined routines 41 | * Default is to standard system routines 42 | * 43 | * NOTE: uses calloc() which initialises memory to Zero, so make sure 44 | * any substituted routine does the same! 45 | */ 46 | 47 | #include "miracl.h" 48 | #include 49 | 50 | #ifndef MR_STATIC 51 | 52 | miracl *mr_first_alloc() 53 | { 54 | return (miracl *)calloc(1,sizeof(miracl)); 55 | } 56 | 57 | void *mr_alloc(_MIPD_ int num,int size) 58 | { 59 | char *p; 60 | #ifdef MR_OS_THREADS 61 | miracl *mr_mip=get_mip(); 62 | #endif 63 | 64 | if (mr_mip==NULL) 65 | { 66 | p=(char *)calloc(num,size); 67 | return (void *)p; 68 | } 69 | 70 | if (mr_mip->ERNUM) return NULL; 71 | 72 | p=(char *)calloc(num,size); 73 | if (p==NULL) mr_berror(_MIPP_ MR_ERR_OUT_OF_MEMORY); 74 | return (void *)p; 75 | 76 | } 77 | 78 | void mr_free(void *addr) 79 | { 80 | if (addr==NULL) return; 81 | free(addr); 82 | return; 83 | } 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /source/mrbits.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL bit manipulation routines 37 | * mrbits.c 38 | */ 39 | 40 | #include 41 | #include "miracl.h" 42 | 43 | #ifdef MR_FP 44 | #include 45 | #endif 46 | 47 | int logb2(_MIPD_ big x) 48 | { /* returns number of bits in x */ 49 | int xl,lg2; 50 | mr_small top; 51 | #ifdef MR_OS_THREADS 52 | miracl *mr_mip=get_mip(); 53 | #endif 54 | if (mr_mip->ERNUM || size(x)==0) return 0; 55 | 56 | MR_IN(49) 57 | 58 | 59 | #ifndef MR_ALWAYS_BINARY 60 | if (mr_mip->base==mr_mip->base2) 61 | { 62 | #endif 63 | xl=(int)(x->len&MR_OBITS); 64 | lg2=mr_mip->lg2b*(xl-1); 65 | top=x->w[xl-1]; 66 | while (top>=1) 67 | { 68 | lg2++; 69 | top/=2; 70 | } 71 | 72 | #ifndef MR_ALWAYS_BINARY 73 | } 74 | else 75 | { 76 | copy(x,mr_mip->w0); 77 | insign(PLUS,mr_mip->w0); 78 | lg2=0; 79 | while (mr_mip->w0->len>1) 80 | { 81 | #ifdef MR_FP_ROUNDING 82 | mr_sdiv(_MIPP_ mr_mip->w0,mr_mip->base2,mr_invert(mr_mip->base2),mr_mip->w0); 83 | #else 84 | mr_sdiv(_MIPP_ mr_mip->w0,mr_mip->base2,mr_mip->w0); 85 | #endif 86 | lg2+=mr_mip->lg2b; 87 | } 88 | 89 | while (mr_mip->w0->w[0]>=1) 90 | { 91 | lg2++; 92 | mr_mip->w0->w[0]/=2; 93 | } 94 | } 95 | #endif 96 | MR_OUT 97 | return lg2; 98 | } 99 | 100 | void sftbit(_MIPD_ big x,int n,big z) 101 | { /* shift x by n bits */ 102 | int m; 103 | mr_small sm; 104 | #ifdef MR_OS_THREADS 105 | miracl *mr_mip=get_mip(); 106 | #endif 107 | if (mr_mip->ERNUM) return; 108 | copy(x,z); 109 | if (n==0) return; 110 | 111 | MR_IN(47) 112 | 113 | m=mr_abs(n); 114 | sm=mr_shiftbits((mr_small)1,m%mr_mip->lg2b); 115 | if (n>0) 116 | { /* shift left */ 117 | 118 | #ifndef MR_ALWAYS_BINARY 119 | if (mr_mip->base==mr_mip->base2) 120 | { 121 | #endif 122 | mr_shift(_MIPP_ z,n/mr_mip->lg2b,z); 123 | mr_pmul(_MIPP_ z,sm,z); 124 | #ifndef MR_ALWAYS_BINARY 125 | } 126 | else 127 | { 128 | expb2(_MIPP_ m,mr_mip->w1); 129 | multiply(_MIPP_ z,mr_mip->w1,z); 130 | } 131 | #endif 132 | } 133 | else 134 | { /* shift right */ 135 | 136 | #ifndef MR_ALWAYS_BINARY 137 | if (mr_mip->base==mr_mip->base2) 138 | { 139 | #endif 140 | mr_shift(_MIPP_ z,n/mr_mip->lg2b,z); 141 | #ifdef MR_FP_ROUNDING 142 | mr_sdiv(_MIPP_ z,sm,mr_invert(sm),z); 143 | #else 144 | mr_sdiv(_MIPP_ z,sm,z); 145 | #endif 146 | 147 | #ifndef MR_ALWAYS_BINARY 148 | } 149 | else 150 | { 151 | expb2(_MIPP_ m,mr_mip->w1); 152 | divide(_MIPP_ z,mr_mip->w1,z); 153 | } 154 | #endif 155 | } 156 | MR_OUT 157 | } 158 | 159 | void expb2(_MIPD_ int n,big x) 160 | { /* sets x=2^n */ 161 | int r,p; 162 | #ifndef MR_ALWAYS_BINARY 163 | int i; 164 | #endif 165 | #ifdef MR_OS_THREADS 166 | miracl *mr_mip=get_mip(); 167 | #endif 168 | if (mr_mip->ERNUM) return; 169 | convert(_MIPP_ 1,x); 170 | if (n==0) return; 171 | 172 | MR_IN(149) 173 | 174 | if (n<0) 175 | { 176 | mr_berror(_MIPP_ MR_ERR_NEG_POWER); 177 | MR_OUT 178 | return; 179 | } 180 | r=n/mr_mip->lg2b; 181 | p=n%mr_mip->lg2b; 182 | 183 | #ifndef MR_ALWAYS_BINARY 184 | if (mr_mip->base==mr_mip->base2) 185 | { 186 | #endif 187 | mr_shift(_MIPP_ x,r,x); 188 | x->w[x->len-1]=mr_shiftbits(x->w[x->len-1],p); 189 | #ifndef MR_ALWAYS_BINARY 190 | } 191 | else 192 | { 193 | for (i=1;i<=r;i++) 194 | mr_pmul(_MIPP_ x,mr_mip->base2,x); 195 | mr_pmul(_MIPP_ x,mr_shiftbits((mr_small)1,p),x); 196 | } 197 | #endif 198 | MR_OUT 199 | } 200 | 201 | #ifndef MR_NO_RAND 202 | 203 | void bigbits(_MIPD_ int n,big x) 204 | { /* sets x as random < 2^n */ 205 | mr_small r; 206 | mr_lentype wlen; 207 | #ifdef MR_FP 208 | mr_small dres; 209 | #endif 210 | #ifdef MR_OS_THREADS 211 | miracl *mr_mip=get_mip(); 212 | #endif 213 | zero(x); 214 | if (mr_mip->ERNUM || n<=0) return; 215 | 216 | MR_IN(150) 217 | 218 | expb2(_MIPP_ n,mr_mip->w1); 219 | wlen=mr_mip->w1->len; 220 | do 221 | { 222 | r=brand(_MIPPO_ ); 223 | if (mr_mip->base==0) x->w[x->len++]=r; 224 | else x->w[x->len++]=MR_REMAIN(r,mr_mip->base); 225 | } while (x->lenbase==mr_mip->base2) 228 | { 229 | #endif 230 | 231 | x->w[wlen-1]=MR_REMAIN(x->w[wlen-1],mr_mip->w1->w[wlen-1]); 232 | mr_lzero(x); 233 | 234 | #ifndef MR_ALWAYS_BINARY 235 | } 236 | else 237 | { 238 | divide(_MIPP_ x,mr_mip->w1,mr_mip->w1); 239 | } 240 | #endif 241 | 242 | MR_OUT 243 | } 244 | 245 | #endif 246 | -------------------------------------------------------------------------------- /source/mrbrick.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * Module to implement Comb method for fast 37 | * computation of g^x mod n, for fixed g and n, using precomputation. 38 | * This idea can be used to substantially speed up certain phases 39 | * of the Digital Signature Standard (DSS) for example. 40 | * 41 | * See "Handbook of Applied Cryptography", CRC Press, 2001 42 | */ 43 | 44 | #include 45 | #include "miracl.h" 46 | 47 | #ifndef MR_STATIC 48 | 49 | BOOL brick_init(_MIPD_ brick *b,big g,big n,int window,int nb) 50 | { /* Uses Montgomery arithmetic internally * 51 | * g is the fixed base for exponentiation * 52 | * n is the fixed modulus * 53 | * nb is the maximum number of bits in the exponent */ 54 | 55 | int i,j,k,t,bp,len,bptr,is; 56 | big *table; 57 | 58 | #ifdef MR_OS_THREADS 59 | miracl *mr_mip=get_mip(); 60 | #endif 61 | if (nb<2 || window<1 || window>nb || mr_mip->ERNUM) return FALSE; 62 | t=MR_ROUNDUP(nb,window); 63 | if (t<2) return FALSE; 64 | 65 | MR_IN(109) 66 | 67 | #ifndef MR_ALWAYS_BINARY 68 | if (mr_mip->base != mr_mip->base2) 69 | { 70 | mr_berror(_MIPP_ MR_ERR_NOT_SUPPORTED); 71 | MR_OUT 72 | return FALSE; 73 | } 74 | #endif 75 | 76 | b->window=window; 77 | b->max=nb; 78 | table=(big *)mr_alloc(_MIPP_ (1<n=mirvar(_MIPP_ 0); 87 | copy(n,b->n); 88 | prepare_monty(_MIPP_ n); 89 | nres(_MIPP_ g,mr_mip->w1); 90 | convert(_MIPP_ 1,mr_mip->w2); 91 | nres(_MIPP_ mr_mip->w2,mr_mip->w2); 92 | 93 | table[0]=mirvar(_MIPP_ 0); 94 | copy(mr_mip->w2,table[0]); 95 | table[1]=mirvar(_MIPP_ 0); 96 | copy(mr_mip->w1,table[1]); 97 | for (j=0;jw1,mr_mip->w1,mr_mip->w1); 99 | 100 | k=1; 101 | for (i=2;i<(1<w1,table[i]); 108 | 109 | for (j=0;jw1,mr_mip->w1,mr_mip->w1); 111 | continue; 112 | } 113 | bp=1; 114 | copy(mr_mip->w2,table[i]); 115 | for (j=0;jlen; 129 | bptr=0; 130 | b->table=(mr_small *)mr_alloc(_MIPP_ len*(1<table[bptr++]=table[i]->w[j]; 137 | } 138 | mirkill(table[i]); 139 | } 140 | 141 | mr_free(table); 142 | 143 | MR_OUT 144 | return TRUE; 145 | } 146 | 147 | void brick_end(brick *b) 148 | { 149 | mirkill(b->n); 150 | mr_free(b->table); 151 | } 152 | 153 | #else 154 | 155 | /* use precomputated table in ROM - see ebrick2.c for example of how to create such a table, and ecdh.c 156 | for an example of use */ 157 | 158 | void brick_init(brick *b,const mr_small *table,big n,int window,int nb) 159 | { 160 | b->table=table; 161 | b->n=n; 162 | b->window=window; 163 | b->max=nb; 164 | } 165 | 166 | #endif 167 | 168 | void pow_brick(_MIPD_ brick *b,big e,big w) 169 | { 170 | int i,j,t,len,promptr,maxsize; 171 | 172 | #ifdef MR_OS_THREADS 173 | miracl *mr_mip=get_mip(); 174 | #endif 175 | 176 | if (size(e)<0) mr_berror(_MIPP_ MR_ERR_NEG_POWER); 177 | t=MR_ROUNDUP(b->max,b->window); 178 | 179 | MR_IN(110) 180 | 181 | #ifndef MR_ALWAYS_BINARY 182 | if (mr_mip->base != mr_mip->base2) 183 | { 184 | mr_berror(_MIPP_ MR_ERR_NOT_SUPPORTED); 185 | MR_OUT 186 | return; 187 | } 188 | #endif 189 | 190 | if (logb2(_MIPP_ e) > b->max) 191 | { 192 | mr_berror(_MIPP_ MR_ERR_EXP_TOO_BIG); 193 | MR_OUT 194 | return; 195 | } 196 | 197 | prepare_monty(_MIPP_ b->n); 198 | j=recode(_MIPP_ e,t,b->window,t-1); 199 | 200 | len=b->n->len; 201 | maxsize=(1<window)*len; 202 | 203 | promptr=j*len; 204 | init_big_from_rom(mr_mip->w1,len,b->table,maxsize,&promptr); 205 | 206 | for (i=t-2;i>=0;i--) 207 | { 208 | j=recode(_MIPP_ e,t,b->window,i); 209 | nres_modmult(_MIPP_ mr_mip->w1,mr_mip->w1,mr_mip->w1); 210 | if (j>0) 211 | { 212 | promptr=j*len; 213 | init_big_from_rom(mr_mip->w2,len,b->table,maxsize,&promptr); 214 | nres_modmult(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w1); 215 | } 216 | } 217 | redc(_MIPP_ mr_mip->w1,w); 218 | MR_OUT 219 | } 220 | -------------------------------------------------------------------------------- /source/mrbuild.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL flash number builder: uses generator of 37 | * regular continued fraction expansion to create 38 | * a flash number, rounded if necessary. 39 | * mrbuild.c 40 | */ 41 | 42 | #include "miracl.h" 43 | 44 | #ifdef MR_FLASH 45 | 46 | void build(_MIPD_ flash x,int (*gen)(_MIPT_ big,int)) 47 | { /* Build x from its regular c.f. * 48 | * generated by gen() */ 49 | mr_small ex1,ex2,ex,st,sr; 50 | int a,b,c,d,rm,q,n,prc,lw2,lw4,lz; 51 | BOOL finoff,last; 52 | big t; 53 | #ifdef MR_OS_THREADS 54 | miracl *mr_mip=get_mip(); 55 | #endif 56 | if (mr_mip->ERNUM) return; 57 | 58 | MR_IN(48) 59 | 60 | zero(mr_mip->w1); 61 | convert(_MIPP_ 1,mr_mip->w2); 62 | convert(_MIPP_ 1,mr_mip->w3); 63 | zero(mr_mip->w4); 64 | finoff=FALSE; 65 | last=FALSE; 66 | n=0; 67 | q=(*gen)(_MIPP_ x,n); /* Note - first quotient may be zero */ 68 | ex=mr_mip->base-1; 69 | if (mr_mip->nib==mr_mip->workprec) prc=mr_mip->nib; 70 | else prc=mr_mip->workprec+1; 71 | while (!mr_mip->ERNUM && q>=0) 72 | { 73 | if (q==MR_TOOBIG || n==0 || finoff) 74 | { 75 | if (q!=MR_TOOBIG) convert(_MIPP_ q,x); 76 | else last=FALSE; 77 | mr_mip->check=OFF; 78 | multiply(_MIPP_ mr_mip->w2,x,mr_mip->w0); 79 | subtract(_MIPP_ mr_mip->w1,mr_mip->w0,mr_mip->w7); 80 | mr_mip->check=ON; 81 | if ((int)(mr_mip->w7->len&MR_OBITS)>mr_mip->nib) break; 82 | copy(mr_mip->w7,mr_mip->w1); 83 | t=mr_mip->w1,mr_mip->w1=mr_mip->w2,mr_mip->w2=t; /* swap(w1,w2) */ 84 | mr_mip->check=OFF; 85 | multiply(_MIPP_ mr_mip->w4,x,mr_mip->w0); 86 | subtract(_MIPP_ mr_mip->w3,mr_mip->w0,mr_mip->w7); 87 | mr_mip->check=ON; 88 | if ((int)(mr_mip->w7->len&MR_OBITS)>mr_mip->nib) 89 | { /* oops! */ 90 | fpack(_MIPP_ mr_mip->w1,mr_mip->w4,x); 91 | negify(x,x); 92 | mr_mip->EXACT=FALSE; 93 | MR_OUT 94 | return; 95 | } 96 | copy(mr_mip->w7,mr_mip->w3); 97 | t=mr_mip->w3,mr_mip->w3=mr_mip->w4,mr_mip->w4=t; /* swap(w3,w4) */ 98 | n++; 99 | } 100 | lw2=(int)(mr_mip->w2->len&MR_OBITS); 101 | lw4=(int)(mr_mip->w4->len&MR_OBITS); 102 | lz=lw2+lw4; 103 | if (lz > prc) break; /* too big - exit */ 104 | if (last) 105 | { 106 | if (finoff) break; 107 | finoff=TRUE; 108 | q=(*gen)(_MIPP_ x,n); 109 | continue; 110 | } 111 | if (lz>=prc-1) 112 | { /* nearly finished - so be careful not to overshoot */ 113 | if (mr_mip->base==0) 114 | { 115 | #ifndef MR_NOFULLWIDTH 116 | st=mr_mip->w2->w[lw2-1]+1; 117 | if (st==0) ex1=1; 118 | else ex1=muldvm((mr_small)1,(mr_small)0,st,&sr); 119 | st=mr_mip->w4->w[lw4-1]+1; 120 | if (st==0) ex2=1; 121 | else ex2=muldvm((mr_small)1,(mr_small)0,st,&sr); 122 | #endif 123 | } 124 | else 125 | { 126 | ex1=mr_mip->base/(mr_mip->w2->w[lw2-1]+1); 127 | ex2=mr_mip->base/(mr_mip->w4->w[lw4-1]+1); 128 | } 129 | if (ex2>ex1) ex=ex1,ex1=ex2,ex2=ex; 130 | if (lz==prc) ex=ex2; 131 | else ex=ex1; 132 | last=TRUE; 133 | } 134 | a=1; 135 | b=0; 136 | c=0; 137 | d=1; 138 | forever 139 | { 140 | q=(*gen)(_MIPP_ x,n); 141 | if (q<0 || q>=MR_TOOBIG/mr_abs(d)) 142 | { /* there could be more.... *** V3.21 mod *** */ 143 | last=FALSE; 144 | break; 145 | } 146 | rm=b-q*d; 147 | b=d; 148 | d=rm; 149 | rm=a-q*c; 150 | a=c; 151 | c=rm; 152 | n++; 153 | if ((mr_small)(mr_abs(c-d))>ex) break; 154 | } 155 | premult(_MIPP_ mr_mip->w1,c,mr_mip->w7); 156 | premult(_MIPP_ mr_mip->w1,a,mr_mip->w1); 157 | premult(_MIPP_ mr_mip->w2,b,mr_mip->w0); 158 | premult(_MIPP_ mr_mip->w2,d,mr_mip->w2); 159 | add(_MIPP_ mr_mip->w1,mr_mip->w0,mr_mip->w1); 160 | add(_MIPP_ mr_mip->w2,mr_mip->w7,mr_mip->w2); 161 | premult(_MIPP_ mr_mip->w3,c,mr_mip->w7); 162 | premult(_MIPP_ mr_mip->w3,a,mr_mip->w3); 163 | premult(_MIPP_ mr_mip->w4,b,mr_mip->w0); 164 | premult(_MIPP_ mr_mip->w4,d,mr_mip->w4); 165 | add(_MIPP_ mr_mip->w3,mr_mip->w0,mr_mip->w3); 166 | add(_MIPP_ mr_mip->w4,mr_mip->w7,mr_mip->w4); 167 | } 168 | if (fit(mr_mip->w2,mr_mip->w4,mr_mip->nib)) fpack(_MIPP_ mr_mip->w2,mr_mip->w4,x); 169 | else fpack(_MIPP_ mr_mip->w1,mr_mip->w3,x); 170 | negify (x,x); 171 | if (q!=(-1)) mr_mip->EXACT=FALSE; 172 | MR_OUT 173 | } 174 | 175 | #endif 176 | 177 | -------------------------------------------------------------------------------- /source/mrcrt.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL Chinese Remainder Thereom routines (for use with big moduli) 37 | * mrcrt.c 38 | */ 39 | 40 | #include 41 | #include "miracl.h" 42 | 43 | #ifndef MR_STATIC 44 | 45 | BOOL crt_init(_MIPD_ big_chinese *c,int r,big *moduli) 46 | { /* calculate CRT constants */ 47 | int i,j,k; 48 | #ifdef MR_OS_THREADS 49 | miracl *mr_mip=get_mip(); 50 | #endif 51 | if (r<2 || mr_mip->ERNUM) return FALSE; 52 | for (i=0;iM=(big *)mr_alloc(_MIPP_ r,sizeof(big)); 57 | if (c->M==NULL) 58 | { 59 | mr_berror(_MIPP_ MR_ERR_OUT_OF_MEMORY); 60 | MR_OUT 61 | return FALSE; 62 | } 63 | c->C=(big *)mr_alloc(_MIPP_ r*(r-1)/2,sizeof(big)); 64 | if (c->C==NULL) 65 | { 66 | mr_free(c->M); 67 | mr_berror(_MIPP_ MR_ERR_OUT_OF_MEMORY); 68 | MR_OUT 69 | return FALSE; 70 | } 71 | c->V=(big *)mr_alloc(_MIPP_ r,sizeof(big)); 72 | if (c->V==NULL) 73 | { 74 | mr_free(c->M); 75 | mr_free(c->C); 76 | mr_berror(_MIPP_ MR_ERR_OUT_OF_MEMORY); 77 | MR_OUT 78 | return FALSE; 79 | } 80 | for (k=0,i=0;iV[i]=mirvar(_MIPP_ 0); 83 | c->M[i]=mirvar(_MIPP_ 0); 84 | copy(moduli[i],c->M[i]); 85 | for (j=0;jC[k]=mirvar(_MIPP_ 0); 88 | invmodp(_MIPP_ c->M[j],c->M[i],c->C[k]); 89 | } 90 | } 91 | c->NP=r; 92 | MR_OUT 93 | return TRUE; 94 | } 95 | 96 | void crt_end(big_chinese *c) 97 | { /* clean up after CRT */ 98 | int i,j,k; 99 | if (c->NP<2) return; 100 | for (k=0,i=0;iNP;i++) 101 | { 102 | mirkill(c->M[i]); 103 | for (j=0;jC[k++]); 105 | mirkill(c->V[i]); 106 | } 107 | mr_free(c->M); 108 | mr_free(c->V); 109 | mr_free(c->C); 110 | c->NP=0; 111 | } 112 | 113 | #endif 114 | 115 | void crt(_MIPD_ big_chinese *c,big *u,big x) 116 | { /* Chinese Remainder Thereom * 117 | * Calculate x given remainders u[i] mod M[i] */ 118 | int i,j,k; 119 | #ifdef MR_OS_THREADS 120 | miracl *mr_mip=get_mip(); 121 | #endif 122 | if (c->NP<2 || mr_mip->ERNUM) return; 123 | 124 | MR_IN(74) 125 | 126 | copy(u[0],c->V[0]); 127 | for (k=0,i=1;iNP;i++) 128 | { /* Knuth page 274 */ 129 | subtract(_MIPP_ u[i],c->V[0],c->V[i]); 130 | mad(_MIPP_ c->V[i],c->C[k],c->C[k],c->M[i],c->M[i],c->V[i]); 131 | k++; 132 | for (j=1;jV[i],c->V[j],c->V[i]); 135 | mad(_MIPP_ c->V[i],c->C[k],c->C[k],c->M[i],c->M[i],c->V[i]); 136 | } 137 | if (size(c->V[i])<0) add(_MIPP_ c->V[i],c->M[i],c->V[i]); 138 | } 139 | zero(x); 140 | convert(_MIPP_ 1,mr_mip->w1); 141 | for (i=0;iNP;i++) 142 | { 143 | multiply(_MIPP_ mr_mip->w1,c->V[i],mr_mip->w2); 144 | add(_MIPP_ x,mr_mip->w2,x); 145 | multiply(_MIPP_ mr_mip->w1,c->M[i],mr_mip->w1); 146 | } 147 | MR_OUT 148 | } 149 | 150 | -------------------------------------------------------------------------------- /source/mrdouble.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL Double to Flash conversion routines - use with care 37 | * mrdouble.c 38 | * 39 | */ 40 | 41 | #include 42 | #include "miracl.h" 43 | 44 | #ifdef MR_FLASH 45 | 46 | #define sign(x) ((x)<0? (-1) : 1) 47 | 48 | static int dquot(_MIPD_ big x,int num) 49 | { /* generate c.f. for a double D */ 50 | int m; 51 | #ifdef MR_OS_THREADS 52 | miracl *mr_mip=get_mip(); 53 | #endif 54 | if (num==0) 55 | { 56 | mr_mip->oldn=(-1); 57 | if (mr_mip->base==0) mr_mip->db=pow(2.0,(double)MIRACL); 58 | else mr_mip->db=(double)mr_mip->base; 59 | if (mr_mip->D<1.0) 60 | { 61 | mr_mip->D=(1.0/mr_mip->D); 62 | return (mr_mip->q=0); 63 | } 64 | } 65 | else if (mr_mip->q<0 || num==mr_mip->oldn) return mr_mip->q; 66 | mr_mip->oldn=num; 67 | if (mr_mip->D==0.0) return (mr_mip->q=(-1)); 68 | mr_mip->D=modf(mr_mip->D,&mr_mip->n); /* n is whole number part */ 69 | m=0; /* D is fractional part (or guard digits!) */ 70 | zero(x); 71 | while (mr_mip->n>0.0) 72 | { /* convert n to big */ 73 | m++; 74 | if (m>mr_mip->nib) return (mr_mip->q=(-2)); 75 | mr_mip->p=mr_mip->n/mr_mip->db; 76 | modf(mr_mip->p,&mr_mip->p); 77 | x->w[m-1]=(mr_small)(mr_mip->n-mr_mip->db*mr_mip->p); 78 | mr_mip->n=mr_mip->p; 79 | } 80 | x->len=m; 81 | if (mr_mip->D>0.0) mr_mip->D=(1.0/mr_mip->D); 82 | return (mr_mip->q=size(x)); 83 | } 84 | 85 | void dconv(_MIPD_ double d,flash w) 86 | { /* convert double to rounded flash */ 87 | int s; 88 | #ifdef MR_OS_THREADS 89 | miracl *mr_mip=get_mip(); 90 | #endif 91 | if (mr_mip->ERNUM) return; 92 | 93 | MR_IN(32) 94 | 95 | zero(w); 96 | if (d==0.0) 97 | { 98 | MR_OUT 99 | return; 100 | } 101 | mr_mip->D=d; 102 | s=sign(mr_mip->D); 103 | mr_mip->D=mr_abs(mr_mip->D); 104 | build(_MIPP_ w,dquot); 105 | insign(s,w); 106 | 107 | MR_OUT 108 | } 109 | 110 | double fdsize(_MIPD_ flash w) 111 | { /* express flash number as double. */ 112 | int i,s,en,ed; 113 | double n,d,b,BIGGEST; 114 | #ifdef MR_OS_THREADS 115 | miracl *mr_mip=get_mip(); 116 | #endif 117 | if (mr_mip->ERNUM || size(w)==0) return (0.0); 118 | 119 | MR_IN(11) 120 | 121 | BIGGEST=pow(2.0,(double)(1<<(MR_EBITS-4))); 122 | mr_mip->EXACT=FALSE; 123 | n=0.0; 124 | d=0.0; 125 | if (mr_mip->base==0) b=pow(2.0,(double)MIRACL); 126 | else b=(double)mr_mip->base; 127 | numer(_MIPP_ w,mr_mip->w1); 128 | s=exsign(mr_mip->w1); 129 | insign(PLUS,mr_mip->w1); 130 | en=(int)mr_mip->w1->len; 131 | for (i=0;iw1->w[i]+(n/b); 133 | denom(_MIPP_ w,mr_mip->w1); 134 | ed=(int)mr_mip->w1->len; 135 | for (i=0;iw1->w[i]+(d/b); 137 | n/=d; 138 | while (en!=ed) 139 | { 140 | if (en>ed) 141 | { 142 | ed++; 143 | if (BIGGEST/b. * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL flash exponential and logs 37 | * mrflsh2.c 38 | */ 39 | 40 | #include 41 | #include "miracl.h" 42 | 43 | #ifdef MR_FLASH 44 | 45 | static int expon(_MIPD_ big w,int n) 46 | { /* generator for C.F. of e */ 47 | if (n==0) return 2; 48 | if (n%3==2) return 2*(n/3)+2; 49 | else return 1; 50 | } 51 | 52 | void fexp(_MIPD_ flash x,flash y) 53 | { /* calculates y=exp(x) */ 54 | int i,n,nsq,m,sqrn,op[5]; 55 | BOOL minus,rem; 56 | #ifdef MR_OS_THREADS 57 | miracl *mr_mip=get_mip(); 58 | #endif 59 | if (mr_mip->ERNUM) return; 60 | if (size(x)==0) 61 | { 62 | convert(_MIPP_ 1,y); 63 | return; 64 | } 65 | copy(x,y); 66 | 67 | MR_IN(54) 68 | 69 | minus=FALSE; 70 | if (size(y)<0) 71 | { 72 | minus=TRUE; 73 | negify(y,y); 74 | } 75 | ftrunc(_MIPP_ y,y,mr_mip->w9); 76 | n=size(y); 77 | if (n==MR_TOOBIG) 78 | { 79 | mr_berror(_MIPP_ MR_ERR_FLASH_OVERFLOW); 80 | MR_OUT 81 | return; 82 | } 83 | if (n==0) convert(_MIPP_ 1,y); 84 | else 85 | { 86 | build(_MIPP_ y,expon); 87 | if (minus) 88 | { /* underflow to zero - bit of a bodge */ 89 | rem=mr_mip->ERCON; 90 | mr_mip->ERCON=TRUE; 91 | fpower(_MIPP_ y,n,y); 92 | mr_mip->ERCON=rem; 93 | if (mr_mip->ERNUM) 94 | { 95 | mr_mip->ERNUM=0; 96 | zero(y); 97 | MR_OUT 98 | return; 99 | } 100 | } 101 | else fpower(_MIPP_ y,n,y); 102 | } 103 | if (size(mr_mip->w9)==0) 104 | { 105 | if (minus) frecip(_MIPP_ y,y); 106 | MR_OUT 107 | return; 108 | } 109 | sqrn=isqrt(mr_mip->lg2b*mr_mip->workprec,mr_mip->lg2b); 110 | nsq=0; 111 | copy(mr_mip->w9,mr_mip->w8); 112 | frecip(_MIPP_ mr_mip->w9,mr_mip->w9); 113 | ftrunc(_MIPP_ mr_mip->w9,mr_mip->w9,mr_mip->w9); 114 | m=logb2(_MIPP_ mr_mip->w9); 115 | if (mw9); 119 | fdiv(_MIPP_ mr_mip->w8,mr_mip->w9,mr_mip->w8); 120 | } 121 | zero(mr_mip->w10); 122 | op[0]=0x4B; /* set up for x/(C+y) */ 123 | op[1]=1; 124 | op[2]=0; 125 | for (m=sqrn;m>0;m--) 126 | { /* Unwind C.F. expansion for exp(x)-1 */ 127 | if (m%2==0) op[4]=2,op[3]=1; 128 | else op[4]=m,op[3]=(-1); 129 | flop(_MIPP_ mr_mip->w8,mr_mip->w10,op,mr_mip->w10); 130 | } 131 | op[0]=0x2C; /* set up for (x+2).y */ 132 | op[1]=op[3]=1; 133 | op[2]=2; 134 | op[4]=0; 135 | for (i=0;iw10,mr_mip->w10,op,mr_mip->w10); 138 | } 139 | op[2]=1; 140 | flop(_MIPP_ mr_mip->w10,y,op,y); 141 | if (minus) frecip(_MIPP_ y,y); 142 | MR_OUT 143 | } 144 | 145 | void flog(_MIPD_ flash x,flash y) 146 | { /* calculate y=log(x) to base e */ 147 | BOOL hack; 148 | int op[5]; 149 | #ifdef MR_OS_THREADS 150 | miracl *mr_mip=get_mip(); 151 | #endif 152 | copy(x,y); 153 | if (mr_mip->ERNUM) return; 154 | if (size(y)==1) 155 | { 156 | zero(y); 157 | return; 158 | } 159 | 160 | MR_IN(55) 161 | 162 | if (size(y)<=0) 163 | { 164 | mr_berror(_MIPP_ MR_ERR_NEG_LOG); 165 | MR_OUT 166 | return; 167 | } 168 | hack=FALSE; 169 | if (mr_lent(y)<=2) 170 | { /* for 'simple' values of y */ 171 | hack=TRUE; 172 | build(_MIPP_ mr_mip->w11,expon); 173 | fdiv(_MIPP_ y,mr_mip->w11,y); 174 | } 175 | op[0]=0x68; 176 | op[1]=op[3]=1; 177 | op[2]=(-1); 178 | op[4]=0; 179 | mr_mip->workprec=mr_mip->stprec; 180 | dconv(_MIPP_ log(fdsize(_MIPP_ y)),mr_mip->w11); 181 | while (mr_mip->workprec!=mr_mip->nib) 182 | { /* Newtons iteration w11=w11+(y-exp(w11))/exp(w11) */ 183 | if (mr_mip->workprecnib) mr_mip->workprec*=2; 184 | if (mr_mip->workprec>=mr_mip->nib) mr_mip->workprec=mr_mip->nib; 185 | else if (mr_mip->workprec*2>mr_mip->nib) mr_mip->workprec=(mr_mip->nib+1)/2; 186 | fexp(_MIPP_ mr_mip->w11,mr_mip->w12); 187 | flop(_MIPP_ y,mr_mip->w12,op,mr_mip->w12); 188 | fadd(_MIPP_ mr_mip->w12,mr_mip->w11,mr_mip->w11); 189 | } 190 | copy(mr_mip->w11,y); 191 | if (hack) fincr(_MIPP_ y,1,1,y); 192 | MR_OUT 193 | } 194 | 195 | void fpowf(_MIPD_ flash x,flash y,flash z) 196 | { /* raise flash number to flash power * 197 | * z=x^y -> z=exp(y.log(x)) */ 198 | int n; 199 | #ifdef MR_OS_THREADS 200 | miracl *mr_mip=get_mip(); 201 | #endif 202 | if (mr_mip->ERNUM) return; 203 | 204 | MR_IN(56) 205 | 206 | n=size(y); 207 | if (mr_abs(n)w13); 214 | n=size(mr_mip->w13); 215 | if (mr_abs(n)w13,z); 224 | fexp(_MIPP_ z,z); 225 | MR_OUT 226 | } 227 | 228 | #endif 229 | 230 | -------------------------------------------------------------------------------- /source/mrflsh4.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL flash hyperbolic trig. 37 | * mrflsh4.c 38 | */ 39 | 40 | #include "miracl.h" 41 | 42 | #ifdef MR_FLASH 43 | 44 | void ftanh(_MIPD_ flash x,flash y) 45 | { /* calculates y=tanh(x) */ 46 | int op[5]; 47 | #ifdef MR_OS_THREADS 48 | miracl *mr_mip=get_mip(); 49 | #endif 50 | copy(x,y); 51 | if (mr_mip->ERNUM || size(y)==0) return; 52 | 53 | MR_IN(63) 54 | fexp(_MIPP_ y,y); 55 | op[0]=0x33; 56 | op[1]=op[3]=op[4]=1; 57 | op[2]=(-1); 58 | flop(_MIPP_ y,y,op,y); 59 | MR_OUT 60 | } 61 | 62 | void fatanh(_MIPD_ flash x,flash y) 63 | { /* calculate y=atanh(x) */ 64 | int op[5]; 65 | #ifdef MR_OS_THREADS 66 | miracl *mr_mip=get_mip(); 67 | #endif 68 | copy(x,y); 69 | if (mr_mip->ERNUM || size(y)==0) return; 70 | 71 | MR_IN(64) 72 | fconv(_MIPP_ 1,1,mr_mip->w11); 73 | op[0]=0x66; 74 | op[1]=op[2]=op[3]=1; 75 | op[4]=(-1); 76 | flop(_MIPP_ mr_mip->w11,y,op,y); 77 | flog(_MIPP_ y,y); 78 | fpmul(_MIPP_ y,1,2,y); 79 | MR_OUT 80 | } 81 | 82 | void fsinh(_MIPD_ flash x,flash y) 83 | { /* calculate y=sinh(x) */ 84 | int op[5]; 85 | #ifdef MR_OS_THREADS 86 | miracl *mr_mip=get_mip(); 87 | #endif 88 | copy(x,y); 89 | if (mr_mip->ERNUM || size(y)==0) return; 90 | 91 | MR_IN(65) 92 | fexp(_MIPP_ y,y); 93 | op[0]=0xC6; 94 | op[2]=op[3]=op[4]=1; 95 | op[1]=(-1); 96 | flop(_MIPP_ y,y,op,y); 97 | MR_OUT 98 | } 99 | 100 | void fasinh(_MIPD_ flash x,flash y) 101 | { /* calculate y=asinh(x) */ 102 | #ifdef MR_OS_THREADS 103 | miracl *mr_mip=get_mip(); 104 | #endif 105 | copy(x,y); 106 | if (mr_mip->ERNUM || size(y)==0) return; 107 | 108 | MR_IN(66) 109 | fmul(_MIPP_ y,y,mr_mip->w11); 110 | fincr(_MIPP_ mr_mip->w11,1,1,mr_mip->w11); 111 | froot(_MIPP_ mr_mip->w11,2,mr_mip->w11); 112 | fadd(_MIPP_ y,mr_mip->w11,y); 113 | flog(_MIPP_ y,y); 114 | MR_OUT 115 | } 116 | 117 | void fcosh(_MIPD_ flash x,flash y) 118 | { /* calculate y=cosh(x) */ 119 | int op[5]; 120 | #ifdef MR_OS_THREADS 121 | miracl *mr_mip=get_mip(); 122 | #endif 123 | copy(x,y); 124 | if (mr_mip->ERNUM || size(y)==0) 125 | { 126 | convert(_MIPP_ 1,y); 127 | return; 128 | } 129 | 130 | MR_IN(67) 131 | fexp(_MIPP_ y,y); 132 | op[0]=0xC6; 133 | op[1]=op[2]=op[3]=op[4]=1; 134 | flop(_MIPP_ y,y,op,y); 135 | MR_OUT 136 | } 137 | 138 | void facosh(_MIPD_ flash x,flash y) 139 | { /* calculate y=acosh(x) */ 140 | #ifdef MR_OS_THREADS 141 | miracl *mr_mip=get_mip(); 142 | #endif 143 | copy(x,y); 144 | if (mr_mip->ERNUM) return; 145 | 146 | MR_IN(68) 147 | fmul(_MIPP_ y,y,mr_mip->w11); 148 | fincr(_MIPP_ mr_mip->w11,(-1),1,mr_mip->w11); 149 | froot(_MIPP_ mr_mip->w11,2,mr_mip->w11); 150 | fadd(_MIPP_ y,mr_mip->w11,y); 151 | flog(_MIPP_ y,y); 152 | MR_OUT 153 | } 154 | 155 | #endif 156 | 157 | -------------------------------------------------------------------------------- /source/mrfrnd.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL flash random number routine 37 | * mrfrnd.c 38 | */ 39 | 40 | #include "miracl.h" 41 | 42 | #ifdef MR_FP 43 | #include 44 | #endif 45 | 46 | #ifdef MR_FLASH 47 | 48 | #ifndef MR_NO_RAND 49 | 50 | void frand(_MIPD_ flash x) 51 | { /* generates random flash number 0ERNUM) return; 60 | 61 | MR_IN(46) 62 | 63 | zero(mr_mip->w6); 64 | mr_mip->w6->len=mr_mip->nib; 65 | for (i=0;inib;i++) 66 | { /* generate a full width random number */ 67 | if (mr_mip->base==0) mr_mip->w6->w[i]=brand(_MIPPO_ ); 68 | else mr_mip->w6->w[i]=MR_REMAIN(brand(_MIPPO_ ),mr_mip->base); 69 | } 70 | mr_mip->check=OFF; 71 | bigrand(_MIPP_ mr_mip->w6,mr_mip->w5); 72 | mr_mip->check=ON; 73 | mround(_MIPP_ mr_mip->w5,mr_mip->w6,x); 74 | 75 | MR_OUT 76 | } 77 | 78 | #endif 79 | 80 | #endif 81 | 82 | -------------------------------------------------------------------------------- /source/mrgcd.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL Greatest Common Divisor module. 37 | * mrgcd.c 38 | */ 39 | 40 | #include "miracl.h" 41 | 42 | #ifdef MR_FP 43 | #include 44 | #endif 45 | 46 | int egcd(_MIPD_ big x,big y,big z) 47 | { /* greatest common divisor z=gcd(x,y) by Euclids * 48 | * method using Lehmers algorithm for big numbers */ 49 | int q,r,a,b,c,d,n; 50 | mr_small sr,m,sm; 51 | mr_small u,v,lq,lr; 52 | #ifdef MR_FP 53 | mr_small dres; 54 | #endif 55 | big t; 56 | #ifdef MR_OS_THREADS 57 | miracl *mr_mip=get_mip(); 58 | #endif 59 | if (mr_mip->ERNUM) return 0; 60 | 61 | MR_IN(12) 62 | 63 | copy(x,mr_mip->w1); 64 | copy(y,mr_mip->w2); 65 | insign(PLUS,mr_mip->w1); 66 | insign(PLUS,mr_mip->w2); 67 | a=b=c=d=0; 68 | while (size(mr_mip->w2)!=0) 69 | { 70 | /* printf("a= %d b= %d c= %d d=%d\n",a,b,c,d); */ 71 | if (b==0) 72 | { /* update w1 and w2 */ 73 | divide(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w2); 74 | t=mr_mip->w1,mr_mip->w1=mr_mip->w2,mr_mip->w2=t; /* swap(w1,w2) */ 75 | } 76 | else 77 | { 78 | premult(_MIPP_ mr_mip->w1,c,z); 79 | premult(_MIPP_ mr_mip->w1,a,mr_mip->w1); 80 | premult(_MIPP_ mr_mip->w2,b,mr_mip->w0); 81 | premult(_MIPP_ mr_mip->w2,d,mr_mip->w2); 82 | add(_MIPP_ mr_mip->w1,mr_mip->w0,mr_mip->w1); 83 | add(_MIPP_ mr_mip->w2,z,mr_mip->w2); 84 | } 85 | if (mr_mip->ERNUM || size(mr_mip->w2)==0) break; 86 | n=(int)mr_mip->w1->len; 87 | if (mr_mip->w2->len==1) 88 | { /* special case if mr_mip->w2 is now small */ 89 | sm=mr_mip->w2->w[0]; 90 | #ifdef MR_FP_ROUNDING 91 | sr=mr_sdiv(_MIPP_ mr_mip->w1,sm,mr_invert(sm),mr_mip->w1); 92 | #else 93 | sr=mr_sdiv(_MIPP_ mr_mip->w1,sm,mr_mip->w1); 94 | #endif 95 | if (sr==0) 96 | { 97 | copy(mr_mip->w2,mr_mip->w1); 98 | break; 99 | } 100 | zero(mr_mip->w1); 101 | mr_mip->w1->len=1; 102 | mr_mip->w1->w[0]=sr; 103 | while ((sr=MR_REMAIN(mr_mip->w2->w[0],mr_mip->w1->w[0]))!=0) 104 | mr_mip->w2->w[0]=mr_mip->w1->w[0],mr_mip->w1->w[0]=sr; 105 | break; 106 | } 107 | a=1; 108 | b=0; 109 | c=0; 110 | d=1; 111 | m=mr_mip->w1->w[n-1]+1; 112 | /* printf("m= %d\n",m); */ 113 | #ifndef MR_SIMPLE_BASE 114 | if (mr_mip->base==0) 115 | { 116 | #endif 117 | #ifndef MR_NOFULLWIDTH 118 | if (m==0) 119 | { 120 | u=mr_mip->w1->w[n-1]; 121 | v=mr_mip->w2->w[n-1]; 122 | } 123 | else 124 | { 125 | /* printf("w1[n-1]= %d w1[n-2]= %d\n", mr_mip->w1->w[n-1],mr_mip->w1->w[n-2]); 126 | printf("w2[n-1]= %d w2[n-2]= %d\n", mr_mip->w2->w[n-1],mr_mip->w2->w[n-2]);*/ 127 | u=muldvm(mr_mip->w1->w[n-1],mr_mip->w1->w[n-2],m,&sr); 128 | v=muldvm(mr_mip->w2->w[n-1],mr_mip->w2->w[n-2],m,&sr); 129 | } 130 | #endif 131 | #ifndef MR_SIMPLE_BASE 132 | } 133 | else 134 | { 135 | u=muldiv(mr_mip->w1->w[n-1],mr_mip->base,mr_mip->w1->w[n-2],m,&sr); 136 | v=muldiv(mr_mip->w2->w[n-1],mr_mip->base,mr_mip->w2->w[n-2],m,&sr); 137 | } 138 | #endif 139 | /* printf("u= %d v= %d\n",u,v);*/ 140 | 141 | forever 142 | { /* work only with most significant piece */ 143 | if (((v+c)==0) || ((v+d)==0)) break; 144 | lq=MR_DIV((u+a),(v+c)); 145 | if (lq!=MR_DIV((u+b),(v+d))) break; 146 | if (lq>=(mr_small)(MR_TOOBIG/mr_abs(d))) break; 147 | q=(int)lq; 148 | r=a-q*c; 149 | a=c; 150 | c=r; 151 | r=b-q*d; 152 | b=d; 153 | d=r; 154 | lr=u-lq*v; 155 | u=v; 156 | v=lr; 157 | } 158 | } 159 | copy(mr_mip->w1,z); 160 | MR_OUT 161 | return (size(mr_mip->w1)); 162 | } 163 | 164 | -------------------------------------------------------------------------------- /source/mrio2.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL I/O routines 2. 37 | * mrio2.c 38 | */ 39 | 40 | #include "miracl.h" 41 | 42 | #ifndef MR_SIMPLE_BASE 43 | #ifndef MR_SIMPLE_IO 44 | 45 | static void cbase(_MIPD_ big x,mr_small oldbase,big y) 46 | { /* change radix of x from oldbase to base */ 47 | int i,s; 48 | mr_small n; 49 | BOOL done; 50 | #ifdef MR_OS_THREADS 51 | miracl *mr_mip=get_mip(); 52 | #endif 53 | if (mr_mip->ERNUM) return; 54 | if (mr_mip->base==oldbase) 55 | { 56 | copy(x,y); 57 | return; 58 | } 59 | 60 | MR_IN(13) 61 | 62 | s=exsign(x); 63 | #ifdef MR_FLASH 64 | numer(_MIPP_ x,mr_mip->w1); 65 | denom(_MIPP_ x,mr_mip->w2); 66 | done=FALSE; 67 | #else 68 | copy(x,mr_mip->w1); 69 | done=TRUE; 70 | #endif 71 | insign(PLUS,mr_mip->w1); 72 | 73 | forever 74 | { 75 | zero(mr_mip->w6); 76 | convert(_MIPP_ 1,mr_mip->w0); 77 | 78 | for (i=0;i<(int)mr_mip->w1->len;i++) 79 | { /* this is a bit slow - but not time critical */ 80 | 81 | 82 | mr_pmul(_MIPP_ mr_mip->w0,mr_mip->w1->w[i],mr_mip->w5); 83 | 84 | add(_MIPP_ mr_mip->w6,mr_mip->w5,mr_mip->w6); 85 | if (oldbase==0) 86 | { /* bit of a frig! */ 87 | n=mr_shiftbits(1,MIRACL/2); 88 | mr_pmul(_MIPP_ mr_mip->w0,n,mr_mip->w0); 89 | mr_pmul(_MIPP_ mr_mip->w0,n,mr_mip->w0); 90 | } 91 | else mr_pmul(_MIPP_ mr_mip->w0,oldbase,mr_mip->w0); 92 | } 93 | if (mr_mip->ERNUM || done) break; 94 | #ifdef MR_FLASH 95 | copy(mr_mip->w2,mr_mip->w1); 96 | copy(mr_mip->w6,mr_mip->w7); 97 | done=TRUE; 98 | #endif 99 | } 100 | 101 | #ifdef MR_FLASH 102 | fpack(_MIPP_ mr_mip->w7,mr_mip->w6,y); 103 | #else 104 | copy(mr_mip->w6,y); 105 | #endif 106 | 107 | insign(s,y); 108 | MR_OUT 109 | } 110 | 111 | int cinstr(_MIPD_ flash x,char *string) 112 | { /* input big number in base IOBASE */ 113 | mr_small newb,oldb,b; 114 | mr_lentype lx; 115 | int ipt; 116 | #ifdef MR_OS_THREADS 117 | miracl *mr_mip=get_mip(); 118 | #endif 119 | if (mr_mip->ERNUM) return 0; 120 | 121 | MR_IN(78) 122 | 123 | newb=mr_mip->IOBASE; 124 | oldb=mr_mip->apbase; 125 | mr_setbase(_MIPP_ newb); /* temporarily change base ... */ 126 | b=mr_mip->base; 127 | mr_mip->check=OFF; 128 | ipt=instr(_MIPP_ mr_mip->w5,string); /* ... and get number */ 129 | 130 | mr_mip->check=ON; 131 | lx=(mr_mip->w5->len&MR_OBITS); 132 | #ifdef MR_FLASH 133 | if ((int)(lx&MR_MSK)>mr_mip->nib || (int)((lx>>MR_BTS)&MR_MSK)>mr_mip->nib) 134 | #else 135 | if ((int)lx>mr_mip->nib) 136 | #endif 137 | { /* numerator or denominator too big */ 138 | mr_berror(_MIPP_ MR_ERR_OVERFLOW); 139 | MR_OUT 140 | return 0; 141 | } 142 | mr_setbase(_MIPP_ oldb); /* restore original base */ 143 | 144 | cbase(_MIPP_ mr_mip->w5,b,x); 145 | 146 | MR_OUT 147 | return ipt; 148 | } 149 | 150 | int cotstr(_MIPD_ flash x,char *string) 151 | { /* output a big number in base IOBASE */ 152 | mr_small newb,oldb,b; 153 | int ipt; 154 | #ifdef MR_OS_THREADS 155 | miracl *mr_mip=get_mip(); 156 | #endif 157 | if (mr_mip->ERNUM) return 0; 158 | 159 | MR_IN(77) 160 | newb=mr_mip->IOBASE; 161 | oldb=mr_mip->apbase; 162 | b=mr_mip->base; 163 | mr_setbase(_MIPP_ newb); /* temporarily change base ... */ 164 | mr_mip->check=OFF; 165 | 166 | cbase(_MIPP_ x,b,mr_mip->w5); /* number may get bigger ! */ 167 | mr_mip->check=ON; 168 | 169 | ipt=otstr(_MIPP_ mr_mip->w5,string); /*..... and output number */ 170 | zero(mr_mip->w5); 171 | mr_setbase(_MIPP_ oldb); 172 | MR_OUT 173 | return ipt; 174 | } 175 | 176 | #ifndef MR_NO_FILE_IO 177 | 178 | int cinnum(_MIPD_ flash x,FILE *filep) 179 | { /* convert from string to flash x */ 180 | int n; 181 | #ifdef MR_OS_THREADS 182 | miracl *mr_mip=get_mip(); 183 | #endif 184 | if (mr_mip->ERNUM) return 0; 185 | 186 | MR_IN(14) 187 | mr_mip->infile=filep; 188 | mr_mip->fin=TRUE; 189 | n=cinstr(_MIPP_ x,NULL); 190 | mr_mip->fin=FALSE; 191 | MR_OUT 192 | return n; 193 | } 194 | 195 | int cotnum(_MIPD_ flash x,FILE *filep) 196 | { /* convert flash to string */ 197 | int n; 198 | #ifdef MR_OS_THREADS 199 | miracl *mr_mip=get_mip(); 200 | #endif 201 | if (mr_mip->ERNUM) return 0; 202 | 203 | MR_IN(15) 204 | mr_mip->otfile=filep; 205 | mr_mip->fout=TRUE; 206 | n=cotstr(_MIPP_ x,NULL); 207 | mr_mip->fout=FALSE; 208 | MR_OUT 209 | return n; 210 | } 211 | 212 | #endif 213 | 214 | #endif 215 | #endif 216 | -------------------------------------------------------------------------------- /source/mrlucas.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL methods for evaluating lucas V function 37 | * mrlucas.c (Postl's algorithm) 38 | */ 39 | 40 | #include 41 | #include "miracl.h" 42 | 43 | void nres_lucas(_MIPD_ big p,big r,big vp,big v) 44 | { 45 | int i,nb; 46 | #ifdef MR_OS_THREADS 47 | miracl *mr_mip=get_mip(); 48 | #endif 49 | if (mr_mip->ERNUM) return; 50 | 51 | MR_IN(107) 52 | 53 | if (size(r)==0) 54 | { 55 | zero(vp); 56 | convert(_MIPP_ 2,v); 57 | nres(_MIPP_ v,v); 58 | MR_OUT 59 | return; 60 | } 61 | if (size(r)==1 || size(r)==(-1)) 62 | { /* note - sign of r doesn't matter */ 63 | convert(_MIPP_ 2,vp); 64 | nres(_MIPP_ vp,vp); 65 | copy(p,v); 66 | MR_OUT 67 | return; 68 | } 69 | 70 | copy(p,mr_mip->w3); 71 | 72 | convert(_MIPP_ 2,mr_mip->w4); 73 | nres(_MIPP_ mr_mip->w4,mr_mip->w4); /* w4=2 */ 74 | 75 | copy(mr_mip->w4,mr_mip->w8); 76 | copy(mr_mip->w3,mr_mip->w9); 77 | 78 | copy(r,mr_mip->w1); 79 | insign(PLUS,mr_mip->w1); 80 | decr(_MIPP_ mr_mip->w1,1,mr_mip->w1); 81 | 82 | #ifndef MR_ALWAYS_BINARY 83 | if (mr_mip->base==mr_mip->base2) 84 | { 85 | #endif 86 | nb=logb2(_MIPP_ mr_mip->w1); 87 | for (i=nb-1;i>=0;i--) 88 | { 89 | if (mr_mip->user!=NULL) (*mr_mip->user)(); 90 | 91 | if (mr_testbit(_MIPP_ mr_mip->w1,i)) 92 | { 93 | nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w9,mr_mip->w8); 94 | nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w3,mr_mip->w8); 95 | nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w9,mr_mip->w9); 96 | nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w4,mr_mip->w9); 97 | 98 | } 99 | else 100 | { 101 | nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w8,mr_mip->w9); 102 | nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w3,mr_mip->w9); 103 | nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w8,mr_mip->w8); 104 | nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w4,mr_mip->w8); 105 | } 106 | } 107 | 108 | #ifndef MR_ALWAYS_BINARY 109 | } 110 | else 111 | { 112 | expb2(_MIPP_ logb2(_MIPP_ mr_mip->w1)-1,mr_mip->w2); 113 | 114 | while (!mr_mip->ERNUM && size(mr_mip->w2)!=0) 115 | { /* use binary method */ 116 | if (mr_compare(mr_mip->w1,mr_mip->w2)>=0) 117 | { /* vp=v*vp-p, v=v*v-2 */ 118 | nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w9,mr_mip->w8); 119 | nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w3,mr_mip->w8); 120 | nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w9,mr_mip->w9); 121 | nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w4,mr_mip->w9); 122 | subtract(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w1); 123 | } 124 | else 125 | { /* v=v*vp-p, vp=vp*vp-2 */ 126 | nres_modmult(_MIPP_ mr_mip->w9,mr_mip->w8,mr_mip->w9); 127 | nres_modsub(_MIPP_ mr_mip->w9,mr_mip->w3,mr_mip->w9); 128 | nres_modmult(_MIPP_ mr_mip->w8,mr_mip->w8,mr_mip->w8); 129 | nres_modsub(_MIPP_ mr_mip->w8,mr_mip->w4,mr_mip->w8); 130 | } 131 | subdiv(_MIPP_ mr_mip->w2,2,mr_mip->w2); 132 | } 133 | } 134 | #endif 135 | 136 | copy(mr_mip->w9,v); 137 | if (v!=vp) copy(mr_mip->w8,vp); 138 | MR_OUT 139 | 140 | } 141 | 142 | void lucas(_MIPD_ big p,big r,big n,big vp,big v) 143 | { 144 | #ifdef MR_OS_THREADS 145 | miracl *mr_mip=get_mip(); 146 | #endif 147 | if (mr_mip->ERNUM) return; 148 | 149 | MR_IN(108) 150 | prepare_monty(_MIPP_ n); 151 | nres(_MIPP_ p,mr_mip->w3); 152 | nres_lucas(_MIPP_ mr_mip->w3,r,mr_mip->w8,mr_mip->w9); 153 | redc(_MIPP_ mr_mip->w9,v); 154 | if (v!=vp) redc(_MIPP_ mr_mip->w8,vp); 155 | MR_OUT 156 | } 157 | 158 | -------------------------------------------------------------------------------- /source/mrmuldv.c: -------------------------------------------------------------------------------- 1 | 2 | /* GCC inline assembly version for Linux64 */ 3 | 4 | #include "miracl.h" 5 | 6 | 7 | mr_small muldiv(mr_small a,mr_small b,mr_small c,mr_small m,mr_small *rp) 8 | { 9 | mr_small q; 10 | __asm__ __volatile__ ( 11 | "movq %1,%%rax\n" 12 | "mulq %2\n" 13 | "addq %3,%%rax\n" 14 | "adcq $0,%%rdx\n" 15 | "divq %4\n" 16 | "movq %5,%%rbx\n" 17 | "movq %%rdx,(%%rbx)\n" 18 | "movq %%rax,%0\n" 19 | : "=m"(q) 20 | : "m"(a),"m"(b),"m"(c),"m"(m),"m"(rp) 21 | : "rax","rbx","memory" 22 | ); 23 | return q; 24 | } 25 | 26 | mr_small muldvm(mr_small a,mr_small c,mr_small m,mr_small *rp) 27 | { 28 | mr_small q; 29 | __asm__ __volatile__ ( 30 | "movq %1,%%rdx\n" 31 | "movq %2,%%rax\n" 32 | "divq %3\n" 33 | "movq %4,%%rbx\n" 34 | "movq %%rdx,(%%rbx)\n" 35 | "movq %%rax,%0\n" 36 | : "=m"(q) 37 | : "m"(a),"m"(c),"m"(m),"m"(rp) 38 | : "rax","rbx","memory" 39 | ); 40 | return q; 41 | } 42 | 43 | mr_small muldvd(mr_small a,mr_small b,mr_small c,mr_small *rp) 44 | { 45 | mr_small q; 46 | __asm__ __volatile__ ( 47 | "movq %1,%%rax\n" 48 | "mulq %2\n" 49 | "addq %3,%%rax\n" 50 | "adcq $0,%%rdx\n" 51 | "movq %4,%%rbx\n" 52 | "movq %%rax,(%%rbx)\n" 53 | "movq %%rdx,%0\n" 54 | : "=m"(q) 55 | : "m"(a),"m"(b),"m"(c),"m"(rp) 56 | : "rax","rbx","memory" 57 | ); 58 | return q; 59 | } 60 | 61 | void muldvd2(mr_small a,mr_small b,mr_small *c,mr_small *rp) 62 | { 63 | __asm__ __volatile__ ( 64 | "movq %0,%%rax\n" 65 | "mulq %1\n" 66 | "movq %2,%%rbx\n" 67 | "addq (%%rbx),%%rax\n" 68 | "adcq $0,%%rdx\n" 69 | "movq %3,%%rsi\n" 70 | "addq (%%rsi),%%rax\n" 71 | "adcq $0,%%rdx\n" 72 | "movq %%rax,(%%rsi)\n" 73 | "movq %%rdx,(%%rbx)\n" 74 | : 75 | : "m"(a),"m"(b),"m"(c),"m"(rp) 76 | : "rax","rbx","rsi","memory" 77 | ); 78 | 79 | } 80 | 81 | -------------------------------------------------------------------------------- /source/mrmuldv.c32: -------------------------------------------------------------------------------- 1 | /* 2 | * Borland C++ 32-bit compiler (BCC32). Use with mirdef.h32 3 | * Uses inline assembly feature. Suitable for Win32 Apps 4 | * Also compatible with Microsoft Visual C++ 32-bit compiler 5 | */ 6 | 7 | #define ASM _asm 8 | 9 | int muldiv(a,b,c,m,rp) 10 | int a,b,c,m,*rp; 11 | { 12 | ASM mov eax,DWORD PTR a 13 | ASM mul DWORD PTR b 14 | ASM add eax,DWORD PTR c 15 | ASM adc edx,0h 16 | ASM div DWORD PTR m 17 | ASM mov ebx,DWORD PTR rp 18 | ASM mov [ebx],edx 19 | } 20 | 21 | int muldvm(a,c,m,rp) 22 | int a,c,m,*rp; 23 | { 24 | ASM mov edx,DWORD PTR a 25 | ASM mov eax,DWORD PTR c 26 | ASM div DWORD PTR m 27 | ASM mov ebx,DWORD PTR rp 28 | ASM mov [ebx],edx 29 | } 30 | 31 | int muldvd(a,b,c,rp) 32 | int a,b,c,*rp; 33 | { 34 | ASM mov eax,DWORD PTR a 35 | ASM mul DWORD PTR b 36 | ASM add eax,DWORD PTR c 37 | ASM adc edx,0h 38 | ASM mov ebx,DWORD PTR rp 39 | ASM mov [ebx],eax 40 | ASM mov eax,edx 41 | } 42 | 43 | void muldvd2(a,b,c,rp) 44 | int a,b,*c,*rp; 45 | { 46 | ASM mov eax,DWORD PTR a 47 | ASM mul DWORD PTR b 48 | ASM mov ebx,DWORD PTR c 49 | ASM add eax,[ebx] 50 | ASM adc edx,0h 51 | ASM mov esi,DWORD PTR rp 52 | ASM add eax,[esi] 53 | ASM adc edx,0h 54 | ASM mov [esi],eax 55 | ASM mov [ebx],edx 56 | } 57 | 58 | -------------------------------------------------------------------------------- /source/mrmuldv.ccc: -------------------------------------------------------------------------------- 1 | /* Standard C version of mrmuldv.c */ 2 | 3 | #include 4 | #include "miracl.h" 5 | 6 | mr_small muldiv(mr_small a,mr_small b,mr_small c,mr_small m,mr_small *rp) 7 | { 8 | mr_small q; 9 | mr_large dble=(mr_large)a*b+c; 10 | q=(mr_small)MR_LROUND(dble/m); 11 | *rp=(mr_small)(dble-(mr_large)q*m); 12 | return q; 13 | } 14 | 15 | #ifdef MR_FP_ROUNDING 16 | 17 | mr_small imuldiv(mr_small a,mr_small b,mr_small c,mr_small m,mr_large im,mr_small *rp) 18 | { 19 | mr_small q; 20 | mr_large dble=(mr_large)a*b+c; 21 | q=(mr_small)MR_LROUND(dble*im); 22 | *rp=(mr_small)(dble-(mr_large)q*m); 23 | return q; 24 | } 25 | 26 | #endif 27 | 28 | 29 | #ifndef MR_NOFULLWIDTH 30 | 31 | mr_small muldvm(mr_small a,mr_small c,mr_small m,mr_small *rp) 32 | { 33 | mr_small q; 34 | union doubleword dble; 35 | dble.h[MR_BOT]=c; 36 | dble.h[MR_TOP]=a; 37 | q=(mr_small)(dble.d/m); 38 | *rp=(mr_small)(dble.d-(mr_large)q*m); 39 | return q; 40 | } 41 | 42 | mr_small muldvd(mr_small a,mr_small b,mr_small c,mr_small *rp) 43 | { 44 | union doubleword dble; 45 | dble.d=(mr_large)a*b+c; 46 | *rp=dble.h[MR_BOT]; 47 | return dble.h[MR_TOP]; 48 | } 49 | 50 | void muldvd2(mr_small a,mr_small b,mr_small *c,mr_small *rp) 51 | { 52 | union doubleword dble; 53 | dble.d=(mr_large)a*b+*c+*rp; 54 | *rp=dble.h[MR_BOT]; 55 | *c=dble.h[MR_TOP]; 56 | } 57 | 58 | #endif 59 | 60 | -------------------------------------------------------------------------------- /source/mrmuldv.g64: -------------------------------------------------------------------------------- 1 | 2 | /* GCC inline assembly version for Linux64 */ 3 | 4 | #include "miracl.h" 5 | 6 | 7 | mr_small muldiv(mr_small a,mr_small b,mr_small c,mr_small m,mr_small *rp) 8 | { 9 | mr_small q; 10 | __asm__ __volatile__ ( 11 | "movq %1,%%rax\n" 12 | "mulq %2\n" 13 | "addq %3,%%rax\n" 14 | "adcq $0,%%rdx\n" 15 | "divq %4\n" 16 | "movq %5,%%rbx\n" 17 | "movq %%rdx,(%%rbx)\n" 18 | "movq %%rax,%0\n" 19 | : "=m"(q) 20 | : "m"(a),"m"(b),"m"(c),"m"(m),"m"(rp) 21 | : "rax","rbx","memory" 22 | ); 23 | return q; 24 | } 25 | 26 | mr_small muldvm(mr_small a,mr_small c,mr_small m,mr_small *rp) 27 | { 28 | mr_small q; 29 | __asm__ __volatile__ ( 30 | "movq %1,%%rdx\n" 31 | "movq %2,%%rax\n" 32 | "divq %3\n" 33 | "movq %4,%%rbx\n" 34 | "movq %%rdx,(%%rbx)\n" 35 | "movq %%rax,%0\n" 36 | : "=m"(q) 37 | : "m"(a),"m"(c),"m"(m),"m"(rp) 38 | : "rax","rbx","memory" 39 | ); 40 | return q; 41 | } 42 | 43 | mr_small muldvd(mr_small a,mr_small b,mr_small c,mr_small *rp) 44 | { 45 | mr_small q; 46 | __asm__ __volatile__ ( 47 | "movq %1,%%rax\n" 48 | "mulq %2\n" 49 | "addq %3,%%rax\n" 50 | "adcq $0,%%rdx\n" 51 | "movq %4,%%rbx\n" 52 | "movq %%rax,(%%rbx)\n" 53 | "movq %%rdx,%0\n" 54 | : "=m"(q) 55 | : "m"(a),"m"(b),"m"(c),"m"(rp) 56 | : "rax","rbx","memory" 57 | ); 58 | return q; 59 | } 60 | 61 | void muldvd2(mr_small a,mr_small b,mr_small *c,mr_small *rp) 62 | { 63 | __asm__ __volatile__ ( 64 | "movq %0,%%rax\n" 65 | "mulq %1\n" 66 | "movq %2,%%rbx\n" 67 | "addq (%%rbx),%%rax\n" 68 | "adcq $0,%%rdx\n" 69 | "movq %3,%%rsi\n" 70 | "addq (%%rsi),%%rax\n" 71 | "adcq $0,%%rdx\n" 72 | "movq %%rax,(%%rsi)\n" 73 | "movq %%rdx,(%%rbx)\n" 74 | : 75 | : "m"(a),"m"(b),"m"(c),"m"(rp) 76 | : "rax","rbx","rsi","memory" 77 | ); 78 | 79 | } 80 | 81 | -------------------------------------------------------------------------------- /source/mrmuldv.gcc: -------------------------------------------------------------------------------- 1 | 2 | /* GCC inline assembly version for Linux */ 3 | 4 | #include "miracl.h" 5 | 6 | 7 | mr_small muldiv(mr_small a,mr_small b,mr_small c,mr_small m,mr_small *rp) 8 | { 9 | mr_small q; 10 | __asm__ __volatile__ ( 11 | "movl %1,%%eax\n" 12 | "mull %2\n" 13 | "addl %3,%%eax\n" 14 | "adcl $0,%%edx\n" 15 | "divl %4\n" 16 | "movl %5,%%ebx\n" 17 | "movl %%edx,(%%ebx)\n" 18 | "movl %%eax,%0\n" 19 | : "=m"(q) 20 | : "m"(a),"m"(b),"m"(c),"m"(m),"m"(rp) 21 | : "eax","ebx","memory" 22 | ); 23 | return q; 24 | } 25 | 26 | mr_small muldvm(mr_small a,mr_small c,mr_small m,mr_small *rp) 27 | { 28 | mr_small q; 29 | __asm__ __volatile__ ( 30 | "movl %1,%%edx\n" 31 | "movl %2,%%eax\n" 32 | "divl %3\n" 33 | "movl %4,%%ebx\n" 34 | "movl %%edx,(%%ebx)\n" 35 | "movl %%eax,%0\n" 36 | : "=m"(q) 37 | : "m"(a),"m"(c),"m"(m),"m"(rp) 38 | : "eax","ebx","memory" 39 | ); 40 | return q; 41 | } 42 | 43 | mr_small muldvd(mr_small a,mr_small b,mr_small c,mr_small *rp) 44 | { 45 | mr_small q; 46 | __asm__ __volatile__ ( 47 | "movl %1,%%eax\n" 48 | "mull %2\n" 49 | "addl %3,%%eax\n" 50 | "adcl $0,%%edx\n" 51 | "movl %4,%%ebx\n" 52 | "movl %%eax,(%%ebx)\n" 53 | "movl %%edx,%0\n" 54 | : "=m"(q) 55 | : "m"(a),"m"(b),"m"(c),"m"(rp) 56 | : "eax","ebx","memory" 57 | ); 58 | return q; 59 | } 60 | 61 | void muldvd2(mr_small a,mr_small b,mr_small *c,mr_small *rp) 62 | { 63 | __asm__ __volatile__ ( 64 | "movl %0,%%eax\n" 65 | "mull %1\n" 66 | "movl %2,%%ebx\n" 67 | "addl (%%ebx),%%eax\n" 68 | "adcl $0,%%edx\n" 69 | "movl %3,%%esi\n" 70 | "addl (%%esi),%%eax\n" 71 | "adcl $0,%%edx\n" 72 | "movl %%eax,(%%esi)\n" 73 | "movl %%edx,(%%ebx)\n" 74 | : 75 | : "m"(a),"m"(b),"m"(c),"m"(rp) 76 | : "eax","ebx","esi","memory" 77 | ); 78 | 79 | } 80 | 81 | -------------------------------------------------------------------------------- /source/mrmuldv.gpp: -------------------------------------------------------------------------------- 1 | / 2 | / DJGPP GNU C version for DOS 3 | / M. Scott 22/3/98 4 | / 5 | 6 | 7 | .file "mrmuldv.c" 8 | .text 9 | .globl _muldiv 10 | _muldiv: 11 | pushl %ebp 12 | movl %esp,%ebp 13 | pushl %ebx 14 | 15 | 16 | movl 8(%ebp),%eax 17 | mull 12(%ebp) 18 | addl 16(%ebp),%eax 19 | adcl $0,%edx 20 | 21 | divl 20(%ebp) 22 | movl 24(%ebp),%ebx 23 | movl %edx,(%ebx) 24 | 25 | popl %ebx 26 | popl %ebp 27 | ret 28 | 29 | .globl _muldvm 30 | _muldvm: 31 | pushl %ebp 32 | movl %esp,%ebp 33 | pushl %ebx 34 | 35 | movl 8(%ebp),%edx 36 | movl 12(%ebp),%eax 37 | divl 16(%ebp) 38 | 39 | movl 20(%ebp),%ebx 40 | movl %edx,(%ebx) 41 | 42 | popl %ebx 43 | popl %ebp 44 | ret 45 | 46 | .globl _muldvd 47 | _muldvd: 48 | pushl %ebp 49 | movl %esp,%ebp 50 | pushl %ebx 51 | 52 | movl 8(%ebp),%eax 53 | mull 12(%ebp) 54 | addl 16(%ebp),%eax 55 | adcl $0,%edx 56 | movl 20(%ebp),%ebx 57 | movl %eax,(%ebx) 58 | movl %edx,%eax 59 | 60 | popl %ebx 61 | popl %ebp 62 | ret 63 | 64 | .globl _muldvd2 65 | _muldvd2: 66 | pushl %ebp 67 | movl %esp,%ebp 68 | pushl %ebx 69 | pushl %esi 70 | 71 | movl 8(%ebp),%eax 72 | mull 12(%ebp) 73 | movl 16(%ebp),%ebx 74 | addl (%ebx),%eax 75 | adcl $0,%edx 76 | movl 20(%ebp),%esi 77 | addl (%esi),%eax 78 | adcl $0,%edx 79 | 80 | movl %eax,(%esi) 81 | movl %edx,(%ebx) 82 | 83 | popl %esi 84 | popl %ebx 85 | popl %ebp 86 | ret 87 | 88 | -------------------------------------------------------------------------------- /source/mrmuldv.ppc: -------------------------------------------------------------------------------- 1 | 2 | /* GCC inline assembly version for Linux */ 3 | 4 | #include "miracl.h" 5 | 6 | mr_small muldiv(a,b,c,m,rp) 7 | mr_small a,b,c,m; 8 | mr_small *rp; 9 | { 10 | int i; 11 | mr_small d,q=0,r=0; 12 | d=m-a; 13 | for (i=MIRACL/4;i>0;i--) 14 | { /* do it bit by bit */ 15 | r<<=1; 16 | if ((mr_utype)c<0) r++; 17 | c<<=1; 18 | q<<=1; 19 | if ((mr_utype)b<0) 20 | { 21 | if (r>=m) { r-=d; q++; } 22 | else r+=a; 23 | } 24 | if (r>=m) { r-=m; q++; } 25 | b<<=1; 26 | r<<=1; 27 | if ((mr_utype)c<0) r++; 28 | c<<=1; 29 | q<<=1; 30 | if ((mr_utype)b<0) 31 | { 32 | if (r>=m) { r-=d; q++; } 33 | else r+=a; 34 | } 35 | if (r>=m) { r-=m; q++; } 36 | b<<=1; 37 | r<<=1; 38 | if ((mr_utype)c<0) r++; 39 | c<<=1; 40 | q<<=1; 41 | if ((mr_utype)b<0) 42 | { 43 | if (r>=m) { r-=d; q++; } 44 | else r+=a; 45 | } 46 | if (r>=m) { r-=m; q++; } 47 | b<<=1; 48 | r<<=1; 49 | if ((mr_utype)c<0) r++; 50 | c<<=1; 51 | q<<=1; 52 | if ((mr_utype)b<0) 53 | { 54 | if (r>=m) { r-=d; q++; } 55 | else r+=a; 56 | } 57 | if (r>=m) { r-=m; q++; } 58 | b<<=1; 59 | } 60 | *rp=r; 61 | return q; 62 | } 63 | 64 | mr_small muldvm(a,c,m,rp) 65 | mr_small a,c,m; 66 | mr_small *rp; 67 | { /* modified Blakely-Sloan */ 68 | register int i,carry; 69 | register mr_small q=0,r=0; 70 | r=a; 71 | for (i=MIRACL/4;i>0;i--) 72 | { /* do it bit by bit */ 73 | carry=0; 74 | if ((mr_utype)r<0) carry=1; 75 | r<<=1; 76 | if ((mr_utype)c<0) r++; 77 | c<<=1; 78 | q<<=1; 79 | if (carry || r>=m) { r-=m; q++; } 80 | carry=0; 81 | if ((mr_utype)r<0) carry=1; 82 | r<<=1; 83 | if ((mr_utype)c<0) r++; 84 | c<<=1; 85 | q<<=1; 86 | if (carry || r>=m) { r-=m; q++; } 87 | carry=0; 88 | if ((mr_utype)r<0) carry=1; 89 | r<<=1; 90 | if ((mr_utype)c<0) r++; 91 | c<<=1; 92 | q<<=1; 93 | if (carry || r>=m) { r-=m; q++; } 94 | carry=0; 95 | if ((mr_utype)r<0) carry=1; 96 | r<<=1; 97 | if ((mr_utype)c<0) r++; 98 | c<<=1; 99 | q<<=1; 100 | if (carry || r>=m) { r-=m; q++; } 101 | } 102 | *rp=r; 103 | return q; 104 | } 105 | 106 | void muldvd2(mr_small a,mr_small b,mr_small *c,mr_small *rp) 107 | { 108 | __asm__ __volatile__ ( 109 | "mulld %%r16,%0,%1\n" 110 | "mulhdu %%r17,%0,%1\n" 111 | "ld %%r18,0(%2)\n" 112 | "addc %%r16,%%r18,%%r16\n" 113 | "addze %%r17,%%r17\n" 114 | "ld %%r19,0(%3)\n" 115 | "addc %%r16,%%r19,%%r16\n" 116 | "addze %%r17,%%r17\n" 117 | "std %%r16,0(%3)\n" 118 | "std %%r17,0(%2)\n" 119 | : 120 | : "r"(a),"r"(b),"r"(c),"r"(rp) 121 | : "r16","r17","r18","r19","memory" 122 | ); 123 | 124 | } 125 | 126 | mr_small muldvd(mr_small a,mr_small b,mr_small c,mr_small *rp) 127 | { 128 | mr_small q; 129 | __asm__ __volatile__ ( 130 | "mulld %%r16,%1,%2\n" 131 | "mulhdu %%r17,%1,%2\n" 132 | "addc %%r16,%3,%%r16\n" 133 | "addze %%r17,%%r17\n" 134 | "std %%r16,0(%4)\n" 135 | "or %0,%%r17,%%r17\n" 136 | : "=r"(q) 137 | : "r"(a),"r"(b),"r"(c),"r"(rp) 138 | : "r16","r17","memory" 139 | ); 140 | return q; 141 | } 142 | 143 | /* 144 | mr_small test(mr_small a,mr_small b,mr_small c,mr_small *rp) 145 | { 146 | mr_small q; 147 | q=a*b+c+*rp; 148 | *rp=q; 149 | return q; 150 | } 151 | 152 | int main() 153 | { 154 | mr_small a,b,c,q,r; 155 | 156 | 157 | printf("sizeof(mr_small)= %d\n",sizeof(mr_small)); 158 | a=(mr_small)-1; 159 | b=(mr_small)-1; 160 | c=0; 161 | q=0; 162 | r=0; 163 | q=muldvd(a,b,c,&r); 164 | muldvd2(a,b,&c,&r); 165 | printf("c= %lx r= %lx\n",c,r); 166 | } 167 | 168 | */ 169 | -------------------------------------------------------------------------------- /source/mrmuldv.s: -------------------------------------------------------------------------------- 1 | 2 | .file "mrmuldv.s" 3 | .text 4 | .globl muldiv 5 | muldiv: 6 | pushl %ebp 7 | movl %esp,%ebp 8 | pushl %ebx 9 | 10 | 11 | movl 8(%ebp),%eax 12 | mull 12(%ebp) 13 | addl 16(%ebp),%eax 14 | adcl $0,%edx 15 | 16 | divl 20(%ebp) 17 | movl 24(%ebp),%ebx 18 | movl %edx,(%ebx) 19 | 20 | popl %ebx 21 | popl %ebp 22 | ret 23 | 24 | .globl muldvm 25 | muldvm: 26 | pushl %ebp 27 | movl %esp,%ebp 28 | pushl %ebx 29 | 30 | movl 8(%ebp),%edx 31 | movl 12(%ebp),%eax 32 | divl 16(%ebp) 33 | 34 | movl 20(%ebp),%ebx 35 | movl %edx,(%ebx) 36 | 37 | popl %ebx 38 | popl %ebp 39 | ret 40 | 41 | .globl muldvd 42 | muldvd: 43 | pushl %ebp 44 | movl %esp,%ebp 45 | pushl %ebx 46 | 47 | movl 8(%ebp),%eax 48 | mull 12(%ebp) 49 | addl 16(%ebp),%eax 50 | adcl $0,%edx 51 | movl 20(%ebp),%ebx 52 | movl %eax,(%ebx) 53 | movl %edx,%eax 54 | 55 | popl %ebx 56 | popl %ebp 57 | ret 58 | 59 | .globl muldvd2 60 | muldvd2: 61 | pushl %ebp 62 | movl %esp,%ebp 63 | pushl %ebx 64 | pushl %esi 65 | 66 | movl 8(%ebp),%eax 67 | mull 12(%ebp) 68 | movl 16(%ebp),%ebx 69 | addl (%ebx),%eax 70 | adcl $0,%edx 71 | movl 20(%ebp),%esi 72 | addl (%esi),%eax 73 | adcl $0,%edx 74 | 75 | movl %eax,(%esi) 76 | movl %edx,(%ebx) 77 | 78 | popl %esi 79 | popl %ebx 80 | popl %ebp 81 | ret 82 | 83 | -------------------------------------------------------------------------------- /source/mrmuldv.s64: -------------------------------------------------------------------------------- 1 | 2 | .file "mrmuldv.s" 3 | .text 4 | .globl muldiv 5 | muldiv: 6 | 7 | pushq %rbx 8 | movq %rdi,%rax 9 | movq %rdx,%rbx 10 | mulq %rsi 11 | addq %rbx,%rax 12 | adcq $0,%rdx 13 | 14 | divq %rcx 15 | movq %r8,%rbx 16 | movq %rdx,(%rbx) 17 | popq %rbx 18 | 19 | ret 20 | 21 | .globl muldvm 22 | muldvm: 23 | 24 | pushq %rbx 25 | movq %rdx,%rbx 26 | movq %rdi,%rdx 27 | movq %rsi,%rax 28 | divq %rbx 29 | 30 | movq %rcx,%rbx 31 | movq %rdx,(%rbx) 32 | popq %rbx 33 | 34 | ret 35 | 36 | .globl muldvd 37 | muldvd: 38 | 39 | pushq %rbx 40 | movq %rdi,%rax 41 | movq %rdx,%rbx 42 | mulq %rsi 43 | addq %rbx,%rax 44 | adcq $0,%rdx 45 | 46 | movq %rcx,%rbx 47 | movq %rax,(%rbx) 48 | movq %rdx,%rax 49 | popq %rbx 50 | 51 | ret 52 | 53 | .globl muldvd2 54 | muldvd2: 55 | 56 | pushq %rbx 57 | movq %rdi,%rax 58 | movq %rdx,%rbx 59 | mulq %rsi 60 | addq (%rbx),%rax 61 | adcq $0,%rdx 62 | addq (%rcx),%rax 63 | adcq $0,%rdx 64 | 65 | movq %rax,(%rcx) 66 | movq %rdx,(%rbx) 67 | popq %rbx 68 | 69 | ret 70 | 71 | -------------------------------------------------------------------------------- /source/mrmuldv.tcc: -------------------------------------------------------------------------------- 1 | /* 2 | * Turbo C compiler V1.5+, Turbo/Borland C++. Microsoft C/C++ 3 | * Uses inline assembly feature 4 | * Generates code identical to above version, and 5 | * can be used instead. 6 | */ 7 | 8 | #define ASM asm 9 | 10 | /* or perhaps #define ASM _asm */ 11 | 12 | unsigned int muldiv(a,b,c,m,rp) 13 | unsigned int a,b,c,m,*rp; 14 | { 15 | ASM mov ax,a ;/* get a */ 16 | ASM mul WORD PTR b ;/* multiply by b */ 17 | ASM add ax,c ;/* add c to low word */ 18 | ASM adc dx,0h ;/* add carry to high word */ 19 | ASM div WORD PTR m ;/* divide by m */ 20 | ASM mov bx,rp ;/* get address for remainder */ 21 | ASM mov [bx],dx ;/* store remainder */ 22 | } 23 | /* Replace last two ASM lines when using large data memory models */ 24 | /* ASM les bx, DWORD PTR rp ; get address for remainder */ 25 | /* ASM mov WORD PTR es:[bx],dx ; store remainder */ 26 | 27 | unsigned int muldvm(a,c,m,rp) 28 | unsigned int a,c,m,*rp; 29 | { 30 | ASM mov dx,a ;/* get a */ 31 | ASM mov ax,c ;/* add in c to low word */ 32 | ASM div WORD PTR m ;/* divide by m */ 33 | ASM mov bx,rp ;/* get address for remainder */ 34 | ASM mov [bx],dx ;/* store remainder */ 35 | } 36 | /* Replace last two ASM lines when using large data memory models */ 37 | /* ASM les bx, DWORD PTR rp ; get address for remainder */ 38 | /* ASM mov WORD PTR es:[bx],dx ; store remainder */ 39 | 40 | unsigned int muldvd(a,b,c,rp) 41 | unsigned int a,b,c,*rp; 42 | { 43 | ASM mov ax,a ;/* get a */ 44 | ASM mul WORD PTR b ;/* multiply by b */ 45 | ASM add ax,c ;/* add c to low word */ 46 | ASM adc dx,0h ;/* add carry to high word */ 47 | ASM mov bx,rp ;/* get address for remainder */ 48 | ASM mov [bx],ax ;/* store remainder */ 49 | ASM mov ax,dx 50 | } 51 | /* Replace second and third last lines if using large data memory models */ 52 | /* ASM les bx, DWORD PTR rp ; get address for remainder */ 53 | /* ASM mov WORD PTR es:[bx],ax ; store remainder */ 54 | 55 | void muldvd2(a,b,c,rp) 56 | unsigned int a,b,*c,*rp; 57 | { 58 | ASM mov ax,a ;/* get a */ 59 | ASM mul WORD PTR b ;/* multiply by b */ 60 | ASM mov bx,c 61 | ASM add ax,[bx] 62 | ASM adc dx,0h ;/* add carry to high word */ 63 | ASM mov si,rp 64 | ASM add ax,[si] 65 | ASM adc dx,0h 66 | ASM mov [si],ax 67 | ASM mov [bx],dx 68 | } 69 | 70 | /* for large memory model .... 71 | ASM mov ax,a 72 | ASM mul WORD PTR b 73 | ASM les bx, DWORD PTR c 74 | ASM add ax, WORD PTR es:[bx] 75 | ASM adc dx,0h 76 | ASM les si,DWORD PTR rp 77 | ASM add ax,WORD PTR es:[si] 78 | ASM adc dx,0h 79 | ASM mov WORD PTR es:[si],ax 80 | ASM les bx,DWORD PTR c 81 | ASM mov WORD PTR es:[bx],dx 82 | */ 83 | -------------------------------------------------------------------------------- /source/mrmuldv.w64: -------------------------------------------------------------------------------- 1 | /* Win64 C version of mrmuldv.c */ 2 | 3 | #include "miracl.h" 4 | 5 | mr_small muldiv(mr_small a,mr_small b,mr_small c,mr_small m,mr_small *rp) 6 | { 7 | int i; 8 | mr_small d,q=0,r=0; 9 | d=m-a; 10 | for (i=MIRACL/4;i>0;i--) 11 | { /* do it bit by bit */ 12 | r<<=1; 13 | if ((mr_utype)c<0) r++; 14 | c<<=1; 15 | q<<=1; 16 | if ((mr_utype)b<0) 17 | { 18 | if (r>=m) { r-=d; q++; } 19 | else r+=a; 20 | } 21 | if (r>=m) { r-=m; q++; } 22 | b<<=1; 23 | r<<=1; 24 | if ((mr_utype)c<0) r++; 25 | c<<=1; 26 | q<<=1; 27 | if ((mr_utype)b<0) 28 | { 29 | if (r>=m) { r-=d; q++; } 30 | else r+=a; 31 | } 32 | if (r>=m) { r-=m; q++; } 33 | b<<=1; 34 | r<<=1; 35 | if ((mr_utype)c<0) r++; 36 | c<<=1; 37 | q<<=1; 38 | if ((mr_utype)b<0) 39 | { 40 | if (r>=m) { r-=d; q++; } 41 | else r+=a; 42 | } 43 | if (r>=m) { r-=m; q++; } 44 | b<<=1; 45 | r<<=1; 46 | if ((mr_utype)c<0) r++; 47 | c<<=1; 48 | q<<=1; 49 | if ((mr_utype)b<0) 50 | { 51 | if (r>=m) { r-=d; q++; } 52 | else r+=a; 53 | } 54 | if (r>=m) { r-=m; q++; } 55 | b<<=1; 56 | } 57 | *rp=r; 58 | return q; 59 | } 60 | 61 | mr_small muldvm(mr_small a,mr_small c,mr_small m,mr_small *rp) 62 | { /* modified Blakely-Sloan */ 63 | register int i,carry; 64 | register mr_small q=0,r=0; 65 | r=a; 66 | for (i=MIRACL/4;i>0;i--) 67 | { /* do it bit by bit */ 68 | carry=0; 69 | if ((mr_utype)r<0) carry=1; 70 | r<<=1; 71 | if ((mr_utype)c<0) r++; 72 | c<<=1; 73 | q<<=1; 74 | if (carry || r>=m) { r-=m; q++; } 75 | carry=0; 76 | if ((mr_utype)r<0) carry=1; 77 | r<<=1; 78 | if ((mr_utype)c<0) r++; 79 | c<<=1; 80 | q<<=1; 81 | if (carry || r>=m) { r-=m; q++; } 82 | carry=0; 83 | if ((mr_utype)r<0) carry=1; 84 | r<<=1; 85 | if ((mr_utype)c<0) r++; 86 | c<<=1; 87 | q<<=1; 88 | if (carry || r>=m) { r-=m; q++; } 89 | carry=0; 90 | if ((mr_utype)r<0) carry=1; 91 | r<<=1; 92 | if ((mr_utype)c<0) r++; 93 | c<<=1; 94 | q<<=1; 95 | if (carry || r>=m) { r-=m; q++; } 96 | } 97 | *rp=r; 98 | return q; 99 | } 100 | 101 | #ifndef MR_NOFULLWIDTH 102 | 103 | #ifdef MR_NO_INTRINSICS 104 | 105 | /* define mr_hltype as that C type that is half the size in bits of the 106 | underlying type (mr_utype in mirdef.h). Perhaps short if mr_utype is long? 107 | Possible int if mr_utype is 64-bit long long ?? */ 108 | 109 | #define mr_hltype int 110 | 111 | mr_small muldvd(mr_small a,mr_small b,mr_small c,mr_small *rp) 112 | { /* multiply by parts */ 113 | mr_small middle,middle2; 114 | mr_small q,r; 115 | unsigned mr_hltype am,al,bm,bl; 116 | int hshift=(MIRACL>>1); 117 | am=(unsigned mr_hltype)(a>>hshift); 118 | al=(unsigned mr_hltype)a; 119 | bm=(unsigned mr_hltype)(b>>hshift); 120 | bl=(unsigned mr_hltype)b; 121 | /* form partial products */ 122 | r= (mr_small)al*bl; 123 | q= (mr_small)am*bm; 124 | middle=(mr_small)al*bm; 125 | middle2=(mr_small)bl*am; 126 | middle+=middle2; /* combine them - carefully */ 127 | if (middle>hshift)<(unsigned mr_hltype)middle) q++; 130 | q+=(middle>>hshift); 131 | r+=c; 132 | if (r>1); 143 | am=(unsigned mr_hltype)(a>>hshift); 144 | al=(unsigned mr_hltype)a; 145 | bm=(unsigned mr_hltype)(b>>hshift); 146 | bl=(unsigned mr_hltype)b; 147 | /* form partial products */ 148 | r= (mr_small)al*bl; 149 | q= (mr_small)am*bm; 150 | middle=(mr_small)al*bm; 151 | middle2=(mr_small)bl*am; 152 | middle+=middle2; /* combine them - carefully */ 153 | if (middle>hshift)<(unsigned mr_hltype)middle) q++; 156 | q+=(middle>>hshift); 157 | r+=*c; 158 | if (r<*c) q++; 159 | r+=*rp; 160 | if (r<*rp) q++; 161 | *rp=r; 162 | *c=q; 163 | } 164 | 165 | 166 | #endif 167 | 168 | /* These are now in-lined - see miracl.h */ 169 | 170 | /* 171 | mr_small muldvd(mr_small a,mr_small b,mr_small c,mr_small *rp) 172 | { 173 | mr_small q,r; 174 | r=_umul128(a,b,&q); 175 | r+=c; 176 | q+=(r. * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL calculate pi - by Gauss-Legendre method 37 | * mrpi.c 38 | */ 39 | 40 | #include 41 | #include "miracl.h" 42 | 43 | #ifdef MR_FLASH 44 | 45 | void fpi(_MIPD_ flash pi) 46 | { /* Calculate pi using Guass-Legendre method */ 47 | int x,nits,op[5]; 48 | #ifdef MR_OS_THREADS 49 | miracl *mr_mip=get_mip(); 50 | #endif 51 | if (mr_mip->ERNUM) return; 52 | 53 | MR_IN(53) 54 | 55 | if (size(mr_mip->pi)!=0) 56 | { 57 | copy(mr_mip->pi,pi); 58 | mr_mip->EXACT=FALSE; 59 | MR_OUT 60 | return; 61 | } 62 | 63 | fconv(_MIPP_ 1,2,mr_mip->pi); 64 | froot(_MIPP_ mr_mip->pi,2,mr_mip->pi); 65 | fconv(_MIPP_ 1,1,mr_mip->w11); 66 | fconv(_MIPP_ 1,4,mr_mip->w12); 67 | x=1; 68 | op[0]=0x6C; 69 | op[1]=1; 70 | op[4]=0; 71 | nits=mr_mip->lg2b*mr_mip->nib/4; 72 | while (xw11,mr_mip->w13); 75 | op[2]=1; 76 | op[3]=2; 77 | flop(_MIPP_ mr_mip->w11,mr_mip->pi,op,mr_mip->w11); 78 | fmul(_MIPP_ mr_mip->pi,mr_mip->w13,mr_mip->pi); 79 | froot(_MIPP_ mr_mip->pi,2,mr_mip->pi); 80 | fsub(_MIPP_ mr_mip->w11,mr_mip->w13,mr_mip->w13); 81 | fmul(_MIPP_ mr_mip->w13,mr_mip->w13,mr_mip->w13); 82 | op[3]=1; 83 | op[2]=(-x); 84 | flop(_MIPP_ mr_mip->w12,mr_mip->w13,op,mr_mip->w12); /* w12 = w12 - x.w13 */ 85 | x*=2; 86 | } 87 | fadd(_MIPP_ mr_mip->w11,mr_mip->pi,mr_mip->pi); 88 | fmul(_MIPP_ mr_mip->pi,mr_mip->pi,mr_mip->pi); 89 | op[0]=0x48; 90 | op[2]=0; 91 | op[3]=4; 92 | flop(_MIPP_ mr_mip->pi,mr_mip->w12,op,mr_mip->pi); /* pi = pi/(4.w12) */ 93 | if (pi!=NULL) copy(mr_mip->pi,pi); 94 | MR_OUT 95 | } 96 | 97 | #endif 98 | 99 | -------------------------------------------------------------------------------- /source/mrrand.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL random number routines 37 | * mrrand.c 38 | */ 39 | 40 | #include "miracl.h" 41 | 42 | #ifdef MR_FP 43 | #include 44 | #endif 45 | 46 | #ifndef MR_NO_RAND 47 | 48 | void bigrand(_MIPD_ big w,big x) 49 | { /* generate a big random number 0<=xERNUM) return; 59 | 60 | MR_IN(20) 61 | 62 | /* decr(_MIPP_ w,2,w); */ 63 | m=0; 64 | zero(mr_mip->w0); 65 | 66 | do 67 | { /* create big rand piece by piece */ 68 | m++; 69 | mr_mip->w0->len=m; 70 | r=brand(_MIPPO_ ); 71 | if (mr_mip->base==0) mr_mip->w0->w[m-1]=r; 72 | else mr_mip->w0->w[m-1]=MR_REMAIN(r,mr_mip->base); 73 | } while (mr_compare(mr_mip->w0,w)<0); 74 | mr_lzero(mr_mip->w0); 75 | divide(_MIPP_ mr_mip->w0,w,w); 76 | 77 | copy(mr_mip->w0,x); 78 | /* incr(_MIPP_ x,2,x); 79 | if (w!=x) incr(_MIPP_ w,2,w); */ 80 | MR_OUT 81 | } 82 | 83 | void bigdig(_MIPD_ int n,int b,big x) 84 | { /* generate random number n digits long * 85 | * to "printable" base b */ 86 | #ifdef MR_OS_THREADS 87 | miracl *mr_mip=get_mip(); 88 | #endif 89 | if (mr_mip->ERNUM) return; 90 | 91 | MR_IN(19) 92 | 93 | if (b<2 || b>256) 94 | { 95 | mr_berror(_MIPP_ MR_ERR_BASE_TOO_BIG); 96 | MR_OUT 97 | return; 98 | } 99 | 100 | do 101 | { /* repeat if x too small */ 102 | expint(_MIPP_ b,n,mr_mip->w1); 103 | bigrand(_MIPP_ mr_mip->w1,x); 104 | subdiv(_MIPP_ mr_mip->w1,b,mr_mip->w1); 105 | } while (!mr_mip->ERNUM && mr_compare(x,mr_mip->w1)<0); 106 | 107 | MR_OUT 108 | } 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /source/mrround.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL euclidean mediant rounding routine 37 | * mrround.c 38 | */ 39 | 40 | #include "miracl.h" 41 | 42 | #ifdef MR_FP 43 | #include 44 | #endif 45 | 46 | #ifdef MR_FLASH 47 | 48 | static int euclid(_MIPD_ big x,int num) 49 | { /* outputs next c.f. quotient from gcd(w5,w6) */ 50 | mr_small sr,m; 51 | #ifdef MR_FP 52 | mr_small dres; 53 | #endif 54 | mr_small lr,lq; 55 | big t; 56 | #ifdef MR_OS_THREADS 57 | miracl *mr_mip=get_mip(); 58 | #endif 59 | if (num==0) 60 | { 61 | mr_mip->oldn=(-1); 62 | mr_mip->carryon=FALSE; 63 | mr_mip->last=FALSE; 64 | if (mr_compare(mr_mip->w6,mr_mip->w5)>0) 65 | { /* ensure w5>w6 */ 66 | t=mr_mip->w5,mr_mip->w5=mr_mip->w6,mr_mip->w6=t; 67 | return (mr_mip->q=0); 68 | } 69 | } 70 | else if (num==mr_mip->oldn || mr_mip->q<0) return mr_mip->q; 71 | mr_mip->oldn=num; 72 | if (mr_mip->carryon) goto middle; 73 | start: 74 | if (size(mr_mip->w6)==0) return (mr_mip->q=(-1)); 75 | mr_mip->ndig=(int)mr_mip->w5->len; 76 | mr_mip->carryon=TRUE; 77 | mr_mip->a=1; 78 | mr_mip->b=0; 79 | mr_mip->c=0; 80 | mr_mip->d=1; 81 | if (mr_mip->ndig==1) 82 | { 83 | mr_mip->last=TRUE; 84 | mr_mip->u=mr_mip->w5->w[0]; 85 | mr_mip->v=mr_mip->w6->w[0]; 86 | } 87 | else 88 | { 89 | m=mr_mip->w5->w[mr_mip->ndig-1]+1; 90 | if (mr_mip->base==0) 91 | { 92 | #ifndef MR_NOFULLWIDTH 93 | if (m==0) 94 | { 95 | mr_mip->u=mr_mip->w5->w[mr_mip->ndig-1]; 96 | mr_mip->v=mr_mip->w6->w[mr_mip->ndig-1]; 97 | } 98 | else 99 | { 100 | mr_mip->u=muldvm(mr_mip->w5->w[mr_mip->ndig-1],mr_mip->w5->w[mr_mip->ndig-2],m,&sr); 101 | mr_mip->v=muldvm(mr_mip->w6->w[mr_mip->ndig-1],mr_mip->w6->w[mr_mip->ndig-2],m,&sr); 102 | } 103 | #endif 104 | } 105 | else 106 | { 107 | mr_mip->u=muldiv(mr_mip->w5->w[mr_mip->ndig-1],mr_mip->base,mr_mip->w5->w[mr_mip->ndig-2],m,&sr); 108 | mr_mip->v=muldiv(mr_mip->w6->w[mr_mip->ndig-1],mr_mip->base,mr_mip->w6->w[mr_mip->ndig-2],m,&sr); 109 | } 110 | } 111 | mr_mip->ku=mr_mip->u; 112 | mr_mip->kv=mr_mip->v; 113 | middle: 114 | forever 115 | { /* work only with most significant piece */ 116 | if (mr_mip->last) 117 | { 118 | if (mr_mip->v==0) return (mr_mip->q=(-1)); 119 | lq=MR_DIV(mr_mip->u,mr_mip->v); 120 | } 121 | else 122 | { 123 | if (((mr_mip->v+mr_mip->c)==0) || ((mr_mip->v+mr_mip->d)==0)) break; 124 | lq=MR_DIV((mr_mip->u+mr_mip->a),(mr_mip->v+mr_mip->c)); 125 | if (lq!=MR_DIV((mr_mip->u+mr_mip->b),(mr_mip->v+mr_mip->d))) break; 126 | } 127 | if (lq>=(mr_small)(MR_TOOBIG/mr_abs(mr_mip->d))) break; 128 | 129 | mr_mip->q=(int)lq; 130 | mr_mip->r=mr_mip->a-mr_mip->q*mr_mip->c; 131 | mr_mip->a=mr_mip->c; 132 | mr_mip->c=mr_mip->r; 133 | mr_mip->r=mr_mip->b-mr_mip->q*mr_mip->d; 134 | mr_mip->b=mr_mip->d; 135 | mr_mip->d=mr_mip->r; 136 | lr=mr_mip->u-lq*mr_mip->v; 137 | mr_mip->u=mr_mip->v; 138 | mr_mip->v=lr; 139 | return mr_mip->q; 140 | } 141 | mr_mip->carryon=FALSE; 142 | if (mr_mip->b==0) 143 | { /* update w5 and w6 */ 144 | mr_mip->check=OFF; 145 | divide(_MIPP_ mr_mip->w5,mr_mip->w6,mr_mip->w7); 146 | mr_mip->check=ON; 147 | if (mr_lent(mr_mip->w7)>mr_mip->nib) return (mr_mip->q=(-2)); 148 | t=mr_mip->w5,mr_mip->w5=mr_mip->w6,mr_mip->w6=t; /* swap(w5,w6) */ 149 | copy(mr_mip->w7,x); 150 | return (mr_mip->q=size(x)); 151 | } 152 | else 153 | { 154 | mr_mip->check=OFF; 155 | premult(_MIPP_ mr_mip->w5,mr_mip->c,mr_mip->w7); 156 | premult(_MIPP_ mr_mip->w5,mr_mip->a,mr_mip->w5); 157 | premult(_MIPP_ mr_mip->w6,mr_mip->b,mr_mip->w0); 158 | premult(_MIPP_ mr_mip->w6,mr_mip->d,mr_mip->w6); 159 | add(_MIPP_ mr_mip->w5,mr_mip->w0,mr_mip->w5); 160 | add(_MIPP_ mr_mip->w6,mr_mip->w7,mr_mip->w6); 161 | mr_mip->check=ON; 162 | } 163 | goto start; 164 | } 165 | 166 | 167 | void mround(_MIPD_ big num,big den,flash z) 168 | { /* reduces and rounds the fraction num/den into z */ 169 | int s; 170 | #ifdef MR_OS_THREADS 171 | miracl *mr_mip=get_mip(); 172 | #endif 173 | if (mr_mip->ERNUM) return; 174 | if (size(num)==0) 175 | { 176 | zero(z); 177 | return; 178 | } 179 | 180 | MR_IN(34) 181 | 182 | if (size(den)==0) 183 | { 184 | mr_berror(_MIPP_ MR_ERR_FLASH_OVERFLOW); 185 | MR_OUT 186 | return; 187 | } 188 | copy(num,mr_mip->w5); 189 | copy(den,mr_mip->w6); 190 | s=exsign(mr_mip->w5)*exsign(mr_mip->w6); 191 | insign(PLUS,mr_mip->w5); 192 | insign(PLUS,mr_mip->w6); 193 | if (mr_compare(mr_mip->w5,mr_mip->w6)==0) 194 | { 195 | convert(_MIPP_ s,z); 196 | MR_OUT 197 | return; 198 | } 199 | if (size(mr_mip->w6)==1) 200 | { 201 | if ((int)mr_mip->w5->len>mr_mip->nib) 202 | { 203 | mr_berror(_MIPP_ MR_ERR_FLASH_OVERFLOW); 204 | MR_OUT 205 | return; 206 | } 207 | copy(mr_mip->w5,z); 208 | insign(s,z); 209 | MR_OUT 210 | return; 211 | } 212 | build(_MIPP_ z,euclid); 213 | insign(s,z); 214 | MR_OUT 215 | } 216 | 217 | #endif 218 | 219 | -------------------------------------------------------------------------------- /source/mrshs.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * Implementation of the Secure Hashing Standard (SHS) 37 | * specified for use with the NIST Digital Signature Standard (DSS) 38 | * 39 | * Generates a 160 bit message digest. It should be impossible to come 40 | * come up with two messages that hash to the same value ("collision free"). 41 | * 42 | * For use with byte-oriented messages only. Could/Should be speeded 43 | * up by unwinding loops in shs_transform(), and assembly patches. 44 | */ 45 | 46 | #include "miracl.h" 47 | /* for definition of mr_unsign32 & prototypes */ 48 | #define FIX 49 | 50 | /* Include this #define in order to implement the 51 | rather mysterious 'fix' to SHS 52 | 53 | With this definition in, SHA-1 is implemented 54 | Without this definition, SHA-0 is implemented 55 | */ 56 | 57 | 58 | #define H0 0x67452301L 59 | #define H1 0xefcdab89L 60 | #define H2 0x98badcfeL 61 | #define H3 0x10325476L 62 | #define H4 0xc3d2e1f0L 63 | 64 | #define K0 0x5a827999L 65 | #define K1 0x6ed9eba1L 66 | #define K2 0x8f1bbcdcL 67 | #define K3 0xca62c1d6L 68 | 69 | #define PAD 0x80 70 | #define ZERO 0 71 | 72 | /* functions */ 73 | 74 | #define S(n,x) (((x)<>(32-n))) 75 | 76 | #define F0(x,y,z) (z^(x&(y^z))) 77 | #define F1(x,y,z) (x^y^z) 78 | #define F2(x,y,z) ((x&y) | (z&(x|y))) 79 | #define F3(x,y,z) (x^y^z) 80 | 81 | static void shs_transform(sha *sh) 82 | { /* basic transformation step */ 83 | mr_unsign32 a,b,c,d,e,temp; 84 | int t; 85 | #ifdef FIX 86 | for (t=16;t<80;t++) sh->w[t]=S(1,sh->w[t-3]^sh->w[t-8]^sh->w[t-14]^sh->w[t-16]); 87 | #else 88 | for (t=16;t<80;t++) sh->w[t]=sh->w[t-3]^sh->w[t-8]^sh->w[t-14]^sh->w[t-16]; 89 | #endif 90 | a=sh->h[0]; b=sh->h[1]; c=sh->h[2]; d=sh->h[3]; e=sh->h[4]; 91 | for (t=0;t<20;t++) 92 | { /* 20 times - mush it up */ 93 | temp=K0+F0(b,c,d)+S(5,a)+e+sh->w[t]; 94 | e=d; d=c; 95 | c=S(30,b); 96 | b=a; a=temp; 97 | } 98 | for (t=20;t<40;t++) 99 | { /* 20 more times - mush it up */ 100 | temp=K1+F1(b,c,d)+S(5,a)+e+sh->w[t]; 101 | e=d; d=c; 102 | c=S(30,b); 103 | b=a; a=temp; 104 | } 105 | for (t=40;t<60;t++) 106 | { /* 20 more times - mush it up */ 107 | temp=K2+F2(b,c,d)+S(5,a)+e+sh->w[t]; 108 | e=d; d=c; 109 | c=S(30,b); 110 | b=a; a=temp; 111 | } 112 | for (t=60;t<80;t++) 113 | { /* 20 more times - mush it up */ 114 | temp=K3+F3(b,c,d)+S(5,a)+e+sh->w[t]; 115 | e=d; d=c; 116 | c=S(30,b); 117 | b=a; a=temp; 118 | } 119 | sh->h[0]+=a; sh->h[1]+=b; sh->h[2]+=c; 120 | sh->h[3]+=d; sh->h[4]+=e; 121 | } 122 | 123 | void shs_init(sha *sh) 124 | { /* re-initialise */ 125 | int i; 126 | for (i=0;i<80;i++) sh->w[i]=0L; 127 | sh->length[0]=sh->length[1]=0L; 128 | sh->h[0]=H0; 129 | sh->h[1]=H1; 130 | sh->h[2]=H2; 131 | sh->h[3]=H3; 132 | sh->h[4]=H4; 133 | } 134 | 135 | void shs_process(sha *sh,int byte) 136 | { /* process the next message byte */ 137 | int cnt; 138 | 139 | cnt=(int)((sh->length[0]/32)%16); 140 | 141 | sh->w[cnt]<<=8; 142 | sh->w[cnt]|=(mr_unsign32)(byte&0xFF); 143 | 144 | sh->length[0]+=8; 145 | if (sh->length[0]==0L) { sh->length[1]++; sh->length[0]=0L; } 146 | if ((sh->length[0]%512)==0) shs_transform(sh); 147 | } 148 | 149 | void shs_hash(sha *sh,char hash[20]) 150 | { /* pad message and finish - supply digest */ 151 | int i; 152 | mr_unsign32 len0,len1; 153 | len0=sh->length[0]; 154 | len1=sh->length[1]; 155 | shs_process(sh,PAD); 156 | while ((sh->length[0]%512)!=448) shs_process(sh,ZERO); 157 | sh->w[14]=len1; 158 | sh->w[15]=len0; 159 | shs_transform(sh); 160 | for (i=0;i<20;i++) 161 | { /* convert to bytes */ 162 | hash[i]=(char)((sh->h[i/4]>>(8*(3-i%4))) & 0xffL); 163 | } 164 | shs_init(sh); 165 | } 166 | 167 | /* test program: should produce digest 168 | 169 | 84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1 170 | 171 | #include 172 | #include "miracl.h" 173 | 174 | char test[]="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 175 | 176 | int main() 177 | { 178 | char hash[20]; 179 | int i; 180 | sha sh; 181 | shs_init(&sh); 182 | for (i=0;test[i]!=0;i++) shs_process(&sh,test[i]); 183 | shs_hash(&sh,hash); 184 | for (i=0;i<20;i++) printf("%02x",(unsigned char)hash[i]); 185 | printf("\n"); 186 | return 0; 187 | } 188 | 189 | */ 190 | 191 | -------------------------------------------------------------------------------- /source/mrshs256.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * Implementation of the Secure Hashing Algorithm (SHA-256) 37 | * 38 | * Generates a 256 bit message digest. It should be impossible to come 39 | * come up with two messages that hash to the same value ("collision free"). 40 | * 41 | * For use with byte-oriented messages only. Could/Should be speeded 42 | * up by unwinding loops in shs_transform(), and assembly patches. 43 | */ 44 | 45 | #include "miracl.h" 46 | 47 | #define H0 0x6A09E667L 48 | #define H1 0xBB67AE85L 49 | #define H2 0x3C6EF372L 50 | #define H3 0xA54FF53AL 51 | #define H4 0x510E527FL 52 | #define H5 0x9B05688CL 53 | #define H6 0x1F83D9ABL 54 | #define H7 0x5BE0CD19L 55 | 56 | static const mr_unsign32 K[64]={ 57 | 0x428a2f98L,0x71374491L,0xb5c0fbcfL,0xe9b5dba5L,0x3956c25bL,0x59f111f1L,0x923f82a4L,0xab1c5ed5L, 58 | 0xd807aa98L,0x12835b01L,0x243185beL,0x550c7dc3L,0x72be5d74L,0x80deb1feL,0x9bdc06a7L,0xc19bf174L, 59 | 0xe49b69c1L,0xefbe4786L,0x0fc19dc6L,0x240ca1ccL,0x2de92c6fL,0x4a7484aaL,0x5cb0a9dcL,0x76f988daL, 60 | 0x983e5152L,0xa831c66dL,0xb00327c8L,0xbf597fc7L,0xc6e00bf3L,0xd5a79147L,0x06ca6351L,0x14292967L, 61 | 0x27b70a85L,0x2e1b2138L,0x4d2c6dfcL,0x53380d13L,0x650a7354L,0x766a0abbL,0x81c2c92eL,0x92722c85L, 62 | 0xa2bfe8a1L,0xa81a664bL,0xc24b8b70L,0xc76c51a3L,0xd192e819L,0xd6990624L,0xf40e3585L,0x106aa070L, 63 | 0x19a4c116L,0x1e376c08L,0x2748774cL,0x34b0bcb5L,0x391c0cb3L,0x4ed8aa4aL,0x5b9cca4fL,0x682e6ff3L, 64 | 0x748f82eeL,0x78a5636fL,0x84c87814L,0x8cc70208L,0x90befffaL,0xa4506cebL,0xbef9a3f7L,0xc67178f2L}; 65 | 66 | #define PAD 0x80 67 | #define ZERO 0 68 | 69 | /* functions */ 70 | 71 | #define S(n,x) (((x)>>n) | ((x)<<(32-n))) 72 | #define R(n,x) ((x)>>n) 73 | 74 | #define Ch(x,y,z) ((x&y)^(~(x)&z)) 75 | #define Maj(x,y,z) ((x&y)^(x&z)^(y&z)) 76 | #define Sig0(x) (S(2,x)^S(13,x)^S(22,x)) 77 | #define Sig1(x) (S(6,x)^S(11,x)^S(25,x)) 78 | #define theta0(x) (S(7,x)^S(18,x)^R(3,x)) 79 | #define theta1(x) (S(17,x)^S(19,x)^R(10,x)) 80 | 81 | static void shs_transform(sha256 *sh) 82 | { /* basic transformation step */ 83 | mr_unsign32 a,b,c,d,e,f,g,h,t1,t2; 84 | int j; 85 | for (j=16;j<64;j++) 86 | sh->w[j]=theta1(sh->w[j-2])+sh->w[j-7]+theta0(sh->w[j-15])+sh->w[j-16]; 87 | 88 | a=sh->h[0]; b=sh->h[1]; c=sh->h[2]; d=sh->h[3]; 89 | e=sh->h[4]; f=sh->h[5]; g=sh->h[6]; h=sh->h[7]; 90 | 91 | for (j=0;j<64;j++) 92 | { /* 64 times - mush it up */ 93 | t1=h+Sig1(e)+Ch(e,f,g)+K[j]+sh->w[j]; 94 | t2=Sig0(a)+Maj(a,b,c); 95 | h=g; g=f; f=e; 96 | e=d+t1; 97 | d=c; 98 | c=b; 99 | b=a; 100 | a=t1+t2; 101 | } 102 | sh->h[0]+=a; sh->h[1]+=b; sh->h[2]+=c; sh->h[3]+=d; 103 | sh->h[4]+=e; sh->h[5]+=f; sh->h[6]+=g; sh->h[7]+=h; 104 | } 105 | 106 | void shs256_init(sha256 *sh) 107 | { /* re-initialise */ 108 | int i; 109 | for (i=0;i<64;i++) sh->w[i]=0L; 110 | sh->length[0]=sh->length[1]=0L; 111 | sh->h[0]=H0; 112 | sh->h[1]=H1; 113 | sh->h[2]=H2; 114 | sh->h[3]=H3; 115 | sh->h[4]=H4; 116 | sh->h[5]=H5; 117 | sh->h[6]=H6; 118 | sh->h[7]=H7; 119 | } 120 | 121 | void shs256_process(sha256 *sh,int byte) 122 | { /* process the next message byte */ 123 | int cnt; 124 | 125 | cnt=(int)((sh->length[0]/32)%16); 126 | 127 | sh->w[cnt]<<=8; 128 | sh->w[cnt]|=(mr_unsign32)(byte&0xFF); 129 | 130 | sh->length[0]+=8; 131 | if (sh->length[0]==0L) { sh->length[1]++; sh->length[0]=0L; } 132 | if ((sh->length[0]%512)==0) shs_transform(sh); 133 | } 134 | 135 | void shs256_hash(sha256 *sh,char hash[32]) 136 | { /* pad message and finish - supply digest */ 137 | int i; 138 | mr_unsign32 len0,len1; 139 | len0=sh->length[0]; 140 | len1=sh->length[1]; 141 | shs256_process(sh,PAD); 142 | while ((sh->length[0]%512)!=448) shs256_process(sh,ZERO); 143 | sh->w[14]=len1; 144 | sh->w[15]=len0; 145 | shs_transform(sh); 146 | for (i=0;i<32;i++) 147 | { /* convert to bytes */ 148 | hash[i]=(char)((sh->h[i/4]>>(8*(3-i%4))) & 0xffL); 149 | } 150 | shs256_init(sh); 151 | } 152 | 153 | /* test program: should produce digest 154 | 155 | 248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1 156 | 157 | 158 | #include 159 | #include "miracl.h" 160 | 161 | char test[]="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 162 | 163 | int main() 164 | { 165 | char hash[32]; 166 | int i; 167 | sha256 sh; 168 | shs256_init(&sh); 169 | for (i=0;test[i]!=0;i++) shs256_process(&sh,test[i]); 170 | shs256_hash(&sh,hash); 171 | for (i=0;i<32;i++) printf("%02x",(unsigned char)hash[i]); 172 | printf("\n"); 173 | return 0; 174 | } 175 | 176 | */ 177 | 178 | -------------------------------------------------------------------------------- /source/mrsmall.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL small number theoretic routines 37 | * mrsmall.c 38 | */ 39 | 40 | #include "miracl.h" 41 | 42 | #ifdef MR_FP 43 | #include 44 | #endif 45 | 46 | #ifdef MR_WIN64 47 | #include 48 | #endif 49 | 50 | 51 | mr_small smul(mr_small x,mr_small y,mr_small n) 52 | { /* returns x*y mod n */ 53 | mr_small r; 54 | 55 | #ifdef MR_ITANIUM 56 | mr_small tm; 57 | #endif 58 | #ifdef MR_WIN64 59 | mr_small tm; 60 | #endif 61 | #ifdef MR_FP 62 | mr_small dres; 63 | #endif 64 | #ifndef MR_NOFULLWIDTH 65 | if (n==0) 66 | { /* Assume n=2^MIRACL */ 67 | muldvd(x,y,(mr_small)0,&r); 68 | return r; 69 | } 70 | #endif 71 | x=MR_REMAIN(x,n); 72 | y=MR_REMAIN(y,n); 73 | muldiv(x,y,(mr_small)0,n,&r); 74 | return r; 75 | } 76 | 77 | mr_small invers(mr_small x,mr_small y) 78 | { /* returns inverse of x mod y */ 79 | mr_small r,s,q,t,p; 80 | #ifdef MR_FP 81 | mr_small dres; 82 | #endif 83 | 84 | BOOL pos; 85 | if (y!=0) x=MR_REMAIN(x,y); 86 | r=1; 87 | s=0; 88 | p=y; 89 | pos=TRUE; 90 | #ifndef MR_NOFULLWIDTH 91 | if (p==0) 92 | { /* if modulus is 0, assume its actually 2^MIRACL */ 93 | if (x==1) return (mr_small)1; 94 | t=r; r=s; s=t; 95 | p=x; 96 | q=muldvm((mr_small)1,(mr_small)0,p,&t); 97 | t=r+s*q; r=s; s=t; 98 | t=0-p*q; x=p; p=t; 99 | } 100 | #endif 101 | while (p!=0) 102 | { /* main euclidean loop */ 103 | q=MR_DIV(x,p); 104 | t=r+s*q; r=s; s=t; 105 | t=x-p*q; x=p; p=t; 106 | pos=!pos; 107 | } 108 | if (!pos) r=y-r; 109 | return r; 110 | } 111 | 112 | int jac(mr_small x,mr_small n) 113 | { /* finds (x/n) as (-1)^m */ 114 | int m,k,n8,u4; 115 | mr_small t; 116 | #ifdef MR_FP 117 | mr_small dres; 118 | #endif 119 | if (x==0) 120 | { 121 | if (n==1) return 1; 122 | else return 0; 123 | } 124 | if (MR_REMAIN(n,2)==0) return 0; 125 | x=MR_REMAIN(x,n); 126 | m=0; 127 | while(n>1) 128 | { /* main loop */ 129 | if (x==0) return 0; 130 | 131 | /* extract powers of 2 */ 132 | for (k=0;MR_REMAIN(x,2)==0;k++) x=MR_DIV(x,2); 133 | n8=(int)MR_REMAIN(n,8); 134 | if (k%2==1) m+=(n8*n8-1)/8; 135 | 136 | /* quadratic reciprocity */ 137 | u4=(int)MR_REMAIN(x,4); 138 | m+=(n8-1)*(u4-1)/4; 139 | t=n; t=MR_REMAIN(t,x); 140 | n=x; x=t; 141 | m%=2; 142 | } 143 | if (m==0) return 1; 144 | else return (-1); 145 | } 146 | 147 | #ifndef MR_STATIC 148 | 149 | mr_small spmd(mr_small x,mr_small n,mr_small m) 150 | { /* returns x^n mod m */ 151 | mr_small r,sx; 152 | #ifdef MR_FP 153 | mr_small dres; 154 | #endif 155 | x=MR_REMAIN(x,m); 156 | r=0; 157 | if (x==0) return r; 158 | r=1; 159 | if (n==0) return r; 160 | sx=x; 161 | forever 162 | { 163 | if (MR_REMAIN(n,2)!=0) muldiv(r,sx,(mr_small)0,m,&r); 164 | n=MR_DIV(n,2); 165 | if (n==0) return r; 166 | muldiv(sx,sx,(mr_small)0,m,&sx); 167 | } 168 | } 169 | 170 | mr_small sqrmp(mr_small x,mr_small m) 171 | { /* square root mod a small prime by Shanks method * 172 | * returns 0 if root does not exist or m not prime */ 173 | mr_small z,y,v,w,t,q; 174 | #ifdef MR_FP 175 | mr_small dres; 176 | #endif 177 | int i,e,n,r; 178 | BOOL pp; 179 | x=MR_REMAIN(x,m); 180 | if (x==0) return 0; 181 | if (x==1) return 1; 182 | if (spmd(x,(mr_small)((m-1)/2),m)!=1) return 0; /* Legendre symbol not 1 */ 183 | if (MR_REMAIN(m,4)==3) return spmd(x,(mr_small)((m+1)/4),m); /* easy case for m=4.k+3 */ 184 | if (MR_REMAIN(m,8)==5) 185 | { /* also relatively easy */ 186 | t=spmd(x,(mr_small)((m-1)/4),m); 187 | if (t==1) return spmd(x,(mr_small)((m+3)/8),m); 188 | if (t==(mr_small)(m-1)) 189 | { 190 | muldiv((mr_small)4,x,(mr_small)0,m,&t); 191 | t=spmd(t,(mr_small)((m+3)/8),m); 192 | muldiv(t,(mr_small)((m+1)/2),(mr_small)0,m,&t); 193 | return t; 194 | } 195 | return 0; 196 | } 197 | q=m-1; 198 | e=0; 199 | while (MR_REMAIN(q,2)==0) 200 | { 201 | q=MR_DIV(q,2); 202 | e++; 203 | } 204 | if (e==0) return 0; /* even m */ 205 | for (r=2;;r++) 206 | { /* find suitable z */ 207 | z=spmd((mr_small)r,q,m); 208 | if (z==1) continue; 209 | t=z; 210 | pp=FALSE; 211 | for (i=1;i=r) return 0; 229 | y=spmd(y,mr_shiftbits(1,r-n-1),m); 230 | muldiv(v,y,(mr_small)0,m,&v); 231 | muldiv(y,y,(mr_small)0,m,&y); 232 | muldiv(w,y,(mr_small)0,m,&w); 233 | r=n; 234 | } 235 | return v; 236 | } 237 | 238 | #endif 239 | -------------------------------------------------------------------------------- /source/mrsroot.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL method for modular square root 37 | * mrsroot.c 38 | * 39 | * Siguna Mueller's O(lg(p)^3) algorithm, Designs Codes and Cryptography, 2004 40 | * 41 | * This is a little slower for p=1 mod 4 primes, but its not time critical, and 42 | * more importantly it doesn't pull in the large powmod code into elliptic curve programs 43 | * It does require code from mrjack.c and mrlucas.c 44 | * 45 | * If p=3 mod 4, then sqrt(a)=a^[(p+1)/4] mod p. Note that for many elliptic curves 46 | * (p+1)/4 has very low hamming weight. 47 | * 48 | * (was sqrt(a) = V_{(p+1)/4}(a+1/a,1)/(1+1/a)) 49 | * 50 | * Mueller's method is also very simple, uses very little memory, and it works just fine for p=1 mod 8 primes 51 | * (for example the "annoying" NIST modulus 2^224-2^96+1) 52 | * Also doesn't waste time on non-squares, as a jacobi test is done first 53 | * 54 | * If you know that the prime is 3 mod 4, and you know that x is almost certainly a QR 55 | * then the jacobi-dependent code can be deleted with some space savings. 56 | * 57 | * NOTE - IF p IS NOT PRIME, THIS CODE WILL FAIL SILENTLY! 58 | * 59 | */ 60 | 61 | #include 62 | #include "miracl.h" 63 | 64 | BOOL nres_sqroot(_MIPD_ big x,big w) 65 | { /* w=sqrt(x) mod p. This depends on p being prime! */ 66 | int t,js; 67 | 68 | #ifdef MR_OS_THREADS 69 | miracl *mr_mip=get_mip(); 70 | #endif 71 | if (mr_mip->ERNUM) return FALSE; 72 | 73 | copy(x,w); 74 | if (size(w)==0) return TRUE; 75 | 76 | MR_IN(100) 77 | 78 | redc(_MIPP_ w,w); /* get it back into normal form */ 79 | 80 | if (size(w)==1) /* square root of 1 is 1 */ 81 | { 82 | nres(_MIPP_ w,w); 83 | MR_OUT 84 | return TRUE; 85 | } 86 | 87 | if (size(w)==4) /* square root of 4 is 2 */ 88 | { 89 | convert(_MIPP_ 2,w); 90 | nres(_MIPP_ w,w); 91 | MR_OUT 92 | return TRUE; 93 | } 94 | 95 | if (jack(_MIPP_ w,mr_mip->modulus)!=1) 96 | { /* Jacobi test */ 97 | zero(w); 98 | MR_OUT 99 | return FALSE; 100 | } 101 | 102 | js=mr_mip->pmod8%4-2; /* 1 mod 4 or 3 mod 4 prime? */ 103 | 104 | incr(_MIPP_ mr_mip->modulus,js,mr_mip->w10); 105 | subdiv(_MIPP_ mr_mip->w10,4,mr_mip->w10); /* (p+/-1)/4 */ 106 | 107 | if (js==1) 108 | { /* 3 mod 4 primes - do a quick and dirty sqrt(x)=x^(p+1)/4 mod p */ 109 | nres(_MIPP_ w,mr_mip->w2); 110 | copy(mr_mip->one,w); 111 | forever 112 | { /* Simple Right-to-Left exponentiation */ 113 | 114 | if (mr_mip->user!=NULL) (*mr_mip->user)(); 115 | if (subdiv(_MIPP_ mr_mip->w10,2,mr_mip->w10)!=0) 116 | nres_modmult(_MIPP_ w,mr_mip->w2,w); 117 | if (mr_mip->ERNUM || size(mr_mip->w10)==0) break; 118 | nres_modmult(_MIPP_ mr_mip->w2,mr_mip->w2,mr_mip->w2); 119 | } 120 | 121 | /* nres_moddiv(_MIPP_ mr_mip->one,w,mr_mip->w11); 122 | nres_modadd(_MIPP_ mr_mip->w11,w,mr_mip->w3); 123 | nres_lucas(_MIPP_ mr_mip->w3,mr_mip->w10,w,w); 124 | nres_modadd(_MIPP_ mr_mip->w11,mr_mip->one,mr_mip->w11); 125 | nres_moddiv(_MIPP_ w,mr_mip->w11,w); */ 126 | } 127 | else 128 | { /* 1 mod 4 primes */ 129 | for (t=1; ;t++) 130 | { /* t=1.5 on average */ 131 | if (t==1) copy(w,mr_mip->w4); 132 | else 133 | { 134 | premult(_MIPP_ w,t,mr_mip->w4); 135 | divide(_MIPP_ mr_mip->w4,mr_mip->modulus,mr_mip->modulus); 136 | premult(_MIPP_ mr_mip->w4,t,mr_mip->w4); 137 | divide(_MIPP_ mr_mip->w4,mr_mip->modulus,mr_mip->modulus); 138 | } 139 | 140 | decr(_MIPP_ mr_mip->w4,4,mr_mip->w1); 141 | if (jack(_MIPP_ mr_mip->w1,mr_mip->modulus)==js) break; 142 | if (mr_mip->ERNUM) break; 143 | } 144 | 145 | decr(_MIPP_ mr_mip->w4,2,mr_mip->w3); 146 | nres(_MIPP_ mr_mip->w3,mr_mip->w3); 147 | nres_lucas(_MIPP_ mr_mip->w3,mr_mip->w10,w,w); /* heavy lifting done here */ 148 | if (t!=1) 149 | { 150 | convert(_MIPP_ t,mr_mip->w11); 151 | nres(_MIPP_ mr_mip->w11,mr_mip->w11); 152 | nres_moddiv(_MIPP_ w,mr_mip->w11,w); 153 | } 154 | } 155 | 156 | MR_OUT 157 | return TRUE; 158 | } 159 | 160 | BOOL sqroot(_MIPD_ big x,big p,big w) 161 | { /* w = sqrt(x) mod p */ 162 | #ifdef MR_OS_THREADS 163 | miracl *mr_mip=get_mip(); 164 | #endif 165 | if (mr_mip->ERNUM) return FALSE; 166 | 167 | MR_IN(101) 168 | 169 | if (subdivisible(_MIPP_ p,2)) 170 | { /* p must be odd */ 171 | zero(w); 172 | MR_OUT 173 | return FALSE; 174 | } 175 | 176 | prepare_monty(_MIPP_ p); 177 | nres(_MIPP_ x,w); 178 | if (nres_sqroot(_MIPP_ w,w)) 179 | { 180 | redc(_MIPP_ w,w); 181 | MR_OUT 182 | return TRUE; 183 | } 184 | 185 | zero(w); 186 | MR_OUT 187 | return FALSE; 188 | } 189 | -------------------------------------------------------------------------------- /source/mrzzn2b.c: -------------------------------------------------------------------------------- 1 | 2 | /*************************************************************************** 3 | * 4 | Copyright 2013 CertiVox UK Ltd. * 5 | * 6 | This file is part of CertiVox MIRACL Crypto SDK. * 7 | * 8 | The CertiVox MIRACL Crypto SDK provides developers with an * 9 | extensive and efficient set of cryptographic functions. * 10 | For further information about its features and functionalities please * 11 | refer to http://www.certivox.com * 12 | * 13 | * The CertiVox MIRACL Crypto SDK is free software: you can * 14 | redistribute it and/or modify it under the terms of the * 15 | GNU Affero General Public License as published by the * 16 | Free Software Foundation, either version 3 of the License, * 17 | or (at your option) any later version. * 18 | * 19 | * The CertiVox MIRACL Crypto SDK is distributed in the hope * 20 | that it will be useful, but WITHOUT ANY WARRANTY; without even the * 21 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 22 | See the GNU Affero General Public License for more details. * 23 | * 24 | * You should have received a copy of the GNU Affero General Public * 25 | License along with CertiVox MIRACL Crypto SDK. * 26 | If not, see . * 27 | * 28 | You can be released from the requirements of the license by purchasing * 29 | a commercial license. Buying such a license is mandatory as soon as you * 30 | develop commercial activities involving the CertiVox MIRACL Crypto SDK * 31 | without disclosing the source code of your own applications, or shipping * 32 | the CertiVox MIRACL Crypto SDK with a closed source product. * 33 | * 34 | ***************************************************************************/ 35 | /* 36 | * MIRACL F_p^2 support functions 37 | * mrzzn2b.c 38 | */ 39 | 40 | #include 41 | #include "miracl.h" 42 | 43 | BOOL zzn2_qr(_MIPD_ zzn2 *u) 44 | { 45 | int j; 46 | #ifdef MR_OS_THREADS 47 | miracl *mr_mip=get_mip(); 48 | #endif 49 | 50 | if (mr_mip->ERNUM) return FALSE; 51 | if (zzn2_iszero(u)) return TRUE; 52 | if (size(u->b)==0) return TRUE; 53 | 54 | if (mr_mip->qnr==-1 && size(u->a)==0) return TRUE; 55 | 56 | 57 | MR_IN(203) 58 | 59 | nres_modmult(_MIPP_ u->b,u->b,mr_mip->w1); 60 | if (mr_mip->qnr==-2) nres_modadd(_MIPP_ mr_mip->w1,mr_mip->w1,mr_mip->w1); 61 | nres_modmult(_MIPP_ u->a,u->a,mr_mip->w2); 62 | nres_modadd(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w1); 63 | redc(_MIPP_ mr_mip->w1,mr_mip->w1); 64 | j=jack(_MIPP_ mr_mip->w1,mr_mip->modulus); 65 | 66 | MR_OUT 67 | if (j==1) return TRUE; 68 | return FALSE; 69 | } 70 | 71 | BOOL zzn2_sqrt(_MIPD_ zzn2 *u,zzn2 *w) 72 | { /* sqrt(a+ib) = sqrt(a+sqrt(a*a-n*b*b)/2)+ib/(2*sqrt(a+sqrt(a*a-n*b*b)/2)) 73 | where i*i=n */ 74 | #ifdef MR_OS_THREADS 75 | miracl *mr_mip=get_mip(); 76 | #endif 77 | if (mr_mip->ERNUM) return FALSE; 78 | 79 | zzn2_copy(u,w); 80 | if (zzn2_iszero(w)) return TRUE; 81 | 82 | MR_IN(204) 83 | 84 | if (size(w->b)==0) 85 | { 86 | if (!nres_sqroot(_MIPP_ w->a,mr_mip->w15)) 87 | { 88 | nres_negate(_MIPP_ w->a,w->b); 89 | zero(w->a); 90 | if (mr_mip->qnr==-2) nres_div2(_MIPP_ w->b,w->b); 91 | nres_sqroot(_MIPP_ w->b,w->b); 92 | } 93 | else 94 | copy(mr_mip->w15,w->a); 95 | 96 | MR_OUT 97 | return TRUE; 98 | } 99 | 100 | if (mr_mip->qnr==-1 && size(w->a)==0) 101 | { 102 | nres_div2(_MIPP_ w->b,w->b); 103 | if (nres_sqroot(_MIPP_ w->b,mr_mip->w15)) 104 | { 105 | copy(mr_mip->w15,w->b); 106 | copy(w->b,w->a); 107 | } 108 | else 109 | { 110 | nres_negate(_MIPP_ w->b,w->b); 111 | nres_sqroot(_MIPP_ w->b,w->b); 112 | nres_negate(_MIPP_ w->b,w->a); 113 | } 114 | 115 | MR_OUT 116 | return TRUE; 117 | } 118 | 119 | nres_modmult(_MIPP_ w->b,w->b,mr_mip->w7); 120 | if (mr_mip->qnr==-2) nres_modadd(_MIPP_ mr_mip->w7,mr_mip->w7,mr_mip->w7); 121 | nres_modmult(_MIPP_ w->a,w->a,mr_mip->w1); 122 | nres_modadd(_MIPP_ mr_mip->w7,mr_mip->w1,mr_mip->w7); 123 | 124 | if (!nres_sqroot(_MIPP_ mr_mip->w7,mr_mip->w7)) /* s=w7 */ 125 | { 126 | zzn2_zero(w); 127 | MR_OUT 128 | return FALSE; 129 | } 130 | 131 | nres_modadd(_MIPP_ w->a,mr_mip->w7,mr_mip->w15); 132 | nres_div2(_MIPP_ mr_mip->w15,mr_mip->w15); 133 | 134 | if (!nres_sqroot(_MIPP_ mr_mip->w15,mr_mip->w15)) 135 | { 136 | 137 | nres_modsub(_MIPP_ w->a,mr_mip->w7,mr_mip->w15); 138 | nres_div2(_MIPP_ mr_mip->w15,mr_mip->w15); 139 | if (!nres_sqroot(_MIPP_ mr_mip->w15,mr_mip->w15)) 140 | { 141 | zzn2_zero(w); 142 | MR_OUT 143 | return FALSE; 144 | } 145 | } 146 | 147 | copy(mr_mip->w15,w->a); 148 | nres_modadd(_MIPP_ mr_mip->w15,mr_mip->w15,mr_mip->w15); 149 | nres_moddiv(_MIPP_ w->b,mr_mip->w15,w->b); 150 | 151 | MR_OUT 152 | return TRUE; 153 | } 154 | 155 | /* y=1/x, z=1/w 156 | 157 | BOOL zzn2_double_inverse(_MIPD_ zzn2 *x,zzn2 *y,zzn2 *w,zzn2 *z) 158 | { 159 | zzn2 t1,t2; 160 | #ifdef MR_OS_THREADS 161 | miracl *mr_mip=get_mip(); 162 | #endif 163 | MR_IN(214) 164 | 165 | t1.a=mr_mip->w8; 166 | t1.b=mr_mip->w9; 167 | t2.a=mr_mip->w10; 168 | t2.b=mr_mip->w11; 169 | 170 | zzn2_mul(_MIPP_ x,w,&t1); 171 | if (zzn2_iszero(_MIPP_ &t1)) 172 | { 173 | mr_berror(_MIPP_ MR_ERR_DIV_BY_ZERO); 174 | MR_OUT 175 | return FALSE; 176 | } 177 | zzn2_inv(_MIPP_ &t1); 178 | 179 | zzn2_mul(_MIPP_ &w,&t1,&t2); 180 | zzn2_mul(_MIPP_ &x,&t1,&z); 181 | zzn2_copy(&t2,&y); 182 | 183 | MR_OUT 184 | return TRUE; 185 | 186 | } 187 | */ 188 | 189 | -------------------------------------------------------------------------------- /utils/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | $(MAKE) -C bmark 4 | $(MAKE) -C sm3 5 | 6 | clean: 7 | $(MAKE) -C bmark clean 8 | $(MAKE) -C sm3 clean 9 | -------------------------------------------------------------------------------- /utils/bmark/Makefile: -------------------------------------------------------------------------------- 1 | DIR_OBJ = ./.obj 2 | 3 | IDIR += ../../source/include/ 4 | 5 | CFLAGS += $(addprefix -I, $(IDIR)) 6 | CFLAGS += $(addprefix -D, $(DEFS)) 7 | 8 | LIBA := ../../.obj/libgmsdf.a 9 | LIBS := pthread dl 10 | 11 | LDFLAGS := $(addprefix -L, $(LDIR)) 12 | LDFLAGS += $(addprefix -l, $(LIBS)) 13 | 14 | OBJ = $(patsubst %.c,${DIR_OBJ}/%.o,$(notdir $(wildcard *.c))) 15 | 16 | .PHONY: all 17 | 18 | 19 | all: $(OBJ) 20 | $(CC) $(LDFLAGS) $(OBJ) $(LIBA) -o bmark 21 | 22 | ${DIR_OBJ}/%.o:%.c 23 | test -d $(DIR_OBJ) || mkdir -p $(DIR_OBJ) 24 | $(CC) $(CFLAGS) -c $< -o $@ 25 | 26 | clean: 27 | $(RM) $(DIR_OBJ)/* *.o 28 | -------------------------------------------------------------------------------- /utils/bmark/bmark: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/bmark/bmark -------------------------------------------------------------------------------- /utils/sm2/.obj/kdf.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/.obj/kdf.o -------------------------------------------------------------------------------- /utils/sm2/.obj/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/.obj/main.o -------------------------------------------------------------------------------- /utils/sm2/.obj/sm2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/.obj/sm2.o -------------------------------------------------------------------------------- /utils/sm2/.obj/sm2_enc.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/.obj/sm2_enc.o -------------------------------------------------------------------------------- /utils/sm2/.obj/sm2_key_ex.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/.obj/sm2_key_ex.o -------------------------------------------------------------------------------- /utils/sm2/.obj/sm2_sv.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/.obj/sm2_sv.o -------------------------------------------------------------------------------- /utils/sm2/.obj/sm3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/.obj/sm3.o -------------------------------------------------------------------------------- /utils/sm2/Makefile: -------------------------------------------------------------------------------- 1 | DIR_OBJ = ./.obj 2 | 3 | IDIR += ../../source/include/ ./ 4 | 5 | CFLAGS += $(addprefix -I, $(IDIR)) 6 | CFLAGS += $(addprefix -D, $(DEFS)) 7 | 8 | LIBA := ../../.obj/libgmsdf.a 9 | LIBS := pthread dl 10 | 11 | LDFLAGS := $(addprefix -L, $(LDIR)) 12 | LDFLAGS += $(addprefix -l, $(LIBS)) 13 | 14 | OBJ = $(patsubst %.c,${DIR_OBJ}/%.o,$(notdir $(wildcard *.c))) 15 | 16 | .PHONY: all 17 | 18 | 19 | all: $(OBJ) 20 | $(CC) $(LDFLAGS) $(OBJ) $(LIBA) -o sm2 21 | 22 | ${DIR_OBJ}/%.o:%.c 23 | test -d $(DIR_OBJ) || mkdir -p $(DIR_OBJ) 24 | $(CC) $(CFLAGS) -c $< -o $@ 25 | 26 | clean: 27 | $(RM) $(DIR_OBJ)/* *.o 28 | -------------------------------------------------------------------------------- /utils/sm2/kdf.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/kdf.c -------------------------------------------------------------------------------- /utils/sm2/kdf.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "miracl.h" 4 | #include "sm3.h" 5 | 6 | #define SM2_WORDSIZE 8 7 | #define SM2_NUMBITS 256 8 | #define SM2_NUMWORD (SM2_NUMBITS/SM2_WORDSIZE) //32 9 | #define ERR_ECURVE_INIT 0x00000001 10 | #define ERR_NOT_VALID_POINT 0x00000003 11 | #define ERR_ORDER 0x00000004 12 | #define ERR_GENERATE_R 0x00000006 13 | #define ERR_GENERATE_S 0x00000007 14 | #define ERR_OUTRANGE_R 0x00000008 15 | #define ERR_OUTRANGE_S 0x00000009 16 | #define ERR_GENERATE_T 0x0000000A 17 | #define ERR_PUBKEY_INIT 0x0000000B 18 | #define ERR_DATA_MEMCMP 0x0000000C 19 | 20 | #define ERR_INFINITY_POINT 0x00000001 21 | #define ERR_NOT_VALID_ELEMENT 0x00000002 22 | #define ERR_NOT_VALID_POINT 0x00000003 23 | #define ERR_ORDER 0x00000004 24 | #define ERR_ARRAY_NULL 0x00000005 25 | #define ERR_C3_MATCH 0x00000006 26 | #define ERR_SELFTEST_KG 0x00000008 27 | #define ERR_SELFTEST_ENC 0x00000009 28 | #define ERR_SELFTEST_DEC 0x0000000A 29 | 30 | 31 | extern unsigned char SM2_p[32]; 32 | extern unsigned char SM2_a[32]; 33 | extern unsigned char SM2_b[32]; 34 | extern unsigned char SM2_Gx[32]; 35 | extern unsigned char SM2_Gy[32]; 36 | extern unsigned char SM2_n[32]; 37 | 38 | void SM3_KDF(unsigned char *Z ,unsigned short zlen,unsigned short klen,unsigned char *K); 39 | 40 | extern epoint *G,*nG; 41 | extern big para_p,para_a,para_b,para_n,para_Gx,para_Gy,para_h; 42 | extern miracl *mip; 43 | 44 | int SM2_Init(); 45 | int Test_Null(unsigned char array[],int len); 46 | int Test_Point(epoint* point); 47 | int Test_PubKey(epoint *pubKey); 48 | int Test_Zero(big x); 49 | int Test_n(big x); 50 | int Test_Range(big x); 51 | 52 | int SM2_Encrypt(unsigned char* randK,epoint *pubKey,unsigned char M[],int klen,unsigned char C[]); 53 | int SM2_Decrypt(big dB,unsigned char C[],int Clen,unsigned char M[]); 54 | int SM2_ENC_SelfTest(); 55 | 56 | int SM2_KeyGeneration_enc(big priKey,epoint *pubKey); 57 | int SM2_KeyGeneration(unsigned char PriKey[],unsigned char Px[],unsigned char Py[]); 58 | int SM2_Sign(unsigned char *message,int len,unsigned char ZA[],unsigned char rand[],unsigned char d[],unsigned char R[],unsigned char S[]); 59 | int SM2_Verify(unsigned char *message,int len,unsigned char ZA[],unsigned char Px[],unsigned char Py[],unsigned char R[],unsigned char S[]); 60 | int SM2_SelfCheck(); 61 | -------------------------------------------------------------------------------- /utils/sm2/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | extern int SM2_ENC_SelfTest(); 7 | extern int SM2_SelfCheck(); 8 | extern int SM3_SelfTest(); 9 | 10 | int main() 11 | { 12 | SM2_ENC_SelfTest(); 13 | SM2_SelfCheck(); 14 | SM3_SelfTest(); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /utils/sm2/sm2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/sm2 -------------------------------------------------------------------------------- /utils/sm2/sm2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm2/sm2.c -------------------------------------------------------------------------------- /utils/sm2/sm2_enc.c: -------------------------------------------------------------------------------- 1 | 2 | #include "miracl.h" 3 | #include "mirdef.h" 4 | #include "kdf.h" 5 | 6 | 7 | int SM2_KeyGeneration_enc(big priKey,epoint *pubKey) 8 | { 9 | int i=0; 10 | big x,y; 11 | x=mirvar(0); 12 | y=mirvar(0); 13 | ecurve_mult(priKey,G,pubKey);//通过大数和基点产生公钥 14 | epoint_get(pubKey,x,y); 15 | if(Test_PubKey(pubKey)!=0) 16 | return 1; 17 | else 18 | return 0; 19 | } 20 | 21 | int SM2_Encrypt(unsigned char* randK,epoint *pubKey,unsigned char M[],int klen,unsigned char C[]) 22 | { 23 | big C1x,C1y,x2,y2,rand; 24 | epoint *C1,*kP,*S; 25 | int i=0; 26 | unsigned char x2y2[SM2_NUMWORD*2]={0}; 27 | SM3_STATE md; 28 | C1x=mirvar(0); 29 | C1y=mirvar(0); 30 | x2=mirvar(0); 31 | y2=mirvar(0); 32 | rand=mirvar(0); 33 | C1=epoint_init(); 34 | kP=epoint_init(); 35 | S=epoint_init(); 36 | //Step2. calculate C1=[k]G=(rGx,rGy) 37 | bytes_to_big(SM2_NUMWORD,randK,rand); 38 | ecurve_mult(rand,G,C1); //C1=[k]G 39 | epoint_get(C1,C1x,C1y); 40 | big_to_bytes(SM2_NUMWORD,C1x,C,1); 41 | big_to_bytes(SM2_NUMWORD,C1y,C+SM2_NUMWORD,1); 42 | //Step3. test if S=[h]pubKey if the point at infinity 43 | ecurve_mult(para_h,pubKey,S);if (point_at_infinity(S))// if S is point at infinity, return error; 44 | return ERR_INFINITY_POINT; 45 | //Step4. calculate [k]PB=(x2,y2) 46 | ecurve_mult(rand,pubKey,kP); //kP=[k]P 47 | epoint_get(kP,x2,y2); 48 | //Step5. KDF(x2||y2,klen) 49 | big_to_bytes(SM2_NUMWORD,x2,x2y2,1); 50 | big_to_bytes(SM2_NUMWORD,y2,x2y2+SM2_NUMWORD,1); 51 | SM3_KDF(x2y2 ,SM2_NUMWORD*2, klen,C+SM2_NUMWORD*3); 52 | if(Test_Null(C+SM2_NUMWORD*3,klen)!=0) 53 | return ERR_ARRAY_NULL; 54 | //Step6. C2=M^t 55 | for(i=0;iIOBASE=16; 151 | x=mirvar(0); 152 | y=mirvar(0); 153 | ks=mirvar(0); 154 | kG=epoint_init(); 155 | bytes_to_big(32,std_priKey,ks); //ks is the standard private key 156 | //initiate SM2 curve 157 | SM2_Init(); 158 | //generate key pair 159 | tmp=SM2_KeyGeneration_enc(ks,kG); 160 | if (tmp!=0) 161 | return tmp; 162 | 163 | 164 | epoint_get(kG,x,y); 165 | big_to_bytes(SM2_NUMWORD,x,kGxy,1); 166 | big_to_bytes(SM2_NUMWORD,y,kGxy+SM2_NUMWORD,1); 167 | if(memcmp(kGxy,std_pubKey,SM2_NUMWORD*2)!=0) 168 | return ERR_SELFTEST_KG; 169 | 170 | //encrypt data and compare the result with the standard data 171 | tmp=SM2_Encrypt(std_rand,kG,std_Message,19,Cipher); 172 | if(tmp!=0) 173 | return tmp; 174 | 175 | if(memcmp(Cipher,std_Cipher,19+SM2_NUMWORD*3)!=0) 176 | return ERR_SELFTEST_ENC; 177 | 178 | //decrypt cipher and compare the result with the standard data 179 | tmp=SM2_Decrypt(ks,Cipher,115,M); 180 | if(tmp!=0) 181 | return tmp; 182 | 183 | if(memcmp(M,std_Message,19)!=0){ 184 | printf("sm2 env err \n"); 185 | return ERR_SELFTEST_DEC; 186 | }else 187 | printf(" sm2 env ok \n"); 188 | return 0; 189 | } 190 | -------------------------------------------------------------------------------- /utils/sm2/sm2_key_ex.h: -------------------------------------------------------------------------------- 1 | #include "miracl.h" 2 | #include "mirdef.h" 3 | 4 | #define ERR_INFINITY_POINT 0x00000001 5 | #define ERR_NOT_VALID_ELEMENT 0x00000002 6 | #define ERR_NOT_VALID_POINT 0x00000003 7 | #define ERR_ORDER 0x00000004 8 | #define ERR_KEYEX_RA 0x00000006 9 | #define ERR_KEYEX_RB 0x00000007 10 | #define ERR_EQUAL_S1SB 0x00000008 11 | #define ERR_EQUAL_S2SA 0x00000009 12 | #define ERR_SELFTEST_Z 0x0000000A 13 | #define ERR_SELFTEST_INI_I 0x0000000B 14 | #define ERR_SELFTEST_RES_I 0x0000000C 15 | #define ERR_SELFTEST_INI_II 0x0000000D 16 | 17 | int SM2_W(big n); 18 | void SM3_Z(unsigned char ID[], unsigned short int ELAN, epoint* pubKey, unsigned char hash[]); 19 | 20 | int SM2_KeyEx_Init_I(big ra, epoint* RA); 21 | int SM2_KeyEx_Re_I(big rb, big dB, epoint* RA, epoint* PA, unsigned char ZA[],unsigned char ZB[], 22 | unsigned char K[],int klen,epoint* RB, epoint* V,unsigned char hash[]); 23 | int SM2_KeyEx_Init_II(big ra, big dA, epoint* RA,epoint* RB, epoint* PB, unsigned char ZA[], 24 | unsigned char ZB[],unsigned char SB[],unsigned char K[],int klen,unsigned char SA[]); 25 | int SM2_KeyEx_Re_II(epoint *V,epoint *RA,epoint *RB,unsigned char ZA[],unsigned char ZB[],unsigned char SA[]); 26 | int SM2_KeyEx_SelfTest(); 27 | -------------------------------------------------------------------------------- /utils/sm2/sm3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "sm3.h" 6 | 7 | void BiToW(unsigned int Bi[], unsigned int W[]) 8 | { 9 | int i; 10 | unsigned int tmp; 11 | for(i=0;i<=15;i++) 12 | { 13 | W[i]=Bi[i]; 14 | } 15 | for(i=16;i<=67;i++) 16 | { 17 | tmp=W[i-16] 18 | ^ W[i-9] 19 | ^ SM3_rotl32(W[i-3],15); 20 | W[i]=SM3_p1(tmp) 21 | ^ (SM3_rotl32(W[i-13],7)) 22 | ^ W[i-6]; 23 | } 24 | } 25 | 26 | void WToW1(unsigned int W[], unsigned int W1[]) 27 | { 28 | int i; 29 | for(i=0;i<=63;i++) 30 | { 31 | W1[i]=W[i]^W[i+4]; 32 | } 33 | } 34 | 35 | void CF(unsigned int W[], unsigned int W1[], unsigned int V[]) 36 | { 37 | unsigned int SS1; 38 | unsigned int SS2; 39 | unsigned int TT1; 40 | unsigned int TT2; 41 | unsigned int A,B,C,D,E,F,G,H; 42 | unsigned int T=SM3_T1; 43 | unsigned int FF; 44 | unsigned int GG; 45 | int j; 46 | //reg init,set ABCDEFGH=V0 47 | A=V[0]; 48 | B=V[1]; 49 | C=V[2]; 50 | D=V[3]; 51 | E=V[4]; 52 | F=V[5]; 53 | G=V[6]; 54 | H=V[7]; 55 | for(j=0;j<=63;j++) 56 | { 57 | //SS1 58 | if(j==0) 59 | { 60 | T=SM3_T1; 61 | } 62 | else if(j==16) 63 | { 64 | T=SM3_rotl32(SM3_T2,16); 65 | } 66 | else 67 | { 68 | T=SM3_rotl32(T,1); 69 | }SS1=SM3_rotl32((SM3_rotl32(A,12)+E+T),7); 70 | //SS2 71 | SS2=SS1^SM3_rotl32(A,12); 72 | //TT1 73 | if(j<=15) 74 | { 75 | FF=SM3_ff0(A,B,C); 76 | } 77 | else 78 | { 79 | FF=SM3_ff1(A,B,C); 80 | } 81 | TT1=FF+D+SS2+*W1; 82 | W1++; 83 | //TT2 84 | if(j<=15) 85 | { 86 | GG=SM3_gg0(E,F,G); 87 | } 88 | else 89 | { 90 | GG=SM3_gg1(E,F,G); 91 | } 92 | TT2=GG+H+SS1+*W; 93 | W++; 94 | //D 95 | D=C; 96 | //C 97 | C=SM3_rotl32(B,9); 98 | //B 99 | B=A; 100 | //A 101 | A=TT1; 102 | //H 103 | H=G;//G 104 | G=SM3_rotl32(F,19); 105 | //F 106 | F=E; 107 | //E 108 | E=SM3_p0(TT2); 109 | } 110 | //update V 111 | V[0]=A^V[0]; 112 | V[1]=B^V[1]; 113 | V[2]=C^V[2]; 114 | V[3]=D^V[3]; 115 | V[4]=E^V[4]; 116 | V[5]=F^V[5]; 117 | V[6]=G^V[6]; 118 | V[7]=H^V[7]; 119 | } 120 | 121 | void BigEndian(unsigned char src[], unsigned int bytelen, unsigned char des[]) 122 | { 123 | unsigned char tmp = 0; 124 | unsigned int i = 0; 125 | for(i=0; icurlen = md->length = 0; 138 | md->state[0] = SM3_IVA; 139 | md->state[1] = SM3_IVB; 140 | md->state[2] = SM3_IVC; 141 | md->state[3] = SM3_IVD; 142 | md->state[4] = SM3_IVE; 143 | md->state[5] = SM3_IVF; 144 | md->state[6] = SM3_IVG; 145 | md->state[7] = SM3_IVH; 146 | } 147 | 148 | void SM3_compress(SM3_STATE * md) 149 | { 150 | unsigned int W[68]; 151 | unsigned int W1[64]; 152 | //if CPU uses little-endian, BigEndian function is a necessary call 153 | BigEndian(md->buf, 64, md->buf); 154 | BiToW((unsigned int *)md->buf,W); 155 | WToW1(W,W1); 156 | CF(W, W1, md->state); 157 | } 158 | 159 | void SM3_process(SM3_STATE * md, unsigned char *buf, int len) 160 | { 161 | while (len--) 162 | { 163 | /* copy byte */ 164 | md->buf[md->curlen] = *buf++; 165 | md->curlen++; 166 | /* is 64 bytes full? */ 167 | if (md->curlen == 64) 168 | { 169 | SM3_compress(md); 170 | md->length += 512; 171 | md->curlen = 0; 172 | }} 173 | } 174 | 175 | void SM3_done(SM3_STATE *md, unsigned char hash[]) 176 | { 177 | int i; 178 | unsigned char tmp = 0; 179 | /* increase the bit length of the message */ 180 | md->length += md->curlen <<3; 181 | /* append the '1' bit */ 182 | md->buf[md->curlen] = 0x80; 183 | md->curlen++; 184 | /* if the length is currently above 56 bytes, appends zeros till 185 | it reaches 64 bytes, compress the current block, creat a new 186 | block by appending zeros and length,and then compress it 187 | */ 188 | if (md->curlen >56) 189 | { 190 | for (; md->curlen < 64;) 191 | { 192 | md->buf[md->curlen] = 0; 193 | md->curlen++; 194 | } 195 | SM3_compress(md); 196 | md->curlen = 0; 197 | } 198 | /* if the length is less than 56 bytes, pad upto 56 bytes of zeroes */ 199 | for (; md->curlen < 56;) 200 | {md->buf[md->curlen] = 0; 201 | md->curlen++; 202 | } 203 | /* since all messages are under 2^32 bits we mark the top bits zero */ 204 | for (i = 56; i < 60; i++) 205 | { 206 | md->buf[i] = 0; 207 | } 208 | /* append length */ 209 | md->buf[63] = md->length & 0xff; 210 | md->buf[62] = (md->length >> 8) & 0xff; 211 | md->buf[61] = (md->length >> 16) & 0xff; 212 | md->buf[60] = (md->length >> 24) & 0xff; 213 | SM3_compress(md); 214 | /* copy output */ 215 | memcpy(hash,md->state,SM3_len/8); 216 | BigEndian(hash,SM3_len/8,hash);//if CPU uses little-endian, BigEndian function is a necessary call 217 | } 218 | 219 | void SM3_256(unsigned char buf[], int len, unsigned char hash[]) 220 | { 221 | SM3_STATE md; 222 | SM3_init(&md); 223 | SM3_process(&md, buf, len); 224 | SM3_done(&md, hash); 225 | } 226 | 227 | int SM3_SelfTest() 228 | { 229 | unsigned int a=1,b=1; 230 | 231 | int MsgLen1=3; 232 | unsigned char Msg1[3]={0x61,0x62,0x63}; 233 | unsigned char MsgHash1[32]={0}; 234 | unsigned char StdHash1[32]={ 235 | 0x66,0xC7,0xF0,0xF4,0x62,0xEE,0xED,0xD9,0xD1,0xF2,0xD4,0x6B,0xDC,0x10,0xE4,0xE2, 236 | 0x41,0x67,0xC4,0x87,0x5C,0xF2,0xF7,0xA2,0x29,0x7D,0xA0,0x2B,0x8F,0x4B,0xA8,0xE0}; 237 | 238 | int MsgLen2=64; 239 | unsigned char Msg2[64]={ 240 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64, 241 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64, 242 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64, 243 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64}; 244 | unsigned char MsgHash2[32]={0}; 245 | unsigned char StdHash2[32]={ 246 | 0xde,0xbe,0x9f,0xf9,0x22,0x75,0xb8,0xa1,0x38,0x60,0x48,0x89,0xc1,0x8e,0x5a,0x4d, 247 | 0x6f,0xdb,0x70,0xe5,0x38,0x7e,0x57,0x65,0x29,0x3d,0xcb,0xa3,0x9c,0x0c,0x57,0x32}; 248 | 249 | SM3_256(Msg1,MsgLen1,MsgHash1); 250 | SM3_256(Msg2,MsgLen2,MsgHash2); 251 | 252 | a=memcmp(MsgHash1,StdHash1,SM3_len/8); 253 | b=memcmp(MsgHash2,StdHash2,SM3_len/8); 254 | 255 | if ((a==0) && (b==0)) 256 | { 257 | printf("sm3 ok \n"); 258 | return 0; 259 | } 260 | else 261 | { 262 | printf(" err \n"); 263 | return 1; 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /utils/sm2/sm3.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define SM3_len 256 5 | #define SM3_T1 0x79CC4519 6 | #define SM3_T2 0x7A879D8A 7 | #define SM3_IVA 0x7380166f 8 | #define SM3_IVB 0x4914b2b9 9 | #define SM3_IVC 0x172442d7 10 | #define SM3_IVD 0xda8a0600 11 | #define SM3_IVE 0xa96f30bc 12 | #define SM3_IVF 0x163138aa 13 | #define SM3_IVG 0xe38dee4d 14 | #define SM3_IVH 0xb0fb0e4e 15 | 16 | /* Various logical functions */ 17 | #define SM3_p1(x) (x^SM3_rotl32(x,15)^SM3_rotl32(x,23)) 18 | #define SM3_p0(x) (x^SM3_rotl32(x,9)^SM3_rotl32(x,17)) 19 | #define SM3_ff0(a,b,c) (a^b^c) 20 | #define SM3_ff1(a,b,c) ((a&b)|(a&c)|(b&c)) 21 | #define SM3_gg0(e,f,g) (e^f^g) 22 | #define SM3_gg1(e,f,g) ((e&f)|((~e)&g)) 23 | #define SM3_rotl32(x,n) ((((unsigned int) x) << n) | (((unsigned int) x) >> (32 - n))) 24 | #define SM3_rotr32(x,n) ((((unsigned int) x) >> n) | (((unsigned int) x) << (32 - n))) 25 | 26 | typedef struct { 27 | unsigned int state[8]; 28 | unsigned int length; 29 | unsigned int curlen; 30 | unsigned char buf[64]; 31 | } SM3_STATE; 32 | 33 | void BiToWj(unsigned int Bi[], unsigned int Wj[]); 34 | void WjToWj1(unsigned int Wj[], unsigned int Wj1[]); 35 | void CF(unsigned int Wj[], unsigned int Wj1[], unsigned int V[]); 36 | void BigEndian(unsigned char src[], unsigned int bytelen, unsigned char des[]); 37 | void SM3_init(SM3_STATE *md); 38 | void SM3_compress(SM3_STATE * md); 39 | void SM3_process(SM3_STATE * md, unsigned char buf[], int len); 40 | void SM3_done(SM3_STATE *md, unsigned char *hash); 41 | void SM3_256(unsigned char buf[], int len, unsigned char hash[]); 42 | int SM3_SelfTest(); 43 | -------------------------------------------------------------------------------- /utils/sm3/Makefile: -------------------------------------------------------------------------------- 1 | DIR_OBJ = ./.obj 2 | 3 | IDIR += ../../source/include/ ./ 4 | 5 | CFLAGS += $(addprefix -I, $(IDIR)) 6 | CFLAGS += $(addprefix -D, $(DEFS)) 7 | 8 | LIBA := ../../.obj/libgmsdf.a 9 | LIBS := pthread dl 10 | 11 | LDFLAGS := $(addprefix -L, $(LDIR)) 12 | LDFLAGS += $(addprefix -l, $(LIBS)) 13 | 14 | OBJ = $(patsubst %.c,${DIR_OBJ}/%.o,$(notdir $(wildcard *.c))) 15 | 16 | .PHONY: all 17 | 18 | 19 | all: $(OBJ) 20 | $(CC) $(LDFLAGS) $(OBJ) $(LIBA) -o sm3 21 | 22 | ${DIR_OBJ}/%.o:%.c 23 | test -d $(DIR_OBJ) || mkdir -p $(DIR_OBJ) 24 | $(CC) $(CFLAGS) -c $< -o $@ 25 | 26 | clean: 27 | $(RM) $(DIR_OBJ)/* *.o 28 | -------------------------------------------------------------------------------- /utils/sm3/sm3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm3/sm3 -------------------------------------------------------------------------------- /utils/sm3/sm3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "sm3.h" 6 | 7 | void BiToW(unsigned int Bi[], unsigned int W[]) 8 | { 9 | int i; 10 | unsigned int tmp; 11 | for(i=0;i<=15;i++) 12 | { 13 | W[i]=Bi[i]; 14 | } 15 | for(i=16;i<=67;i++) 16 | { 17 | tmp=W[i-16] 18 | ^ W[i-9] 19 | ^ SM3_rotl32(W[i-3],15); 20 | W[i]=SM3_p1(tmp) 21 | ^ (SM3_rotl32(W[i-13],7)) 22 | ^ W[i-6]; 23 | } 24 | } 25 | 26 | void WToW1(unsigned int W[], unsigned int W1[]) 27 | { 28 | int i; 29 | for(i=0;i<=63;i++) 30 | { 31 | W1[i]=W[i]^W[i+4]; 32 | } 33 | } 34 | 35 | void CF(unsigned int W[], unsigned int W1[], unsigned int V[]) 36 | { 37 | unsigned int SS1; 38 | unsigned int SS2; 39 | unsigned int TT1; 40 | unsigned int TT2; 41 | unsigned int A,B,C,D,E,F,G,H; 42 | unsigned int T=SM3_T1; 43 | unsigned int FF; 44 | unsigned int GG; 45 | int j; 46 | //reg init,set ABCDEFGH=V0 47 | A=V[0]; 48 | B=V[1]; 49 | C=V[2]; 50 | D=V[3]; 51 | E=V[4]; 52 | F=V[5]; 53 | G=V[6]; 54 | H=V[7]; 55 | for(j=0;j<=63;j++) 56 | { 57 | //SS1 58 | if(j==0) 59 | { 60 | T=SM3_T1; 61 | } 62 | else if(j==16) 63 | { 64 | T=SM3_rotl32(SM3_T2,16); 65 | } 66 | else 67 | { 68 | T=SM3_rotl32(T,1); 69 | }SS1=SM3_rotl32((SM3_rotl32(A,12)+E+T),7); 70 | //SS2 71 | SS2=SS1^SM3_rotl32(A,12); 72 | //TT1 73 | if(j<=15) 74 | { 75 | FF=SM3_ff0(A,B,C); 76 | } 77 | else 78 | { 79 | FF=SM3_ff1(A,B,C); 80 | } 81 | TT1=FF+D+SS2+*W1; 82 | W1++; 83 | //TT2 84 | if(j<=15) 85 | { 86 | GG=SM3_gg0(E,F,G); 87 | } 88 | else 89 | { 90 | GG=SM3_gg1(E,F,G); 91 | } 92 | TT2=GG+H+SS1+*W; 93 | W++; 94 | //D 95 | D=C; 96 | //C 97 | C=SM3_rotl32(B,9); 98 | //B 99 | B=A; 100 | //A 101 | A=TT1; 102 | //H 103 | H=G;//G 104 | G=SM3_rotl32(F,19); 105 | //F 106 | F=E; 107 | //E 108 | E=SM3_p0(TT2); 109 | } 110 | //update V 111 | V[0]=A^V[0]; 112 | V[1]=B^V[1]; 113 | V[2]=C^V[2]; 114 | V[3]=D^V[3]; 115 | V[4]=E^V[4]; 116 | V[5]=F^V[5]; 117 | V[6]=G^V[6]; 118 | V[7]=H^V[7]; 119 | } 120 | 121 | void BigEndian(unsigned char src[], unsigned int bytelen, unsigned char des[]) 122 | { 123 | unsigned char tmp = 0; 124 | unsigned int i = 0; 125 | for(i=0; icurlen = md->length = 0; 138 | md->state[0] = SM3_IVA; 139 | md->state[1] = SM3_IVB; 140 | md->state[2] = SM3_IVC; 141 | md->state[3] = SM3_IVD; 142 | md->state[4] = SM3_IVE; 143 | md->state[5] = SM3_IVF; 144 | md->state[6] = SM3_IVG; 145 | md->state[7] = SM3_IVH; 146 | } 147 | 148 | void SM3_compress(SM3_STATE * md) 149 | { 150 | unsigned int W[68]; 151 | unsigned int W1[64]; 152 | //if CPU uses little-endian, BigEndian function is a necessary call 153 | BigEndian(md->buf, 64, md->buf); 154 | BiToW((unsigned int *)md->buf,W); 155 | WToW1(W,W1); 156 | CF(W, W1, md->state); 157 | } 158 | 159 | void SM3_process(SM3_STATE * md, unsigned char *buf, int len) 160 | { 161 | while (len--) 162 | { 163 | /* copy byte */ 164 | md->buf[md->curlen] = *buf++; 165 | md->curlen++; 166 | /* is 64 bytes full? */ 167 | if (md->curlen == 64) 168 | { 169 | SM3_compress(md); 170 | md->length += 512; 171 | md->curlen = 0; 172 | }} 173 | } 174 | 175 | void SM3_done(SM3_STATE *md, unsigned char hash[]) 176 | { 177 | int i; 178 | unsigned char tmp = 0; 179 | /* increase the bit length of the message */ 180 | md->length += md->curlen <<3; 181 | /* append the '1' bit */ 182 | md->buf[md->curlen] = 0x80; 183 | md->curlen++; 184 | /* if the length is currently above 56 bytes, appends zeros till 185 | it reaches 64 bytes, compress the current block, creat a new 186 | block by appending zeros and length,and then compress it 187 | */ 188 | if (md->curlen >56) 189 | { 190 | for (; md->curlen < 64;) 191 | { 192 | md->buf[md->curlen] = 0; 193 | md->curlen++; 194 | } 195 | SM3_compress(md); 196 | md->curlen = 0; 197 | } 198 | /* if the length is less than 56 bytes, pad upto 56 bytes of zeroes */ 199 | for (; md->curlen < 56;) 200 | {md->buf[md->curlen] = 0; 201 | md->curlen++; 202 | } 203 | /* since all messages are under 2^32 bits we mark the top bits zero */ 204 | for (i = 56; i < 60; i++) 205 | { 206 | md->buf[i] = 0; 207 | } 208 | /* append length */ 209 | md->buf[63] = md->length & 0xff; 210 | md->buf[62] = (md->length >> 8) & 0xff; 211 | md->buf[61] = (md->length >> 16) & 0xff; 212 | md->buf[60] = (md->length >> 24) & 0xff; 213 | SM3_compress(md); 214 | /* copy output */ 215 | memcpy(hash,md->state,SM3_len/8); 216 | BigEndian(hash,SM3_len/8,hash);//if CPU uses little-endian, BigEndian function is a necessary call 217 | } 218 | 219 | void SM3_256(unsigned char buf[], int len, unsigned char hash[]) 220 | { 221 | SM3_STATE md; 222 | SM3_init(&md); 223 | SM3_process(&md, buf, len); 224 | SM3_done(&md, hash); 225 | } 226 | 227 | int main() 228 | { 229 | unsigned int a=1,b=1; 230 | 231 | int MsgLen1=3; 232 | unsigned char Msg1[3]={0x61,0x62,0x63}; 233 | unsigned char MsgHash1[32]={0}; 234 | unsigned char StdHash1[32]={ 235 | 0x66,0xC7,0xF0,0xF4,0x62,0xEE,0xED,0xD9,0xD1,0xF2,0xD4,0x6B,0xDC,0x10,0xE4,0xE2, 236 | 0x41,0x67,0xC4,0x87,0x5C,0xF2,0xF7,0xA2,0x29,0x7D,0xA0,0x2B,0x8F,0x4B,0xA8,0xE0}; 237 | 238 | int MsgLen2=64; 239 | unsigned char Msg2[64]={ 240 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64, 241 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64, 242 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64, 243 | 0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64}; 244 | unsigned char MsgHash2[32]={0}; 245 | unsigned char StdHash2[32]={ 246 | 0xde,0xbe,0x9f,0xf9,0x22,0x75,0xb8,0xa1,0x38,0x60,0x48,0x89,0xc1,0x8e,0x5a,0x4d, 247 | 0x6f,0xdb,0x70,0xe5,0x38,0x7e,0x57,0x65,0x29,0x3d,0xcb,0xa3,0x9c,0x0c,0x57,0x32}; 248 | 249 | SM3_256(Msg1,MsgLen1,MsgHash1); 250 | SM3_256(Msg2,MsgLen2,MsgHash2); 251 | 252 | a=memcmp(MsgHash1,StdHash1,SM3_len/8); 253 | b=memcmp(MsgHash2,StdHash2,SM3_len/8); 254 | 255 | if ((a==0) && (b==0)) 256 | { 257 | printf(" ok \n"); 258 | return 0; 259 | } 260 | else 261 | { 262 | printf(" err \n"); 263 | return 1; 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /utils/sm3/sm3.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define SM3_len 256 5 | #define SM3_T1 0x79CC4519 6 | #define SM3_T2 0x7A879D8A 7 | #define SM3_IVA 0x7380166f 8 | #define SM3_IVB 0x4914b2b9 9 | #define SM3_IVC 0x172442d7 10 | #define SM3_IVD 0xda8a0600 11 | #define SM3_IVE 0xa96f30bc 12 | #define SM3_IVF 0x163138aa 13 | #define SM3_IVG 0xe38dee4d 14 | #define SM3_IVH 0xb0fb0e4e 15 | 16 | /* Various logical functions */ 17 | #define SM3_p1(x) (x^SM3_rotl32(x,15)^SM3_rotl32(x,23)) 18 | #define SM3_p0(x) (x^SM3_rotl32(x,9)^SM3_rotl32(x,17)) 19 | #define SM3_ff0(a,b,c) (a^b^c) 20 | #define SM3_ff1(a,b,c) ((a&b)|(a&c)|(b&c)) 21 | #define SM3_gg0(e,f,g) (e^f^g) 22 | #define SM3_gg1(e,f,g) ((e&f)|((~e)&g)) 23 | #define SM3_rotl32(x,n) ((((unsigned int) x) << n) | (((unsigned int) x) >> (32 - n))) 24 | #define SM3_rotr32(x,n) ((((unsigned int) x) >> n) | (((unsigned int) x) << (32 - n))) 25 | 26 | typedef struct { 27 | unsigned int state[8]; 28 | unsigned int length; 29 | unsigned int curlen; 30 | unsigned char buf[64]; 31 | } SM3_STATE; 32 | 33 | void BiToWj(unsigned int Bi[], unsigned int Wj[]); 34 | void WjToWj1(unsigned int Wj[], unsigned int Wj1[]); 35 | void CF(unsigned int Wj[], unsigned int Wj1[], unsigned int V[]); 36 | void BigEndian(unsigned char src[], unsigned int bytelen, unsigned char des[]); 37 | void SM3_init(SM3_STATE *md); 38 | void SM3_compress(SM3_STATE * md); 39 | void SM3_process(SM3_STATE * md, unsigned char buf[], int len); 40 | void SM3_done(SM3_STATE *md, unsigned char *hash); 41 | void SM3_256(unsigned char buf[], int len, unsigned char hash[]); 42 | int SM3_SelfTest(); 43 | -------------------------------------------------------------------------------- /utils/sm4/.obj/sm4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm4/.obj/sm4.o -------------------------------------------------------------------------------- /utils/sm4/.obj/sm4test.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm4/.obj/sm4test.o -------------------------------------------------------------------------------- /utils/sm4/Makefile: -------------------------------------------------------------------------------- 1 | DIR_OBJ = ./.obj 2 | 3 | IDIR += ../../source/include/ ./ 4 | 5 | CFLAGS += $(addprefix -I, $(IDIR)) 6 | CFLAGS += $(addprefix -D, $(DEFS)) 7 | 8 | LIBA := ../../.obj/libgmsdf.a 9 | LIBS := pthread dl 10 | 11 | LDFLAGS := $(addprefix -L, $(LDIR)) 12 | LDFLAGS += $(addprefix -l, $(LIBS)) 13 | 14 | OBJ = $(patsubst %.c,${DIR_OBJ}/%.o,$(notdir $(wildcard *.c))) 15 | 16 | .PHONY: all 17 | 18 | 19 | all: $(OBJ) 20 | $(CC) $(LDFLAGS) $(OBJ) $(LIBA) -o sm4 21 | 22 | ${DIR_OBJ}/%.o:%.c 23 | test -d $(DIR_OBJ) || mkdir -p $(DIR_OBJ) 24 | $(CC) $(CFLAGS) -c $< -o $@ 25 | 26 | clean: 27 | $(RM) $(DIR_OBJ)/* *.o 28 | -------------------------------------------------------------------------------- /utils/sm4/sm4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuqun/gmalg/98605f9af9a9140c8829f30caed64124f53cfa79/utils/sm4/sm4 -------------------------------------------------------------------------------- /utils/sm4/sm4.h: -------------------------------------------------------------------------------- 1 | #ifndef XYSSL_SM4_H 2 | #define XYSSL_SM4_H 3 | 4 | #define SM4_ENCRYPT 1 5 | #define SM4_DECRYPT 0 6 | 7 | typedef struct 8 | { 9 | int mode; /*!< encrypt/decrypt */ 10 | unsigned long sk[32]; /*!< SM4 subkeys */ 11 | } 12 | sm4_context; 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | void sm4_setkey_enc( sm4_context *ctx, unsigned char key[16] ); 19 | void sm4_setkey_dec( sm4_context *ctx, unsigned char key[16] ); 20 | void sm4_crypt_ecb( sm4_context *ctx, 21 | int mode, 22 | int length, 23 | unsigned char *input, 24 | unsigned char *output); 25 | 26 | void sm4_crypt_cbc( sm4_context *ctx, 27 | int mode, 28 | int length, 29 | unsigned char iv[16], 30 | unsigned char *input, 31 | unsigned char *output ); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* sm4.h */ 38 | -------------------------------------------------------------------------------- /utils/sm4/sm4test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sm4.h" 4 | 5 | int main() 6 | { 7 | unsigned char key[16] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; 8 | unsigned char input[16] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; 9 | unsigned char output[16]; 10 | sm4_context ctx; 11 | unsigned long i; 12 | 13 | //encrypt standard testing vector 14 | sm4_setkey_enc(&ctx,key); 15 | sm4_crypt_ecb(&ctx,1,16,input,output); 16 | for(i=0;i<16;i++) 17 | printf("%02x ", output[i]); 18 | printf("\n"); 19 | 20 | //decrypt testing 21 | sm4_setkey_dec(&ctx,key); 22 | sm4_crypt_ecb(&ctx,0,16,output,output); 23 | for(i=0;i<16;i++) 24 | printf("%02x ", output[i]); 25 | printf("\n"); 26 | 27 | //decrypt 1M times testing vector based on standards. 28 | i = 0; 29 | sm4_setkey_enc(&ctx,key); 30 | while (i<1000000) 31 | { 32 | sm4_crypt_ecb(&ctx,1,16,input,input); 33 | i++; 34 | } 35 | for(i=0;i<16;i++) 36 | printf("%02x ", input[i]); 37 | printf("\n"); 38 | 39 | return 0; 40 | } 41 | --------------------------------------------------------------------------------