File: CAST5.hs

package info (click to toggle)
haskell-cryptonite 0.30-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,372 kB
  • sloc: ansic: 22,009; haskell: 18,423; makefile: 8
file content (43 lines) | stat: -rw-r--r-- 1,312 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- |
-- 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"
    cipherKeySize _ = KeySizeRange 5 16
    cipherInit      = initCAST5

instance BlockCipher CAST5 where
    blockSize _ = 8
    ecbEncrypt (CAST5 k) = B.mapAsWord64 (encrypt k)
    ecbDecrypt (CAST5 k) = B.mapAsWord64 (decrypt k)

initCAST5 :: ByteArrayAccess key => key -> CryptoFailable CAST5
initCAST5 bs
    | len <   5 = CryptoFailed CryptoError_KeySizeInvalid
    | len <  16 = CryptoPassed (CAST5 $ buildKey short padded)
    | len == 16 = CryptoPassed (CAST5 $ buildKey False bs)
    | otherwise = CryptoFailed CryptoError_KeySizeInvalid
  where
    len   = B.length bs
    short = len <= 10

    padded :: B.Bytes
    padded = B.convert bs `B.append` B.replicate (16 - len) 0