ASCON Suite
snippets.c
Go to the documentation of this file.
4  static unsigned char const iv[8] = {
5  0x80, 0x40, 0x0c, 0x06, 0xFF, 0xFF, 0xFF, 0xFF
6  };
7  ascon_overwrite_bytes(&state, iv, 0, 8);
8  ascon_overwrite_bytes(&state, key, 8, 16);
9  ascon_overwrite_bytes(&state, nonce, 24, 16);
12 
14  unsigned char data[8];
16  ascon_add_bytes(&state, data, 0, 8);
17  ascon_clean(data, sizeof(data));
19 
23 
25 void ofb_encrypt(ascon_state_t *state, unsigned char *c, const unsigned char *m, int len)
26 {
27  int posn;
28  for (posn = 0; (posn + 16) <= len; posn += 16) {
29  ascon_extract_and_add_bytes(state, m + posn, c + posn, 0, 16);
31  }
32  if (posn < len) {
33  ascon_extract_and_add_bytes(state, m + posn, c + posn, 0, len - posn);
34  }
35 }
37 
39 void cfb_encrypt(ascon_state_t *state, unsigned char *c, const unsigned char *m, int len)
40 {
41  int posn;
42  for (posn = 0; (posn + 16) <= len; posn += 16) {
43  ascon_add_bytes(state, m + posn, 0, 16);
44  ascon_extract_bytes(state, c + posn, 0, 16);
46  }
47  if (posn < len) {
48  ascon_add_bytes(state, m + posn, 0, len - posn);
49  ascon_extract_bytes(state, c + posn, 0, len - posn);
50  }
51 }
53 
55 void cfb_decrypt(ascon_state_t *state, unsigned char *m, const unsigned char *c, int len)
56 {
57  int posn;
58  for (posn = 0; (posn + 16) <= len; posn += 16) {
59  ascon_extract_and_add_bytes(state, m + posn, c + posn, 0, 16);
60  ascon_overwrite_bytes(state, c + posn, 0, 16);
62  }
63  if (posn < len) {
64  ascon_extract_and_add_bytes(state, m + posn, c + posn, 0, len - posn);
65  ascon_overwrite_bytes(state, c + posn, 0, len - posn);
66  }
67 }
69 
71 void cfb_decrypt(ascon_state_t *state, unsigned char *m, const unsigned char *c, int len)
72 {
73  int posn;
74  for (posn = 0; (posn + 16) <= len; posn += 16) {
75  ascon_extract_and_overwrite_bytes(state, c + posn, m + posn, 0, 16);
77  }
78  if (posn < len) {
79  ascon_extract_and_overwrite_bytes(state, c + posn, m + posn, 0, len - posn);
80  }
81 }
83 
85 void cfb_auth_encrypt(ascon_state_t *state, unsigned char *c, const unsigned char *m, int len, unsigned char tag[16])
86 {
87  static unsigned char const pad[1] = {0x80};
88  int posn;
89  for (posn = 0; (posn + 16) <= len; posn += 16) {
90  ascon_add_bytes(state, m + posn, 0, 16);
91  ascon_extract_bytes(state, c + posn, 0, 16);
93  }
94  if (posn < len) {
95  ascon_add_bytes(state, m + posn, 0, len - posn);
96  ascon_extract_bytes(state, c + posn, 0, len - posn);
97  }
98  ascon_add_bytes(state, pad, len - posn, 1); /* padding */
100  ascon_extract_bytes(state, tag, 24, 16); /* extract the tag */
101 }
#define ascon_permute12(state)
Permutes the ASCON state with 12 rounds of the permutation.
Definition: permutation.h:199
void ascon_overwrite_with_zeroes(ascon_state_t *state, unsigned offset, unsigned size)
Overwrites a part of the ASCON state with zeroes.
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_extract_and_overwrite_bytes(ascon_state_t *state, const uint8_t *input, uint8_t *output, unsigned offset, unsigned size)
Extracts bytes from the ASCON state and XOR's them with input bytes to produce output bytes....
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_extract_and_add_bytes(const ascon_state_t *state, const uint8_t *input, uint8_t *output, unsigned offset, unsigned size)
Extracts bytes from the ASCON state and XOR's them with input bytes to produce output 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
void cfb_decrypt(ascon_state_t *state, unsigned char *m, const unsigned char *c, int len)
[snippet_encrypt_cfb]
Definition: snippets.c:55
void cfb_auth_encrypt(ascon_state_t *state, unsigned char *c, const unsigned char *m, int len, unsigned char tag[16])
[snippet_decrypt_cfb2]
Definition: snippets.c:85
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
ascon_clean(data, sizeof(data))
void cfb_encrypt(ascon_state_t *state, unsigned char *c, const unsigned char *m, int len)
[snippet_encrypt_ofb]
Definition: snippets.c:39
void ofb_encrypt(ascon_state_t *state, unsigned char *c, const unsigned char *m, int len)
[snippet_zero2]
Definition: snippets.c:25
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63