#include #include "core.h" void process_data(state* s, unsigned char* out, const unsigned char* in, unsigned long long len, u8 mode) { u32_2 t0; u64 tmp0; while (len >= RATE) { tmp0 = (*(u64*)in); t0 = to_big(tmp0); s->x0.h ^= t0.h; s->x0.l ^= t0.l; if (mode != ASCON_AD) { tmp0 = from_big(s->x0); *(u64*)out = (tmp0); } if (mode == ASCON_DEC) { s->x0 = t0; } P(s, START_ROUND(PB_ROUNDS)); in += RATE; out += RATE; len -= RATE; } u8 bytes[8]; memcpy(bytes, in, len); memset(bytes + len, 0, 8 - len); bytes[len] ^= 0x80; u64* tmp = (u64*)bytes; t0 = to_big(tmp[0]); s->x0.h ^= t0.h; s->x0.l ^= t0.l; if (mode != ASCON_AD) { tmp[0] = from_big(s->x0); memcpy(out, bytes, len); } if (mode == ASCON_DEC) { memcpy(bytes, in, len); s->x0 = to_big(tmp[0]); } } void ascon_core(state* s, unsigned char* out, const unsigned char* in, unsigned long long tlen, const unsigned char* ad, unsigned long long adlen, const unsigned char* npub, const unsigned char* k, u8 mode) { u32_2 K0, K1, K2, N0, N1; // load key and nonce K0 = to_big((*(u64*)k)); K1 = to_big((*(u64*)(k + 4))); K2 = to_big((*(u64*)(k + 12))); N0 = to_big((*(u64*)npub)); N1 = to_big((*(u64*)(npub + 8))); // initialization to_big_immediate(s->x0, IV); s->x0.l = K0.h; s->x1.h = K1.h; s->x1.l = K1.l; s->x2.h = K2.h; s->x2.l = K2.l; s->x3.h = N0.h; s->x3.l = N0.l; s->x4.h = N1.h; s->x4.l = N1.l; P(s, START_ROUND(PA_ROUNDS)); s->x2.l ^= K0.h; s->x3.h ^= K1.h; s->x3.l ^= K1.l; s->x4.h ^= K2.h; s->x4.l ^= K2.l; // process associated data if (adlen) { process_data(s, (void*)0, ad, adlen, ASCON_AD); P(s, START_ROUND(PB_ROUNDS)); } s->x4.l ^= 1; // process plaintext/ciphertext process_data(s, out, in, tlen, mode); // finalization s->x1.h ^= K0.h; s->x1.l ^= K1.h; s->x2.h ^= K1.l; s->x2.l ^= K2.h; s->x3.h ^= K2.l; P(s, START_ROUND(PA_ROUNDS)); s->x3.h ^= K1.h; s->x3.l ^= K1.l; s->x4.h ^= K2.h; s->x4.l ^= K2.l; }