summaryrefslogtreecommitdiff
path: root/Data/OpenPGP/Util/DecryptSecretKey.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Data/OpenPGP/Util/DecryptSecretKey.hs')
-rw-r--r--Data/OpenPGP/Util/DecryptSecretKey.hs40
1 files changed, 1 insertions, 39 deletions
diff --git a/Data/OpenPGP/Util/DecryptSecretKey.hs b/Data/OpenPGP/Util/DecryptSecretKey.hs
index a637b29..57dd8c3 100644
--- a/Data/OpenPGP/Util/DecryptSecretKey.hs
+++ b/Data/OpenPGP/Util/DecryptSecretKey.hs
@@ -27,6 +27,7 @@ import qualified Crypto.Cipher.AES as Vincent
27import qualified Crypto.Cipher.Blowfish as Vincent 27import qualified Crypto.Cipher.Blowfish as Vincent
28 28
29import qualified Crypto.Cipher.Types as Vincent 29import qualified Crypto.Cipher.Types as Vincent
30import Data.OpenPGP.Util.Decrypt
30 31
31#if defined(VERSION_cryptonite) 32#if defined(VERSION_cryptonite)
32import qualified Data.ByteArray as Bytes 33import qualified Data.ByteArray as Bytes
@@ -131,18 +132,6 @@ withS2K codec OpenPGP.Blowfish s2k s = withIV $ codec (string2key s2k s :: Vince
131withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128) 132withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128)
132withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K" 133withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K"
133 134
134withS2K' :: OpenPGP.SymmetricAlgorithm -> Maybe OpenPGP.S2K -> LZ.ByteString
135 -> (forall b. Vincent.BlockCipher b => b -> x) -> x
136withS2K' OpenPGP.AES128 s2k s f = f (string2key s2k s :: Vincent.AES128)
137withS2K' OpenPGP.AES192 s2k s f = f (string2key s2k s :: Vincent.AES192)
138withS2K' OpenPGP.AES256 s2k s f = f (string2key s2k s :: Vincent.AES256)
139withS2K' OpenPGP.Blowfish s2k s f = f (string2key s2k s :: Vincent.Blowfish128)
140withS2K' OpenPGP.CAST5 s2k s f = f (string2key s2k s :: ThomasToVincent CAST5_128)
141
142-- decryption codec for withS2K
143simpleUnCFB :: (Vincent.BlockCipher k) => k -> Vincent.IV k -> LZ.ByteString -> LZ.ByteString
144simpleUnCFB k iv = padThenUnpad k (toLazyBS . Vincent.cfbDecrypt k iv . toStrictBS)
145
146simpleCFB :: forall k g. (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g) 135simpleCFB :: forall k g. (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g)
147simpleCFB g k bs = ( padThenUnpad k (LZ.fromChunks . (ivbs:) . (:[]) . Vincent.cfbEncrypt k iv . toStrictBS) bs 136simpleCFB g k bs = ( padThenUnpad k (LZ.fromChunks . (ivbs:) . (:[]) . Vincent.cfbEncrypt k iv . toStrictBS) bs
148 , g' ) 137 , g' )
@@ -158,33 +147,6 @@ simpleCFB g k bs = ( padThenUnpad k (LZ.fromChunks . (ivbs:) . (:[]) . Vincent.c
158 _ = Vincent.constEqBytes z iv 147 _ = Vincent.constEqBytes z iv
159#endif 148#endif
160 149
161-- Apply a function f to a zero-padded bytestring s to a multiple
162-- of the blocksize for cyper k.
163-- Then drop the same number of bytes from the result of f.
164padThenUnpad :: (Vincent.BlockCipher k) => k -> (LZ.ByteString -> LZ.ByteString) -> LZ.ByteString -> LZ.ByteString
165padThenUnpad k f s = dropPadEnd (f padded)
166 where
167 dropPadEnd s = LZ.take (LZ.length s - padAmount) s
168 padded = s `LZ.append` LZ.replicate padAmount 0
169 padAmount = blksize - (LZ.length s `mod` blksize)
170 blksize = fromIntegral $ Vincent.blockSize k
171
172string2key :: (Vincent.BlockCipher k) => Maybe OpenPGP.S2K -> LZ.ByteString -> k
173string2key ms2k s = cipher
174 where
175#if defined(VERSION_cryptonite)
176 CryptoPassed cipher = Vincent.cipherInit k
177 k = toStrictBS $ LZ.take ksize $ maybe s (\s2k -> OpenPGP.string2key hashBySymbol s2k s) ms2k
178#else
179 cipher = Vincent.cipherInit k
180 Right k = Vincent.makeKey $ toStrictBS $
181 LZ.take ksize $ maybe s (\s2k -> OpenPGP.string2key hashBySymbol s2k s) ms2k
182#endif
183 ksize = case Vincent.cipherKeySize cipher of
184 Vincent.KeySizeFixed n -> fromIntegral n
185 Vincent.KeySizeEnum xs -> error $ "Unknown key size in string2key"
186 Vincent.KeySizeRange min max -> error $ "Unknown key size range in string2key"
187
188catchIO_ :: IO a -> IO a -> IO a 150catchIO_ :: IO a -> IO a -> IO a
189catchIO_ a h = Exception.catch a (\(_ :: IOException) -> h) 151catchIO_ a h = Exception.catch a (\(_ :: IOException) -> h)
190 152