#ifndef PERMUTATIONS_H_ #define PERMUTATIONS_H_ #include #include "api.h" #include "ascon.h" #include "printstate.h" #include "round.h" #define ASCON_128_KEYBYTES 16 #define ASCON_128A_KEYBYTES 16 #define ASCON_80PQ_KEYBYTES 20 #define ASCON_128_RATE 8 #define ASCON_128A_RATE 16 #define ASCON_128_PA_ROUNDS 12 #define ASCON_128_PB_ROUNDS 6 #define ASCON_128A_PB_ROUNDS 8 #define ASCON_HASH_BYTES 32 #define ASCON_128_IV WORD_T(0x8040000020301000ull) #define ASCON_128A_IV WORD_T(0xc000000030200000ull) #define ASCON_80PQ_IV WORD_T(0x8040800020301000ull) #define ASCON_HASH_IV WORD_T(0x0040000020200002ull) #define ASCON_XOF_IV WORD_T(0x0040000020200000ull) #define ASCON_HASH_IV0 WORD_T(0xfa8e976bb985dc4dull) #define ASCON_HASH_IV1 WORD_T(0xc8085072a40ccd94ull) #define ASCON_HASH_IV2 WORD_T(0xfe1781be5a847314ull) #define ASCON_HASH_IV3 WORD_T(0x2f871f6c6d0082b2ull) #define ASCON_HASH_IV4 WORD_T(0x7a1ba68850ec407eull) #define ASCON_XOF_IV0 WORD_T(0x8a46f0d354e771b8ull) #define ASCON_XOF_IV1 WORD_T(0x04489f4084368cd0ull) #define ASCON_XOF_IV2 WORD_T(0x6c94f2150dbcf66cull) #define ASCON_XOF_IV3 WORD_T(0x48965294f143b44eull) #define ASCON_XOF_IV4 WORD_T(0x0788515fe0e5fb8aull) #if ASCON_RATE == 8 && CRYPTO_KEYBYTES == 16 #define IV ASCON_128_IV #define PA_ROUNDS 12 #define PB_ROUNDS 6 #define PB P6 #endif #if ASCON_RATE == 16 #define IV ASCON_128A_IV #define PA_ROUNDS 12 #define PB_ROUNDS 8 #define PB P8 #endif #if ASCON_RATE == 8 && CRYPTO_KEYBYTES == 20 #define IV ASCON_80PQ_IV #define PA_ROUNDS 12 #define PB_ROUNDS 6 #define PB P6 #endif #define START(n) (12 - n) #if ASCON_UNROLL_LOOPS __forceinline void P12ROUNDS(state_t* s) { ROUND(s, 0x0101010100000000ull); ROUND(s, 0x0101010000000001ull); ROUND(s, 0x0101000100000100ull); ROUND(s, 0x0101000000000101ull); ROUND(s, 0x0100010100010000ull); ROUND(s, 0x0100010000010001ull); ROUND(s, 0x0100000100010100ull); ROUND(s, 0x0100000000010101ull); ROUND(s, 0x0001010101000000ull); ROUND(s, 0x0001010001000001ull); ROUND(s, 0x0001000101000100ull); ROUND(s, 0x0001000001000101ull); } __forceinline void P8ROUNDS(state_t* s) { ROUND(s, 0x0100010100010000ull); ROUND(s, 0x0100010000010001ull); ROUND(s, 0x0100000100010100ull); ROUND(s, 0x0100000000010101ull); ROUND(s, 0x0001010101000000ull); ROUND(s, 0x0001010001000001ull); ROUND(s, 0x0001000101000100ull); ROUND(s, 0x0001000001000101ull); } __forceinline void P6ROUNDS(state_t* s) { ROUND(s, 0x0100000100010100ull); ROUND(s, 0x0100000000010101ull); ROUND(s, 0x0001010101000000ull); ROUND(s, 0x0001010001000001ull); ROUND(s, 0x0001000101000100ull); ROUND(s, 0x0001000001000101ull); } #else /* !ASCON_UNROLL_LOOPS */ extern const uint64_t constants[12]; __forceinline void P12ROUNDS(state_t* s) { for (int i = START(12); i < 12; ++i) ROUND(s, constants[i]); } __forceinline void P8ROUNDS(state_t* s) { for (int i = START(8); i < 12; ++i) ROUND(s, constants[i]); } __forceinline void P6ROUNDS(state_t* s) { for (int i = START(6); i < 12; ++i) ROUND(s, constants[i]); } #endif #if ASCON_INLINE_PERM __forceinline void P12(state_t* s) { printstate(" permutation input", s); P12ROUNDS(s); } __forceinline void P8(state_t* s) { printstate(" permutation input", s); P8ROUNDS(s); } __forceinline void P6(state_t* s) { printstate(" permutation input", s); P6ROUNDS(s); } __forceinline void P(state_t* s, int i) { if (i == 12) P12(s); if (i == 8) P8(s); if (i == 6) P6(s); } #elif ASCON_SINGLE_PERM #define P12(s) P(s, 12) #define P8(s) P(s, 8) #define P6(s) P(s, 6) void P(state_t* s, uint8_t rounds); #else /* !ASCON_INLINE_PERM && !ASCON_SINGLE_PERM */ void P12(state_t* s); void P8(state_t* s); void P6(state_t* s); __forceinline void P(state_t* s, int i) { if (i == 12) P12(s); if (i == 8) P8(s); if (i == 6) P6(s); } #endif #endif /* PERMUTATIONS_H_ */