Skip to content

Commit

Permalink
Merge pull request #51 from dongbeiouba/example/oneshot
Browse files Browse the repository at this point in the history
Add examples for oneshot. Add hmac-sm3 test cases.
  • Loading branch information
InfoHunter authored Dec 13, 2023
2 parents 379181d + dcd4916 commit 019c9e3
Show file tree
Hide file tree
Showing 38 changed files with 928 additions and 304 deletions.
100 changes: 94 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ include_directories(include)
set(libsrc
src/error.c
src/mem.c
src/meth.c
src/pool.c
src/version.c
)
Expand All @@ -36,6 +37,7 @@ OPTION(WITH_ASCON "Build with ASCON module" OFF)
OPTION(WITH_ASN1 "Build with asn1 module" OFF)
OPTION(WITH_DEBUG "Enable debug" OFF)
OPTION(WITH_ERRSTR "Build with error string" OFF)
OPTION(WITH_EXAMPLE "Build example program" OFF)
OPTION(WITH_HMAC "Build with HMAC module" OFF)
OPTION(WITH_LOG "Build with log module" OFF)
OPTION(WITH_OSCORE "Build with OSCORE module" OFF)
Expand All @@ -48,6 +50,7 @@ if(WITH_ALL)
set(WITH_ASN1 ON)
set(WITH_DEBUG ON)
set(WITH_ERRSTR ON)
set(WITH_EXAMPLE ON)
set(WITH_HMAC ON)
set(WITH_LOG ON)
set(WITH_OSCORE ON)
Expand Down Expand Up @@ -148,15 +151,25 @@ endif()
if(WITH_HMAC)
list(APPEND libsrc src/hmac.c)

if(WITH_SM3)
add_executable(test_hmac_sm3 test/test_hmac_sm3.c)
target_include_directories(test_hmac_sm3 PUBLIC test)
target_link_libraries(test_hmac_sm3 LINK_PUBLIC tongsuo-mini)
add_test(NAME test_hmac_sm3
COMMAND python3 -m pytest test_hmac_sm3.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
endif(WITH_SM3)

if(WITH_ASCON)
add_executable(test_ascon_hmac test/test_ascon_hmac.c)
target_include_directories(test_ascon_hmac PUBLIC test)
target_link_libraries(test_ascon_hmac LINK_PUBLIC tongsuo-mini)
add_test(NAME test_ascon_hmac
COMMAND python3 -m pytest test_ascon_hmac.py
add_executable(test_hmac_ascon test/test_hmac_ascon.c)
target_include_directories(test_hmac_ascon PUBLIC test)
target_link_libraries(test_hmac_ascon LINK_PUBLIC tongsuo-mini)
add_test(NAME test_hmac_ascon
COMMAND python3 -m pytest test_hmac_ascon.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
endif()
endif(WITH_ASCON)
endif()

add_library(tongsuo-mini-obj OBJECT ${libsrc})
Expand All @@ -174,6 +187,80 @@ if(WITH_PERF)
target_link_libraries(minisuo-perf LINK_PUBLIC tongsuo-mini)
endif()

if(WITH_EXAMPLE)
if(WITH_SM3)
add_executable(sm3_demo examples/sm3_demo.c)
target_link_libraries(sm3_demo LINK_PUBLIC tongsuo-mini)
set_property(TARGET sm3_demo
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)

add_executable(sm3_oneshot examples/sm3_oneshot.c)
target_link_libraries(sm3_oneshot LINK_PUBLIC tongsuo-mini)
set_property(TARGET sm3_oneshot
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)
endif(WITH_SM3)

if(WITH_SM4)
add_executable(sm4_cbc_enc examples/sm4_cbc_enc.c)
target_link_libraries(sm4_cbc_enc LINK_PUBLIC tongsuo-mini)
set_property(TARGET sm4_cbc_enc
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)

add_executable(sm4_oneshot examples/sm4_oneshot.c)
target_link_libraries(sm4_oneshot LINK_PUBLIC tongsuo-mini)
set_property(TARGET sm4_oneshot
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)
endif(WITH_SM4)

if(WITH_ASCON)
add_executable(ascon_aead_enc examples/ascon_aead_enc.c)
target_link_libraries(ascon_aead_enc LINK_PUBLIC tongsuo-mini)
set_property(TARGET ascon_aead_enc
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)

add_executable(ascon_aead_oneshot examples/ascon_aead_oneshot.c)
target_link_libraries(ascon_aead_oneshot LINK_PUBLIC tongsuo-mini)
set_property(TARGET ascon_aead_oneshot
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)

add_executable(ascon_hash examples/ascon_hash.c)
target_link_libraries(ascon_hash LINK_PUBLIC tongsuo-mini)
set_property(TARGET ascon_hash
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)

add_executable(ascon_hash_oneshot examples/ascon_hash_oneshot.c)
target_link_libraries(ascon_hash_oneshot LINK_PUBLIC tongsuo-mini)
set_property(TARGET ascon_hash_oneshot
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)
endif(WITH_ASCON)

if(WITH_HMAC)
if(WITH_ASCON)
add_executable(hmac_ascon examples/hmac_ascon.c)
target_link_libraries(hmac_ascon LINK_PUBLIC tongsuo-mini)
set_property(TARGET hmac_ascon
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)
endif(WITH_ASCON)

if(WITH_SM3)
add_executable(hmac_sm3 examples/hmac_sm3.c)
target_link_libraries(hmac_sm3 LINK_PUBLIC tongsuo-mini)
set_property(TARGET hmac_sm3
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/examples)
endif(WITH_SM3)
endif(WITH_HMAC)
endif()

enable_testing()
add_test(NAME test_minisuo
COMMAND python3 -m pytest test_minisuo.py
Expand All @@ -189,6 +276,7 @@ message(STATUS "WITH_ASCON.................${WITH_ASCON}")
message(STATUS "WITH_ASN1..................${WITH_ASN1}")
message(STATUS "WITH_DEBUG.................${WITH_DEBUG}")
message(STATUS "WITH_ERRSTR................${WITH_ERRSTR}")
message(STATUS "WITH_EXAMPLE...............${WITH_EXAMPLE}")
message(STATUS "WITH_HMAC..................${WITH_HMAC}")
message(STATUS "WITH_LOG...................${WITH_LOG}")
message(STATUS "WITH_OSCORE................${WITH_OSCORE}")
Expand Down
11 changes: 6 additions & 5 deletions app/minisuo.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ static int sm3_handler(int argc, char **argv)
return 1;
}
/* calculate SM3 hash, take argv[3] as the input */
if (tsm_sm3_oneshot(argv[3], strlen(argv[3]), md) != TSM_OK) {
if (tsm_sm3_oneshot((const unsigned char *)argv[3], strlen(argv[3]), md) != TSM_OK) {
fprintf(stderr, "calculation error\n");
return 1;
}
Expand Down Expand Up @@ -461,16 +461,17 @@ static int ascon_hash_handler(int argc, char **argv)
return 1;
}
if (strcasecmp(argv[3], "HASH") == 0) {
scheme = TSM_ASCON_HASH;
scheme = TSM_HASH_ASCON_HASH;
} else if (strcasecmp(argv[3], "HASHA") == 0) {
scheme = TSM_ASCON_HASHA;
scheme = TSM_HASH_ASCON_HASHA;
} else {
fprintf(stderr, "wrong ASCON hash scheme\n");
return 1;
}
/* calculate ASCON hash, take argv[5] as the input */
if (tsm_ascon_hash_oneshot(scheme, (const unsigned char *)argv[5], strlen(argv[5]),
md, &md_len) != TSM_OK) {
if (tsm_ascon_hash_oneshot(scheme, (const unsigned char *)argv[5], strlen(argv[5]), md,
&md_len)
!= TSM_OK) {
fprintf(stderr, "calculation error\n");
return 1;
}
Expand Down
18 changes: 15 additions & 3 deletions examples/ascon_aead_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ int main(void)
int ret = 1;
void *ctx = NULL;
const char *plaintext = "hello world";
const char *ad = "0123456789abcdef";
unsigned char *key = tsm_hex2buf("0123456789abcdef0123456789abcdef");
unsigned char *iv = tsm_hex2buf("0123456789abcdef0123456789abcdef");
unsigned char out[1024];
unsigned char tag[TSM_ASCON_AEAD_TAG_LEN];
size_t outl, tmplen;

if (key == NULL || iv == NULL) {
goto err;
}

ctx = tsm_ascon_aead_ctx_new();
if (ctx == NULL) {
goto err;
}

if (tsm_ascon_aead_init(ctx, TSM_ASCON_AEAD_128, key, iv, TSM_CIPH_FLAG_ENCRYPT) != TSM_OK
|| tsm_ascon_aead_update(ctx, (const unsigned char *)ad, strlen(ad), NULL, NULL) != TSM_OK
|| tsm_ascon_aead_update(ctx,
(const unsigned char *)plaintext,
strlen(plaintext),
Expand All @@ -41,12 +48,20 @@ int main(void)

outl += tmplen;

if (tsm_ascon_aead_get_tag(ctx, tag) != TSM_OK) {
goto err;
}

printf("ASCON_AEAD_Encrypt(%s)=", plaintext);

for (size_t i = 0; i < outl; i++) {
printf("%02x", out[i]);
}

for (size_t i = 0; i < TSM_ASCON_AEAD_TAG_LEN; i++) {
printf("%02x", tag[i]);
}

printf("\n");

ret = 0;
Expand All @@ -56,6 +71,3 @@ int main(void)
tsm_free(iv);
return ret;
}
/* cc ascon_aead_enc.c -I/opt/tongsuo-mini/include -L/opt/tongsuo-mini/lib -ltongsuo-mini \
-Wl,-rpath /opt/tongsuo-mini/lib -o ascon_aead_enc
*/
57 changes: 57 additions & 0 deletions examples/ascon_aead_oneshot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 The Tongsuo Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://github.com/Tongsuo-Project/tongsuo-mini/blob/main/LICENSE
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <tongsuo/minisuo.h>
#include <tongsuo/ascon.h>
#include <tongsuo/mem.h>

int main(void)
{
int ret = 1;
const char *plaintext = "hello world";
const char *ad = "0123456789abcdef";
unsigned char *key = tsm_hex2buf("0123456789abcdef0123456789abcdef");
unsigned char *iv = tsm_hex2buf("0123456789abcdef0123456789abcdef");
unsigned char out[1024];
size_t outl;

if (key == NULL || iv == NULL) {
goto err;
}

if (tsm_ascon_aead_oneshot(TSM_ASCON_AEAD_128,
key,
iv,
(const unsigned char *)ad,
strlen(ad),
(const unsigned char *)plaintext,
strlen(plaintext),
out,
&outl,
TSM_CIPH_FLAG_ENCRYPT)
!= TSM_OK) {
goto err;
}

printf("ASCON_AEAD_Encrypt(%s)=", plaintext);

for (size_t i = 0; i < outl; i++) {
printf("%02x", out[i]);
}

printf("\n");

ret = 0;
err:
tsm_free(key);
tsm_free(iv);
return ret;
}
5 changes: 1 addition & 4 deletions examples/ascon_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(void)
return 1;
}

if (tsm_ascon_hash_init(ctx, TSM_ASCON_HASH) != TSM_OK
if (tsm_ascon_hash_init(ctx) != TSM_OK
|| tsm_ascon_hash_update(ctx, (const unsigned char *)data, strlen(data)) != TSM_OK
|| tsm_ascon_hash_final(ctx, md, &outl) != TSM_OK) {
tsm_ascon_hash_ctx_free(ctx);
Expand All @@ -43,6 +43,3 @@ int main(void)

return 0;
}
/* cc ascon_hash.c -I/opt/tongsuo-mini/include -L/opt/tongsuo-mini/lib -ltongsuo-mini -Wl,-rpath \
/opt/tongsuo-mini/lib -o ascon_hash
*/
35 changes: 35 additions & 0 deletions examples/ascon_hash_oneshot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 The Tongsuo Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://github.com/Tongsuo-Project/tongsuo-mini/blob/main/LICENSE
*/
#include <stdio.h>
#include <string.h>
#include <tongsuo/minisuo.h>
#include <tongsuo/ascon.h>

int main(void)
{
const char *data = "hello world";
unsigned char md[TSM_ASCON_HASH_LEN];
size_t outl;

if (tsm_ascon_hash_oneshot(TSM_HASH_ASCON_HASH, (const unsigned char *)data, strlen(data), md,
&outl)
!= TSM_OK) {
return 1;
}

printf("ASCON_HASH(%s)=", data);

for (size_t i = 0; i < outl; i++) {
printf("%02x", md[i]);
}

printf("\n");

return 0;
}
54 changes: 54 additions & 0 deletions examples/hmac_ascon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 The Tongsuo Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://github.com/Tongsuo-Project/tongsuo-mini/blob/main/LICENSE
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <tongsuo/minisuo.h>
#include <tongsuo/ascon.h>
#include <tongsuo/mem.h>
#include <tongsuo/hmac.h>

int main(void)
{
int ret = 1;
void *ctx = NULL;
const char *data = "hello world";
unsigned char *key =
tsm_hex2buf("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
unsigned char hmac[TSM_ASCON_HMAC_LEN];
size_t outl;

if (key == NULL)
goto err;

ctx = tsm_hmac_ctx_new();
if (ctx == NULL) {
goto err;
}

if (tsm_hmac_init(ctx, key, 32, TSM_HASH_ASCON_HASH) != TSM_OK
|| tsm_hmac_update(ctx, (const unsigned char *)data, strlen(data)) != TSM_OK
|| tsm_hmac_final(ctx, hmac, &outl) != TSM_OK) {
goto err;
}

printf("HMAC_ASCON(%s)=", data);

for (size_t i = 0; i < outl; i++) {
printf("%02x", hmac[i]);
}

printf("\n");

ret = 0;
err:
tsm_free(key);
tsm_hmac_ctx_free(ctx);
return ret;
}
Loading

0 comments on commit 019c9e3

Please sign in to comment.