diff options
Diffstat (limited to 'Data/OpenPGP/Util/DecryptSecretKey.hs')
-rw-r--r-- | Data/OpenPGP/Util/DecryptSecretKey.hs | 40 |
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 | |||
27 | import qualified Crypto.Cipher.Blowfish as Vincent | 27 | import qualified Crypto.Cipher.Blowfish as Vincent |
28 | 28 | ||
29 | import qualified Crypto.Cipher.Types as Vincent | 29 | import qualified Crypto.Cipher.Types as Vincent |
30 | import Data.OpenPGP.Util.Decrypt | ||
30 | 31 | ||
31 | #if defined(VERSION_cryptonite) | 32 | #if defined(VERSION_cryptonite) |
32 | import qualified Data.ByteArray as Bytes | 33 | import qualified Data.ByteArray as Bytes |
@@ -131,18 +132,6 @@ withS2K codec OpenPGP.Blowfish s2k s = withIV $ codec (string2key s2k s :: Vince | |||
131 | withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128) | 132 | withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128) |
132 | withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K" | 133 | withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K" |
133 | 134 | ||
134 | withS2K' :: OpenPGP.SymmetricAlgorithm -> Maybe OpenPGP.S2K -> LZ.ByteString | ||
135 | -> (forall b. Vincent.BlockCipher b => b -> x) -> x | ||
136 | withS2K' OpenPGP.AES128 s2k s f = f (string2key s2k s :: Vincent.AES128) | ||
137 | withS2K' OpenPGP.AES192 s2k s f = f (string2key s2k s :: Vincent.AES192) | ||
138 | withS2K' OpenPGP.AES256 s2k s f = f (string2key s2k s :: Vincent.AES256) | ||
139 | withS2K' OpenPGP.Blowfish s2k s f = f (string2key s2k s :: Vincent.Blowfish128) | ||
140 | withS2K' OpenPGP.CAST5 s2k s f = f (string2key s2k s :: ThomasToVincent CAST5_128) | ||
141 | |||
142 | -- decryption codec for withS2K | ||
143 | simpleUnCFB :: (Vincent.BlockCipher k) => k -> Vincent.IV k -> LZ.ByteString -> LZ.ByteString | ||
144 | simpleUnCFB k iv = padThenUnpad k (toLazyBS . Vincent.cfbDecrypt k iv . toStrictBS) | ||
145 | |||
146 | simpleCFB :: forall k g. (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g) | 135 | simpleCFB :: forall k g. (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g) |
147 | simpleCFB g k bs = ( padThenUnpad k (LZ.fromChunks . (ivbs:) . (:[]) . Vincent.cfbEncrypt k iv . toStrictBS) bs | 136 | simpleCFB 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. | ||
164 | padThenUnpad :: (Vincent.BlockCipher k) => k -> (LZ.ByteString -> LZ.ByteString) -> LZ.ByteString -> LZ.ByteString | ||
165 | padThenUnpad 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 | |||
172 | string2key :: (Vincent.BlockCipher k) => Maybe OpenPGP.S2K -> LZ.ByteString -> k | ||
173 | string2key 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 | |||
188 | catchIO_ :: IO a -> IO a -> IO a | 150 | catchIO_ :: IO a -> IO a -> IO a |
189 | catchIO_ a h = Exception.catch a (\(_ :: IOException) -> h) | 151 | catchIO_ a h = Exception.catch a (\(_ :: IOException) -> h) |
190 | 152 | ||