module Botan.Low.FPE (
FPE(..)
, FPEFlags(..)
, fPEFlags
, withFPE
, fpeInitFE1
, fpeDestroy
, fpeEncrypt
, fpeDecrypt
) where
import Botan.Bindings.ConstPtr (ConstPtr (..))
import Botan.Bindings.FPE
import Botan.Low.Error.Internal
import Botan.Low.Internal.ByteString
import Botan.Low.MPI
import Botan.Low.Remake
import Data.ByteString (ByteString)
import Data.Word
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Ptr
newtype FPE = MkFPE { FPE -> ForeignPtr BotanFPEStruct
foreignPtr :: ForeignPtr BotanFPEStruct }
withFPE :: FPE -> (BotanFPE -> IO a) -> IO a
fpeDestroy :: FPE -> IO ()
createFPE :: (Ptr BotanFPE -> IO CInt) -> IO FPE
(FPE -> (BotanFPE -> IO a) -> IO a
withFPE, FPE -> IO ()
fpeDestroy, (Ptr BotanFPE -> IO CInt) -> IO FPE
createFPE)
= (Ptr BotanFPEStruct -> BotanFPE)
-> (BotanFPE -> Ptr BotanFPEStruct)
-> (ForeignPtr BotanFPEStruct -> FPE)
-> (FPE -> ForeignPtr BotanFPEStruct)
-> FinalizerPtr BotanFPEStruct
-> (FPE -> (BotanFPE -> IO a) -> IO a, FPE -> IO (),
(Ptr BotanFPE -> IO CInt) -> IO FPE)
forall botan struct object a.
Storable botan =>
(Ptr struct -> botan)
-> (botan -> Ptr struct)
-> (ForeignPtr struct -> object)
-> (object -> ForeignPtr struct)
-> FinalizerPtr struct
-> (object -> (botan -> IO a) -> IO a, object -> IO (),
(Ptr botan -> IO CInt) -> IO object)
mkBindings
Ptr BotanFPEStruct -> BotanFPE
MkBotanFPE (.ptr)
ForeignPtr BotanFPEStruct -> FPE
MkFPE (.foreignPtr)
FinalizerPtr BotanFPEStruct
botan_fpe_destroy
data FPEFlags =
FPENone
| FPEFE1CompatMode
fPEFlags :: FPEFlags -> Word32
fPEFlags :: FPEFlags -> Word32
fPEFlags FPEFlags
FPENone = Word32
forall a. (Eq a, Num a) => a
BOTAN_FPE_FLAG_NONE
fPEFlags FPEFlags
FPEFE1CompatMode = Word32
forall a. (Eq a, Num a) => a
BOTAN_FPE_FLAG_FE1_COMPAT_MODE
fpeInitFE1
:: MP
-> ByteString
-> Int
-> FPEFlags
-> IO FPE
fpeInitFE1 :: MP -> ByteString -> Int -> FPEFlags -> IO FPE
fpeInitFE1 MP
n ByteString
key Int
rounds FPEFlags
flags = MP -> (BotanMP -> IO FPE) -> IO FPE
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP MP
n ((BotanMP -> IO FPE) -> IO FPE) -> (BotanMP -> IO FPE) -> IO FPE
forall a b. (a -> b) -> a -> b
$ \ BotanMP
nPtr -> do
ByteString -> (Ptr Word8 -> CSize -> IO FPE) -> IO FPE
forall byte a. ByteString -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen ByteString
key ((Ptr Word8 -> CSize -> IO FPE) -> IO FPE)
-> (Ptr Word8 -> CSize -> IO FPE) -> IO FPE
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
keyPtr CSize
keyLen -> do
(Ptr BotanFPE -> IO CInt) -> IO FPE
createFPE ((Ptr BotanFPE -> IO CInt) -> IO FPE)
-> (Ptr BotanFPE -> IO CInt) -> IO FPE
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanFPE
out -> Ptr BotanFPE
-> BotanMP -> ConstPtr Word8 -> CSize -> CSize -> Word32 -> IO CInt
botan_fpe_fe1_init
Ptr BotanFPE
out
BotanMP
nPtr
(Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
keyPtr)
CSize
keyLen
(Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
rounds)
(FPEFlags -> Word32
fPEFlags FPEFlags
flags)
fpeEncrypt
:: FPE
-> MP
-> ByteString
-> IO ()
fpeEncrypt :: FPE -> MP -> ByteString -> IO ()
fpeEncrypt FPE
fpe MP
mp ByteString
tweak = do
FPE -> (BotanFPE -> IO ()) -> IO ()
forall a. FPE -> (BotanFPE -> IO a) -> IO a
withFPE FPE
fpe ((BotanFPE -> IO ()) -> IO ()) -> (BotanFPE -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanFPE
fpePtr -> do
MP -> (BotanMP -> IO ()) -> IO ()
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP MP
mp ((BotanMP -> IO ()) -> IO ()) -> (BotanMP -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanMP
mpPtr -> do
ByteString -> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall byte a. ByteString -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen ByteString
tweak ((Ptr Word8 -> CSize -> IO ()) -> IO ())
-> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
tweakPtr CSize
tweakLen -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanFPE -> BotanMP -> ConstPtr Word8 -> CSize -> IO CInt
botan_fpe_encrypt BotanFPE
fpePtr BotanMP
mpPtr (Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
tweakPtr) CSize
tweakLen
fpeDecrypt
:: FPE
-> MP
-> ByteString
-> IO ()
fpeDecrypt :: FPE -> MP -> ByteString -> IO ()
fpeDecrypt FPE
fpe MP
mp ByteString
tweak = do
FPE -> (BotanFPE -> IO ()) -> IO ()
forall a. FPE -> (BotanFPE -> IO a) -> IO a
withFPE FPE
fpe ((BotanFPE -> IO ()) -> IO ()) -> (BotanFPE -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanFPE
fpePtr -> do
MP -> (BotanMP -> IO ()) -> IO ()
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP MP
mp ((BotanMP -> IO ()) -> IO ()) -> (BotanMP -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanMP
mpPtr -> do
ByteString -> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall byte a. ByteString -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen ByteString
tweak ((Ptr Word8 -> CSize -> IO ()) -> IO ())
-> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
tweakPtr CSize
tweakLen -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanFPE -> BotanMP -> ConstPtr Word8 -> CSize -> IO CInt
botan_fpe_decrypt BotanFPE
fpePtr BotanMP
mpPtr (Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
tweakPtr) CSize
tweakLen