mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 00:46:17 +08:00
154 lines
3.7 KiB
C
154 lines
3.7 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "sm3.h"
|
|
|
|
int sm3_test1()
|
|
{
|
|
char *msg = "abc";
|
|
unsigned char dgst[SM3_DIGEST_LENGTH];
|
|
unsigned char result[] = {
|
|
0x66,0xc7,0xf0,0xf4,0x62,0xee,0xed,0xd9,
|
|
0xd1,0xf2,0xd4,0x6b,0xdc,0x10,0xe4,0xe2,
|
|
0x41,0x67,0xc4,0x87,0x5c,0xf2,0xf7,0xa2,
|
|
0x29,0x7d,0xa0,0x2b,0x8f,0x4b,0xa8,0xe0
|
|
};
|
|
int i;
|
|
|
|
printf("sm3 test 1\n");
|
|
memset(dgst, 0, sizeof(dgst));
|
|
sm3((unsigned char *)msg, strlen(msg), dgst);
|
|
|
|
printf(" message : %s\n", msg);
|
|
printf(" digest : 0x");
|
|
for(i = 0; i < sizeof(dgst); i++) {
|
|
printf("%02x", dgst[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
printf(" result : ");
|
|
if (memcmp(dgst, result, sizeof(result))) {
|
|
printf("failed\n");
|
|
return -1;
|
|
} else {
|
|
printf("passed\n");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int sm3_test2()
|
|
{
|
|
unsigned char msg[] = {
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
0x61,0x62,0x63,0x64,0x61,0x62,0x63,0x64,
|
|
};
|
|
|
|
unsigned char dgst[SM3_DIGEST_LENGTH];
|
|
unsigned char result[] = {
|
|
0xde,0xbe,0x9f,0xf9,0x22,0x75,0xb8,0xa1,
|
|
0x38,0x60,0x48,0x89,0xc1,0x8e,0x5a,0x4d,
|
|
0x6f,0xdb,0x70,0xe5,0x38,0x7e,0x57,0x65,
|
|
0x29,0x3d,0xcb,0xa3,0x9c,0x0c,0x57,0x32,
|
|
};
|
|
int i;
|
|
|
|
printf("sm3 test 2\n");
|
|
memset(dgst, 0, sizeof(dgst));
|
|
sm3(msg, sizeof(msg), dgst);
|
|
|
|
printf(" message : 0x");
|
|
for (i = 0; i < sizeof(msg); i++) {
|
|
printf("%02x", msg[i]);
|
|
}
|
|
printf("\n");
|
|
printf(" digest: 0x");
|
|
for (i = 0; i < sizeof(dgst); i++) {
|
|
printf("%02x", dgst[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
printf(" result : ");
|
|
if (memcmp(dgst, result, sizeof(result))) {
|
|
printf("failed\n");
|
|
return -1;
|
|
} else {
|
|
printf("passed\n");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int hmac_sm3_test()
|
|
{
|
|
int ret = 0;
|
|
int i, j;
|
|
unsigned char mac[HMAC_SM3_MAC_SIZE];
|
|
hmac_sm3_ctx_t ctx;
|
|
|
|
char *testarray[4] = {
|
|
"abc",
|
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
|
"a",
|
|
"01234567012345670123456701234567"
|
|
};
|
|
int repeatcount[4] = { 1, 1, 1000000, 20 };
|
|
unsigned char key[4] = {
|
|
"hello",
|
|
"world",
|
|
"23492304982304982340923480",
|
|
"a"
|
|
};
|
|
unsigned char result[4][32] = {
|
|
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
|
|
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
|
|
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
|
|
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
|
|
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
|
|
};
|
|
|
|
for (i = 0; i < sizeof(testarray)/sizeof(testarray[0]); i++) {
|
|
hmac_sm3_init(&ctx, key[i], key_length[i]);
|
|
for (j = 0; j < repeatcount[i]; j++) {
|
|
hmac_sm3_update(&ctx, (const unsigned char *)testarray[i],
|
|
strlen(testarray[i]));
|
|
}
|
|
hmac_sm3_final(&ctx, mac);
|
|
if (memcmp(mac, &result[i][0], sizeof(mac)) != 0) {
|
|
fprintf(stderr, "hmac-sm3 test-%d failed\n", i);
|
|
ret = 1;
|
|
}
|
|
}
|
|
|
|
if (ret == 0) {
|
|
printf("hmac-sm3 test success!\n");
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if (sm3_test1())
|
|
return -1;
|
|
|
|
if (sm3_test2())
|
|
return -2;
|
|
|
|
return 0;
|
|
}
|