From 6b42fc6904d05979c409c0043d6d7b2eed22b58c Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 14 Dec 2013 23:14:16 -0500 Subject: Refactoring. --- Data/OpenPGP/Util/Base.hs | 67 +++++++++++++++++++++++++++++++++++ Data/OpenPGP/Util/DecryptSecretKey.hs | 26 ++------------ Data/OpenPGP/Util/Sign.hs | 56 ++--------------------------- Data/OpenPGP/Util/Verify.hs | 66 +--------------------------------- 4 files changed, 72 insertions(+), 143 deletions(-) create mode 100644 Data/OpenPGP/Util/Base.hs (limited to 'Data') diff --git a/Data/OpenPGP/Util/Base.hs b/Data/OpenPGP/Util/Base.hs new file mode 100644 index 0000000..802d503 --- /dev/null +++ b/Data/OpenPGP/Util/Base.hs @@ -0,0 +1,67 @@ +module Data.OpenPGP.Util.Base where + +import qualified Data.ByteString as BS +import qualified Data.ByteString.Lazy as LZ +import Data.Binary (encode) + +import Data.OpenPGP as OpenPGP +import Crypto.Hash.MD5 as MD5 +import Crypto.Hash.SHA1 as SHA1 +import Crypto.Hash.SHA256 as SHA256 +import Crypto.Hash.SHA384 as SHA384 +import Crypto.Hash.SHA512 as SHA512 +import Crypto.Hash.SHA224 as SHA224 +import Crypto.Hash.RIPEMD160 as RIPEMD160 +import qualified Crypto.PubKey.RSA as Vincent.RSA +import Crypto.PubKey.HashDescr as Vincent + +import Data.OpenPGP.Util.Fingerprint (fingerprint) + +hashBySymbol OpenPGP.MD5 = MD5.hashlazy +hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy +hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy +hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy +hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy +hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy +hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy + +toStrictBS :: LZ.ByteString -> BS.ByteString +toStrictBS = BS.concat . LZ.toChunks + +toLazyBS :: BS.ByteString -> LZ.ByteString +toLazyBS = LZ.fromChunks . (:[]) + +find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet +find_key = OpenPGP.find_key fingerprint + + + +keyParam :: Char -> OpenPGP.Packet -> Integer +keyParam c k = fromJustMPI $ lookup c (OpenPGP.key k) + where + fromJustMPI :: Maybe OpenPGP.MPI -> Integer + fromJustMPI (Just (OpenPGP.MPI x)) = x + fromJustMPI _ = error "Not a Just MPI, Data.OpenPGP.CryptoAPI" + +integerBytesize :: Integer -> Int +integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2 + +rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey +rsaKey k = + Vincent.RSA.PublicKey (integerBytesize n) n (keyParam 'e' k) + where + n = keyParam 'n' k + +-- http://tools.ietf.org/html/rfc3447#page-43 +-- http://tools.ietf.org/html/rfc4880#section-5.2.2 +hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 +hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 +hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 +hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 +hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 +hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 +hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 +hashAlgoDesc _ = + error "Unsupported HashAlgorithm in hashAlgoDesc" + + diff --git a/Data/OpenPGP/Util/DecryptSecretKey.hs b/Data/OpenPGP/Util/DecryptSecretKey.hs index 0ba89d2..4370ffc 100644 --- a/Data/OpenPGP/Util/DecryptSecretKey.hs +++ b/Data/OpenPGP/Util/DecryptSecretKey.hs @@ -3,22 +3,14 @@ module Data.OpenPGP.Util.DecryptSecretKey where import qualified Data.OpenPGP as OpenPGP import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as LZ -import Data.Word (Word16,Word8) -import Data.Char (toUpper) +import Data.Word (Word16) import Control.Monad (foldM) -import Numeric (showHex) import Data.Binary (get,Binary,Get) import Data.Binary.Get (runGetOrFail) import qualified Data.Serialize as Serialize import Control.Applicative ( (<$>) ) -import Crypto.Hash.MD5 as MD5 import Crypto.Hash.SHA1 as SHA1 -import Crypto.Hash.SHA256 as SHA256 -import Crypto.Hash.SHA384 as SHA384 -import Crypto.Hash.SHA512 as SHA512 -import Crypto.Hash.SHA224 as SHA224 -import Crypto.Hash.RIPEMD160 as RIPEMD160 import qualified Crypto.Cipher.AES as Vincent import qualified Crypto.Cipher.Blowfish as Vincent @@ -28,16 +20,9 @@ import qualified Data.Byteable as Vincent import Crypto.Cipher.Cast5 (CAST5_128) import Crypto.Cipher.ThomasToVincent +import Data.OpenPGP.Util.Base (toStrictBS,toLazyBS,hashBySymbol) -hashBySymbol OpenPGP.MD5 = MD5.hashlazy -hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy -hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy -hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy -hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy -hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy -hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy - data Enciphered = @@ -94,13 +79,6 @@ decryptSecretKey pass k@(OpenPGP.SecretKeyPacket { decryptSecretKey _ _ = Nothing -toStrictBS :: LZ.ByteString -> BS.ByteString -toStrictBS = BS.concat . LZ.toChunks - -toLazyBS :: BS.ByteString -> LZ.ByteString -toLazyBS = LZ.fromChunks . (:[]) - - string2sdecrypt :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString -> Enciphered -> LZ.ByteString string2sdecrypt OpenPGP.AES128 s2k s = withIV $ simpleUnCFB (string2key s2k s :: Vincent.AES128) diff --git a/Data/OpenPGP/Util/Sign.hs b/Data/OpenPGP/Util/Sign.hs index ef7d16b..e492f95 100644 --- a/Data/OpenPGP/Util/Sign.hs +++ b/Data/OpenPGP/Util/Sign.hs @@ -17,28 +17,8 @@ import qualified Crypto.Random as Vincent import qualified Crypto.PubKey.DSA as Vincent.DSA import qualified Crypto.PubKey.RSA as Vincent.RSA import qualified Crypto.PubKey.RSA.PKCS15 as Vincent.RSA -import Crypto.PubKey.HashDescr as Vincent - -import Crypto.Hash.MD5 as MD5 -import Crypto.Hash.SHA1 as SHA1 -import Crypto.Hash.SHA256 as SHA256 -import Crypto.Hash.SHA384 as SHA384 -import Crypto.Hash.SHA512 as SHA512 -import Crypto.Hash.SHA224 as SHA224 -import Crypto.Hash.RIPEMD160 as RIPEMD160 - -hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 -hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 -hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 -hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 -hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 -hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 -hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 -hashAlgoDesc _ = - error "Unsupported HashAlgorithm in hashAlgoDesc" - -find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet -find_key = OpenPGP.find_key fingerprint + +import Data.OpenPGP.Util.Base privateDSAkey :: OpenPGP.Packet -> Vincent.DSA.PrivateKey @@ -58,38 +38,6 @@ privateRSAkey k = q = keyParam 'q' k pubkey = rsaKey k -rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey -rsaKey k = - Vincent.RSA.PublicKey (integerBytesize n) n (keyParam 'e' k) - where - n = keyParam 'n' k - -integerBytesize :: Integer -> Int -integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2 - - -toStrictBS :: LZ.ByteString -> BS.ByteString -toStrictBS = BS.concat . LZ.toChunks - -toLazyBS :: BS.ByteString -> LZ.ByteString -toLazyBS = LZ.fromChunks . (:[]) - - -keyParam :: Char -> OpenPGP.Packet -> Integer -keyParam c k = fromJustMPI $ lookup c (OpenPGP.key k) -fromJustMPI :: Maybe OpenPGP.MPI -> Integer -fromJustMPI (Just (OpenPGP.MPI x)) = x -fromJustMPI _ = error "Not a Just MPI, Data.OpenPGP.CryptoAPI" - -hashBySymbol OpenPGP.MD5 = MD5.hashlazy -hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy -hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy -hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy -hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy -hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy -hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy - - -- | Make a signature diff --git a/Data/OpenPGP/Util/Verify.hs b/Data/OpenPGP/Util/Verify.hs index 137c00f..2367570 100644 --- a/Data/OpenPGP/Util/Verify.hs +++ b/Data/OpenPGP/Util/Verify.hs @@ -6,69 +6,18 @@ import Data.Binary (encode) import Control.Monad import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as LZ -import Data.Monoid ( (<>) ) - -import Data.OpenPGP.Util.Fingerprint (fingerprint) import qualified Crypto.PubKey.DSA as Vincent.DSA -import qualified Crypto.PubKey.RSA as Vincent.RSA import qualified Crypto.PubKey.RSA.PKCS15 as Vincent.RSA -import Crypto.PubKey.HashDescr as Vincent - -import Crypto.Hash.MD5 as MD5 -import Crypto.Hash.SHA1 as SHA1 -import Crypto.Hash.SHA256 as SHA256 -import Crypto.Hash.SHA384 as SHA384 -import Crypto.Hash.SHA512 as SHA512 -import Crypto.Hash.SHA224 as SHA224 -import Crypto.Hash.RIPEMD160 as RIPEMD160 - -hashBySymbol OpenPGP.MD5 = MD5.hashlazy -hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy -hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy -hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy -hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy -hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy -hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy - - -toStrictBS :: LZ.ByteString -> BS.ByteString -toStrictBS = BS.concat . LZ.toChunks - -toLazyBS :: BS.ByteString -> LZ.ByteString -toLazyBS = LZ.fromChunks . (:[]) - -hush :: Either a b -> Maybe b -hush (Left _) = Nothing -hush (Right x) = Just x - -fromJustMPI :: Maybe OpenPGP.MPI -> Integer -fromJustMPI (Just (OpenPGP.MPI x)) = x -fromJustMPI _ = error "Not a Just MPI, Data.OpenPGP.CryptoAPI" +import Data.OpenPGP.Util.Base -find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet -find_key = OpenPGP.find_key fingerprint - -integerBytesize :: Integer -> Int -integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2 - dsaKey :: OpenPGP.Packet -> Vincent.DSA.PublicKey dsaKey k = Vincent.DSA.PublicKey (Vincent.DSA.Params (keyParam 'p' k) (keyParam 'g' k) (keyParam 'q' k)) (keyParam 'y' k) -rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey -rsaKey k = - Vincent.RSA.PublicKey (integerBytesize n) n (keyParam 'e' k) - where - n = keyParam 'n' k - - -keyParam :: Char -> OpenPGP.Packet -> Integer -keyParam c k = fromJustMPI $ lookup c (OpenPGP.key k) - -- | Verify a message signature verify :: @@ -100,16 +49,3 @@ verifyOne keys sig over = fmap (const sig) $ maybeKey >>= verification >>= guard hash_algo = OpenPGP.hash_algorithm sig maybeKey = OpenPGP.signature_issuer sig >>= find_key keys --- http://tools.ietf.org/html/rfc3447#page-43 --- http://tools.ietf.org/html/rfc4880#section-5.2.2 -hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 -hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 -hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 -hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 -hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 -hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 -hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 -hashAlgoDesc _ = - error "Unsupported HashAlgorithm in hashAlgoDesc" - - -- cgit v1.2.3