25 #include "utility/EndianUtil.h"
26 #include "utility/RotateUtil.h"
27 #include "utility/ProgMemUtil.h"
49 #if defined(CRYPTO_LITTLE_ENDIAN)
104 memcpy(state.K, key, 16);
105 #if defined(CRYPTO_LITTLE_ENDIAN)
106 state.K[0] = be64toh(state.K[0]);
107 state.K[1] = be64toh(state.K[1]);
119 state.S[0] = 0x80400C0600000000ULL;
120 state.S[1] = state.K[0];
121 state.S[2] = state.K[1];
122 memcpy(state.S + 3, iv, 16);
123 #if defined(CRYPTO_LITTLE_ENDIAN)
124 state.S[3] = be64toh(state.S[3]);
125 state.S[4] = be64toh(state.S[4]);
137 state.S[3] ^= state.K[0];
138 state.S[4] ^= state.K[1];
146 const uint8_t *in = (
const uint8_t *)input;
147 uint8_t *out = (uint8_t *)output;
150 ((uint8_t *)(state.S))[posn] ^= *in++;
151 *out++ = ((
const uint8_t *)(state.S))[posn];
155 #if defined(CRYPTO_LITTLE_ENDIAN)
175 const uint8_t *in = (
const uint8_t *)input;
176 uint8_t *out = (uint8_t *)output;
179 *out++ = ((
const uint8_t *)(state.S))[posn] ^ *in;
180 ((uint8_t *)(state.S))[posn] = *in++;
184 #if defined(CRYPTO_LITTLE_ENDIAN)
204 const uint8_t *in = (
const uint8_t *)data;
207 ((uint8_t *)(state.S))[posn] ^= *in++;
211 #if defined(CRYPTO_LITTLE_ENDIAN)
235 ((uint8_t *)(state.S))[posn] ^= 0x80;
236 state.S[1] ^= state.K[0];
237 state.S[2] ^= state.K[1];
242 T[0] = htobe64(state.S[3] ^ state.K[0]);
243 T[1] = htobe64(state.S[4] ^ state.K[1]);
261 ((uint8_t *)(state.S))[posn] ^= 0x80;
262 state.S[1] ^= state.K[0];
263 state.S[2] ^= state.K[1];
268 T[0] = htobe64(state.S[3] ^ state.K[0]);
269 T[1] = htobe64(state.S[4] ^ state.K[1]);
272 bool ok = secure_compare(T, tag, len);
283 #if defined(CRYPTO_LITTLE_ENDIAN)
292 #if !defined(__AVR__) || defined(CRYPTO_DOC)
299 void Ascon128::permute(uint8_t first)
301 uint64_t t0, t1, t2, t3, t4;
302 #define x0 state.S[0]
303 #define x1 state.S[1]
304 #define x2 state.S[2]
305 #define x3 state.S[3]
306 #define x4 state.S[4]
309 x2 ^= ((0x0F - first) << 4) | first;
313 x0 ^= x4; x4 ^= x3; x2 ^= x1;
314 t0 = ~x0; t1 = ~x1; t2 = ~x2; t3 = ~x3; t4 = ~x4;
315 t0 &= x1; t1 &= x2; t2 &= x3; t3 &= x4; t4 &= x0;
316 x0 ^= t1; x1 ^= t2; x2 ^= t3; x3 ^= t4; x4 ^= t0;
317 x1 ^= x0; x0 ^= x4; x3 ^= x2; x2 = ~x2;
320 x0 ^= rightRotate19_64(x0) ^ rightRotate28_64(x0);
321 x1 ^= rightRotate61_64(x1) ^ rightRotate39_64(x1);
322 x2 ^= rightRotate1_64(x2) ^ rightRotate6_64(x2);
323 x3 ^= rightRotate10_64(x3) ^ rightRotate17_64(x3);
324 x4 ^= rightRotate7_64(x4) ^ rightRotate41_64(x4);
341 void Ascon128::endAuth()
346 ((uint8_t *)(state.S))[posn] ^= 0x80;
351 #if defined(CRYPTO_LITTLE_ENDIAN)
Ascon128()
Constructs a new Ascon128 authenticated cipher.
size_t keySize() const
Gets the size of the Ascon128 key in bytes.
size_t tagSize() const
Gets the size of the Ascon128 authentication tag in bytes.
void decrypt(uint8_t *output, const uint8_t *input, size_t len)
Decrypts an input buffer and writes the plaintext to an output buffer.
void encrypt(uint8_t *output, const uint8_t *input, size_t len)
Encrypts an input buffer and writes the ciphertext to an output buffer.
bool setIV(const uint8_t *iv, size_t len)
Sets the initialization vector to use for future encryption and decryption operations.
void clear()
Clears all security-sensitive state from this cipher object.
bool checkTag(const void *tag, size_t len)
Finalizes the decryption process and checks the authentication tag.
virtual ~Ascon128()
Destroys this Ascon128 authenticated cipher.
void addAuthData(const void *data, size_t len)
Adds extra data that will be authenticated but not encrypted.
void computeTag(void *tag, size_t len)
Finalizes the encryption process and computes the authentication tag.
size_t ivSize() const
Gets the size of the Ascon128 initialization vector in bytes.
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.