module Crypto.Cipher.DES
( DES
) where
import Data.Word
import Crypto.Error
import Crypto.Cipher.Types
import Crypto.Cipher.DES.Primitive
import Crypto.Internal.ByteArray (ByteArrayAccess)
import qualified Crypto.Internal.ByteArray as B
import Data.Memory.Endian
data DES = DES Word64
deriving (DES -> DES -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DES -> DES -> Bool
$c/= :: DES -> DES -> Bool
== :: DES -> DES -> Bool
$c== :: DES -> DES -> Bool
Eq)
instance Cipher DES where
cipherName :: DES -> String
cipherName DES
_ = String
"DES"
cipherKeySize :: DES -> KeySizeSpecifier
cipherKeySize DES
_ = Int -> KeySizeSpecifier
KeySizeFixed Int
8
cipherInit :: forall key. ByteArray key => key -> CryptoFailable DES
cipherInit key
k = forall key. ByteArrayAccess key => key -> CryptoFailable DES
initDES key
k
instance BlockCipher DES where
blockSize :: DES -> Int
blockSize DES
_ = Int
8
ecbEncrypt :: forall ba. ByteArray ba => DES -> ba -> ba
ecbEncrypt (DES Word64
key) = forall bs. ByteArray bs => (Word64 -> Word64) -> bs -> bs
B.mapAsWord64 (Block -> Word64
unBlock forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Block -> Block
encrypt Word64
key forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Block
Block)
ecbDecrypt :: forall ba. ByteArray ba => DES -> ba -> ba
ecbDecrypt (DES Word64
key) = forall bs. ByteArray bs => (Word64 -> Word64) -> bs -> bs
B.mapAsWord64 (Block -> Word64
unBlock forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Block -> Block
decrypt Word64
key forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Block
Block)
initDES :: ByteArrayAccess key => key -> CryptoFailable DES
initDES :: forall key. ByteArrayAccess key => key -> CryptoFailable DES
initDES key
k
| Int
len forall a. Eq a => a -> a -> Bool
== Int
8 = forall a. a -> CryptoFailable a
CryptoPassed forall a b. (a -> b) -> a -> b
$ Word64 -> DES
DES Word64
key
| Bool
otherwise = forall a. CryptoError -> CryptoFailable a
CryptoFailed forall a b. (a -> b) -> a -> b
$ CryptoError
CryptoError_KeySizeInvalid
where len :: Int
len = forall ba. ByteArrayAccess ba => ba -> Int
B.length key
k
key :: Word64
key = forall a. ByteSwap a => BE a -> a
fromBE forall a b. (a -> b) -> a -> b
$ forall bs. ByteArrayAccess bs => bs -> Int -> BE Word64
B.toW64BE key
k Int
0