{VERSION 5 0 "IBM INTEL NT" "5.0" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 1 }{CSTYLE "" -1 256 "" 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 257 "" 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 258 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "Geneva" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Text Output" -1 2 1 {CSTYLE "" -1 -1 "Cour ier" 1 10 0 0 255 1 0 0 0 0 0 1 3 0 3 0 }1 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Heading 2" -1 4 1 {CSTYLE "" -1 -1 "Geneva" 1 14 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 2 1 0 1 0 2 2 0 1 }{PSTYLE "Warning" 2 7 1 {CSTYLE "" -1 -1 "" 0 1 0 0 255 1 0 0 0 0 0 0 1 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Title" -1 18 1 {CSTYLE "" -1 -1 "Gene va" 1 18 0 0 0 1 2 1 1 2 2 2 1 1 1 1 }3 1 0 0 12 12 1 0 1 0 2 2 19 1 } {PSTYLE "Author" -1 19 1 {CSTYLE "" -1 -1 "Geneva" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }3 1 0 0 8 8 1 0 1 0 2 2 0 1 }{PSTYLE "Heading 3" -1 256 1 {CSTYLE "" -1 -1 "Geneva" 1 12 0 0 0 1 1 1 2 2 2 2 1 1 1 1 }3 1 0 0 0 0 1 0 1 0 2 2 0 1 }} {SECT 0 {EXCHG {PARA 18 "" 0 "" {TEXT -1 13 "DES functions" }}{PARA 19 "" 0 "" {TEXT -1 23 "\251 Mike May, S. J., 2002" }}{PARA 19 "" 0 " " {TEXT -1 13 "maymk@slu.edu" }}{PARA 256 "" 0 "" {TEXT 256 38 "A coll ection of procedures used in DES" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 147 "To execute D ES we need to define quite a number of functions. It seems best to co llect them on one worksheet so they can all be loaded in at once." }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 182 "The first 4 sections of functions are needed to be able to step through a single round of DES. They de al with form conversion, applying permutations, using XOR, and reading s-boxes." }}{PARA 0 "" 0 "" {TEXT -1 223 "The next section of functio ns produces the f function, so that a round may be done in closer to a single step. These first 5 sections let you through most of the DES \+ Example worksheet that looks at encoding a single block." }}{PARA 0 " " 0 "" {TEXT -1 416 "The last three sections of functions are designed to do operations as a single procedure, so that you can focus on mode s of operating DES rather than on the functioning with a single block. Of those 3 sections, the first deals with key expansion, the second \+ deals with encrypting and decrypting with DES, and the third deals wit h converting a longer message between an ASCII string and a vector of \+ 8-byte hex words." }}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 0 "" }{TEXT 257 39 "Conversion between different data types" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 101 " The first section of code contains functions to co nvert to hex or binary and keep the correct length" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 1465 " #When we convert a decimal equivalent t o hex, we want 2-digit hex\nhex2digit := x -> if x < 16 then cat(`0`,c onvert(x,hex))\n else convert(x,hex) fi:\n #We want \+ to convert an ASCII string of up to 8 characters to a \n #64 bit bina ry string.\nbin64 := proc(shortstring)\n local temp,l1,missing,i, bin str:\n temp := convert(shortstring,bytes):\n l1 := max(1,length(shor tstring)):\n binstr := convert(convert( cat(seq(hex2digit(temp[i]),i= 1..l1)), \n decimal,hex),binary):\n missing := 6 4 -length(binstr):\n cat(seq(`0`,i=1..missing),binstr):\nend:\n #We \+ want to convert an ASCII string of up to 8 characters to a \n #16 cha racter hex string.\nASCII2hex16 := proc(shortstring)\n local temp,l1, missing,i, hexstr:\n temp := convert(shortstring,bytes):\n l1 := max (1,length(shortstring)):\n hexstr := cat(seq(hex2digit(temp[i]),i=1.. l1)):\n missing := 16 -length(hexstr):\n cat(seq(`0`,i=1..missing),h exstr):\nend:\n #We want to convert an hex string of up to 16 charact ers to a \n #64 bit binary string.\nbin64hex := proc(hexstring)\n lo cal temp,l1,missing,i, binstr:\n l1 := 16 - max(1,length(hexstring)): \n temp := cat(seq(\"0\",i=1..l1),hexstring):\n binstr := convert(co nvert( temp,decimal,hex),binary):\n missing := 64 -max(1,length(binst r)):\n cat(seq(\"0\",i=1..missing),binstr):\nend:\n #The contents of s-boxes should be converted to 4 bit binary\nfourbitbin := decnum -> \n substring(convert(convert(decnum+16,binary),string),2..5):" }}} }{SECT 0 {PARA 4 "" 0 "" {TEXT -1 0 "" }{TEXT 258 0 "" }{TEXT -1 30 "S tring permutations and XORing" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 59 "W e want functions to do the various permutation expansions." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 388 "PC1onKey := binstr -> cat(seq(subs tring(binstr,PC1[i]),i=1..56)):\nPC2onKey := binstr -> cat(seq(substri ng(binstr,PC2[i]),i=1..48)):\nexpander := binstr -> cat(seq(substring( binstr,E[i]),i=1..48)):\nInitPerm := binstr -> cat(seq(substring(binst r,IP[i]),i=1..64)):\nFinalPerm := binstr -> cat(seq(substring(binstr,I PI[i]),i=1..64)):\nPPerm := binstr -> cat(seq(substring(binstr,P[i]),i =1..32)):" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 145 "We need to be able \+ to do XOR on 64, 48, and 32 bit strings.\nWe would also like to be abl e to XOR two 64 bit strings that are given in hex format." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 561 "XOR := (a,b) -> if (a=b) then \"0 \" else \"1\" fi:\nxorNbits := proc(a,b,N) \n local aString, bString: \n aString := convert(a,string):\n bString := convert(b,string): \n \+ cat(seq(XOR(substring(aString,i), substring(bString,i)),i=1..N)):\nen d:\nxor64 := (a,b) -> xorNbits(a,b,64):\nxor48 := (a,b) -> xorNbits(a, b,48):\nxor32 := (a,b) -> xorNbits(a,b,32):\n\nxor64hex := proc(a,b)\n local binab, hexab, i, l:\n binab := xor64(bin64hex(a),bin64hex(b)) :\n hexab := convert(convert(parse(binab),decimal,binary),hex):\n l \+ := 16 - length(hexab):\n cat(seq(\"0\",i=1..l),hexab):\nend:" }}}} {SECT 0 {PARA 4 "" 0 "" {TEXT -1 25 "SBoxes and the f function" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 236 "To do the s-box substitutions, we want to be able to cut the 48 bit word into a vector of 6 bit words. \+ Since we have made the SBox a list of tables with 4 bit entries index ed by 6 bit strings, looking up SBox values is straightforward." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 172 "prodbs := proc(binstr)\n l ocal bvec,i: \n bvec := linalg[vector](8):\n for i from 1 to 8 do\n \+ bvec[i] := substring(binstr,i*6-5..i*6):\n od:\n convert(bvec,lis t):\nend: " }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 351 "We now want a si ngle function that corresponds to the f function discussed in class an d in the text. It should takes a round key and a half word, expand th e half word and xor with the round key, then cut the 48 bit result int o 8 6 bit strings that are used for SBox look ups. The results are co ncatenated together and permuted according to the rule." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 227 "fcomb := proc(ri, ki)\n local t1, t2, t3, vec1, vec2, i:\n t1 := xor48(ki,expander(ri)):\n vec1 := pro dbs(t1):\n vec2 := linalg[vector](8,[seq(SBox[i][vec1[i]],i=1..8)]): \n t2 := cat(seq(vec2[i],i=1..8)):\n t3 := PPerm(t2):\nend:" }}}} {SECT 0 {PARA 4 "" 0 "" {TEXT -1 17 "One line commands" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 164 "When we are ready to look at using DES o n more than 64 bits of information at a time we want to be able to do \+ key expansion and DES encryption with single commands." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 78 "keyexpander takes a \+ hex string as input and produces a list of the 16 subkeys." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 794 "keyexpander := proc(hexstring)\n \+ local keybin, binlength, keybin64, PC1key, c, d, key, i: \n keybin:= convert(convert(hexstring,decimal,hex),binary):\n binlength := lengt h(keybin):\n keybin64 := cat(seq(`0`,i=1..(64-binlength)),keybin):\n \+ PC1key := PC1onKey(keybin64):\n c := linalg[vector](17): d := linalg [vector](17):\n key := linalg[vector](16):\n c[1] := substring(PC1ke y,1..28): d[1]:=substring(PC1key,29..56):\n for i from 2 to 17 do\n \+ c[i] := cat(substring(c[i-1],keyshifts[i-1]+1..28),\n \+ substring(c[i-1],1..keyshifts[i-1])):\n d[i] := cat(substring(d [i-1],keyshifts[i-1]+1..28),\n substring(d[i-1],1..key shifts[i-1])):\n od:\n for i from 1 to 16 do\n key[i] := convert( PC2onKey(cat(c[i+1],d[i+1])),string);\n od:\n key := convert(key,lis t):\nend:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 155 "The command qdDEShe x (qd for quick and dirty) uses DES and a vector of subkeys to encode \+ a hex input. The input should be no more than 16 hex digits long." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 86 "The comma nd qdDESASCII encodes an ASCII string that is no more than 8 character s long." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 42 "The command unDEShex decodes a hex string." }}}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 1537 "qdDEShex := proc(hexstring,keyvec)\n local b in2, R, i, keytriv,code1,code2, code3, l1:\n keytriv := cat(seq(`0`,j= 1..48));\n bin2 := InitPerm(bin64hex(hexstring));\n R := linalg[vector ](18):\n R[2] := substring(bin2,33..64);\n R[1] := substring(bin2,1..3 2);\n for i from 1 to 16 do\n R[i+2] := xor32(R[i],fcomb(R[i+1],keyv ec[i]));\n od:\n code1 := cat(R[18],R[17]);\n code2 := FinalPerm(code1 );\n code3 := convert(convert(parse(code2),decimal,binary),hex);\n l1 \+ := length(code3);\n cat(seq(\"0\",i=1..(16-l1)),code3);\nend:\nunDEShe x := proc(hexstring,keyvec)\n local bin2, R, i, keytriv,code1,code2, c ode3, l1:\n keytriv := cat(seq(\"0\",j=1..48));\n bin2 := InitPerm(bin 64hex(hexstring));\n R := linalg[vector](18):\n R[17] := substring(bin 2,33..64);\n R[18] := substring(bin2,1..32);\n for i from 1 to 16 do\n R[17-i] := xor32(R[19-i],fcomb(R[18-i],keyvec[17-i]));\n od:\n code 1 := cat(R[1],R[2]);\n code2 := FinalPerm(code1);\n code3 := convert(c onvert(parse(code2),decimal,binary),hex);\n l1 := length(code3);\n cat (seq(\"0\",i=1..(16-l1)),code3);\nend:\nqdDESASCII := proc(ASCIIstring ,keyvec)\n local bin2, R, i, keytriv,code1,code2, code3, l1:\n keytriv := cat(seq(\"0\",j=1..48));\n bin2 := InitPerm(bin64(ASCIIstring));\n R := linalg[vector](18):\n R[2] := substring(bin2,33..64);\n R[1] := \+ substring(bin2,1..32);\n for i from 1 to 16 do\n R[i+2] := xor32(R[i ],fcomb(R[i+1],keyvec[i]));\n od:\n code1 := cat(R[18],R[17]);\n code2 := FinalPerm(code1);\n code3 := convert(convert(parse(code2),decimal, binary),hex);\n l1 := length(code3);\n cat(seq(\"0\",i=1..(16-l1)),cod e3);\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 26 "More data type co nversions" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 165 "If we are going to u se DES on a longer message, we want to convert the message from an ASC II string to a vector of hex words with each word being 16 characters \+ long." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 1401 " #When we conve rt a decimal equivalent to hex, we want 2-digit hex\nhex2digit := x -> if x < 16 then cat(`0`,convert(x,hex))\n else conver t(x,hex) fi:\nasciistrtohexword := proc(messagestring)\n local string ofhex, lenofstr, roundlen, padding, wordlen, temp, j, i, k:\n #Firs t we convert the ASCII string to a list of decimal equivalents\n #T hen we convert thedecimal numbers to 2 place hex equivalents\n lenofs tr := length(messagestring);\n stringofhex := map(hex2digit, convert( messagestring,bytes)):\n #Time to compute the appropriate lengths\n roundlen := lenofstr + 7 - (lenofstr + 7 mod 8);\n padding := round len - lenofstr:\n wordlen := roundlen/8:\n #Now we put togehter he x words\n temp := linalg[vector](wordlen):\n for i from 1 to (wordle n -1) do\n temp[i] := cat(seq(stringofhex[j],j=(i*8-7)..i*8)):\n o d:\n temp[wordlen] :=cat(seq(stringofhex[j], j=(roundlen-7)..lenofstr ),\n seq(`00`,k=1..padding)):\n #Finally we conv ert the vector to a list\n convert(temp,list):\nend:\nhexwordtoasciis tr := proc(wordlist)\n local l1, temp, i, j, vec1:\n vec1 := convert (wordlist,array):\n l1 := linalg[vectdim](wordlist):\n temp := linal g[vector](8*l1):\n for i from 1 to l1 do\n for j from 1 to 8 do\n \+ temp[j+8*(i-1)] := substring(wordlist[i],2*j-1..2*j):\n od:\n \+ od:\n temp := convert(temp,list):\n convert(map(convert,temp,decima l,hex),bytes):\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 30 "Saving \+ constants and functions" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 216 "Rather than rerunning the DES constants and DES functions worksheets each ti me we can save the functions and constants in a file named DES.m. Thi s file will be created in the same directory as the Maple application. " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 324 "save \nSBox, PC1, PC2, keyshifts, IP, E, P, IPI, \nhex2digit, bin64, ASCII2hex16, bin64hex, \+ fourbitbin,\nPC1onKey, PC2onKey, expander, InitPerm, FinalPerm, PPerm, \nXOR, xorNbits, xor64, xor48, xor32, xor64hex,\nprodbs, fcomb, keyexp ander, qdDEShex, unDEShex,\nqdDESASCII, \nhex2digit, asciistrtohexword , hexwordtoasciistr,\n`DES.m`;" }}{PARA 7 "" 1 "" {TEXT -1 54 "Warning , unassigned variable `SBox` in save statement\n" }}{PARA 7 "" 1 "" {TEXT -1 53 "Warning, unassigned variable `PC1` in save statement\n" } }{PARA 7 "" 1 "" {TEXT -1 53 "Warning, unassigned variable `PC2` in sa ve statement\n" }}{PARA 7 "" 1 "" {TEXT -1 59 "Warning, unassigned var iable `keyshifts` in save statement\n" }}{PARA 7 "" 1 "" {TEXT -1 52 " Warning, unassigned variable `IP` in save statement\n" }}{PARA 7 "" 1 "" {TEXT -1 51 "Warning, unassigned variable `E` in save statement\n" }}{PARA 7 "" 1 "" {TEXT -1 51 "Warning, unassigned variable `P` in sav e statement\n" }}{PARA 7 "" 1 "" {TEXT -1 53 "Warning, unassigned vari able `IPI` in save statement\n" }}}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 59 "Next you need to execute the worksheet on DES Key Expansion" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 0 "" }}}}{MARK "0 0 0" 0 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 0 1 2 33 1 1 }