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 +++++++++++++++++++++++-- Data/OpenPGP/Util/DecryptSecretKey.hs | 14 ++++++-------- 3 files changed, 30 insertions(+), 11 deletions(-) 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 {- diff --git a/Data/OpenPGP/Util/DecryptSecretKey.hs b/Data/OpenPGP/Util/DecryptSecretKey.hs index 01728d3..1c519ae 100644 --- a/Data/OpenPGP/Util/DecryptSecretKey.hs +++ b/Data/OpenPGP/Util/DecryptSecretKey.hs @@ -47,13 +47,13 @@ data Enciphered = EncipheredWithIV !LZ.ByteString -- initial vector is appended to front of ByteString | EncipheredZeroIV !LZ.ByteString -- initial vector is zero, ByteString contains only the block -withIV :: (Vincent.BlockCipher k) => (Vincent.IV k -> LZ.ByteString -> LZ.ByteString) -> Enciphered -> LZ.ByteString +withIV :: forall k. (Vincent.BlockCipher k) => (Vincent.IV k -> LZ.ByteString -> LZ.ByteString) -> Enciphered -> LZ.ByteString withIV f (EncipheredWithIV s) = f iv bs where Just iv = Vincent.makeIV (toStrictBS ivbs) (ivbs,bs) = LZ.splitAt (fromIntegral ivlen) s #if defined(VERSION_cryptonite) - ivlen = Bytes.length iv + ivlen = Bytes.length (Vincent.nullIV :: Vincent.IV k) #else ivlen = Vincent.byteableLength z _ = Vincent.constEqBytes z iv @@ -126,8 +126,7 @@ withS2K codec OpenPGP.AES128 s2k s = withIV $ codec (string2key s2k s :: Vince withS2K codec OpenPGP.AES192 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES192) withS2K codec OpenPGP.AES256 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES256) withS2K codec OpenPGP.Blowfish s2k s = withIV $ codec (string2key s2k s :: Vincent.Blowfish128) --- TODO: cast5 support --- withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128) +withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128) withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K" withS2K' :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString @@ -136,21 +135,20 @@ withS2K' OpenPGP.AES128 s2k s f = f (string2key s2k s :: Vincent.AES128) withS2K' OpenPGP.AES192 s2k s f = f (string2key s2k s :: Vincent.AES192) withS2K' OpenPGP.AES256 s2k s f = f (string2key s2k s :: Vincent.AES256) withS2K' OpenPGP.Blowfish s2k s f = f (string2key s2k s :: Vincent.Blowfish128) --- TODO: cast5 support --- withS2K' OpenPGP.CAST5 s2k s f = f (string2key s2k s :: ThomasToVincent CAST5_128) +withS2K' OpenPGP.CAST5 s2k s f = f (string2key s2k s :: ThomasToVincent CAST5_128) -- decryption codec for withS2K simpleUnCFB :: (Vincent.BlockCipher k) => k -> Vincent.IV k -> LZ.ByteString -> LZ.ByteString simpleUnCFB k iv = padThenUnpad k (toLazyBS . Vincent.cfbDecrypt k iv . toStrictBS) -simpleCFB :: (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g) +simpleCFB :: forall k g. (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g) simpleCFB g k bs = ( padThenUnpad k (LZ.fromChunks . (ivbs:) . (:[]) . Vincent.cfbEncrypt k iv . toStrictBS) bs , g' ) where Just iv = Vincent.makeIV ivbs #if defined(VERSION_cryptonite) (ivbs,g') = Vincent.randomBytesGenerate ivlen g - ivlen = Bytes.length iv + ivlen = Bytes.length (Vincent.nullIV :: Vincent.IV k) #else z = Vincent.nullIV (ivbs,g') = Vincent.cprgGenerate ivlen g -- cgit v1.2.3