PHOTONPHOTON is light-weight cryptography method for hashing and is based on an AES-type approach. It can create 80-bit, 128-bit, 160-bit, 224-bit and 256-bit hashes [paper]. |
Outline
The PHOTON-n/r/r' variants can be defined with its internal parameters:
n: the bitsize of the hash output t: the bitsize of the internal state (we have t = c + r), which is also the size of the internal permutation P c: the bitsize of the capacity part of the internal state r: the bitsize of the bitrate part of the internal state (or the message block bitsize) d: the number of cell columns and rows in the internal permutation s: the bitsize of one cell in the internal permutation
Code
The following shows a C++ version of the code:
/* Thanks to Chien-Ning Chen for correcting a bug in previous version * * This is the simple implementation with table lookup * The fastest implementation is yet to be posted. * * Last Modified:04 December 2013 */ #include stdlib.h #include stdio.h #include string.h #include math.h #include "photon.h" #if defined(_PHOTON256_) || defined(_AES_) || defined(_PHOTONAES_) #define S 8 const byte ReductionPoly = 0x1b; #else #define S 4 const byte ReductionPoly = 0x3; #endif const byte WORDFILTER = ((byte) 1<<S)-1; int DEBUG = 0; /* to be completed for one time pass mode */ unsigned long long MessBitLen = 0; const byte RC[D][12] = { #if defined(_PHOTON80_) {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}, {7, 5, 1, 8, 11, 13, 0, 10, 15, 4, 3, 12}, {5, 7, 3, 10, 9, 15, 2, 8, 13, 6, 1, 14} #elif defined(_PHOTON128_) || defined(_PHOTON256_) {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}, {7, 5, 1, 8, 11, 13, 0, 10, 15, 4, 3, 12}, {5, 7, 3, 10, 9, 15, 2, 8, 13, 6, 1, 14} #elif defined(_PHOTON160_) || defined(_PHOTON192_) {1, 3, 7, 14, 13, 11, 6, 12, 9, 2, 5, 10}, {0, 2, 6, 15, 12, 10, 7, 13, 8, 3, 4, 11}, {3, 1, 5, 12, 15, 9, 4, 14, 11, 0, 7, 8}, {4, 6, 2, 11, 8, 14, 3, 9, 12, 7, 0, 15}, {2, 0, 4, 13, 14, 8, 5, 15, 10, 1, 6, 9}, {7, 5, 1, 8, 11, 13, 0, 10, 15, 4, 3, 12}, {5, 7, 3, 10, 9, 15, 2, 8, 13, 6, 1, 14} #elif defined(_PHOTON224_) {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} #elif defined(_AES_) || defined(_PHOTONAES_) {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}, {7, 5, 1, 8, 11, 13, 0, 10, 15, 4, 3, 12} #endif }; const byte MixColMatrix[D][D] = { #if defined(_PHOTON80_) { 1, 2, 9, 9, 2}, { 2, 5, 3, 8, 13}, {13, 11, 10, 12, 1}, { 1, 15, 2, 3, 14}, {14, 14, 8, 5, 12} #elif defined(_PHOTON128_) { 1, 2, 8, 5, 8, 2}, { 2, 5, 1, 2, 6, 12}, {12, 9, 15, 8, 8, 13}, {13, 5, 11, 3, 10, 1}, { 1, 15, 13, 14, 11, 8}, { 8, 2, 3, 3, 2, 8} #elif defined(_PHOTON160_) || defined(_PHOTON192_) { 1, 4, 6, 1, 1, 6, 4}, { 4, 2, 15, 2, 5, 10, 5}, { 5, 3, 15, 10, 7, 8, 13}, {13, 4, 11, 2, 7, 15, 9}, { 9, 15, 7, 2, 11, 4, 13}, {13, 8, 7, 10, 15, 3, 5}, { 5, 10, 5, 2 , 15, 2, 4} #elif defined(_PHOTON224_) { 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} #elif defined(_PHOTON256_) { 2, 3, 1, 2, 1, 4}, { 8, 14, 7, 9, 6, 17}, { 34, 59, 31, 37, 24, 66}, {132, 228, 121, 155, 103, 11}, { 22, 153, 239, 111, 144, 75}, {150, 203, 210, 121, 36, 167} #elif defined(_AES_) {2,3,1,1}, {1,2,3,1}, {1,1,2,3}, {3,1,1,2} #elif defined(_PHOTONAES_) { 1, 2, 1, 4}, { 4, 9, 6, 17}, {17, 38, 24, 66}, {66, 149, 100, 11} #endif }; #if defined(_PHOTON256_) || defined(_AES_) || defined(_PHOTONAES_) byte sbox[256] = { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x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d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; #else byte sbox[16] = {12, 5, 6, 11, 9, 0, 10, 13, 3, 14, 15, 8, 4, 7, 1, 2}; #endif 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 printDigest(const byte* digest) { int i; for(i = 0; i < DIGESTSIZE/8; i++) printf("%.2x", digest[i]); printf("\n"); } void AddKey(byte state[D][D], int round) { int i; for(i = 0; i < D; i++) state[i][0] ^= RC[i][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<>3]<<4) | (str[(BitOffSet>>3)+1]>>4)); else return str[BitOffSet>>3]; #else #if 0 int ByteIndex = BitOffSet >> 3; int BitIndex = BitOffSet & 0x7; byte localFilter = (((byte)1)<<NoOfBits) - 1; if(BitIndex+ NoOfBits <= 8) { return (str[ByteIndex] >>(8-BitIndex-NoOfBits)) & localFilter; } else { u32 tmp = ((((u32) str[ByteIndex])<<8)&0xFF00) | (((u32) str[ByteIndex+1])&0xFF); return ((byte)(tmp>>(16-BitIndex-NoOfBits))) & localFilter; } #endif return (str[BitOffSet>>3] >> (4-(BitOffSet&0x4))) & WORDFILTER; #endif } void WordXorByte(byte state[D][D], const byte*str, int BitOffSet, int WordOffSet, int NoOfBits) { int i = 0; while(i < NoOfBits) { state[(WordOffSet+(i/S))/D][(WordOffSet+(i/S))%D] ^= GetByte(str, BitOffSet+i, min(S, NoOfBits-i))<<(S-min(S,NoOfBits-i)); i += S; } } /* ensure NoOfBits <=8 */ void WriteByte(byte*str, byte value, int BitOffSet, int NoOfBits) { int ByteIndex = BitOffSet >> 3; int BitIndex = BitOffSet & 0x7; byte localFilter = (((byte)1)<<NoOfBits) - 1; value &= localFilter; if(BitIndex+ NoOfBits <= 8) { str[ByteIndex] &= ~(localFilter<<(8-BitIndex-NoOfBits)); str[ByteIndex] |= value<<(8-BitIndex-NoOfBits); } else { u32 tmp = ((((u32) str[ByteIndex])<<8)&0xFF00) | (((u32) str[ByteIndex+1])&0xFF); tmp &= ~((((u32)localFilter)&0xFF)<<(16-BitIndex-NoOfBits)); tmp |= (((u32)(value))&0xFF)<<(16-BitIndex-NoOfBits); str[ByteIndex] = (tmp>>8)&0xFF; str[ByteIndex+1] = tmp&0xFF; } } void WordToByte(byte state[D][D], byte*str, int BitOffSet, int NoOfBits) { int i = 0; while(i < NoOfBits) { WriteByte(str, (state[i/(S*D)][(i/S)%D] & WORDFILTER)>>(S-min(S, NoOfBits-i)), BitOffSet+i, min(S, NoOfBits-i)); i += S; } } void PermutationOnByte(byte* in, int R) { byte state[D][D]; int i; for(i = 0; i < D*D; i++) state[i/D][i%D] = GetByte(in, i*S, S); Permutation(state, R); WordToByte(state, in, 0, D*D*S); } void Init(byte state[D][D]) { int i,j; MessBitLen = 0; for(i = 0; i < D; i++) for(j = 0; j < D; j++) state[i][j] = 0; byte presets[3]; presets[0] = (DIGESTSIZE>>2) & 0xFF; presets[1] = RATE & 0xFF; presets[2] = RATEP & 0xFF; WordXorByte(state, presets, 0, D*D-24/S, 24); } void CompressFunction(byte state[D][D], const byte* mess, int BitOffSet) { WordXorByte(state, mess, BitOffSet, 0, RATE); Permutation(state, ROUND); } /* assume DIGESTSIZE is multiple of RATEP, RATEP is multiple of S */ void Squeeze(byte state[D][D], byte*digest) { int i = 0; while(1){ WordToByte(state, digest, i, min(RATEP, DIGESTSIZE-i)); i += RATEP; if(i >= DIGESTSIZE) break; Permutation(state, ROUND); } } void hash(byte* digest,const byte* mess, int BitLen) { byte state[D][D], padded[(int)ceil(RATE/8.0) + 1]; Init(state); int MessIndex = 0; while(MessIndex <= (BitLen-RATE)) { CompressFunction(state, mess, MessIndex); MessIndex += RATE; } int i,j; for(i = 0; i < (ceil(RATE/8.0)+1); i++) padded[i] = 0; j = ceil((BitLen - MessIndex)/8.0); for(i = 0; i < j; i++) padded[i]=mess[(MessIndex/8)+i]; padded[i] = 0x80; CompressFunction(state, padded, MessIndex&0x7); Squeeze(state, digest); }