cipher-des 0.0.5 → 0.0.6
raw patch · 3 files changed
+17/−14 lines, 3 files
Files
- Crypto/Cipher/DES/Primitive.hs +12/−9
- Crypto/Cipher/TripleDES.hs +4/−4
- cipher-des.cabal +1/−1
Crypto/Cipher/DES/Primitive.hs view
@@ -21,7 +21,6 @@ type Rotation = Int type Key = Word64 -type BitsX = [Bool] type Bits4 = [Bool] type Bits6 = [Bool] type Bits32 = [Bool]@@ -29,12 +28,12 @@ type Bits56 = [Bool] type Bits64 = [Bool] -instance Num [Bool]+desXor :: [Bool] -> [Bool] -> [Bool]+desXor a b = zipWith (\x y -> (not x && y) || (x && not y)) a b -instance Bits [Bool] where- a `xor` b = zipWith (\x y -> (not x && y) || (x && not y)) a b- rotate bits rot = drop rot' bits ++ take rot' bits- where rot' = rot `mod` (length bits)+desRotate :: [Bool] -> Int -> [Bool]+desRotate bits rot = drop rot' bits ++ take rot' bits+ where rot' = rot `mod` length bits bitify :: Word64 -> Bits64 bitify w = map (\b -> w .&. (shiftL 1 b) /= 0) [63,62..0]@@ -77,7 +76,7 @@ where kb' = get_key kb r comp_kb = compression_permutation kb' expa_mr = expansion_permutation mr- res = comp_kb `xor` expa_mr+ res = comp_kb `desXor` expa_mr res' = tail $ iterate (trans 6) ([], res) trans n (_, b) = (take n b, drop n b) res_s = concat $ zipWith (\f (x,_) -> f x) [s_box_1, s_box_2,@@ -85,12 +84,12 @@ s_box_5, s_box_6, s_box_7, s_box_8] res' res_p = p_box res_s- m' = res_p `xor` ml+ m' = res_p `desXor` ml get_key :: Bits56 -> Rotation -> Bits56 get_key kb r = kb' where (kl, kr) = takeDrop 28 kb- kb' = rotateL kl r ++ rotateL kr r+ kb' = desRotate kl r ++ desRotate kr r compression_permutation :: Bits56 -> Bits48 compression_permutation kb = map ((!!) kb) i@@ -110,9 +109,13 @@ s_box s [a,b,c,d,e,f] = to_bool 4 $ (s !! row) !! col where row = sum $ zipWith numericise [a,f] [1, 0] col = sum $ zipWith numericise [b,c,d,e] [3, 2, 1, 0]+ numericise :: Bool -> Int -> Int numericise = (\x y -> if x then 2^y else 0)++ to_bool :: Int -> Word8 -> [Bool] to_bool 0 _ = [] to_bool n i = ((i .&. 8) == 8):to_bool (n-1) (shiftL i 1)+s_box _ _ = error "DES: internal error bits6 more than 6 elements" s_box_1 :: Bits6 -> Bits4 s_box_1 = s_box i
Crypto/Cipher/TripleDES.hs view
@@ -57,13 +57,13 @@ instance BlockCipher DES_EEE3 where blockSize _ = 8- ecbEncrypt (DES_EEE3 k1 k2 k3) = unblockify . map (encrypt k1 . encrypt k2 . encrypt k3) . blockify- ecbDecrypt (DES_EEE3 k1 k2 k3) = unblockify . map (decrypt k3 . decrypt k2 . decrypt k1) . blockify+ ecbEncrypt (DES_EEE3 k1 k2 k3) = unblockify . map (encrypt k3 . encrypt k2 . encrypt k1) . blockify+ ecbDecrypt (DES_EEE3 k1 k2 k3) = unblockify . map (decrypt k1 . decrypt k2 . decrypt k3) . blockify instance BlockCipher DES_EDE3 where blockSize _ = 8- ecbEncrypt (DES_EDE3 k1 k2 k3) = unblockify . map (encrypt k1 . decrypt k2 . encrypt k3) . blockify- ecbDecrypt (DES_EDE3 k1 k2 k3) = unblockify . map (decrypt k3 . encrypt k2 . decrypt k1) . blockify+ ecbEncrypt (DES_EDE3 k1 k2 k3) = unblockify . map (encrypt k3 . decrypt k2 . encrypt k1) . blockify+ ecbDecrypt (DES_EDE3 k1 k2 k3) = unblockify . map (decrypt k1 . encrypt k2 . decrypt k3) . blockify instance BlockCipher DES_EEE2 where blockSize _ = 8
cipher-des.cabal view
@@ -1,5 +1,5 @@ Name: cipher-des-Version: 0.0.5+Version: 0.0.6 Description: DES and 3DES primitives License: BSD3 License-file: LICENSE