#include #include #include #include /* The code below for SBox8_PIA was copied from * http://www.freecx.co.uk/crypto/SBox8/Definition_SBox8.php. See that * page for licensing information */ /* It has been modified to compute a histogram of byte values in the SBox[] * array after the KSA has been run */ #define swap(X,Y) { uint8_t T = X; X = Y; Y = T; } static uint8_t a, b, c, d, j; static uint8_t SBox[256]; void SBox8_PIA(uint32_t seed) { if (seed == 0) { a = 14; b = 59; c = 23; d = 77; } else { a = ( seed & 0xff); b = ((seed >> 8) & 0xff); c = ((seed >> 16) & 0xff); d = ((seed >> 24) & 0xff); } // Shifting to the next odd Value of each Variable // in Order to generate an alternating Permutation uint8_t t = ((b ^ c) + d); t |= 1; // Initialise the alternating Permutation // into the internal State Array for (int i=0; i<256; i++) { a += t; SBox[i] = a; } } void SBox8_KSA(uint8_t key[], uint8_t keyLen) { // Shuffle the Key into the Permutation // of the internal State Array for (int i=0; i<256; i++) { d = i % keyLen; a += SBox[b]; b += SBox[a]; c = a + b + i + key[d]; swap(SBox[c], SBox[i]); } // a,b and c are not explicitly set to zero // but keep on carrying over their values j=0; } /* Test harness code below */ int main(int argc, char ** argv) { /* 75, 124, 88 were picked at random */ uint8_t key[4] = {75, 0, 0, 0}; unsigned int sbox_histogram[256][256]; memset(&sbox_histogram, 0, sizeof(int)*256*256); uint16_t maxdeeps = 256; for (int y=0; y<256; y++) { key[1] = y & 0xff; for (int x=0; x<256; x++) { key[2] = x & 0xff; for (int i=0; i<256; i++) { SBox8_PIA(0x000000ff); key[3] = i & 0xff; SBox8_KSA(key, 4); for (int idx=0; idx<256; idx++) { sbox_histogram[idx][SBox[idx]]++; } } } } printf("Histogram of SBox[] after KSA for key[2-3] from 0x0 to 0xffff:\n"); printf(" SBox[i]:\n "); for (int i=0; i<256; i++) { printf("%7d ", i); } printf("\nVal: "); for (int i=0; i<256; i++) { printf("======="); } printf("\n"); for (int byte=0; byte<256; byte++) { printf("%02x: ", byte); for (int idx=0; idx<256; idx++) { if ( sbox_histogram[idx][byte] > 100000 ){ printf("***%7d ", sbox_histogram[idx][byte]); } else if ( sbox_histogram[idx][byte] < 24000 ){ printf("---%7d ", sbox_histogram[idx][byte]); } else { printf("%10d ", sbox_histogram[idx][byte]); } } printf("\n"); } }