Cryptography Reference
In-Depth Information
4.4.2 AES Encryption and Decryption
We now give Maple implementations of AES forward and inverse cipher functions.
4.4.2.1 AES Rounds
First we define an AES round. The input is a round subkey (given as a 0..3, 0..3 array)
and a state array, and the output is a modified state.
> Round := (roundk,state) ->
AddRoundKey(roundk,MixColumns(ShiftRows(SubBytes(state)))):
The inverse round, which is used for decryption, is given by the next function:
> InvRound := (roundk,state) ->
InvSubBytes(InvShiftRows(InvMixColumns(AddRoundKey(roundk,state)))):
4.4.2.2 The Forward Cipher Function
The next function implements the forward cipher function of AES. The input is an
expanded key in the format produced by KeyExpansion and a plaintext block
given as a state array. The output is the state array with the corresponding ciphertext
block.
> AESEncrypt := proc(expandedkey, state)
local st, Nr, i;
st := Array(state);
Nr := nops(expandedkey)-1;
st := AddRoundKey(expandedkey[1], st); #The 0th round
for i from 2 to Nr do
st := Round(expandedkey[i], st) #Rounds 1 to Nr-1
end do;
AddRoundKey(expandedkey[Nr+1], ShiftRows(SubBytes(st))) #The last round
end proc:
4.4.2.3 The Inverse Cipher Function
The inverse cipher function takes as input an expanded key and a state array (a
ciphertext block) and outputs the state with the corresponding plaintext block.
> AESDecrypt := proc(expandedkey, state)
local Nr, st, i;
st := Array(state);
Nr := nops(expandedkey)-1;
st := InvSubBytes(InvShiftRows(AddRoundKey(expandedkey[Nr+1], st)));
for i from 0 to Nr-2 do
st := InvRound(expandedkey[Nr-i], st)
end do;
AddRoundKey(expandedkey[1], st)
end proc:
 
Search WWH ::




Custom Search