module Botan.Low.Cipher
(
Cipher(..)
, CipherName(..)
, CipherKey(..)
, CipherNonce(..)
, CipherInitFlags(..)
, pattern MaskDirection
, pattern Encrypt
, pattern Decrypt
, CipherUpdateFlags(..)
, pattern CipherUpdate
, pattern CipherFinal
, withCipher
, cipherInit
, cipherDestroy
, cipherName
, cipherOutputLength
, cipherValidNonceLength
, cipherGetTagLength
, cipherGetDefaultNonceLength
, cipherGetUpdateGranularity
, cipherGetIdealUpdateGranularity
, cipherQueryKeylen
, cipherGetKeyspec
, cipherSetKey
, cipherReset
, cipherSetAssociatedData
, cipherStart
, cipherUpdate
, cipherEncrypt
, cipherDecrypt
, cipherClear
, CipherMode(..)
, cbcMode
, cfbMode
, cfbModeWith
, xtsMode
, CBCPaddingName(..)
, pattern PKCS7
, pattern OneAndZeros
, pattern X9_23
, pattern ESP
, pattern CTS
, pattern NoPadding
, AEADName(..)
, pattern ChaCha20Poly1305
, chaCha20Poly1305
, AEADMode(..)
, gcmMode
, gcmModeWith
, ocbMode
, ocbModeWith
, eaxMode
, eaxModeWith
, sivMode
, ccmMode
, ccmModeWith
, cipherEncryptOnline
, cipherDecryptOnline
, cipherModes
, cbcPaddings
, aeads
, allCiphers
) where
import qualified Data.ByteString as ByteString
import Botan.Bindings.Cipher
import Botan.Low.BlockCipher
import Botan.Low.Error
import Botan.Low.Make
import Botan.Low.Prelude
import Botan.Low.Remake
import Botan.Low.RNG
newtype Cipher = MkCipher { Cipher -> ForeignPtr BotanCipherStruct
getCipherForeignPtr :: ForeignPtr BotanCipherStruct }
newCipher :: BotanCipher -> IO Cipher
withCipher :: Cipher -> (BotanCipher -> IO a) -> IO a
cipherDestroy :: Cipher -> IO ()
createCipher :: (Ptr BotanCipher -> IO CInt) -> IO Cipher
(BotanCipher -> IO Cipher
newCipher, Cipher -> (BotanCipher -> IO a) -> IO a
withCipher, Cipher -> IO ()
cipherDestroy, (Ptr BotanCipher -> IO CInt) -> IO Cipher
createCipher, (Ptr BotanCipher -> Ptr CSize -> IO CInt) -> IO [Cipher]
_)
= (Ptr BotanCipherStruct -> BotanCipher)
-> (BotanCipher -> Ptr BotanCipherStruct)
-> (ForeignPtr BotanCipherStruct -> Cipher)
-> (Cipher -> ForeignPtr BotanCipherStruct)
-> FinalizerPtr BotanCipherStruct
-> (BotanCipher -> IO Cipher,
Cipher -> (BotanCipher -> IO a) -> IO a, Cipher -> IO (),
(Ptr BotanCipher -> IO CInt) -> IO Cipher,
(Ptr BotanCipher -> Ptr CSize -> IO CInt) -> IO [Cipher])
forall botan struct object a.
Storable botan =>
(Ptr struct -> botan)
-> (botan -> Ptr struct)
-> (ForeignPtr struct -> object)
-> (object -> ForeignPtr struct)
-> FinalizerPtr struct
-> (botan -> IO object, object -> (botan -> IO a) -> IO a,
object -> IO (), (Ptr botan -> IO CInt) -> IO object,
(Ptr botan -> Ptr CSize -> IO CInt) -> IO [object])
mkBindings
Ptr BotanCipherStruct -> BotanCipher
MkBotanCipher BotanCipher -> Ptr BotanCipherStruct
runBotanCipher
ForeignPtr BotanCipherStruct -> Cipher
MkCipher Cipher -> ForeignPtr BotanCipherStruct
getCipherForeignPtr
FinalizerPtr BotanCipherStruct
botan_cipher_destroy
type CipherInitFlags = Word32
type CipherUpdateFlags = Int
type CipherNonce = ByteString
type CipherKey = ByteString
type CipherName = ByteString
type CipherMode = ByteString
type CBCPaddingName = ByteString
pattern PKCS7
, OneAndZeros
, X9_23
, ESP
, CTS
, NoPadding
:: CBCPaddingName
pattern $mPKCS7 :: forall {r}. AEADName -> ((# #) -> r) -> ((# #) -> r) -> r
$bPKCS7 :: AEADName
PKCS7 = BOTAN_CBC_PADDING_PKCS7
pattern $mOneAndZeros :: forall {r}. AEADName -> ((# #) -> r) -> ((# #) -> r) -> r
$bOneAndZeros :: AEADName
OneAndZeros = BOTAN_CBC_PADDING_ONE_AND_ZEROS
pattern $mX9_23 :: forall {r}. AEADName -> ((# #) -> r) -> ((# #) -> r) -> r
$bX9_23 :: AEADName
X9_23 = BOTAN_CBC_PADDING_X9_23
pattern $mESP :: forall {r}. AEADName -> ((# #) -> r) -> ((# #) -> r) -> r
$bESP :: AEADName
ESP = BOTAN_CBC_PADDING_ESP
pattern $mCTS :: forall {r}. AEADName -> ((# #) -> r) -> ((# #) -> r) -> r
$bCTS :: AEADName
CTS = BOTAN_CBC_PADDING_CTS
pattern $mNoPadding :: forall {r}. AEADName -> ((# #) -> r) -> ((# #) -> r) -> r
$bNoPadding :: AEADName
NoPadding = BOTAN_CBC_PADDING_NO_PADDING
cbcMode :: BlockCipherName -> CBCPaddingName -> CipherName
cbcMode :: AEADName -> AEADName -> AEADName
cbcMode AEADName
bc AEADName
padding = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_CIPHER_MODE_CBC AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
padding
cfbMode :: BlockCipherName -> CipherName
cfbMode :: AEADName -> AEADName
cfbMode AEADName
bc = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_CIPHER_MODE_CFB
cfbModeWith :: BlockCipherName -> Int -> CipherName
cfbModeWith :: AEADName -> Int -> AEADName
cfbModeWith AEADName
bc Int
feedbackSz = AEADName -> AEADName
cfbMode AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
/$ Int -> AEADName
forall a. Show a => a -> AEADName
showBytes Int
feedbackSz
xtsMode :: BlockCipherName -> CipherName
xtsMode :: AEADName -> AEADName
xtsMode AEADName
bc = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_CIPHER_MODE_XTS
type AEADName = CipherName
pattern ChaCha20Poly1305 :: CipherName
pattern $mChaCha20Poly1305 :: forall {r}. AEADName -> ((# #) -> r) -> ((# #) -> r) -> r
$bChaCha20Poly1305 :: AEADName
ChaCha20Poly1305 = BOTAN_AEAD_CHACHA20POLY1305
chaCha20Poly1305 :: AEADName
chaCha20Poly1305 :: AEADName
chaCha20Poly1305 = AEADName
forall a. (Eq a, IsString a) => a
BOTAN_AEAD_CHACHA20POLY1305
type AEADMode = ByteString
gcmMode :: BlockCipher128Name -> AEADName
gcmMode :: AEADName -> AEADName
gcmMode AEADName
bc = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_AEAD_MODE_GCM
gcmModeWith :: BlockCipher128Name -> Int -> AEADName
gcmModeWith :: AEADName -> Int -> AEADName
gcmModeWith AEADName
bc Int
tagSz = AEADName -> AEADName
gcmMode AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
/$ Int -> AEADName
forall a. Show a => a -> AEADName
showBytes Int
tagSz
ocbMode :: BlockCipher128Name -> AEADName
ocbMode :: AEADName -> AEADName
ocbMode AEADName
bc = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_AEAD_MODE_OCB
ocbModeWith :: BlockCipher128Name -> Int -> AEADName
ocbModeWith :: AEADName -> Int -> AEADName
ocbModeWith AEADName
bc Int
tagSz = AEADName -> AEADName
ocbMode AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
/$ Int -> AEADName
forall a. Show a => a -> AEADName
showBytes Int
tagSz
eaxMode :: BlockCipherName -> AEADName
eaxMode :: AEADName -> AEADName
eaxMode AEADName
bc = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_AEAD_MODE_EAX
eaxModeWith :: BlockCipherName -> Int -> AEADName
eaxModeWith :: AEADName -> Int -> AEADName
eaxModeWith AEADName
bc Int
tagSz = AEADName -> AEADName
eaxMode AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
/$ Int -> AEADName
forall a. Show a => a -> AEADName
showBytes Int
tagSz
sivMode :: BlockCipher128Name -> AEADName
sivMode :: AEADName -> AEADName
sivMode AEADName
bc = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_AEAD_MODE_SIV
ccmMode :: BlockCipher128Name -> AEADName
ccmMode :: AEADName -> AEADName
ccmMode AEADName
bc = AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
// AEADName
forall a. (Eq a, IsString a) => a
BOTAN_AEAD_MODE_CCM
ccmModeWith :: BlockCipher128Name -> Int -> Int -> AEADName
ccmModeWith :: AEADName -> Int -> Int -> AEADName
ccmModeWith AEADName
bc Int
tagSz Int
l = AEADName -> AEADName
ccmMode AEADName
bc AEADName -> AEADName -> AEADName
forall a. (IsString a, Semigroup a) => a -> a -> a
/$ Int -> AEADName
forall a. Show a => a -> AEADName
showBytes Int
tagSz AEADName -> AEADName -> AEADName
forall a. Semigroup a => a -> a -> a
<> AEADName
"," AEADName -> AEADName -> AEADName
forall a. Semigroup a => a -> a -> a
<> Int -> AEADName
forall a. Show a => a -> AEADName
showBytes Int
l
cbcPaddings :: [AEADName]
cbcPaddings =
[ AEADName
PKCS7
, AEADName
OneAndZeros
, AEADName
X9_23
, AEADName
ESP
, AEADName
CTS
, AEADName
NoPadding
]
cipherModes :: [AEADName]
cipherModes = [[AEADName]] -> [AEADName]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [ AEADName -> AEADName -> AEADName
cbcMode AEADName
bc AEADName
pd | AEADName
bc <- [AEADName]
allBlockCiphers, AEADName
pd <- [AEADName]
cbcPaddings ]
, [ AEADName -> AEADName
cfbMode AEADName
bc | AEADName
bc <- [AEADName]
allBlockCiphers ]
, [ AEADName -> AEADName
xtsMode AEADName
bc | AEADName
bc <- [AEADName]
allBlockCiphers ]
]
aeads :: [AEADName]
aeads = [[AEADName]] -> [AEADName]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [ AEADName
chaCha20Poly1305 ]
, [ AEADName -> AEADName
gcmMode AEADName
bc | AEADName
bc <- [AEADName]
blockCipher128s ]
, [ AEADName -> AEADName
ocbMode AEADName
bc | AEADName
bc <- [AEADName]
blockCipher128s ]
, [ AEADName -> AEADName
eaxMode AEADName
bc | AEADName
bc <- [AEADName]
blockCiphers ]
, [ AEADName -> AEADName
sivMode AEADName
bc | AEADName
bc <- [AEADName]
blockCipher128s ]
, [ AEADName -> AEADName
ccmMode AEADName
bc | AEADName
bc <- [AEADName]
blockCipher128s ]
]
allCiphers :: [AEADName]
allCiphers = [AEADName]
cipherModes [AEADName] -> [AEADName] -> [AEADName]
forall a. [a] -> [a] -> [a]
++ [AEADName]
aeads
pattern MaskDirection
, Encrypt
, Decrypt
:: CipherInitFlags
pattern $mMaskDirection :: forall {r}. CipherInitFlags -> ((# #) -> r) -> ((# #) -> r) -> r
$bMaskDirection :: CipherInitFlags
MaskDirection = BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION
pattern $mEncrypt :: forall {r}. CipherInitFlags -> ((# #) -> r) -> ((# #) -> r) -> r
$bEncrypt :: CipherInitFlags
Encrypt = BOTAN_CIPHER_INIT_FLAG_ENCRYPT
pattern $mDecrypt :: forall {r}. CipherInitFlags -> ((# #) -> r) -> ((# #) -> r) -> r
$bDecrypt :: CipherInitFlags
Decrypt = BOTAN_CIPHER_INIT_FLAG_DECRYPT
pattern CipherUpdate
, CipherFinal
:: CipherUpdateFlags
pattern $mCipherUpdate :: forall {r}. Int -> ((# #) -> r) -> ((# #) -> r) -> r
$bCipherUpdate :: Int
CipherUpdate = BOTAN_CIPHER_UPDATE_FLAG_NONE
pattern $mCipherFinal :: forall {r}. Int -> ((# #) -> r) -> ((# #) -> r) -> r
$bCipherFinal :: Int
CipherFinal = BOTAN_CIPHER_UPDATE_FLAG_FINAL
cipherInit
:: CipherName
-> CipherInitFlags
-> IO Cipher
cipherInit :: AEADName -> CipherInitFlags -> IO Cipher
cipherInit = ((Ptr BotanCipher -> IO CInt) -> IO Cipher)
-> (Ptr BotanCipher
-> ConstPtr CChar -> CipherInitFlags -> IO CInt)
-> AEADName
-> CipherInitFlags
-> IO Cipher
forall botan object a.
((Ptr botan -> IO CInt) -> IO object)
-> (Ptr botan -> ConstPtr CChar -> a -> IO CInt)
-> AEADName
-> a
-> IO object
mkCreateObjectCString1 (Ptr BotanCipher -> IO CInt) -> IO Cipher
createCipher Ptr BotanCipher -> ConstPtr CChar -> CipherInitFlags -> IO CInt
botan_cipher_init
withCipherInit :: CipherName -> CipherInitFlags -> (Cipher -> IO a) -> IO a
withCipherInit :: forall a. AEADName -> CipherInitFlags -> (Cipher -> IO a) -> IO a
withCipherInit = (AEADName -> CipherInitFlags -> IO Cipher)
-> (Cipher -> IO ())
-> AEADName
-> CipherInitFlags
-> (Cipher -> IO a)
-> IO a
forall x y t a.
(x -> y -> IO t) -> (t -> IO ()) -> x -> y -> (t -> IO a) -> IO a
mkWithTemp2 AEADName -> CipherInitFlags -> IO Cipher
cipherInit Cipher -> IO ()
cipherDestroy
cipherName
:: Cipher
-> IO CipherName
cipherName :: Cipher -> IO AEADName
cipherName = WithPtr Cipher BotanCipher
-> GetCString BotanCipher CChar -> Cipher -> IO AEADName
forall typ ptr byte.
WithPtr typ ptr -> GetCString ptr byte -> typ -> IO AEADName
mkGetCString Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetCString BotanCipher CChar
botan_cipher_name
cipherOutputLength
:: Cipher
-> Int
-> IO Int
cipherOutputLength :: Cipher -> Int -> IO Int
cipherOutputLength = WithPtr Cipher BotanCipher
-> GetSize_csize BotanCipher -> Cipher -> Int -> IO Int
forall typ ptr.
WithPtr typ ptr -> GetSize_csize ptr -> typ -> Int -> IO Int
mkGetSize_csize Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetSize_csize BotanCipher
botan_cipher_output_length
cipherValidNonceLength
:: Cipher
-> Int
-> IO Bool
cipherValidNonceLength :: Cipher -> Int -> IO Bool
cipherValidNonceLength = WithPtr Cipher BotanCipher
-> GetBoolCode_csize BotanCipher -> Cipher -> Int -> IO Bool
forall typ ptr.
WithPtr typ ptr -> GetBoolCode_csize ptr -> typ -> Int -> IO Bool
mkGetBoolCode_csize Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetBoolCode_csize BotanCipher
botan_cipher_valid_nonce_length
cipherGetTagLength
:: Cipher
-> IO Int
cipherGetTagLength :: Cipher -> IO Int
cipherGetTagLength = WithPtr Cipher BotanCipher
-> GetSize BotanCipher -> Cipher -> IO Int
forall typ ptr. WithPtr typ ptr -> GetSize ptr -> typ -> IO Int
mkGetSize Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetSize BotanCipher
botan_cipher_get_tag_length
cipherGetDefaultNonceLength
:: Cipher
-> IO Int
cipherGetDefaultNonceLength :: Cipher -> IO Int
cipherGetDefaultNonceLength = WithPtr Cipher BotanCipher
-> GetSize BotanCipher -> Cipher -> IO Int
forall typ ptr. WithPtr typ ptr -> GetSize ptr -> typ -> IO Int
mkGetSize Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetSize BotanCipher
botan_cipher_get_default_nonce_length
cipherGetUpdateGranularity
:: Cipher
-> IO Int
cipherGetUpdateGranularity :: Cipher -> IO Int
cipherGetUpdateGranularity = WithPtr Cipher BotanCipher
-> GetSize BotanCipher -> Cipher -> IO Int
forall typ ptr. WithPtr typ ptr -> GetSize ptr -> typ -> IO Int
mkGetSize Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetSize BotanCipher
botan_cipher_get_update_granularity
cipherGetIdealUpdateGranularity
:: Cipher
-> IO Int
cipherGetIdealUpdateGranularity :: Cipher -> IO Int
cipherGetIdealUpdateGranularity = WithPtr Cipher BotanCipher
-> GetSize BotanCipher -> Cipher -> IO Int
forall typ ptr. WithPtr typ ptr -> GetSize ptr -> typ -> IO Int
mkGetSize Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetSize BotanCipher
botan_cipher_get_ideal_update_granularity
cipherQueryKeylen
:: Cipher
-> IO (Int,Int)
cipherQueryKeylen :: Cipher -> IO (Int, Int)
cipherQueryKeylen = WithPtr Cipher BotanCipher
-> GetSizes2 BotanCipher -> Cipher -> IO (Int, Int)
forall typ ptr.
WithPtr typ ptr -> GetSizes2 ptr -> typ -> IO (Int, Int)
mkGetSizes2 Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetSizes2 BotanCipher
botan_cipher_query_keylen
{-# DEPRECATED cipherQueryKeylen "Prefer cipherGetKeyspec." #-}
cipherGetKeyspec
:: Cipher
-> IO (Int,Int,Int)
cipherGetKeyspec :: Cipher -> IO (Int, Int, Int)
cipherGetKeyspec = WithPtr Cipher BotanCipher
-> GetSizes3 BotanCipher -> Cipher -> IO (Int, Int, Int)
forall typ ptr.
WithPtr typ ptr -> GetSizes3 ptr -> typ -> IO (Int, Int, Int)
mkGetSizes3 Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher GetSizes3 BotanCipher
botan_cipher_get_keyspec
cipherSetKey
:: Cipher
-> ByteString
-> IO ()
cipherSetKey :: Cipher -> AEADName -> IO ()
cipherSetKey = WithPtr Cipher BotanCipher
-> (BotanCipher -> ConstPtr Word8 -> CSize -> IO CInt)
-> Cipher
-> AEADName
-> IO ()
forall object botan.
(forall a. object -> (botan -> IO a) -> IO a)
-> (botan -> ConstPtr Word8 -> CSize -> IO CInt)
-> object
-> AEADName
-> IO ()
mkWithObjectSetterCBytesLen Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher BotanCipher -> ConstPtr Word8 -> CSize -> IO CInt
botan_cipher_set_key
cipherReset
:: Cipher
-> IO ()
cipherReset :: Cipher -> IO ()
cipherReset = WithPtr Cipher BotanCipher -> Action BotanCipher -> Cipher -> IO ()
forall typ ptr. WithPtr typ ptr -> Action ptr -> typ -> IO ()
mkAction Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher Action BotanCipher
botan_cipher_reset
cipherSetAssociatedData
:: Cipher
-> ByteString
-> IO ()
cipherSetAssociatedData :: Cipher -> AEADName -> IO ()
cipherSetAssociatedData = WithPtr Cipher BotanCipher
-> (BotanCipher -> ConstPtr Word8 -> CSize -> IO CInt)
-> Cipher
-> AEADName
-> IO ()
forall object botan.
(forall a. object -> (botan -> IO a) -> IO a)
-> (botan -> ConstPtr Word8 -> CSize -> IO CInt)
-> object
-> AEADName
-> IO ()
mkWithObjectSetterCBytesLen Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher BotanCipher -> ConstPtr Word8 -> CSize -> IO CInt
botan_cipher_set_associated_data
cipherStart
:: Cipher
-> ByteString
-> IO ()
cipherStart :: Cipher -> AEADName -> IO ()
cipherStart = WithPtr Cipher BotanCipher
-> (BotanCipher -> ConstPtr Word8 -> CSize -> IO CInt)
-> Cipher
-> AEADName
-> IO ()
forall object botan.
(forall a. object -> (botan -> IO a) -> IO a)
-> (botan -> ConstPtr Word8 -> CSize -> IO CInt)
-> object
-> AEADName
-> IO ()
mkWithObjectSetterCBytesLen Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher BotanCipher -> ConstPtr Word8 -> CSize -> IO CInt
botan_cipher_start
cipherUpdate
:: Cipher
-> CipherUpdateFlags
-> Int
-> ByteString
-> IO (Int,ByteString)
cipherUpdate :: Cipher -> Int -> Int -> AEADName -> IO (Int, AEADName)
cipherUpdate Cipher
ctx Int
flags Int
outputSz AEADName
input = Cipher -> (BotanCipher -> IO (Int, AEADName)) -> IO (Int, AEADName)
WithPtr Cipher BotanCipher
withCipher Cipher
ctx ((BotanCipher -> IO (Int, AEADName)) -> IO (Int, AEADName))
-> (BotanCipher -> IO (Int, AEADName)) -> IO (Int, AEADName)
forall a b. (a -> b) -> a -> b
$ \ BotanCipher
ctxPtr -> do
AEADName
-> (Ptr Word8 -> CSize -> IO (Int, AEADName)) -> IO (Int, AEADName)
forall byte a. AEADName -> (Ptr byte -> CSize -> IO a) -> IO a
unsafeAsBytesLen AEADName
input ((Ptr Word8 -> CSize -> IO (Int, AEADName)) -> IO (Int, AEADName))
-> (Ptr Word8 -> CSize -> IO (Int, AEADName)) -> IO (Int, AEADName)
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
inputPtr CSize
inputSz -> do
(Ptr CSize -> IO (Int, AEADName)) -> IO (Int, AEADName)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CSize -> IO (Int, AEADName)) -> IO (Int, AEADName))
-> (Ptr CSize -> IO (Int, AEADName)) -> IO (Int, AEADName)
forall a b. (a -> b) -> a -> b
$ \ Ptr CSize
consumedPtr -> do
(Ptr CSize -> IO (Int, AEADName)) -> IO (Int, AEADName)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CSize -> IO (Int, AEADName)) -> IO (Int, AEADName))
-> (Ptr CSize -> IO (Int, AEADName)) -> IO (Int, AEADName)
forall a b. (a -> b) -> a -> b
$ \ Ptr CSize
writtenPtr -> do
AEADName
output <- Int -> (Ptr Word8 -> IO ()) -> IO AEADName
forall byte. Int -> (Ptr byte -> IO ()) -> IO AEADName
allocBytes Int
outputSz ((Ptr Word8 -> IO ()) -> IO AEADName)
-> (Ptr Word8 -> IO ()) -> IO AEADName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
outputPtr -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanCipher
-> CipherInitFlags
-> Ptr Word8
-> CSize
-> Ptr CSize
-> ConstPtr Word8
-> CSize
-> Ptr CSize
-> IO CInt
botan_cipher_update
BotanCipher
ctxPtr
(Int -> CipherInitFlags
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
flags)
Ptr Word8
outputPtr
(Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
outputSz)
Ptr CSize
writtenPtr
(Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
inputPtr)
CSize
inputSz
Ptr CSize
consumedPtr
Int
consumed <- CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> IO CSize -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
consumedPtr
Int
written <- CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> IO CSize -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
writtenPtr
let processed :: AEADName
processed = Int -> AEADName -> AEADName
ByteString.take Int
written AEADName
output
in AEADName
processed AEADName -> IO (Int, AEADName) -> IO (Int, AEADName)
forall a b. a -> b -> b
`seq` (Int, AEADName) -> IO (Int, AEADName)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
consumed,AEADName
processed)
cipherEncrypt :: Cipher -> ByteString -> IO ByteString
cipherEncrypt :: Cipher -> AEADName -> IO AEADName
cipherEncrypt = Cipher -> AEADName -> IO AEADName
cipherEncryptOffline
cipherDecrypt :: Cipher -> ByteString -> IO ByteString
cipherDecrypt :: Cipher -> AEADName -> IO AEADName
cipherDecrypt = Cipher -> AEADName -> IO AEADName
cipherDecryptOffline
cipherClear :: Cipher -> IO ()
cipherClear :: Cipher -> IO ()
cipherClear = WithPtr Cipher BotanCipher -> Action BotanCipher -> Cipher -> IO ()
forall typ ptr. WithPtr typ ptr -> Action ptr -> typ -> IO ()
mkAction Cipher -> (BotanCipher -> IO a) -> IO a
WithPtr Cipher BotanCipher
withCipher Action BotanCipher
botan_cipher_clear
{-# DEPRECATED cipherEstimateOutputLength "This will be moved from botan-low to botan" #-}
cipherEstimateOutputLength :: Cipher -> CipherInitFlags -> Int -> IO Int
cipherEstimateOutputLength :: Cipher -> CipherInitFlags -> Int -> IO Int
cipherEstimateOutputLength Cipher
ctx CipherInitFlags
flags Int
input = do
Int
o <- Cipher -> Int -> IO Int
cipherOutputLength Cipher
ctx Int
input
Int
u <- Cipher -> IO Int
cipherGetUpdateGranularity Cipher
ctx
Int
t <- Cipher -> IO Int
cipherGetTagLength Cipher
ctx
if CipherInitFlags
flags CipherInitFlags -> CipherInitFlags -> Bool
forall a. Eq a => a -> a -> Bool
== CipherInitFlags
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_INIT_FLAG_ENCRYPT
then Int -> IO Int
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
t)
else Int -> IO Int
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t)
{-# DEPRECATED cipherEstimateFinalOutputLength "Moving from botan-low to botan" #-}
cipherEstimateFinalOutputLength :: Cipher -> CipherInitFlags -> Int -> Int -> IO Int
cipherEstimateFinalOutputLength :: Cipher -> CipherInitFlags -> Int -> Int -> IO Int
cipherEstimateFinalOutputLength Cipher
ctx CipherInitFlags
flags Int
offset Int
input = do
Int
len <- Cipher -> CipherInitFlags -> Int -> IO Int
cipherEstimateOutputLength Cipher
ctx CipherInitFlags
flags (Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
input)
Int -> IO Int
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> IO Int) -> Int -> IO Int
forall a b. (a -> b) -> a -> b
$ Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
offset
{-# DEPRECATED cipherProcess "Moving from botan-low to botan" #-}
cipherProcess :: Cipher -> CipherUpdateFlags -> Int -> ByteString -> IO (ByteString,ByteString)
cipherProcess :: Cipher -> Int -> Int -> AEADName -> IO (AEADName, AEADName)
cipherProcess Cipher
ctx Int
flags Int
outputSz AEADName
input = do
(Int
consumed,AEADName
processed) <- Cipher -> Int -> Int -> AEADName -> IO (Int, AEADName)
cipherUpdate Cipher
ctx Int
flags Int
outputSz AEADName
input
let remaining :: AEADName
remaining = Int -> AEADName -> AEADName
ByteString.drop Int
consumed AEADName
input
in AEADName
processed AEADName -> IO (AEADName, AEADName) -> IO (AEADName, AEADName)
forall a b. a -> b -> b
`seq` AEADName
remaining AEADName -> IO (AEADName, AEADName) -> IO (AEADName, AEADName)
forall a b. a -> b -> b
`seq` (AEADName, AEADName) -> IO (AEADName, AEADName)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (AEADName
processed,AEADName
remaining)
{-# DEPRECATED cipherProcessOffline "Moving from botan-low to botan" #-}
cipherProcessOffline :: Cipher -> CipherInitFlags -> ByteString -> IO ByteString
cipherProcessOffline :: Cipher -> CipherInitFlags -> AEADName -> IO AEADName
cipherProcessOffline Cipher
ctx CipherInitFlags
flags AEADName
msg = do
Int
o <- Cipher -> CipherInitFlags -> Int -> IO Int
cipherEstimateOutputLength Cipher
ctx CipherInitFlags
flags (AEADName -> Int
ByteString.length AEADName
msg)
(AEADName, AEADName) -> AEADName
forall a b. (a, b) -> a
fst ((AEADName, AEADName) -> AEADName)
-> IO (AEADName, AEADName) -> IO AEADName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Cipher -> Int -> Int -> AEADName -> IO (AEADName, AEADName)
cipherProcess Cipher
ctx Int
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_UPDATE_FLAG_FINAL Int
o AEADName
msg
{-# WARNING cipherEncryptOffline "May be renamed to cipherEncrypt, may be moved to botan" #-}
cipherEncryptOffline :: Cipher -> ByteString -> IO ByteString
cipherEncryptOffline :: Cipher -> AEADName -> IO AEADName
cipherEncryptOffline Cipher
ctx = Cipher -> CipherInitFlags -> AEADName -> IO AEADName
cipherProcessOffline Cipher
ctx CipherInitFlags
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_INIT_FLAG_ENCRYPT
{-# WARNING cipherDecryptOffline "May be renamed to cipherDecrypt, may be moved to botan" #-}
cipherDecryptOffline :: Cipher -> ByteString -> IO ByteString
cipherDecryptOffline :: Cipher -> AEADName -> IO AEADName
cipherDecryptOffline Cipher
ctx = Cipher -> CipherInitFlags -> AEADName -> IO AEADName
cipherProcessOffline Cipher
ctx CipherInitFlags
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_INIT_FLAG_DECRYPT
{-# DEPRECATED cipherProcessOnline "Moving from botan-low to botan" #-}
cipherProcessOnline :: Cipher -> CipherInitFlags -> ByteString -> IO ByteString
cipherProcessOnline :: Cipher -> CipherInitFlags -> AEADName -> IO AEADName
cipherProcessOnline Cipher
ctx CipherInitFlags
flags = if CipherInitFlags
flags CipherInitFlags -> CipherInitFlags -> Bool
forall a. Eq a => a -> a -> Bool
== CipherInitFlags
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_INIT_FLAG_ENCRYPT
then Cipher -> AEADName -> IO AEADName
cipherEncryptOnline Cipher
ctx
else Cipher -> AEADName -> IO AEADName
cipherDecryptOnline Cipher
ctx
{-# DEPRECATED cipherEncryptOnline "Moving from botan-low to botan" #-}
cipherEncryptOnline :: Cipher -> ByteString -> IO ByteString
cipherEncryptOnline :: Cipher -> AEADName -> IO AEADName
cipherEncryptOnline Cipher
ctx AEADName
msg = do
Int
g <- Cipher -> IO Int
cipherGetIdealUpdateGranularity Cipher
ctx
[AEADName] -> AEADName
ByteString.concat ([AEADName] -> AEADName) -> IO [AEADName] -> IO AEADName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> AEADName -> IO [AEADName]
go Int
0 Int
g AEADName
msg
where
go :: Int -> Int -> AEADName -> IO [AEADName]
go Int
i Int
g AEADName
bs = case Int -> AEADName -> (AEADName, AEADName)
ByteString.splitAt Int
g AEADName
bs of
(AEADName
block,AEADName
"") -> do
Int
o <- Cipher -> CipherInitFlags -> Int -> Int -> IO Int
cipherEstimateFinalOutputLength Cipher
ctx CipherInitFlags
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_INIT_FLAG_ENCRYPT Int
i (AEADName -> Int
ByteString.length AEADName
block)
(AEADName
processed,AEADName
_) <- Cipher -> Int -> Int -> AEADName -> IO (AEADName, AEADName)
cipherProcess Cipher
ctx Int
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_UPDATE_FLAG_FINAL Int
o AEADName
block
[AEADName] -> IO [AEADName]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return [AEADName
processed]
(AEADName
block,AEADName
rest) -> do
(AEADName
processed,AEADName
remaining) <- Cipher -> Int -> Int -> AEADName -> IO (AEADName, AEADName)
cipherProcess Cipher
ctx Int
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_UPDATE_FLAG_NONE Int
g AEADName
block
(AEADName
processed AEADName -> [AEADName] -> [AEADName]
forall a. a -> [a] -> [a]
:) ([AEADName] -> [AEADName]) -> IO [AEADName] -> IO [AEADName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> AEADName -> IO [AEADName]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
g) Int
g (AEADName
remaining AEADName -> AEADName -> AEADName
forall a. Semigroup a => a -> a -> a
<> AEADName
rest)
{-# DEPRECATED cipherDecryptOnline "Moving from botan-low to botan" #-}
cipherDecryptOnline :: Cipher -> ByteString -> IO ByteString
cipherDecryptOnline :: Cipher -> AEADName -> IO AEADName
cipherDecryptOnline Cipher
ctx AEADName
msg = do
Int
g <- Cipher -> IO Int
cipherGetIdealUpdateGranularity Cipher
ctx
Int
t <- Cipher -> IO Int
cipherGetTagLength Cipher
ctx
[AEADName] -> AEADName
ByteString.concat ([AEADName] -> AEADName) -> IO [AEADName] -> IO AEADName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> Int -> AEADName -> IO [AEADName]
forall {t}. Int -> Int -> t -> AEADName -> IO [AEADName]
go Int
0 Int
g Int
t AEADName
msg
where
go :: Int -> Int -> t -> AEADName -> IO [AEADName]
go Int
i Int
g t
t AEADName
bs = case Int -> AEADName -> (AEADName, AEADName)
ByteString.splitAt Int
g AEADName
bs of
(AEADName
block,AEADName
"") -> do
Int
o <- Cipher -> CipherInitFlags -> Int -> Int -> IO Int
cipherEstimateFinalOutputLength Cipher
ctx CipherInitFlags
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_INIT_FLAG_DECRYPT Int
i (AEADName -> Int
ByteString.length AEADName
block)
(AEADName
processed,AEADName
_) <- Cipher -> Int -> Int -> AEADName -> IO (AEADName, AEADName)
cipherProcess Cipher
ctx Int
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_UPDATE_FLAG_FINAL Int
o AEADName
block
[AEADName] -> IO [AEADName]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return [AEADName
processed]
(AEADName
block,AEADName
rest) -> do
(AEADName
processed,AEADName
remaining) <- Cipher -> Int -> Int -> AEADName -> IO (AEADName, AEADName)
cipherProcess Cipher
ctx Int
forall a. (Eq a, Num a) => a
BOTAN_CIPHER_UPDATE_FLAG_NONE Int
g AEADName
block
(AEADName
processed AEADName -> [AEADName] -> [AEADName]
forall a. a -> [a] -> [a]
:) ([AEADName] -> [AEADName]) -> IO [AEADName] -> IO [AEADName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> t -> AEADName -> IO [AEADName]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
g) Int
g t
t (AEADName
remaining AEADName -> AEADName -> AEADName
forall a. Semigroup a => a -> a -> a
<> AEADName
rest)