From 7909c13a18e260af68819be7444829f799cb7c38 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 26 Aug 2016 01:21:22 -0400 Subject: WIP: support for cryptonite. --- Data/OpenPGP/Util/Base.hs | 65 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'Data/OpenPGP/Util/Base.hs') diff --git a/Data/OpenPGP/Util/Base.hs b/Data/OpenPGP/Util/Base.hs index ed0e32c..c1088d8 100644 --- a/Data/OpenPGP/Util/Base.hs +++ b/Data/OpenPGP/Util/Base.hs @@ -1,10 +1,22 @@ +{-# LANGUAGE CPP #-} +{-# LANGUAGE ExistentialQuantification #-} +{-# LANGUAGE ConstraintKinds #-} 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.Word import Data.OpenPGP as OpenPGP +#if defined(VERSION_cryptonite) +import Crypto.Hash +import Crypto.Hash.Algorithms as Vincent +import qualified Crypto.PubKey.ECC.Types as Vincent.ECDSA +import qualified Data.ByteArray as Bytes +import qualified Crypto.PubKey.ECC.ECDSA as Vincent.ECDSA +import Crypto.PubKey.RSA.PKCS15 (HashAlgorithmASN1) +#else import Crypto.Hash.MD5 as MD5 import Crypto.Hash.SHA1 as SHA1 import Crypto.Hash.SHA256 as SHA256 @@ -12,13 +24,34 @@ 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 qualified Crypto.Types.PubKey.ECC as Vincent.ECDSA import qualified Crypto.Types.PubKey.ECDSA as Vincent.ECDSA +#endif +import qualified Crypto.PubKey.RSA as Vincent.RSA +import qualified Crypto.Random as Vincent import Data.OpenPGP.Util.Fingerprint (fingerprint) +#if defined(VERSION_cryptonite) +import Data.Hourglass +import System.Hourglass +import Control.Arrow (second) +import Data.Binary (decode) +#else +import qualified Data.Time.Clock.POSIX +#endif + +hashBySymbol :: OpenPGP.HashAlgorithm -> LZ.ByteString -> BS.ByteString +#if defined(VERSION_cryptonite) +hashBySymbol OpenPGP.MD5 x = Bytes.convert (hashlazy x :: Digest MD5) +hashBySymbol OpenPGP.SHA1 x = Bytes.convert (hashlazy x :: Digest SHA1) +hashBySymbol OpenPGP.SHA256 x = Bytes.convert (hashlazy x :: Digest SHA256) +hashBySymbol OpenPGP.SHA384 x = Bytes.convert (hashlazy x :: Digest SHA384) +hashBySymbol OpenPGP.SHA512 x = Bytes.convert (hashlazy x :: Digest SHA512) +hashBySymbol OpenPGP.SHA224 x = Bytes.convert (hashlazy x :: Digest SHA224) +hashBySymbol OpenPGP.RIPEMD160 x = Bytes.convert (hashlazy x :: Digest RIPEMD160) +#else hashBySymbol OpenPGP.MD5 = MD5.hashlazy hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy @@ -26,6 +59,7 @@ hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy +#endif curveFromOID :: Integer -> Vincent.ECDSA.Curve curveFromOID 0x2a8648ce3d030107 = Vincent.ECDSA.getCurveByName Vincent.ECDSA.SEC_p256r1 -- NIST P-256 @@ -74,6 +108,17 @@ rsaKey k = -- http://tools.ietf.org/html/rfc3447#page-43 -- http://tools.ietf.org/html/rfc4880#section-5.2.2 +#if defined(VERSION_cryptonite) +data HashDescr = forall hashAlg. HashAlgorithmASN1 hashAlg => HashDescr hashAlg + +hashAlgoDesc OpenPGP.MD5 = HashDescr Vincent.MD5 +hashAlgoDesc OpenPGP.SHA1 = HashDescr Vincent.SHA1 +hashAlgoDesc OpenPGP.RIPEMD160 = HashDescr Vincent.RIPEMD160 +hashAlgoDesc OpenPGP.SHA256 = HashDescr Vincent.SHA256 +hashAlgoDesc OpenPGP.SHA384 = HashDescr Vincent.SHA384 +hashAlgoDesc OpenPGP.SHA512 = HashDescr Vincent.SHA512 +hashAlgoDesc OpenPGP.SHA224 = HashDescr Vincent.SHA224 +#else hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 @@ -81,7 +126,25 @@ hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 +#endif hashAlgoDesc _ = error "Unsupported HashAlgorithm in hashAlgoDesc" +currentTime :: Integral b => IO b +#if defined(VERSION_hourglass) +currentTime = fromIntegral . toSeconds <$> dateCurrent + where + toSeconds vincentTime = t + where (Elapsed (Seconds t)) = timeGetElapsed vincentTime +#else +currentTime = floor <$> Data.Time.Clock.POSIX.getPOSIXTime +#endif + + +#if defined(VERSION_cryptonite) +type RG = Vincent.DRG +#else +type RG = Vincent.CPRG +#endif + -- cgit v1.2.3