MIRACL(Multiprecision Integer and RationalArithmetic C/c++ Library)是一套由Shamus Software Ltd.所开发的一套关于大数运算函数库,用来设计与大数运算相关的密码学之应用,包含了RSA 公开密码学、Diffie-Hellman密钥交换(Key Exchange)、AES、DSA数字签名,还包含了较新的椭圆曲线密码学(Elliptic CurveCryptography)等等。运算速度快,并提供源代码。
源码:https://codeload.github.com/miracl/MIRACL/zip/refs/heads/master
下载.zip压缩包,名为MIRACL-master.zip。
在与MIRACL-master.zip同一级目录下创建program/arm、program/linux64、program/miracl
mkdir program
cd program
mkdir arm linux64 miracl
把所有独立的文件都放在这个目录,即将所有非目录的文件解压在当前目录下。
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ cd miracl
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ unzip -j -aa -L ../../MIRACL-master.zip
Archive: ../MIRACL-master.zip
b9b8fd471de253cfd6313446cb490741edb069a7
inflating: readme.md
inflating: aesgcm.txt
inflating: amd64.txt
inflating: arm.txt
inflating: blackfin.txt
inflating: borland.txt
inflating: config.c
inflating: cpp.txt
inflating: cygwin.txt
inflating: devcpp.txt
replace readme.md? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
用shell文件编译。如果是64位系统用linux64,如果是32位系统用linux。
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ uname -a
Linux ubuntu 5.4.0-87-generic #98~18.04.1-Ubuntu SMP Wed Sep 22 10:45:04 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
ms@ubuntu:~/ln_work/code/miracl/program/miracl$
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ bash linux64
rm: cannot remove '*.exe': No such file or directory
rm: cannot remove 'miracl.a': No such file or directory
ms@ubuntu:~/ln_work/code/miracl/program/miracl$
运行源码提供的示例代码 pk-demo,进行测试。一下是否编译成功。
ms@ubuntu:/ln_work/code/miracl/program/miracl$ ./pk-demo
First Diffie-Hellman Key exchange ....
Alice's offline calculation
Bob's offline calculation
Alice calculates Key=
50364662501526487864432001969919529634929523819891684175510011864189987006429341060054894763740797155348560187109217960041463451840644246746745731788188770837780761542010749543061156654257036657524769985478773580669931825928510164224348457183235567582407192633395908376343750448459570737818267250158540649830
Bob calculates Key=
50364662501526487864432001969919529634929523819891684175510011864189987006429341060054894763740797155348560187109217960041463451840644246746745731788188770837780761542010749543061156654257036657524769985478773580669931825928510164224348457183235567582407192633395908376343750448459570737818267250158540649830
Alice and Bob's keys should be the same!
Lets try that again using elliptic curves....
Alice's offline calculation
Bob's offline calculation
Alice calculates Key=
2632066098179618621622368948595011643742480207600957776883
Bob calculates Key=
2632066098179618621622368948595011643742480207600957776883
Alice and Bob's keys should be the same! (but much smaller)
Testing El Gamal's public key method
Ciphertext=
123454552143422756230533563900150788651233399595032538817871124883071467046901766055499830558996524543153875077045157120508034097819314351196564149225399024337912898659834649101888235432981157961897767603756515164628892662298134003676867489361277006252611938201975779691382773976313137920709929645942513699787
46948156442752805102970176005154158900679636966566307267261350304340237514869362397519939821534298537190482058530721553069565104280768303496307576931494182513244886658869694788003139725625101280633255336696749427433704185621192275880789592799749511930776493439589457374156206361881508502391182629552020329847
Plaintext=
MIRACL - Best multi-precision library in the World!
Now generating 512-bit random primes p and q
9463366198600718869553188160993206094090841444433901297697342503179340627347792407495881510435832792839067355725539742165525160146097241717862212800904083
6800455385726454022838570672136998046829038004426778711543784952768563443897598367317102736397164976445049562014170978730527797814789741170584951419247937
n = p.q =
64355199632375938545892135322061855181211711445387308047725665259053941806518762914695730541551715465767264386385167909481818113421912382184501565331657270848056714909739707574327964621960251759485546934837634292155268609295764838366491140677400315358439729979466194601667536553241940410367521288961532626771
Encrypting test string
Ciphertext=
162540337589097599167634066811608574971172889096342523572172515432583720745458166546201048696303603494108104756677833720985911518940050894622222117544938053753219505366612594667214347053986985765706399880568580476531823146264564935451567638071070335283688660354172047272715506553056854446232135064325655057
Decrypting test string
Plaintext=
MIRACL - Best multi-precision library in the World!
ms@ubuntu:/ln_work/code/miracl/program/miracl$
ms@ubuntu:/ln_work/code/miracl/program/linux64$ cp mirdef.h miracl.h miracl.a ../linux64/
gcc -O2 test.c -o test -I./ -L./ miracl.a
根据参考文档arm.txt,进行操作。
/*
* MIRACL compiler/hardware definitions - mirdef.h
*/
#define MIRACL 32
#define MR_LITTLE_ENDIAN
/* or possibly
#define MR_BIG_ENDIAN
*/
#define mr_utype int
#define MR_IBITS 32
#define MR_LBITS 32
#define mr_dltype long long
#define mr_unsign32 unsigned int
#define mr_unsign64 unsigned long long
#define MAXBASE ((mr_small)1<<(MIRACL-1))
#define MR_NOASM
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ cp linux64 linux-arm
参考arm.txt,拷贝相关内容,并做部分修改调整,内容如下。
rm *.exe
rm miracl.a
#cp mirdef.arm mirdef.h
arm-linux-gcc -I. -c -O2 mrcore.c
arm-linux-gcc -I. -c -O2 mrarth0.c
arm-linux-gcc -I. -c -O2 mrarth1.c
arm-linux-gcc -I. -c -O2 mrarth2.c
arm-linux-gcc -I. -c -O2 mralloc.c
arm-linux-gcc -I. -c -O2 mrsmall.c
arm-linux-gcc -I. -c -O2 mrio1.c
arm-linux-gcc -I. -c -O2 mrio2.c
arm-linux-gcc -I. -c -O2 mrgcd.c
arm-linux-gcc -I. -c -O2 mrjack.c
arm-linux-gcc -I. -c -O2 mrbits.c
arm-linux-gcc -I. -c -O2 mrxgcd.c
arm-linux-gcc -I. -c -O2 mrarth3.c
arm-linux-gcc -I. -c -O2 mrrand.c
arm-linux-gcc -I. -c -O2 mrprime.c
arm-linux-gcc -I. -c -O2 mrcrt.c
arm-linux-gcc -I. -c -O2 mrscrt.c
arm-linux-gcc -I. -c -O2 mrmonty.c
arm-linux-gcc -I. -c -O2 mrpower.c
arm-linux-gcc -I. -c -O2 mrsroot.c
arm-linux-gcc -I. -c -O2 mrcurve.c
arm-linux-gcc -I. -c -O2 mrfast.c
arm-linux-gcc -I. -c -O2 mrshs.c
arm-linux-gcc -I. -c -O2 mrshs256.c
arm-linux-gcc -I. -c -O2 mrshs512.c
arm-linux-gcc -I. -c -O2 mraes.c
arm-linux-gcc -I. -c -O2 mrgcm.c
arm-linux-gcc -I. -c -O2 mrlucas.c
arm-linux-gcc -I. -c -O2 mrstrong.c
arm-linux-gcc -I. -c -O2 mrbrick.c
arm-linux-gcc -I. -c -O2 mrebrick.c
arm-linux-gcc -I. -c -O2 mrgf2m.c
arm-linux-gcc -I. -c -O2 mrec2m.c
arm-linux-gcc -I. -c -O2 mrzzn2.c
arm-linux-gcc -I. -c -O2 mrzzn2b.c
arm-linux-gcc -I. -c -O2 mrzzn3.c
arm-linux-gcc -I. -c -O2 mrecn2.c
arm-linux-ar -rc miracl.a mrcore.o mrarth0.o mrarth1.o mrarth2.o mralloc.o mrsmall.o
arm-linux-ar -r miracl.a mrio1.o mrio2.o mrjack.o mrgcd.o mrxgcd.o mrarth3.o mrgcm.o
arm-linux-ar -r miracl.a mrrand.o mrprime.o mrcrt.o mrscrt.o mrmonty.o mrcurve.o
arm-linux-ar -r miracl.a mrfast.o mrshs.o mraes.o mrlucas.o mrstrong.o mrbrick.o
arm-linux-ar -r miracl.a mrebrick.o mrec2m.o mrgf2m.o mrpower.o mrsroot.o mrzzn2b.o
arm-linux-ar -r miracl.a mrshs256.o mrshs512.o mrbits.o mrzzn2.o mrzzn3.o mrecn2.o
rm mr*.o
arm-linux-gcc -I. -c pk-demo.c
arm-none-linux-gnueabi-ld pk-demo.o miracl.a -o pk-demo.axf
#cp mrmuldv.ccc mrmuldv.c
#gcc -c -m32 -O2 mrmuldv.c
#ar r miracl.a mrbrick.o mrebrick.o mrec2m.o mrgf2m.o mrmuldv.o mrshs512.o mrsha3.o mrfpe.o
#rm mr*.o
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ bash linux-arm
rm: cannot remove '*.exe': No such file or directory
rm: cannot remove 'miracl.a': No such file or directory
linux-arm: line 50: arm-linux-link: command not found
ms@ubuntu:~/ln_work/code/miracl/program/miracl$
arm-linux-gcc -static -o pk-demo.out pk-demo.c -I./ -L./ miracl.a
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ file pk-demo.out
pk-demo.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
ms@ubuntu:~/ln_work/code/miracl/program/miracl$
ms@ubuntu:~/ln_work/code/miracl/program/miracl$ cp mirdef.h miracl.h miracl.a ../arm/
ms@ubuntu:~/ln_work/code/miracl/program/miracl$
https://download.csdn.net/download/weixin_43782998/32971309
GitHub - guanzhi/GmSSL: 支持国密SM2/SM3/SM4/SM9/ZUC/SSL的OpenSSL分支
编译命令
gcc -O2 sm2.c sm3.c -o sm2 -I./ -L./ miracl.a
linux64下编译
gcc -O2 sm2.c sm3.c -o sm2-linux64 -I../linux64 -L./ ../linux64/miracl.a
交叉编译
ms@ubuntu:~/ln_work/code/miracl/program/sm2$ arm-linux-gcc -O2 sm2.c sm3.c -o sm2-arm -I../arm -L./ ../arm/miracl.a
ms@ubuntu:~/ln_work/code/miracl/program/sm2$ file sm2-arm
sm2-arm: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 2.6.16, not stripped
ms@ubuntu:~/ln_work/code/miracl/program/sm2$