module Botan.Low.PubKey.KeyAgreement (
KeyAgreement(..)
, withKeyAgreement
, keyAgreementCreate
, keyAgreementDestroy
, keyAgreementExportPublic
, keyAgreementSize
, keyAgreement
) where
import Botan.Bindings.ConstPtr (ConstPtr (..))
import Botan.Bindings.PubKey.KeyAgreement
import Botan.Low.Error.Internal
import Botan.Low.Internal.ByteString
import Botan.Low.KDF
import Botan.Low.Make
import Botan.Low.PubKey
import Botan.Low.Remake
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
newtype KeyAgreement = MkKeyAgreement { KeyAgreement -> ForeignPtr BotanPKOpKeyAgreementStruct
foreignPtr :: ForeignPtr BotanPKOpKeyAgreementStruct }
withKeyAgreement :: KeyAgreement -> (BotanPKOpKeyAgreement -> IO a) -> IO a
keyAgreementDestroy :: KeyAgreement -> IO ()
createKeyAgreement :: (Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement
(KeyAgreement -> (BotanPKOpKeyAgreement -> IO a) -> IO a
withKeyAgreement, KeyAgreement -> IO ()
keyAgreementDestroy, (Ptr BotanPKOpKeyAgreement -> IO CInt) -> IO KeyAgreement
createKeyAgreement)
= (Ptr BotanPKOpKeyAgreementStruct -> BotanPKOpKeyAgreement)
-> (BotanPKOpKeyAgreement -> Ptr BotanPKOpKeyAgreementStruct)
-> (ForeignPtr BotanPKOpKeyAgreementStruct -> KeyAgreement)
-> (KeyAgreement -> ForeignPtr BotanPKOpKeyAgreementStruct)
-> FinalizerPtr BotanPKOpKeyAgreementStruct
-> (KeyAgreement -> (BotanPKOpKeyAgreement -> IO a) -> IO a,
KeyAgreement -> IO (),
(Ptr BotanPKOpKeyAgreement -> 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
-> (object -> (botan -> IO a) -> IO a, object -> IO (),
(Ptr botan -> IO CInt) -> IO object)
mkBindings
Ptr BotanPKOpKeyAgreementStruct -> BotanPKOpKeyAgreement
MkBotanPKOpKeyAgreement (.ptr)
ForeignPtr BotanPKOpKeyAgreementStruct -> KeyAgreement
MkKeyAgreement (.foreignPtr)
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 ->
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 ->
(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
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 ->
(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
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 ->
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 ->
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