-
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 #54 from dongbeiouba/test/ascon_gen_kat
Add ascon_gen_kat
- Loading branch information
Showing
8 changed files
with
215 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
* Copyright 2024 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 <stdlib.h> | ||
#include <string.h> | ||
#include <tongsuo/minisuo.h> | ||
#include <tongsuo/ascon.h> | ||
#include <tongsuo/hmac.h> | ||
|
||
static int gen_aead(FILE *fp, int scheme) | ||
{ | ||
unsigned char key[TSM_ASCON_AEAD_KEY_LEN]; | ||
unsigned char iv[TSM_ASCON_AEAD_IV_LEN]; | ||
unsigned char pt[32]; | ||
unsigned char ad[32]; | ||
unsigned char ct[1024]; | ||
unsigned char tag[TSM_ASCON_AEAD_TAG_LEN]; | ||
size_t outl; | ||
int i, j, k, cnt = 0; | ||
|
||
for (i = 0; i < sizeof(key); i++) { | ||
key[i] = i; | ||
} | ||
for (i = 0; i < sizeof(iv); i++) { | ||
iv[i] = i; | ||
} | ||
for (i = 0; i < sizeof(pt); i++) { | ||
pt[i] = i; | ||
} | ||
for (i = 0; i < sizeof(ad); i++) { | ||
ad[i] = i; | ||
} | ||
|
||
fprintf(fp, "# Generated by ascon_gen_kat\n"); | ||
|
||
for (i = 0; i <= sizeof(pt); i++) { | ||
for (j = 0; j <= sizeof(ad); j++) { | ||
fprintf(fp, "Count = %d\n", ++cnt); | ||
|
||
fprintf(fp, "Key = "); | ||
for (k = 0; k < sizeof(key); k++) { | ||
fprintf(fp, "%02X", key[k]); | ||
} | ||
|
||
fprintf(fp, "\nNonce = "); | ||
for (k = 0; k < sizeof(iv); k++) { | ||
fprintf(fp, "%02X", iv[k]); | ||
} | ||
|
||
fprintf(fp, "\nPT = "); | ||
for (k = 0; k < i; k++) { | ||
fprintf(fp, "%02X", pt[k]); | ||
} | ||
|
||
fprintf(fp, "\nAD = "); | ||
for (k = 0; k < j; k++) { | ||
fprintf(fp, "%02X", ad[k]); | ||
} | ||
|
||
fprintf(fp, "\nCT = "); | ||
if (tsm_ascon_aead_oneshot(scheme, key, iv, ad, j, pt, i, ct, &outl, | ||
TSM_CIPH_FLAG_ENCRYPT) | ||
!= TSM_OK) { | ||
fprintf(stderr, "Error: could not encrypt\n"); | ||
return 0; | ||
} | ||
for (k = 0; k < outl; k++) { | ||
fprintf(fp, "%02X", ct[k]); | ||
} | ||
|
||
fprintf(fp, "\n\n"); | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
static int gen_hash(FILE *fp, int scheme) | ||
{ | ||
unsigned int i, j, cnt = 0; | ||
unsigned char buf[1024]; | ||
unsigned char hash[TSM_ASCON_HASH_LEN]; | ||
size_t outl; | ||
|
||
for (i = 0; i < sizeof(buf); i++) { | ||
buf[i] = i & 0xff; | ||
} | ||
|
||
fprintf(fp, "# Generated by ascon_gen_kat\n"); | ||
for (i = 0; i <= sizeof(buf); i++) { | ||
fprintf(fp, "Count = %u\n", i + 1); | ||
|
||
fprintf(fp, "Msg = "); | ||
for (j = 0; j < i; j++) { | ||
fprintf(fp, "%02X", buf[j]); | ||
} | ||
fprintf(fp, "\n"); | ||
|
||
if (tsm_ascon_hash_oneshot(scheme, buf, i, hash, &outl) != TSM_OK) { | ||
fprintf(stderr, "Error: could not generate hash\n"); | ||
return 0; | ||
} | ||
|
||
fprintf(fp, "MD = "); | ||
for (j = 0; j < TSM_ASCON_HASH_LEN; j++) { | ||
fprintf(fp, "%02X", hash[j]); | ||
} | ||
|
||
fprintf(fp, "\n\n"); | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
static int gen_hmac(FILE *fp, int hash) | ||
{ | ||
unsigned char msg[1024]; | ||
unsigned char key[32]; | ||
unsigned char buf[TSM_ASCON_HMAC_LEN]; | ||
size_t outlen; | ||
int i; | ||
|
||
fprintf(fp, "# Generated by ascon_gen_kat\n"); | ||
|
||
for (i = 0; i < sizeof(key); i++) { | ||
key[i] = i; | ||
} | ||
|
||
for (i = 0; i < sizeof(msg); i++) { | ||
msg[i] = i & 0xff; | ||
} | ||
|
||
for (i = 0; i <= sizeof(msg); i++) { | ||
fprintf(fp, "Count = %d\n", i + 1); | ||
|
||
fprintf(fp, "Key = "); | ||
for (int j = 0; j < sizeof(key); j++) { | ||
fprintf(fp, "%02X", key[j]); | ||
} | ||
|
||
fprintf(fp, "\nMsg = "); | ||
for (int j = 0; j < i; j++) { | ||
fprintf(fp, "%02X", msg[j]); | ||
} | ||
|
||
fprintf(fp, "\nTag = "); | ||
if (tsm_hmac_oneshot(hash, key, sizeof(key), msg, i, buf, &outlen) != TSM_OK) { | ||
fprintf(stderr, "Error: could not generate HMAC\n"); | ||
return 0; | ||
} | ||
for (int j = 0; j < outlen; j++) { | ||
fprintf(fp, "%02X", buf[j]); | ||
} | ||
|
||
fprintf(fp, "\n\n"); | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int ret = 0; | ||
|
||
if (argc != 2) { | ||
fprintf(stderr, "Usage: %s [-hash|-hasha|-aead128|-aead128a|-hmac|-hmaca]\n", argv[0]); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
if (strcmp(argv[1], "-hash") == 0) { | ||
ret = gen_hash(stdout, TSM_HASH_ASCON_HASH); | ||
} else if (strcmp(argv[1], "-hasha") == 0) { | ||
ret = gen_hash(stdout, TSM_HASH_ASCON_HASHA); | ||
} else if (strcmp(argv[1], "-aead128") == 0) { | ||
ret = gen_aead(stdout, TSM_ASCON_AEAD_128); | ||
} else if (strcmp(argv[1], "-aead128a") == 0) { | ||
ret = gen_aead(stdout, TSM_ASCON_AEAD_128A); | ||
} else if (strcmp(argv[1], "-hmac") == 0) { | ||
ret = gen_hmac(stdout, TSM_HASH_ASCON_HASH); | ||
} else if (strcmp(argv[1], "-hmaca") == 0) { | ||
ret = gen_hmac(stdout, TSM_HASH_ASCON_HASHA); | ||
} else { | ||
fprintf(stderr, "Error: invalid argument %s\n", argv[1]); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
if (ret == 0) { | ||
fprintf(stderr, "Error: could not generate KAT\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
// cc ascon_gen_kat.c ../build/libtongsuo-mini.a -o ascon_gen_kat -I ../include | ||
// ./ascon_gen_kat -hash > ascon_hash.txt |
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
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