File: CAST5.hs

package info (click to toggle)
haskell-crypton 1.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,548 kB
  • sloc: haskell: 26,764; ansic: 22,294; makefile: 6
file content (42 lines) | stat: -rw-r--r-- 1,253 bytes parent folder | download
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
-- |
-- 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.Cipher.CAST5.Primitive
import Crypto.Cipher.Types
import Crypto.Error
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