diff options
Diffstat (limited to 'Data/OpenPGP/Util/Base.hs')
-rw-r--r-- | Data/OpenPGP/Util/Base.hs | 65 |
1 files changed, 64 insertions, 1 deletions
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 @@ | |||
1 | {-# LANGUAGE CPP #-} | ||
2 | {-# LANGUAGE ExistentialQuantification #-} | ||
3 | {-# LANGUAGE ConstraintKinds #-} | ||
1 | module Data.OpenPGP.Util.Base where | 4 | module Data.OpenPGP.Util.Base where |
2 | 5 | ||
3 | import qualified Data.ByteString as BS | 6 | import qualified Data.ByteString as BS |
4 | import qualified Data.ByteString.Lazy as LZ | 7 | import qualified Data.ByteString.Lazy as LZ |
5 | import Data.Binary (encode) | 8 | import Data.Binary (encode) |
9 | import Data.Word | ||
6 | 10 | ||
7 | import Data.OpenPGP as OpenPGP | 11 | import Data.OpenPGP as OpenPGP |
12 | #if defined(VERSION_cryptonite) | ||
13 | import Crypto.Hash | ||
14 | import Crypto.Hash.Algorithms as Vincent | ||
15 | import qualified Crypto.PubKey.ECC.Types as Vincent.ECDSA | ||
16 | import qualified Data.ByteArray as Bytes | ||
17 | import qualified Crypto.PubKey.ECC.ECDSA as Vincent.ECDSA | ||
18 | import Crypto.PubKey.RSA.PKCS15 (HashAlgorithmASN1) | ||
19 | #else | ||
8 | import Crypto.Hash.MD5 as MD5 | 20 | import Crypto.Hash.MD5 as MD5 |
9 | import Crypto.Hash.SHA1 as SHA1 | 21 | import Crypto.Hash.SHA1 as SHA1 |
10 | import Crypto.Hash.SHA256 as SHA256 | 22 | import Crypto.Hash.SHA256 as SHA256 |
@@ -12,13 +24,34 @@ import Crypto.Hash.SHA384 as SHA384 | |||
12 | import Crypto.Hash.SHA512 as SHA512 | 24 | import Crypto.Hash.SHA512 as SHA512 |
13 | import Crypto.Hash.SHA224 as SHA224 | 25 | import Crypto.Hash.SHA224 as SHA224 |
14 | import Crypto.Hash.RIPEMD160 as RIPEMD160 | 26 | import Crypto.Hash.RIPEMD160 as RIPEMD160 |
15 | import qualified Crypto.PubKey.RSA as Vincent.RSA | ||
16 | import Crypto.PubKey.HashDescr as Vincent | 27 | import Crypto.PubKey.HashDescr as Vincent |
17 | import qualified Crypto.Types.PubKey.ECC as Vincent.ECDSA | 28 | import qualified Crypto.Types.PubKey.ECC as Vincent.ECDSA |
18 | import qualified Crypto.Types.PubKey.ECDSA as Vincent.ECDSA | 29 | import qualified Crypto.Types.PubKey.ECDSA as Vincent.ECDSA |
30 | #endif | ||
31 | import qualified Crypto.PubKey.RSA as Vincent.RSA | ||
32 | import qualified Crypto.Random as Vincent | ||
19 | 33 | ||
20 | import Data.OpenPGP.Util.Fingerprint (fingerprint) | 34 | import Data.OpenPGP.Util.Fingerprint (fingerprint) |
21 | 35 | ||
36 | #if defined(VERSION_cryptonite) | ||
37 | import Data.Hourglass | ||
38 | import System.Hourglass | ||
39 | import Control.Arrow (second) | ||
40 | import Data.Binary (decode) | ||
41 | #else | ||
42 | import qualified Data.Time.Clock.POSIX | ||
43 | #endif | ||
44 | |||
45 | hashBySymbol :: OpenPGP.HashAlgorithm -> LZ.ByteString -> BS.ByteString | ||
46 | #if defined(VERSION_cryptonite) | ||
47 | hashBySymbol OpenPGP.MD5 x = Bytes.convert (hashlazy x :: Digest MD5) | ||
48 | hashBySymbol OpenPGP.SHA1 x = Bytes.convert (hashlazy x :: Digest SHA1) | ||
49 | hashBySymbol OpenPGP.SHA256 x = Bytes.convert (hashlazy x :: Digest SHA256) | ||
50 | hashBySymbol OpenPGP.SHA384 x = Bytes.convert (hashlazy x :: Digest SHA384) | ||
51 | hashBySymbol OpenPGP.SHA512 x = Bytes.convert (hashlazy x :: Digest SHA512) | ||
52 | hashBySymbol OpenPGP.SHA224 x = Bytes.convert (hashlazy x :: Digest SHA224) | ||
53 | hashBySymbol OpenPGP.RIPEMD160 x = Bytes.convert (hashlazy x :: Digest RIPEMD160) | ||
54 | #else | ||
22 | hashBySymbol OpenPGP.MD5 = MD5.hashlazy | 55 | hashBySymbol OpenPGP.MD5 = MD5.hashlazy |
23 | hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy | 56 | hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy |
24 | hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy | 57 | hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy |
@@ -26,6 +59,7 @@ hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy | |||
26 | hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy | 59 | hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy |
27 | hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy | 60 | hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy |
28 | hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy | 61 | hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy |
62 | #endif | ||
29 | 63 | ||
30 | curveFromOID :: Integer -> Vincent.ECDSA.Curve | 64 | curveFromOID :: Integer -> Vincent.ECDSA.Curve |
31 | curveFromOID 0x2a8648ce3d030107 = Vincent.ECDSA.getCurveByName Vincent.ECDSA.SEC_p256r1 -- NIST P-256 | 65 | curveFromOID 0x2a8648ce3d030107 = Vincent.ECDSA.getCurveByName Vincent.ECDSA.SEC_p256r1 -- NIST P-256 |
@@ -74,6 +108,17 @@ rsaKey k = | |||
74 | 108 | ||
75 | -- http://tools.ietf.org/html/rfc3447#page-43 | 109 | -- http://tools.ietf.org/html/rfc3447#page-43 |
76 | -- http://tools.ietf.org/html/rfc4880#section-5.2.2 | 110 | -- http://tools.ietf.org/html/rfc4880#section-5.2.2 |
111 | #if defined(VERSION_cryptonite) | ||
112 | data HashDescr = forall hashAlg. HashAlgorithmASN1 hashAlg => HashDescr hashAlg | ||
113 | |||
114 | hashAlgoDesc OpenPGP.MD5 = HashDescr Vincent.MD5 | ||
115 | hashAlgoDesc OpenPGP.SHA1 = HashDescr Vincent.SHA1 | ||
116 | hashAlgoDesc OpenPGP.RIPEMD160 = HashDescr Vincent.RIPEMD160 | ||
117 | hashAlgoDesc OpenPGP.SHA256 = HashDescr Vincent.SHA256 | ||
118 | hashAlgoDesc OpenPGP.SHA384 = HashDescr Vincent.SHA384 | ||
119 | hashAlgoDesc OpenPGP.SHA512 = HashDescr Vincent.SHA512 | ||
120 | hashAlgoDesc OpenPGP.SHA224 = HashDescr Vincent.SHA224 | ||
121 | #else | ||
77 | hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 | 122 | hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 |
78 | hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 | 123 | hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 |
79 | hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 | 124 | hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 |
@@ -81,7 +126,25 @@ hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 | |||
81 | hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 | 126 | hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 |
82 | hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 | 127 | hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 |
83 | hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 | 128 | hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 |
129 | #endif | ||
84 | hashAlgoDesc _ = | 130 | hashAlgoDesc _ = |
85 | error "Unsupported HashAlgorithm in hashAlgoDesc" | 131 | error "Unsupported HashAlgorithm in hashAlgoDesc" |
86 | 132 | ||
87 | 133 | ||
134 | currentTime :: Integral b => IO b | ||
135 | #if defined(VERSION_hourglass) | ||
136 | currentTime = fromIntegral . toSeconds <$> dateCurrent | ||
137 | where | ||
138 | toSeconds vincentTime = t | ||
139 | where (Elapsed (Seconds t)) = timeGetElapsed vincentTime | ||
140 | #else | ||
141 | currentTime = floor <$> Data.Time.Clock.POSIX.getPOSIXTime | ||
142 | #endif | ||
143 | |||
144 | |||
145 | #if defined(VERSION_cryptonite) | ||
146 | type RG = Vincent.DRG | ||
147 | #else | ||
148 | type RG = Vincent.CPRG | ||
149 | #endif | ||
150 | |||