-- |
-- Module      : Crypto.Cipher.CAST5
-- License     : BSD-style
-- Maintainer  : Olivier Chéron <olivier.cheron@gmail.com>
-- Stability   : stable
-- Portability : good
--
module Crypto.Cipher.CAST5
    ( CAST5
    ) where

import           Crypto.Error
import           Crypto.Cipher.Types
import           Crypto.Cipher.CAST5.Primitive
import           Crypto.Internal.ByteArray (ByteArrayAccess)
import qualified Crypto.Internal.ByteArray as B

-- | CAST5 block cipher (also known as CAST-128).  Key is between
-- 40 and 128 bits.
newtype CAST5 = CAST5 Key

instance Cipher CAST5 where
    cipherName :: CAST5 -> String
cipherName    CAST5
_ = String
"CAST5"
    cipherKeySize :: CAST5 -> KeySizeSpecifier
cipherKeySize CAST5
_ = Int -> Int -> KeySizeSpecifier
KeySizeRange Int
5 Int
16
    cipherInit :: forall key. ByteArray key => key -> CryptoFailable CAST5
cipherInit      = forall key. ByteArrayAccess key => key -> CryptoFailable CAST5
initCAST5

instance BlockCipher CAST5 where
    blockSize :: CAST5 -> Int
blockSize CAST5
_ = Int
8
    ecbEncrypt :: forall ba. ByteArray ba => CAST5 -> ba -> ba
ecbEncrypt (CAST5 Key
k) = forall bs. ByteArray bs => (Word64 -> Word64) -> bs -> bs
B.mapAsWord64 (Key -> Word64 -> Word64
encrypt Key
k)
    ecbDecrypt :: forall ba. ByteArray ba => CAST5 -> ba -> ba
ecbDecrypt (CAST5 Key
k) = forall bs. ByteArray bs => (Word64 -> Word64) -> bs -> bs
B.mapAsWord64 (Key -> Word64 -> Word64
decrypt Key
k)

initCAST5 :: ByteArrayAccess key => key -> CryptoFailable CAST5
initCAST5 :: forall key. ByteArrayAccess key => key -> CryptoFailable CAST5
initCAST5 key
bs
    | Int
len forall a. Ord a => a -> a -> Bool
<   Int
5 = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_KeySizeInvalid
    | Int
len forall a. Ord a => a -> a -> Bool
<  Int
16 = forall a. a -> CryptoFailable a
CryptoPassed (Key -> CAST5
CAST5 forall a b. (a -> b) -> a -> b
$ forall key. ByteArrayAccess key => Bool -> key -> Key
buildKey Bool
short Bytes
padded)
    | Int
len forall a. Eq a => a -> a -> Bool
== Int
16 = forall a. a -> CryptoFailable a
CryptoPassed (Key -> CAST5
CAST5 forall a b. (a -> b) -> a -> b
$ forall key. ByteArrayAccess key => Bool -> key -> Key
buildKey Bool
False key
bs)
    | Bool
otherwise = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_KeySizeInvalid
  where
    len :: Int
len   = forall ba. ByteArrayAccess ba => ba -> Int
B.length key
bs
    short :: Bool
short = Int
len forall a. Ord a => a -> a -> Bool
<= Int
10

    padded :: B.Bytes
    padded :: Bytes
padded = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
B.convert key
bs forall bs. ByteArray bs => bs -> bs -> bs
`B.append` forall ba. ByteArray ba => Int -> Word8 -> ba
B.replicate (Int
16 forall a. Num a => a -> a -> a
- Int
len) Word8
0