module Z.Crypto.Cipher
(
BlockCipherType(..), KeySpec(..)
, BlockCipher, blockCipherName, blockCipherKeySpec, blockCipherSize
, newBlockCipher, setBlockCipherKey, clearBlockCipher
, encryptBlocks, decryptBlocks
, CipherMode(..), CipherDirection(..)
, Cipher, cipherName, cipherKeySpec, cipherTagLength, defaultNonceLength
, newCipher, setCipherKey, clearCipher, runCipher
, StreamCipherType(..), StreamCipher, streamCipherName, streamCipherKeySpec, defaultIVLength
, newStreamCipher, setStreamCipherKey, clearStreamCipher
, setStreamCipherIV, seekStreamCipher, runStreamCipher, streamCipherKeyStream, streamCipherBIO, keyStreamSource
, blockCipherTypeToCBytes
, withBlockCipher
, withCipher
, withStreamCipher
, HashType(..)
, module Z.Crypto.SafeMem
) where
import Control.Monad
import GHC.Generics
import Z.Botan.Exception
import Z.Botan.FFI
import Z.Crypto.Hash
import Z.Crypto.SafeMem
import Z.Data.CBytes as CB
import Z.Data.JSON (JSON)
import qualified Z.Data.Text as T
import qualified Z.Data.Vector.Base as V
import qualified Z.Data.Vector.Extra as V
import Z.Foreign
import Z.IO.BIO
data BlockCipherType
= AES128
| AES192
| AES256
| ARIA128
| ARIA192
| ARIA256
| Blowfish
| Camellia128
| Camellia192
| Camellia256
| Cascade BlockCipherType BlockCipherType
| CAST128
| CAST256
| DES
| DESX
| TripleDES
| IDEA
| KASUMI
| Lion HashType StreamCipherType Int
| MISTY1
| Noekeon
| SEED
| Serpent
| SHACAL2
| Twofish
| SM4
| Threefish512
| XTEA
deriving (Int -> BlockCipherType -> ShowS
[BlockCipherType] -> ShowS
BlockCipherType -> String
(Int -> BlockCipherType -> ShowS)
-> (BlockCipherType -> String)
-> ([BlockCipherType] -> ShowS)
-> Show BlockCipherType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BlockCipherType] -> ShowS
$cshowList :: [BlockCipherType] -> ShowS
show :: BlockCipherType -> String
$cshow :: BlockCipherType -> String
showsPrec :: Int -> BlockCipherType -> ShowS
$cshowsPrec :: Int -> BlockCipherType -> ShowS
Show, ReadPrec [BlockCipherType]
ReadPrec BlockCipherType
Int -> ReadS BlockCipherType
ReadS [BlockCipherType]
(Int -> ReadS BlockCipherType)
-> ReadS [BlockCipherType]
-> ReadPrec BlockCipherType
-> ReadPrec [BlockCipherType]
-> Read BlockCipherType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BlockCipherType]
$creadListPrec :: ReadPrec [BlockCipherType]
readPrec :: ReadPrec BlockCipherType
$creadPrec :: ReadPrec BlockCipherType
readList :: ReadS [BlockCipherType]
$creadList :: ReadS [BlockCipherType]
readsPrec :: Int -> ReadS BlockCipherType
$creadsPrec :: Int -> ReadS BlockCipherType
Read, BlockCipherType -> BlockCipherType -> Bool
(BlockCipherType -> BlockCipherType -> Bool)
-> (BlockCipherType -> BlockCipherType -> Bool)
-> Eq BlockCipherType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BlockCipherType -> BlockCipherType -> Bool
$c/= :: BlockCipherType -> BlockCipherType -> Bool
== :: BlockCipherType -> BlockCipherType -> Bool
$c== :: BlockCipherType -> BlockCipherType -> Bool
Eq, Eq BlockCipherType
Eq BlockCipherType
-> (BlockCipherType -> BlockCipherType -> Ordering)
-> (BlockCipherType -> BlockCipherType -> Bool)
-> (BlockCipherType -> BlockCipherType -> Bool)
-> (BlockCipherType -> BlockCipherType -> Bool)
-> (BlockCipherType -> BlockCipherType -> Bool)
-> (BlockCipherType -> BlockCipherType -> BlockCipherType)
-> (BlockCipherType -> BlockCipherType -> BlockCipherType)
-> Ord BlockCipherType
BlockCipherType -> BlockCipherType -> Bool
BlockCipherType -> BlockCipherType -> Ordering
BlockCipherType -> BlockCipherType -> BlockCipherType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BlockCipherType -> BlockCipherType -> BlockCipherType
$cmin :: BlockCipherType -> BlockCipherType -> BlockCipherType
max :: BlockCipherType -> BlockCipherType -> BlockCipherType
$cmax :: BlockCipherType -> BlockCipherType -> BlockCipherType
>= :: BlockCipherType -> BlockCipherType -> Bool
$c>= :: BlockCipherType -> BlockCipherType -> Bool
> :: BlockCipherType -> BlockCipherType -> Bool
$c> :: BlockCipherType -> BlockCipherType -> Bool
<= :: BlockCipherType -> BlockCipherType -> Bool
$c<= :: BlockCipherType -> BlockCipherType -> Bool
< :: BlockCipherType -> BlockCipherType -> Bool
$c< :: BlockCipherType -> BlockCipherType -> Bool
compare :: BlockCipherType -> BlockCipherType -> Ordering
$ccompare :: BlockCipherType -> BlockCipherType -> Ordering
$cp1Ord :: Eq BlockCipherType
Ord, (forall x. BlockCipherType -> Rep BlockCipherType x)
-> (forall x. Rep BlockCipherType x -> BlockCipherType)
-> Generic BlockCipherType
forall x. Rep BlockCipherType x -> BlockCipherType
forall x. BlockCipherType -> Rep BlockCipherType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BlockCipherType x -> BlockCipherType
$cfrom :: forall x. BlockCipherType -> Rep BlockCipherType x
Generic)
deriving anyclass (Int -> BlockCipherType -> Builder ()
(Int -> BlockCipherType -> Builder ()) -> Print BlockCipherType
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> BlockCipherType -> Builder ()
$ctoUTF8BuilderP :: Int -> BlockCipherType -> Builder ()
T.Print, Value -> Converter BlockCipherType
BlockCipherType -> Value
BlockCipherType -> Builder ()
(Value -> Converter BlockCipherType)
-> (BlockCipherType -> Value)
-> (BlockCipherType -> Builder ())
-> JSON BlockCipherType
forall a.
(Value -> Converter a)
-> (a -> Value) -> (a -> Builder ()) -> JSON a
encodeJSON :: BlockCipherType -> Builder ()
$cencodeJSON :: BlockCipherType -> Builder ()
toValue :: BlockCipherType -> Value
$ctoValue :: BlockCipherType -> Value
fromValue :: Value -> Converter BlockCipherType
$cfromValue :: Value -> Converter BlockCipherType
JSON)
blockCipherTypeToCBytes :: BlockCipherType -> CBytes
{-# INLINABLE blockCipherTypeToCBytes #-}
blockCipherTypeToCBytes :: BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
b = case BlockCipherType
b of
BlockCipherType
AES128 -> CBytes
"AES-128"
BlockCipherType
AES192 -> CBytes
"AES-192"
BlockCipherType
AES256 -> CBytes
"AES-256"
BlockCipherType
ARIA128 -> CBytes
"ARIA-128"
BlockCipherType
ARIA192 -> CBytes
"ARIA-192"
BlockCipherType
ARIA256 -> CBytes
"ARIA-256"
BlockCipherType
Serpent -> CBytes
"Serpent"
BlockCipherType
SHACAL2 -> CBytes
"SHACAL2"
BlockCipherType
Twofish -> CBytes
"Twofish"
BlockCipherType
Threefish512 -> CBytes
"Threefish-512"
BlockCipherType
Blowfish -> CBytes
"Blowfish"
BlockCipherType
Camellia128 -> CBytes
"Camellia-128"
BlockCipherType
Camellia192 -> CBytes
"Camellia-192"
BlockCipherType
Camellia256 -> CBytes
"Camellia-256"
BlockCipherType
DES -> CBytes
"DES"
BlockCipherType
DESX -> CBytes
"DESX"
BlockCipherType
TripleDES -> CBytes
"TripleDES"
BlockCipherType
Noekeon -> CBytes
"Noekeon"
BlockCipherType
CAST128 -> CBytes
"CAST-128"
BlockCipherType
CAST256 -> CBytes
"CAST-256"
BlockCipherType
IDEA -> CBytes
"IDEA"
BlockCipherType
KASUMI -> CBytes
"KASUMI"
BlockCipherType
MISTY1 -> CBytes
"MISTY1"
BlockCipherType
SEED -> CBytes
"SEED"
BlockCipherType
SM4 -> CBytes
"SM4"
BlockCipherType
XTEA -> CBytes
"XTEA"
Cascade BlockCipherType
b1 BlockCipherType
b2 -> [CBytes] -> CBytes
CB.concat [ CBytes
"Cascade("
, BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
b1
, CBytes
","
, BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
b2
, CBytes
")"]
Lion HashType
hasht StreamCipherType
st Int
siz -> [CBytes] -> CBytes
CB.concat [ CBytes
"Lion("
, HashType -> CBytes
hashTypeToCBytes HashType
hasht
, CBytes
","
, StreamCipherType -> CBytes
streamCipherTypeToCBytes StreamCipherType
st
, CBytes
","
, Text -> CBytes
CB.fromText (Int -> Text
forall a. Print a => a -> Text
T.toText Int
siz)
, CBytes
")"]
data BlockCipher = BlockCipher
{ BlockCipher -> BotanStruct
blockCipher :: {-# UNPACK #-} !BotanStruct
, BlockCipher -> CBytes
blockCipherName :: {-# UNPACK #-} !CBytes
, BlockCipher -> Int
blockCipherSize :: {-# UNPACK #-} !Int
, BlockCipher -> KeySpec
blockCipherKeySpec :: {-# UNPACK #-} !KeySpec
}
deriving (Int -> BlockCipher -> ShowS
[BlockCipher] -> ShowS
BlockCipher -> String
(Int -> BlockCipher -> ShowS)
-> (BlockCipher -> String)
-> ([BlockCipher] -> ShowS)
-> Show BlockCipher
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BlockCipher] -> ShowS
$cshowList :: [BlockCipher] -> ShowS
show :: BlockCipher -> String
$cshow :: BlockCipher -> String
showsPrec :: Int -> BlockCipher -> ShowS
$cshowsPrec :: Int -> BlockCipher -> ShowS
Show, (forall x. BlockCipher -> Rep BlockCipher x)
-> (forall x. Rep BlockCipher x -> BlockCipher)
-> Generic BlockCipher
forall x. Rep BlockCipher x -> BlockCipher
forall x. BlockCipher -> Rep BlockCipher x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BlockCipher x -> BlockCipher
$cfrom :: forall x. BlockCipher -> Rep BlockCipher x
Generic)
deriving anyclass Int -> BlockCipher -> Builder ()
(Int -> BlockCipher -> Builder ()) -> Print BlockCipher
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> BlockCipher -> Builder ()
$ctoUTF8BuilderP :: Int -> BlockCipher -> Builder ()
T.Print
withBlockCipher :: BlockCipher -> (BotanStructT -> IO r) -> IO r
{-# INLINABLE withBlockCipher #-}
withBlockCipher :: BlockCipher -> (BotanStructT -> IO r) -> IO r
withBlockCipher (BlockCipher BotanStruct
bc CBytes
_ Int
_ KeySpec
_) = BotanStruct -> (BotanStructT -> IO r) -> IO r
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
bc
data KeySpec = KeySpec
{ KeySpec -> Int
keyLenMin :: {-# UNPACK #-} !Int
, KeySpec -> Int
keyLenMax :: {-# UNPACK #-} !Int
, KeySpec -> Int
keyLenMod :: {-# UNPACK #-} !Int
}
deriving (KeySpec -> KeySpec -> Bool
(KeySpec -> KeySpec -> Bool)
-> (KeySpec -> KeySpec -> Bool) -> Eq KeySpec
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: KeySpec -> KeySpec -> Bool
$c/= :: KeySpec -> KeySpec -> Bool
== :: KeySpec -> KeySpec -> Bool
$c== :: KeySpec -> KeySpec -> Bool
Eq, Eq KeySpec
Eq KeySpec
-> (KeySpec -> KeySpec -> Ordering)
-> (KeySpec -> KeySpec -> Bool)
-> (KeySpec -> KeySpec -> Bool)
-> (KeySpec -> KeySpec -> Bool)
-> (KeySpec -> KeySpec -> Bool)
-> (KeySpec -> KeySpec -> KeySpec)
-> (KeySpec -> KeySpec -> KeySpec)
-> Ord KeySpec
KeySpec -> KeySpec -> Bool
KeySpec -> KeySpec -> Ordering
KeySpec -> KeySpec -> KeySpec
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: KeySpec -> KeySpec -> KeySpec
$cmin :: KeySpec -> KeySpec -> KeySpec
max :: KeySpec -> KeySpec -> KeySpec
$cmax :: KeySpec -> KeySpec -> KeySpec
>= :: KeySpec -> KeySpec -> Bool
$c>= :: KeySpec -> KeySpec -> Bool
> :: KeySpec -> KeySpec -> Bool
$c> :: KeySpec -> KeySpec -> Bool
<= :: KeySpec -> KeySpec -> Bool
$c<= :: KeySpec -> KeySpec -> Bool
< :: KeySpec -> KeySpec -> Bool
$c< :: KeySpec -> KeySpec -> Bool
compare :: KeySpec -> KeySpec -> Ordering
$ccompare :: KeySpec -> KeySpec -> Ordering
$cp1Ord :: Eq KeySpec
Ord, Int -> KeySpec -> ShowS
[KeySpec] -> ShowS
KeySpec -> String
(Int -> KeySpec -> ShowS)
-> (KeySpec -> String) -> ([KeySpec] -> ShowS) -> Show KeySpec
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [KeySpec] -> ShowS
$cshowList :: [KeySpec] -> ShowS
show :: KeySpec -> String
$cshow :: KeySpec -> String
showsPrec :: Int -> KeySpec -> ShowS
$cshowsPrec :: Int -> KeySpec -> ShowS
Show, (forall x. KeySpec -> Rep KeySpec x)
-> (forall x. Rep KeySpec x -> KeySpec) -> Generic KeySpec
forall x. Rep KeySpec x -> KeySpec
forall x. KeySpec -> Rep KeySpec x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep KeySpec x -> KeySpec
$cfrom :: forall x. KeySpec -> Rep KeySpec x
Generic)
deriving anyclass Int -> KeySpec -> Builder ()
(Int -> KeySpec -> Builder ()) -> Print KeySpec
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> KeySpec -> Builder ()
$ctoUTF8BuilderP :: Int -> KeySpec -> Builder ()
T.Print
newBlockCipher :: HasCallStack => BlockCipherType -> IO BlockCipher
{-# INLINABLE newBlockCipher #-}
newBlockCipher :: BlockCipherType -> IO BlockCipher
newBlockCipher BlockCipherType
typ = do
let name :: CBytes
name = BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
typ
BotanStruct
bc <- (MBA# BotanStructT -> IO CInt)
-> FunPtr (BotanStructT -> IO ()) -> IO BotanStruct
forall a.
HasCallStack =>
(MBA# BotanStructT -> IO CInt)
-> FunPtr (BotanStructT -> IO a) -> IO BotanStruct
newBotanStruct
(\ MBA# BotanStructT
bts -> CBytes -> (BA# Word8 -> IO CInt) -> IO CInt
forall a. CBytes -> (BA# Word8 -> IO a) -> IO a
withCBytesUnsafe CBytes
name (MBA# BotanStructT -> BA# Word8 -> IO CInt
botan_block_cipher_init MBA# BotanStructT
bts))
FunPtr (BotanStructT -> IO ())
botan_block_cipher_destroy
CInt
bsiz <- BotanStruct -> (BotanStructT -> IO CInt) -> IO CInt
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
bc BotanStructT -> IO CInt
botan_block_cipher_block_size
(Int
a, (Int
b, (Int
c, ()
_))) <- BotanStruct
-> (BotanStructT -> IO (Int, (Int, (Int, ()))))
-> IO (Int, (Int, (Int, ())))
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
bc ((BotanStructT -> IO (Int, (Int, (Int, ()))))
-> IO (Int, (Int, (Int, ()))))
-> (BotanStructT -> IO (Int, (Int, (Int, ()))))
-> IO (Int, (Int, (Int, ())))
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pbc ->
(MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ())))
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ()))))
-> (MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ())))
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pa ->
(MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ()))
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ())))
-> (MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ()))
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pb ->
(MBA# BotanStructT -> IO ()) -> IO (Int, ())
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO ()) -> IO (Int, ()))
-> (MBA# BotanStructT -> IO ()) -> IO (Int, ())
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pc ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_
(BotanStructT
-> MBA# BotanStructT
-> MBA# BotanStructT
-> MBA# BotanStructT
-> IO CInt
botan_block_cipher_get_keyspec BotanStructT
pbc MBA# BotanStructT
pa MBA# BotanStructT
pb MBA# BotanStructT
pc)
BlockCipher -> IO BlockCipher
forall (m :: * -> *) a. Monad m => a -> m a
return (BotanStruct -> CBytes -> Int -> KeySpec -> BlockCipher
BlockCipher BotanStruct
bc CBytes
name (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
bsiz) (Int -> Int -> Int -> KeySpec
KeySpec Int
a Int
b Int
c))
setBlockCipherKey :: HasCallStack => BlockCipher -> Secret -> IO ()
{-# INLINABLE setBlockCipherKey #-}
setBlockCipherKey :: BlockCipher -> Secret -> IO ()
setBlockCipherKey (BlockCipher BotanStruct
bc CBytes
_ Int
_ KeySpec
_) Secret
key =
BotanStruct -> (BotanStructT -> IO ()) -> IO ()
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
bc ((BotanStructT -> IO ()) -> IO ())
-> (BotanStructT -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pbc -> do
Secret -> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall r. Secret -> (Ptr Word8 -> CSize -> IO r) -> IO r
withSecret Secret
key ((Ptr Word8 -> CSize -> IO ()) -> IO ())
-> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
pkey CSize
key_len ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (BotanStructT -> Ptr Word8 -> CSize -> IO CInt
botan_block_cipher_set_key BotanStructT
pbc Ptr Word8
pkey CSize
key_len)
clearBlockCipher :: HasCallStack => BlockCipher -> IO ()
{-# INLINABLE clearBlockCipher #-}
clearBlockCipher :: BlockCipher -> IO ()
clearBlockCipher (BlockCipher BotanStruct
bc CBytes
_ Int
_ KeySpec
_) =
BotanStruct -> (BotanStructT -> IO ()) -> IO ()
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
bc (IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (IO CInt -> IO ())
-> (BotanStructT -> IO CInt) -> BotanStructT -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BotanStructT -> IO CInt
botan_block_cipher_clear)
encryptBlocks :: HasCallStack
=> BlockCipher
-> V.Bytes
-> Int
-> IO V.Bytes
{-# INLINABLE encryptBlocks #-}
encryptBlocks :: BlockCipher -> Bytes -> Int -> IO Bytes
encryptBlocks (BlockCipher BotanStruct
bc CBytes
_ Int
blockSiz KeySpec
_) Bytes
blocks Int
n = do
let inputLen :: Int
inputLen = Bytes -> Int
forall (v :: * -> *) a. Vec v a => v a -> Int
V.length Bytes
blocks
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
inputLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
blockSiz Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
CInt -> IO ()
forall x. HasCallStack => CInt -> IO x
throwBotanError CInt
BOTAN_FFI_ERROR_INVALID_INPUT
BotanStruct -> (BotanStructT -> IO Bytes) -> IO Bytes
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
bc ((BotanStructT -> IO Bytes) -> IO Bytes)
-> (BotanStructT -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pbc -> do
Bytes -> (BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe Bytes
blocks ((BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes)
-> (BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
pb Int
pboff Int
_ ->
(Bytes, ()) -> Bytes
forall a b. (a, b) -> a
fst ((Bytes, ()) -> Bytes) -> IO (Bytes, ()) -> IO Bytes
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> (MBA# BotanStructT -> IO ()) -> IO (Bytes, ())
forall a b.
Prim a =>
Int -> (MBA# BotanStructT -> IO b) -> IO (PrimVector a, b)
allocPrimVectorUnsafe Int
inputLen (\ MBA# BotanStructT
pbuf ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (BotanStructT
-> BA# Word8 -> Int -> MBA# BotanStructT -> Int -> IO CInt
hs_botan_block_cipher_encrypt_blocks
BotanStructT
pbc BA# Word8
pb Int
pboff MBA# BotanStructT
pbuf Int
n))
decryptBlocks :: HasCallStack
=> BlockCipher
-> V.Bytes
-> Int
-> IO V.Bytes
{-# INLINABLE decryptBlocks #-}
decryptBlocks :: BlockCipher -> Bytes -> Int -> IO Bytes
decryptBlocks (BlockCipher BotanStruct
bc CBytes
_ Int
blockSiz KeySpec
_) Bytes
blocks Int
n = do
let inputLen :: Int
inputLen = Bytes -> Int
forall (v :: * -> *) a. Vec v a => v a -> Int
V.length Bytes
blocks
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
inputLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
blockSiz Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
CInt -> IO ()
forall x. HasCallStack => CInt -> IO x
throwBotanError CInt
BOTAN_FFI_ERROR_INVALID_INPUT
BotanStruct -> (BotanStructT -> IO Bytes) -> IO Bytes
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
bc ((BotanStructT -> IO Bytes) -> IO Bytes)
-> (BotanStructT -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pbc -> do
Bytes -> (BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe Bytes
blocks ((BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes)
-> (BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
pb Int
pboff Int
_ ->
(Bytes, ()) -> Bytes
forall a b. (a, b) -> a
fst ((Bytes, ()) -> Bytes) -> IO (Bytes, ()) -> IO Bytes
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> (MBA# BotanStructT -> IO ()) -> IO (Bytes, ())
forall a b.
Prim a =>
Int -> (MBA# BotanStructT -> IO b) -> IO (PrimVector a, b)
allocPrimVectorUnsafe Int
inputLen (\ MBA# BotanStructT
pbuf ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (BotanStructT
-> BA# Word8 -> Int -> MBA# BotanStructT -> Int -> IO CInt
hs_botan_block_cipher_decrypt_blocks
BotanStructT
pbc BA# Word8
pb Int
pboff MBA# BotanStructT
pbuf Int
n))
data CipherMode
= ChaCha20Poly1305
| GCM BlockCipherType
| GCM' BlockCipherType Int
| OCB BlockCipherType
| OCB' BlockCipherType Int
| EAX BlockCipherType
| EAX' BlockCipherType Int
| SIV BlockCipherType
| CCM BlockCipherType
| CCM' BlockCipherType
Int
Int
| CFB BlockCipherType
| CFB' BlockCipherType
Int
| XTS BlockCipherType
| CBC_PKCS7 BlockCipherType
| CBC_OneAndZeros BlockCipherType
| CBC_X9'23 BlockCipherType
| CBC_ESP BlockCipherType
| CBC_CTS BlockCipherType
| CBC_NoPadding BlockCipherType
deriving (Int -> CipherMode -> ShowS
[CipherMode] -> ShowS
CipherMode -> String
(Int -> CipherMode -> ShowS)
-> (CipherMode -> String)
-> ([CipherMode] -> ShowS)
-> Show CipherMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CipherMode] -> ShowS
$cshowList :: [CipherMode] -> ShowS
show :: CipherMode -> String
$cshow :: CipherMode -> String
showsPrec :: Int -> CipherMode -> ShowS
$cshowsPrec :: Int -> CipherMode -> ShowS
Show, ReadPrec [CipherMode]
ReadPrec CipherMode
Int -> ReadS CipherMode
ReadS [CipherMode]
(Int -> ReadS CipherMode)
-> ReadS [CipherMode]
-> ReadPrec CipherMode
-> ReadPrec [CipherMode]
-> Read CipherMode
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CipherMode]
$creadListPrec :: ReadPrec [CipherMode]
readPrec :: ReadPrec CipherMode
$creadPrec :: ReadPrec CipherMode
readList :: ReadS [CipherMode]
$creadList :: ReadS [CipherMode]
readsPrec :: Int -> ReadS CipherMode
$creadsPrec :: Int -> ReadS CipherMode
Read, CipherMode -> CipherMode -> Bool
(CipherMode -> CipherMode -> Bool)
-> (CipherMode -> CipherMode -> Bool) -> Eq CipherMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CipherMode -> CipherMode -> Bool
$c/= :: CipherMode -> CipherMode -> Bool
== :: CipherMode -> CipherMode -> Bool
$c== :: CipherMode -> CipherMode -> Bool
Eq, Eq CipherMode
Eq CipherMode
-> (CipherMode -> CipherMode -> Ordering)
-> (CipherMode -> CipherMode -> Bool)
-> (CipherMode -> CipherMode -> Bool)
-> (CipherMode -> CipherMode -> Bool)
-> (CipherMode -> CipherMode -> Bool)
-> (CipherMode -> CipherMode -> CipherMode)
-> (CipherMode -> CipherMode -> CipherMode)
-> Ord CipherMode
CipherMode -> CipherMode -> Bool
CipherMode -> CipherMode -> Ordering
CipherMode -> CipherMode -> CipherMode
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CipherMode -> CipherMode -> CipherMode
$cmin :: CipherMode -> CipherMode -> CipherMode
max :: CipherMode -> CipherMode -> CipherMode
$cmax :: CipherMode -> CipherMode -> CipherMode
>= :: CipherMode -> CipherMode -> Bool
$c>= :: CipherMode -> CipherMode -> Bool
> :: CipherMode -> CipherMode -> Bool
$c> :: CipherMode -> CipherMode -> Bool
<= :: CipherMode -> CipherMode -> Bool
$c<= :: CipherMode -> CipherMode -> Bool
< :: CipherMode -> CipherMode -> Bool
$c< :: CipherMode -> CipherMode -> Bool
compare :: CipherMode -> CipherMode -> Ordering
$ccompare :: CipherMode -> CipherMode -> Ordering
$cp1Ord :: Eq CipherMode
Ord, (forall x. CipherMode -> Rep CipherMode x)
-> (forall x. Rep CipherMode x -> CipherMode) -> Generic CipherMode
forall x. Rep CipherMode x -> CipherMode
forall x. CipherMode -> Rep CipherMode x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CipherMode x -> CipherMode
$cfrom :: forall x. CipherMode -> Rep CipherMode x
Generic)
deriving anyclass (Int -> CipherMode -> Builder ()
(Int -> CipherMode -> Builder ()) -> Print CipherMode
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> CipherMode -> Builder ()
$ctoUTF8BuilderP :: Int -> CipherMode -> Builder ()
T.Print, Value -> Converter CipherMode
CipherMode -> Value
CipherMode -> Builder ()
(Value -> Converter CipherMode)
-> (CipherMode -> Value)
-> (CipherMode -> Builder ())
-> JSON CipherMode
forall a.
(Value -> Converter a)
-> (a -> Value) -> (a -> Builder ()) -> JSON a
encodeJSON :: CipherMode -> Builder ()
$cencodeJSON :: CipherMode -> Builder ()
toValue :: CipherMode -> Value
$ctoValue :: CipherMode -> Value
fromValue :: Value -> Converter CipherMode
$cfromValue :: Value -> Converter CipherMode
JSON)
cipherTypeToCBytes :: CipherMode -> CBytes
{-# INLINABLE cipherTypeToCBytes #-}
cipherTypeToCBytes :: CipherMode -> CBytes
cipherTypeToCBytes CipherMode
ct = case CipherMode
ct of
CipherMode
ChaCha20Poly1305 -> CBytes
"ChaCha20Poly1305"
GCM BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/GCM"
GCM' BlockCipherType
bct Int
tagsiz -> [CBytes] -> CBytes
CB.concat [ BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct
, CBytes
"/GCM("
, Text -> CBytes
CB.fromText (Int -> Text
forall a. Print a => a -> Text
T.toText Int
tagsiz)
, CBytes
")"
]
OCB BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/OCB"
OCB' BlockCipherType
bct Int
tagsiz -> [CBytes] -> CBytes
CB.concat [ BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct
, CBytes
"/OCB("
, Text -> CBytes
CB.fromText (Int -> Text
forall a. Print a => a -> Text
T.toText Int
tagsiz)
, CBytes
")"
]
EAX BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/EAX"
EAX' BlockCipherType
bct Int
tagsiz -> [CBytes] -> CBytes
CB.concat [ BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct
, CBytes
"/EAX("
, Text -> CBytes
CB.fromText (Int -> Text
forall a. Print a => a -> Text
T.toText Int
tagsiz)
, CBytes
")"
]
SIV BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/SIV"
CCM BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CCM"
CCM' BlockCipherType
bct Int
tagsiz Int
l -> [CBytes] -> CBytes
CB.concat [ BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct
, CBytes
"/CCM("
, Text -> CBytes
CB.fromText (Int -> Text
forall a. Print a => a -> Text
T.toText Int
tagsiz)
, CBytes
","
, Text -> CBytes
CB.fromText (Int -> Text
forall a. Print a => a -> Text
T.toText Int
l)
, CBytes
")"
]
CFB BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CFB"
CFB' BlockCipherType
bct Int
x -> [CBytes] -> CBytes
CB.concat [ BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct
, CBytes
"/CFB("
, Text -> CBytes
CB.fromText (Int -> Text
forall a. Print a => a -> Text
T.toText Int
x)
, CBytes
")"
]
XTS BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/XTS"
CBC_PKCS7 BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CBC/PKCS7"
CBC_OneAndZeros BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CBC/OneAndZeros"
CBC_X9'23 BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CBC/X9.23"
CBC_ESP BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CBC/ESP"
CBC_CTS BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CBC/CTS"
CBC_NoPadding BlockCipherType
bct -> BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
bct CBytes -> CBytes -> CBytes
forall a. Semigroup a => a -> a -> a
<> CBytes
"/CBC/NoPadding"
data Cipher = Cipher
{ Cipher -> BotanStruct
cipher :: {-# UNPACK #-} !BotanStruct
, Cipher -> CBytes
cipherName :: {-# UNPACK #-} !CBytes
, Cipher -> KeySpec
cipherKeySpec :: {-# UNPACK #-} !KeySpec
, Cipher -> Int
cipherTagLength :: {-# UNPACK #-} !Int
, Cipher -> Int
defaultNonceLength :: {-# UNPACK #-} !Int
}
deriving (Int -> Cipher -> ShowS
[Cipher] -> ShowS
Cipher -> String
(Int -> Cipher -> ShowS)
-> (Cipher -> String) -> ([Cipher] -> ShowS) -> Show Cipher
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Cipher] -> ShowS
$cshowList :: [Cipher] -> ShowS
show :: Cipher -> String
$cshow :: Cipher -> String
showsPrec :: Int -> Cipher -> ShowS
$cshowsPrec :: Int -> Cipher -> ShowS
Show, (forall x. Cipher -> Rep Cipher x)
-> (forall x. Rep Cipher x -> Cipher) -> Generic Cipher
forall x. Rep Cipher x -> Cipher
forall x. Cipher -> Rep Cipher x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Cipher x -> Cipher
$cfrom :: forall x. Cipher -> Rep Cipher x
Generic)
deriving anyclass Int -> Cipher -> Builder ()
(Int -> Cipher -> Builder ()) -> Print Cipher
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> Cipher -> Builder ()
$ctoUTF8BuilderP :: Int -> Cipher -> Builder ()
T.Print
withCipher :: Cipher -> (BotanStructT -> IO r) -> IO r
{-# INLINABLE withCipher #-}
withCipher :: Cipher -> (BotanStructT -> IO r) -> IO r
withCipher (Cipher BotanStruct
c CBytes
_ KeySpec
_ Int
_ Int
_) = BotanStruct -> (BotanStructT -> IO r) -> IO r
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
c
newCipher :: HasCallStack => CipherMode -> CipherDirection -> IO Cipher
{-# INLINABLE newCipher #-}
newCipher :: CipherMode -> CipherDirection -> IO Cipher
newCipher CipherMode
typ CipherDirection
dir = do
let name :: CBytes
name = CipherMode -> CBytes
cipherTypeToCBytes CipherMode
typ
BotanStruct
ci <- (MBA# BotanStructT -> IO CInt)
-> FunPtr (BotanStructT -> IO ()) -> IO BotanStruct
forall a.
HasCallStack =>
(MBA# BotanStructT -> IO CInt)
-> FunPtr (BotanStructT -> IO a) -> IO BotanStruct
newBotanStruct
(\ MBA# BotanStructT
bts -> CBytes -> (BA# Word8 -> IO CInt) -> IO CInt
forall a. CBytes -> (BA# Word8 -> IO a) -> IO a
withCBytesUnsafe CBytes
name ((BA# Word8 -> IO CInt) -> IO CInt)
-> (BA# Word8 -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
pb ->
MBA# BotanStructT -> BA# Word8 -> Word32 -> IO CInt
botan_cipher_init MBA# BotanStructT
bts BA# Word8
pb (CipherDirection -> Word32
cipherDirectionToFlag CipherDirection
dir))
FunPtr (BotanStructT -> IO ())
botan_cipher_destroy
BotanStruct -> (BotanStructT -> IO Cipher) -> IO Cipher
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
ci ((BotanStructT -> IO Cipher) -> IO Cipher)
-> (BotanStructT -> IO Cipher) -> IO Cipher
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pci -> do
(Int
a, (Int
b, (Int
c, ()
_))) <- (MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ())))
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ()))))
-> (MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ())))
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pa ->
(MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ()))
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ())))
-> (MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ()))
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pb ->
(MBA# BotanStructT -> IO ()) -> IO (Int, ())
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO ()) -> IO (Int, ()))
-> (MBA# BotanStructT -> IO ()) -> IO (Int, ())
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pc ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_
(BotanStructT
-> MBA# BotanStructT
-> MBA# BotanStructT
-> MBA# BotanStructT
-> IO CInt
botan_cipher_get_keyspec BotanStructT
pci MBA# BotanStructT
pa MBA# BotanStructT
pb MBA# BotanStructT
pc)
(Int
t, CInt
_) <- (MBA# BotanStructT -> IO CInt) -> IO (Int, CInt)
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO CInt) -> IO (Int, CInt))
-> (MBA# BotanStructT -> IO CInt) -> IO (Int, CInt)
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pt ->
BotanStructT -> MBA# BotanStructT -> IO CInt
botan_cipher_get_tag_length BotanStructT
pci MBA# BotanStructT
pt
(Int
n, CInt
_) <- (MBA# BotanStructT -> IO CInt) -> IO (Int, CInt)
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO CInt) -> IO (Int, CInt))
-> (MBA# BotanStructT -> IO CInt) -> IO (Int, CInt)
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pn ->
BotanStructT -> MBA# BotanStructT -> IO CInt
botan_cipher_get_default_nonce_length BotanStructT
pci MBA# BotanStructT
pn
Cipher -> IO Cipher
forall (m :: * -> *) a. Monad m => a -> m a
return (BotanStruct -> CBytes -> KeySpec -> Int -> Int -> Cipher
Cipher BotanStruct
ci CBytes
name (Int -> Int -> Int -> KeySpec
KeySpec Int
a Int
b Int
c) Int
t Int
n)
clearCipher :: HasCallStack => Cipher -> IO ()
{-# INLINABLE clearCipher #-}
clearCipher :: Cipher -> IO ()
clearCipher Cipher
ci = Cipher -> (BotanStructT -> IO ()) -> IO ()
forall r. Cipher -> (BotanStructT -> IO r) -> IO r
withCipher Cipher
ci (IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (IO CInt -> IO ())
-> (BotanStructT -> IO CInt) -> BotanStructT -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BotanStructT -> IO CInt
botan_cipher_clear)
setCipherKey :: HasCallStack => Cipher -> Secret -> IO ()
{-# INLINABLE setCipherKey #-}
setCipherKey :: Cipher -> Secret -> IO ()
setCipherKey Cipher
ci Secret
key =
Cipher -> (BotanStructT -> IO ()) -> IO ()
forall r. Cipher -> (BotanStructT -> IO r) -> IO r
withCipher Cipher
ci ((BotanStructT -> IO ()) -> IO ())
-> (BotanStructT -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pci -> do
Secret -> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall r. Secret -> (Ptr Word8 -> CSize -> IO r) -> IO r
withSecret Secret
key ((Ptr Word8 -> CSize -> IO ()) -> IO ())
-> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
pkey CSize
key_len -> do
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (BotanStructT -> Ptr Word8 -> CSize -> IO CInt
botan_cipher_set_key BotanStructT
pci Ptr Word8
pkey CSize
key_len)
runCipher :: HasCallStack
=> Cipher
-> Nonce
-> V.Bytes
-> V.Bytes
-> IO V.Bytes
{-# INLINABLE runCipher #-}
runCipher :: Cipher -> Bytes -> Bytes -> Bytes -> IO Bytes
runCipher Cipher
ci Bytes
nonce Bytes
inp Bytes
ad =
Cipher -> (BotanStructT -> IO Bytes) -> IO Bytes
forall r. Cipher -> (BotanStructT -> IO r) -> IO r
withCipher Cipher
ci ((BotanStructT -> IO Bytes) -> IO Bytes)
-> (BotanStructT -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pci -> do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Cipher -> Int
cipherTagLength Cipher
ci Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) (IO () -> IO ())
-> ((BA# Word8 -> Int -> Int -> IO ()) -> IO ())
-> (BA# Word8 -> Int -> Int -> IO ())
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes -> (BA# Word8 -> Int -> Int -> IO ()) -> IO ()
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe Bytes
ad ((BA# Word8 -> Int -> Int -> IO ()) -> IO ())
-> (BA# Word8 -> Int -> Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
pad Int
ad_off Int
ad_len -> do
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (BotanStructT -> BA# Word8 -> Int -> Int -> IO CInt
hs_botan_cipher_set_associated_data
BotanStructT
pci BA# Word8
pad Int
ad_off Int
ad_len)
Bytes -> (BA# Word8 -> Int -> Int -> IO ()) -> IO ()
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe Bytes
nonce ((BA# Word8 -> Int -> Int -> IO ()) -> IO ())
-> (BA# Word8 -> Int -> Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
pnonce Int
nonce_off Int
nonce_len ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (BotanStructT -> BA# Word8 -> Int -> Int -> IO CInt
hs_botan_cipher_start BotanStructT
pci BA# Word8
pnonce Int
nonce_off Int
nonce_len)
Int
osiz <- BotanStructT -> Int -> IO Int
hs_botan_cipher_output_size BotanStructT
pci (Bytes -> Int
forall (v :: * -> *) a. Vec v a => v a -> Int
V.length Bytes
inp)
(Bytes
out, Int
r) <- Int -> (MBA# BotanStructT -> IO Int) -> IO (Bytes, Int)
forall a b.
Prim a =>
Int -> (MBA# BotanStructT -> IO b) -> IO (PrimVector a, b)
allocPrimVectorUnsafe Int
osiz ((MBA# BotanStructT -> IO Int) -> IO (Bytes, Int))
-> (MBA# BotanStructT -> IO Int) -> IO (Bytes, Int)
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
out ->
Bytes -> (BA# Word8 -> Int -> Int -> IO Int) -> IO Int
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe Bytes
inp ((BA# Word8 -> Int -> Int -> IO Int) -> IO Int)
-> (BA# Word8 -> Int -> Int -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
pinp Int
inp_off Int
inp_len ->
IO Int -> IO Int
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwBotanIfMinus (BotanStructT
-> MBA# BotanStructT -> Int -> BA# Word8 -> Int -> Int -> IO Int
hs_botan_cipher_finish BotanStructT
pci MBA# BotanStructT
out Int
osiz BA# Word8
pinp Int
inp_off Int
inp_len)
Bytes -> IO Bytes
forall (m :: * -> *) a. Monad m => a -> m a
return (Bytes -> IO Bytes) -> Bytes -> IO Bytes
forall a b. (a -> b) -> a -> b
$! Int -> Bytes -> Bytes
forall (v :: * -> *) a. Vec v a => Int -> v a -> v a
V.unsafeTake Int
r Bytes
out
data StreamCipherType
= CTR_BE BlockCipherType
| OFB BlockCipherType
| ChaCha8
| ChaCha12
| ChaCha20
| Salsa20
| SHAKE128'
| RC4
deriving (Int -> StreamCipherType -> ShowS
[StreamCipherType] -> ShowS
StreamCipherType -> String
(Int -> StreamCipherType -> ShowS)
-> (StreamCipherType -> String)
-> ([StreamCipherType] -> ShowS)
-> Show StreamCipherType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StreamCipherType] -> ShowS
$cshowList :: [StreamCipherType] -> ShowS
show :: StreamCipherType -> String
$cshow :: StreamCipherType -> String
showsPrec :: Int -> StreamCipherType -> ShowS
$cshowsPrec :: Int -> StreamCipherType -> ShowS
Show, ReadPrec [StreamCipherType]
ReadPrec StreamCipherType
Int -> ReadS StreamCipherType
ReadS [StreamCipherType]
(Int -> ReadS StreamCipherType)
-> ReadS [StreamCipherType]
-> ReadPrec StreamCipherType
-> ReadPrec [StreamCipherType]
-> Read StreamCipherType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [StreamCipherType]
$creadListPrec :: ReadPrec [StreamCipherType]
readPrec :: ReadPrec StreamCipherType
$creadPrec :: ReadPrec StreamCipherType
readList :: ReadS [StreamCipherType]
$creadList :: ReadS [StreamCipherType]
readsPrec :: Int -> ReadS StreamCipherType
$creadsPrec :: Int -> ReadS StreamCipherType
Read, StreamCipherType -> StreamCipherType -> Bool
(StreamCipherType -> StreamCipherType -> Bool)
-> (StreamCipherType -> StreamCipherType -> Bool)
-> Eq StreamCipherType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StreamCipherType -> StreamCipherType -> Bool
$c/= :: StreamCipherType -> StreamCipherType -> Bool
== :: StreamCipherType -> StreamCipherType -> Bool
$c== :: StreamCipherType -> StreamCipherType -> Bool
Eq, Eq StreamCipherType
Eq StreamCipherType
-> (StreamCipherType -> StreamCipherType -> Ordering)
-> (StreamCipherType -> StreamCipherType -> Bool)
-> (StreamCipherType -> StreamCipherType -> Bool)
-> (StreamCipherType -> StreamCipherType -> Bool)
-> (StreamCipherType -> StreamCipherType -> Bool)
-> (StreamCipherType -> StreamCipherType -> StreamCipherType)
-> (StreamCipherType -> StreamCipherType -> StreamCipherType)
-> Ord StreamCipherType
StreamCipherType -> StreamCipherType -> Bool
StreamCipherType -> StreamCipherType -> Ordering
StreamCipherType -> StreamCipherType -> StreamCipherType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: StreamCipherType -> StreamCipherType -> StreamCipherType
$cmin :: StreamCipherType -> StreamCipherType -> StreamCipherType
max :: StreamCipherType -> StreamCipherType -> StreamCipherType
$cmax :: StreamCipherType -> StreamCipherType -> StreamCipherType
>= :: StreamCipherType -> StreamCipherType -> Bool
$c>= :: StreamCipherType -> StreamCipherType -> Bool
> :: StreamCipherType -> StreamCipherType -> Bool
$c> :: StreamCipherType -> StreamCipherType -> Bool
<= :: StreamCipherType -> StreamCipherType -> Bool
$c<= :: StreamCipherType -> StreamCipherType -> Bool
< :: StreamCipherType -> StreamCipherType -> Bool
$c< :: StreamCipherType -> StreamCipherType -> Bool
compare :: StreamCipherType -> StreamCipherType -> Ordering
$ccompare :: StreamCipherType -> StreamCipherType -> Ordering
$cp1Ord :: Eq StreamCipherType
Ord, (forall x. StreamCipherType -> Rep StreamCipherType x)
-> (forall x. Rep StreamCipherType x -> StreamCipherType)
-> Generic StreamCipherType
forall x. Rep StreamCipherType x -> StreamCipherType
forall x. StreamCipherType -> Rep StreamCipherType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep StreamCipherType x -> StreamCipherType
$cfrom :: forall x. StreamCipherType -> Rep StreamCipherType x
Generic)
deriving anyclass (Int -> StreamCipherType -> Builder ()
(Int -> StreamCipherType -> Builder ()) -> Print StreamCipherType
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> StreamCipherType -> Builder ()
$ctoUTF8BuilderP :: Int -> StreamCipherType -> Builder ()
T.Print, Value -> Converter StreamCipherType
StreamCipherType -> Value
StreamCipherType -> Builder ()
(Value -> Converter StreamCipherType)
-> (StreamCipherType -> Value)
-> (StreamCipherType -> Builder ())
-> JSON StreamCipherType
forall a.
(Value -> Converter a)
-> (a -> Value) -> (a -> Builder ()) -> JSON a
encodeJSON :: StreamCipherType -> Builder ()
$cencodeJSON :: StreamCipherType -> Builder ()
toValue :: StreamCipherType -> Value
$ctoValue :: StreamCipherType -> Value
fromValue :: Value -> Converter StreamCipherType
$cfromValue :: Value -> Converter StreamCipherType
JSON)
data StreamCipher = StreamCipher
{ StreamCipher -> BotanStruct
streamCipher :: {-# UNPACK #-} !BotanStruct
, StreamCipher -> CBytes
streamCipherName :: {-# UNPACK #-} !CBytes
, StreamCipher -> KeySpec
streamCipherKeySpec :: {-# UNPACK #-} !KeySpec
, StreamCipher -> Int
defaultIVLength :: {-# UNPACK #-} !Int
}
deriving (Int -> StreamCipher -> ShowS
[StreamCipher] -> ShowS
StreamCipher -> String
(Int -> StreamCipher -> ShowS)
-> (StreamCipher -> String)
-> ([StreamCipher] -> ShowS)
-> Show StreamCipher
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StreamCipher] -> ShowS
$cshowList :: [StreamCipher] -> ShowS
show :: StreamCipher -> String
$cshow :: StreamCipher -> String
showsPrec :: Int -> StreamCipher -> ShowS
$cshowsPrec :: Int -> StreamCipher -> ShowS
Show, (forall x. StreamCipher -> Rep StreamCipher x)
-> (forall x. Rep StreamCipher x -> StreamCipher)
-> Generic StreamCipher
forall x. Rep StreamCipher x -> StreamCipher
forall x. StreamCipher -> Rep StreamCipher x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep StreamCipher x -> StreamCipher
$cfrom :: forall x. StreamCipher -> Rep StreamCipher x
Generic)
deriving anyclass Int -> StreamCipher -> Builder ()
(Int -> StreamCipher -> Builder ()) -> Print StreamCipher
forall a. (Int -> a -> Builder ()) -> Print a
toUTF8BuilderP :: Int -> StreamCipher -> Builder ()
$ctoUTF8BuilderP :: Int -> StreamCipher -> Builder ()
T.Print
withStreamCipher :: StreamCipher -> (BotanStructT -> IO r) -> IO r
{-# INLINE withStreamCipher #-}
withStreamCipher :: StreamCipher -> (BotanStructT -> IO r) -> IO r
withStreamCipher (StreamCipher BotanStruct
sci CBytes
_ KeySpec
_ Int
_) = BotanStruct -> (BotanStructT -> IO r) -> IO r
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
sci
streamCipherTypeToCBytes :: StreamCipherType -> CBytes
{-# INLINABLE streamCipherTypeToCBytes #-}
streamCipherTypeToCBytes :: StreamCipherType -> CBytes
streamCipherTypeToCBytes StreamCipherType
s = case StreamCipherType
s of
CTR_BE BlockCipherType
b -> [CBytes] -> CBytes
CB.concat [CBytes
"CTR-BE(", BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
b, CBytes
")"]
OFB BlockCipherType
b -> [CBytes] -> CBytes
CB.concat [CBytes
"OFB(", BlockCipherType -> CBytes
blockCipherTypeToCBytes BlockCipherType
b, CBytes
")"]
StreamCipherType
ChaCha8 -> CBytes
"ChaCha(8)"
StreamCipherType
ChaCha12 -> CBytes
"ChaCha(12)"
StreamCipherType
ChaCha20 -> CBytes
"ChaCha(20)"
StreamCipherType
Salsa20 -> CBytes
"Salsa20"
StreamCipherType
SHAKE128' -> CBytes
"SHAKE-128"
StreamCipherType
RC4 -> CBytes
"RC4"
newStreamCipher :: HasCallStack => StreamCipherType -> IO StreamCipher
{-# INLINABLE newStreamCipher #-}
newStreamCipher :: StreamCipherType -> IO StreamCipher
newStreamCipher StreamCipherType
typ = do
let name :: CBytes
name = StreamCipherType -> CBytes
streamCipherTypeToCBytes StreamCipherType
typ
BotanStruct
ci <- (MBA# BotanStructT -> IO CInt)
-> FunPtr (BotanStructT -> IO ()) -> IO BotanStruct
forall a.
HasCallStack =>
(MBA# BotanStructT -> IO CInt)
-> FunPtr (BotanStructT -> IO a) -> IO BotanStruct
newBotanStruct
(\ MBA# BotanStructT
bts -> CBytes -> (BA# Word8 -> IO CInt) -> IO CInt
forall a. CBytes -> (BA# Word8 -> IO a) -> IO a
withCBytesUnsafe CBytes
name (MBA# BotanStructT -> BA# Word8 -> IO CInt
botan_stream_cipher_init MBA# BotanStructT
bts))
FunPtr (BotanStructT -> IO ())
botan_stream_cipher_destroy
BotanStruct -> (BotanStructT -> IO StreamCipher) -> IO StreamCipher
forall a. BotanStruct -> (BotanStructT -> IO a) -> IO a
withBotanStruct BotanStruct
ci ((BotanStructT -> IO StreamCipher) -> IO StreamCipher)
-> (BotanStructT -> IO StreamCipher) -> IO StreamCipher
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pci -> do
(Int
a, (Int
b, (Int
c, ()
_))) <- (MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ())))
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ()))))
-> (MBA# BotanStructT -> IO (Int, (Int, ())))
-> IO (Int, (Int, (Int, ())))
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pa ->
(MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ()))
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ())))
-> (MBA# BotanStructT -> IO (Int, ())) -> IO (Int, (Int, ()))
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pb ->
(MBA# BotanStructT -> IO ()) -> IO (Int, ())
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO ()) -> IO (Int, ()))
-> (MBA# BotanStructT -> IO ()) -> IO (Int, ())
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pc ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_
(BotanStructT
-> MBA# BotanStructT
-> MBA# BotanStructT
-> MBA# BotanStructT
-> IO CInt
botan_stream_cipher_get_keyspec BotanStructT
pci MBA# BotanStructT
pa MBA# BotanStructT
pb MBA# BotanStructT
pc)
(Int
n, CInt
_) <- (MBA# BotanStructT -> IO CInt) -> IO (Int, CInt)
forall a b. Prim a => (MBA# BotanStructT -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# BotanStructT -> IO CInt) -> IO (Int, CInt))
-> (MBA# BotanStructT -> IO CInt) -> IO (Int, CInt)
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
pn ->
BotanStructT -> MBA# BotanStructT -> IO CInt
botan_stream_cipher_get_default_iv_length BotanStructT
pci MBA# BotanStructT
pn
StreamCipher -> IO StreamCipher
forall (m :: * -> *) a. Monad m => a -> m a
return (BotanStruct -> CBytes -> KeySpec -> Int -> StreamCipher
StreamCipher BotanStruct
ci CBytes
name (Int -> Int -> Int -> KeySpec
KeySpec Int
a Int
b Int
c) Int
n)
setStreamCipherKey :: HasCallStack => StreamCipher -> Secret -> IO ()
{-# INLINABLE setStreamCipherKey #-}
setStreamCipherKey :: StreamCipher -> Secret -> IO ()
setStreamCipherKey StreamCipher
ci Secret
key =
StreamCipher -> (BotanStructT -> IO ()) -> IO ()
forall r. StreamCipher -> (BotanStructT -> IO r) -> IO r
withStreamCipher StreamCipher
ci ((BotanStructT -> IO ()) -> IO ())
-> (BotanStructT -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pci ->
Secret -> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall r. Secret -> (Ptr Word8 -> CSize -> IO r) -> IO r
withSecret Secret
key ((Ptr Word8 -> CSize -> IO ()) -> IO ())
-> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
pkey CSize
key_len ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (BotanStructT -> Ptr Word8 -> CSize -> IO CInt
botan_stream_cipher_set_key BotanStructT
pci Ptr Word8
pkey CSize
key_len)
setStreamCipherIV :: StreamCipher -> Nonce -> IO ()
{-# INLINABLE setStreamCipherIV #-}
setStreamCipherIV :: StreamCipher -> Bytes -> IO ()
setStreamCipherIV StreamCipher
sc Bytes
nonce =
StreamCipher -> (BotanStructT -> IO ()) -> IO ()
forall r. StreamCipher -> (BotanStructT -> IO r) -> IO r
withStreamCipher StreamCipher
sc ((BotanStructT -> IO ()) -> IO ())
-> (BotanStructT -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
psc ->
Bytes -> (BA# Word8 -> Int -> Int -> IO ()) -> IO ()
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe Bytes
nonce ((BA# Word8 -> Int -> Int -> IO ()) -> IO ())
-> (BA# Word8 -> Int -> Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
n Int
noff Int
nlen ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanStructT -> BA# Word8 -> Int -> Int -> IO CInt
hs_botan_stream_cipher_set_iv BotanStructT
psc BA# Word8
n Int
noff Int
nlen
clearStreamCipher :: HasCallStack => StreamCipher -> IO ()
{-# INLINABLE clearStreamCipher #-}
clearStreamCipher :: StreamCipher -> IO ()
clearStreamCipher StreamCipher
ci = StreamCipher -> (BotanStructT -> IO ()) -> IO ()
forall r. StreamCipher -> (BotanStructT -> IO r) -> IO r
withStreamCipher StreamCipher
ci (IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (IO CInt -> IO ())
-> (BotanStructT -> IO CInt) -> BotanStructT -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BotanStructT -> IO CInt
botan_stream_cipher_clear)
seekStreamCipher :: StreamCipher -> Int -> IO ()
{-# INLINABLE seekStreamCipher #-}
seekStreamCipher :: StreamCipher -> Int -> IO ()
seekStreamCipher StreamCipher
sc Int
off =
StreamCipher -> (BotanStructT -> IO ()) -> IO ()
forall r. StreamCipher -> (BotanStructT -> IO r) -> IO r
withStreamCipher StreamCipher
sc ((BotanStructT -> IO ()) -> IO ())
-> (BotanStructT -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
psc ->
IO CInt -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwBotanIfMinus_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanStructT -> CSize -> IO CInt
botan_stream_cipher_seek BotanStructT
psc (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
off)
runStreamCipher :: HasCallStack
=> StreamCipher
-> V.Bytes
-> IO V.Bytes
{-# INLINABLE runStreamCipher #-}
runStreamCipher :: StreamCipher -> Bytes -> IO Bytes
runStreamCipher StreamCipher
sci Bytes
input =
StreamCipher -> (BotanStructT -> IO Bytes) -> IO Bytes
forall r. StreamCipher -> (BotanStructT -> IO r) -> IO r
withStreamCipher StreamCipher
sci ((BotanStructT -> IO Bytes) -> IO Bytes)
-> (BotanStructT -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pci -> do
Bytes -> (BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe Bytes
input ((BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes)
-> (BA# Word8 -> Int -> Int -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
in_p Int
in_off Int
in_len -> do
(Bytes
out, CInt
_) <- Int -> (MBA# BotanStructT -> IO CInt) -> IO (Bytes, CInt)
forall a b.
Prim a =>
Int -> (MBA# BotanStructT -> IO b) -> IO (PrimVector a, b)
allocPrimVectorUnsafe Int
in_len ((MBA# BotanStructT -> IO CInt) -> IO (Bytes, CInt))
-> (MBA# BotanStructT -> IO CInt) -> IO (Bytes, CInt)
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
out_p ->
IO CInt -> IO CInt
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwBotanIfMinus (BotanStructT
-> MBA# BotanStructT -> BA# Word8 -> Int -> Int -> IO CInt
hs_botan_stream_cipher_cipher BotanStructT
pci
MBA# BotanStructT
out_p BA# Word8
in_p Int
in_off Int
in_len)
Bytes -> IO Bytes
forall (m :: * -> *) a. Monad m => a -> m a
return Bytes
out
streamCipherKeyStream :: HasCallStack
=> StreamCipher
-> Int
-> IO V.Bytes
{-# INLINABLE streamCipherKeyStream #-}
streamCipherKeyStream :: StreamCipher -> Int -> IO Bytes
streamCipherKeyStream StreamCipher
sci Int
siz =
StreamCipher -> (BotanStructT -> IO Bytes) -> IO Bytes
forall r. StreamCipher -> (BotanStructT -> IO r) -> IO r
withStreamCipher StreamCipher
sci ((BotanStructT -> IO Bytes) -> IO Bytes)
-> (BotanStructT -> IO Bytes) -> IO Bytes
forall a b. (a -> b) -> a -> b
$ \ BotanStructT
pci -> do
(Bytes
pa, CInt
_) <- Int -> (MBA# BotanStructT -> IO CInt) -> IO (Bytes, CInt)
forall a b.
Prim a =>
Int -> (MBA# BotanStructT -> IO b) -> IO (PrimVector a, b)
allocPrimVectorUnsafe Int
siz ((MBA# BotanStructT -> IO CInt) -> IO (Bytes, CInt))
-> (MBA# BotanStructT -> IO CInt) -> IO (Bytes, CInt)
forall a b. (a -> b) -> a -> b
$ \ MBA# BotanStructT
out_p -> do
IO CInt -> IO CInt
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwBotanIfMinus (BotanStructT -> MBA# BotanStructT -> CSize -> IO CInt
botan_stream_cipher_write_keystream BotanStructT
pci MBA# BotanStructT
out_p (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
siz))
Bytes -> IO Bytes
forall (m :: * -> *) a. Monad m => a -> m a
return Bytes
pa
streamCipherBIO :: HasCallStack => StreamCipher -> BIO V.Bytes V.Bytes
{-# INLINABLE streamCipherBIO #-}
streamCipherBIO :: StreamCipher -> BIO Bytes Bytes
streamCipherBIO StreamCipher
c = \ Maybe Bytes -> IO ()
k Maybe Bytes
mbs -> case Maybe Bytes
mbs of
Just Bytes
chunk -> Maybe Bytes -> IO ()
k (Maybe Bytes -> IO ()) -> (Bytes -> Maybe Bytes) -> Bytes -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes -> Maybe Bytes
forall a. a -> Maybe a
Just (Bytes -> IO ()) -> IO Bytes -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< HasCallStack => StreamCipher -> Bytes -> IO Bytes
StreamCipher -> Bytes -> IO Bytes
runStreamCipher StreamCipher
c Bytes
chunk
Maybe Bytes
_ -> Maybe Bytes -> IO ()
k Maybe Bytes
forall a. Maybe a
EOF
keyStreamSource :: HasCallStack
=> StreamCipher
-> Int
-> Source V.Bytes
{-# INLINABLE keyStreamSource #-}
keyStreamSource :: StreamCipher -> Int -> Source Bytes
keyStreamSource StreamCipher
c Int
cs = \ Maybe Bytes -> IO ()
k Maybe Void
_ -> Maybe Bytes -> IO ()
k (Maybe Bytes -> IO ()) -> (Bytes -> Maybe Bytes) -> Bytes -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes -> Maybe Bytes
forall a. a -> Maybe a
Just (Bytes -> IO ()) -> IO Bytes -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< HasCallStack => StreamCipher -> Int -> IO Bytes
StreamCipher -> Int -> IO Bytes
streamCipherKeyStream StreamCipher
c Int
cs