summaryrefslogtreecommitdiff
path: root/Crypto/Cipher/Cast5.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Crypto/Cipher/Cast5.hs')
-rw-r--r--Crypto/Cipher/Cast5.hs50
1 files changed, 30 insertions, 20 deletions
diff --git a/Crypto/Cipher/Cast5.hs b/Crypto/Cipher/Cast5.hs
index fb5db20..dfd30c7 100644
--- a/Crypto/Cipher/Cast5.hs
+++ b/Crypto/Cipher/Cast5.hs
@@ -14,7 +14,7 @@ import qualified Data.Vector.Unboxed as Vector
14import Data.Vector.Unboxed (Vector, (//)) 14import Data.Vector.Unboxed (Vector, (//))
15import Data.Word 15import Data.Word
16import Data.Tuple 16import Data.Tuple
17import Crypto.Classes 17-- import Crypto.Classes -- Thomas' crypto-api incompatible with Vincent's crypto-random.
18import Data.Serialize 18import Data.Serialize
19import qualified Data.ByteString as S 19import qualified Data.ByteString as S
20import Data.Tagged (Tagged(..)) 20import Data.Tagged (Tagged(..))
@@ -81,25 +81,32 @@ instance Cast5Bits size => Serialize (Cast5 size) where
81 let Just key = buildKey bs 81 let Just key = buildKey bs
82 return key 82 return key
83 83
84instance Cast5Bits size => BlockCipher (Cast5 size) where 84-- instance Cast5Bits size => BlockCipher (Cast5 size) where
85 blockSize = Tagged 64 85blockSize :: forall size. Cast5Bits size => Tagged (Cast5 size) Int
86 encryptBlock (Cast5 subkeys fs _ _ key) = 86blockSize = Tagged 64
87 fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair 87
88 where 88encryptBlock :: forall size. Cast5Bits size => Cast5 size -> S.ByteString -> S.ByteString
89 nrounds = numberOfRounds (cast5bits (undefined::size)) 89encryptBlock (Cast5 subkeys fs _ _ key) =
90 decryptBlock (Cast5 _ _ subkeys fs key) = 90 fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair
91 fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair 91 where
92 where 92 nrounds = numberOfRounds (cast5bits (undefined::size))
93 nrounds = numberOfRounds (cast5bits (undefined::size)) 93decryptBlock :: forall size. Cast5Bits size => Cast5 size -> S.ByteString -> S.ByteString
94 buildKey bs = Just (Cast5 subs fs subs' fs' key) 94decryptBlock (Cast5 _ _ subkeys fs key) =
95 where 95 fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair
96 nrounds = numberOfRounds (cast5bits (undefined::size)) 96 where
97 key = initKey bs 97 nrounds = numberOfRounds (cast5bits (undefined::size))
98 fs = cycle [f1,f2,f3] 98buildKey :: forall size. Cast5Bits size => S.ByteString -> Maybe (Cast5 size)
99 subs = subkeys key 99buildKey bs = Just (Cast5 subs fs subs' fs' key)
100 fs' = drop (3 - (nrounds `rem` 3)) $ cycle [f3,f2,f1] 100 where
101 subs' = (reverse . take nrounds $ subs) 101 nrounds = numberOfRounds (cast5bits (undefined::size))
102 keyLength = Tagged (cast5bits (undefined::size)) 102 key = initKey bs
103 fs = cycle [f1,f2,f3]
104 subs = subkeys key
105 fs' = drop (3 - (nrounds `rem` 3)) $ cycle [f3,f2,f1]
106 subs' = (reverse . take nrounds $ subs)
107
108keyLength :: forall size. Cast5Bits size => Tagged (Cast5 size) Int
109keyLength = Tagged (cast5bits (undefined::size))
103 110
104 111
105{-# INLINE (^) #-} 112{-# INLINE (^) #-}
@@ -544,6 +551,8 @@ hasSize :: Cast5 size -> size -> a
544hasSize _ _ = undefined 551hasSize _ _ = undefined
545 552
546 553
554{-
555
547data StaticTest size = StaticTest { 556data StaticTest size = StaticTest {
548 keysize :: size, 557 keysize :: size,
549 keybytes :: S.ByteString, 558 keybytes :: S.ByteString,
@@ -673,3 +682,4 @@ main = do
673 , "expected b = " ++ show (S.unpack expected_b) 682 , "expected b = " ++ show (S.unpack expected_b)
674 , "computed b = " ++ show (S.unpack result_b) ] 683 , "computed b = " ++ show (S.unpack result_b) ]
675 return () 684 return ()
685-}