permutations.h 4.54 KB
Newer Older
Martin Schläffer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#ifndef PERMUTATIONS_H_
#define PERMUTATIONS_H_

#include <stdint.h>

#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                                     \
  U64TOWORD(((uint64_t)(ASCON_128_KEYBYTES * 8) << 56) | \
            ((uint64_t)(ASCON_128_RATE * 8) << 48) |     \
            ((uint64_t)(ASCON_128_PA_ROUNDS) << 40) |    \
            ((uint64_t)(ASCON_128_PB_ROUNDS) << 32))

#define ASCON_128A_IV                                    \
  U64TOWORD(((uint64_t)(ASCON_128_KEYBYTES * 8) << 56) | \
            ((uint64_t)(ASCON_128A_RATE * 8) << 48) |    \
            ((uint64_t)(ASCON_128_PA_ROUNDS) << 40) |    \
            ((uint64_t)(ASCON_128A_PB_ROUNDS) << 32))

#define ASCON_80PQ_IV                                     \
  U64TOWORD(((uint64_t)(ASCON_80PQ_KEYBYTES * 8) << 56) | \
            ((uint64_t)(ASCON_128_RATE * 8) << 48) |      \
            ((uint64_t)(ASCON_128_PA_ROUNDS) << 40) |     \
            ((uint64_t)(ASCON_128_PB_ROUNDS) << 32))

#define ASCON_HASH_IV                                 \
  U64TOWORD(((uint64_t)(ASCON_128_RATE * 8) << 48) |  \
            ((uint64_t)(ASCON_128_PA_ROUNDS) << 40) | \
            ((uint64_t)(ASCON_HASH_BYTES * 8) << 0))

#define ASCON_XOF_IV                                 \
  U64TOWORD(((uint64_t)(ASCON_128_RATE * 8) << 48) | \
            ((uint64_t)(ASCON_128_PA_ROUNDS) << 40))

#define ASCON_HASH_IV0 U64TOWORD(0xee9398aadb67f03dull)
#define ASCON_HASH_IV1 U64TOWORD(0x8bb21831c60f1002ull)
#define ASCON_HASH_IV2 U64TOWORD(0xb48a92db98d5da62ull)
#define ASCON_HASH_IV3 U64TOWORD(0x43189921b8f8e3e8ull)
#define ASCON_HASH_IV4 U64TOWORD(0x348fa5c9d525e140ull)

#define ASCON_XOF_IV0 U64TOWORD(0xb57e273b814cd416ull)
#define ASCON_XOF_IV1 U64TOWORD(0x2b51042562ae2420ull)
#define ASCON_XOF_IV2 U64TOWORD(0x66a3a7768ddf2218ull)
#define ASCON_XOF_IV3 U64TOWORD(0x5aad0a7a8153650cull)
#define ASCON_XOF_IV4 U64TOWORD(0x4f3e0e32539493b6ull)

#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, 0xc, 0xc);
  ROUND(s, 0x9, 0xc);
  ROUND(s, 0xc, 0x9);
  ROUND(s, 0x9, 0x9);
  ROUND(s, 0x6, 0xc);
  ROUND(s, 0x3, 0xc);
  ROUND(s, 0x6, 0x9);
  ROUND(s, 0x3, 0x9);
  ROUND(s, 0xc, 0x6);
  ROUND(s, 0x9, 0x6);
  ROUND(s, 0xc, 0x3);
  ROUND(s, 0x9, 0x3);
}

__forceinline void P8ROUNDS(state_t* s) {
  ROUND(s, 0x6, 0xc);
  ROUND(s, 0x3, 0xc);
  ROUND(s, 0x6, 0x9);
  ROUND(s, 0x3, 0x9);
  ROUND(s, 0xc, 0x6);
  ROUND(s, 0x9, 0x6);
  ROUND(s, 0xc, 0x3);
  ROUND(s, 0x9, 0x3);
}

__forceinline void P6ROUNDS(state_t* s) {
  ROUND(s, 0x6, 0x9);
  ROUND(s, 0x3, 0x9);
  ROUND(s, 0xc, 0x6);
  ROUND(s, 0x9, 0x6);
  ROUND(s, 0xc, 0x3);
  ROUND(s, 0x9, 0x3);
}

#else /* !ASCON_UNROLL_LOOPS */

extern const uint8_t constants[][2];

__forceinline void P12ROUNDS(state_t* s) {
  for (int i = START(12); i < 12; i++)
    ROUND(s, constants[i][0], constants[i][1]);
}

__forceinline void P8ROUNDS(state_t* s) {
  for (int i = START(8); i < 12; i++)
    ROUND(s, constants[i][0], constants[i][1]);
}

__forceinline void P6ROUNDS(state_t* s) {
  for (int i = START(6); i < 12; i++)
    ROUND(s, constants[i][0], constants[i][1]);
}

#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_ */