summaryrefslogtreecommitdiff
path: root/Data/OpenPGP/Util/DecryptSecretKey.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-11-14 16:45:14 -0500
committerJoe Crayne <joe@jerkface.net>2019-11-14 16:45:14 -0500
commitb42c0d847a785487f3222b0d5360746d25d3209c (patch)
tree11ef85e3e4577eb047227f9938761bdac94a1309 /Data/OpenPGP/Util/DecryptSecretKey.hs
parent76bf7e08bccbb1a3a689068016b8a9c29d1e060e (diff)
Cv25519 encryption.
Diffstat (limited to 'Data/OpenPGP/Util/DecryptSecretKey.hs')
-rw-r--r--Data/OpenPGP/Util/DecryptSecretKey.hs27
1 files changed, 8 insertions, 19 deletions
diff --git a/Data/OpenPGP/Util/DecryptSecretKey.hs b/Data/OpenPGP/Util/DecryptSecretKey.hs
index 1188f3e..a637b29 100644
--- a/Data/OpenPGP/Util/DecryptSecretKey.hs
+++ b/Data/OpenPGP/Util/DecryptSecretKey.hs
@@ -90,7 +90,7 @@ decryptSecretKey pass k@(OpenPGP.SecretKeyPacket {
90 | OpenPGP.s2k_useage k == 254 = (20, sha1 . toStrictBS) 90 | OpenPGP.s2k_useage k == 254 = (20, sha1 . toStrictBS)
91 | otherwise = (2, toStrictBS . encode . checksum . toStrictBS) 91 | otherwise = (2, toStrictBS . encode . checksum . toStrictBS)
92 -- Words16s are written as 2 bytes in big-endian (network) order 92 -- Words16s are written as 2 bytes in big-endian (network) order
93 decd = withS2K simpleUnCFB salgo s2k (toLazyBS pass) (EncipheredWithIV encd) 93 decd = withS2K simpleUnCFB salgo (Just s2k) (toLazyBS pass) (EncipheredWithIV encd)
94 94
95#if defined(VERSION_cryptonite) 95#if defined(VERSION_cryptonite)
96 sha1 x = Bytes.convert (hash x :: Digest SHA1) 96 sha1 x = Bytes.convert (hash x :: Digest SHA1)
@@ -122,7 +122,7 @@ maybeGet g bs = unsafePerformIO $
122 122
123withS2K :: (forall k. (Vincent.BlockCipher k) => k -> Vincent.IV k -> LZ.ByteString -> LZ.ByteString) 123withS2K :: (forall k. (Vincent.BlockCipher k) => k -> Vincent.IV k -> LZ.ByteString -> LZ.ByteString)
124 -> OpenPGP.SymmetricAlgorithm 124 -> OpenPGP.SymmetricAlgorithm
125 -> OpenPGP.S2K 125 -> Maybe OpenPGP.S2K
126 -> LZ.ByteString -> Enciphered -> LZ.ByteString 126 -> LZ.ByteString -> Enciphered -> LZ.ByteString
127withS2K codec OpenPGP.AES128 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES128) 127withS2K codec OpenPGP.AES128 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES128)
128withS2K codec OpenPGP.AES192 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES192) 128withS2K codec OpenPGP.AES192 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES192)
@@ -131,7 +131,7 @@ 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) 131withS2K 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" 132withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K"
133 133
134withS2K' :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString 134withS2K' :: OpenPGP.SymmetricAlgorithm -> Maybe OpenPGP.S2K -> LZ.ByteString
135 -> (forall b. Vincent.BlockCipher b => b -> x) -> x 135 -> (forall b. Vincent.BlockCipher b => b -> x) -> x
136withS2K' OpenPGP.AES128 s2k s f = f (string2key s2k s :: Vincent.AES128) 136withS2K' OpenPGP.AES128 s2k s f = f (string2key s2k s :: Vincent.AES128)
137withS2K' OpenPGP.AES192 s2k s f = f (string2key s2k s :: Vincent.AES192) 137withS2K' OpenPGP.AES192 s2k s f = f (string2key s2k s :: Vincent.AES192)
@@ -169,27 +169,16 @@ padThenUnpad k f s = dropPadEnd (f padded)
169 padAmount = blksize - (LZ.length s `mod` blksize) 169 padAmount = blksize - (LZ.length s `mod` blksize)
170 blksize = fromIntegral $ Vincent.blockSize k 170 blksize = fromIntegral $ Vincent.blockSize k
171 171
172{- 172string2key :: (Vincent.BlockCipher k) => Maybe OpenPGP.S2K -> LZ.ByteString -> k
173Data/OpenPGP/Util/DecryptSecretKey.hs:172:20: 173string2key ms2k s = cipher
174 Couldn't match expected type ‘k’
175 with actual type ‘cryptonite-0.15:Crypto.Error.Types.CryptoFailable
176 cipher0’
177 ‘k’ is a rigid type variable bound by
178 the type signature for
179 string2key :: Vincent.BlockCipher k =>
180 OpenPGP.S2K -> LZ.ByteString -> k
181 at Data/OpenPGP/Util/DecryptSecretKey.hs:171:15
182-}
183string2key :: (Vincent.BlockCipher k) => OpenPGP.S2K -> LZ.ByteString -> k
184string2key s2k s = cipher
185 where 174 where
186#if defined(VERSION_cryptonite) 175#if defined(VERSION_cryptonite)
187 CryptoPassed cipher = Vincent.cipherInit k 176 CryptoPassed cipher = Vincent.cipherInit k
188 k = toStrictBS $ LZ.take ksize $ OpenPGP.string2key hashBySymbol s2k s 177 k = toStrictBS $ LZ.take ksize $ maybe s (\s2k -> OpenPGP.string2key hashBySymbol s2k s) ms2k
189#else 178#else
190 cipher = Vincent.cipherInit k 179 cipher = Vincent.cipherInit k
191 Right k = Vincent.makeKey $ toStrictBS $ 180 Right k = Vincent.makeKey $ toStrictBS $
192 LZ.take ksize $ OpenPGP.string2key hashBySymbol s2k s 181 LZ.take ksize $ maybe s (\s2k -> OpenPGP.string2key hashBySymbol s2k s) ms2k
193#endif 182#endif
194 ksize = case Vincent.cipherKeySize cipher of 183 ksize = case Vincent.cipherKeySize cipher of
195 Vincent.KeySizeFixed n -> fromIntegral n 184 Vincent.KeySizeFixed n -> fromIntegral n
@@ -217,7 +206,7 @@ encryptSecretKey passphrase s2k salgo plain = do
217 maybeToList $ lookup f (OpenPGP.key plain) 206 maybeToList $ lookup f (OpenPGP.key plain)
218 chk = LZ.fromChunks [ chkF material ] 207 chk = LZ.fromChunks [ chkF material ]
219 decd = LZ.append material chk 208 decd = LZ.append material chk
220 encd g = fst $ withS2K' salgo s2k (toLazyBS passphrase) (simpleCFB g) decd 209 encd g = fst $ withS2K' salgo (Just s2k) (toLazyBS passphrase) (simpleCFB g) decd
221 210
222 -- If the string-to-key usage octet is zero or 255, then a two-octet 211 -- If the string-to-key usage octet is zero or 255, then a two-octet
223 -- checksum of the plaintext of the algorithm-specific portion (sum 212 -- checksum of the plaintext of the algorithm-specific portion (sum