# /* 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 # Keystream: 8A1A89B3221AC4AB9AAB # Plaintext: Plaintext # Ciphertext: DA76E8DA4C6EA1D3EEA1 #-------------------------------------------------------------------- # Key: SecretKey # Keystream: 983283BFE117A0B211E5B433FD0799DCBB43 # Plaintext: Secure my Secrets # Ciphertext: CB57E0CA937280DF68C5E7569E75FCA8C849 #-------------------------------------------------------------------- # Key: HQQMG005 # Keystream: 9094D05D6D1F67EAE75C6A6FD59D35 # Plaintext: Attack at dawn # Ciphertext: D1E0A43C0E74478B937C0E0EA2F33F #------------------------------------------------------------------- # Key: HQQMG007 # Keystream: 295C7428FC2C329C627ADD05043F76AC # Plaintext: Victory is near # Ciphertext: 7F35175C935E4BBC0B09FD6B615E04A6 #------------------------------------------------------------------- # import sys, os #----------------------------------------- # # zx8_ps ***** #1 * 23.01.2012 ***** # #----------------------------------------- # # Swap Values # # (primitive Way for some Kind of ANSI-C # Compatibility when comparing Listings) # def swap(X,Y): return Y, X # ----------------------------------------- # # Global Varaiable Definition # KeyWord = bytearray(256) KeyLen = int(0) # Secret State Arrays z = bytearray(256) x = bytearray(256) # Global Carry on Array Indices a = int(0) b = int(0) #----------------------------------------- # # Key Schedule Algorithm (ps) # def KSA(): # Key must be at least 12 Characters long # and should have >= 60-Bit of Entropy global KeyWord, KeyLen global z, x, a, b i = j = k = n = t = int(0) # Prefill the Arrays for i in range(256): z[i] = i x[i] = i for i in range(256): k = (i % KeyLen) for n in range(128): t = PRGA() j = (t + j + z[i] + KeyWord[k]) % 256 z[i], z[j] = swap(z[i], z[j]) for n in range(128): t = PRGA() j = (t + j + x[i] + z[x[j]]) % 256 x[i], x[j] = swap(x[i], x[j]) # Reset the Array Indices Start Point a = int(0) b = int(0) #----------------------------------------- # # Pseudo Random Generation Algorithm (ps) # def PRGA(): global z, x, a, b n1 = n2 = y = m = bytearray(1) # Calculate distant Array Element Indices n1 = (z[a] + x[a]) % 256 n2 = (z[b] + x[b]) % 256 # First Swap randomly selected Array Element z[a], z[n1] = swap(z[a], z[n1]) x[a], x[n2] = swap(x[a], x[n2]) # Update the global Carry on Array Indices a = (a + b + (n1^n2)) % 256 b = (b + 1) % 256 # Second Swap sequentially cycle over every Array Element z[b], z[n1] = swap(z[b], z[n1]) x[b], x[n2] = swap(x[b], x[n2]) # Calculate the internal State Selector Value y = (z[n1] ^ x[n2]) % 256 # Calculate the internal State Protection Value m = (n1 + n2) % 256 # Never reveal internal State Values directly return (z[x[y]] ^ m) % 256