Skip to content

Commit

Permalink
Merge pull request #50 from dongbeiouba/doc/example
Browse files Browse the repository at this point in the history
Add examples for SM3, SM4, Ascon-hash and Ascon-aead
  • Loading branch information
InfoHunter authored Dec 13, 2023
2 parents 19cb4ef + 9b71677 commit 379181d
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 13 deletions.
Empty file removed doc/0-toc.md
Empty file.
Empty file removed doc/HOWTO/ascon.md
Empty file.
Empty file removed doc/HOWTO/perf.md
Empty file.
Empty file removed doc/HOWTO/sm3.md
Empty file.
Empty file removed doc/HOWTO/sm4.md
Empty file.
61 changes: 61 additions & 0 deletions examples/ascon_aead_enc.c
Original file line number Diff line number Diff line change
@@ -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 <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;
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
*/
48 changes: 48 additions & 0 deletions examples/ascon_hash.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <string.h>
#include <assert.h>
#include <tongsuo/minisuo.h>
#include <tongsuo/ascon.h>

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
*/
45 changes: 45 additions & 0 deletions examples/sm3_demo.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <string.h>
#include <tongsuo/minisuo.h>
#include <tongsuo/sm3.h>

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
*/
64 changes: 64 additions & 0 deletions examples/sm4_cbc_enc.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <string.h>
#include <tongsuo/minisuo.h>
#include <tongsuo/mem.h>
#include <tongsuo/sm4.h>

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
*/
12 changes: 6 additions & 6 deletions include/tongsuo/ascon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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. */
Expand Down
14 changes: 7 additions & 7 deletions src/ascon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -538,22 +538,22 @@ 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)
{
size_t tmplen = 0;
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 */
Expand Down
6 changes: 6 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "internal/log.h"

#define TSM_MAX_LOG_STRLEN 2048

static int default_log_level = TSM_LOG_ERROR;

const char *log_level[] = {
"DEBUG",
"INFO",
Expand All @@ -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;

Expand Down

0 comments on commit 379181d

Please sign in to comment.