ASCON Suite
permutation/hash/main.c
/*
* This example demonstrates how to implement ASCON-HASHA using
* just the ASCON permutation API.
*
* Usage: hash "text to be hashed"
*
* This example is placed into the public domain.
*/
#include <ascon/hash.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
const unsigned char *data;
size_t len, posn;
/* Validate the command-line parameters */
if (argc < 2) {
fprintf(stderr, "Usage: %s \"text to be hashed\"\n", argv[0]);
return 1;
}
data = (const unsigned char *)(argv[1]);
len = strlen(argv[1]);
/* Initialize the ASCON permutation state */
/* Set the ASCON-HASHA initialization vector and permute for 12 rounds */
static unsigned char const iv[8] = {
0x00, 0x40, 0x0c, 0x04, 0x00, 0x00, 0x01, 0x00
};
/* Absorb the data 8 bytes at a time. Permute each block with 8 rounds */
for (posn = 0; (posn + 8) <= len; posn += 8) {
ascon_add_bytes(&state, data + posn, 0, 8);
}
/* Absorb and pad the last block. Use 12 permutation rounds when
* transitioning from absorbing to squeezing. */
static unsigned char const pad[1] = {0x80};
ascon_add_bytes(&state, data + posn, 0, len - posn);
ascon_add_bytes(&state, pad, len - posn, 1);
/* Squeeze out the 32-byte hash value, 8 bytes at a time */
unsigned char hash[32];
ascon_extract_bytes(&state, hash, 0, 8);
ascon_extract_bytes(&state, hash + 8, 0, 8);
ascon_extract_bytes(&state, hash + 16, 0, 8);
ascon_extract_bytes(&state, hash + 24, 0, 8);
/* Free the resources associated with the ASCON permutation state */
/* Print the final hash value */
for (posn = 0; posn < 32; ++posn)
printf("%02x", hash[posn]);
printf("\n");
/* Cross-check with the library's version of ASCON-HASHA */
unsigned char hash2[32];
ascon_hasha(hash2, data, len);
if (memcmp(hash, hash2, 32) != 0) {
printf("Hash value is incorrect!");
return 1;
}
return 0;
}
ASCON-HASH and ASCON-HASHA hash algorithms.
void ascon_hasha(unsigned char *out, const unsigned char *in, size_t inlen)
Hashes a block of input data with ASCON-HASHA.
Definition: ascon-hasha.c:27
Direct access to the ASCON permutation primitive.
#define ascon_permute12(state)
Permutes the ASCON state with 12 rounds of the permutation.
Definition: permutation.h:199
void ascon_free(ascon_state_t *state)
Frees an ASCON permutation state and attempts to destroy any sensitive material.
void ascon_overwrite_bytes(ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
Overwrites existing bytes in the ASCON state.
void ascon_extract_bytes(const ascon_state_t *state, uint8_t *data, unsigned offset, unsigned size)
Extracts bytes from the ASCON state.
void ascon_add_bytes(ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
Adds bytes to the ASCON state by XOR'ing them with existing bytes.
void ascon_init(ascon_state_t *state)
Initializes the words of the ASCON permutation state to zero.
#define ascon_permute8(state)
Permutes the ASCON state with 8 rounds of the permutation.
Definition: permutation.h:206
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63