From 9b71677396bd281e622cc0494efbe519a9264fc0 Mon Sep 17 00:00:00 2001 From: K1 Date: Tue, 12 Dec 2023 21:52:53 +0800 Subject: [PATCH] Add examples for SM3, SM4, Ascon-hash and Ascon-aead --- doc/0-toc.md | 0 doc/HOWTO/ascon.md | 0 doc/HOWTO/perf.md | 0 doc/HOWTO/sm3.md | 0 doc/HOWTO/sm4.md | 0 examples/ascon_aead_enc.c | 61 +++++++++++++++++++++++++++++++++++++ examples/ascon_hash.c | 48 +++++++++++++++++++++++++++++ examples/sm3_demo.c | 45 +++++++++++++++++++++++++++ examples/sm4_cbc_enc.c | 64 +++++++++++++++++++++++++++++++++++++++ include/tongsuo/ascon.h | 12 ++++---- src/ascon.c | 14 ++++----- src/log.c | 6 ++++ 12 files changed, 237 insertions(+), 13 deletions(-) delete mode 100644 doc/0-toc.md delete mode 100644 doc/HOWTO/ascon.md delete mode 100644 doc/HOWTO/perf.md delete mode 100644 doc/HOWTO/sm3.md delete mode 100644 doc/HOWTO/sm4.md create mode 100644 examples/ascon_aead_enc.c create mode 100644 examples/ascon_hash.c create mode 100644 examples/sm3_demo.c create mode 100644 examples/sm4_cbc_enc.c diff --git a/doc/0-toc.md b/doc/0-toc.md deleted file mode 100644 index e69de29..0000000 diff --git a/doc/HOWTO/ascon.md b/doc/HOWTO/ascon.md deleted file mode 100644 index e69de29..0000000 diff --git a/doc/HOWTO/perf.md b/doc/HOWTO/perf.md deleted file mode 100644 index e69de29..0000000 diff --git a/doc/HOWTO/sm3.md b/doc/HOWTO/sm3.md deleted file mode 100644 index e69de29..0000000 diff --git a/doc/HOWTO/sm4.md b/doc/HOWTO/sm4.md deleted file mode 100644 index e69de29..0000000 diff --git a/examples/ascon_aead_enc.c b/examples/ascon_aead_enc.c new file mode 100644 index 0000000..73fb97e --- /dev/null +++ b/examples/ascon_aead_enc.c @@ -0,0 +1,61 @@ +/* + * 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 +#include +#include +#include +#include +#include + +int main(void) +{ + int ret = 1; + void *ctx = NULL; + const char *plaintext = "hello world"; + unsigned char *key = tsm_hex2buf("0123456789abcdef0123456789abcdef"); + unsigned char *iv = tsm_hex2buf("0123456789abcdef0123456789abcdef"); + unsigned char out[1024]; + size_t outl, tmplen; + + 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 *)plaintext, + strlen(plaintext), + out, + &outl) + != TSM_OK + || tsm_ascon_aead_final(ctx, out + outl, &tmplen) != TSM_OK) { + goto err; + } + + outl += tmplen; + + printf("ASCON_AEAD_Encrypt(%s)=", plaintext); + + for (size_t i = 0; i < outl; i++) { + printf("%02x", out[i]); + } + + printf("\n"); + + ret = 0; +err: + tsm_ascon_aead_ctx_free(ctx); + tsm_free(key); + 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 + */ diff --git a/examples/ascon_hash.c b/examples/ascon_hash.c new file mode 100644 index 0000000..0e7c95f --- /dev/null +++ b/examples/ascon_hash.c @@ -0,0 +1,48 @@ +/* + * 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 +#include +#include +#include +#include + +int main(void) +{ + void *ctx = NULL; + const char *data = "hello world"; + unsigned char md[TSM_ASCON_HASH_LEN]; + size_t outl; + + ctx = tsm_ascon_hash_ctx_new(); + if (ctx == NULL) { + return 1; + } + + if (tsm_ascon_hash_init(ctx, TSM_ASCON_HASH) != 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); + return 1; + } + + tsm_ascon_hash_ctx_free(ctx); + + printf("ASCON_HASH(%s)=", data); + + for (size_t i = 0; i < outl; i++) { + printf("%02x", md[i]); + } + + printf("\n"); + + 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 + */ diff --git a/examples/sm3_demo.c b/examples/sm3_demo.c new file mode 100644 index 0000000..951939f --- /dev/null +++ b/examples/sm3_demo.c @@ -0,0 +1,45 @@ +/* + * 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 +#include +#include +#include + +int main(void) +{ + void *ctx = NULL; + const char *data = "hello world"; + unsigned char md[TSM_SM3_DIGEST_LEN]; + + ctx = tsm_sm3_ctx_new(); + if (ctx == NULL) { + return 1; + } + + if (tsm_sm3_init(ctx) != TSM_OK || tsm_sm3_update(ctx, data, strlen(data)) != TSM_OK + || tsm_sm3_final(ctx, md) != TSM_OK) { + tsm_sm3_ctx_free(ctx); + return 1; + } + + tsm_sm3_ctx_free(ctx); + + printf("SM3(%s)=", data); + + for (int i = 0; i < TSM_SM3_DIGEST_LEN; i++) { + printf("%02x", md[i]); + } + + printf("\n"); + + return 0; +} +/* cc sm3_demo.c -I/opt/tongsuo-mini/include -L/opt/tongsuo-mini/lib -ltongsuo-mini -Wl,-rpath \ +/opt/tongsuo-mini/lib -o sm3_demo + */ diff --git a/examples/sm4_cbc_enc.c b/examples/sm4_cbc_enc.c new file mode 100644 index 0000000..a3ea8f4 --- /dev/null +++ b/examples/sm4_cbc_enc.c @@ -0,0 +1,64 @@ +/* + * 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 +#include +#include +#include +#include + +int main(void) +{ + int ret = 1; + void *ctx = NULL; + const char *plaintext = "hello world"; + unsigned char *key = tsm_hex2buf("0123456789abcdef0123456789abcdef"); + unsigned char *iv = tsm_hex2buf("0123456789abcdef0123456789abcdef"); + unsigned char ciphertext[1024]; + size_t outl, tmplen; + + if (key == NULL || iv == NULL) { + goto err; + } + + ctx = tsm_sm4_ctx_new(); + if (ctx == NULL) { + goto err; + } + + if (tsm_sm4_init(ctx, TSM_CIPH_MODE_CBC, key, iv, TSM_CIPH_FLAG_ENCRYPT) != TSM_OK + || tsm_sm4_update(ctx, + (const unsigned char *)plaintext, + strlen(plaintext), + ciphertext, + &outl) + != TSM_OK + || tsm_sm4_final(ctx, ciphertext + outl, &tmplen) != TSM_OK) { + goto err; + } + outl += tmplen; + + printf("SM4_CBC_Encrypt(%s)=", plaintext); + + for (size_t i = 0; i < outl; i++) { + printf("%02x", ciphertext[i]); + } + + printf("\n"); + + ret = 0; +err: + tsm_sm4_ctx_free(ctx); + tsm_free(key); + tsm_free(iv); + return ret; +} + +/* cc sm4_cbc_enc.c -I/opt/tongsuo-mini/include -L/opt/tongsuo-mini/lib -ltongsuo-mini -Wl,-rpath \ +/opt/tongsuo-mini/lib -o sm4_cbc_enc + */ diff --git a/include/tongsuo/ascon.h b/include/tongsuo/ascon.h index 15f3e0c..abcbb9c 100644 --- a/include/tongsuo/ascon.h +++ b/include/tongsuo/ascon.h @@ -22,7 +22,7 @@ extern "C" { # define TSM_ASCON_AEAD_TAG_LEN 16 # define TSM_ASCON_AEAD_KEY_LEN 16 -# define TSM_ASCON_AEAD_NONCE_LEN 16 +# define TSM_ASCON_AEAD_IV_LEN 16 # define TSM_ASCON_HASH 0x1 # define TSM_ASCON_HASHA 0x2 @@ -34,12 +34,12 @@ extern "C" { void *tsm_ascon_aead_ctx_new(void); /* Frees up the context ctx of ascon aead. */ void tsm_ascon_aead_ctx_free(void *ctx); -/* Initializes the context ctx with type, key, nonce and flags. type should be TSM_ASCON_AEAD_128 or - * TSM_ASCON_AEAD_128A. The length of key and nonce should be 16 bytes. flags may be +/* Initializes the context ctx with type, key, iv and flags. type should be TSM_ASCON_AEAD_128 or + * TSM_ASCON_AEAD_128A. The length of key and iv should be 16 bytes. flags may be * TSM_CIPH_FLAG_ENCRYPT or TSM_CIPH_FLAG_DECRYPT. If you want to encrypt data without padding, * flags should be TSM_CIPH_FLAG_ENCRYPT | TSM_CIPH_FLAG_NO_PAD. Returns TSM_OK for success and * others for failure. */ -int tsm_ascon_aead_init(void *ctx, int type, const unsigned char *key, const unsigned char *nonce, +int tsm_ascon_aead_init(void *ctx, int type, const unsigned char *key, const unsigned char *iv, int flags); /* Encrypts or decrypts data at in with the length inl, and retrives the result at out. The number * of bytes of result written will be written to the integer at outl. Returns TSM_OK for success and @@ -57,12 +57,12 @@ int tsm_ascon_aead_set_tag(void *ctx, const unsigned char *tag); /* Get tag after ascon AEAD encryption. */ int tsm_ascon_aead_get_tag(void *ctx, unsigned char *tag); /* Encrypts or decrypts data at in with length inl, and retrives the result at out. The number of - * bytes of result written will be written to the integer at outl. The length of key and nonce + * bytes of result written will be written to the integer at outl. The length of key and iv * should be 16 bytes. type should be TSM_ASCON_AEAD_128 or TSM_ASCON_AEAD_128A. flags may be * TSM_CIPH_FLAG_ENCRYPT or TSM_CIPH_FLAG_DECRYPT. If you want to encrypt data without padding, * flags should be TSM_CIPH_FLAG_ENCRYPT | TSM_CIPH_FLAG_NO_PAD. Returns TSM_OK for success and * others for failure. */ -int tsm_ascon_aead_oneshot(int type, const unsigned char *key, const unsigned char *nonce, +int tsm_ascon_aead_oneshot(int type, const unsigned char *key, const unsigned char *iv, const unsigned char *ad, size_t adl, const unsigned char *in, size_t inl, unsigned char *out, size_t *outl, int flags); /* Create ctx of ascon hash. */ diff --git a/src/ascon.c b/src/ascon.c index 8936e51..b8a0ddd 100644 --- a/src/ascon.c +++ b/src/ascon.c @@ -453,12 +453,12 @@ void *tsm_ascon_aead_ctx_new(void) return ctx; } -int tsm_ascon_aead_init(void *c, int type, const unsigned char *key, const unsigned char *nonce, +int tsm_ascon_aead_init(void *c, int type, const unsigned char *key, const unsigned char *iv, int flags) { TSM_ASCON_AEAD_CTX *ctx = c; - if (ctx == NULL || key == NULL || nonce == NULL) + if (ctx == NULL || key == NULL || iv == NULL) return eLOG(TSM_ERR_PASS_NULL_PARAM); ctx->mode = type; @@ -467,8 +467,8 @@ int tsm_ascon_aead_init(void *c, int type, const unsigned char *key, const unsig /* load key and nonce */ ctx->K[0] = LOADBYTES(key, 8); ctx->K[1] = LOADBYTES(key + 8, 8); - ctx->N[0] = LOADBYTES(nonce, 8); - ctx->N[1] = LOADBYTES(nonce + 8, 8); + ctx->N[0] = LOADBYTES(iv, 8); + ctx->N[1] = LOADBYTES(iv + 8, 8); if (ctx->mode == TSM_ASCON_AEAD_128) { ctx->block_size = ASCON_128_RATE; @@ -538,7 +538,7 @@ int tsm_ascon_aead_get_tag(void *ctx, unsigned char *tag) return TSM_OK; } -int tsm_ascon_aead_oneshot(int type, const unsigned char *key, const unsigned char *nonce, +int tsm_ascon_aead_oneshot(int type, const unsigned char *key, const unsigned char *iv, const unsigned char *ad, size_t adl, const unsigned char *in, size_t inl, unsigned char *out, size_t *outl, int flags) { @@ -546,14 +546,14 @@ int tsm_ascon_aead_oneshot(int type, const unsigned char *key, const unsigned ch int ret; void *ctx; - if (key == NULL || nonce == NULL || out == NULL || outl == NULL) + if (key == NULL || iv == NULL || out == NULL || outl == NULL) return eLOG(TSM_ERR_PASS_NULL_PARAM); ctx = tsm_ascon_aead_ctx_new(); if (ctx == NULL) return TSM_ERR_MALLOC_FAILED; - if ((ret = tsm_ascon_aead_init(ctx, type, key, nonce, flags)) != TSM_OK) + if ((ret = tsm_ascon_aead_init(ctx, type, key, iv, flags)) != TSM_OK) goto err; /* Expect tag after ciphertext */ diff --git a/src/log.c b/src/log.c index 9c3db49..c54c678 100644 --- a/src/log.c +++ b/src/log.c @@ -10,9 +10,12 @@ #include #include #include +#include "internal/log.h" #define TSM_MAX_LOG_STRLEN 2048 +static int default_log_level = TSM_LOG_ERROR; + const char *log_level[] = { "DEBUG", "INFO", @@ -26,6 +29,9 @@ void tsm_log_impl(const char *file, int line, int level, const char *fmt, ...) va_list args; char msg[TSM_MAX_LOG_STRLEN]; + if (level < default_log_level) + return; + p = msg; end = p + sizeof(msg) - 1;