diff options
author | joe <joe@jerkface.net> | 2016-08-26 02:40:02 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-07-01 09:19:55 -0400 |
commit | a90b1f609d8a559694ad31ea0b28ec6309a8b661 (patch) | |
tree | 20a12fad58c51d0f75e5305636ae0050d2dc6e72 /Crypto | |
parent | f1a0772d5fb17f4460bdf996e06bb0983cf7a530 (diff) |
Drop dependency on Thamas's crypto-api package.
Diffstat (limited to 'Crypto')
-rw-r--r-- | Crypto/Cipher/Cast5.hs | 50 | ||||
-rw-r--r-- | Crypto/Cipher/ThomasToVincent.hs | 52 |
2 files changed, 77 insertions, 25 deletions
diff --git a/Crypto/Cipher/Cast5.hs b/Crypto/Cipher/Cast5.hs index fb5db20..dfd30c7 100644 --- a/Crypto/Cipher/Cast5.hs +++ b/Crypto/Cipher/Cast5.hs | |||
@@ -14,7 +14,7 @@ import qualified Data.Vector.Unboxed as Vector | |||
14 | import Data.Vector.Unboxed (Vector, (//)) | 14 | import Data.Vector.Unboxed (Vector, (//)) |
15 | import Data.Word | 15 | import Data.Word |
16 | import Data.Tuple | 16 | import Data.Tuple |
17 | import Crypto.Classes | 17 | -- import Crypto.Classes -- Thomas' crypto-api incompatible with Vincent's crypto-random. |
18 | import Data.Serialize | 18 | import Data.Serialize |
19 | import qualified Data.ByteString as S | 19 | import qualified Data.ByteString as S |
20 | import Data.Tagged (Tagged(..)) | 20 | import Data.Tagged (Tagged(..)) |
@@ -81,25 +81,32 @@ instance Cast5Bits size => Serialize (Cast5 size) where | |||
81 | let Just key = buildKey bs | 81 | let Just key = buildKey bs |
82 | return key | 82 | return key |
83 | 83 | ||
84 | instance Cast5Bits size => BlockCipher (Cast5 size) where | 84 | -- instance Cast5Bits size => BlockCipher (Cast5 size) where |
85 | blockSize = Tagged 64 | 85 | blockSize :: forall size. Cast5Bits size => Tagged (Cast5 size) Int |
86 | encryptBlock (Cast5 subkeys fs _ _ key) = | 86 | blockSize = Tagged 64 |
87 | fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair | 87 | |
88 | where | 88 | encryptBlock :: forall size. Cast5Bits size => Cast5 size -> S.ByteString -> S.ByteString |
89 | nrounds = numberOfRounds (cast5bits (undefined::size)) | 89 | encryptBlock (Cast5 subkeys fs _ _ key) = |
90 | decryptBlock (Cast5 _ _ subkeys fs key) = | 90 | fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair |
91 | fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair | 91 | where |
92 | where | 92 | nrounds = numberOfRounds (cast5bits (undefined::size)) |
93 | nrounds = numberOfRounds (cast5bits (undefined::size)) | 93 | decryptBlock :: forall size. Cast5Bits size => Cast5 size -> S.ByteString -> S.ByteString |
94 | buildKey bs = Just (Cast5 subs fs subs' fs' key) | 94 | decryptBlock (Cast5 _ _ subkeys fs key) = |
95 | where | 95 | fromW32Pair . coreCrypto nrounds subkeys fs . toW32Pair |
96 | nrounds = numberOfRounds (cast5bits (undefined::size)) | 96 | where |
97 | key = initKey bs | 97 | nrounds = numberOfRounds (cast5bits (undefined::size)) |
98 | fs = cycle [f1,f2,f3] | 98 | buildKey :: forall size. Cast5Bits size => S.ByteString -> Maybe (Cast5 size) |
99 | subs = subkeys key | 99 | buildKey bs = Just (Cast5 subs fs subs' fs' key) |
100 | fs' = drop (3 - (nrounds `rem` 3)) $ cycle [f3,f2,f1] | 100 | where |
101 | subs' = (reverse . take nrounds $ subs) | 101 | nrounds = numberOfRounds (cast5bits (undefined::size)) |
102 | keyLength = Tagged (cast5bits (undefined::size)) | 102 | key = initKey bs |
103 | fs = cycle [f1,f2,f3] | ||
104 | subs = subkeys key | ||
105 | fs' = drop (3 - (nrounds `rem` 3)) $ cycle [f3,f2,f1] | ||
106 | subs' = (reverse . take nrounds $ subs) | ||
107 | |||
108 | keyLength :: forall size. Cast5Bits size => Tagged (Cast5 size) Int | ||
109 | keyLength = Tagged (cast5bits (undefined::size)) | ||
103 | 110 | ||
104 | 111 | ||
105 | {-# INLINE (^) #-} | 112 | {-# INLINE (^) #-} |
@@ -544,6 +551,8 @@ hasSize :: Cast5 size -> size -> a | |||
544 | hasSize _ _ = undefined | 551 | hasSize _ _ = undefined |
545 | 552 | ||
546 | 553 | ||
554 | {- | ||
555 | |||
547 | data StaticTest size = StaticTest { | 556 | data StaticTest size = StaticTest { |
548 | keysize :: size, | 557 | keysize :: size, |
549 | keybytes :: S.ByteString, | 558 | keybytes :: S.ByteString, |
@@ -673,3 +682,4 @@ main = do | |||
673 | , "expected b = " ++ show (S.unpack expected_b) | 682 | , "expected b = " ++ show (S.unpack expected_b) |
674 | , "computed b = " ++ show (S.unpack result_b) ] | 683 | , "computed b = " ++ show (S.unpack result_b) ] |
675 | return () | 684 | return () |
685 | -} | ||
diff --git a/Crypto/Cipher/ThomasToVincent.hs b/Crypto/Cipher/ThomasToVincent.hs index 11cede3..5a68cf3 100644 --- a/Crypto/Cipher/ThomasToVincent.hs +++ b/Crypto/Cipher/ThomasToVincent.hs | |||
@@ -2,16 +2,57 @@ | |||
2 | {-# LANGUAGE ScopedTypeVariables #-} | 2 | {-# LANGUAGE ScopedTypeVariables #-} |
3 | module Crypto.Cipher.ThomasToVincent where | 3 | module Crypto.Cipher.ThomasToVincent where |
4 | 4 | ||
5 | import qualified Data.ByteString as S | ||
5 | import Crypto.Cipher.Types | 6 | import Crypto.Cipher.Types |
7 | import Crypto.Cipher.Cast5 | ||
6 | import Data.Byteable | 8 | import Data.Byteable |
7 | 9 | ||
8 | import Data.Tagged | 10 | import Data.Tagged |
11 | {- | ||
9 | import qualified Crypto.Classes as Thomas | 12 | import qualified Crypto.Classes as Thomas |
10 | #if ! MIN_VERSION_crypto_api(0,11,0) | 13 | if ! MIN_VERSION_crypto_api(0,11,0) |
11 | import qualified Crypto.Modes as Thomas | 14 | import qualified Crypto.Modes as Thomas |
12 | #endif | 15 | endif |
16 | -} | ||
13 | 17 | ||
14 | 18 | ||
19 | type ThomasToVincent b = b | ||
20 | |||
21 | instance Cast5Bits size => Cipher (Cast5 size) where | ||
22 | cipherName _ = "CAST-5" | ||
23 | cipherInit k = b | ||
24 | where Just b = buildKey (toBytes k) | ||
25 | cipherKeySize _ = KeySizeFixed (bitlen `div` 8) | ||
26 | where Tagged bitlen = keyLength :: Tagged (Cast5 size) Int | ||
27 | |||
28 | |||
29 | -- Break a bytestring into block size chunks. | ||
30 | chunkFor' :: (Cast5Bits size) => Cast5 size -> S.ByteString -> [S.ByteString] | ||
31 | chunkFor' k = go | ||
32 | where | ||
33 | blkSz = (Crypto.Cipher.Cast5.blockSize `for` k) `div` 8 | ||
34 | go bs | S.length bs < blkSz = [] | ||
35 | | otherwise = let (blk,rest) = S.splitAt blkSz bs in blk : go rest | ||
36 | |||
37 | -- |Obtain a tagged value for a particular instantiated type. | ||
38 | for :: Tagged a b -> a -> b | ||
39 | for t _ = unTagged t | ||
40 | |||
41 | instance Cast5Bits size => BlockCipher (Cast5 size) where | ||
42 | blockSize _ = bitlen `div` 8 | ||
43 | where Tagged bitlen = Crypto.Cipher.Cast5.blockSize :: Tagged (Cast5 size) Int | ||
44 | |||
45 | -- modeEcb' :: BlockCipher k => k -> B.ByteString -> B.ByteString | ||
46 | ecbEncrypt k msg = | ||
47 | let chunks = chunkFor' k msg | ||
48 | in S.concat $ map (encryptBlock k) chunks | ||
49 | |||
50 | ecbDecrypt k ct = | ||
51 | let chunks = chunkFor' k ct | ||
52 | in S.concat $ map (decryptBlock k) chunks | ||
53 | |||
54 | |||
55 | {- | ||
15 | newtype ThomasToVincent b = ThomasToVincent b | 56 | newtype ThomasToVincent b = ThomasToVincent b |
16 | 57 | ||
17 | instance Thomas.BlockCipher b => Cipher (ThomasToVincent b) where | 58 | instance Thomas.BlockCipher b => Cipher (ThomasToVincent b) where |
@@ -24,12 +65,13 @@ instance Thomas.BlockCipher b => Cipher (ThomasToVincent b) where | |||
24 | instance Thomas.BlockCipher b => BlockCipher (ThomasToVincent b) where | 65 | instance Thomas.BlockCipher b => BlockCipher (ThomasToVincent b) where |
25 | blockSize _ = bitlen `div` 8 | 66 | blockSize _ = bitlen `div` 8 |
26 | where Tagged bitlen = Thomas.blockSize :: Tagged b Int | 67 | where Tagged bitlen = Thomas.blockSize :: Tagged b Int |
27 | #if ! MIN_VERSION_crypto_api(0,11,0) | 68 | if ! MIN_VERSION_crypto_api(0,11,0) |
28 | ecbEncrypt (ThomasToVincent k) = Thomas.ecb' k | 69 | ecbEncrypt (ThomasToVincent k) = Thomas.ecb' k |
29 | ecbDecrypt (ThomasToVincent k) = Thomas.unEcb' k | 70 | ecbDecrypt (ThomasToVincent k) = Thomas.unEcb' k |
30 | #else | 71 | else |
31 | ecbEncrypt (ThomasToVincent k) = Thomas.ecb k | 72 | ecbEncrypt (ThomasToVincent k) = Thomas.ecb k |
32 | ecbDecrypt (ThomasToVincent k) = Thomas.unEcb k | 73 | ecbDecrypt (ThomasToVincent k) = Thomas.unEcb k |
33 | #endif | 74 | endif |
75 | -} | ||
34 | 76 | ||
35 | 77 | ||