photon.c 5.84 KB
Newer Older
lwc-tester 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "photon.h"

byte PHOTON_key_state[D/2][D];

#define S				4
const byte ReductionPoly = 0x3;
const byte WORDFILTER = ((byte) 1<<S)-1;
int DEBUG = 0;

// These three functions are additional functions to turn PHOTON permutation into keyed and tweakable block cipher
void AddKey(byte state[D][D])
{
	int i, j;
	for(i = 0; i < D / 2; i++)
	{
		for (j = 0; j < D; j++)
		{
			state[i][j] ^= PHOTON_key_state[i][j];
		}
	}
	for(i = D / 2; i < D; i++)
	{
		for (j = 0; j < D; j++)
		{
			state[i][j] ^= PHOTON_key_state[i - D / 2][j];
		}
	}	
}

void AddTweak(byte state[D][D], byte tweak_state[D/2][D])
{
	int i, j;
	for(i = D / 2; i < D; i++)
	{
		for (j = 0; j < D; j++)
		{
			state[i][j] ^= tweak_state[i - D / 2][j];
		}
	}	
}

void AddDomain(byte state[D][D], unsigned char domain)
{
	int i;
	for(i = 0; i < D; i++)
		state[i][1] ^= domain;
}

// Below are round constant different from the original ones. Here, use the lower 4-bit of the constant of LED
const byte RC[ROUND_N] = {0x1, 0x3, 0x7, 0xf, 0xf, 0xe, 0xd, 0xb, 0x7, 0xf, 0xe, 0xc, 0x9, 0x3, 0x7, 0xe, 0xd, 0xa, 0x5, 0xb};

// const byte RC[D][12] = {
// 	{1, 3, 7, 14, 13, 11, 6, 12, 9, 2, 5, 10},
// 	{0, 2, 6, 15, 12, 10, 7, 13, 8, 3, 4, 11},
// 	{2, 0, 4, 13, 14, 8, 5, 15, 10, 1, 6, 9},
// 	{6, 4, 0, 9, 10, 12, 1, 11, 14, 5, 2, 13},
// 	{14, 12, 8, 1, 2, 4, 9, 3, 6, 13, 10, 5},
// 	{15, 13, 9, 0, 3, 5, 8, 2, 7, 12, 11, 4},
// 	{13, 15, 11, 2, 1, 7, 10, 0, 5, 14, 9, 6},
// 	{9, 11, 15, 6, 5, 3, 14, 4, 1, 10, 13, 2}
// };

/* to be completed for one time pass mode */
unsigned long long MessBitLen = 0;

const byte IC[D] = {0, 1, 3, 7, 15, 14, 12, 8};

const byte MixColMatrix[D][D] = {
	{ 2,  4,  2, 11,  2,  8,  5,  6},
	{12,  9,  8, 13,  7,  7,  5,  2},
	{ 4,  4, 13, 13,  9,  4, 13,  9},
	{ 1,  6,  5,  1, 12, 13, 15, 14},
	{15, 12,  9, 13, 14,  5, 14, 13},
	{ 9, 14,  5, 15,  4, 12,  9,  6},
	{12,  2,  2, 10,  3,  1,  1, 14},
	{15,  1, 13, 10,  5, 10,  2,  3}
};

byte sbox[16] = {12, 5, 6, 11, 9, 0, 10, 13, 3, 14, 15, 8, 4, 7, 1, 2};

byte FieldMult(byte a, byte b)
{
	byte x = a, ret = 0;
	int i;
	for(i = 0; i < S; i++) {
		if((b>>i)&1) ret ^= x;
		if((x>>(S-1))&1) {
			x <<= 1;
			x ^= ReductionPoly;
		}
		else x <<= 1;
	}
	return ret&WORDFILTER;
}

void PrintState(byte state[D][D])
{
	if(!DEBUG) return;
	int i, j;
	for(i = 0; i < D; i++){
		for(j = 0; j < D; j++)
			printf("%2X ", state[i][j]);
		printf("\n");
	}
	printf("\n");
}

void PrintState_Column(CWord state[D])
{
	if(!DEBUG) return;
	int i, j;
	for(i = 0; i < D; i++){
		for(j = 0; j < D; j++)
			printf("%2X ", (state[j]>>(i*S)) & WORDFILTER);
		printf("\n");
	}
	printf("\n");
}

void AddConstant(byte state[D][D], int round)
{
	int i;
	for(i = 0; i < D; i++)
		state[i][0] = state[i][0] ^ IC[i] ^ RC[round];
}

void SubCell(byte state[D][D])
{
	int i,j;
	for(i = 0; i < D; i++)
		for(j = 0; j <  D; j++)
			state[i][j] = sbox[state[i][j]];
}

void ShiftRow(byte state[D][D])
{
	int i, j;
	byte tmp[D];
	for(i = 1; i < D; i++) {
		for(j = 0; j < D; j++)
			tmp[j] = state[i][j];
		for(j = 0; j < D; j++)
			state[i][j] = tmp[(j+i)%D];
	}
}

void MixColumn(byte state[D][D])
{
	int i, j, k;
	byte tmp[D];
	for(j = 0; j < D; j++){
		for(i = 0; i < D; i++) {
			byte sum = 0;
			for(k = 0; k < D; k++)
				sum ^= FieldMult(MixColMatrix[i][k], state[k][j]);
			tmp[i] = sum;
		}
		for(i = 0; i < D; i++)
			state[i][j] = tmp[i];
	}
}


#ifdef _TABLE_
tword Table[D][1<<S];
void BuildTableSCShRMCS()
{
	int c, v, r;
	tword tv;
	for(v = 0; v < (1<<S); v++) {
		for(c = 0; c < D; c++){ // compute the entry Table[c][v]
			tv = 0;
			for(r = 0; r < D; r++){
				tv <<= S;
				tv |= (tword) FieldMult(MixColMatrix[r][c], sbox[v]);
			}
			Table[c][v] = tv;
		}
	}
	if(DEBUG){
		printf("tword Table[D][1<<S] = {\n");
		for(c = 0; c < D; c++){ 
			printf("\t{");
			for(v = 0; v < (1<<S); v++) {
				printf("0x%.8XU, ", Table[c][v]);
			}
			printf("}");
			if(v != (1<<S)-1) printf(",");
			printf("\n");
		}
		printf("};\n");
	}
}

void SCShRMCS(byte state[D][D])
{
	int c,r;
	tword v;
	byte os[D][D];
	memcpy(os, state, D*D);

	for(c = 0; c < D; c++){ // for all columns
		v = 0;
		for(r = 0; r < D; r++) // for all rows in this column i after ShiftRow
			v ^= Table[r][os[r][(r+c)%D]];

		for(r = 1; r <= D; r++){
			state[D-r][c] = (byte)v & WORDFILTER;
			v >>= S;
		}
	}
}
#endif

void Step(byte state[D][D], byte tweak_state[D/2][D], int start_rnd, unsigned char domain)
{
	int i;
	AddKey(state); PrintState(state);
	AddTweak(state, tweak_state); PrintState(state);
	for(i = start_rnd; i < start_rnd + STEP_INTER_ROUND_N; i++) {
if(DEBUG) printf("--- Round %d ---\n", i);
		AddDomain(state, domain); PrintState(state);
		AddConstant(state, i); PrintState(state);
#ifndef _TABLE_
		SubCell(state); PrintState(state);
		ShiftRow(state); PrintState(state);
		MixColumn(state); 
#else
		SCShRMCS(state);
#endif
		PrintState(state);
	}
}

void TEM_PHOTON_Permutation(
	unsigned char *state_inout,
	const unsigned char *tweak_in, unsigned char domain)
{
    byte state[D][D];
	byte tweak_state[D/2][D];

    int i;
	int stp;

	for (i = 0; i < D * D; i++)
	{
		state[i / D][i % D] = (state_inout[i / 2] >> (4 * (i & 1))) & 0xf;
	}
	for (i = 0; i < (D / 2) * D; i++)
	{
		tweak_state[i / D][i % D] = (tweak_in[i / 2] >> (4 * (i & 1))) & 0xf;
	}
    
    for (stp = 0; stp < STEP_N; stp++)
	{
		Step(state, tweak_state, stp * STEP_INTER_ROUND_N, domain);
	}
	AddKey(state); PrintState(state);
	AddTweak(state, tweak_state); PrintState(state);

	memset(state_inout, 0, (D * D) / 2);
	for (i = 0; i < D * D; i++)
	{
		state_inout[i / 2] |= (state[i / D][i % D] & 0xf) << (4 * (i & 1));
	}
}