summaryrefslogtreecommitdiff
path: root/Crypto/Cipher/Feistel.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Crypto/Cipher/Feistel.hs')
-rw-r--r--Crypto/Cipher/Feistel.hs50
1 files changed, 50 insertions, 0 deletions
diff --git a/Crypto/Cipher/Feistel.hs b/Crypto/Cipher/Feistel.hs
new file mode 100644
index 0000000..3493e29
--- /dev/null
+++ b/Crypto/Cipher/Feistel.hs
@@ -0,0 +1,50 @@
1{-# LANGUAGE UnboxedTuples #-}
2module Crypto.Cipher.Feistel where
3
4import qualified Data.ByteString as B
5import qualified Data.ByteString.Lazy as L
6import Data.Word
7import Data.Bits
8import Data.Binary
9import System.Endian
10
11toW32Pair :: B.ByteString -> ( Word32, Word32 )
12toW32Pair b = let (x1, x2) = B.splitAt 4 b
13 w1 = decode32be x1
14 w2 = decode32be x2
15 in ( w1,w2 )
16
17fromW32Pair :: (Word32, Word32) -> B.ByteString
18fromW32Pair (w1,w2)
19 = let w1' = fromIntegral w1
20 w2' = fromIntegral w2
21 w = (w1' `shiftL` 32) .|. w2'
22 in encode64be w
23
24
25decode32be :: B.ByteString -> Word32
26decode32be s = id $!
27 (fromIntegral (s `B.index` 0) `shiftL` 24) .|.
28 (fromIntegral (s `B.index` 1) `shiftL` 16) .|.
29 (fromIntegral (s `B.index` 2) `shiftL` 8) .|.
30 (fromIntegral (s `B.index` 3) )
31
32encode64be :: Word64 -> B.ByteString
33encode64be w = B.pack . map fromIntegral $
34 [ (w `shiftR` 56) .&. 0xff
35 , (w `shiftR` 48) .&. 0xff
36 , (w `shiftR` 40) .&. 0xff
37 , (w `shiftR` 32) .&. 0xff
38 , (w `shiftR` 24) .&. 0xff
39 , (w `shiftR` 16) .&. 0xff
40 , (w `shiftR` 8) .&. 0xff
41 , w .&. 0xff
42 ]
43
44splitWord32 :: Word32 -> (# Word8, Word8, Word8, Word8 #)
45splitWord32 w = (# a,b,c,d #)
46 where
47 a = fromIntegral $ w `shiftR` 24
48 b = fromIntegral $ w `shiftR` 16
49 c = fromIntegral $ w `shiftR` 8
50 d = fromIntegral $ w