module Botan.Low.PubKey.KeyAgreement
(
KeyAgreement(..)
, withKeyAgreement
, keyAgreementCreate
, keyAgreementDestroy
, keyAgreementExportPublic
, keyAgreementSize
, keyAgreement
) where
import qualified Data.ByteString as ByteString
import Botan.Bindings.PubKey.KeyAgreement
import Botan.Low.Error
import Botan.Low.KDF
import Botan.Low.Make
import Botan.Low.Prelude
import Botan.Low.PubKey
import Botan.Low.Remake
newtype KeyAgreement = MkKeyAgreement { KeyAgreement -> ForeignPtr BotanPKOpKeyAgreementStruct
getKeyAgreementForeignPtr :: ForeignPtr BotanPKOpKeyAgreementStruct }
newKeyAgreement :: BotanPKOpKeyAgreement -> IO KeyAgreement
withKeyAgreement :: KeyAgreement -> (BotanPKOpKeyAgreement -> IO a) -> IO a
keyAgreementDestroy :: KeyAgreement -> IO ()
createKeyAgreement :: (Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement
(BotanPKOpKeyAgreement -> IO KeyAgreement
newKeyAgreement, KeyAgreement -> (BotanPKOpKeyAgreement -> IO a) -> IO a
withKeyAgreement, KeyAgreement -> IO ()
keyAgreementDestroy, (Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement
createKeyAgreement, (Ptr BotanPKOpKeyAgreement -> Ptr CSize -> IO CInt)
-> IO [KeyAgreement]
_)
= (Ptr BotanPKOpKeyAgreementStruct -> BotanPKOpKeyAgreement)
-> (BotanPKOpKeyAgreement -> Ptr BotanPKOpKeyAgreementStruct)
-> (ForeignPtr BotanPKOpKeyAgreementStruct -> KeyAgreement)
-> (KeyAgreement -> ForeignPtr BotanPKOpKeyAgreementStruct)
-> FinalizerPtr BotanPKOpKeyAgreementStruct
-> (BotanPKOpKeyAgreement -> IO KeyAgreement,
KeyAgreement -> (BotanPKOpKeyAgreement -> IO a) -> IO a,
KeyAgreement -> IO (),
(Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement,
(Ptr BotanPKOpKeyAgreement -> Ptr CSize -> IO CInt)
-> IO [KeyAgreement])
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 BotanPKOpKeyAgreementStruct -> BotanPKOpKeyAgreement
MkBotanPKOpKeyAgreement BotanPKOpKeyAgreement -> Ptr BotanPKOpKeyAgreementStruct
runBotanPKOpKeyAgreement
ForeignPtr BotanPKOpKeyAgreementStruct -> KeyAgreement
MkKeyAgreement KeyAgreement -> ForeignPtr BotanPKOpKeyAgreementStruct
getKeyAgreementForeignPtr
FinalizerPtr BotanPKOpKeyAgreementStruct
botan_pk_op_key_agreement_destroy
keyAgreementCreate
:: PrivKey
-> KDFName
-> IO KeyAgreement
keyAgreementCreate :: PrivKey -> KDFName -> IO KeyAgreement
keyAgreementCreate PrivKey
sk KDFName
algo = PrivKey -> (BotanPrivKey -> IO KeyAgreement) -> IO KeyAgreement
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey PrivKey
sk ((BotanPrivKey -> IO KeyAgreement) -> IO KeyAgreement)
-> (BotanPrivKey -> IO KeyAgreement) -> IO KeyAgreement
forall a b. (a -> b) -> a -> b
$ \ BotanPrivKey
skPtr -> do
KDFName -> (Ptr CChar -> IO KeyAgreement) -> IO KeyAgreement
forall a. KDFName -> (Ptr CChar -> IO a) -> IO a
asCString KDFName
algo ((Ptr CChar -> IO KeyAgreement) -> IO KeyAgreement)
-> (Ptr CChar -> IO KeyAgreement) -> IO KeyAgreement
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
algoPtr -> do
(Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement
createKeyAgreement ((Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement)
-> (Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPKOpKeyAgreement
out -> Ptr BotanPKOpKeyAgreement
-> BotanPrivKey -> ConstPtr CChar -> Word32 -> IO CInt
botan_pk_op_key_agreement_create
Ptr BotanPKOpKeyAgreement
out
BotanPrivKey
skPtr
(Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
algoPtr)
Word32
0
withKeyAgreementCreate :: PrivKey -> KDFName -> (KeyAgreement -> IO a) -> IO a
withKeyAgreementCreate :: forall a. PrivKey -> KDFName -> (KeyAgreement -> IO a) -> IO a
withKeyAgreementCreate = (PrivKey -> KDFName -> IO KeyAgreement)
-> (KeyAgreement -> IO ())
-> PrivKey
-> KDFName
-> (KeyAgreement -> IO a)
-> IO a
forall x y t a.
(x -> y -> IO t) -> (t -> IO ()) -> x -> y -> (t -> IO a) -> IO a
mkWithTemp2 PrivKey -> KDFName -> IO KeyAgreement
keyAgreementCreate KeyAgreement -> IO ()
keyAgreementDestroy
keyAgreementExportPublic
:: PrivKey
-> IO ByteString
keyAgreementExportPublic :: PrivKey -> IO KDFName
keyAgreementExportPublic PrivKey
sk = PrivKey -> (BotanPrivKey -> IO KDFName) -> IO KDFName
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey PrivKey
sk ((BotanPrivKey -> IO KDFName) -> IO KDFName)
-> (BotanPrivKey -> IO KDFName) -> IO KDFName
forall a b. (a -> b) -> a -> b
$ \ BotanPrivKey
skPtr -> do
(Ptr Word8 -> Ptr CSize -> IO CInt) -> IO KDFName
forall byte. (Ptr byte -> Ptr CSize -> IO CInt) -> IO KDFName
allocBytesQuerying ((Ptr Word8 -> Ptr CSize -> IO CInt) -> IO KDFName)
-> (Ptr Word8 -> Ptr CSize -> IO CInt) -> IO KDFName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
outPtr Ptr CSize
outLen -> BotanPrivKey -> Ptr Word8 -> Ptr CSize -> IO CInt
botan_pk_op_key_agreement_export_public
BotanPrivKey
skPtr
Ptr Word8
outPtr
Ptr CSize
outLen
keyAgreementSize
:: KeyAgreement
-> IO Int
keyAgreementSize :: KeyAgreement -> IO Int
keyAgreementSize = WithPtr KeyAgreement BotanPKOpKeyAgreement
-> GetSize BotanPKOpKeyAgreement -> KeyAgreement -> IO Int
forall typ ptr. WithPtr typ ptr -> GetSize ptr -> typ -> IO Int
mkGetSize KeyAgreement -> (BotanPKOpKeyAgreement -> IO a) -> IO a
WithPtr KeyAgreement BotanPKOpKeyAgreement
withKeyAgreement GetSize BotanPKOpKeyAgreement
botan_pk_op_key_agreement_size
{-# WARNING keyAgreement "This function was leaking memory and causing crashes. Please observe carefully and report any future leaks." #-}
keyAgreement
:: KeyAgreement
-> ByteString
-> ByteString
-> IO ByteString
keyAgreement :: KeyAgreement -> KDFName -> KDFName -> IO KDFName
keyAgreement KeyAgreement
ka KDFName
key KDFName
salt = KeyAgreement -> (BotanPKOpKeyAgreement -> IO KDFName) -> IO KDFName
WithPtr KeyAgreement BotanPKOpKeyAgreement
withKeyAgreement KeyAgreement
ka ((BotanPKOpKeyAgreement -> IO KDFName) -> IO KDFName)
-> (BotanPKOpKeyAgreement -> IO KDFName) -> IO KDFName
forall a b. (a -> b) -> a -> b
$ \ BotanPKOpKeyAgreement
kaPtr -> do
KDFName -> (Ptr Word8 -> CSize -> IO KDFName) -> IO KDFName
forall byte a. KDFName -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen KDFName
key ((Ptr Word8 -> CSize -> IO KDFName) -> IO KDFName)
-> (Ptr Word8 -> CSize -> IO KDFName) -> IO KDFName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
keyPtr CSize
keyLen -> do
KDFName -> (Ptr Word8 -> CSize -> IO KDFName) -> IO KDFName
forall byte a. KDFName -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen KDFName
salt ((Ptr Word8 -> CSize -> IO KDFName) -> IO KDFName)
-> (Ptr Word8 -> CSize -> IO KDFName) -> IO KDFName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
saltPtr CSize
saltLen -> do
Int
outSz <- KeyAgreement -> IO Int
keyAgreementSize KeyAgreement
ka
(Ptr CSize -> IO KDFName) -> IO KDFName
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CSize -> IO KDFName) -> IO KDFName)
-> (Ptr CSize -> IO KDFName) -> IO KDFName
forall a b. (a -> b) -> a -> b
$ \ Ptr CSize
szPtr -> do
Ptr CSize -> CSize -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr CSize
szPtr (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
outSz)
KDFName
out <- Int -> (Ptr Word8 -> IO ()) -> IO KDFName
forall byte. Int -> (Ptr byte -> IO ()) -> IO KDFName
allocBytes Int
outSz ((Ptr Word8 -> IO ()) -> IO KDFName)
-> (Ptr Word8 -> IO ()) -> IO KDFName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
outPtr -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanPKOpKeyAgreement
-> Ptr Word8
-> Ptr CSize
-> ConstPtr Word8
-> CSize
-> ConstPtr Word8
-> CSize
-> IO CInt
botan_pk_op_key_agreement
BotanPKOpKeyAgreement
kaPtr
Ptr Word8
outPtr
Ptr CSize
szPtr
(Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
keyPtr)
CSize
keyLen
(Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
saltPtr)
CSize
saltLen
Int
sz <- 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
szPtr
KDFName -> IO KDFName
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (KDFName -> IO KDFName) -> KDFName -> IO KDFName
forall a b. (a -> b) -> a -> b
$! Int -> KDFName -> KDFName
ByteString.take Int
sz KDFName
out