/* zx8 (ps) - Stream Cipher Algorithm/Source Code */ /* Copyright (c) 2012, Karl-Uwe Frank All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #** zx8 (ps) algorithm developed by Karl-Uwe Frank */ /* Revision: 2012-12-02 Minor change from // Reset the Array Indices Start Point state->a = state->b = 0; into // Reset the Array Indices Start Point state->a = 0; state->b = 1; because this will produce better results on statistical tests like "Bookstack" with low amount of data */ // // gcc -Wall -Wextra -O -std=c99 -pedantic -shared zx8_b1_struct_cy.c -o zx8_b1_struct_cy.so // #include #include #include #include // #include //----------------------------------------- // // zx8_ps ***** #1 * 23.01.2012 ***** // //----------------------------------------- // // Swap Values // // http://rosettacode.org/wiki/Generic_swap#Works_with:_gcc // #define swap(X,Y) do { __typeof__ (X) _T = X; X = Y; Y = _T; } while(0) //----------------------------------------- // // Global Varaiable Definition // typedef struct { unsigned char z[256]; unsigned char x[256]; unsigned char a, b; } zx8_CTX; // Initialise the Structure static zx8_CTX ctx; // Initialise the Key static unsigned char KeyWord[256]; int KeyLen; // Function prototypes void zx8_KSA(zx8_CTX *state, size_t keybytes, unsigned char *key, int bVal); void zx8_PRGA(zx8_CTX *state, size_t n, unsigned char *in, unsigned char *out); //----------------------------------------- // // Key Schedule Algorithm (ps) // void KSA(unsigned char* keyString, int KeyLen, int bVal) { zx8_KSA(&ctx, KeyLen, keyString, bVal); //###printf ("C) z x = %3d %3d\n", ctx.z[0], ctx.x[0]); // Delete the Key immediately after // the internal State has been initialised // to prevent Memory Trade off Attacks searching // for the Keyword int i; for (i=0; i<256; i++) KeyWord[i] = 0; KeyWord[256] = '\0'; KeyLen = 0; } //----------------------------------------- // // Encrypt/Decrypt Binary File(buffered Read/Write) FILE // void Encrypt_file(char* inFile, char* outFile, int inFileSize, int inFileSeekPos) { const int BUF_SIZE = 8192; unsigned char inB[BUF_SIZE], outB[BUF_SIZE]; FILE* fIn = fopen(inFile, "rb"); FILE* fOut = fopen(outFile, "ab"); // append mode // in case there is some kind of header fseek(fIn, inFileSeekPos, SEEK_SET); // Seek to EOF of Outfile rewind(fOut); // read from file int byteRead = 0; int readByteRemain = (inFileSize - inFileSeekPos); int readBufferSize = BUF_SIZE; while (readByteRemain > 0) { if (readBufferSize > readByteRemain) readBufferSize = readByteRemain; // Binary File (buffered Read) byteRead = fread(&inB, sizeof(char), readBufferSize, fIn); // Perform the Encryption/Decryption zx8_PRGA(&ctx, byteRead, inB, outB); // write to file fwrite(&outB, sizeof(char), byteRead, fOut); // Update the file pointer readByteRemain = readByteRemain - byteRead; } fclose(fIn); fflush(fOut); fclose(fOut); } //####################################################################### //----------------------------------------- // // Key Schedule Algorithm (ps) // void zx8_KSA(zx8_CTX *state, size_t keybytes, unsigned char *key, int bVal) { int i; unsigned char j, bufI[128], bufO[128]; // Make sure the arrays get initialised properly // and wipe out anything which might be there for (i=0; i<128; i++) bufI[i] = bufO[i] = 0; j = 0; // Prefill the Arrays for (i=0; i<256; i++) state->z[i] = state->x[i] = i; state->a = state->b = 0; for (i=0; i<256; i++) { zx8_PRGA(state, 128, bufI, bufO); j += (bufO[127] + state->z[i] + key[(i%keybytes)]); swap(state->z[i], state->z[j]); zx8_PRGA(state, 128, bufI, bufO); j += (bufO[127] + state->x[i] + state->z[state->x[j]]); swap(state->x[i], state->x[j]); } // Reset the Array Indices Start Point state->a = 0; state->b = bVal; // 0 or 1 } //----------------------------------------- // // Pseudo Random Generation Algorithm (ps) // void zx8_PRGA(zx8_CTX *state, size_t len, unsigned char *in, unsigned char *out) { unsigned char a, b, n1, n2, y; a = state->a; b = state->b; while (len--) { // Calculate distant Array Element Indices n1 = state->z[a] + state->x[a]; n2 = state->z[b] + state->x[b]; // First Swap randomly selected Array Element swap(state->z[a], state->z[n1]); swap(state->x[a], state->x[n2]); // Update the global Carry on Array Indices a += b + (n1^n2); b += 1; // Second Swap sequentially cycle over every Array Element swap(state->z[b], state->z[n1]); swap(state->x[b], state->x[n2]); // Calculate the internal State Selector Value y = (state->z[n1] ^ state->x[n2]); // Never reveal internal State Values directly *out++ = *in++ ^ (state->z[state->x[y]] ^ (n1+n2)); } state->a = a; state->b = b; }