blob: cf5cdee273e3440b72037ea2cea604674fffad77 (
plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Crypto.Cipher.ThomasToVincent where
import qualified Data.ByteString as S
import Crypto.Cipher.Types
import Crypto.Cipher.Cast5
#if defined(VERSION_cryptonite)
import qualified Data.ByteArray as Bytes
import Crypto.Error
#else
import Data.Byteable
#endif
import Data.Tagged
{-
import qualified Crypto.Classes as Thomas
if ! MIN_VERSION_crypto_api(0,11,0)
import qualified Crypto.Modes as Thomas
endif
-}
type ThomasToVincent b = b
instance Cast5Bits size => Cipher (Cast5 size) where
cipherName _ = "CAST-"++show (cast5bits (undefined :: size))
#if defined(VERSION_cryptonite)
cipherInit k = CryptoPassed b
where Just b = buildKey (Bytes.convert k)
#else
cipherInit k = b
where Just b = buildKey (toBytes k)
#endif
cipherKeySize _ = KeySizeFixed (bitlen `div` 8)
where Tagged bitlen = keyLength :: Tagged (Cast5 size) Int
-- Break a bytestring into block size chunks.
chunkFor' :: (Cast5Bits size) => Cast5 size -> S.ByteString -> [S.ByteString]
chunkFor' k = go
where
blkSz = (Crypto.Cipher.Cast5.blockSize `for` k) `div` 8
go bs | S.length bs < blkSz = []
| otherwise = let (blk,rest) = S.splitAt blkSz bs in blk : go rest
-- |Obtain a tagged value for a particular instantiated type.
for :: Tagged a b -> a -> b
for t _ = unTagged t
instance Cast5Bits size => BlockCipher (Cast5 size) where
blockSize _ = bitlen `div` 8
where Tagged bitlen = Crypto.Cipher.Cast5.blockSize :: Tagged (Cast5 size) Int
-- ecbEncrypt :: (BlockCipher cipher, ByteArray ba) => cipher -> ba -> ba
-- modeEcb' :: BlockCipher k => k -> B.ByteString -> B.ByteString
ecbEncrypt k msg =
#if defined(VERSION_cryptonite)
let chunks = chunkFor' k $ Bytes.convert msg
in Bytes.convert $ S.concat $ map (encryptBlock k) chunks
#else
let chunks = chunkFor' k $ msg
in S.concat $ map (encryptBlock k) chunks
#endif
ecbDecrypt k ct =
#if defined(VERSION_cryptonite)
let chunks = chunkFor' k $ Bytes.convert ct
in Bytes.convert $ S.concat $ map (decryptBlock k) chunks
#else
let chunks = chunkFor' k ct
in S.concat $ map (decryptBlock k) chunks
#endif
{-
newtype ThomasToVincent b = ThomasToVincent b
instance Thomas.BlockCipher b => Cipher (ThomasToVincent b) where
cipherName _ = "ThomasToVincent"
cipherInit k = ThomasToVincent b
where Just b = Thomas.buildKey (toBytes k)
cipherKeySize _ = KeySizeFixed (bitlen `div` 8)
where Tagged bitlen = Thomas.keyLength :: Tagged b Int
instance Thomas.BlockCipher b => BlockCipher (ThomasToVincent b) where
blockSize _ = bitlen `div` 8
where Tagged bitlen = Thomas.blockSize :: Tagged b Int
if ! MIN_VERSION_crypto_api(0,11,0)
ecbEncrypt (ThomasToVincent k) = Thomas.ecb' k
ecbDecrypt (ThomasToVincent k) = Thomas.unEcb' k
else
ecbEncrypt (ThomasToVincent k) = Thomas.ecb k
ecbDecrypt (ThomasToVincent k) = Thomas.unEcb k
endif
-}
|