-- tea.hs (C) 2002 HardCore SoftWare, Doug Hoyte -- -- This program is distributed under the terms of the GNU GPL. -- See www.gnu.org for more information. -- -- Haskell implementation of David Wheeler's and Roger Needham's Tiny -- Encryption Algorithm (TEA). This is the "New Variant" of TEA, which -- fixes some minor problems with the original TEA. This implementation -- uses 32 rounds, as recommended. -- -- The API is quite simple: -- -- teaenc takes a list of four Word32s which represent the (128 bit) key, -- and a tuple of 2 Word32s which represent the plaintext. It returns -- a tuple of 2 Word32s which represnet the ciphertext. -- -- teadec takes a list of four Word32s which represent the (128 bit) key, -- and a tuple of 2 Word32s which represent the ciphertext. It returns -- a tuple of 2 Word32s which represnet the plaintext. module TEA (teaenc, teadec) where import Bits import Word delta, decsum :: Word32 delta = 0x9e3779b9 decsum = 0xc6ef3720 teaenc :: [Word32] -> (Word32, Word32) -> (Word32, Word32) teaenc a b = calc_enc a b 0 0 where calc_enc :: [Word32] -> (Word32, Word32) -> Int -> Word32 -> (Word32, Word32) calc_enc k r@(y,z) 32 sum = r calc_enc k (y,z) iter sum = calc_enc k (newy,newz) (iter+1) newsum where newy = y + (((mangle z) + z) `xor` (sum + (k!! word32ToInt (sum .&. 3)))) newz = z + (((mangle newy) + newy) `xor` (newsum + (k!! word32ToInt ((newsum `shiftR` 11) .&. 3)))) newsum = sum + delta mangle q = (q `shiftL` 4) `xor` (q `shiftR` 5) teadec :: [Word32] -> (Word32, Word32) -> (Word32, Word32) teadec a b = calc_dec a b 0 decsum where calc_dec :: [Word32] -> (Word32, Word32) -> Int -> Word32 -> (Word32, Word32) calc_dec k r@(y,z) 32 sum = r calc_dec k (y,z) iter sum = calc_dec k (newy,newz) (iter+1) newsum where newz = z - (((mangle y) + y) `xor` (sum + (k!! word32ToInt ((sum `shiftR` 11) .&. 3)))) newy = y - (((mangle newz) + newz) `xor` (newsum + (k!! word32ToInt (newsum .&. 3)))) newsum = sum - delta mangle q = (q `shiftL` 4) `xor` (q `shiftR` 5)