skinny128.h 6.8 KB
Newer Older
Alexandre Adomnicai 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
#ifndef SKINNY128_H_
#define SKINNY128_H_

#include "tk_schedule.h"

void skinny128_384_encrypt(u8* ctext, const u8* ptext, const u32* rtk1, const u32* rtk2_3);
void skinny128_384_decrypt(u8* ctext, const u8* ptext, const u32* rtk1, const u32* rtk2_3);

#define SKINNY128_384_ROUNDS	56

#define QUADRUPLE_ROUND(state, rtk1, rtk2_3) ({			\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	state[1] ^= (state[2] | state[3]);					\
	SWAPMOVE(state[3], state[0], 0x55555555, 0);		\
	state[0] ^= (rtk1)[0];								\
	state[1] ^= (rtk1)[1];								\
	state[2] ^= (rtk1)[2];								\
	state[3] ^= (rtk1)[3];								\
	state[0] ^= (rtk2_3)[0];							\
	state[1] ^= (rtk2_3)[1];							\
	state[2] ^= (rtk2_3)[2];							\
	state[3] ^= (rtk2_3)[3];							\
	mixcolumns_0(state);								\
	state[1] ^= ~(state[2] | state[3]); 				\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	state[3] ^= (state[0] | state[1]);					\
	SWAPMOVE(state[1], state[2], 0x55555555, 0);		\
	state[0] ^= (rtk1)[4];								\
	state[1] ^= (rtk1)[5];								\
	state[2] ^= (rtk1)[6];								\
	state[3] ^= (rtk1)[7];								\
	state[0] ^= (rtk2_3)[4];							\
	state[1] ^= (rtk2_3)[5];							\
	state[2] ^= (rtk2_3)[6];							\
	state[3] ^= (rtk2_3)[7];							\
	mixcolumns_1(state);								\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	state[1] ^= (state[2] | state[3]);					\
	SWAPMOVE(state[3], state[0], 0x55555555, 0);		\
	state[0] ^= (rtk1)[8];								\
	state[1] ^= (rtk1)[9];								\
	state[2] ^= (rtk1)[10];								\
	state[3] ^= (rtk1)[11];								\
	state[0] ^= (rtk2_3)[8];							\
	state[1] ^= (rtk2_3)[9];							\
	state[2] ^= (rtk2_3)[10];							\
	state[3] ^= (rtk2_3)[11];							\
	mixcolumns_2(state);								\
	state[1] ^= ~(state[2] | state[3]); 				\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	state[3] ^= (state[0] | state[1]);					\
	SWAPMOVE(state[1], state[2], 0x55555555, 0);		\
	state[0] ^= (rtk1)[12];								\
	state[1] ^= (rtk1)[13];								\
	state[2] ^= (rtk1)[14];								\
	state[3] ^= (rtk1)[15];								\
	state[0] ^= (rtk2_3)[12];							\
	state[1] ^= (rtk2_3)[13];							\
	state[2] ^= (rtk2_3)[14];							\
	state[3] ^= (rtk2_3)[15];							\
	mixcolumns_3(state);								\
})

#define INV_QUADRUPLE_ROUND(state, rtk1, rtk2_3) ({		\
	inv_mixcolumns_3(state);							\
	state[0] ^= (rtk1)[12];								\
	state[1] ^= (rtk1)[13];								\
	state[2] ^= (rtk1)[14];								\
	state[3] ^= (rtk1)[15];								\
	state[0] ^= (rtk2_3)[12];							\
	state[1] ^= (rtk2_3)[13];							\
	state[2] ^= (rtk2_3)[14];							\
	state[3] ^= (rtk2_3)[15];							\
	SWAPMOVE(state[1], state[2], 0x55555555, 0);		\
	state[3] ^= (state[0] | state[1]);					\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]); 				\
	inv_mixcolumns_2(state); 							\
	state[0] ^= (rtk1)[8];								\
	state[1] ^= (rtk1)[9];								\
	state[2] ^= (rtk1)[10];								\
	state[3] ^= (rtk1)[11];								\
	state[0] ^= (rtk2_3)[8];							\
	state[1] ^= (rtk2_3)[9];							\
	state[2] ^= (rtk2_3)[10];							\
	state[3] ^= (rtk2_3)[11];							\
	SWAPMOVE(state[3], state[0], 0x55555555, 0);		\
	state[1] ^= (state[2] | state[3]);					\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	inv_mixcolumns_1(state); 							\
	state[0] ^= (rtk1)[4];								\
	state[1] ^= (rtk1)[5];								\
	state[2] ^= (rtk1)[6];								\
	state[3] ^= (rtk1)[7];								\
	state[0] ^= (rtk2_3)[4];							\
	state[1] ^= (rtk2_3)[5];							\
	state[2] ^= (rtk2_3)[6];							\
	state[3] ^= (rtk2_3)[7];							\
	SWAPMOVE(state[1], state[2], 0x55555555, 0);		\
	state[3] ^= (state[0] | state[1]);					\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]); 				\
	inv_mixcolumns_0(state); 							\
	state[0] ^= (rtk1)[0];								\
	state[1] ^= (rtk1)[1];								\
	state[2] ^= (rtk1)[2];								\
	state[3] ^= (rtk1)[3];								\
	state[0] ^= (rtk2_3)[0];							\
	state[1] ^= (rtk2_3)[1];							\
	state[2] ^= (rtk2_3)[2];							\
	state[3] ^= (rtk2_3)[3];							\
	SWAPMOVE(state[3], state[0], 0x55555555, 0);		\
	state[1] ^= (state[2] | state[3]);					\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
	SWAPMOVE(state[0], state[3], 0x55555555, 1);		\
	SWAPMOVE(state[1], state[0], 0x55555555, 1);		\
	state[1] ^= ~(state[2] | state[3]);					\
	SWAPMOVE(state[3], state[2], 0x55555555, 1);		\
	SWAPMOVE(state[2], state[1], 0x55555555, 1);		\
	state[3] ^= ~(state[0] | state[1]);					\
})

#endif  // SKINNY128_H_