/* verify_sbox_params.c - Don Yang (uguu.org) Check that string encoded by sbox_params.c is usable. 06/08/12 */ #include typedef unsigned char Byte; typedef unsigned int Word; const char *params = "h\32:,<$4~$HhVF+\16tz;Z}8^\1Mt|:0t;^OC|1h|||L|Av>jLt{H*J" "H*\nb9]zIcx3hp9n`Nm*?$F-\rzWdN{\4nC&^KEf\33;z0h\0~z25K25" "\13Tb{R7\2P8\t04KDNBZo|Lt427J27\n42KT\27\37n'Kb9\rb;\14*" "I~PzGPZ!PX\"jg\4.W\16XP;PV\r(ZeRU\16~/.(Xft\22\3h\2=$>BV" "aD&%9@.eD,e~u3&[6\10z\rr\32k(X6\n{\rzbmX\"e$J\16|t3&U1 o" "JR/9n\t\33,7\13PJ{^\21[Z\23[@\36[p^yB\37[D\32\\P$fj\rzx" "\\,\6Dj[if[;D~BjYj&?B`\16\rR1\5" "&1Af[j:_4FiDDhDF9DP>\6@\2aP\6\n4Zv@\0b^}7j\7Mh\4NTrhld"; /* Reduce bit dependencies */ static void AdjustSingleBitDependencies(Byte *sbox) { int bit, i, j, x; Byte current, neighbor, tmp; for(bit = 0; bit < 8; bit++) { for(i = 0; i < 256; i++) { current = sbox[i]; neighbor = sbox[i ^ (1 << bit)]; x = current ^ neighbor; if( (x & (x - 1)) == 0 ) { j = i ^ (1 << ((bit + 1) % 8)); tmp = sbox[i]; sbox[i] = sbox[j]; sbox[j] = tmp; } } } } /* Initialize sbox from 3 offset parameters */ static void InitSbox(int offset1, int offset2, int offset3, Byte *sbox) { int i, j; for(i = 0; i < 256; i++) { j = ((i + offset1) & 0x7f) | (i & 0x80); j = (((j >> 1) + offset2) & 0x7f) | ((j << 7) & 0x80); j = (((j >> 1) + offset3) & 0x7f) | ((j << 7) & 0x80); sbox[i] = (Byte)j; } AdjustSingleBitDependencies(sbox); AdjustSingleBitDependencies(sbox); } #define ROTL32(value, r) \ (((value) << r) | ((value) >> (32 - r))) /* Compute hash of sbox */ static Word GetHash(const Byte *sbox) { const Word c1 = 0xcc9e2d51; const Word c2 = 0x1b873593; Word h = 42; /* seed */ Word *data = (Word*)sbox; Word k; int i; for(i = 0; i < 64; i++) { k = *data++; k *= c1; k = ROTL32(k, 15); k *= c2; h ^= k; h = ROTL32(h, 13); h = h * 5 + 0xe6546b64; } h ^= h >> 16; h *= 0x85ebca6b; h ^= h >> 13; h *= 0xc2b2ae35; h ^= h >> 16; return h; } int main(/*@unused@*/int argc, /*@unused@*/char **argv) { Byte sbox[256]; int i; for(i = 0; i < 9 * 16 * 3; i += 3) { InitSbox(params[i] ^ 81, params[i + 1] ^ 25, params[i + 2] ^ 16, sbox); printf("%08x\n", GetHash(sbox)); } return 0; }