File: Blowfish.hs

package info (click to toggle)
haskell-cipher-blowfish 0.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 96 kB
  • ctags: 1
  • sloc: haskell: 476; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,671 bytes parent folder | download | duplicates (3)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE CPP #-}
-- |
-- Module      : Crypto.Cipher.Blowfish
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : stable
-- Portability : good
--
module Crypto.Cipher.Blowfish
    ( Blowfish
    , Blowfish64
    , Blowfish128
    , Blowfish256
    , Blowfish448
    ) where

import Data.Byteable
import Crypto.Cipher.Types
import Crypto.Cipher.Blowfish.Primitive

-- | variable keyed blowfish state
newtype Blowfish = Blowfish Context

-- | 64 bit keyed blowfish state
newtype Blowfish64 = Blowfish64 Context

-- | 128 bit keyed blowfish state
newtype Blowfish128 = Blowfish128 Context

-- | 256 bit keyed blowfish state
newtype Blowfish256 = Blowfish256 Context

-- | 448 bit keyed blowfish state
newtype Blowfish448 = Blowfish448 Context

instance Cipher Blowfish where
    cipherName _    = "blowfish"
    cipherKeySize _ = KeySizeRange 6 56
    cipherInit k = either error Blowfish $ initBlowfish (toBytes k)

instance BlockCipher Blowfish where
    blockSize _ = 8
    ecbEncrypt (Blowfish bf) = encrypt bf
    ecbDecrypt (Blowfish bf) = decrypt bf

#define INSTANCE_CIPHER(CSTR, NAME, KEYSIZE) \
instance Cipher CSTR where \
    { cipherName _ = NAME \
    ; cipherKeySize _ = KeySizeFixed KEYSIZE \
    ; cipherInit k = either error CSTR $ initBlowfish (toBytes k) \
    }; \
instance BlockCipher CSTR where \
    { blockSize _ = 8 \
    ; ecbEncrypt (CSTR bf) = encrypt bf \
    ; ecbDecrypt (CSTR bf) = decrypt bf \
    };

INSTANCE_CIPHER(Blowfish64, "blowfish64", 8)
INSTANCE_CIPHER(Blowfish128, "blowfish128", 16)
INSTANCE_CIPHER(Blowfish256, "blowfish256", 32)
INSTANCE_CIPHER(Blowfish448, "blowfish448", 56)