Add sm3 demos and sm3_hmac bug fix

This commit is contained in:
Zhi Guan
2022-07-31 21:25:03 +08:00
parent 4ab269919d
commit fa7b6a6b06
7 changed files with 100 additions and 6 deletions

View File

@@ -4,5 +4,9 @@ project(sm3demo)
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
add_executable(sm3demo sm3demo.c)
target_link_libraries(sm3demo gmssl)
add_executable(sm3_demo sm3_demo.c)
add_executable(sm3_hmac_demo sm3_hmac_demo.c)
add_executable(sm3_kdf_demo sm3_kdf_demo.c)
target_link_libraries(sm3_demo gmssl)
target_link_libraries(sm3_hmac_demo gmssl)
target_link_libraries(sm3_kdf_demo gmssl)

9
demos/sm3/Makefile Normal file
View File

@@ -0,0 +1,9 @@
all:
cc sm3_demo.c -lgmssl -o sm3_demo
cc sm3_hmac_demo.c -lgmssl -o sm3_hmac_demo
cc sm3_kdf_demo.c -lgmssl -o sm3_kdf_demo
clean:
rm -fr sm3_demo
rm -fr sm3_hmac_demo
rm -fr sm3_kdf_demo

42
demos/sm3/sm3_hmac_demo.c Normal file
View File

@@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm3.h>
int main(void)
{
SM3_HMAC_CTX hmac_ctx;
unsigned char key[16] = {
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x01,0xf2,0x03,0x04,0x05,0x06,0x07,0x08,
};
unsigned char mbuf[16] = {
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
};
unsigned char hmac[32] = {0};
int i;
sm3_hmac_init(&hmac_ctx, key, sizeof(key));
sm3_hmac_update(&hmac_ctx, mbuf, sizeof(mbuf));
sm3_hmac_finish(&hmac_ctx, hmac);
printf("hmac: ");
for (i = 0; i < sizeof(hmac); i++) {
printf("%02X", hmac[i]);
}
printf("\n");
memset(hmac, 0, sizeof(hmac));
sm3_hmac(key, sizeof(key), mbuf, sizeof(mbuf), hmac);
printf("hmac: ");
for (i = 0; i < sizeof(hmac); i++) {
printf("%02X", hmac[i]);
}
printf("\n");
return 0;
}

28
demos/sm3/sm3_kdf_demo.c Normal file
View File

@@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm3.h>
int main(void)
{
SM3_KDF_CTX kdf_ctx;
unsigned char key[16] = {0};
unsigned char raw[32] = {
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
};
int i;
sm3_kdf_init(&kdf_ctx, sizeof(key));
sm3_kdf_update(&kdf_ctx, raw, sizeof(raw));
sm3_kdf_finish(&kdf_ctx, key);
printf("key: ");
for (i = 0; i < sizeof(key); i++) {
printf("%02X", key[i]);
}
printf("\n");
return 0;
}