/* 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 */ /* ------------------------------------------------------------------ zx8 (ps) Test vectors ------------------------------------------------------------------ Key: Password Key (Hex): 50617373776F7264 Keystream: 8A1A89B3221AC4AB9AAB Plaintext: Plaintext Ciphertext: DA76E8DA4C6EA1D3EEA1 -------------------------------------------------------------------- Key: SecretKey Key (Hex): 5365637265744B6579 Keystream: 983283BFE117A0B211E5B433FD0799DCBB43 Plaintext: Secure my Secrets Ciphertext: CB57E0CA937280DF68C5E7569E75FCA8C849 -------------------------------------------------------------------- Key: HQQMG005 Key (Hex): 4851514D47303035 Keystream: 9094D05D6D1F67EAE75C6A6FD59D35 Plaintext: Attack at dawn Ciphertext: D1E0A43C0E74478B937C0E0EA2F33F ------------------------------------------------------------------- Key: HQQMG007 Key (Hex): 4851514D47303037 Keystream: 295C7428FC2C329C627ADD05043F76AC Plaintext: Victory is near Ciphertext: 7F35175C935E4BBC0B09FD6B615E04A6 ------------------------------------------------------------------- Example: echo "Secure my Secrets" | ./zx8_stdio 5365637265744B6579 > ciphertext.out cat ciphertext.out | ./zx8_stdio 5365637265744B6579 ------------------------------------------------------------------- */ #include #include #include #include //----------------------------------------- // // 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; // Function prototypes void zx8_KSA(zx8_CTX *state, size_t keybytes, unsigned char *key); void zx8_PRGA(zx8_CTX *state, size_t n, unsigned char *in, unsigned char *out); //----------------------------------------- // // rm -f zx8_stdio && gcc -O3 -std=c99 zx8_stdio.c -o zx8_stdio // //----------------------------------------- // // ./zx8_stdio $(echo -en 'egN99T8eK6peC2UC' | md5) < Plainfile > CipherFile // // ./zx8_stdio 7d0ef66789aca2cfa6c76db7560554 CipherFile // // cat CipherFile | ./zx8_stdio 7d0ef66789aca2cfa6c76db7560554 > Plainfile // //----------------------------------------- // void ShowUsage(char* ThisName) { fprintf(stderr, "\nUsage : %s Key(in Hex) out\n", ThisName); fprintf(stderr, "\nExample: %s 7d0ef66789aca2cfa6c76db7560554 CipherFile\n", ThisName); fprintf(stderr, "\n %s $(echo -en 'egN99T8eK6peC2UC' | md5) < Plainfile > CipherFile\n", ThisName); fprintf(stderr, "\n cat CipherFile | %s 7d0ef66789aca2cfa6c76db7560554 > Plainfile\n\n", ThisName); } //----------------------------------------- // Main // int main(int argc, char *argv[]) { // Check if a Parameter are passed through char* PrgName; PrgName = argv[0]; if(argc < 2) { ShowUsage(PrgName); return 1; } //----------------------------------------- // // Read the Encryption Keyword // unsigned char KeyWord[256]; int KeyLen; // only char here char KeyHash[512]; char hex[3]; int i; // Make sure the arrays get initialised properly // and wipe out anything which might be there for (i=0; i<256; i++) KeyWord[i] = 0; KeyWord[256] = '\0'; for (i=0; i<512; i++) KeyHash[i] = 0; KeyHash[512] = '\0'; hex[0]=0; hex[1]=0; hex[2]='\0'; // Rebuild KeyWord[] from KeyHash strcpy(KeyHash, argv[1]); KeyLen = strlen(KeyHash); KeyHash[KeyLen] = '\0'; KeyLen = KeyLen/2; for (i=0; i 0) { zx8_PRGA(&ctx, byteRead, inB, outB); // redirect output to stdout fwrite(&outB, sizeof(char), byteRead, stdout); byteRead = fread(&inB, sizeof(char), BUF_SIZE, stdin); } fflush(stdin); fflush(stdout); return 0; } //----------------------------------------- // // Key Schedule Algorithm (ps) // void zx8_KSA(zx8_CTX *state, size_t keybytes, unsigned char *key) { int i; unsigned char j, bufI[128], bufO[128]; // 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 = state->b = 0; } //----------------------------------------- // // Pseudo Random Generation Algorithm (ps) // void zx8_PRGA(zx8_CTX *state, size_t len, unsigned char *in, unsigned char *out) { unsigned char n1, n2, y; while (len--) { // Calculate distant Array Element Indices n1 = state->z[state->a] + state->x[state->a]; n2 = state->z[state->b] + state->x[state->b]; // First Swap randomly selected Array Element swap(state->z[state->a], state->z[n1]); swap(state->x[state->a], state->x[n2]); // Update the global Carry on Array Indices state->a += state->b + (n1^n2); state->b += 1; // Second Swap sequentially cycle over every Array Element swap(state->z[state->b], state->z[n1]); swap(state->x[state->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)); } }