{VERSION 5 0 "IBM INTEL LINUX" "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 1 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 257 "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 257 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.1" }}{PARA 0 "" 0 "" {TEXT -1 17 "Platform: \+ Maple 7" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 44 "F256 := GF( 2, 8, x^8 + x^4 + x^3 + x + 1 ):" }}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 15 "T ypdefinitionen" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 111 "`type/F25 6` := proc( a )\n try F256[output]( a )\n catch : RETURN( false )\n end;\n RETURN( true );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 99 "`type/StateType` := proc( State )\n RETURN( type( S tate, 'array'( F256, 0..Nb-1, 0..3 ) ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 108 "`type/RoundKeyType` := proc( RoundKey )\n \+ RETURN( type( RoundKey, 'array'( F256, 0..Nb-1, 0..3 ) ) );\nend:" }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 90 "`type/KeyType` := proc( Key )\n RETURN( type( Key, 'array'( F256, 0 .. 4*Nk-1 ) ) );\nend:" } {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 138 "`type/Expa ndedKeyType` := proc( ExpandedKey )\nlocal i;\n RETURN( type( Expand edKey, 'array'( WordType, 0 .. Nb * (Nr + 1) - 1 ) ) );\nend:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 173 "`type/WordType` := proc( Wo rd )\n if not type( Word, 'list'( F256 ) ) then RETURN( false ); fi; \n if not nops( Word ) = 4 then RETURN( false ); fi;\n RETURN( tru e );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 71 "Nb, Nk and Nr - T hey 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 358 "SBox := proc( a :: F256 )\n option remember;\ndescription \"the Rijndael SBox, input is an element of F256\";\nlocal b;\n if a = F256[zero] then\n b := F256[zero ];\n else\n b := F256[inverse]( a );\n fi;\n F256[ConvertIn ]( Rem( F256[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 361 "InvSBox := proc( b :: F256 \+ )\noption remember;\ndescription \"the inverse to the Rijndael SBox, i nput is an element of F256\";\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] t hen\n a := F256[inverse]( a );\n fi;\n RETURN( a );\nend:" }} }}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 22 "ByteSub and InvByteSub" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 79 "ByteSub := proc( )\nglobal S tate;\n State := map( a -> SBox( a ), State );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 85 "InvByteSub := proc( )\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 ]:\nShiftOff sets[6] := [ 1, 2, 3 ]:\nShiftOffsets[8] := [ 1, 3, 4 ]: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 237 "ShiftRowBy := proc( i :: posint , \+ C :: integer)\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;\nen d:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 119 "ShiftRow := proc( ) \nglobal Nb;\nlocal i;\n for i from 1 to 3 do\n ShiftRowBy( i, \+ ShiftOffsets[Nb][i] );\n od;\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 123 "InvShiftRow := proc( )\nglobal Nb;\nlocal i;\n for i from 1 to 3 do\n ShiftRowBy( i, -ShiftOffsets[Nb][i] );\n od ;\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 26 "MixColumn and InvMix Column" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 256 "MixColumnPolynomi al := F256[ConvertOut]( F256[input]( 3 ) ) * T^3\n \+ + F256[ConvertOut]( F256[input]( 1 ) ) * T^2\n + \+ F256[ConvertOut]( 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 from 0 to Nb - 1 do\n a := linalg[row]( matrix(S tate), j+1 );\n a := sum( 'F256[ConvertOut]( a[i] ) * T^(i-1)' , \+ 'i' = 1..4 );\n b := Rem( a * MixColumnPolynomial, T^4 + 1, T ) m od 2;\n for i from 0 to 3 do\n State[j,i] := F256[Convert In]( coeff( b, T, i ) );\n od; \n od;\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 272 "InvMixColumnPolynomial := F256[ConvertOut] ( F256[input]( 11 ) ) * T^3\n + F256[ConvertOut ]( F256[input]( 13 ) ) * T^2\n + F256[ConvertOu t]( F256[input]( 9 ) ) * T\n + F256[ConvertOut ]( F256[input]( 14 ) ):" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 477 "InvMixColumn := proc( RoundKey :: RoundKeyType )\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[ConvertOut]( a[i] ) * T^(i-1)' , 'i' = 1..4 ); \n b := Rem( a * InvMixColumnPolynomial, 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 213 "AddRoundKey := proc( RoundKey :: R oundKeyType )\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;\nend:" }}}}{SECT 0 {PARA 4 " " 0 "" {TEXT -1 31 "KeyExpansion and I_KeyExpansion" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 196 "RotByte := proc( word_in :: WordType )\nlo cal 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 101 "Rcon := proc( i :: posint )\n [ RC[i], F256[input] ( 0 ), F256[input]( 0 ), F256[input]( 0 ) ];\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 165 "AddWords := proc( a :: WordType, b :: Wo rdType )\nlocal 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 582 "KeyExpansion := proc( Key : : KeyType )\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 th en\n temp := AddWords( 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 RETU RN( W );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 186 "RoundKey := proc( ExpandedKey :: ExpandedKeyType, i :: nonnegint)\nglobal Nb; \n RETURN( array( 0 .. Nb - 1, 0 .. 3,\n [ 'ExpandedKey[Nb * i + j]' $ 'j' = 0 .. Nb - 1 ] ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 480 "I_KeyExpansion := proc( Key :: KeyType )\nglobal \+ Nr, Nb;\nlocal I_ExpandedKey, i, I_RoundKey, j;\n I_ExpandedKey := K eyExpansion( Key );\n for i from 1 to Nr - 1 do\n I_RoundKey := InvMixColumn( RoundKey( I_ExpandedKey, i ) );\n for j from Nb * \+ i to Nb * (i + 1) - 1 do\n I_ExpandedKey[j] := convert( linalg [row]( matrix( I_RoundKey ),\n \+ j mod Nb + 1), 'list' );\n od;\n od;\n RETURN( I_E xpandedKey );\nend: " }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 43 "Roun d, FinalRound and I_Round, I_FinalRound" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 660 "Round := proc( RoundKey :: RoundKeyType )\n ByteSu b( );\n userinfo( 4, Round, `State after ByteSub :`,\n \+ StateToHexForUserinfo( State ) );\n ShiftRow( );\n user info( 4, Round, `State after ShiftRow :`,\n Sta teToHexForUserinfo( State ) );\n MixColumn( );\n userinfo( 4, Roun d, `State after MixColumn :`,\n StateToHexForUse rinfo( State ) );\n userinfo( 4, Round, `RoundKey \+ :`,\n StateToHexForUserinfo( RoundKey ) );\n AddRou ndKey( RoundKey );\n userinfo( 4, Round, `State after AddRoundkey \+ :`,\n StateToHexForUserinfo( State ) );\nend:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 541 "FinalRound := proc( RoundKe y :: RoundKeyType )\n ByteSub( );\n userinfo( 4, FinalRound, `Stat e after ByteSub :`,\n StateToHexForUserinfo( State \+ ) );\n ShiftRow( );\n userinfo( 4, FinalRound, `State after ShiftR ow :`,\n StateToHexForUserinfo( State ) );\n useri nfo( 4, FinalRound, `RoundKey :`,\n Stat eToHexForUserinfo( RoundKey ) );\n AddRoundKey( RoundKey );\n user info( 4, FinalRound, `State after AddRoundkey :`,\n Sta teToHexForUserinfo( State ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 706 "I_Round := proc( I_RoundKey :: RoundKeyType )\ngloba l State;\n InvByteSub( );\n userinfo( 4, I_Round, `State after Inv ByteSub :`,\n StateToHexForUserinfo( State ) );\n \+ InvShiftRow( );\n userinfo( 4, I_Round, `State after InvShiftRow \+ :`,\n StateToHexForUserinfo( State ) );\n State := I nvMixColumn( State );\n userinfo( 4, I_Round, `State after InvMixCol umn :`,\n StateToHexForUserinfo( State ) );\n useri nfo( 4, I_Round, `I_RoundKey :`,\n Stat eToHexForUserinfo( 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 556 "I_FinalRound := proc ( I_RoundKey :: RoundKeyType ) \n InvByteSub( );\n userinfo( 4, I_FinalRound, `State after InvByt eSub :`,\n StateToHexForUserinfo( State ) );\n InvShif tRow( );\n userinfo( 4, I_FinalRound, `State after InvShiftRow :`, \n StateToHexForUserinfo( State ) );\n userinfo( 4, I_Fi nalRound, `I_RoundKey :`,\n StateToHexForUse rinfo( I_RoundKey ) );\n AddRoundKey( I_RoundKey );\n userinfo( 4, I_FinalRound, `State after AddRoundkey :`,\n StateToHexF orUserinfo( State ) );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 46 "Rijndael and I_Rijndael and InitializeRijndael" }}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 725 "Rijndael := proc( CipherInput :: StateType, C ipherKey :: KeyType )\nglobal State, Nr;\nlocal ExpandedKey, i;\n St ate := copy( CipherInput );\n ExpandedKey := KeyExpansion( CipherKey );\n userinfo( 4, Rijndael, `initial State :`,\n \+ StateToHexForUserinfo( State ) );\n userinfo( 4, Rijndael, \+ `I_RoundKey :`,\n StateToHexForUserinfo( RoundKey( ExpandedKey, 0 ) ) );\n AddRoundKey( RoundKey( ExpandedKe y, 0 ) );\n userinfo( 4, Rijndael, `State after AddRoundkey :`, \n StateToHexForUserinfo( State ) );\n for i from 1 to N r - 1 do\n Round( RoundKey( ExpandedKey, i ) );\n od;\n Final Round( RoundKey( ExpandedKey, Nr ) );\n RETURN( State );\nend: " } }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 749 "I_Rijndael := proc( Ciphe rInput :: StateType, CipherKey :: KeyType )\nglobal State, Nr;\nlocal \+ I_ExpandedKey, i;\n State := copy( 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( RoundKey( I_ExpandedKey, Nr ) ) );\n Ad dRoundKey( RoundKey( I_ExpandedKey, Nr ) );\n userinfo( 4, I_Rijndae l, `State after AddRoundkey :`,\n StateToHexForUserinfo ( State ) );\n for i from Nr - 1 to 1 by -1 do\n I_Round( Round Key( I_ExpandedKey, i ) );\n od;\n I_FinalRound( RoundKey( I_Expan dedKey, 0 ) );\n RETURN( State );\nend:" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 315 "InitializeRijndael := proc( Nb_in :: posint, Nk_in :: posint )\nglobal Nb, Nk, Nr;\n if not member( Nb_in, \{4, 6, 8\} ) then\n ERROR(\"Nb must be 4, 6 or 8.\");\n elif 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;\ne nd:" }}}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 17 "Random generation" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "GenerateRandomByte := rand( \+ 0 .. 255 ):" }}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 17 "GenerateRandomKey " }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 206 "GenerateRandomKey := pr oc( )\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]( Gener ateRandomByte( ) );\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( )\nglobal 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 Key[j,i] : = F256[input]( GenerateRandomByte( ) );\n od;\n od;\n RETURN( Key );\nend:" }}}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 18 "Modes of oper ation" }}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 19 "StringToCipherInput" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 319 "BytesToCipherInput := proc( ByteList :: `list`(nonnegint) )\nglobal Nb;\nlocal CipherInput, i, j; \n CipherInput := 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] := F2 56[input]( ByteList[4*j+i+1] );\n od;\n od;\n RETURN( CipherI nput ); \nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 546 " StringToCipherInput := proc( Text :: string )\nlocal ByteList, ByteLis tLength, NumberOfBlocks, CipherInput, i;\n ByteList := convert( Text , bytes );\n ByteListLength := nops( ByteList );\n NumberOfBlocks \+ := ceil( ByteListLength / (4 * Nb) );\n ByteList := [ op( ByteList ) ,\n 0 $ NumberOfBlocks * 4 * Nb - ByteListLength ];\n \+ for i from 1 to NumberOfBlocks do\n CipherInput[i] := BytesToCi pherInput(\n [ 'ByteList[ 4 * Nb * (i-1) + 1 + j ]' $ 'j' = 0 . . 4 * Nb - 1 ] );\n od;\n convert( CipherInput, 'list' );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 144 "BytesToKey := proc( Byte List :: `list`(nonnegkint) )\nglobal Nk;\n array( 0 .. 4 * Nk - 1, m ap( x -> F256[input](x), ByteList[1..4*Nk] ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 378 "HexToCipherInput := proc( HexInput :: string )\nglobal Nb;\nlocal CipherInput, j, i;\n CipherInput := \+ 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]( convert( \+ HexInput[8*j+2*i+1..8*j+2*i+2],\n \+ decimal, hex ) );\n od;\n od;\n RETURN( CipherInput );\n end: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 268 "HexToKey := proc( HexKey :: string )\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 d ecimal, hex ) );\n od;\n RETURN( Key );\nend: " }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "RijndaelECB" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 385 "RijndaelECB := proc( Text :: string, CipherKey :: Ke yType )\nglobal State;\nlocal CipherInput, NumberOfBlocks, CipherOutpu t, i;\n CipherInput := StringToCipherInput( Text );\n NumberOfBloc ks := nops( CipherInput );\n for i from 1 to NumberOfBlocks do\n \+ Rijndael( CipherInput[i], CipherKey );\n CipherOutput[i] := cop y( State );\n od;\n convert( CipherOutput, 'list' );\nend: " }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 451 "I_RijndaelECB := proc( Ciph erOutput :: `list`(StateType), CipherKey :: KeyType )\nglobal State;\n local NumberOfBlocks, CipherInput, i;\n NumberOfBlocks := nops( Ciph erOutput );\n CipherInput := vector( NumberOfBlocks );\n for i fro m 1 to NumberOfBlocks do\n I_Rijndael( CipherOutput[i], CipherKey );\n CipherInput[i] := copy( State );\n od;\n cat( 'StateToT ext( CipherInput[i] )' $\n 'i' = 1 .. NumberOfBlo cks ); \nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "RijndaelCBC " }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 306 "AddStates := proc( Stat e1 :: StateType, State2 :: StateType )\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 );\n end: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 492 "RijndaelC BC := proc( Text :: string, CipherKey :: KeyType )\nglobal State;\nloc al CipherInput, NumberOfBlocks, CipherOutput, i;\n CipherInput := St ringToCipherInput( Text );\n NumberOfBlocks := nops( CipherInput ); \n CipherOutput[0] := GenerateRandomMessage( );\n for i from 1 to \+ NumberOfBlocks do\n Rijndael( AddStates( CipherOutput[i-1],\n \+ CipherInput[i] ), CipherKey );\n CipherOut put[i] := copy( State );\n od;\n convert( CipherOutput, 'list' ); \nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 502 "I_RijndaelCBC : = proc( CipherOutput :: `list`(StateType), CipherKey :: KeyType )\nglo bal State;\nlocal NumberOfBlocks, CipherInput, i;\n NumberOfBlocks : = nops( CipherOutput );\n CipherInput[1] := CipherOutput[1];\n for i from 2 to NumberOfBlocks do\n I_Rijndael( CipherOutput[i], Cip herKey );\n CipherInput[i] := AddStates( State, CipherOutput[i-1] );\n od;\n convert( CipherInput, 'list' );\n cat( 'StateToText( CipherInput[i] )' $\n 'i' = 2 .. NumberOfBlocks );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 514 "RijndaelCBC0 \+ := proc( Text :: string, CipherKey :: KeyType )\nglobal State;\nlocal \+ CipherInput, NumberOfBlocks, CipherOutput, i;\n CipherInput := Strin gToCipherInput( Text );\n NumberOfBlocks := nops( CipherInput );\n \+ CipherOutput[1] := copy( Rijndael( CipherInput[1], CipherKey ) );\n \+ for i from 2 to NumberOfBlocks do\n Rijndael( AddStates( CipherO utput[i-1],\n CipherInput[i] ), CipherKey ); \n CipherOutput[i] := copy( State );\n od;\n convert( CipherO utput, 'list' );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 501 "I_RijndaelCBC0 := proc( CipherOutput :: `list`(StateType), CipherKey \+ :: KeyType )\nglobal State;\nlocal NumberOfBlocks, CipherInput, i;\n \+ NumberOfBlocks := nops( CipherOutput );\n CipherInput[1] := copy( I _Rijndael( CipherOutput[1], CipherKey ) );\n for i from 2 to NumberO fBlocks do\n I_Rijndael( CipherOutput[i], CipherKey );\n Cip herInput[i] := AddStates( State, CipherOutput[i-1] );\n od;\n cat( 'StateToText( CipherInput[i] )' $\n 'i' = 1 .. \+ NumberOfBlocks );\nend:" }}}}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 11 "Rijn daelCFB" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 456 "RijndaelCFB := p roc( Text :: string, CipherKey :: KeyType )\nglobal State;\nlocal Ciph erInput, NumberOfBlocks, CipherOutput, i;\n CipherInput := StringToC ipherInput( Text );\n NumberOfBlocks := nops( CipherInput );\n Cip herOutput[0] := GenerateRandomMessage( );\n for i from 1 to NumberOf Blocks do\n Rijndael( CipherOutput[i-1], CipherKey );\n Ciph erOutput[i] := AddStates( State, CipherInput[i] );\n od;\n convert ( CipherOutput, 'list' );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 500 "I_RijndaelCFB := proc( CipherOutput :: `list`(StateType), Cip herKey :: KeyType )\nglobal State;\nlocal NumberOfBlocks, CipherInput, i;\n NumberOfBlocks := nops( CipherOutput );\n CipherInput[1] := \+ CipherOutput[1];\n for i from 2 to NumberOfBlocks do\n Rijndael ( CipherOutput[i-1], CipherKey );\n CipherInput[i] := AddStates( \+ State, CipherOutput[i] );\n od;\n convert( 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 473 "Rijnd aelOFB := proc( Text :: string, CipherKey :: KeyType )\nglobal State; \nlocal CipherInput, NumberOfBlocks, CipherOutput, i;\n CipherInput \+ := StringToCipherInput( Text );\n NumberOfBlocks := nops( CipherInpu t );\n CipherOutput[0] := GenerateRandomMessage( );\n State := Cip herOutput[0];\n for i from 1 to NumberOfBlocks do\n Rijndael( S tate, CipherKey );\n CipherOutput[i] := AddStates( State, CipherI nput[i] );\n od;\n convert( CipherOutput, 'list' );\nend:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 517 "I_RijndaelOFB := proc( Ciph erOutput :: `list`(StateType), CipherKey :: KeyType )\nglobal State;\n local NumberOfBlocks, CipherInput, i;\n NumberOfBlocks := nops( Ciph erOutput );\n CipherInput[1] := CipherOutput[1];\n State := Cipher Output[1];\n for i from 2 to NumberOfBlocks do\n Rijndael( Stat e, CipherKey );\n CipherInput[i] := AddStates( State, CipherOutpu t[i] );\n od;\n convert( CipherInput, 'list' );\n cat( 'StateToT ext( CipherInput[i] )' $\n 'i' = 2 .. NumberOfBl ocks );\nend:" }}}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 16 "Output functi ons" }}{SECT 0 {PARA 4 "" 0 "" {TEXT -1 10 "StateTo..." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 301 "StateToText := proc( State :: Stat eType )\nglobal Nb;\nlocal ByteList, i, j;\n ByteList := array(1..4* Nb);\n for j from 0 to Nb - 1 do\n for 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, 'list' ), bytes ) );\nend:" } }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 203 "StateToHexForUserinfo := \+ proc( State :: StateType )\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 137 "StateToHex := proc( State :: StateType )\n li nalg[transpose]( matrix(\n map( x -> convert( F256[output]( x ), h ex), State ) ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 124 "StateToBytes := proc( State :: StateType )\n linalg[transpose]( mat rix(\n map( x -> F256[output]( x ), State ) ) );\nend:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 167 "StateToBits := proc( State :: Stat eType )\n linalg[transpose]( 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 140 "KeyToHex := proc( Key :: KeyType )\n lina lg[transpose]( matrix( Nk, 4, \n map( x -> convert( F256[output]( \+ x ), hex), Key ) ) );\nend: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 124 "KeyToBytes := proc( Key :: KeyType )\n linalg[transpose]( m atrix( Nk, 4, \n map( x -> F256[output]( x ), Key ) ) );\nend:" }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 166 "KeyToBits := proc( Key :: \+ KeyType )\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 "StateListToHex" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 115 "StateListToHex := proc( Sta teList :: `list`(StateType) )\n map( Block -> StateToHex( Block ), S tateList );\nend: " }}}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 10 "Speich ern:" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 45 "Speichere alles im Verzeic hnis /tmp/Rijndael:" }{MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 144 "repo := \"/tmp/Rijndael\":\nmkdir( repo ):\nmarch( ' create', repo, 100 ):\nsavelibname := repo:\nlibname := savelibname, l ibname:\nsavelib( anames() ):" }{TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" } }}}}{MARK "0 0 0" 10 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 0 1 2 33 1 1 }