/**
* 验证GM/T 0003.5--2012 附录A 数字签名与验证示例
* GmSSL: https://github.com/guanzhi/GmSSL
* git版本:95c0dba
*/
void testSM2Prestreat() {
BIGNUM *a, *b, *xG, *yG, *xA, *yA, *zA;
a = BN_new();
b = BN_new();
xG = BN_new();
yG = BN_new();
xA = BN_new();
yA = BN_new();
zA = BN_new();
BN_hex2bn(&a,
"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC");
BN_hex2bn(&b,
"28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93");
BN_hex2bn(&xG,
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7");
BN_hex2bn(&yG,
"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0");
BN_hex2bn(&xA,
"09F9DF311E5421A150DD7D161E4BC5C672179FAD1833FC076BB08FF356F35020 ");
BN_hex2bn(&yA,
"CCEA490CE26775A52DC6EA718CC1AA600AED05FBF35E084A6632F6072DA9AD13");
unsigned char ENTL[2];
unsigned char id[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
int idLen = sizeof(id) * 8;
unsigned char aBin[32] = { 0 };
unsigned char bBin[32] = { 0 };
unsigned char xGBin[32] = { 0 };
unsigned char yGBin[32] = { 0 };
unsigned char xABin[32] = { 0 };
unsigned char yABin[32] = { 0 };
//内存中数据存储均采用大端存储
BN_bn2bin(a, aBin);
BN_bn2bin(b, bBin);
BN_bn2bin(xG, xGBin);
BN_bn2bin(yG, yGBin);
BN_bn2bin(xA, xABin);
BN_bn2bin(yA, yABin);
ENTL[0] = 0;
ENTL[1] = idLen;
FILE *fp = fopen("/tmp/sm2Prestrest1.bin", "wb");
fwrite(ENTL, 2, 1, fp);
fwrite(id, sizeof(id), 1, fp);
fwrite(aBin, sizeof(aBin), 1, fp);
fwrite(bBin, sizeof(bBin), 1, fp);
fwrite(xGBin, sizeof(xGBin), 1, fp);
fwrite(yGBin, sizeof(yGBin), 1, fp);
fwrite(xABin, sizeof(xABin), 1, fp);
fwrite(yABin, sizeof(yABin), 1, fp);
fclose(fp);
/*
[yeqiang@localhost exampleProgram]$ hexdump -C /tmp/sm2Prestrest1.bin
00000000 00 80 31 32 33 34 35 36 37 38 31 32 33 34 35 36 |..12345678123456|
00000010 37 38 ff ff ff fe ff ff ff ff ff ff ff ff ff ff |78..............|
00000020 ff ff ff ff ff ff 00 00 00 00 ff ff ff ff ff ff |................|
00000030 ff fc 28 e9 fa 9e 9d 9f 5e 34 4d 5a 9e 4b cf 65 |..(.....^4MZ.K.e|
00000040 09 a7 f3 97 89 f5 15 ab 8f 92 dd bc bd 41 4d 94 |.............AM.|
00000050 0e 93 32 c4 ae 2c 1f 19 81 19 5f 99 04 46 6a 39 |..2..,...._..Fj9|
00000060 c9 94 8f e3 0b bf f2 66 0b e1 71 5a 45 89 33 4c |.......f..qZE.3L|
00000070 74 c7 bc 37 36 a2 f4 f6 77 9c 59 bd ce e3 6b 69 |t..76...w.Y...ki|
00000080 21 53 d0 a9 87 7c c6 2a 47 40 02 df 32 e5 21 39 |!S...|.*G@..2.!9|
00000090 f0 a0 09 f9 df 31 1e 54 21 a1 50 dd 7d 16 1e 4b |.....1.T!.P.}..K|
000000a0 c5 c6 72 17 9f ad 18 33 fc 07 6b b0 8f f3 56 f3 |..r....3..k...V.|
000000b0 50 20 cc ea 49 0c e2 67 75 a5 2d c6 ea 71 8c c1 |P ..I..gu.-..q..|
000000c0 aa 60 0a ed 05 fb f3 5e 08 4a 66 32 f6 07 2d a9 |.`.....^.Jf2..-.|
000000d0 ad 13 |..|
000000d2
[yeqiang@localhost exampleProgram]$ /usr/local/gmssl/bin/gmssl dgst -sm3 -hex /tmp/sm2Prestrest1.bin
SM3(/tmp/sm2Prestrest1.bin)= b2e14c5c79c6df5b85f4fe7ed8db7a262b9da7e07ccb0ea9f4747b8ccda8a4f3
*/
//计算预处理2
unsigned char m[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x64,
0x69, 0x67, 0x65, 0x73, 0x74 };
BN_hex2bn(&zA,
"b2e14c5c79c6df5b85f4fe7ed8db7a262b9da7e07ccb0ea9f4747b8ccda8a4f3");
unsigned char zAm[32 + sizeof(m)];
BN_bn2bin(zA, zAm);
memcpy(zAm + 32, m, sizeof(m));
fp = fopen("/tmp/zAm.bin", "wb");
fwrite(zAm, sizeof(zAm), 1, fp);
fclose(fp);
/**
[yeqiang@localhost exampleProgram]$ hexdump -C /tmp/zAm.bin
00000000 b2 e1 4c 5c 79 c6 df 5b 85 f4 fe 7e d8 db 7a 26 |..L\y..[...~..z&|
00000010 2b 9d a7 e0 7c cb 0e a9 f4 74 7b 8c cd a8 a4 f3 |+...|....t{.....|
00000020 6d 65 73 73 61 67 65 20 64 69 67 65 73 74 |message digest|
0000002e
[yeqiang@localhost exampleProgram]$ /usr/local/gmssl/bin/gmssl dgst -sm3 -hex /tmp/zAm.bin
SM3(/tmp/zAm.bin)= f0b43e94ba45accaace692ed534382eb17e6ab5a19ce7b31f4486fdfc0d28640
*/
}
注意大整数在内存中采用大端存储方式
GM/T 0003.5 部分原文: