-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for SM3, SM4, Ascon-hash and Ascon-aead
- Loading branch information
1 parent
19cb4ef
commit 9b71677
Showing
12 changed files
with
237 additions
and
13 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters