diff options
author | joe <joe@jerkface.net> | 2016-08-26 03:10:40 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-07-01 09:19:55 -0400 |
commit | 39d8a08aad1d2dd48b807ab867aa17475e4278c4 (patch) | |
tree | bd3146dacd3d57b123dd172e4901c61014ea3db9 | |
parent | a90b1f609d8a559694ad31ea0b28ec6309a8b661 (diff) |
Completed cryptonite support.
-rw-r--r-- | Crypto/Cipher/Cast5.hs | 2 | ||||
-rw-r--r-- | Crypto/Cipher/ThomasToVincent.hs | 25 | ||||
-rw-r--r-- | Data/OpenPGP/Util/DecryptSecretKey.hs | 14 |
3 files changed, 30 insertions, 11 deletions
diff --git a/Crypto/Cipher/Cast5.hs b/Crypto/Cipher/Cast5.hs index dfd30c7..da9d109 100644 --- a/Crypto/Cipher/Cast5.hs +++ b/Crypto/Cipher/Cast5.hs | |||
@@ -83,7 +83,7 @@ instance Cast5Bits size => Serialize (Cast5 size) where | |||
83 | 83 | ||
84 | -- instance Cast5Bits size => BlockCipher (Cast5 size) where | 84 | -- instance Cast5Bits size => BlockCipher (Cast5 size) where |
85 | blockSize :: forall size. Cast5Bits size => Tagged (Cast5 size) Int | 85 | blockSize :: forall size. Cast5Bits size => Tagged (Cast5 size) Int |
86 | blockSize = Tagged 64 | 86 | blockSize = Tagged 64 -- bits |
87 | 87 | ||
88 | encryptBlock :: forall size. Cast5Bits size => Cast5 size -> S.ByteString -> S.ByteString | 88 | encryptBlock :: forall size. Cast5Bits size => Cast5 size -> S.ByteString -> S.ByteString |
89 | encryptBlock (Cast5 subkeys fs _ _ key) = | 89 | encryptBlock (Cast5 subkeys fs _ _ key) = |
diff --git a/Crypto/Cipher/ThomasToVincent.hs b/Crypto/Cipher/ThomasToVincent.hs index 5a68cf3..cf5cdee 100644 --- a/Crypto/Cipher/ThomasToVincent.hs +++ b/Crypto/Cipher/ThomasToVincent.hs | |||
@@ -5,7 +5,12 @@ module Crypto.Cipher.ThomasToVincent where | |||
5 | import qualified Data.ByteString as S | 5 | import qualified Data.ByteString as S |
6 | import Crypto.Cipher.Types | 6 | import Crypto.Cipher.Types |
7 | import Crypto.Cipher.Cast5 | 7 | import Crypto.Cipher.Cast5 |
8 | #if defined(VERSION_cryptonite) | ||
9 | import qualified Data.ByteArray as Bytes | ||
10 | import Crypto.Error | ||
11 | #else | ||
8 | import Data.Byteable | 12 | import Data.Byteable |
13 | #endif | ||
9 | 14 | ||
10 | import Data.Tagged | 15 | import Data.Tagged |
11 | {- | 16 | {- |
@@ -19,9 +24,14 @@ endif | |||
19 | type ThomasToVincent b = b | 24 | type ThomasToVincent b = b |
20 | 25 | ||
21 | instance Cast5Bits size => Cipher (Cast5 size) where | 26 | instance Cast5Bits size => Cipher (Cast5 size) where |
22 | cipherName _ = "CAST-5" | 27 | cipherName _ = "CAST-"++show (cast5bits (undefined :: size)) |
28 | #if defined(VERSION_cryptonite) | ||
29 | cipherInit k = CryptoPassed b | ||
30 | where Just b = buildKey (Bytes.convert k) | ||
31 | #else | ||
23 | cipherInit k = b | 32 | cipherInit k = b |
24 | where Just b = buildKey (toBytes k) | 33 | where Just b = buildKey (toBytes k) |
34 | #endif | ||
25 | cipherKeySize _ = KeySizeFixed (bitlen `div` 8) | 35 | cipherKeySize _ = KeySizeFixed (bitlen `div` 8) |
26 | where Tagged bitlen = keyLength :: Tagged (Cast5 size) Int | 36 | where Tagged bitlen = keyLength :: Tagged (Cast5 size) Int |
27 | 37 | ||
@@ -42,14 +52,25 @@ instance Cast5Bits size => BlockCipher (Cast5 size) where | |||
42 | blockSize _ = bitlen `div` 8 | 52 | blockSize _ = bitlen `div` 8 |
43 | where Tagged bitlen = Crypto.Cipher.Cast5.blockSize :: Tagged (Cast5 size) Int | 53 | where Tagged bitlen = Crypto.Cipher.Cast5.blockSize :: Tagged (Cast5 size) Int |
44 | 54 | ||
55 | -- ecbEncrypt :: (BlockCipher cipher, ByteArray ba) => cipher -> ba -> ba | ||
45 | -- modeEcb' :: BlockCipher k => k -> B.ByteString -> B.ByteString | 56 | -- modeEcb' :: BlockCipher k => k -> B.ByteString -> B.ByteString |
46 | ecbEncrypt k msg = | 57 | ecbEncrypt k msg = |
47 | let chunks = chunkFor' k msg | 58 | #if defined(VERSION_cryptonite) |
59 | let chunks = chunkFor' k $ Bytes.convert msg | ||
60 | in Bytes.convert $ S.concat $ map (encryptBlock k) chunks | ||
61 | #else | ||
62 | let chunks = chunkFor' k $ msg | ||
48 | in S.concat $ map (encryptBlock k) chunks | 63 | in S.concat $ map (encryptBlock k) chunks |
64 | #endif | ||
49 | 65 | ||
50 | ecbDecrypt k ct = | 66 | ecbDecrypt k ct = |
67 | #if defined(VERSION_cryptonite) | ||
68 | let chunks = chunkFor' k $ Bytes.convert ct | ||
69 | in Bytes.convert $ S.concat $ map (decryptBlock k) chunks | ||
70 | #else | ||
51 | let chunks = chunkFor' k ct | 71 | let chunks = chunkFor' k ct |
52 | in S.concat $ map (decryptBlock k) chunks | 72 | in S.concat $ map (decryptBlock k) chunks |
73 | #endif | ||
53 | 74 | ||
54 | 75 | ||
55 | {- | 76 | {- |
diff --git a/Data/OpenPGP/Util/DecryptSecretKey.hs b/Data/OpenPGP/Util/DecryptSecretKey.hs index 01728d3..1c519ae 100644 --- a/Data/OpenPGP/Util/DecryptSecretKey.hs +++ b/Data/OpenPGP/Util/DecryptSecretKey.hs | |||
@@ -47,13 +47,13 @@ data Enciphered = | |||
47 | EncipheredWithIV !LZ.ByteString -- initial vector is appended to front of ByteString | 47 | EncipheredWithIV !LZ.ByteString -- initial vector is appended to front of ByteString |
48 | | EncipheredZeroIV !LZ.ByteString -- initial vector is zero, ByteString contains only the block | 48 | | EncipheredZeroIV !LZ.ByteString -- initial vector is zero, ByteString contains only the block |
49 | 49 | ||
50 | withIV :: (Vincent.BlockCipher k) => (Vincent.IV k -> LZ.ByteString -> LZ.ByteString) -> Enciphered -> LZ.ByteString | 50 | withIV :: forall k. (Vincent.BlockCipher k) => (Vincent.IV k -> LZ.ByteString -> LZ.ByteString) -> Enciphered -> LZ.ByteString |
51 | withIV f (EncipheredWithIV s) = f iv bs | 51 | withIV f (EncipheredWithIV s) = f iv bs |
52 | where | 52 | where |
53 | Just iv = Vincent.makeIV (toStrictBS ivbs) | 53 | Just iv = Vincent.makeIV (toStrictBS ivbs) |
54 | (ivbs,bs) = LZ.splitAt (fromIntegral ivlen) s | 54 | (ivbs,bs) = LZ.splitAt (fromIntegral ivlen) s |
55 | #if defined(VERSION_cryptonite) | 55 | #if defined(VERSION_cryptonite) |
56 | ivlen = Bytes.length iv | 56 | ivlen = Bytes.length (Vincent.nullIV :: Vincent.IV k) |
57 | #else | 57 | #else |
58 | ivlen = Vincent.byteableLength z | 58 | ivlen = Vincent.byteableLength z |
59 | _ = Vincent.constEqBytes z iv | 59 | _ = Vincent.constEqBytes z iv |
@@ -126,8 +126,7 @@ withS2K codec OpenPGP.AES128 s2k s = withIV $ codec (string2key s2k s :: Vince | |||
126 | withS2K codec OpenPGP.AES192 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES192) | 126 | withS2K codec OpenPGP.AES192 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES192) |
127 | withS2K codec OpenPGP.AES256 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES256) | 127 | withS2K codec OpenPGP.AES256 s2k s = withIV $ codec (string2key s2k s :: Vincent.AES256) |
128 | withS2K codec OpenPGP.Blowfish s2k s = withIV $ codec (string2key s2k s :: Vincent.Blowfish128) | 128 | withS2K codec OpenPGP.Blowfish s2k s = withIV $ codec (string2key s2k s :: Vincent.Blowfish128) |
129 | -- TODO: cast5 support | 129 | withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128) |
130 | -- withS2K codec OpenPGP.CAST5 s2k s = withIV $ codec (string2key s2k s :: ThomasToVincent CAST5_128) | ||
131 | withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K" | 130 | withS2K codec algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.withS2K" |
132 | 131 | ||
133 | withS2K' :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString | 132 | withS2K' :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString |
@@ -136,21 +135,20 @@ withS2K' OpenPGP.AES128 s2k s f = f (string2key s2k s :: Vincent.AES128) | |||
136 | withS2K' OpenPGP.AES192 s2k s f = f (string2key s2k s :: Vincent.AES192) | 135 | withS2K' OpenPGP.AES192 s2k s f = f (string2key s2k s :: Vincent.AES192) |
137 | withS2K' OpenPGP.AES256 s2k s f = f (string2key s2k s :: Vincent.AES256) | 136 | withS2K' OpenPGP.AES256 s2k s f = f (string2key s2k s :: Vincent.AES256) |
138 | withS2K' OpenPGP.Blowfish s2k s f = f (string2key s2k s :: Vincent.Blowfish128) | 137 | withS2K' OpenPGP.Blowfish s2k s f = f (string2key s2k s :: Vincent.Blowfish128) |
139 | -- TODO: cast5 support | 138 | withS2K' OpenPGP.CAST5 s2k s f = f (string2key s2k s :: ThomasToVincent CAST5_128) |
140 | -- withS2K' OpenPGP.CAST5 s2k s f = f (string2key s2k s :: ThomasToVincent CAST5_128) | ||
141 | 139 | ||
142 | -- decryption codec for withS2K | 140 | -- decryption codec for withS2K |
143 | simpleUnCFB :: (Vincent.BlockCipher k) => k -> Vincent.IV k -> LZ.ByteString -> LZ.ByteString | 141 | simpleUnCFB :: (Vincent.BlockCipher k) => k -> Vincent.IV k -> LZ.ByteString -> LZ.ByteString |
144 | simpleUnCFB k iv = padThenUnpad k (toLazyBS . Vincent.cfbDecrypt k iv . toStrictBS) | 142 | simpleUnCFB k iv = padThenUnpad k (toLazyBS . Vincent.cfbDecrypt k iv . toStrictBS) |
145 | 143 | ||
146 | simpleCFB :: (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g) | 144 | simpleCFB :: forall k g. (Vincent.BlockCipher k, RG g) => g -> k -> LZ.ByteString -> (LZ.ByteString, g) |
147 | simpleCFB g k bs = ( padThenUnpad k (LZ.fromChunks . (ivbs:) . (:[]) . Vincent.cfbEncrypt k iv . toStrictBS) bs | 145 | simpleCFB g k bs = ( padThenUnpad k (LZ.fromChunks . (ivbs:) . (:[]) . Vincent.cfbEncrypt k iv . toStrictBS) bs |
148 | , g' ) | 146 | , g' ) |
149 | where | 147 | where |
150 | Just iv = Vincent.makeIV ivbs | 148 | Just iv = Vincent.makeIV ivbs |
151 | #if defined(VERSION_cryptonite) | 149 | #if defined(VERSION_cryptonite) |
152 | (ivbs,g') = Vincent.randomBytesGenerate ivlen g | 150 | (ivbs,g') = Vincent.randomBytesGenerate ivlen g |
153 | ivlen = Bytes.length iv | 151 | ivlen = Bytes.length (Vincent.nullIV :: Vincent.IV k) |
154 | #else | 152 | #else |
155 | z = Vincent.nullIV | 153 | z = Vincent.nullIV |
156 | (ivbs,g') = Vincent.cprgGenerate ivlen g | 154 | (ivbs,g') = Vincent.cprgGenerate ivlen g |