word.h 2.56 KB
Newer Older
Martin Schläffer committed
1 2 3 4
#ifndef WORD_H_
#define WORD_H_

#include <stdint.h>
Martin Schläffer committed
5
#include <string.h>
Martin Schläffer committed
6

Martin Schläffer committed
7
#include "config.h"
Martin Schläffer committed
8
#include "endian.h"
Enrico Pozzobon committed
9
#include "forceinline.h"
Martin Schläffer committed
10
#include "interleave.h"
Martin Schläffer committed
11

Martin Schläffer committed
12
#if ASCON_EXTERN_BI
Martin Schläffer committed
13

Martin Schläffer committed
14 15
#define U64TOWORD(x) U64BIG(x)
#define WORDTOU64(x) U64BIG(x)
Martin Schläffer committed
16

Martin Schläffer committed
17
#else
Martin Schläffer committed
18

Martin Schläffer committed
19 20
#define U64TOWORD(x) TOBI(x)
#define WORDTOU64(x) FROMBI(x)
Martin Schläffer committed
21

Martin Schläffer committed
22
#endif
Martin Schläffer committed
23

Martin Schläffer committed
24 25 26 27 28
typedef union {
  uint64_t x;
  uint32_t w[2];
  uint8_t b[8];
} word_t;
Martin Schläffer committed
29

Martin Schläffer committed
30 31
forceinline uint32_t ROR32(uint32_t x, int n) {
  return x >> n | x << (-n & 31);
Martin Schläffer committed
32 33
}

Martin Schläffer committed
34 35 36 37 38
forceinline uint64_t ROR(uint64_t x, int n) {
  word_t b, a = {.x = x};
  b.w[0] = (n % 2) ? ROR32(a.w[1], (n - 1) / 2) : ROR32(a.w[0], n / 2);
  b.w[1] = (n % 2) ? ROR32(a.w[0], (n + 1) / 2) : ROR32(a.w[1], n / 2);
  return b.x;
Martin Schläffer committed
39 40
}

Martin Schläffer committed
41 42 43 44 45
forceinline uint64_t KEYROT(uint64_t a, uint64_t b) {
  word_t w, lo2hi = {.x = a}, hi2lo = {.x = b};
  w.w[0] = lo2hi.w[0] << 16 | hi2lo.w[0] >> 16;
  w.w[1] = lo2hi.w[1] << 16 | hi2lo.w[1] >> 16;
  return w.x;
Martin Schläffer committed
46 47
}

Martin Schläffer committed
48 49 50
forceinline int NOTZERO(uint64_t a, uint64_t b) {
  uint64_t result = a | b;
  result |= result >> 32;
Martin Schläffer committed
51 52
  result |= result >> 16;
  result |= result >> 8;
Enrico Pozzobon committed
53
  return ((((int)(result & 0xff) - 1) >> 8) & 1) - 1;
Martin Schläffer committed
54 55
}

Martin Schläffer committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#if ASCON_EXTERN_BI

forceinline uint64_t PAD(int i) { return 0x80ull << (56 - 8 * i); }

forceinline uint64_t PRFS_MLEN(uint64_t len) { return len << 51; }

forceinline uint64_t CLEAR(uint64_t w, int n) {
  /* undefined for n == 0 */
  uint64_t mask = ~0ull >> (8 * n);
  return w & mask;
}

#else

forceinline uint64_t PAD(int i) {
  return ((uint64_t)((uint32_t)0x08 << (28 - 4 * i)) << 32);
Martin Schläffer committed
72 73
}

Martin Schläffer committed
74 75 76 77 78 79 80 81 82
forceinline uint64_t PRFS_MLEN(uint64_t len) {
  return ((len & 0x01) << 57) | /* 0000x */
         ((len & 0x02) << 25) | /* 000x0 */
         ((len & 0x04) << 56) | /* 00x00 */
         ((len & 0x08) << 24) | /* 0x000 */
         ((len & 0x10) << 55);  /* x0000 */
}

forceinline uint64_t CLEAR(uint64_t w, int n) {
Martin Schläffer committed
83
  /* undefined for n == 0 */
Martin Schläffer committed
84 85
  uint32_t mask = 0xffffffffull >> (4 * n);
  return w & ((uint64_t)mask << 32 | mask);
Martin Schläffer committed
86 87
}

Martin Schläffer committed
88 89
#endif

Enrico Pozzobon committed
90
forceinline uint64_t MASK(int n) {
Martin Schläffer committed
91 92 93 94
  /* undefined for n == 0 */
  return ~0ull >> (64 - 8 * n);
}

Martin Schläffer committed
95
forceinline uint64_t LOAD(const uint8_t* bytes, int n) {
Martin Schläffer committed
96
  uint64_t x = *(uint64_t*)bytes & MASK(n);
Martin Schläffer committed
97
  return U64TOWORD(x);
Martin Schläffer committed
98 99
}

Martin Schläffer committed
100
forceinline void STORE(uint8_t* bytes, uint64_t w, int n) {
Martin Schläffer committed
101
  *(uint64_t*)bytes &= ~MASK(n);
Martin Schläffer committed
102
  *(uint64_t*)bytes |= WORDTOU64(w);
Martin Schläffer committed
103 104
}

Martin Schläffer committed
105
forceinline uint64_t LOADBYTES(const uint8_t* bytes, int n) {
Martin Schläffer committed
106
  uint64_t x = 0;
Martin Schläffer committed
107
  memcpy(&x, bytes, n);
Martin Schläffer committed
108 109 110
  return U64TOWORD(x);
}

Martin Schläffer committed
111
forceinline void STOREBYTES(uint8_t* bytes, uint64_t w, int n) {
Martin Schläffer committed
112
  uint64_t x = WORDTOU64(w);
Martin Schläffer committed
113
  memcpy(bytes, &x, n);
Martin Schläffer committed
114 115 116
}

#endif /* WORD_H_ */