/* SBox8: 8 bit Pseudo-Random Number Generator Algorithm/Source Code */ /* This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to #** SBox8 algorithm originally designed and developed by Karl-Uwe Frank */ //********************************************************************** // rm -f ./SBox8_for_tests; gcc -std=c99 -O3 -fomit-frame-pointer -mtune=native SBox8_for_tests.c -o SBox8_for_tests // // ./SBox8_for_tests seed key < in_Byte > SBox8_rng_out.bin // // ./SBox8_for_tests 0x2ddd6a2c 0c8c8279b04c4430d69a6454d069458e < ~/Public/zero_4kb.bin > SBox8_rng_out.bin // // ./SBox8_for_tests 0x00f75bce a619e43a58d095f5aaf2c5a235097a9e < ~/Public/zero_4kb.bin > SBox8_rng_out.bin // // ./SBox8_for_tests 0xdeadbeef 04c7a580a0c8e7bf7ec5a0c9aef63c81 < ~/Public/zero_64kb.bin > SBox8_rng_out.bin #include #include #include #include // needed to generate uint32 on a 64bit OS #include /////////////////////////////////////////// #define swap(X,Y) { uint8_t T = X; X = Y; Y = T; } int main (int argc, char *argv[]) { uint8_t a, b, c, d, j; uint8_t SBox[256]; uint32_t seed; // Check if a Seed and a Key is passed through if (argc < 3) { fprintf(stderr, "\nUsage : %s Seed (in Hex) Key(in Hex) out\n", argv[0]); fprintf(stderr, "\nExample: %s 78e68cf3 7d0ef66789aca2cfa6c76db7560554 CipherFile\n", argv[0]); fprintf(stderr, "\n cat CipherFile | %s 78e68cf3 7d0ef66789aca2cfa6c76db7560554 > Plainfile\n\n", argv[0]); return 1; } seed = (int)strtoul(argv[1], NULL, 16); // We have a Keyword uint8_t keyword[256]; uint8_t keyLen; uint8_t hexKey[256]; uint8_t hex[3]; // Make sure the arrays get initialised properly // and wipe out anything which might be there memset(keyword, 0, 256); memset(hexKey, 0, 256); memset(hex, 0, 3); // Rebuild KeyWord[] from KeyHash strcpy(hexKey, argv[2]); keyLen = strlen(hexKey); hexKey[keyLen] = 0; keyLen = keyLen/2; for (uint8_t i=0; i> 8) & 0xff); c = ((seed >> 16) & 0xff); d = ((seed >> 24) & 0xff); } // Shifting in Order to generate an alternating Permutation while (( b % 8) != 3) b++; while (((c % 8) != 5) && ((c % 8) != 7)) c++; while (((d % 8) != 5) && ((d % 8) != 7)) d++; // Initialise the alternating Permutation memset(SBox, 0, sizeof(SBox)); for (int i=0; i<256; i++) { a = (a + (b ^ c) + d); SBox[i] = a; } // Shuffle the Keyword into the Permutation for (int i=0; i<256; i++) { d = i % keyLen; a += SBox[b]; b += SBox[a]; c = a + b + i + keyword[d]; swap(SBox[c], SBox[i]); } // Encrypt/Decrypt Binary File(buffered Read/Write) int bufferRead; const int BUF_SIZE = 65536; unsigned char inB[BUF_SIZE], outB[BUF_SIZE]; memset(inB, 0, sizeof(inB)); memset(outB, 0, sizeof(outB)); fflush(stdin); fflush(stdout); j=0; while ((bufferRead = fread(&inB, sizeof(char), BUF_SIZE, stdin)) > 0) { for (int i=0; i