#include #include #include #include #include //----------------------------------------- // Swap Values // #define swap(X,Y) { uint8_t T = X; X = Y; Y = T; } // Returns 1 if LITTLE-ENDIAN or 0 if BIG-ENDIAN #include int is_little_endian() { union { uint8_t c[4]; uint32_t i; } data; data.i = 0x12345678; return (data.c[0] == 0x78); } static uint8_t a, b, c, d, j; static uint8_t SBox[256]; void SBox8_PIA(uint32_t seed); void SBox8_KSA(uint8_t key[], uint16_t keyLen); uint8_t SBox8_PRGA(); //----------------------------------------- // PIA = Permutation Initialisation Algorithm // 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 for (int i=0; i<256; i++) { a += t; SBox[i] = a; } } //----------------------------------------- // KSA = Key Schedule Algorithm // void SBox8_KSA(uint8_t key[], uint16_t keyLen) { // Shuffle the key into the Permutation 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]); } j=0; } //----------------------------------------- // 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)); } //---------------------------------------------------------------------------- // SBox8 8bit Hash void p8(const void *data, int len, uint32_t seed, void *out) { const uint8_t * dataByte = (const uint8_t*)data; // DEBUG uint8_t debug = 0; if (debug) { fprintf(stderr, "\n-------------------------------\n"); fprintf(stderr, "seed ==> %08x\n", seed); fprintf(stderr, "data ==> "); for (int n=0; n> 24) & 0xff); Key[1] = ((seed >> 16) & 0xff); Key[2] = ((seed >> 8) & 0xff); Key[3] = ( seed & 0xff); // Initialisation of SBoxes SBox8_PIA(seed); // Shuffle the Key into the SBox Permutation SBox8_KSA(Key, 4); } // Reset the Key Length keyLen = 256; // Offset for j j = 0; // Hash the Content for (int i=0; i "); for (int i=0; i