Skip to content

Commit 09fa582

Browse files
committed
Add an example for algorithm Ascon-Hash256
1 parent 706bd59 commit 09fa582

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

hash/Ascon-Hash256.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* Ascon-Hash256.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
23+
#ifdef NO_INLINE
24+
#include <wolfssl/wolfcrypt/misc.h>
25+
#endif
26+
#include <wolfssl/options.h>
27+
#include <wolfssl/wolfcrypt/settings.h>
28+
#include <wolfssl/ssl.h>
29+
#include <wolfssl/wolfcrypt/ascon.h>
30+
31+
#ifdef HAVE_ASCON
32+
void usage(void)
33+
{
34+
printf("./Ascon-Hash256 <file to hash>\n");
35+
exit(-99);
36+
}
37+
#endif
38+
39+
void free_mem(wc_AsconHash256 *asconHash, byte *hash, byte *rawInput, FILE *inputStream) {
40+
fclose(inputStream);
41+
free(rawInput);
42+
free(hash);
43+
wc_AsconHash256_Free(asconHash);
44+
}
45+
46+
int main(int argc, char** argv)
47+
{
48+
int ret = -1;
49+
#ifdef HAVE_ASCON
50+
wc_AsconHash256* asconHash = NULL;
51+
byte* hash = NULL;
52+
byte* rawInput = NULL;
53+
FILE* inputStream = NULL;
54+
char* fName = NULL;
55+
int fileLength = 0;
56+
57+
if (argc < 2)
58+
usage();
59+
fName = argv[1];
60+
printf("Hash input file %s\n", fName);
61+
62+
inputStream = fopen(fName, "rb");
63+
if (inputStream == NULL) {
64+
printf("ERROR: Unable to open file\n");
65+
return -1;
66+
}
67+
68+
/* find length of the file */
69+
fseek(inputStream, 0, SEEK_END);
70+
fileLength = (int) ftell(inputStream);
71+
fseek(inputStream, 0, SEEK_SET);
72+
73+
/* Create and initialize hash context */
74+
asconHash = wc_AsconHash256_New();
75+
if (asconHash == NULL) {
76+
printf("ERROR: Unable to create the hash context\n");
77+
}
78+
79+
hash = (byte*) malloc(ASCON_HASH256_SZ);
80+
if (hash == NULL) {
81+
printf("ERROR: Unable to allocate space for hash value\n");
82+
}
83+
84+
rawInput = (byte*) malloc(fileLength);
85+
if (rawInput == NULL) {
86+
printf("ERROR: Unable to allocate space for raw input\n");
87+
}
88+
89+
/* Read input file into a byte array*/
90+
ret = fread(rawInput, 1, fileLength, inputStream);
91+
if (ret != fileLength) {
92+
printf("ERROR: Failed to read the size of input file\n");
93+
}
94+
95+
ret = wc_AsconHash256_Update(asconHash, rawInput, fileLength);
96+
if (ret != 0) {
97+
printf("ERROR: Hash update failed\n");
98+
}
99+
100+
ret = wc_AsconHash256_Final(asconHash, hash);
101+
if (ret != 0) {
102+
printf("ERROR: Hash operation failed");
103+
}
104+
else {
105+
printf("Hash result is: ");
106+
for (int i = 0; i < ASCON_HASH256_SZ; i++)
107+
printf("%02x", hash[i]);
108+
printf("\n");
109+
}
110+
111+
free_mem(asconHash, hash, rawInput, inputStream);
112+
#else
113+
printf("Please enable Ascon-Hash256 (--enable-ascon --enable-experimental) in wolfCrypt\n");
114+
#endif
115+
return ret;
116+
}

hash/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ LIBS+=$(STATIC_LIB)
3737

3838
## Usage
3939

40+
### `Ascon-hash`
41+
42+
This example shows how to hash an input file using Ascon-Hash256.
43+
44+
```
45+
./Ascon-Hash256 input.txt
46+
Hash input file input.txt
47+
Hash result is: cfef206d17eff5187fe5b5451326d5489eab2d65fe71faab84a6d9300ef7c6f8
48+
```
49+
4050
### `sha256-hash`
4151

4252
This example shows how to hash an input file using SHA-256.

0 commit comments

Comments
 (0)