From 39d8a08aad1d2dd48b807ab867aa17475e4278c4 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 26 Aug 2016 03:10:40 -0400 Subject: Completed cryptonite support. --- Crypto/Cipher/Cast5.hs | 2 +- Crypto/Cipher/ThomasToVincent.hs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'Crypto/Cipher') diff --git a/Crypto/Cipher/Cast5.hs b/Crypto/Cipher/Cast5.hs index dfd30c7..da9d109 100644 --- a/Crypto/Cipher/Cast5.hs +++ b/Crypto/Cipher/Cast5.hs @@ -83,7 +83,7 @@ instance Cast5Bits size => Serialize (Cast5 size) where -- instance Cast5Bits size => BlockCipher (Cast5 size) where blockSize :: forall size. Cast5Bits size => Tagged (Cast5 size) Int -blockSize = Tagged 64 +blockSize = Tagged 64 -- bits encryptBlock :: forall size. Cast5Bits size => Cast5 size -> S.ByteString -> S.ByteString encryptBlock (Cast5 subkeys fs _ _ key) = diff --git a/Crypto/Cipher/ThomasToVincent.hs b/Crypto/Cipher/ThomasToVincent.hs index 5a68cf3..cf5cdee 100644 --- a/Crypto/Cipher/ThomasToVincent.hs +++ b/Crypto/Cipher/ThomasToVincent.hs @@ -5,7 +5,12 @@ 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 {- @@ -19,9 +24,14 @@ endif type ThomasToVincent b = b instance Cast5Bits size => Cipher (Cast5 size) where - cipherName _ = "CAST-5" + 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 @@ -42,14 +52,25 @@ 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 = - let chunks = chunkFor' 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 {- -- cgit v1.2.3