// Encryption/Decryption with RC6-w/r/b
//
// Input: Plaintext stored in four w-bit input registers A, B, C & D
// r is the number of rounds
// w-bit round keys S[0, ... , 2r + 3]
//
// Output: Ciphertext stored in A, B, C, D
//
// '''Encryption Procedure:'''
B = B + S[0]
D = D + S[1]
for i = 1 to r do
{
t = (B*(2B + 1)) <<< lg w
u = (D*(2D + 1)) <<< lg w
A = ((A ⊕ t) <<< u) + S[2i]
C = ((C ⊕ u) <<< t) + S[2i + 1]
(A, B, C, D) = (B, C, D, A)
}
A = A + S[2r + 2]
C = C + S[2r + 3]
// '''Decryption Procedure:'''
C = C - S[2r + 3]
A = A - S[2r + 2]
for i = r downto 1 do
{
(A, B, C, D) = (D, A, B, C)
u = (D*(2D + 1)) <<< lg w
t = (B*(2B + 1)) <<< lg w
C = ((C - S[2i + 1]) >>> t) ⊕ u
A = ((A - S[2i]) >>> u) ⊕ t
}
D = D - S[1]
B = B - S[0]