#include #include #include #include /* 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 */ // gcc -std=c99 -O3 -fomit-frame-pointer -mtune=native sbox8_v2.testksa2.c -o sbox8_v2.testksa2 #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_INIT(); void SBox8_KSA(uint8_t key[], uint8_t keyLen); uint8_t SBox8_PRGA(); //----------------------------------------- // INIT = Initialise the Internal State // void SBox8_INIT() { // SBox initalisation for (int i=0; i<256; i++) { SBox[i] = i; } a = b = c = d = j = 0; } //----------------------------------------- // KSA = Key Schedule Algorithm // void SBox8_KSA(uint8_t key[], uint8_t keyLen) { uint8_t t, k = 0; // Shuffle the Key into the Permutation for (int i=0; i<256; i++) { k = i % keyLen; for (int n=0; n<8; n++) t = SBox8_PRGA(); t += SBox[i] + key[k]; swap(SBox[i], SBox[t]); } } //----------------------------------------- // PRGA = Pseudo-Random Generation Algorithm // uint8_t SBox8_PRGA() { a += SBox[b]; b += SBox[a]; c += a + b + j + SBox[j]; swap(SBox[c], SBox[j]); d = SBox[(SBox[c] + SBox[d]) &0xff]; j++; return ((a + b) ^ (c + d)); } /* 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 maxruns = 256; for (int y=0; y 90000 ){ 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"); } }