#include "hmac_sm3.h" #include #include 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() { return hmac_sm3_test(); }