Slavi 94 Master Poster Featured Poster

Hey guys,
It's not exactly trouble with coding that I am facing more like trouble with understanding what I have to implement ..

Quick background .. I am trying to implement statistical-saturation attack on block ciphers. The piper that describes the attack has the following algorithm that is used against the block cipher PRESENT(that's what about the paper is)

input: a 8-bit subkey guess sk and the 8-bit input distribution distrib_in[256]
output: the 8-bit output distribution distrib_out[256]

initialize distrib_out[256] to the all-zero state
for each 8-bit values text do
        for each 8-bit values rand do
                fix the 8-bit trial to text and xor with sk
                fix the 8-bit non trial to rand
                apply the sboxes
                apply the permutation
                evaluate the value of the 8 bit trial out
                update distrib_out[out]=distrib_out[out]+distrib_in[text]/256
        end for
end for

So I tried implementing that in python and I get the following:

================= Python code implementing it =====================
===== Fixed bits for simplicity now are 1234????

def algorithm1(sk4bit):
        text = ["".join(seq) for seq in itertools.product("01", repeat=4)] # generate all possible fixed inputs
        rand = ["".join(seq) for seq in itertools.product("01", repeat=4)] # generate all possible rand inputs
        sk4bit = getHexFromBinaryNibble(sk4bit)

        #fixed bits the ****????       
        for t in text:
                for r in rand:
                        # fix and xor text with sk
                        left = xor_strings(getHexFromBinaryNibble(t),sk4bit)
                        #fix rand -> don't need to as nothing is changed to other positions

                        #apply sboxes
                        left = sBox(left)
                        right = sBox(getHexFromBinaryNibble(r))

                        #permutation , 4th bit of left is going to 1st bit of right, and 1st bit of right to 4th bit of left
                        permuted1, permuted2 = permute(left,right)

                        # Algorithm ends here ---

                        #xor with sk
                        permuted1XOR = xor_strings(permuted1,sk4bit)

                        #apply sboxes
                        permuted1XOR = sBox(permuted1XOR)
                        permuted2 = sBox(permuted2)

                        #or last time
                        permuted1XOR2 = xor_strings(permuted1XOR,sk4bit)

                        #print the outcome
                        print 'Input: %s%s Output: %s%s Key: %s' % (t,r,bin(int(permuted1XOR2, 16))[2:].zfill(4),bin(int(permuted2, 16))[2:].zfill(4),sk4bit)

I tried following the algorithm step by step but something is dodgy and the results are not as expected
The part after ==algorithm ends here== is because the block cipher that I am making it for is so simple that it doesn't require additional methods to add the rest of the encryption which is another xor, through xbox and xor again, basically this:

             | | | |    | | | |
             | | | |    | | | |  
             ^ ^ ^ ^    ^ ^ ^ ^  K(i)
             |_|_|_|    |_|_|_|
             |     |    |     |
             |  S  |    |  S  |
             |_ _ _|    |_ _ _|
             | | | |    | | | |
              ____Round 1_____
             | | | \    / | | |
             | | |   --   | | |
             | | | /    \ | | |
             | | | |    | | | |
             ^ ^ ^ ^    ^ ^ ^ ^  K(i+1)
             |_|_|_|    |_|_|_|
             |     |    |     |
             |  S  |    |  S  |
             |_ _ _|    |_ _ _|
             | | | |    | | | |
              ____Round 2_____
             | | | |    | | | |
             ^ ^ ^ ^    ^ ^ ^ ^  K(i+2)
             | | | |    | | | |
             | | | |    | | | |

Any help appreciated ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.