Sage Tool

profileAmeychekka
Assignment2-BlockCipher.docx.docx

Assignment2 – Simplified DES Encrypt and Decrypt 1

Assignment2 – Simplified DES Encrypt and Decrypt 8

Assignment2- Simplified DES Encrypt and Decrypt

XXXXXXXXXXX

University of Cumberlands

a. Let temp_block denote a Sage variable that contains the output of the first application of function fK (f_K in the Sage example code) while encrypting with Simplified DES. Using subroutines from the example sage Code, write sage code to recover the input block passed to Simplifed DES Decrypt. Meaning reverse the first steps in Simplified DES Encrypt. You may assume that you have the first round key in a variable K1.

LS1_data = [2, 3, 4, 5, 1];

LS2_data = [3, 4, 5, 1, 2];

P10_data = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6];

P8_data = [6, 3, 7, 4, 8, 5, 10, 9];

P4_data = [2, 4, 3, 1];

IP_data = [2, 6, 3, 1, 4, 8, 5, 7];

IPinv_data = [4, 1, 3, 5, 7, 2, 8, 6];

SW_data = [5, 6, 7, 8, 1, 2, 3, 4];

EP_data = [4, 1, 2, 3, 2, 3, 4, 1];

#

# SDES lookup tables

#

S0_data = [[1, 0, 3, 2]

[3, 2, 1, 0],

[0, 2, 1, 3],

[3, 1, 3, 2]];

S1_data = [[0, 1, 2, 3],

[2, 0, 1, 3],

[3, 0, 1, 0],

[2, 1, 0, 3]];

def ApplySBox(X, SBox):

#

#This function Applies the SDES lookup tables for SBox

#

r = 2*X[0] + X[3];

c = 2*X[1] + X[2];

o = SBox[r][c];

return [o & 2, o & 1];

def ApplyPermutation(X, permutation):

#

#This function takes a list of bit positions. And outputs a bit list with the bits taken from X.

# permute the list X

l = len(permutation);

return [X[permutation[j]-1] for j in xrange(l)];

#

# ApplyPermutation functions are used to perform an SDES Permutation

#

def P8(X):

return ApplyPermutation(X, P8_data);

def P10(X):

return ApplyPermutation(X, P10_data);

def IPinv(X):

return ApplyPermutation(X, IPinv_data);

def IP(X):

return ApplyPermutation(X, IP_data);

def SW(X):

return ApplyPermutation(X, SW_data);

def EP(X):

return ApplyPermutation(X, EP_data);

def LS1(X):

return ApplyPermutation(X, LS1_data);

def P4(X):

return ApplyPermutation(X, P4_data);

def LS2(X):

return ApplyPermutation(X, LS2_data);

#

# Below S0 and S1 functions are used to do the SBox substitutions

#

def S0(X):

return ApplySBox(X, S0_data);

def S1(X):

return ApplySBox(X, S1_data);

def concatenate(left, right):

#

#combines the bits together.

#

ret = [left[i] for i in xrange(len(left))];

ret.extend(right);

return ret;

def LeftHalfBits(block):

#

# This function used to get the left half bits from the block

#

l = len(block);

return [block[i] for i in xrange(l/2)];

def RightHalfBits(block):

#

# This function used to get the right half bits from the block.

#

l = len(block);

return [block[i] for i in xrange(l/2, l)];

def XorBlock(block1, block2):

#

#This function is used to get two blocks together.

#

l = len(block1);

if (l != len(block2)):

raise ValueError, "XorBlock arguments must be same length"

return [(block1[i]+block2[i]) % 2 for i in xrange(l)];

def SDESKeySchedule(K):

#

#This function is used to expand an SDES key bit list into two round keys.

#

temp_K = P10(K);

left_temp_K = LeftHalfBits(temp_K);

right_temp_K = RightHalfBits(temp_K);

K1left = LS1(left_temp_K);

K1right = LS1(right_temp_K);

K1temp = concatenate(K1left, K1right);

K1 = P8(K1temp);

K2left = LS2(K1left);

K2right = LS2(K1right);

K2temp = concatenate(K2left, K2right);

K2 = P8(K2temp);

return (K1, K2);

def f_K(block, K):

#

#This function is applied based on the supplied values of K and block.

#

left_block = LeftHalfBits(block);

right_block = RightHalfBits(block);

temp_block1 = EP(right_block);

temp_block2 = XorBlock(temp_block1, K);

left_temp_block2 = LeftHalfBits(temp_block2);

right_temp_block2 = RightHalfBits(temp_block2);

S0_out = S0(left_temp_block2);

S1_out = S1(right_temp_block2);

temp_block3 = concatenate(S0_out, S1_out);

temp_block4 = P4(temp_block3)

temp_block5 = XorBlock(temp_block4, left_block);

output_block = concatenate(temp_block5, right_block)

return output_block;

def SDESEncrypt(plaintext_block, K):

#

# This function applies a single SDES plaintext block encryption for a given plaintext and key as #bit lists.

#

(K1, K2) = SDESKeySchedule(K);

temp_block1 = IP(plaintext_block);

temp_block2 = f_K(temp_block1, K1);

temp_block3 = SW(temp_block2);

temp_block4 = f_K(temp_block3, K2);

output_block = IPinv(temp_block4);

return output_block;

b. Using subroutines from the Sage example code for Simplified DES, write a function to compute Simplified DES Decrypt

As I have defined all functions in detail in the initial one, I am just trying to import everything from the Simplified DES in this code.

//Simplified DES Decrypt Function

sage: from sage.crypto.block_cipher.sdes import SimplifiedDES

sage: sdes = SimplifiedDES()

input_block = IPinv(temp_block2);

def SDESDecrypt(input_block, K):

sage:(K1, K2) = SDESKeySchedule(K);

sage:temp_block1 = IP(input_block);

sage:temp_block2 = f_K (temp_block1, K2);

sage:temp_block3 = SW(temp_block2);

sage:temp_block4 = f_K (temp_block3, K1);

sage: output_block = sdes.decrypt(temp_block4,K);

return output_block;