skinny128.h 4.6 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
#ifndef SKINNY128_H_
#define SKINNY128_H_
#include "tk_schedule.h"

void skinny128_384_encrypt(u8* ctext, u8* ctext_bis, const u8* ptext, 
					const u8* ptext_bis, const tweakey tk);

void skinny128_384_decrypt(u8* ctext, u8* ctext_bis, const u8* ptext, 
					const u8* ptext_bis, const tweakey tk);

#define SKINNY128_128_ROUNDS	40
#define SKINNY128_256_ROUNDS	48
#define SKINNY128_384_ROUNDS	56

#define ROR(x,y) (((x) >> (y)) | ((x) << (32 - (y))))

#define QUADRUPLE_ROUND(state, rtk1, rtk2_3) ({			\
	state[3] ^= (state[0] | state[1]);					\
	state[7] ^= (state[4] | state[5]);					\
	state[1] ^= (state[6] | state[5]);					\
	state[2] ^= (state[3] & state[7]);					\
	state[6] ^= (~state[7] | state[4]);					\
	state[0] ^= (state[2] | ~state[1]);					\
	state[4] ^= (~state[3] | state[2]);					\
	state[5] ^= (state[6] & state[0]);					\
	add_tweakey(state, rtk1, rtk2_3); 					\
	mixcolumns_0(state);								\
	state[4] ^= (state[2] | state[3]);					\
	state[5] ^= (state[6] | state[1]);					\
	state[3] ^= (state[0] | state[1]);					\
	state[7] ^= (state[4] & state[5]);					\
	state[0] ^= (~state[5] | state[6]);					\
	state[2] ^= (state[7] | ~state[3]);					\
	state[6] ^= (~state[4] | state[7]);					\
	state[1] ^= (state[0] & state[2]);					\
	add_tweakey(state, rtk1+8, rtk2_3+8); 				\
	mixcolumns_1(state);								\
	state[6] ^= (state[7] | state[4]);					\
	state[1] ^= (state[0] | state[3]);					\
	state[4] ^= (state[2] | state[3]);					\
	state[5] ^= (state[6] & state[1]);					\
	state[2] ^= (~state[1] | state[0]);					\
	state[7] ^= (state[5] | ~state[4]);					\
	state[0] ^= (~state[6] | state[5]);					\
	state[3] ^= (state[2] & state[7]);					\
	add_tweakey(state, rtk1+16, rtk2_3+16); 			\
	mixcolumns_2(state);								\
	state[0] ^= (state[5] | state[6]);					\
	state[3] ^= (state[2] | state[4]);					\
	state[6] ^= (state[7] | state[4]);					\
	state[1] ^= (state[0] & state[3]);					\
	state[7] ^= (~state[3] | state[2]);					\
	state[5] ^= (state[1] | ~state[6]);					\
	state[2] ^= (~state[0] | state[1]);					\
	state[4] ^= (state[7] & state[5]);					\
	add_tweakey(state, rtk1+24, rtk2_3+24); 			\
	mixcolumns_3(state);								\
	state[0] ^= state[1]; 								\
	state[1] ^= state[0]; 								\
	state[0] ^= state[1]; 								\
	state[2] ^= state[3]; 								\
	state[3] ^= state[2]; 								\
	state[2] ^= state[3]; 								\
	state[4] ^= state[7]; 								\
	state[7] ^= state[4]; 								\
	state[4] ^= state[7]; 								\
	state[5] ^= state[6]; 								\
	state[6] ^= state[5]; 								\
	state[5] ^= state[6]; 								\
})

#define INV_QUADRUPLE_ROUND(state, rtk1, rtk2_3) ({		\
	state[0] ^= state[1]; 								\
	state[1] ^= state[0]; 								\
	state[0] ^= state[1]; 								\
	state[2] ^= state[3]; 								\
	state[3] ^= state[2]; 								\
	state[2] ^= state[3]; 								\
	state[4] ^= state[7]; 								\
	state[7] ^= state[4]; 								\
	state[4] ^= state[7]; 								\
	state[5] ^= state[6]; 								\
	state[6] ^= state[5]; 								\
	state[5] ^= state[6]; 								\
	inv_mixcolumns_3(state);							\
	add_tweakey(state, rtk1+24, rtk2_3+24); 			\
	state[4] ^= (state[7] & state[5]);					\
	state[2] ^= (~state[0] | state[1]);					\
	state[5] ^= (state[1] | ~state[6]);					\
	state[7] ^= (~state[3] | state[2]);					\
	state[1] ^= (state[0] & state[3]);					\
	state[6] ^= (state[7] | state[4]);					\
	state[3] ^= (state[2] | state[4]);					\
	state[0] ^= (state[5] | state[6]);					\
	inv_mixcolumns_2(state);							\
	add_tweakey(state, rtk1+16, rtk2_3+16); 			\
	state[3] ^= (state[2] & state[7]);					\
	state[0] ^= (~state[6] | state[5]);					\
	state[7] ^= (state[5] | ~state[4]);					\
	state[2] ^= (~state[1] | state[0]);					\
	state[5] ^= (state[6] & state[1]);					\
	state[4] ^= (state[2] | state[3]);					\
	state[1] ^= (state[0] | state[3]);					\
	state[6] ^= (state[7] | state[4]);					\
	inv_mixcolumns_1(state);							\
	add_tweakey(state, rtk1+8, rtk2_3+8); 				\
	state[1] ^= (state[0] & state[2]);					\
	state[6] ^= (~state[4] | state[7]);					\
	state[2] ^= (state[7] | ~state[3]);					\
	state[0] ^= (~state[5] | state[6]);					\
	state[7] ^= (state[4] & state[5]);					\
	state[3] ^= (state[0] | state[1]);					\
	state[5] ^= (state[6] | state[1]);					\
	state[4] ^= (state[2] | state[3]);					\
	inv_mixcolumns_0(state); 							\
	add_tweakey(state, rtk1, rtk2_3); 					\
	state[5] ^= (state[6] & state[0]);					\
	state[4] ^= (~state[3] | state[2]);					\
	state[0] ^= (state[2] | ~state[1]);					\
	state[6] ^= (~state[7] | state[4]);					\
	state[2] ^= (state[3] & state[7]);					\
	state[1] ^= (state[6] | state[5]);					\
	state[7] ^= (state[4] | state[5]);					\
	state[3] ^= (state[0] | state[1]);					\
})

#endif  // SKINNY128_H_