{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 "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 1 }{CSTYLE "2D Output" 2 20 "" 0 1 0 0 255 1 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 256 "" 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 257 "" 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 258 "Courier" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 259 "" 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 260 "" 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 261 "" 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 }{PSTYLE "Normal " -1 0 1 {CSTYLE "" -1 -1 "Times" 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 "Heading 2" -1 4 1 {CSTYLE "" -1 -1 "Times" 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 "Heading 3" -1 5 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 1 1 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Maple Outpu t" 0 11 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }3 3 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "" 11 12 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }1 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Title" -1 18 1 {CSTYLE "" -1 -1 "Times" 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 "Times" 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 }} {SECT 0 {EXCHG {PARA 18 "" 0 "" {TEXT -1 14 "AES Encryption" }}{PARA 19 "" 0 "" {TEXT -1 36 "\251Mike May, S.J., 2002, maymk@slu.edu" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart;" }}}{EXCHG {PARA 0 " " 0 "" {TEXT -1 357 "In working through an implementation of the crypt ographic system AES (Rijndael), it is easiest to break the task into s everal pieces, some of which were explained in previous worksheets. \+ Encryption with AES uses a number of rounds that are made up of 4 bas ic operations, Byte Substitution (BS), Shift Rows (SR), Mix Columns (M C), and Add Round Key (ARC)." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 251 "To use these 4 operations we need to think of \+ 8 bit bytes as a number of different data types. The preliminary sect ion is mainly concerned with functions for conversion of data type. T hese functions were developed in the worksheet on S-Box creation." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 170 "Byte Sub stitution is done with a look up table that is called an S-Box, which \+ was developed in an earlier worksheet. Here we simply recall the resu lts of that worksheet." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 222 "The Add Round Key operation XORs a round key with t he current message block. The process for expanding the key into a se quence of round keys was developed in a previous worksheet. We recall the results of that worksheet." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 184 "Once the SBoxes and key expansion are es tablished it is easy to construct the 4 basic operations used in a rou nd of encryption and put them together to encrypt and decrypt a plaint ext." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 146 " Having walked carefully through the steps, we then look at one line co mmands for encryption and decryption and save these commands for futur e use." }}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 13 "Preliminaries" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 26 "Data type conversion rules" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 920 "intToBits := intValue -> \n substring(convert(convert(intValue+256, binary), string), 2..9):\n bitToList := bitWord -> \n [seq(parse(substring(bitWord,i)), i=1..8 )]:\nlistToPoly := bitList ->\n sort(sum(bitList[j]*alpha^(8-j), j=1 ..8)):\npolyToInt := poly -> subs(alpha=2, poly):\nhexTo8Bits := hexPa ir -> substring(convert(convert(\n convert(hexPair,decimal,hex)+256 ,binary),string),2..9):\n\nlistToBits := bitList -> cat(seq(convert(bi tList[i],string),i=1..8)):\nbitToInt := bitWord -> convert(parse(bitWo rd),decimal,binary):\nlistToInt := bitList ->\n sort(sum(bitList[j]* 2^(8-j), j=1..8)):\npolyToList := poly -> [seq(coeff(poly,alpha, 8-i), i=1..8)]:\nintToHex := intValue -> \n substring(convert(convert(int Value+256, hex), string), 2..3):\n\npolyToBits := poly -> intToBits(po lyToInt( poly)):\nbitToPoly := bitWord -> listToPoly(bitToList(bitWord )):\nintToPoly := intValue -> listToPoly(bitToList(intToBits(intValue) )):" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 47 "Rules for converting betwe en lists and matrices" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 361 "l istToMatrix := list -> matrix(4,4,list):\nlistToMatrix2 := list ->lina lg[transpose](matrix(4,4,list)):\nmatrixToList := mat -> ListTools[Fla tten](convert(mat,listlist)):\nmatrixToList2 := mat -> \n ListTools[ Flatten](convert(linalg[transpose](mat),listlist)):\nmatrixToHex := bi tMatrix -> cat(op(matrixToList2(\n map(x -> intToHex(bitToInt(x) ),bitMatrix)))):" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 9 "Constants" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 51 "genPoly := alpha^8 + alpha^4 + alpha^3 + alpha + 1:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 269 "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), s ubstring(bString,i)),i=1..N)):\nend:\nxor8 := (a,b) -> xorNbits(a,b,8) :" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 16 "S-Box definition" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 27 "The S-Box as a lookup table" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6420 "SBoxTable :=table([\"01000 010\" = \"00101100\", \"10101000\" = \"11000010\", \"10110110\" = \"01 001110\", \"00011010\" = \"10100010\", \"00111000\" = \"00000111\", \" 11011000\" = \"01100001\", \"00000101\" = \"01101011\", \"00101111\" = \"00010101\", \"00110101\" = \"10010110\", \"00111111\" = \"01110101 \", \"10000010\" = \"00010011\", \"00000110\" = \"01101111\", \"001000 11\" = \"00100110\", \"10010010\" = \"01001111\", \"11000000\" = \"101 11010\", \"11010000\" = \"01110000\", \"10111010\" = \"11110100\", \"1 0010111\" = \"10001000\", \"10101011\" = \"01100010\", \"11100110\" = \+ \"10001110\", \"11101100\" = \"11001110\", \"00011101\" = \"10100100\" , \"00000000\" = \"01100011\", \"10100010\" = \"00111010\", \"11000001 \" = \"01111000\", \"00011001\" = \"11010100\", \"01101110\" = \"10011 111\", \"11101011\" = \"11101001\", \"11101111\" = \"11011111\", \"001 00111\" = \"11001100\", \"11110100\" = \"10111111\", \"00000001\" = \" 01111100\", \"00011011\" = \"10101111\", \"01110111\" = \"11110101\", \+ \"11010101\" = \"00000011\", \"00010111\" = \"11110000\", \"00111100\" = \"11101011\", \"10111011\" = \"11101010\", \"01000000\" = \"0000100 1\", \"11111011\" = \"00001111\", \"01111001\" = \"10110110\", \"10110 000\" = \"11100111\", \"10100100\" = \"01001001\", \"11000010\" = \"00 100101\", \"11110111\" = \"01101000\", \"00110100\" = \"00011000\", \" 01011100\" = \"01001010\", \"00001101\" = \"11010111\", \"00111110\" = \"10110010\", \"01010010\" = \"00000000\", \"01001111\" = \"10000100 \", \"11000101\" = \"10100110\", \"11110110\" = \"01000010\", \"011101 10\" = \"00111000\", \"00100010\" = \"10010011\", \"00101011\" = \"111 10001\", \"11001000\" = \"11101000\", \"01010110\" = \"10110001\", \"1 0101111\" = \"01111001\", \"11011010\" = \"01010111\", \"11111010\" = \+ \"00101101\", \"00101110\" = \"00110001\", \"11011001\" = \"00110101\" , \"11100000\" = \"11100001\", \"01101111\" = \"10101000\", \"11100100 \" = \"01101001\", \"01000101\" = \"01101110\", \"00001000\" = \"00110 000\", \"00000010\" = \"01110111\", \"00011000\" = \"10101101\", \"000 10100\" = \"11111010\", \"01000001\" = \"10000011\", \"10101110\" = \" 11100100\", \"10100110\" = \"00100100\", \"01110000\" = \"01010001\", \+ \"01010100\" = \"00100000\", \"11100001\" = \"11111000\", \"10111000\" = \"01101100\", \"00100110\" = \"11110111\", \"10110011\" = \"0110110 1\", \"11000100\" = \"00011100\", \"11100111\" = \"10010100\", \"11101 010\" = \"10000111\", \"00001100\" = \"11111110\", \"00110110\" = \"00 000101\", \"01010000\" = \"01010011\", \"11101000\" = \"10011011\", \" 10001110\" = \"00011001\", \"11001001\" = \"11011101\", \"11001011\" = \"00011111\", \"10111100\" = \"01100101\", \"00100100\" = \"00110110 \", \"01011110\" = \"01011000\", \"01001000\" = \"01010010\", \"100000 11\" = \"11101100\", \"11100101\" = \"11011001\", \"11011101\" = \"110 00001\", \"00010000\" = \"11001010\", \"01001101\" = \"11100011\", \"0 1111010\" = \"11011010\", \"11010100\" = \"01001000\", \"00101100\" = \+ \"01110001\", \"01011011\" = \"00111001\", \"01011101\" = \"01001100\" , \"10011100\" = \"11011110\", \"01001110\" = \"00101111\", \"11001110 \" = \"10001011\", \"00011111\" = \"11000000\", \"00101010\" = \"11100 101\", \"11110000\" = \"10001100\", \"00110111\" = \"10011010\", \"111 01101\" = \"01010101\", \"11100011\" = \"00010001\", \"10011011\" = \" 00010100\", \"01010001\" = \"11010001\", \"10100101\" = \"00000110\", \+ \"01011111\" = \"11001111\", \"01100001\" = \"11101111\", \"10001010\" = \"01111110\", \"00101001\" = \"10100101\", \"00110000\" = \"0000010 0\", \"01100111\" = \"10000101\", \"10101100\" = \"10010001\", \"11010 010\" = \"10110101\", \"00100000\" = \"10110111\", \"10000101\" = \"10 010111\", \"01101001\" = \"11111001\", \"00000100\" = \"11110010\", \" 10110100\" = \"10001101\", \"01100010\" = \"10101010\", \"01101010\" = \"00000010\", \"01000111\" = \"10100000\", \"00111001\" = \"00010010 \", \"11010001\" = \"00111110\", \"10010011\" = \"11011100\", \"100111 01\" = \"01011110\", \"01110101\" = \"10011101\", \"11001111\" = \"100 01010\", \"00001011\" = \"00101011\", \"01111111\" = \"11010010\", \"1 1010011\" = \"01100110\", \"00011100\" = \"10011100\", \"11111110\" = \+ \"10111011\", \"00011110\" = \"01110010\", \"10000001\" = \"00001100\" , \"10001011\" = \"00111101\", \"00010110\" = \"01000111\", \"10010100 \" = \"00100010\", \"10110101\" = \"11010101\", \"01110011\" = \"10001 111\", \"11011111\" = \"10011110\", \"11111001\" = \"10011001\", \"101 01010\" = \"10101100\", \"10111101\" = \"01111010\", \"11000111\" = \" 11000110\", \"11110010\" = \"10001001\", \"01101000\" = \"01000101\", \+ \"10010000\" = \"01100000\", \"00110001\" = \"11000111\", \"01011000\" = \"01101010\", \"10000000\" = \"11001101\", \"10000100\" = \"0101111 1\", \"10001111\" = \"01110011\", \"10110111\" = \"10101001\", \"10011 000\" = \"01000110\", \"01111101\" = \"11111111\", \"10000111\" = \"00 010111\", \"11110101\" = \"11100110\", \"11000110\" = \"10110100\", \" 10110010\" = \"00110111\", \"01101011\" = \"01111111\", \"01111100\" = \"00010000\", \"10110001\" = \"11001000\", \"00111011\" = \"11100010 \", \"01000100\" = \"00011011\", \"11111111\" = \"00010110\", \"011001 10\" = \"00110011\", \"01111110\" = \"11110011\", \"10100001\" = \"001 10010\", \"00101101\" = \"11011000\", \"01011010\" = \"10111110\", \"0 0111101\" = \"00100111\", \"10101101\" = \"10010101\", \"10000110\" = \+ \"01000100\", \"00010010\" = \"11001001\", \"11110001\" = \"10100001\" , \"01101101\" = \"00111100\", \"10011010\" = \"10111000\", \"01000110 \" = \"01011010\", \"01001001\" = \"00111011\", \"10100000\" = \"11100 000\", \"11101110\" = \"00101000\", \"11111101\" = \"01010100\", \"000 01111\" = \"01110110\", \"00111010\" = \"10000000\", \"11100010\" = \" 10011000\", \"01110001\" = \"10100011\", \"11111000\" = \"01000001\", \+ \"11000011\" = \"00101110\", \"00010101\" = \"01011001\", \"01010011\" = \"11101101\", \"01110010\" = \"01000000\", \"10111110\" = \"1010111 0\", \"00001110\" = \"10101011\", \"00010001\" = \"10000010\", \"10001 100\" = \"01100100\", \"01001010\" = \"11010110\", \"10101001\" = \"11 010011\", \"11001100\" = \"01001011\", \"10001101\" = \"01011101\", \" 11001010\" = \"01110100\", \"10001001\" = \"10100111\", \"10010001\" = \"10000001\", \"11011011\" = \"10111001\", \"00010011\" = \"01111101 \", \"00100101\" = \"00111111\", \"01100000\" = \"11010000\", \"010011 00\" = \"00101001\", \"10011111\" = \"11011011\", \"11101001\" = \"000 11110\", \"00001001\" = \"00000001\", \"00000111\" = \"11000101\", \"0 1011001\" = \"11001011\", \"01111011\" = \"00100001\", \"10001000\" = \+ \"11000100\", \"10111111\" = \"00001000\", \"00101000\" = \"00110100\" , \"10100111\" = \"01011100\", \"10111001\" = \"01010110\", \"00000011 \" = \"01111011\", \"00110010\" = \"00100011\", \"01001011\" = \"10110 011\", \"11110011\" = \"00001101\", \"01010111\" = \"01011011\", \"110 11100\" = \"10000110\", \"10010101\" = \"00101010\", \"10010110\" = \" 10010000\", \"01100011\" = \"11111011\", \"01100101\" = \"01001101\", \+ \"11010111\" = \"00001110\", \"01110100\" = \"10010010\", \"11001101\" = \"10111101\", \"01010101\" = \"11111100\", \"01101100\" = \"0101000 0\", \"10100011\" = \"00001010\", \"11111100\" = \"10110000\", \"10011 110\" = \"00001011\", \"11011110\" = \"00011101\", \"01111000\" = \"10 111100\", \"10011001\" = \"11101110\", \"01000011\" = \"00011010\", \" 11010110\" = \"11110110\", \"00001010\" = \"01100111\", \"00110011\" = \"11000011\", \"01100100\" = \"01000011\", \"00100001\" = \"11111101 \"]):" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 35 "The inverse S-Box as a l ookup table" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6425 "InvSBoxTab le := table([\"00000010\" = \"01101010\", \"01101001\" = \"11100100\" , \"01100000\" = \"10010000\", \"00001111\" = \"11111011\", \"00010100 \" = \"10011011\", \"00100000\" = \"01010100\", \"11101100\" = \"10000 011\", \"10111110\" = \"01011010\", \"01101100\" = \"10111000\", \"100 01000\" = \"10010111\", \"01011110\" = \"10011101\", \"00100100\" = \" 10100110\", \"11111000\" = \"11100001\", \"00111110\" = \"11010001\", \+ \"11011010\" = \"01111010\", \"11110000\" = \"00010111\", \"10111000\" = \"10011010\", \"00101001\" = \"01001100\", \"11100101\" = \"0010101 0\", \"00000111\" = \"00111000\", \"10110101\" = \"11010010\", \"00111 001\" = \"01011011\", \"11100000\" = \"10100000\", \"11110001\" = \"00 101011\", \"01110011\" = \"10001111\", \"00100111\" = \"00111101\", \" 00101000\" = \"11101110\", \"11110110\" = \"11010110\", \"01010011\" = \"01010000\", \"00001011\" = \"10011110\", \"11001001\" = \"00010010 \", \"00001000\" = \"10111111\", \"00000100\" = \"00110000\", \"001010 10\" = \"10010101\", \"10010000\" = \"10010110\", \"10101100\" = \"101 01010\", \"01100110\" = \"11010011\", \"10111111\" = \"11110100\", \"0 1000011\" = \"01100100\", \"00001101\" = \"11110011\", \"10000001\" = \+ \"10010001\", \"01001001\" = \"10100100\", \"01001110\" = \"10110110\" , \"00110000\" = \"00001000\", \"10100101\" = \"00101001\", \"10001010 \" = \"11001111\", \"11110101\" = \"01110111\", \"00000110\" = \"10100 101\", \"10111010\" = \"11000000\", \"10110010\" = \"00111110\", \"111 10111\" = \"00100110\", \"00111111\" = \"00100101\", \"00000000\" = \" 01010010\", \"11001110\" = \"11101100\", \"10101111\" = \"00011011\", \+ \"00110011\" = \"01100110\", \"01111011\" = \"00000011\", \"10000010\" = \"00010001\", \"11101010\" = \"10111011\", \"10110001\" = \"0101011 0\", \"11000101\" = \"00000111\", \"10000011\" = \"01000001\", \"11110 011\" = \"01111110\", \"10111011\" = \"11111110\", \"01101101\" = \"10 110011\", \"10001111\" = \"01110011\", \"01101010\" = \"01011000\", \" 00100010\" = \"10010100\", \"01000101\" = \"01101000\", \"00000001\" = \"00001001\", \"00010010\" = \"00111001\", \"01010000\" = \"01101100 \", \"10011101\" = \"01110101\", \"10101011\" = \"00001110\", \"100001 10\" = \"11011100\", \"00001110\" = \"11010111\", \"11011000\" = \"001 01101\", \"01011111\" = \"10000100\", \"10001011\" = \"11001110\", \"0 1100101\" = \"10111100\", \"10111101\" = \"11001101\", \"10010011\" = \+ \"00100010\", \"10000100\" = \"01001111\", \"11101111\" = \"01100001\" , \"11001000\" = \"10110001\", \"11110100\" = \"10111010\", \"00101110 \" = \"11000011\", \"10010010\" = \"01110100\", \"00110010\" = \"10100 001\", \"10011100\" = \"00011100\", \"00100011\" = \"00110010\", \"001 01011\" = \"00001011\", \"01010100\" = \"11111101\", \"11011001\" = \" 11100101\", \"10101001\" = \"10110111\", \"01110101\" = \"00111111\", \+ \"11011100\" = \"10010011\", \"10011011\" = \"11101000\", \"01010101\" = \"11101101\", \"11000110\" = \"11000111\", \"01100010\" = \"1010101 1\", \"10101101\" = \"00011000\", \"11100100\" = \"10101110\", \"01000 111\" = \"00010110\", \"00010101\" = \"00101111\", \"11010110\" = \"01 001010\", \"10011110\" = \"11011111\", \"01001010\" = \"01011100\", \" 10100110\" = \"11000101\", \"01111000\" = \"11000001\", \"01111101\" = \"00010011\", \"00010111\" = \"10000111\", \"10000101\" = \"01100111 \", \"00100110\" = \"00100011\", \"01111100\" = \"00000001\", \"110111 11\" = \"11101111\", \"00011111\" = \"11001011\", \"11001011\" = \"010 11001\", \"01001100\" = \"01011101\", \"01110010\" = \"00011110\", \"0 1101011\" = \"00000101\", \"01000010\" = \"11110110\", \"10011010\" = \+ \"00110111\", \"11111110\" = \"00001100\", \"00011010\" = \"01000011\" , \"10110110\" = \"01111001\", \"10100001\" = \"11110001\", \"11100011 \" = \"01001101\", \"01100011\" = \"00000000\", \"11001100\" = \"00100 111\", \"11010000\" = \"01100000\", \"01110110\" = \"00001111\", \"001 11010\" = \"10100010\", \"10100111\" = \"10001001\", \"01001111\" = \" 10010010\", \"11000001\" = \"11011101\", \"11110010\" = \"00000100\", \+ \"10100010\" = \"00011010\", \"01011000\" = \"01011110\", \"10110100\" = \"11000110\", \"01100001\" = \"11011000\", \"01010111\" = \"1101101 0\", \"11101001\" = \"11101011\", \"00011000\" = \"00110100\", \"01011 010\" = \"01000110\", \"11101000\" = \"11001000\", \"10101010\" = \"01 100010\", \"00110100\" = \"00101000\", \"00010000\" = \"01111100\", \" 01111110\" = \"10001010\", \"00100101\" = \"11000010\", \"11111011\" = \"01100011\", \"11100001\" = \"11100000\", \"01101000\" = \"11110111 \", \"11111010\" = \"00010100\", \"10010001\" = \"10101100\", \"110001 00\" = \"10001000\", \"10001101\" = \"10110100\", \"00011001\" = \"100 01110\", \"01011100\" = \"10100111\", \"01010010\" = \"01001000\", \"1 0011000\" = \"11100010\", \"10100000\" = \"01000111\", \"01000000\" = \+ \"01110010\", \"00111011\" = \"01001001\", \"10001001\" = \"11110010\" , \"01111010\" = \"10111101\", \"01101111\" = \"00000110\", \"11111001 \" = \"01101001\", \"00101111\" = \"01001110\", \"11101110\" = \"10011 001\", \"11010101\" = \"10110101\", \"11001010\" = \"00010000\", \"101 00011\" = \"01110001\", \"10101110\" = \"10111110\", \"01001011\" = \" 11001100\", \"00011011\" = \"01000100\", \"11101011\" = \"00111100\", \+ \"10000111\" = \"11101010\", \"11000011\" = \"00110011\", \"00101100\" = \"01000010\", \"10010111\" = \"10000101\", \"01010110\" = \"1011100 1\", \"01110001\" = \"00101100\", \"11011110\" = \"10011100\", \"00000 011\" = \"11010101\", \"10110000\" = \"11111100\", \"01110100\" = \"11 001010\", \"11011011\" = \"10011111\", \"11000000\" = \"00011111\", \" 10010110\" = \"00110101\", \"00001100\" = \"10000001\", \"00010011\" = \"10000010\", \"01011101\" = \"10001101\", \"10001100\" = \"11110000 \", \"00110101\" = \"11011001\", \"00111000\" = \"01110110\", \"010001 00\" = \"10000110\", \"10010100\" = \"11100111\", \"11010111\" = \"000 01101\", \"10110111\" = \"00100000\", \"00111100\" = \"01101101\", \"0 1000001\" = \"11111000\", \"11111101\" = \"00100001\", \"00011101\" = \+ \"11011110\", \"10101000\" = \"01101111\", \"01011001\" = \"00010101\" , \"01111001\" = \"10101111\", \"11111111\" = \"01111101\", \"10110011 \" = \"01001011\", \"01110000\" = \"11010000\", \"10111001\" = \"11011 011\", \"00110001\" = \"00101110\", \"01010001\" = \"01110000\", \"000 11100\" = \"11000100\", \"11000010\" = \"10101000\", \"11010100\" = \" 00011001\", \"00010001\" = \"11100011\", \"01110111\" = \"00000010\", \+ \"11101101\" = \"01010011\", \"11001101\" = \"10000000\", \"01100100\" = \"10001100\", \"11000111\" = \"00110001\", \"00010110\" = \"1111111 1\", \"10000000\" = \"00111010\", \"11100110\" = \"11110101\", \"10011 001\" = \"11111001\", \"00001001\" = \"01000000\", \"01000110\" = \"10 011000\", \"11010001\" = \"01010001\", \"10011111\" = \"01101110\", \" 00101101\" = \"11111010\", \"00001010\" = \"10100011\", \"11111100\" = \"01010101\", \"10111100\" = \"01111000\", \"00100001\" = \"01111011 \", \"10100100\" = \"00011101\", \"11100010\" = \"00111011\", \"110111 01\" = \"11001001\", \"00110110\" = \"00100100\", \"00111101\" = \"100 01011\", \"11001111\" = \"01011111\", \"10001110\" = \"11100110\", \"1 1010011\" = \"10101001\", \"11100111\" = \"10110000\", \"00000101\" = \+ \"00110110\", \"11010010\" = \"01111111\", \"00011110\" = \"11101001\" , \"01100111\" = \"00001010\", \"10010101\" = \"10101101\", \"01001101 \" = \"01100101\", \"01111111\" = \"01101011\", \"00110111\" = \"10110 010\", \"01001000\" = \"11010100\", \"01101110\" = \"01000101\", \"010 11011\" = \"01010111\"]):" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 13 "Ke y Expansion" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 322 "roundFudge : = int -> Rem(alpha^(int-1),genPoly,alpha) mod 2:\npolyToInt := poly -> subs(alpha=2, poly):\nroundFudgeWord := int -> polyToBits(roundFudge( int)):\nrandKeyGenerator := () -> \n map(intToBits, [seq(rand(0..255 )(),i=1..16)]):\nhex32ToKey: hexWord ->map(hexTo8Bits,\n [seq(subst ring(hexWord,2*i-1..2*i),i=1..16)]):" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 978 "keyExpander := proc(keyList)\n local keyExpanded, \+ i, j, k, fudgeWord:\n keyExpanded := matrix(4,44):\n for j from 1 \+ to 4 do\n for i from 1 to 4 do\n keyExpanded[i,j] := keyL ist[(j-1)*4+i];\n end do:\n end do:\n for i from 1 to 10 do\n fudgeWord := roundFudgeWord(i);\n keyExpanded[1,4*i+1] := \+ \n xor8(keyExpanded[1,4*i-3],SBoxTable[keyExpanded[2,4*i]]);\n keyExpanded[2,4*i+1] := \n xor8(keyExpanded[2,4*i-3],SB oxTable[keyExpanded[3,4*i]]);\n keyExpanded[3,4*i+1] := \n \+ xor8(keyExpanded[3,4*i-3],SBoxTable[keyExpanded[4,4*i]]);\n ke yExpanded[4,4*i+1] := \n xor8(keyExpanded[4,4*i-3],SBoxTable[ keyExpanded[1,4*i]]);\n keyExpanded[1,4*i+1] := xor8(keyExpanded[ 1,4*i+1],fudgeWord);\n for j from 2 to 4 do\n for k from \+ 1 to 4 do\n keyExpanded[k,4*i+j] \n :=xor8(k eyExpanded[k,4*i+j-4],keyExpanded[k,4*i+j-1]):\n end do:\n \+ end do:\n end do:\n keyExpanded;\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 12 "Round Pieces" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 119 "We are ready to set up our 4 operations needed for encryption. We wi ll create the inverse operations at the same time." }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 137 "The first operation is Byte Substitution. It is done with a simple look-up table. The inverse operation uses the inv erse look-up table," }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 113 "BS \+ := byteMatrix -> map(x->SBoxTable[x], byteMatrix):\nInvBS := byteMatri x -> map(x->InvSBoxTable[x], byteMatrix):" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 334 "For Shift Rows, the current message state is viewed as a 4 by 4 matrix of bytes. The 4 rows are rotated by 0, 1, 2, and 3 pla ces respectively. (If we were to number rows of a matrix starting wit h zero then the ith row is rotated by i places.) The inverse operatio n is a rotation of the 4 rows by 0, 3, 2, and 1 places respectively." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 532 "SR := proc(byteMatrix)\n local byteList, rotList:\n byteList := convert(byteMatrix,listlist): \n rotList :=[byteList[1], ListTools[Rotate](byteList[2],1), \n \+ ListTools[Rotate](byteList[3],2), ListTools[Rotate](byteList[4],3)]: \n convert(rotList, matrix);\nend:\nInvSR := proc(byteMatrix)\nlocal byteList, rotList:\n byteList := convert(byteMatrix,listlist):\n \+ rotList :=[byteList[1], ListTools[Rotate](byteList[2],3), \n Lis tTools[Rotate](byteList[3],2), ListTools[Rotate](byteList[4],1)]:\n \+ convert(rotList, matrix);\nend:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 336 "For the Mix Column operation, the message state is considered to \+ be a 4 by 4 matrix of bytes with the bytes understood as polynomials i n GF(256). This matrix is multiplied by a set 4 by 4 mixing matrix, w ith the operations understood as being in GF(256). The inverse operat ion involves multiplying by the inverse of the mixing matrix." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 650 "MixMat := map(intToPoly,\n \+ matrix(4,4,[2,3,1,1,1,2,3,1,1,1,2,3,3,1,1,2])):\nMC := proc(byteMat rix)\n local product1, polyMatrix:\n polyMatrix := map(bitToPoly, \+ byteMatrix):\n product1 := linalg[multiply](MixMat,polyMatrix):\n \+ map(x->polyToBits(sort(Rem(expand(x),genPoly,alpha) mod 2)), \n p roduct1);\nend:\nInvMixMat := map(intToPoly,\n matrix(4,4,[14,11,13 ,9,9,14,11,13,13,9,14,11,11,13,9,14])):\nInvMC := proc(byteMatrix)\n \+ local product1, polyMatrix:\n polyMatrix := map(bitToPoly, byteMatr ix):\n product1 := linalg[multiply](InvMixMat,polyMatrix):\n map(x ->polyToBits(sort(Rem(expand(x),genPoly,alpha) mod 2)), \n produc t1);\nend:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 106 "The Add Round Key \+ operation XORs the message state with the round key. The inverse oper ation is the same." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 210 "ARK \+ := proc(byteMatrix, expandedKey, roundNum)\n local roundKey:\n rou ndKey := linalg[transpose]\n (matrix([linalg[col](expandedKey,(ro undNum*4+1)..roundNum*4+4)])):\n zip(xor8,byteMatrix,roundKey);\nend :" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 30 "Establishing a Message and Key" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 52 "We are now ready to put th e pieces together for AES." }}{PARA 0 "" 0 "" {TEXT -1 485 "For our fi rst message and key we want to use the hex string \"000102030405060708 090A0B0C0D0E0F\". The reason for such a strange choice is that this w as used for the intermediate values test that was part of the original submission of Daemen and Rijmen when they submitted Rijndael in the c ompetition to establish AES. Using the same plaintext and key means t hat the round key addition for the 0th round will result in a message \+ state of all zeroes. The other intermediate values are:\n" }{TEXT 258 428 " PT =000102030405060708090A0B0C0D0E0F\n CT1=B5C9179EB1CC119 9B9C51B92B5C8159D\n CT2=2B65F6374C427C5B2FE3A9256896755B\n CT3=D1015 FCBB4EF65679688462076B9D6AD\n CT4=8E17064A2A35A183729FE59FF3A591F1\n \+ CT5=D7557DD55999DB3259E2183D558DCDD2\n CT6=73A96A5D7799A5F3111D2B636 84B1F7F\n CT7=1B6B853069EEFC749AFEFD7B57A04CD1\n CT8=107EEADFB6F7793 3B5457A6F08F046B2\n CT9=8EC166481A677AA96A14FF6ECE88C010\n CT =0A940 BB5416EF045F1C39458C653EA5A" }{TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 112 "testString:= \"000102030405060708090A0B0C0D0E0F\": \ntestHexList := [seq(substring(testString,2*i-1..2*i),i=1..16)];" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%,testHexListG72Q#006\"Q#01F'Q#02F'Q# 03F'Q#04F'Q#05F'Q#06F'Q#07F'Q#08F'Q#09F'Q#0AF'Q#0BF'Q#0CF'Q#0DF'Q#0EF' Q#0FF'" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 113 "testByteList := \+ map(hexTo8Bits, testHexList):\ntestKey := testByteList;\nmessMatrix := listToMatrix2(testByteList);" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%(te stKeyG72Q)000000006\"Q)00000001F'Q)00000010F'Q)00000011F'Q)00000100F'Q )00000101F'Q)00000110F'Q)00000111F'Q)00001000F'Q)00001001F'Q)00001010F 'Q)00001011F'Q)00001100F'Q)00001101F'Q)00001110F'Q)00001111F'" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%+messMatrixG-%'matrixG6#7&7&Q)000000 006\"Q)00000100F+Q)00001000F+Q)00001100F+7&Q)00000001F+Q)00000101F+Q)0 0001001F+Q)00001101F+7&Q)00000010F+Q)00000110F+Q)00001010F+Q)00001110F +7&Q)00000011F+Q)00000111F+Q)00001011F+Q)00001111F+" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 40 "testKeyExpanded := keyExpander(testKey): " }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 40 "Round by Round Encryption a nd Decryption" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 330 "Now we are ready for the rounds of encryption. The 0th round simply adds the round ke y. The first through ninth rounds do BS, SR, MC, and ARK in order. T he tenth round does not use MC, so it is BS, SR, and ARK in order. Af ter each round we convert the message state to a 32 character hex stri ng to compare to the known values." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 284 "cipher := ARK(messMatrix, testKeyExpanded, 0);\nhexS tate := matrixToHex(cipher);\nfor i from 1 to 9 do\n cipher := ARK(M C(SR(BS(cipher))),testKeyExpanded,i);\n hexState := matrixToHex(ciph er);\nend do;\ncipher := ARK(SR(BS(cipher)),testKeyExpanded,10);\nciph erHex := matrixToHex(cipher);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'ci pherG-%'matrixG6#7&7&Q)000000006\"F*F*F*F)F)F)" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQA000000000000000000000000000000006\"" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7&7&Q)101101016 \"Q)10110001F+Q)10111001F+F*7&Q)11001001F+Q)11001100F+Q)11000101F+Q)11 001000F+7&Q)00010111F+Q)00010001F+Q)00011011F+Q)00010101F+7&Q)10011110 F+Q)10011001F+Q)10010010F+Q)10011101F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQAB5C9179EB1CC1199B9C51B92B5C8159D6\"" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7&7&Q)001010116\"Q)0100110 0F+Q)00101111F+Q)01101000F+7&Q)01100101F+Q)01000010F+Q)11100011F+Q)100 10110F+7&Q)11110110F+Q)01111100F+Q)10101001F+Q)01110101F+7&Q)00110111F +Q)01011011F+Q)00100101F+F;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexS tateGQA2B65F6374C427C5B2FE3A9256896755B6\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7&7&Q)110100016\"Q)10110100F+Q)10 010110F+Q)01110110F+7&Q)00000001F+Q)11101111F+Q)10001000F+Q)10111001F+ 7&Q)01011111F+Q)01100101F+Q)01000110F+Q)11010110F+7&Q)11001011F+Q)0110 0111F+Q)00100000F+Q)10101101F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%)h exStateGQAD1015FCBB4EF65679688462076B9D6AD6\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7&7&Q)100011106\"Q)00101010F+Q)01 110010F+Q)11110011F+7&Q)00010111F+Q)00110101F+Q)10011111F+Q)10100101F+ 7&Q)00000110F+Q)10100001F+Q)11100101F+Q)10010001F+7&Q)01001010F+Q)1000 0011F+F2Q)11110001F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQA 8E17064A2A35A183729FE59FF3A591F16\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6 #>%'cipherG-%'matrixG6#7&7&Q)110101116\"Q)01011001F+F,Q)01010101F+7&F- Q)10011001F+Q)11100010F+Q)10001101F+7&Q)01111101F+Q)11011011F+Q)000110 00F+Q)11001101F+7&Q)11010101F+Q)00110010F+Q)00111101F+Q)11010010F+" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQAD7557DD55999DB3259E2183D 558DCDD26\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7 &7&Q)011100116\"Q)01110111F+Q)00010001F+Q)01101000F+7&Q)10101001F+Q)10 011001F+Q)00011101F+Q)01001011F+7&Q)01101010F+Q)10100101F+Q)00101011F+ Q)00011111F+7&Q)01011101F+Q)11110011F+Q)01100011F+Q)01111111F+" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQA73A96A5D7799A5F3111D2B63 684B1F7F6\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7 &7&Q)000110116\"Q)01101001F+Q)10011010F+Q)01010111F+7&Q)01101011F+Q)11 101110F+Q)11111110F+Q)10100000F+7&Q)10000101F+Q)11111100F+Q)11111101F+ Q)01001100F+7&Q)00110000F+Q)01110100F+Q)01111011F+Q)11010001F+" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQA1B6B853069EEFC749AFEFD7B 57A04CD16\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7 &7&Q)000100006\"Q)10110110F+Q)10110101F+Q)00001000F+7&Q)01111110F+Q)11 110111F+Q)01000101F+Q)11110000F+7&Q)11101010F+Q)01111001F+Q)01111010F+ Q)01000110F+7&Q)11011111F+Q)00110011F+Q)01101111F+Q)10110010F+" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQA107EEADFB6F77933B5457A6F 08F046B26\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7 &7&Q)100011106\"Q)00011010F+Q)01101010F+Q)11001110F+7&Q)11000001F+Q)01 100111F+Q)00010100F+Q)10001000F+7&Q)01100110F+Q)01111010F+Q)11111111F+ Q)11000000F+7&Q)01001000F+Q)10101001F+Q)01101110F+Q)00010000F+" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%)hexStateGQA8EC166481A677AA96A14FF6E CE88C0106\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%'cipherG-%'matrixG6#7 &7&Q)000010106\"Q)01000001F+Q)11110001F+Q)11000110F+7&Q)10010100F+Q)01 101110F+Q)11000011F+Q)01010011F+7&Q)00001011F+Q)11110000F+F0Q)11101010 F+7&Q)10110101F+Q)01000101F+Q)01011000F+Q)01011010F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%*cipherHexGQA0A940BB5416EF045F1C39458C653EA5A6\"" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 98 "A quick comparison shows that w e have the correct answer and the correct result after each round.." } }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 182 "The next step is to reverse the process and decrypt the message. We want to break the ciphertext int o a list of strings two character long and convert the list to a matri x of bytes." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 127 "ListCipher \+ := [seq(substring(cipherHex,2*i-1..2*i), i = 1..16)];\ncipherByteMatri x := map(hexTo8Bits,listToMatrix2(ListCipher));" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%+ListCipherG72Q#0A6\"Q#94F'Q#0BF'Q#B5F'Q#41F'Q#6EF'Q# F0F'Q#45F'Q#F1F'Q#C3F'F(Q#58F'Q#C6F'Q#53F'Q#EAF'Q#5AF'" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%1cipherByteMatrixG-%'matrixG6#7&7&Q)000010106\"Q )01000001F+Q)11110001F+Q)11000110F+7&Q)10010100F+Q)01101110F+Q)1100001 1F+Q)01010011F+7&Q)00001011F+Q)11110000F+F0Q)11101010F+7&Q)10110101F+Q )01000101F+Q)01011000F+Q)01011010F+" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 40 "Now we undo the 10 rounds of encryption." }}}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 222 "plain := cipherByteMatrix;\nplain := InvBS(In vSR(ARK(plain, testKeyExpanded,10)));\nfor i from 1 to 9 do\nplain := \+ InvBS(InvSR(InvMC(ARK(plain,testKeyExpanded,10-i))));\nend do;\ndecryp tMatrix := ARK(plain, testKeyExpanded, 0);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG%1cipherByteMatrixG" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'matrixG6#7&7&Q)100011106\"Q)00011010F+Q)011 01010F+Q)11001110F+7&Q)11000001F+Q)01100111F+Q)00010100F+Q)10001000F+7 &Q)01100110F+Q)01111010F+Q)11111111F+Q)11000000F+7&Q)01001000F+Q)10101 001F+Q)01101110F+Q)00010000F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&pl ainG-%'matrixG6#7&7&Q)000100006\"Q)10110110F+Q)10110101F+Q)00001000F+7 &Q)01111110F+Q)11110111F+Q)01000101F+Q)11110000F+7&Q)11101010F+Q)01111 001F+Q)01111010F+Q)01000110F+7&Q)11011111F+Q)00110011F+Q)01101111F+Q)1 0110010F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'matrixG6#7&7& Q)000110116\"Q)01101001F+Q)10011010F+Q)01010111F+7&Q)01101011F+Q)11101 110F+Q)11111110F+Q)10100000F+7&Q)10000101F+Q)11111100F+Q)11111101F+Q)0 1001100F+7&Q)00110000F+Q)01110100F+Q)01111011F+Q)11010001F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'matrixG6#7&7&Q)011100116\"Q)0111 0111F+Q)00010001F+Q)01101000F+7&Q)10101001F+Q)10011001F+Q)00011101F+Q) 01001011F+7&Q)01101010F+Q)10100101F+Q)00101011F+Q)00011111F+7&Q)010111 01F+Q)11110011F+Q)01100011F+Q)01111111F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'matrixG6#7&7&Q)110101116\"Q)01011001F+F,Q)01010101F+ 7&F-Q)10011001F+Q)11100010F+Q)10001101F+7&Q)01111101F+Q)11011011F+Q)00 011000F+Q)11001101F+7&Q)11010101F+Q)00110010F+Q)00111101F+Q)11010010F+ " }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'matrixG6#7&7&Q)1000111 06\"Q)00101010F+Q)01110010F+Q)11110011F+7&Q)00010111F+Q)00110101F+Q)10 011111F+Q)10100101F+7&Q)00000110F+Q)10100001F+Q)11100101F+Q)10010001F+ 7&Q)01001010F+Q)10000011F+F2Q)11110001F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'matrixG6#7&7&Q)110100016\"Q)10110100F+Q)10010110F+Q) 01110110F+7&Q)00000001F+Q)11101111F+Q)10001000F+Q)10111001F+7&Q)010111 11F+Q)01100101F+Q)01000110F+Q)11010110F+7&Q)11001011F+Q)01100111F+Q)00 100000F+Q)10101101F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'ma trixG6#7&7&Q)001010116\"Q)01001100F+Q)00101111F+Q)01101000F+7&Q)011001 01F+Q)01000010F+Q)11100011F+Q)10010110F+7&Q)11110110F+Q)01111100F+Q)10 101001F+Q)01110101F+7&Q)00110111F+Q)01011011F+Q)00100101F+F;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&plainG-%'matrixG6#7&7&Q)101101016\"Q)1011 0001F+Q)10111001F+F*7&Q)11001001F+Q)11001100F+Q)11000101F+Q)11001000F+ 7&Q)00010111F+Q)00010001F+Q)00011011F+Q)00010101F+7&Q)10011110F+Q)1001 1001F+Q)10010010F+Q)10011101F+" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&p lainG-%'matrixG6#7&7&Q)000000006\"F*F*F*F)F)F)" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%.decryptMatrixG-%'matrixG6#7&7&Q)000000006\"Q)0000010 0F+Q)00001000F+Q)00001100F+7&Q)00000001F+Q)00000101F+Q)00001001F+Q)000 01101F+7&Q)00000010F+Q)00000110F+Q)00001010F+Q)00001110F+7&Q)00000011F +Q)00000111F+Q)00001011F+Q)00001111F+" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 84 "We want to convert the message back to a hex string to re cover our original message." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "matrixToHex(decryptMatrix);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#QA0 00102030405060708090A0B0C0D0E0F6\"" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 40 "Single Command Encryption and Decryption" }}{SECT 0 {PARA 5 "" 0 "" {TEXT -1 0 "" }{TEXT 259 21 "Hex messages and keys" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 172 "The collection of procedures above can b e collected into single procedures that work with both the message and key given as hex strings. (This will be useful for testing.)" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 572 "encryptAEShex := proc(messa geHEX, keyHEX)\n local expandedKey, cipher, cipherHex, \n i, me ssMatrix, keyList:\n keyList := map(hexTo8Bits,\n [seq(subst ring(keyHEX,2*i-1..2*i),i=1..16)]):\n expandedKey := keyExpander(key List):\n messMatrix := listToMatrix2(map(hexTo8Bits,\n [seq( substring(messageHEX,2*i-1..2*i),i=1..16)])):\n cipher := ARK(messMa trix, expandedKey, 0):\n for i from 1 to 9 do\n cipher := ARK(M C(SR(BS(cipher))),expandedKey,i):\n end do:\n cipher := ARK(SR(BS( cipher)),expandedKey,10):\n cipherHex := matrixToHex(cipher):\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 661 "decryptAEShex := proc( cipherText, keyHEX)\n local expandedKey, ListCipher, cipherByteMatri x, plain, \n i,decryptMatrix, keyList:\n keyList := map(hexTo8B its,\n [seq(substring(keyHEX,2*i-1..2*i),i=1..16)]):\n expan dedKey := keyExpander(keyList):\n ListCipher := [seq(substring(ciphe rText,2*i-1..2*i), i = 1..16)];\n cipherByteMatrix := map(hexTo8Bits ,listToMatrix2(ListCipher));\n plain := cipherByteMatrix;\n plain \+ := InvBS(InvSR(ARK(plain, expandedKey,10)));\n for i from 1 to 9 do \n plain := InvBS(InvSR(InvMC(ARK(plain,expandedKey,10-i))));\n \+ end do;\n decryptMatrix := ARK(plain, expandedKey, 0);\n matrixTo Hex(decryptMatrix);\nend:" }}}{SECT 0 {PARA 5 "" 0 "" {TEXT -1 0 "" } {TEXT 261 9 "Examples:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 170 "c ipherText := encryptAEShex(\"01020304050607080910111213141516\", \n \+ \"0123456789ABCDEFFEDCBA9876543210\");\ndecryptAEShex(cipherText, \+ \"0123456789ABCDEFFEDCBA9876543210\");" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%+cipherTextGQA5036EF30262A39E731F3E08A57966A316\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#QA010203040506070809101112131415166\"" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 179 "messageHex := \"00000000000 000000000000000000000\":\nkeyHex := \"00000000000000000000000000000000 \":\ncipherText := encryptAEShex(messageHex, keyHex);\ndecryptAEShex(c ipherText, keyHex);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%+cipherTextGQ A66E94BD4EF8A2C3B884CFA59CA342B2E6\"" }}{PARA 11 "" 1 "" {XPPMATH 20 " 6#QA000000000000000000000000000000006\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 179 "messageHex := \"00000000000000000000000000000001\": \nkeyHex := \"00000000000000000000000000000000\":\ncipherText := encry ptAEShex(messageHex, keyHex);\ndecryptAEShex(cipherText, keyHex);" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#>%+cipherTextGQA58E2FCCEFA7E3061367F1D 57A4E7455A6\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#QA0000000000000000000 00000000000016\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 179 "messag eHex := \"00000000000000000000000000000000\":\nkeyHex := \"10000000000 000000000000000000000\":\ncipherText := encryptAEShex(messageHex, keyH ex);\ndecryptAEShex(cipherText, keyHex);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%+cipherTextGQA6B1E2FFFE8A114009D8FE22F6DB5F8766\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#QA000000000000000000000000000000006\"" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 194 "In a standard encryption situation, we will want to encr ypt many plaintext words with the same key. It that case it makes sen se to expand the key once and use the expanded key in the procedure." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 873 "hexKeyExpander := heyKey -> keyExpander(map(hexTo8Bits,\n [seq(substring(keyHex,2*i-1. .2*i),i=1..16)])):\nmessExpander := messageHEX -> listToMatrix2(map(he xTo8Bits,\n [seq(substring(messageHEX,2*i-1..2*i),i=1..16)])): \nencryptAESExpanded := proc(messHex, expandedKey)\n local cipher, \+ i:\n cipher := ARK(messExpander(messHex), expandedKey, 0):\n for i from 1 to 9 do\n cipher := ARK(MC(SR(BS(cipher))),expandedKey,i) :\n end do:\n matrixToHex(ARK(SR(BS(cipher)),expandedKey,10)):\nen d:\ndecryptAESExpanded := proc(cipherHex, expandedKey)\n local plain , i,decryptMatrix, keyList:\n plain := messExpander(cipherHex);\n \+ plain := InvBS(InvSR(ARK(plain, expandedKey,10)));\n for i from 1 to 9 do\n plain := InvBS(InvSR(InvMC(ARK(plain,expandedKey,10-i)))) ;\n end do;\n decryptMatrix := ARK(plain, expandedKey, 0);\n mat rixToHex(decryptMatrix);\nend:" }}}{SECT 0 {PARA 5 "" 0 "" {TEXT -1 0 "" }{TEXT 260 9 "Examples:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 608 "messageHex := \"01020304050607080910111213141516\":\nkeyHex := \" 0123456789ABCDEFFEDCBA9876543210\":\nexpandedKey := hexKeyExpander(hex Key):\nmessage1Hex := \"01020304050607080910111213141516\":\nmessage2H ex := \"00000000000000000000000000000000\":\nmessage3Hex := \"01234567 89ABCDEFFEDCBA9876543210\":\ncipher1Text := encryptAESExpanded(message 1Hex, expandedKey);\ncipher2Text := encryptAESExpanded(message2Hex, ex pandedKey);\ncipher3Text := encryptAESExpanded(message3Hex, expandedKe y);\ndecryptAESExpanded(cipher1Text, expandedKey);\ndecryptAESExpanded (cipher2Text, expandedKey);\ndecryptAESExpanded(cipher3Text, expandedK ey);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%,cipher1TextGQA5036EF30262A3 9E731F3E08A57966A316\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%,cipher2Te xtGQAD5C825A21F04643B43E2DF3278A762F76\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%,cipher3TextGQAA674F5A389253565260D08DCBED5C9716\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#QA010203040506070809101112131415166\"" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#QA000000000000000000000000000000006\" " }}{PARA 11 "" 1 "" {XPPMATH 20 "6#QA0123456789ABCDEFFEDCBA9876543210 6\"" }}}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 0 {PARA 5 "" 0 "" {TEXT 256 33 "ASCII messages and Byte List keys" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 142 "These procedures can also be modified sl ightly so that the message is given in ASCII and the key is given as a list of bytes in binary format." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 548 "encryptAESascii := proc(message, keyList)\n local \+ expandedKey, cipher, cipherHexList, cipherHex, i,messMatrix:\n expan dedKey := keyExpander(keyList):\n messMatrix := listToMatrix2(map(in tToBits, convert(message,bytes))):\n cipher := ARK(messMatrix, expan dedKey, 0):\n for i from 1 to 9 do\n cipher := ARK(MC(SR(BS(cip her))),expandedKey,i):\n end do:\n cipher := ARK(SR(BS(cipher)),ex pandedKey,10):\n cipherHexList := matrixToList2(\n map(x -> in tToHex(bitToInt(x)),cipher)):\n cipherHex := cat(seq(cipherHexList[i ],i=1..16)):\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 645 "decr yptAESascii := proc(cipherText, keyList)\n local expandedKey, ListCi pher, cipherByteMatrix, plain, \n i,decryptMatrix, decryptList:\n expandedKey := keyExpander(keyList):\n ListCipher := [seq(substri ng(cipherText,2*i-1..2*i), i = 1..16)];\n cipherByteMatrix := map(he xTo8Bits,listToMatrix2(ListCipher));\n plain := cipherByteMatrix;\n \+ plain := InvBS(InvSR(ARK(plain, expandedKey,10)));\n for i from 1 \+ to 9 do\n plain := InvBS(InvSR(InvMC(ARK(plain,expandedKey,10-i)) ));\n end do;\n decryptMatrix := ARK(plain, expandedKey, 0);\n d ecryptList := matrixToList2(map(bitToInt,decryptMatrix));\n convert( decryptList,bytes);\nend:" }}}{SECT 0 {PARA 5 "" 0 "" {TEXT -1 0 "" } {TEXT 257 8 "Example:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 262 "me ss1 := \"Have a nice day.\";\ntestKeyHex := [\"01\",\"23\",\"45\",\"67 \",\"89\",\"AB\",\"CD\",\"EF\",\n \"01\",\"23\",\"45\",\"67 \",\"89\",\"AB\",\"CD\",\"EF\"]:\ntestKey := map(hexTo8Bits,testKeyHex ):\ncipherText := encryptAESascii(mess1, testKey);\ndecryptAESascii(ci pherText, testKey);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%&mess1GQ1Have ~a~nice~day.6\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%+cipherTextGQA7E5 CCADB157FE07C58FB2DD19A2A181E6\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#Q1 Have~a~nice~day.6\"" }}}}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 30 "Saving commands for future use" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 162 "We wa nt to save the commands and constants created here so that we can call them up for other work with AES without having to repeat all that we \+ have done here. " }}{PARA 0 "" 0 "" {TEXT -1 211 "We also add in some technical commands that we use for testing plaintexts with one nonzer o bit against encrypted with a key of all zeroes,\nThe created file is called `AES.m` and will be in the current directory." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 509 "intTo128Bits := intVal ->\n subst ring(convert(convert(2^128+intVal,hex),string),2..33):\ntestline := pr oc(intVal)\n local mess1, cipher1, keyHex:\n keyHex := intTo128Bit s(0):\n mess1 := intTo128Bits(2^intVal):\n cipher1 := encryptAEShe x(mess1,keyHex);\n print(mess1, cipher1):\nend:\ntestline2 := proc(i ntValMess, intValKey)\n local mess1, cipher1, keyHex:\n keyHex := \+ intTo128Bits(intValKey):\n mess1 := intTo128Bits(intValMess):\n ci pher1 := encryptAEShex(mess1,keyHex);\n print(mess1, cipher1):\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 615 "save intToBits, bitToL ist, listToPoly, polyToInt, hexTo8Bits, \n listToBits, bitToInt, list ToInt, listToInt, polyToList, intToHex, \n polyToBits, bitToPoly, int ToPoly, listToMatrix, listToMatrix2, \n matrixToList, matrixToList2, \+ matrixToHex, genPoly, MixMat, \n InvMixMat, XOR, xorNbits, xor8, SBox Table, InvSBoxTable, roundFudge, \n polyToInt, roundFudgeWord, randKe yGenerator, keyExpander,\n BS, InvBS, SR, InvSR, MC, InvMC, ARK,\n e ncryptAESascii, decryptAESascii, encryptAEShex, decryptAEShex,\n intT o128Bits, testline, testline2, hexKeyExpander, messExpander,\n encryp tAESExpanded, decryptAESExpanded,\n`AES.m`:" }}}{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 }