{VERSION 4 0 "IBM INTEL LINUX22" "4.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 1 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 258 "times" 1 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }{PSTYLE "N ormal" -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 1" -1 3 1 {CSTYLE "" -1 -1 "Times" 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 4 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 "Title" 0 18 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 1 0 0 0 0 0 0 0 }3 0 0 -1 12 12 0 0 0 0 0 0 19 0 }} {SECT 0 {EXCHG {PARA 18 "" 0 "" {TEXT 258 12 "Rijndael.mws" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }{MPLTEXT 1 0 8 "restart:" }}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 18 "Rijndael Functions" }}{PARA 0 "" 0 "" {TEXT -1 67 "Implementation of the Rijndael cipher following the descr iption in " }{TEXT 256 22 "AES Proposal: Rijndael" }{TEXT -1 206 " by \+ Joan Daemen and Vincent Rijmen. This implementation is not in the leas t optimized with respect to performance but rather with a view on the \+ readability in connection with the aforementioned description." }} {PARA 0 "" 0 "" {TEXT -1 19 "Author: Olaf M\374ller" }}{PARA 0 "" 0 " " {TEXT -1 12 "Version: 1.0" }}{PARA 0 "" 0 "" {TEXT -1 17 "Platform: \+ Maple 6" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 45 "#F256 := GF( 2, 8 , x^8 + x^4 + x^3 + x + 1 ):" }}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 71 " Nb, Nk and Nr - They can be changed using InitializeRijndael( Nb, Nk ) ." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "Nb := 4:\nNk := 4:\nNr \+ := max(Nb,Nk) + 6:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 17 "S-Box and InvSBox" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 271 "SBox := proc( a )\nlocal b;\n if a = F256[zero] then\n b := F256[zero];\n el se\n b := F256[inverse]( a );\n fi;\n F256[ConvertIn]( Rem( F 256[ConvertOut]( b ) * ( x^4 + x^3 + x^2 + x + 1 )\n \+ + x^6 + x^5 + x + 1, x^8 + 1, x ) mod 2 );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 259 "InvSBox := proc( b )\nlocal a;\n \+ a := F256[ConvertIn]( Rem( ( F256[ConvertOut]( b ) + x^6 + x^5 + x + \+ 1 )\n * ( x^6 + x^3 + x ), x^8 + 1, x ) mod 2 \+ );\n if a <> F256[zero] then\n a := F256[inverse]( a );\n fi; \n RETURN( a );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 22 "Byte Sub and InvByteSub" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 79 "ByteSu b := proc( )\nglobal State;\n State := map( a -> SBox( a ), State ); \nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 85 "InvByteSub := pro c( )\nglobal State;\n State := map( b -> InvSBox( b ), State );\nend :" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 24 "ShiftRow and InvShiftRow" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 96 "ShiftOffsets[4] := [ 1, 2, 3 ]:\nShiftOffsets[6] := [ 1, 2, 3 ]:\nShiftOffsets[8] := [ 1, 3, 4 ] : " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 217 "ShiftRowBy := proc( \+ i , C )\nglobal Nb, State;\nlocal NewRow, j;\n for j from 0 to Nb - \+ 1 do\n NewRow[j] := State[ j + C mod Nb , i ]; \n od;\n for j from 0 to Nb - 1 do\n State[j,i] := NewRow[j];\n od;\nend:" }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 119 "ShiftRow := proc( )\ngloba l Nb;\nlocal i;\n for i from 1 to 3 do\n ShiftRowBy( i, ShiftOf fsets[Nb][i] );\n od;\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 123 "InvShiftRow := proc( )\nglobal Nb;\nlocal i;\n for i from 1 t o 3 do\n ShiftRowBy( i, -ShiftOffsets[Nb][i] );\n od;\nend:" }} }}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 26 "MixColumn and InvMixColumn" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 256 "MixColumnPolynomial := F256 [ConvertOut]( F256[input]( 3 ) ) * T^3\n + F256[Co nvertOut]( F256[input]( 1 ) ) * T^2\n + F256[Conve rtOut]( F256[input]( 1 ) ) * T\n + F256[ConvertOut ]( F256[input]( 2 ) ):" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 371 " MixColumn := proc( )\nglobal State, Nb;\nlocal j, a, b, i;\n for j f rom 0 to Nb - 1 do\n a := linalg[row]( matrix(State), j+1 );\n \+ a := sum( 'F256[ConvertOut]( a[i] ) * T^(i-1)' , 'i' = 1..4 );\n \+ b := Rem( a * MixColumnPolynomial, T^4 + 1, T ) mod 2;\n for i from 0 to 3 do\n State[j,i] := F256[ConvertIn]( coeff( b, T, \+ i ) );\n od; \n od;\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 272 "InvMixColumnPolynomial := F256[ConvertOut]( F256[inp ut]( 11 ) ) * T^3\n + F256[ConvertOut]( F256[in put]( 13 ) ) * T^2\n + F256[ConvertOut]( F256[i nput]( 9 ) ) * T\n + F256[ConvertOut]( F256[in put]( 14 ) ):" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 461 "InvMixCol umn := proc( RoundKey )\nlocal I_RoundKey, j, a, b, i;\n I_RoundKey \+ := array( 0 .. Nb - 1 , 0 .. 3 );\n for j from 0 to Nb - 1 do\n \+ a := linalg[row]( matrix(RoundKey), j+1 );\n a := sum( 'F256[Con vertOut]( a[i] ) * T^(i-1)' , 'i' = 1..4 );\n b := Rem( a * InvMi xColumnPolynomial, T^4 + 1, T ) mod 2;\n for i from 0 to 3 do\n \+ I_RoundKey[j,i] := F256[ConvertIn]( coeff( b, T, i ) );\n \+ od; \n od;\n RETURN( I_RoundKey );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "AddRoundKey" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 198 "AddRoundKey := proc ( RoundKey )\nglobal State;\nlocal i,j;\n \+ for j from 0 to Nb - 1 do\n for i from 0 to 3 do\n State [j,i] := F256[`+`]( State[j,i], RoundKey[j,i] );\n od;\n od;\ne nd:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 31 "KeyExpansion and I_KeyEx pansion" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 184 "RotByte := proc( word_in )\nlocal word_out, i;\n word_out[4] := word_in[1]:\n for \+ i from 1 to 3 do word_out[i] := word_in[i+1]; od;\n RETURN( [ 'word_ out[i]' $ 'i' = 1 .. 4 ] );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 179 "RC := array( 1 .. iquo( 8 * (14 + 1) - 1, 4 ) ):\nRC [1] := F256[input]( 1 ):\nfor i from 2 to iquo( 8 * (14 + 1) - 1, 4 ) \+ do\n RC[i] := F256[`*`]( F256[input]( 2 ), RC[i-1] );\nod: " }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 91 "Rcon := proc( i )\n [ RC[i ], F256[input]( 0 ), F256[input]( 0 ), F256[input]( 0 ) ];\nend: " }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 141 "AddWords := proc( a, b )\nl ocal i, c;\n for i from 1 to 4 do\n c[i] := F256[`+`]( a[i], b[ i] );\n od;\n [ 'c[i]' $ 'i' = 1 .. 4 ];\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 571 "KeyExpansion := proc( Key )\nglobal Nr, Nb ;\nlocal W,i,temp;\n W := array( 0 .. Nb * (Nr + 1) - 1 );\n for i from 0 to Nk - 1 do\n W[i] := [ Key[4*i], Key[4*i+1], Key[4*i+2] , Key[4*i+3] ];\n od;\n for i from Nk to Nb * (Nr + 1) - 1 do\n \+ temp := W[i-1];\n if i mod Nk = 0 then\n temp := AddWo rds( map( a -> SBox(a), RotByte( temp ) ),\n \+ Rcon( iquo( i, Nk ) ) );\n elif Nk > 6 and i mod Nk = 4 then\n \+ temp := map( a -> SBox( a ), temp );\n fi;\n W[i] := \+ AddWords( W[i-Nk], temp );\n od;\n RETURN( W );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 155 "RoundKey := proc( ExpandedKey, i ) \nglobal Nb;\n RETURN( array( 0 .. Nb - 1, 0 .. 3,\n [ 'Ex pandedKey[Nb * i + j]' $ 'j' = 0 .. Nb - 1 ] ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 469 "I_KeyExpansion := proc( Key )\nglo bal Nr, Nb;\nlocal I_ExpandedKey, i, I_RoundKey, j;\n I_ExpandedKey \+ := KeyExpansion( Key );\n for i from 1 to Nr - 1 do\n I_RoundKe y := InvMixColumn( RoundKey( I_ExpandedKey, i ) );\n for j from N b * i to Nb * (i + 1) - 1 do\n I_ExpandedKey[j] := convert( li nalg[row]( matrix( I_RoundKey ),\n \+ j mod Nb + 1), 'list' );\n od;\n od;\n RETURN( I_ExpandedKey );\nend: " }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 43 " Round, FinalRound and I_Round, I_FinalRound" }}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 644 "Round := proc( RoundKey )\n ByteSub( );\n user info( 4, Round, `State after ByteSub :`,\n Sta teToHexForUserinfo( State ) );\n ShiftRow( );\n userinfo( 4, Round , `State after ShiftRow :`,\n StateToHexForUser info( State ) );\n MixColumn( );\n userinfo( 4, Round, `State afte r MixColumn :`,\n StateToHexForUserinfo( State ) );\n userinfo( 4, Round, `RoundKey :`,\n \+ StateToHexForUserinfo( RoundKey ) );\n AddRoundKey( RoundKe y );\n userinfo( 4, Round, `State after AddRoundkey :`,\n \+ StateToHexForUserinfo( State ) );\nend:" }}}{EXCHG {PARA 0 " > " 0 "" {MPLTEXT 1 0 525 "FinalRound := proc( RoundKey )\n ByteSub( );\n userinfo( 4, FinalRound, `State after ByteSub :`,\n \+ StateToHexForUserinfo( State ) );\n ShiftRow( );\n userin fo( 4, FinalRound, `State after ShiftRow :`,\n State ToHexForUserinfo( State ) );\n userinfo( 4, FinalRound, `RoundKey \+ :`,\n StateToHexForUserinfo( RoundKey ) ); \n AddRoundKey( RoundKey );\n userinfo( 4, FinalRound, `State afte r AddRoundkey :`,\n StateToHexForUserinfo( State ) );\n end:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 690 "I_Round := proc( I _RoundKey )\nglobal State;\n InvByteSub( );\n userinfo( 4, I_Round , `State after InvByteSub :`,\n StateToHexForUserin fo( State ) );\n InvShiftRow( );\n userinfo( 4, I_Round, `State af ter InvShiftRow :`,\n StateToHexForUserinfo( State ) );\n State := InvMixColumn( State );\n userinfo( 4, I_Round, `Sta te after InvMixColumn :`,\n StateToHexForUserinfo( St ate ) );\n userinfo( 4, I_Round, `I_RoundKey :`, \n StateToHexForUserinfo( I_RoundKey ) );\n AddRoundKey( I_RoundKey );\n userinfo( 4, I_Round, `State after AddRoundkey \+ :`,\n StateToHexForUserinfo( State ) );\nend:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 540 "I_FinalRound := proc ( I_Ro undKey )\n InvByteSub( );\n userinfo( 4, I_FinalRound, `State afte r InvByteSub :`,\n StateToHexForUserinfo( State ) );\n \+ InvShiftRow( );\n userinfo( 4, I_FinalRound, `State after InvShiftR ow :`,\n StateToHexForUserinfo( State ) );\n userinfo( \+ 4, I_FinalRound, `I_RoundKey :`,\n StateToHe xForUserinfo( I_RoundKey ) );\n AddRoundKey( I_RoundKey );\n useri nfo( 4, I_FinalRound, `State after AddRoundkey :`,\n Stat eToHexForUserinfo( State ) );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 46 "Rijndael and I_Rijndael and InitializeRijndael" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 701 "Rijndael := proc( CipherInput, Cip herKey )\nglobal State, Nr;\nlocal ExpandedKey, i;\n State := copy( \+ CipherInput );\n ExpandedKey := KeyExpansion( CipherKey );\n useri nfo( 4, Rijndael, `initial State :`,\n Stat eToHexForUserinfo( State ) );\n userinfo( 4, Rijndael, `I_RoundKey \+ :`,\n StateToHexForUserinfo( RoundKey( Ex pandedKey, 0 ) ) );\n AddRoundKey( RoundKey( ExpandedKey, 0 ) );\n \+ userinfo( 4, Rijndael, `State after AddRoundkey :`,\n \+ StateToHexForUserinfo( State ) );\n for i from 1 to Nr - 1 do\n \+ Round( RoundKey( ExpandedKey, i ) );\n od;\n FinalRound( RoundK ey( ExpandedKey, Nr ) );\n RETURN( State );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 725 "I_Rijndael := proc( CipherInput, C ipherKey )\nglobal State, Nr;\nlocal I_ExpandedKey, i;\n State := co py( CipherInput );\n I_ExpandedKey := I_KeyExpansion( CipherKey );\n userinfo( 4, I_Rijndael, `initial State :`,\n \+ StateToHexForUserinfo( State ) );\n userinfo( 4, I_Rijndael, `I_ RoundKey :`,\n StateToHexForUserinfo( Roun dKey( I_ExpandedKey, Nr ) ) );\n AddRoundKey( RoundKey( I_ExpandedKe y, Nr ) );\n userinfo( 4, I_Rijndael, `State after AddRoundkey :` ,\n StateToHexForUserinfo( State ) );\n for i from Nr - \+ 1 to 1 by -1 do\n I_Round( RoundKey( I_ExpandedKey, i ) );\n od ;\n I_FinalRound( RoundKey( I_ExpandedKey, 0 ) );\n RETURN( State \+ );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 295 "InitializeRijn dael := proc( Nb_in, Nk_in )\nglobal Nb, Nk, Nr;\n if not member( Nb _in, \{4, 6, 8\} ) then\n ERROR(\"Nb must be 4, 6 or 8.\");\n e lif not member( Nk_in, \{4, 6, 8\} ) then\n ERROR(\"Nk must be 4, 6 or 8.\");\n fi;\n Nb := Nb_in;\n Nk := Nk_in;\n Nr := max( \+ Nb, Nk ) + 6;\nend:" }}}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 17 "Random \+ generation" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "GenerateRandom Byte := rand( 0 .. 255 ):" }}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 17 "Gen erateRandomKey" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 206 "GenerateR andomKey := proc( )\nglobal Nk;\nlocal Key, i;\n Key := array( 0 .. \+ 4 * Nk - 1 );\n for i from 0 to 4 * Nk - 1 do\n Key[i] := F256[ input]( GenerateRandomByte( ) );\n od;\n RETURN( Key );\nend:" }}} }{SECT 0 {PARA 4 "" 0 "" {TEXT -1 21 "GenerateRandomMessage" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 256 "GenerateRandomMessage := proc( )\n global Nb;\nlocal Key, i, j;\n Key := array( 0 .. Nb - 1 , 0 .. 3 ); \n for j from 0 to Nb - 1 do\n for i from 0 to 3 do\n K ey[j,i] := F256[input]( GenerateRandomByte( ) );\n od;\n od;\n \+ RETURN( Key );\nend:" }}}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 18 "Mode s of operation" }}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 19 "StringToCipherI nput" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 298 "BytesToCipherInput \+ := proc( ByteList )\nglobal Nb;\nlocal CipherInput, i, j;\n CipherIn put := array( 0 .. Nb - 1, 0 .. 3 );\n for j from 0 to Nb - 1 do\n \+ for i from 0 to 3 do\n CipherInput[j,i] := F256[input]( By teList[4*j+i+1] );\n od;\n od;\n RETURN( CipherInput ); \+ \nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 536 "StringToCiphe rInput := proc( Text )\nlocal ByteList, ByteListLength, NumberOfBlocks , CipherInput, i;\n ByteList := convert( Text, bytes );\n ByteList Length := nops( ByteList );\n NumberOfBlocks := ceil( ByteListLength / (4 * Nb) );\n ByteList := [ op( ByteList ),\n 0 $ NumberOfBlocks * 4 * Nb - ByteListLength ];\n for i from 1 to Numbe rOfBlocks do\n CipherInput[i] := BytesToCipherInput(\n [ ' ByteList[ 4 * Nb * (i-1) + 1 + j ]' $ 'j' = 0 .. 4 * Nb - 1 ] );\n o d;\n convert( CipherInput, 'list' );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 122 "BytesToKey := proc( ByteList )\nglobal Nk;\n \+ array( 0 .. 4 * Nk - 1, map( x -> F256[input](x), ByteList[1..4*Nk] ) \+ );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 258 "HexToKey := pr oc( HexKey )\nglobal Nk;\nlocal Key, i;\n Key := array( 0 .. 4 * Nk \+ - 1 );\n for i from 0 to 4 * Nk - 1 do\n Key[i] := F256[input]( convert( HexKey[2*i+1..2*i+2],\n decimal, hex ) );\n od;\n RETURN( Key );\nend: " }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "RijndaelECB" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 364 "RijndaelECB := proc( Text, CipherKey )\nglobal State;\nlocal Ciph erInput, NumberOfBlocks, CipherOutput, i;\n CipherInput := StringToC ipherInput( Text );\n NumberOfBlocks := nops( CipherInput );\n for i from 1 to NumberOfBlocks do\n Rijndael( CipherInput[i], Cipher Key );\n CipherOutput[i] := copy( State );\n od;\n convert( C ipherOutput, 'list' );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 419 "I_RijndaelECB := proc( CipherOutput, CipherKey )\nglobal State; \nlocal NumberOfBlocks, CipherInput, i;\n NumberOfBlocks := nops( Ci pherOutput );\n CipherInput := vector( NumberOfBlocks );\n for i f rom 1 to NumberOfBlocks do\n I_Rijndael( CipherOutput[i], CipherK ey );\n CipherInput[i] := copy( State );\n od;\n cat( 'StateT oText( CipherInput[i] )' $\n 'i' = 1 .. NumberOfB locks ); \nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "RijndaelCB C" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 280 "AddStates := proc( Sta te1, State2 )\nglobal Nb;\nlocal Result, j, i;\n Result := array( 0 \+ .. Nb - 1, 0 .. 3 );\n for j from 0 to Nb - 1 do\n for i from 0 to 3 do\n Result[j,i] := F256[`+`]( State1[j,i], State2[j,i] \+ );\n od;\n od;\n RETURN( Result );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 471 "RijndaelCBC := proc( Text, CipherK ey )\nglobal State;\nlocal CipherInput, NumberOfBlocks, CipherOutput, \+ i;\n CipherInput := StringToCipherInput( Text );\n NumberOfBlocks \+ := nops( CipherInput );\n CipherOutput[0] := GenerateRandomMessage( \+ );\n for i from 1 to NumberOfBlocks do\n Rijndael( AddStates( C ipherOutput[i-1],\n CipherInput[i] ), Cipher Key );\n CipherOutput[i] := copy( State );\n od;\n convert( C ipherOutput, 'list' );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 470 "I_RijndaelCBC := proc( CipherOutput, CipherKey )\nglobal State; \nlocal NumberOfBlocks, CipherInput, i;\n NumberOfBlocks := nops( Ci pherOutput );\n CipherInput[1] := CipherOutput[1];\n for i from 2 \+ to NumberOfBlocks do\n I_Rijndael( CipherOutput[i], CipherKey ); \n CipherInput[i] := AddStates( State, CipherOutput[i-1] );\n o d;\n convert( CipherInput, 'list' );\n cat( 'StateToText( CipherIn put[i] )' $\n 'i' = 2 .. NumberOfBlocks );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 493 "RijndaelCBC0 := proc( \+ Text, CipherKey )\nglobal State;\nlocal CipherInput, NumberOfBlocks, C ipherOutput, i;\n CipherInput := StringToCipherInput( Text );\n Nu mberOfBlocks := nops( CipherInput );\n CipherOutput[1] := copy( Rijn dael( CipherInput[1], CipherKey ) );\n for i from 2 to NumberOfBlock s do\n Rijndael( AddStates( CipherOutput[i-1],\n \+ CipherInput[i] ), CipherKey );\n CipherOutput[i] := cop y( State );\n od;\n convert( CipherOutput, 'list' );\nend: " }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 469 "I_RijndaelCBC0 := proc( Cip herOutput, CipherKey )\nglobal State;\nlocal NumberOfBlocks, CipherInp ut, i;\n NumberOfBlocks := nops( CipherOutput );\n CipherInput[1] \+ := copy( I_Rijndael( CipherOutput[1], CipherKey ) );\n for i from 2 \+ to NumberOfBlocks do\n I_Rijndael( CipherOutput[i], CipherKey ); \n CipherInput[i] := AddStates( State, CipherOutput[i-1] );\n o d;\n cat( 'StateToText( CipherInput[i] )' $\n ' i' = 1 .. NumberOfBlocks );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "RijndaelCFB" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 435 "Rijnd aelCFB := proc( Text, CipherKey )\nglobal State;\nlocal CipherInput, N umberOfBlocks, CipherOutput, i;\n CipherInput := StringToCipherInput ( Text );\n NumberOfBlocks := nops( CipherInput );\n CipherOutput[ 0] := GenerateRandomMessage( );\n for i from 1 to NumberOfBlocks do \n Rijndael( CipherOutput[i-1], CipherKey );\n CipherOutput[ i] := AddStates( State, CipherInput[i] );\n od;\n convert( CipherO utput, 'list' );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 468 " I_RijndaelCFB := proc( CipherOutput, CipherKey )\nglobal State;\nlocal NumberOfBlocks, CipherInput, i;\n NumberOfBlocks := nops( CipherOut put );\n CipherInput[1] := CipherOutput[1];\n for i from 2 to Numb erOfBlocks do\n Rijndael( CipherOutput[i-1], CipherKey );\n \+ CipherInput[i] := AddStates( State, CipherOutput[i] );\n od;\n con vert( CipherInput, 'list' );\n cat( 'StateToText( CipherInput[i] )' \+ $\n 'i' = 2 .. NumberOfBlocks );\nend:" }}}} {SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "RijndaelOFB" }}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 452 "RijndaelOFB := proc( Text, CipherKey )\ngloba l State;\nlocal CipherInput, NumberOfBlocks, CipherOutput, i;\n Ciph erInput := StringToCipherInput( Text );\n NumberOfBlocks := nops( Ci pherInput );\n CipherOutput[0] := GenerateRandomMessage( );\n Stat e := CipherOutput[0];\n for i from 1 to NumberOfBlocks do\n Rij ndael( State, CipherKey );\n CipherOutput[i] := AddStates( State, CipherInput[i] );\n od;\n convert( CipherOutput, 'list' );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 485 "I_RijndaelOFB := proc( CipherOutput, CipherKey )\nglobal State;\nlocal NumberOfBlocks, Ciphe rInput, i;\n NumberOfBlocks := nops( CipherOutput );\n CipherInput [1] := CipherOutput[1];\n State := CipherOutput[1];\n for i from 2 to NumberOfBlocks do\n Rijndael( State, CipherKey );\n Ciph erInput[i] := AddStates( State, CipherOutput[i] );\n od;\n convert ( CipherInput, 'list' );\n cat( 'StateToText( CipherInput[i] )' $\n \+ 'i' = 2 .. NumberOfBlocks );\nend:" }}}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 16 "Output functions" }}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 10 "StateTo..." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 288 "StateToText := proc( State )\nglobal Nb;\nlocal ByteList, i, j;\n ByteList := array(1..4*Nb);\n for j from 0 to Nb - 1 do\n fo r i from 0 to 3 do\n ByteList[4*j+i+1] := F256[output]( State[ j,i] );\n od;\n od;\n RETURN( convert( convert( ByteList, 'li st' ), bytes ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 190 "StateToHexForUserinfo := proc( State )\n convert( matrix(\n map ( x -> cat( \"0\", convert( convert( F256[output]( x ),\n hex ), string ) ) [-2..-1], State ) ), 'listlist');\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 124 "StateToHex := proc( State )\n li nalg[transpose]( matrix(\n map( x -> convert( F256[output]( x ), h ex), State ) ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 111 "StateToBytes := proc( State )\n linalg[transpose]( matrix(\n ma p( x -> F256[output]( x ), State ) ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 154 "StateToBits := proc( State )\n linalg[transpo se]( matrix(\n map( x -> cat( \"0000000\", convert( F256[output]( \+ x ), binary) )[-8..-1], State ) ) );\nend:" }}}}{SECT 0 {PARA 4 "" 0 " " {TEXT -1 8 "KeyTo..." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 129 "K eyToHex := proc( Key )\n linalg[transpose]( matrix( Nk, 4, \n ma p( x -> convert( F256[output]( x ), hex), Key ) ) );\nend: " }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 113 "KeyToBytes := proc( Key )\n linalg[transpose]( matrix( Nk, 4, \n map( x -> F256[output]( x \+ ), Key ) ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 155 "Key ToBits := proc( Key )\n linalg[transpose]( matrix( Nk, 4,\n map( x -> cat( \"0000000\", convert( F256[output]( x ), binary) )[-8..-1], Key ) ) );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 14 "StateListT oHex" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 94 "StateListToHex := pr oc( StateList )\n map( Block -> StateToHex( Block ), StateList );\ne nd: " }}}}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "save \"Rijndael .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 }