diff options
Diffstat (limited to 'Data')
-rw-r--r-- | Data/OpenPGP/Util/Base.hs | 67 | ||||
-rw-r--r-- | Data/OpenPGP/Util/DecryptSecretKey.hs | 26 | ||||
-rw-r--r-- | Data/OpenPGP/Util/Sign.hs | 56 | ||||
-rw-r--r-- | Data/OpenPGP/Util/Verify.hs | 66 |
4 files changed, 72 insertions, 143 deletions
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 @@ | |||
1 | module Data.OpenPGP.Util.Base where | ||
2 | |||
3 | import qualified Data.ByteString as BS | ||
4 | import qualified Data.ByteString.Lazy as LZ | ||
5 | import Data.Binary (encode) | ||
6 | |||
7 | import Data.OpenPGP as OpenPGP | ||
8 | import Crypto.Hash.MD5 as MD5 | ||
9 | import Crypto.Hash.SHA1 as SHA1 | ||
10 | import Crypto.Hash.SHA256 as SHA256 | ||
11 | import Crypto.Hash.SHA384 as SHA384 | ||
12 | import Crypto.Hash.SHA512 as SHA512 | ||
13 | import Crypto.Hash.SHA224 as SHA224 | ||
14 | import Crypto.Hash.RIPEMD160 as RIPEMD160 | ||
15 | import qualified Crypto.PubKey.RSA as Vincent.RSA | ||
16 | import Crypto.PubKey.HashDescr as Vincent | ||
17 | |||
18 | import Data.OpenPGP.Util.Fingerprint (fingerprint) | ||
19 | |||
20 | hashBySymbol OpenPGP.MD5 = MD5.hashlazy | ||
21 | hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy | ||
22 | hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy | ||
23 | hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy | ||
24 | hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy | ||
25 | hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy | ||
26 | hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy | ||
27 | |||
28 | toStrictBS :: LZ.ByteString -> BS.ByteString | ||
29 | toStrictBS = BS.concat . LZ.toChunks | ||
30 | |||
31 | toLazyBS :: BS.ByteString -> LZ.ByteString | ||
32 | toLazyBS = LZ.fromChunks . (:[]) | ||
33 | |||
34 | find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet | ||
35 | find_key = OpenPGP.find_key fingerprint | ||
36 | |||
37 | |||
38 | |||
39 | keyParam :: Char -> OpenPGP.Packet -> Integer | ||
40 | keyParam c k = fromJustMPI $ lookup c (OpenPGP.key k) | ||
41 | where | ||
42 | fromJustMPI :: Maybe OpenPGP.MPI -> Integer | ||
43 | fromJustMPI (Just (OpenPGP.MPI x)) = x | ||
44 | fromJustMPI _ = error "Not a Just MPI, Data.OpenPGP.CryptoAPI" | ||
45 | |||
46 | integerBytesize :: Integer -> Int | ||
47 | integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2 | ||
48 | |||
49 | rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey | ||
50 | rsaKey k = | ||
51 | Vincent.RSA.PublicKey (integerBytesize n) n (keyParam 'e' k) | ||
52 | where | ||
53 | n = keyParam 'n' k | ||
54 | |||
55 | -- http://tools.ietf.org/html/rfc3447#page-43 | ||
56 | -- http://tools.ietf.org/html/rfc4880#section-5.2.2 | ||
57 | hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 | ||
58 | hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 | ||
59 | hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 | ||
60 | hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 | ||
61 | hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 | ||
62 | hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 | ||
63 | hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 | ||
64 | hashAlgoDesc _ = | ||
65 | error "Unsupported HashAlgorithm in hashAlgoDesc" | ||
66 | |||
67 | |||
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 | |||
3 | import qualified Data.OpenPGP as OpenPGP | 3 | import qualified Data.OpenPGP as OpenPGP |
4 | import qualified Data.ByteString as BS | 4 | import qualified Data.ByteString as BS |
5 | import qualified Data.ByteString.Lazy as LZ | 5 | import qualified Data.ByteString.Lazy as LZ |
6 | import Data.Word (Word16,Word8) | 6 | import Data.Word (Word16) |
7 | import Data.Char (toUpper) | ||
8 | import Control.Monad (foldM) | 7 | import Control.Monad (foldM) |
9 | import Numeric (showHex) | ||
10 | import Data.Binary (get,Binary,Get) | 8 | import Data.Binary (get,Binary,Get) |
11 | import Data.Binary.Get (runGetOrFail) | 9 | import Data.Binary.Get (runGetOrFail) |
12 | import qualified Data.Serialize as Serialize | 10 | import qualified Data.Serialize as Serialize |
13 | import Control.Applicative ( (<$>) ) | 11 | import Control.Applicative ( (<$>) ) |
14 | 12 | ||
15 | import Crypto.Hash.MD5 as MD5 | ||
16 | import Crypto.Hash.SHA1 as SHA1 | 13 | import Crypto.Hash.SHA1 as SHA1 |
17 | import Crypto.Hash.SHA256 as SHA256 | ||
18 | import Crypto.Hash.SHA384 as SHA384 | ||
19 | import Crypto.Hash.SHA512 as SHA512 | ||
20 | import Crypto.Hash.SHA224 as SHA224 | ||
21 | import Crypto.Hash.RIPEMD160 as RIPEMD160 | ||
22 | 14 | ||
23 | import qualified Crypto.Cipher.AES as Vincent | 15 | import qualified Crypto.Cipher.AES as Vincent |
24 | import qualified Crypto.Cipher.Blowfish as Vincent | 16 | import qualified Crypto.Cipher.Blowfish as Vincent |
@@ -28,16 +20,9 @@ import qualified Data.Byteable as Vincent | |||
28 | 20 | ||
29 | import Crypto.Cipher.Cast5 (CAST5_128) | 21 | import Crypto.Cipher.Cast5 (CAST5_128) |
30 | import Crypto.Cipher.ThomasToVincent | 22 | import Crypto.Cipher.ThomasToVincent |
23 | import Data.OpenPGP.Util.Base (toStrictBS,toLazyBS,hashBySymbol) | ||
31 | 24 | ||
32 | 25 | ||
33 | hashBySymbol OpenPGP.MD5 = MD5.hashlazy | ||
34 | hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy | ||
35 | hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy | ||
36 | hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy | ||
37 | hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy | ||
38 | hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy | ||
39 | hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy | ||
40 | |||
41 | 26 | ||
42 | 27 | ||
43 | data Enciphered = | 28 | data Enciphered = |
@@ -94,13 +79,6 @@ decryptSecretKey pass k@(OpenPGP.SecretKeyPacket { | |||
94 | 79 | ||
95 | decryptSecretKey _ _ = Nothing | 80 | decryptSecretKey _ _ = Nothing |
96 | 81 | ||
97 | toStrictBS :: LZ.ByteString -> BS.ByteString | ||
98 | toStrictBS = BS.concat . LZ.toChunks | ||
99 | |||
100 | toLazyBS :: BS.ByteString -> LZ.ByteString | ||
101 | toLazyBS = LZ.fromChunks . (:[]) | ||
102 | |||
103 | |||
104 | 82 | ||
105 | string2sdecrypt :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString -> Enciphered -> LZ.ByteString | 83 | string2sdecrypt :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString -> Enciphered -> LZ.ByteString |
106 | string2sdecrypt OpenPGP.AES128 s2k s = withIV $ simpleUnCFB (string2key s2k s :: Vincent.AES128) | 84 | 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 | |||
17 | import qualified Crypto.PubKey.DSA as Vincent.DSA | 17 | import qualified Crypto.PubKey.DSA as Vincent.DSA |
18 | import qualified Crypto.PubKey.RSA as Vincent.RSA | 18 | import qualified Crypto.PubKey.RSA as Vincent.RSA |
19 | import qualified Crypto.PubKey.RSA.PKCS15 as Vincent.RSA | 19 | import qualified Crypto.PubKey.RSA.PKCS15 as Vincent.RSA |
20 | import Crypto.PubKey.HashDescr as Vincent | 20 | |
21 | 21 | import Data.OpenPGP.Util.Base | |
22 | import Crypto.Hash.MD5 as MD5 | ||
23 | import Crypto.Hash.SHA1 as SHA1 | ||
24 | import Crypto.Hash.SHA256 as SHA256 | ||
25 | import Crypto.Hash.SHA384 as SHA384 | ||
26 | import Crypto.Hash.SHA512 as SHA512 | ||
27 | import Crypto.Hash.SHA224 as SHA224 | ||
28 | import Crypto.Hash.RIPEMD160 as RIPEMD160 | ||
29 | |||
30 | hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 | ||
31 | hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 | ||
32 | hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 | ||
33 | hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 | ||
34 | hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 | ||
35 | hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 | ||
36 | hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 | ||
37 | hashAlgoDesc _ = | ||
38 | error "Unsupported HashAlgorithm in hashAlgoDesc" | ||
39 | |||
40 | find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet | ||
41 | find_key = OpenPGP.find_key fingerprint | ||
42 | 22 | ||
43 | 23 | ||
44 | privateDSAkey :: OpenPGP.Packet -> Vincent.DSA.PrivateKey | 24 | privateDSAkey :: OpenPGP.Packet -> Vincent.DSA.PrivateKey |
@@ -58,38 +38,6 @@ privateRSAkey k = | |||
58 | q = keyParam 'q' k | 38 | q = keyParam 'q' k |
59 | pubkey = rsaKey k | 39 | pubkey = rsaKey k |
60 | 40 | ||
61 | rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey | ||
62 | rsaKey k = | ||
63 | Vincent.RSA.PublicKey (integerBytesize n) n (keyParam 'e' k) | ||
64 | where | ||
65 | n = keyParam 'n' k | ||
66 | |||
67 | integerBytesize :: Integer -> Int | ||
68 | integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2 | ||
69 | |||
70 | |||
71 | toStrictBS :: LZ.ByteString -> BS.ByteString | ||
72 | toStrictBS = BS.concat . LZ.toChunks | ||
73 | |||
74 | toLazyBS :: BS.ByteString -> LZ.ByteString | ||
75 | toLazyBS = LZ.fromChunks . (:[]) | ||
76 | |||
77 | |||
78 | keyParam :: Char -> OpenPGP.Packet -> Integer | ||
79 | keyParam c k = fromJustMPI $ lookup c (OpenPGP.key k) | ||
80 | fromJustMPI :: Maybe OpenPGP.MPI -> Integer | ||
81 | fromJustMPI (Just (OpenPGP.MPI x)) = x | ||
82 | fromJustMPI _ = error "Not a Just MPI, Data.OpenPGP.CryptoAPI" | ||
83 | |||
84 | hashBySymbol OpenPGP.MD5 = MD5.hashlazy | ||
85 | hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy | ||
86 | hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy | ||
87 | hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy | ||
88 | hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy | ||
89 | hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy | ||
90 | hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy | ||
91 | |||
92 | |||
93 | 41 | ||
94 | 42 | ||
95 | -- | Make a signature | 43 | -- | 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) | |||
6 | import Control.Monad | 6 | import Control.Monad |
7 | import qualified Data.ByteString as BS | 7 | import qualified Data.ByteString as BS |
8 | import qualified Data.ByteString.Lazy as LZ | 8 | import qualified Data.ByteString.Lazy as LZ |
9 | import Data.Monoid ( (<>) ) | ||
10 | |||
11 | import Data.OpenPGP.Util.Fingerprint (fingerprint) | ||
12 | 9 | ||
13 | import qualified Crypto.PubKey.DSA as Vincent.DSA | 10 | import qualified Crypto.PubKey.DSA as Vincent.DSA |
14 | import qualified Crypto.PubKey.RSA as Vincent.RSA | ||
15 | import qualified Crypto.PubKey.RSA.PKCS15 as Vincent.RSA | 11 | import qualified Crypto.PubKey.RSA.PKCS15 as Vincent.RSA |
16 | import Crypto.PubKey.HashDescr as Vincent | ||
17 | |||
18 | import Crypto.Hash.MD5 as MD5 | ||
19 | import Crypto.Hash.SHA1 as SHA1 | ||
20 | import Crypto.Hash.SHA256 as SHA256 | ||
21 | import Crypto.Hash.SHA384 as SHA384 | ||
22 | import Crypto.Hash.SHA512 as SHA512 | ||
23 | import Crypto.Hash.SHA224 as SHA224 | ||
24 | import Crypto.Hash.RIPEMD160 as RIPEMD160 | ||
25 | |||
26 | hashBySymbol OpenPGP.MD5 = MD5.hashlazy | ||
27 | hashBySymbol OpenPGP.SHA1 = SHA1.hashlazy | ||
28 | hashBySymbol OpenPGP.SHA256 = SHA256.hashlazy | ||
29 | hashBySymbol OpenPGP.SHA384 = SHA384.hashlazy | ||
30 | hashBySymbol OpenPGP.SHA512 = SHA512.hashlazy | ||
31 | hashBySymbol OpenPGP.SHA224 = SHA224.hashlazy | ||
32 | hashBySymbol OpenPGP.RIPEMD160 = RIPEMD160.hashlazy | ||
33 | |||
34 | |||
35 | toStrictBS :: LZ.ByteString -> BS.ByteString | ||
36 | toStrictBS = BS.concat . LZ.toChunks | ||
37 | |||
38 | toLazyBS :: BS.ByteString -> LZ.ByteString | ||
39 | toLazyBS = LZ.fromChunks . (:[]) | ||
40 | |||
41 | hush :: Either a b -> Maybe b | ||
42 | hush (Left _) = Nothing | ||
43 | hush (Right x) = Just x | ||
44 | |||
45 | fromJustMPI :: Maybe OpenPGP.MPI -> Integer | ||
46 | fromJustMPI (Just (OpenPGP.MPI x)) = x | ||
47 | fromJustMPI _ = error "Not a Just MPI, Data.OpenPGP.CryptoAPI" | ||
48 | 12 | ||
13 | import Data.OpenPGP.Util.Base | ||
49 | 14 | ||
50 | 15 | ||
51 | find_key :: OpenPGP.Message -> String -> Maybe OpenPGP.Packet | ||
52 | find_key = OpenPGP.find_key fingerprint | ||
53 | |||
54 | integerBytesize :: Integer -> Int | ||
55 | integerBytesize i = fromIntegral $ LZ.length (encode (OpenPGP.MPI i)) - 2 | ||
56 | |||
57 | dsaKey :: OpenPGP.Packet -> Vincent.DSA.PublicKey | 16 | dsaKey :: OpenPGP.Packet -> Vincent.DSA.PublicKey |
58 | dsaKey k = Vincent.DSA.PublicKey | 17 | dsaKey k = Vincent.DSA.PublicKey |
59 | (Vincent.DSA.Params (keyParam 'p' k) (keyParam 'g' k) (keyParam 'q' k)) | 18 | (Vincent.DSA.Params (keyParam 'p' k) (keyParam 'g' k) (keyParam 'q' k)) |
60 | (keyParam 'y' k) | 19 | (keyParam 'y' k) |
61 | 20 | ||
62 | rsaKey :: OpenPGP.Packet -> Vincent.RSA.PublicKey | ||
63 | rsaKey k = | ||
64 | Vincent.RSA.PublicKey (integerBytesize n) n (keyParam 'e' k) | ||
65 | where | ||
66 | n = keyParam 'n' k | ||
67 | |||
68 | |||
69 | keyParam :: Char -> OpenPGP.Packet -> Integer | ||
70 | keyParam c k = fromJustMPI $ lookup c (OpenPGP.key k) | ||
71 | |||
72 | 21 | ||
73 | -- | Verify a message signature | 22 | -- | Verify a message signature |
74 | verify :: | 23 | verify :: |
@@ -100,16 +49,3 @@ verifyOne keys sig over = fmap (const sig) $ maybeKey >>= verification >>= guard | |||
100 | hash_algo = OpenPGP.hash_algorithm sig | 49 | hash_algo = OpenPGP.hash_algorithm sig |
101 | maybeKey = OpenPGP.signature_issuer sig >>= find_key keys | 50 | maybeKey = OpenPGP.signature_issuer sig >>= find_key keys |
102 | 51 | ||
103 | -- http://tools.ietf.org/html/rfc3447#page-43 | ||
104 | -- http://tools.ietf.org/html/rfc4880#section-5.2.2 | ||
105 | hashAlgoDesc OpenPGP.MD5 = Vincent.hashDescrMD5 | ||
106 | hashAlgoDesc OpenPGP.SHA1 = Vincent.hashDescrSHA1 | ||
107 | hashAlgoDesc OpenPGP.RIPEMD160 = Vincent.hashDescrRIPEMD160 | ||
108 | hashAlgoDesc OpenPGP.SHA256 = Vincent.hashDescrSHA256 | ||
109 | hashAlgoDesc OpenPGP.SHA384 = Vincent.hashDescrSHA384 | ||
110 | hashAlgoDesc OpenPGP.SHA512 = Vincent.hashDescrSHA512 | ||
111 | hashAlgoDesc OpenPGP.SHA224 = Vincent.hashDescrSHA224 | ||
112 | hashAlgoDesc _ = | ||
113 | error "Unsupported HashAlgorithm in hashAlgoDesc" | ||
114 | |||
115 | |||