-
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.
Merge pull request #51 from dongbeiouba/example/oneshot
Add examples for oneshot. Add hmac-sm3 test cases.
- Loading branch information
Showing
38 changed files
with
928 additions
and
304 deletions.
There are no files selected for viewing
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
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,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; | ||
} |
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
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; | ||
} |
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,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; | ||
} |
Oops, something went wrong.