{-|
Module      : Botan.Low.PubKey
Description : Public key cryptography
Copyright   : (c) 2023-2024, Apotheca Labs
              (c) 2024-2025, Haskell Foundation
License     : BSD-3-Clause
Maintainer  : joris@well-typed.com, leo@apotheca.io
Stability   : experimental
Portability : POSIX

Public key cryptography is a collection of techniques allowing
for encryption, signatures, and key agreement.
-}

{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}

module Botan.Low.PubKey (

  -- * Private keys
    PrivKey(..)
  , CheckKeyFlags(..)
  , checkKeyFlags
  , PrivKeyExportFlags(..)
  , privKeyExportFlags
  , withPrivKey
  , privKeyCreate
  , privKeyLoad
  , privKeyDestroy
  , privKeyAlgoName
  , privKeyCheckKey
  , privKeyGetField
  , privKeyExport
  , privKeyExportPubKey

  -- * Public Keys

  , PubKey(..)
  , withPubKey
  , pubKeyLoad
  , pubKeyDestroy
  , pubKeyAlgoName
  , pubKeyCheckKey
  , pubKeyEstimatedStrength
  , pubKeyFingerprint
  , pubKeyGetField
  , pubKeyExport

  -- * PK Algorithms

  , PKName
  , pattern RSA
  , pattern SM2
  , pattern ElGamal
  , pattern DSA
  , pattern ECDSA
  , pattern ECKCDSA
  , pattern ECGDSA
  , pattern GOST_34_10
  , pattern Ed25519
  , pattern XMSS
  , pattern DH
  , pattern ECDH
  , pattern Curve25519
  , pattern Dilithium
  , pattern Kyber
  , pattern McEliece

  -- ** DLGroup

  , DLGroupName
  , pattern FFDHE_IETF_2048
  , pattern FFDHE_IETF_3072
  , pattern FFDHE_IETF_4096
  , pattern FFDHE_IETF_6144
  , pattern FFDHE_IETF_8192
  , pattern MODP_IETF_1024
  , pattern MODP_IETF_1536
  , pattern MODP_IETF_2048
  , pattern MODP_IETF_3072
  , pattern MODP_IETF_4096
  , pattern MODP_IETF_6144
  , pattern MODP_IETF_8192
  , pattern MODP_SRP_1024
  , pattern MODP_SRP_1536
  , pattern MODP_SRP_2048
  , pattern MODP_SRP_3072
  , pattern MODP_SRP_4096
  , pattern MODP_SRP_6144
  , pattern MODP_SRP_8192
  , pattern DSA_JCE_1024
  , pattern DSA_BOTAN_2048
  , pattern DSA_BOTAN_3072

  -- ** ECGroup

  , ECGroupName
  , pattern Secp160k1
  , pattern Secp160r1
  , pattern Secp160r2
  , pattern Secp192k1
  , pattern Secp192r1
  , pattern Secp224k1
  , pattern Secp224r1
  , pattern Secp256k1
  , pattern Secp256r1
  , pattern Secp384r1
  , pattern Secp521r1
  , pattern Brainpool160r1
  , pattern Brainpool192r1
  , pattern Brainpool224r1
  , pattern Brainpool256r1
  , pattern Brainpool320r1
  , pattern Brainpool384r1
  , pattern Brainpool512r1
  , pattern X962_p192v2
  , pattern X962_p192v3
  , pattern X962_p239v1
  , pattern X962_p239v2
  , pattern X962_p239v3
  , pattern Gost_256A
  , pattern Gost_512A
  , pattern Frp256v1
  , pattern Sm2p256v1

  -- ** XMSS

  , XMSSName
  , pattern XMSS_SHA2_10_256
  , pattern XMSS_SHA2_16_256
  , pattern XMSS_SHA2_20_256
  , pattern XMSS_SHA2_10_512
  , pattern XMSS_SHA2_16_512
  , pattern XMSS_SHA2_20_512
  , pattern XMSS_SHAKE_10_256
  , pattern XMSS_SHAKE_16_256
  , pattern XMSS_SHAKE_20_256
  , pattern XMSS_SHAKE_10_512
  , pattern XMSS_SHAKE_16_512
  , pattern XMSS_SHAKE_20_512

  -- * EME

  , EMEName
  , pattern EME_RAW
  , pattern EME_PKCS1_v1_5
  , pattern EME_OAEP
  , eme_raw
  , eme_pkcs1_v1_5
  , eme_oaep
  -- , eme_oaep_mgf
  , eme_hash
  , eme_sm2EncParam

  -- * EMSA

  , EMSAName
  , emsa_none
  , emsa_emsa4
  , emsa_hash
  , emsa_ed25519Pure
  , emsa_ed25519Prehashed
  , emsa_ed25519GnuPG
  , emsa_sm2SignParam

  -- * Convenience

  -- , PKPaddingName(..)
  , createPrivKey
  , createPubKey
  , mkPrivKeyLoad1_name
  , mkPrivKeyLoad3
  , mkPrivKeyLoad4
  , mkPubKeyLoad2
  , mkPubKeyLoad2_name
  , mkPubKeyLoad3
  , mkPubKeyLoad4

  ) where

import           Botan.Bindings.ConstPtr (ConstPtr (..))
import           Botan.Bindings.MPI
import           Botan.Bindings.PubKey
import           Botan.Bindings.RNG
import           Botan.Low.Error.Internal
import           Botan.Low.Hash
import           Botan.Low.Internal.ByteString
import           Botan.Low.Internal.String
import           Botan.Low.Make
import           Botan.Low.MPI
import           Botan.Low.Remake
import           Botan.Low.RNG
import           Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import           Data.Word
import           Foreign.C.Types
import           Foreign.ForeignPtr
import           Foreign.Marshal.Alloc
import           Foreign.Ptr
import           Foreign.Storable

{- $introduction

-}

{- $usage

Unless you need a specific `public key cryptosystem`, it is strongly
recommended that you use the `RSA`, `Ed25519`, or `Curve25519` algorithms,
depending on your desired operation.

Create an RSA keypair:

> import Botan.Low.RNG
> import Botan.Low.PubKey
> rng <- rngInit "user"
> -- Alice generates her keys
> alicePrivKey <- privKeyCreate RSA "4096" rng
> alicePubKey <- privKeyExportPubKey alicePrivKey

> NOTE: For algorithm-specific parameters, consult the Botan documentation and
source

Encrypt a message:

> import Botan.Low.PubKey.Encrypt
> message = "Fee fi fo fum!"
> -- Bob encrypts a message for Alice using her public key
> -- Unlike `Crypto.Saltine.Core.Box`, the message is only encrypted, not signed.
> encrypter <- encryptCreate alicePubKey EME_PKCS1_v1_5
> ciphertext <- encrypt encrypter rng message

> NOTE: For algorithm-specific padding parameters, consult the Botan
documentation and source

Decrypt a message:

> import Botan.Low.PubKey.Decrypt
> -- Alice decrypts the message from Bob using her private key
> decrypter <- decryptCreate alicePrivKey EME_PKCS1_v1_5
> plaintext <- decrypt decrypter ciphertext
> message == plaintext -- True

> NOTE: The same padding must be used for both encryption and decryption

Sign a message:

> import Botan.Low.PubKey.Sign
> import Botan.Low.Hash
> message = "Fee fi fo fum!"
> -- Alice signs the message using her private key
> signer <- signCreate alicePrivKey (emsa_emsa4 SHA3) StandardFormatSignature
> signUpdate signer message
> sig <- signFinish signer rng

> NOTE: Signing uses a different set of padding algorithms `EMSA` from
encryption `EME`, and different signing / encryption algorithms support
different specific padding algorithms

> NOTE: Signing does not yet have proper constants for selecting a padding
mechanism. For more information, refer to the `Botan.PubKey`,
`Botan.PubKey.Sign`, or the Botan C++ documentation. This area will be improved
in the near future.

Verify a message:

> import Botan.Low.PubKey.Verify
> -- Bob verifies the message using Alice's public key
> verifier <- verifyCreate alicePubKey (emsa_emsa4 SHA3) StandardFormatSignature
> verifyUpdate verifier message
> verified <- verifyFinish verifier sig
> verified -- True

> NOTE: The same padding must be used for both encryption and decryption

-}

-- Associated types

-- /*
-- * Public/private key creation, import, ...
-- */

newtype PrivKey = MkPrivKey { PrivKey -> ForeignPtr BotanPrivKeyStruct
foreignPtr :: ForeignPtr BotanPrivKeyStruct }

withPrivKey     :: PrivKey -> (BotanPrivKey -> IO a) -> IO a
privKeyDestroy  :: PrivKey -> IO ()
createPrivKey   :: (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
(PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey, PrivKey -> IO ()
privKeyDestroy, (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey)
    = (Ptr BotanPrivKeyStruct -> BotanPrivKey)
-> (BotanPrivKey -> Ptr BotanPrivKeyStruct)
-> (ForeignPtr BotanPrivKeyStruct -> PrivKey)
-> (PrivKey -> ForeignPtr BotanPrivKeyStruct)
-> FinalizerPtr BotanPrivKeyStruct
-> (PrivKey -> (BotanPrivKey -> IO a) -> IO a, PrivKey -> IO (),
    (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
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 BotanPrivKeyStruct -> BotanPrivKey
MkBotanPrivKey (.ptr)
        ForeignPtr BotanPrivKeyStruct -> PrivKey
MkPrivKey (.foreignPtr)
        FinalizerPtr BotanPrivKeyStruct
botan_privkey_destroy

type PKName = ByteString

pattern RSA
    ,   SM2
    ,   ElGamal
    ,   DSA
    ,   ECDSA
    ,   ECKCDSA
    ,   ECGDSA
    ,   GOST_34_10
    ,   Ed25519
    ,   XMSS
    ,   DH
    ,   ECDH
    ,   Curve25519
    ,   Dilithium
    ,   Kyber
    -- ,   SPHINCSPlus
    ,   McEliece
    ::  PKName

pattern $bRSA :: EMSAName
$mRSA :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
RSA         = BOTAN_PK_RSA
pattern $bSM2 :: EMSAName
$mSM2 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
SM2         = BOTAN_PK_SM2
pattern $bElGamal :: EMSAName
$mElGamal :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
ElGamal     = BOTAN_PK_ELGAMAL
pattern $bDSA :: EMSAName
$mDSA :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
DSA         = BOTAN_PK_DSA
pattern $bECDSA :: EMSAName
$mECDSA :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
ECDSA       = BOTAN_PK_ECDSA
pattern $bECKCDSA :: EMSAName
$mECKCDSA :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
ECKCDSA     = BOTAN_PK_ECKCDSA
pattern $bECGDSA :: EMSAName
$mECGDSA :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
ECGDSA      = BOTAN_PK_ECGDSA
pattern $bGOST_34_10 :: EMSAName
$mGOST_34_10 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
GOST_34_10  = BOTAN_PK_GOST_34_10
pattern $bEd25519 :: EMSAName
$mEd25519 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Ed25519     = BOTAN_PK_ED25519
pattern $bXMSS :: EMSAName
$mXMSS :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS        = BOTAN_PK_XMSS
pattern $bDH :: EMSAName
$mDH :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
DH          = BOTAN_PK_DH
pattern $bECDH :: EMSAName
$mECDH :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
ECDH        = BOTAN_PK_ECDH
pattern $bCurve25519 :: EMSAName
$mCurve25519 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Curve25519  = BOTAN_PK_CURVE25519
pattern $bDilithium :: EMSAName
$mDilithium :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Dilithium   = BOTAN_PK_DILITHIUM
pattern $bKyber :: EMSAName
$mKyber :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Kyber       = BOTAN_PK_KYBER
-- pattern SPHINCSPlus = BOTAN_PK_SPHINCSPLUS
pattern $bMcEliece :: EMSAName
$mMcEliece :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
McEliece    = BOTAN_PK_MCELIECE

type XMSSName = ByteString

pattern XMSS_SHA2_10_256
    ,   XMSS_SHA2_16_256
    ,   XMSS_SHA2_20_256
    ,   XMSS_SHA2_10_512
    ,   XMSS_SHA2_16_512
    ,   XMSS_SHA2_20_512
    ,   XMSS_SHAKE_10_256
    ,   XMSS_SHAKE_16_256
    ,   XMSS_SHAKE_20_256
    ,   XMSS_SHAKE_10_512
    ,   XMSS_SHAKE_16_512
    ,   XMSS_SHAKE_20_512
    ::  XMSSName

pattern $bXMSS_SHA2_10_256 :: EMSAName
$mXMSS_SHA2_10_256 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHA2_10_256 = BOTAN_XMSS_SHA2_10_256
pattern $bXMSS_SHA2_16_256 :: EMSAName
$mXMSS_SHA2_16_256 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHA2_16_256 = BOTAN_XMSS_SHA2_16_256
pattern $bXMSS_SHA2_20_256 :: EMSAName
$mXMSS_SHA2_20_256 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHA2_20_256 = BOTAN_XMSS_SHA2_20_256
pattern $bXMSS_SHA2_10_512 :: EMSAName
$mXMSS_SHA2_10_512 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHA2_10_512 = BOTAN_XMSS_SHA2_10_512
pattern $bXMSS_SHA2_16_512 :: EMSAName
$mXMSS_SHA2_16_512 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHA2_16_512 = BOTAN_XMSS_SHA2_16_512
pattern $bXMSS_SHA2_20_512 :: EMSAName
$mXMSS_SHA2_20_512 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHA2_20_512 = BOTAN_XMSS_SHA2_20_512
pattern $bXMSS_SHAKE_10_256 :: EMSAName
$mXMSS_SHAKE_10_256 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHAKE_10_256 = BOTAN_XMSS_SHAKE_10_256
pattern $bXMSS_SHAKE_16_256 :: EMSAName
$mXMSS_SHAKE_16_256 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHAKE_16_256 = BOTAN_XMSS_SHAKE_16_256
pattern $bXMSS_SHAKE_20_256 :: EMSAName
$mXMSS_SHAKE_20_256 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHAKE_20_256 = BOTAN_XMSS_SHAKE_20_256
pattern $bXMSS_SHAKE_10_512 :: EMSAName
$mXMSS_SHAKE_10_512 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHAKE_10_512 = BOTAN_XMSS_SHAKE_10_512
pattern $bXMSS_SHAKE_16_512 :: EMSAName
$mXMSS_SHAKE_16_512 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHAKE_16_512 = BOTAN_XMSS_SHAKE_16_512
pattern $bXMSS_SHAKE_20_512 :: EMSAName
$mXMSS_SHAKE_20_512 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
XMSS_SHAKE_20_512 = BOTAN_XMSS_SHAKE_20_512

type ECGroupName = ByteString

pattern Secp160k1
    ,   Secp160r1
    ,   Secp160r2
    ,   Secp192k1
    ,   Secp192r1
    ,   Secp224k1
    ,   Secp224r1
    ,   Secp256k1
    ,   Secp256r1
    ,   Secp384r1
    ,   Secp521r1
    ,   Brainpool160r1
    ,   Brainpool192r1
    ,   Brainpool224r1
    ,   Brainpool256r1
    ,   Brainpool320r1
    ,   Brainpool384r1
    ,   Brainpool512r1
    ,   X962_p192v2
    ,   X962_p192v3
    ,   X962_p239v1
    ,   X962_p239v2
    ,   X962_p239v3
    ,   Gost_256A
    ,   Gost_512A
    ,   Frp256v1
    ,   Sm2p256v1
    ::  ECGroupName

pattern $bSecp160k1 :: EMSAName
$mSecp160k1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp160k1       = BOTAN_ECGROUP_SECP_160_K1
pattern $bSecp160r1 :: EMSAName
$mSecp160r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp160r1       = BOTAN_ECGROUP_SECP_160_R1
pattern $bSecp160r2 :: EMSAName
$mSecp160r2 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp160r2       = BOTAN_ECGROUP_SECP_160_R2
pattern $bSecp192k1 :: EMSAName
$mSecp192k1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp192k1       = BOTAN_ECGROUP_SECP_192_K1
pattern $bSecp192r1 :: EMSAName
$mSecp192r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp192r1       = BOTAN_ECGROUP_SECP_192_R1
pattern $bSecp224k1 :: EMSAName
$mSecp224k1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp224k1       = BOTAN_ECGROUP_SECP_224_K1
pattern $bSecp224r1 :: EMSAName
$mSecp224r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp224r1       = BOTAN_ECGROUP_SECP_224_R1
pattern $bSecp256k1 :: EMSAName
$mSecp256k1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp256k1       = BOTAN_ECGROUP_SECP_256_K1
pattern $bSecp256r1 :: EMSAName
$mSecp256r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp256r1       = BOTAN_ECGROUP_SECP_256_R1
pattern $bSecp384r1 :: EMSAName
$mSecp384r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp384r1       = BOTAN_ECGROUP_SECP_384_R1
pattern $bSecp521r1 :: EMSAName
$mSecp521r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Secp521r1       = BOTAN_ECGROUP_SECP_521_R1
pattern $bBrainpool160r1 :: EMSAName
$mBrainpool160r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Brainpool160r1  = BOTAN_ECGROUP_BRAINPOOL_160_R1
pattern $bBrainpool192r1 :: EMSAName
$mBrainpool192r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Brainpool192r1  = BOTAN_ECGROUP_BRAINPOOL_192_R1
pattern $bBrainpool224r1 :: EMSAName
$mBrainpool224r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Brainpool224r1  = BOTAN_ECGROUP_BRAINPOOL_224_R1
pattern $bBrainpool256r1 :: EMSAName
$mBrainpool256r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Brainpool256r1  = BOTAN_ECGROUP_BRAINPOOL_256_R1
pattern $bBrainpool320r1 :: EMSAName
$mBrainpool320r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Brainpool320r1  = BOTAN_ECGROUP_BRAINPOOL_320_R1
pattern $bBrainpool384r1 :: EMSAName
$mBrainpool384r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Brainpool384r1  = BOTAN_ECGROUP_BRAINPOOL_384_R1
pattern $bBrainpool512r1 :: EMSAName
$mBrainpool512r1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Brainpool512r1  = BOTAN_ECGROUP_BRAINPOOL_512_R1
pattern $bX962_p192v2 :: EMSAName
$mX962_p192v2 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
X962_p192v2     = BOTAN_ECGROUP_X962_P192_V2
pattern $bX962_p192v3 :: EMSAName
$mX962_p192v3 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
X962_p192v3     = BOTAN_ECGROUP_X962_P192_V3
pattern $bX962_p239v1 :: EMSAName
$mX962_p239v1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
X962_p239v1     = BOTAN_ECGROUP_X962_P239_V1
pattern $bX962_p239v2 :: EMSAName
$mX962_p239v2 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
X962_p239v2     = BOTAN_ECGROUP_X962_P239_V2
pattern $bX962_p239v3 :: EMSAName
$mX962_p239v3 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
X962_p239v3     = BOTAN_ECGROUP_X962_P239_V3
pattern $bGost_256A :: EMSAName
$mGost_256A :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Gost_256A       = BOTAN_ECGROUP_GOST_256A
pattern $bGost_512A :: EMSAName
$mGost_512A :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Gost_512A       = BOTAN_ECGROUP_GOST_512A
pattern $bFrp256v1 :: EMSAName
$mFrp256v1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Frp256v1        = BOTAN_ECGROUP_FRP_256_V1
pattern $bSm2p256v1 :: EMSAName
$mSm2p256v1 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
Sm2p256v1       = BOTAN_ECGROUP_SM2_P256_V1

type DLGroupName = ByteString

pattern FFDHE_IETF_2048
    ,   FFDHE_IETF_3072
    ,   FFDHE_IETF_4096
    ,   FFDHE_IETF_6144
    ,   FFDHE_IETF_8192
    ,   MODP_IETF_1024
    ,   MODP_IETF_1536
    ,   MODP_IETF_2048
    ,   MODP_IETF_3072
    ,   MODP_IETF_4096
    ,   MODP_IETF_6144
    ,   MODP_IETF_8192
    ,   MODP_SRP_1024
    ,   MODP_SRP_1536
    ,   MODP_SRP_2048
    ,   MODP_SRP_3072
    ,   MODP_SRP_4096
    ,   MODP_SRP_6144
    ,   MODP_SRP_8192
    ,   DSA_JCE_1024
    ,   DSA_BOTAN_2048
    ,   DSA_BOTAN_3072
    ::  DLGroupName

pattern $bFFDHE_IETF_2048 :: EMSAName
$mFFDHE_IETF_2048 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
FFDHE_IETF_2048 = BOTAN_DLGROUP_FFDHE_IETF_2048
pattern $bFFDHE_IETF_3072 :: EMSAName
$mFFDHE_IETF_3072 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
FFDHE_IETF_3072 = BOTAN_DLGROUP_FFDHE_IETF_3072
pattern $bFFDHE_IETF_4096 :: EMSAName
$mFFDHE_IETF_4096 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
FFDHE_IETF_4096 = BOTAN_DLGROUP_FFDHE_IETF_4096
pattern $bFFDHE_IETF_6144 :: EMSAName
$mFFDHE_IETF_6144 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
FFDHE_IETF_6144 = BOTAN_DLGROUP_FFDHE_IETF_6144
pattern $bFFDHE_IETF_8192 :: EMSAName
$mFFDHE_IETF_8192 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
FFDHE_IETF_8192 = BOTAN_DLGROUP_FFDHE_IETF_8192
pattern $bMODP_IETF_1024 :: EMSAName
$mMODP_IETF_1024 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_IETF_1024  = BOTAN_DLGROUP_MODP_IETF_1024
pattern $bMODP_IETF_1536 :: EMSAName
$mMODP_IETF_1536 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_IETF_1536  = BOTAN_DLGROUP_MODP_IETF_1536
pattern $bMODP_IETF_2048 :: EMSAName
$mMODP_IETF_2048 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_IETF_2048  = BOTAN_DLGROUP_MODP_IETF_2048
pattern $bMODP_IETF_3072 :: EMSAName
$mMODP_IETF_3072 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_IETF_3072  = BOTAN_DLGROUP_MODP_IETF_3072
pattern $bMODP_IETF_4096 :: EMSAName
$mMODP_IETF_4096 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_IETF_4096  = BOTAN_DLGROUP_MODP_IETF_4096
pattern $bMODP_IETF_6144 :: EMSAName
$mMODP_IETF_6144 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_IETF_6144  = BOTAN_DLGROUP_MODP_IETF_6144
pattern $bMODP_IETF_8192 :: EMSAName
$mMODP_IETF_8192 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_IETF_8192  = BOTAN_DLGROUP_MODP_IETF_8192
pattern $bMODP_SRP_1024 :: EMSAName
$mMODP_SRP_1024 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_SRP_1024   = BOTAN_DLGROUP_MODP_SRP_1024
pattern $bMODP_SRP_1536 :: EMSAName
$mMODP_SRP_1536 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_SRP_1536   = BOTAN_DLGROUP_MODP_SRP_1536
pattern $bMODP_SRP_2048 :: EMSAName
$mMODP_SRP_2048 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_SRP_2048   = BOTAN_DLGROUP_MODP_SRP_2048
pattern $bMODP_SRP_3072 :: EMSAName
$mMODP_SRP_3072 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_SRP_3072   = BOTAN_DLGROUP_MODP_SRP_3072
pattern $bMODP_SRP_4096 :: EMSAName
$mMODP_SRP_4096 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_SRP_4096   = BOTAN_DLGROUP_MODP_SRP_4096
pattern $bMODP_SRP_6144 :: EMSAName
$mMODP_SRP_6144 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_SRP_6144   = BOTAN_DLGROUP_MODP_SRP_6144
pattern $bMODP_SRP_8192 :: EMSAName
$mMODP_SRP_8192 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
MODP_SRP_8192   = BOTAN_DLGROUP_MODP_SRP_8192
pattern $bDSA_JCE_1024 :: EMSAName
$mDSA_JCE_1024 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
DSA_JCE_1024    = BOTAN_DLGROUP_DSA_JCE_1024
pattern $bDSA_BOTAN_2048 :: EMSAName
$mDSA_BOTAN_2048 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
DSA_BOTAN_2048  = BOTAN_DLGROUP_DSA_BOTAN_2048
pattern $bDSA_BOTAN_3072 :: EMSAName
$mDSA_BOTAN_3072 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
DSA_BOTAN_3072  = BOTAN_DLGROUP_DSA_BOTAN_3072

{- |
Encoding Method for Encryption

WARNING: Name is not completely accurate, may be changed to PKEncryptParams
-}
type EMEName = ByteString

pattern EME_RAW
    ,   EME_PKCS1_v1_5
    ,   EME_OAEP
    :: EMEName

pattern $bEME_RAW :: EMSAName
$mEME_RAW :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
EME_RAW = BOTAN_EME_RAW
pattern $bEME_PKCS1_v1_5 :: EMSAName
$mEME_PKCS1_v1_5 :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
EME_PKCS1_v1_5 = BOTAN_EME_PKCS1_v1_5
pattern $bEME_OAEP :: EMSAName
$mEME_OAEP :: forall {r}. EMSAName -> ((# #) -> r) -> ((# #) -> r) -> r
EME_OAEP = BOTAN_EME_OAEP

eme_raw :: EMEName
eme_raw :: EMSAName
eme_raw = EMSAName
EME_RAW

eme_pkcs1_v1_5 :: EMEName
eme_pkcs1_v1_5 :: EMSAName
eme_pkcs1_v1_5 = EMSAName
EME_PKCS1_v1_5

eme_oaep :: HashName -> EMEName
eme_oaep :: EMSAName -> EMSAName
eme_oaep EMSAName
h = EMSAName
EME_OAEP EMSAName -> EMSAName -> EMSAName
forall a. (IsString a, Semigroup a) => a -> a -> a
/$ EMSAName
h

-- TODO: OAEP with MGF, L
-- eme_oaep_mgf :: HashName -> MGFName -> EMEName
-- eme_oaep_mgf h m = EME_OAEP /$ h <> "," <> m

eme_hash :: HashName -> EMEName
eme_hash :: EMSAName -> EMSAName
eme_hash = EMSAName -> EMSAName
forall a. a -> a
id

eme_sm2EncParam :: HashName -> EMEName
eme_sm2EncParam :: EMSAName -> EMSAName
eme_sm2EncParam EMSAName
h = EMSAName
h


{- |
Encoding Method for Signature with Appendix

WARNING: Name is not completely accurate, may be changed to PKSignParams
-}
type EMSAName = ByteString

-- emsa_rsa ::

emsa_emsa4 :: HashName -> EMSAName
emsa_emsa4 :: EMSAName -> EMSAName
emsa_emsa4 EMSAName
h = EMSAName
"EMSA4(" EMSAName -> EMSAName -> EMSAName
forall a. Semigroup a => a -> a -> a
<> EMSAName
h EMSAName -> EMSAName -> EMSAName
forall a. Semigroup a => a -> a -> a
<> EMSAName
")"

emsa_ed25519Pure :: EMSAName
emsa_ed25519Pure :: EMSAName
emsa_ed25519Pure = EMSAName
"Pure"

emsa_ed25519Prehashed :: EMSAName
emsa_ed25519Prehashed :: EMSAName
emsa_ed25519Prehashed = EMSAName
"Ed25519ph"

emsa_ed25519GnuPG :: HashName -> EMSAName
emsa_ed25519GnuPG :: EMSAName -> EMSAName
emsa_ed25519GnuPG = EMSAName -> EMSAName
emsa_hash

emsa_hash :: HashName -> EMSAName
emsa_hash :: EMSAName -> EMSAName
emsa_hash = EMSAName -> EMSAName
forall a. a -> a
id

emsa_sm2SignParam :: ByteString -> HashName -> EMSAName
emsa_sm2SignParam :: EMSAName -> EMSAName -> EMSAName
emsa_sm2SignParam EMSAName
uid EMSAName
h = EMSAName
uid EMSAName -> EMSAName -> EMSAName
forall a. Semigroup a => a -> a -> a
<> EMSAName
"," EMSAName -> EMSAName -> EMSAName
forall a. Semigroup a => a -> a -> a
<> EMSAName
h

-- emsa_xmss
-- emsa_dilithium

emsa_none :: EMSAName
emsa_none :: EMSAName
emsa_none = EMSAName
""

-- | Create a new private key
privKeyCreate
    :: ByteString   -- ^ __algo_name__: something like "RSA" or "ECDSA"
    -> ByteString   -- ^ __algo_params__: is specific to the algorithm. For RSA, specifies
                    --   the modulus bit length. For ECC is the name of the curve.
    -> RNG          -- ^ __rng__: a random number generator
    -> IO PrivKey   -- ^ __key__: the new object will be placed here
privKeyCreate :: EMSAName -> EMSAName -> RNG -> IO PrivKey
privKeyCreate EMSAName
name EMSAName
params RNG
rng = EMSAName -> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
name ((Ptr CChar -> IO PrivKey) -> IO PrivKey)
-> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
namePtr -> do
    EMSAName -> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
params ((Ptr CChar -> IO PrivKey) -> IO PrivKey)
-> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
paramsPtr -> do
        RNG -> (BotanRNG -> IO PrivKey) -> IO PrivKey
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO PrivKey) -> IO PrivKey)
-> (BotanRNG -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG -> do
            (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey ((Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
-> (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPrivKey
out -> Ptr BotanPrivKey
-> ConstPtr CChar -> ConstPtr CChar -> BotanRNG -> IO CInt
botan_privkey_create
                Ptr BotanPrivKey
out
                (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
namePtr)
                (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
paramsPtr)
                BotanRNG
botanRNG

data CheckKeyFlags =
    CheckKeyNormalTests
  | CheckKeyExpensiveTests

checkKeyFlags :: CheckKeyFlags -> Word32
checkKeyFlags :: CheckKeyFlags -> Word32
checkKeyFlags CheckKeyFlags
CheckKeyNormalTests    = Word32
forall a. (Eq a, Num a) => a
BOTAN_CHECK_KEY_NORMAL_TESTS
checkKeyFlags CheckKeyFlags
CheckKeyExpensiveTests = Word32
forall a. (Eq a, Num a) => a
BOTAN_CHECK_KEY_EXPENSIVE_TESTS

-- TODO: Probably catch -1 (INVALID_INPUT), return Bool
-- | Check the validity of a private key
privKeyCheckKey
    :: PrivKey          -- ^ __key__
    -> RNG              -- ^ __rng__
    -> CheckKeyFlags    -- ^ __flags__
    -> IO ()
privKeyCheckKey :: PrivKey -> RNG -> CheckKeyFlags -> IO ()
privKeyCheckKey PrivKey
sk RNG
rng CheckKeyFlags
flags = PrivKey -> (BotanPrivKey -> IO ()) -> IO ()
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey PrivKey
sk ((BotanPrivKey -> IO ()) -> IO ())
-> (BotanPrivKey -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanPrivKey
skPtr -> do
    RNG -> (BotanRNG -> IO ()) -> IO ()
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO ()) -> IO ()) -> (BotanRNG -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG -> do
        HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanPrivKey -> BotanRNG -> Word32 -> IO CInt
botan_privkey_check_key BotanPrivKey
skPtr BotanRNG
botanRNG (CheckKeyFlags -> Word32
checkKeyFlags CheckKeyFlags
flags)

-- NOTE: Expectes PKCS #8 / PEM structure
-- botan_privkey_export -> null password? and botan_privkey_export_encrypted_... -> use a password?
-- NOTE: This is probably a bad implementation; null pointer and empty string are different
--  and unencrypted is not the same as encrypted with "" as a pasword
--  We'll fix this by having privKeyLoad and privKeyLoadEncrypted be separate functions
{- |
Input currently assumed to be PKCS #8 structure;
Set password to NULL to indicate no encryption expected
Starting in 2.8.0, the rng parameter is unused and may be set to null
-}
privKeyLoad
    :: ByteString   -- ^ __bits[]__
    -> ByteString   -- ^ __password__
    -> IO PrivKey   -- ^ __key__
privKeyLoad :: EMSAName -> EMSAName -> IO PrivKey
privKeyLoad EMSAName
bits EMSAName
password = EMSAName -> (Ptr Word8 -> CSize -> IO PrivKey) -> IO PrivKey
forall byte a. EMSAName -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen EMSAName
bits ((Ptr Word8 -> CSize -> IO PrivKey) -> IO PrivKey)
-> (Ptr Word8 -> CSize -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
bitsPtr CSize
bitsLen -> do
    let asCStringNullable :: EMSAName -> (Ptr CChar -> IO a) -> IO a
asCStringNullable EMSAName
str Ptr CChar -> IO a
act = if EMSAName -> Bool
ByteString.null EMSAName
str
        then Ptr CChar -> IO a
act Ptr CChar
forall a. Ptr a
nullPtr
        else EMSAName -> (Ptr CChar -> IO a) -> IO a
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
str Ptr CChar -> IO a
act
    EMSAName -> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCStringNullable EMSAName
password ((Ptr CChar -> IO PrivKey) -> IO PrivKey)
-> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
passwordPtr -> do
        (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey ((Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
-> (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPrivKey
out -> Ptr BotanPrivKey
-> BotanRNG -> ConstPtr Word8 -> CSize -> ConstPtr CChar -> IO CInt
botan_privkey_load
            Ptr BotanPrivKey
out
            (Ptr BotanRNGStruct -> BotanRNG
MkBotanRNG Ptr BotanRNGStruct
forall a. Ptr a
nullPtr)
            (Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
bitsPtr)
            CSize
bitsLen
            (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
passwordPtr)

data PrivKeyExportFlags =
    PrivKeyExportDER
  | PrivKeyExportPEM

privKeyExportFlags :: PrivKeyExportFlags -> Word32
privKeyExportFlags :: PrivKeyExportFlags -> Word32
privKeyExportFlags PrivKeyExportFlags
PrivKeyExportDER = Word32
forall a. (Eq a, Num a) => a
BOTAN_PRIVKEY_EXPORT_FLAG_DER
privKeyExportFlags PrivKeyExportFlags
PrivKeyExportPEM = Word32
forall a. (Eq a, Num a) => a
BOTAN_PRIVKEY_EXPORT_FLAG_PEM

-- NOTE: Different from allocBytesQuerying / INSUFFICIENT_BUFFER_SPACE
{- |
On input *out_len is number of bytes in out[]
On output *out_len is number of bytes written (or required)
If out is not big enough no output is written, *out_len is set and 1 is returned
Returns 0 on success and sets
If some other error occurs a negative integer is returned.
-}
privKeyExport
    :: PrivKey              -- ^ __key__
    -> PrivKeyExportFlags   -- ^ __flags__
    -> IO ByteString        -- ^ __out[]__
privKeyExport :: PrivKey -> PrivKeyExportFlags -> IO EMSAName
privKeyExport PrivKey
sk PrivKeyExportFlags
flags = PrivKey -> (BotanPrivKey -> IO EMSAName) -> IO EMSAName
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey PrivKey
sk ((BotanPrivKey -> IO EMSAName) -> IO EMSAName)
-> (BotanPrivKey -> IO EMSAName) -> IO EMSAName
forall a b. (a -> b) -> a -> b
$ \ BotanPrivKey
skPtr -> do
    (Ptr CSize -> IO EMSAName) -> IO EMSAName
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CSize -> IO EMSAName) -> IO EMSAName)
-> (Ptr CSize -> IO EMSAName) -> IO EMSAName
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 CSize
0
        -- NOTE: Presumed be -1
        CInt
_ <- BotanPrivKey -> Ptr Word8 -> Ptr CSize -> Word32 -> IO CInt
botan_privkey_export BotanPrivKey
skPtr Ptr Word8
forall a. Ptr a
nullPtr Ptr CSize
szPtr (PrivKeyExportFlags -> Word32
privKeyExportFlags PrivKeyExportFlags
flags)
        CSize
sz <- Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
szPtr
        Int -> (Ptr Word8 -> IO ()) -> IO EMSAName
forall byte. Int -> (Ptr byte -> IO ()) -> IO EMSAName
allocBytes (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
sz) ((Ptr Word8 -> IO ()) -> IO EMSAName)
-> (Ptr Word8 -> IO ()) -> IO EMSAName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
bytesPtr -> do
            HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanPrivKey -> Ptr Word8 -> Ptr CSize -> Word32 -> IO CInt
botan_privkey_export BotanPrivKey
skPtr Ptr Word8
bytesPtr Ptr CSize
szPtr (PrivKeyExportFlags -> Word32
privKeyExportFlags PrivKeyExportFlags
flags)

-- TODO:
-- View the private key's DER encoding
-- privKeyViewDER
--         :: BotanPrivKey                         -- ^ __key__
--         -> BotanViewContext ctx                 -- ^ __ctx__
--         -> FunPtr (BotanViewBinCallback ctx)    -- ^ __view__
--         -> IO CInt

-- TODO:
-- View the private key's PEM encoding
-- privKeyViewPEM
--         :: BotanPrivKey                         -- ^ __key__
--         -> BotanViewContext ctx                 -- ^ __ctx__
--         -> FunPtr (BotanViewStrCallback ctx)    -- ^ __view__
--         -> IO CInt

privKeyAlgoName
    :: PrivKey          -- ^ __key__
    -> IO ByteString    -- ^ __out[]__
privKeyAlgoName :: PrivKey -> IO EMSAName
privKeyAlgoName = (forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a)
-> GetCString BotanPrivKey CChar -> PrivKey -> IO EMSAName
forall typ ptr byte.
WithPtr typ ptr -> GetCString ptr byte -> typ -> IO EMSAName
mkGetCString PrivKey -> (BotanPrivKey -> IO a) -> IO a
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey GetCString BotanPrivKey CChar
botan_privkey_algo_name

-- TODO:
-- privKeyExportEncryptedPBKDFMsec
--     :: PrivKey
--     -> RNG
--     -> ByteString   -- Passphrase
--     -> Word32       -- Msec runtime
--     -> ByteString   -- Cipher algo
--     -> ByteString   -- PBKDF algo
--     -> Word32       -- Flags
--     -> IO ByteString
-- privKeyExportEncryptedPBKDFMsec = undefined

-- TODO:
-- privKeyExportEncryptedPBKDFIter
--     :: PrivKey
--     -> RNG
--     -> ByteString   -- Passphrase
--     -> CSize        -- Iterations
--     -> ByteString   -- Cipher algo
--     -> ByteString   -- PBKDF algo
--     -> Word32       -- Flags
--     -> IO ByteString
-- privKeyExportEncryptedPBKDFIter = undefined

newtype PubKey = MkPubKey { PubKey -> ForeignPtr BotanPubKeyStruct
foreignPtr :: ForeignPtr BotanPubKeyStruct }

withPubKey     :: PubKey -> (BotanPubKey -> IO a) -> IO a
pubKeyDestroy  :: PubKey -> IO ()
createPubKey   :: (Ptr BotanPubKey -> IO CInt) -> IO PubKey
(PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey, PubKey -> IO ()
pubKeyDestroy, (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey)
    = (Ptr BotanPubKeyStruct -> BotanPubKey)
-> (BotanPubKey -> Ptr BotanPubKeyStruct)
-> (ForeignPtr BotanPubKeyStruct -> PubKey)
-> (PubKey -> ForeignPtr BotanPubKeyStruct)
-> FinalizerPtr BotanPubKeyStruct
-> (PubKey -> (BotanPubKey -> IO a) -> IO a, PubKey -> IO (),
    (Ptr BotanPubKey -> IO CInt) -> IO PubKey)
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 BotanPubKeyStruct -> BotanPubKey
MkBotanPubKey (.ptr)
        ForeignPtr BotanPubKeyStruct -> PubKey
MkPubKey (.foreignPtr)
        FinalizerPtr BotanPubKeyStruct
botan_pubkey_destroy

pubKeyLoad
    :: ByteString   -- ^ __bits[]__
    -> IO PubKey    -- ^ __key__
pubKeyLoad :: EMSAName -> IO PubKey
pubKeyLoad = ((Ptr BotanPubKey -> IO CInt) -> IO PubKey)
-> (Ptr BotanPubKey -> ConstPtr Word8 -> CSize -> IO CInt)
-> EMSAName
-> IO PubKey
forall botan object.
((Ptr botan -> IO CInt) -> IO object)
-> (Ptr botan -> ConstPtr Word8 -> CSize -> IO CInt)
-> EMSAName
-> IO object
mkCreateObjectCBytesLen (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey Ptr BotanPubKey -> ConstPtr Word8 -> CSize -> IO CInt
botan_pubkey_load

privKeyExportPubKey
    :: PrivKey      -- ^ __in__
    -> IO PubKey    -- ^ __out__
privKeyExportPubKey :: PrivKey -> IO PubKey
privKeyExportPubKey = ((Ptr BotanPubKey -> IO CInt) -> IO PubKey)
-> (PrivKey -> (BotanPrivKey -> IO PubKey) -> IO PubKey)
-> (Ptr BotanPubKey -> BotanPrivKey -> IO CInt)
-> PrivKey
-> IO PubKey
forall botan object arg carg.
((Ptr botan -> IO CInt) -> IO object)
-> (arg -> (carg -> IO object) -> IO object)
-> (Ptr botan -> carg -> IO CInt)
-> arg
-> IO object
mkCreateObjectWith (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey PrivKey -> (BotanPrivKey -> IO PubKey) -> IO PubKey
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey Ptr BotanPubKey -> BotanPrivKey -> IO CInt
botan_privkey_export_pubkey

type PubKeyExportFlags = PrivKeyExportFlags


-- NOTE: Different from allocBytesQuerying / INSUFFICIENT_BUFFER_SPACE
pubKeyExport
    :: PubKey               -- ^ __key__
    -> PubKeyExportFlags    -- ^ __flags__
    -> IO ByteString        -- ^ __out[]__
pubKeyExport :: PubKey -> PrivKeyExportFlags -> IO EMSAName
pubKeyExport PubKey
pk PrivKeyExportFlags
flags = PubKey -> (BotanPubKey -> IO EMSAName) -> IO EMSAName
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey PubKey
pk ((BotanPubKey -> IO EMSAName) -> IO EMSAName)
-> (BotanPubKey -> IO EMSAName) -> IO EMSAName
forall a b. (a -> b) -> a -> b
$ \ BotanPubKey
pkPtr -> do
    (Ptr CSize -> IO EMSAName) -> IO EMSAName
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CSize -> IO EMSAName) -> IO EMSAName)
-> (Ptr CSize -> IO EMSAName) -> IO EMSAName
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 CSize
0
        -- NOTE: Presumed be -1
        CInt
_ <- BotanPubKey -> Ptr Word8 -> Ptr CSize -> Word32 -> IO CInt
botan_pubkey_export BotanPubKey
pkPtr Ptr Word8
forall a. Ptr a
nullPtr Ptr CSize
szPtr (PrivKeyExportFlags -> Word32
privKeyExportFlags PrivKeyExportFlags
flags)
        CSize
sz <- Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
szPtr
        Int -> (Ptr Word8 -> IO ()) -> IO EMSAName
forall byte. Int -> (Ptr byte -> IO ()) -> IO EMSAName
allocBytes (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
sz) ((Ptr Word8 -> IO ()) -> IO EMSAName)
-> (Ptr Word8 -> IO ()) -> IO EMSAName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
bytesPtr -> do
            HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanPubKey -> Ptr Word8 -> Ptr CSize -> Word32 -> IO CInt
botan_pubkey_export BotanPubKey
pkPtr Ptr Word8
bytesPtr Ptr CSize
szPtr (PrivKeyExportFlags -> Word32
privKeyExportFlags PrivKeyExportFlags
flags)


pubKeyAlgoName
    :: PubKey           -- ^ __key__
    -> IO ByteString    -- ^ __out[]__
pubKeyAlgoName :: PubKey -> IO EMSAName
pubKeyAlgoName = (forall a. PubKey -> (BotanPubKey -> IO a) -> IO a)
-> GetCString BotanPubKey CChar -> PubKey -> IO EMSAName
forall typ ptr byte.
WithPtr typ ptr -> GetCString ptr byte -> typ -> IO EMSAName
mkGetCString PubKey -> (BotanPubKey -> IO a) -> IO a
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey GetCString BotanPubKey CChar
botan_pubkey_algo_name

pubKeyCheckKey ::
     PubKey           -- ^ __key__
  -> RNG              -- ^ __rng__
  -> CheckKeyFlags    -- ^ __flags__
  -> IO Bool
pubKeyCheckKey :: PubKey -> RNG -> CheckKeyFlags -> IO Bool
pubKeyCheckKey PubKey
pk RNG
rng CheckKeyFlags
flags =
    PubKey -> (BotanPubKey -> IO Bool) -> IO Bool
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey PubKey
pk ((BotanPubKey -> IO Bool) -> IO Bool)
-> (BotanPubKey -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ BotanPubKey
pkPtr ->
    RNG -> (BotanRNG -> IO Bool) -> IO Bool
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO Bool) -> IO Bool)
-> (BotanRNG -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG ->
    HasCallStack => IO CInt -> IO Bool
IO CInt -> IO Bool
throwBotanCatchingInvalidInput (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ BotanPubKey -> BotanRNG -> Word32 -> IO CInt
botan_pubkey_check_key BotanPubKey
pkPtr BotanRNG
botanRNG (CheckKeyFlags -> Word32
checkKeyFlags CheckKeyFlags
flags)

-- Annoying - this mixes cint and csize
--  I need to consolidate getsize / getint
pubKeyEstimatedStrength
    :: PubKey   -- ^ __key__
    -> IO Int   -- ^ __estimate__
pubKeyEstimatedStrength :: PubKey -> IO Int
pubKeyEstimatedStrength PubKey
pk = (forall a. PubKey -> (BotanPubKey -> IO a) -> IO a)
-> GetSize BotanPubKey -> PubKey -> IO Int
forall typ ptr. WithPtr typ ptr -> GetSize ptr -> typ -> IO Int
mkGetSize PubKey -> (BotanPubKey -> IO a) -> IO a
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey GetSize BotanPubKey
botan_pubkey_estimated_strength PubKey
pk

pubKeyFingerprint
    :: PubKey           -- ^ __key__
    -> HashName         -- ^ __hash__
    -> IO ByteString    -- ^ __out[]__
pubKeyFingerprint :: PubKey -> EMSAName -> IO EMSAName
pubKeyFingerprint PubKey
pk EMSAName
algo = PubKey -> (BotanPubKey -> IO EMSAName) -> IO EMSAName
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey PubKey
pk ((BotanPubKey -> IO EMSAName) -> IO EMSAName)
-> (BotanPubKey -> IO EMSAName) -> IO EMSAName
forall a b. (a -> b) -> a -> b
$ \ BotanPubKey
pkPtr -> do
    EMSAName -> (Ptr CChar -> IO EMSAName) -> IO EMSAName
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
algo ((Ptr CChar -> IO EMSAName) -> IO EMSAName)
-> (Ptr CChar -> IO EMSAName) -> IO EMSAName
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
algoPtr -> do
        (Ptr Word8 -> Ptr CSize -> IO CInt) -> IO EMSAName
forall byte. (Ptr byte -> Ptr CSize -> IO CInt) -> IO EMSAName
allocBytesQuerying ((Ptr Word8 -> Ptr CSize -> IO CInt) -> IO EMSAName)
-> (Ptr Word8 -> Ptr CSize -> IO CInt) -> IO EMSAName
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
outPtr Ptr CSize
outLen -> BotanPubKey -> ConstPtr CChar -> Ptr Word8 -> Ptr CSize -> IO CInt
botan_pubkey_fingerprint
            BotanPubKey
pkPtr
            (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
algoPtr)
            Ptr Word8
outPtr
            Ptr CSize
outLen

-- | Get arbitrary named fields from public or private keys
pubKeyGetField
    :: MP           -- ^ __output__
    -> PubKey       -- ^ __key__
    -> ByteString   -- ^ __field_name__
    -> IO ()
pubKeyGetField :: MP -> PubKey -> EMSAName -> IO ()
pubKeyGetField MP
mp PubKey
pk EMSAName
field = MP -> (BotanMP -> IO ()) -> IO ()
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP MP
mp ((BotanMP -> IO ()) -> IO ()) -> (BotanMP -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanMP
mpPtr -> do
    PubKey -> (BotanPubKey -> IO ()) -> IO ()
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey PubKey
pk ((BotanPubKey -> IO ()) -> IO ())
-> (BotanPubKey -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanPubKey
pkPtr -> do
        EMSAName -> (Ptr CChar -> IO ()) -> IO ()
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
field ((Ptr CChar -> IO ()) -> IO ()) -> (Ptr CChar -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
fieldPtr -> do
            HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanMP -> BotanPubKey -> ConstPtr CChar -> IO CInt
botan_pubkey_get_field
                BotanMP
mpPtr
                BotanPubKey
pkPtr
                (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
fieldPtr)

-- | Get arbitrary named fields from public or private keys
privKeyGetField
    :: MP           -- ^ __output__
    -> PrivKey      -- ^ __key__
    -> ByteString   -- ^ __field_name__
    -> IO ()
privKeyGetField :: MP -> PrivKey -> EMSAName -> IO ()
privKeyGetField MP
mp PrivKey
sk EMSAName
field = MP -> (BotanMP -> IO ()) -> IO ()
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP MP
mp ((BotanMP -> IO ()) -> IO ()) -> (BotanMP -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanMP
mpPtr -> do
    PrivKey -> (BotanPrivKey -> IO ()) -> IO ()
forall a. PrivKey -> (BotanPrivKey -> IO a) -> IO a
withPrivKey PrivKey
sk ((BotanPrivKey -> IO ()) -> IO ())
-> (BotanPrivKey -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanPrivKey
skPtr -> do
        EMSAName -> (Ptr CChar -> IO ()) -> IO ()
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
field ((Ptr CChar -> IO ()) -> IO ()) -> (Ptr CChar -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
fieldPtr -> do
            HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanMP -> BotanPrivKey -> ConstPtr CChar -> IO CInt
botan_privkey_get_field
                BotanMP
mpPtr
                BotanPrivKey
skPtr
                (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
fieldPtr)

-- Helpers

mkPrivKeyLoad1_name
    :: (Ptr BotanPrivKey -> BotanMP -> ConstPtr CChar -> IO CInt)
    -> MP -> ByteString -> IO PrivKey
mkPrivKeyLoad1_name :: (Ptr BotanPrivKey -> BotanMP -> ConstPtr CChar -> IO CInt)
-> MP -> EMSAName -> IO PrivKey
mkPrivKeyLoad1_name Ptr BotanPrivKey -> BotanMP -> ConstPtr CChar -> IO CInt
load MP
a EMSAName
name = MP -> (BotanMP -> IO PrivKey) -> IO PrivKey
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP MP
a ((BotanMP -> IO PrivKey) -> IO PrivKey)
-> (BotanMP -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ BotanMP
aPtr -> do
    EMSAName -> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
name ((Ptr CChar -> IO PrivKey) -> IO PrivKey)
-> (Ptr CChar -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
namePtr -> do
        (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey ((Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
-> (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPrivKey
out -> Ptr BotanPrivKey -> BotanMP -> ConstPtr CChar -> IO CInt
load Ptr BotanPrivKey
out BotanMP
aPtr (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
namePtr)

mkPrivKeyLoad3
    :: (Ptr BotanPrivKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
    -> MP -> MP -> MP -> IO PrivKey
mkPrivKeyLoad3 :: (Ptr BotanPrivKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
-> MP -> MP -> MP -> IO PrivKey
mkPrivKeyLoad3 Ptr BotanPrivKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load MP
a MP
b MP
c = (forall a. MP -> (BotanMP -> IO a) -> IO a)
-> [MP] -> ([BotanMP] -> IO PrivKey) -> IO PrivKey
forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany MP -> (BotanMP -> IO a) -> IO a
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP [MP
a,MP
b,MP
c] (([BotanMP] -> IO PrivKey) -> IO PrivKey)
-> ([BotanMP] -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \case
    [BotanMP
aPtr,BotanMP
bPtr,BotanMP
cPtr] -> do
      (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey ((Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
-> (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPrivKey
out -> Ptr BotanPrivKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load Ptr BotanPrivKey
out BotanMP
aPtr BotanMP
bPtr BotanMP
cPtr
    [BotanMP]
_ -> [Char] -> IO PrivKey
forall a. HasCallStack => [Char] -> a
error [Char]
"mkPrivKeyLoad3: impossible"

mkPrivKeyLoad4
    :: (Ptr BotanPrivKey -> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
    -> MP -> MP -> MP -> MP -> IO PrivKey
mkPrivKeyLoad4 :: (Ptr BotanPrivKey
 -> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
-> MP -> MP -> MP -> MP -> IO PrivKey
mkPrivKeyLoad4 Ptr BotanPrivKey
-> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load MP
a MP
b MP
c MP
d = (forall a. MP -> (BotanMP -> IO a) -> IO a)
-> [MP] -> ([BotanMP] -> IO PrivKey) -> IO PrivKey
forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany MP -> (BotanMP -> IO a) -> IO a
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP [MP
a,MP
b,MP
c,MP
d] (([BotanMP] -> IO PrivKey) -> IO PrivKey)
-> ([BotanMP] -> IO PrivKey) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \case
    [BotanMP
aPtr,BotanMP
bPtr,BotanMP
cPtr,BotanMP
dPtr] -> do
      (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
createPrivKey ((Ptr BotanPrivKey -> IO CInt) -> IO PrivKey)
-> (Ptr BotanPrivKey -> IO CInt) -> IO PrivKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPrivKey
out -> Ptr BotanPrivKey
-> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load Ptr BotanPrivKey
out BotanMP
aPtr BotanMP
bPtr BotanMP
cPtr BotanMP
dPtr
    [BotanMP]
_ -> [Char] -> IO PrivKey
forall a. HasCallStack => [Char] -> a
error [Char]
"mkPrivKeyLoad4: impossible"

--

mkPubKeyLoad2
    :: (Ptr BotanPubKey -> BotanMP -> BotanMP -> IO CInt)
    -> MP -> MP -> IO PubKey
mkPubKeyLoad2 :: (Ptr BotanPubKey -> BotanMP -> BotanMP -> IO CInt)
-> MP -> MP -> IO PubKey
mkPubKeyLoad2 Ptr BotanPubKey -> BotanMP -> BotanMP -> IO CInt
load MP
a MP
b = (forall a. MP -> (BotanMP -> IO a) -> IO a)
-> [MP] -> ([BotanMP] -> IO PubKey) -> IO PubKey
forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany MP -> (BotanMP -> IO a) -> IO a
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP [MP
a,MP
b] (([BotanMP] -> IO PubKey) -> IO PubKey)
-> ([BotanMP] -> IO PubKey) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \case
    [BotanMP
aPtr,BotanMP
bPtr] -> do
      (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey ((Ptr BotanPubKey -> IO CInt) -> IO PubKey)
-> (Ptr BotanPubKey -> IO CInt) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPubKey
out -> Ptr BotanPubKey -> BotanMP -> BotanMP -> IO CInt
load Ptr BotanPubKey
out BotanMP
aPtr BotanMP
bPtr
    [BotanMP]
_ -> [Char] -> IO PubKey
forall a. HasCallStack => [Char] -> a
error [Char]
"mkPubKeyLoad2: impossible"

mkPubKeyLoad2_name
    :: (Ptr BotanPubKey -> BotanMP -> BotanMP -> ConstPtr CChar -> IO CInt)
    -> MP -> MP -> ByteString -> IO PubKey
mkPubKeyLoad2_name :: (Ptr BotanPubKey
 -> BotanMP -> BotanMP -> ConstPtr CChar -> IO CInt)
-> MP -> MP -> EMSAName -> IO PubKey
mkPubKeyLoad2_name Ptr BotanPubKey -> BotanMP -> BotanMP -> ConstPtr CChar -> IO CInt
load MP
x MP
y EMSAName
name = (forall a. MP -> (BotanMP -> IO a) -> IO a)
-> [MP] -> ([BotanMP] -> IO PubKey) -> IO PubKey
forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany MP -> (BotanMP -> IO a) -> IO a
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP [MP
x,MP
y] (([BotanMP] -> IO PubKey) -> IO PubKey)
-> ([BotanMP] -> IO PubKey) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \case
    [BotanMP
xPtr,BotanMP
yPtr] -> do
      EMSAName -> (Ptr CChar -> IO PubKey) -> IO PubKey
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
name ((Ptr CChar -> IO PubKey) -> IO PubKey)
-> (Ptr CChar -> IO PubKey) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
namePtr -> do
        (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey ((Ptr BotanPubKey -> IO CInt) -> IO PubKey)
-> (Ptr BotanPubKey -> IO CInt) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPubKey
out -> Ptr BotanPubKey -> BotanMP -> BotanMP -> ConstPtr CChar -> IO CInt
load Ptr BotanPubKey
out BotanMP
xPtr BotanMP
yPtr (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
namePtr)
    [BotanMP]
_ -> [Char] -> IO PubKey
forall a. HasCallStack => [Char] -> a
error [Char]
"mkPubKeyLoad2_name: impossible"

mkPubKeyLoad3
    :: (Ptr BotanPubKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
    -> MP -> MP -> MP -> IO PubKey
mkPubKeyLoad3 :: (Ptr BotanPubKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
-> MP -> MP -> MP -> IO PubKey
mkPubKeyLoad3 Ptr BotanPubKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load MP
a MP
b MP
c = (forall a. MP -> (BotanMP -> IO a) -> IO a)
-> [MP] -> ([BotanMP] -> IO PubKey) -> IO PubKey
forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany MP -> (BotanMP -> IO a) -> IO a
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP [MP
a,MP
b,MP
c] (([BotanMP] -> IO PubKey) -> IO PubKey)
-> ([BotanMP] -> IO PubKey) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \case
    [BotanMP
aPtr,BotanMP
bPtr,BotanMP
cPtr] -> do
      (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey ((Ptr BotanPubKey -> IO CInt) -> IO PubKey)
-> (Ptr BotanPubKey -> IO CInt) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPubKey
out -> Ptr BotanPubKey -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load Ptr BotanPubKey
out BotanMP
aPtr BotanMP
bPtr BotanMP
cPtr
    [BotanMP]
_ -> [Char] -> IO PubKey
forall a. HasCallStack => [Char] -> a
error [Char]
"mkPubKeyLoad3: impossible"

mkPubKeyLoad4
    :: (Ptr BotanPubKey -> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
    -> MP -> MP -> MP -> MP -> IO PubKey
mkPubKeyLoad4 :: (Ptr BotanPubKey
 -> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt)
-> MP -> MP -> MP -> MP -> IO PubKey
mkPubKeyLoad4 Ptr BotanPubKey
-> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load MP
a MP
b MP
c MP
d = (forall a. MP -> (BotanMP -> IO a) -> IO a)
-> [MP] -> ([BotanMP] -> IO PubKey) -> IO PubKey
forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany MP -> (BotanMP -> IO a) -> IO a
forall a. MP -> (BotanMP -> IO a) -> IO a
withMP [MP
a,MP
b,MP
c,MP
d] (([BotanMP] -> IO PubKey) -> IO PubKey)
-> ([BotanMP] -> IO PubKey) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \case
    [BotanMP
aPtr,BotanMP
bPtr,BotanMP
cPtr,BotanMP
dPtr] -> do
      (Ptr BotanPubKey -> IO CInt) -> IO PubKey
createPubKey ((Ptr BotanPubKey -> IO CInt) -> IO PubKey)
-> (Ptr BotanPubKey -> IO CInt) -> IO PubKey
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPubKey
out -> Ptr BotanPubKey
-> BotanMP -> BotanMP -> BotanMP -> BotanMP -> IO CInt
load Ptr BotanPubKey
out BotanMP
aPtr BotanMP
bPtr BotanMP
cPtr BotanMP
dPtr
    [BotanMP]
_ -> [Char] -> IO PubKey
forall a. HasCallStack => [Char] -> a
error [Char]
"mkPubKeyLoad4: impossible"

withMany ::
     (forall a . object -> (cobject -> IO a) -> IO a)
  -> [object]
  -> ([cobject] -> IO b)
  -> IO b
withMany :: forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany forall a. object -> (cobject -> IO a) -> IO a
_withObject []         [cobject] -> IO b
act = [cobject] -> IO b
act []
withMany forall a. object -> (cobject -> IO a) -> IO a
withObject  (object
obj:[object]
objs) [cobject] -> IO b
act = object -> (cobject -> IO b) -> IO b
forall a. object -> (cobject -> IO a) -> IO a
withObject object
obj ((cobject -> IO b) -> IO b) -> (cobject -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ cobject
cobj -> (forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
forall object cobject b.
(forall a. object -> (cobject -> IO a) -> IO a)
-> [object] -> ([cobject] -> IO b) -> IO b
withMany object -> (cobject -> IO a) -> IO a
forall a. object -> (cobject -> IO a) -> IO a
withObject [object]
objs ([cobject] -> IO b
act ([cobject] -> IO b)
-> ([cobject] -> [cobject]) -> [cobject] -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (cobject
cobj:))