{-|
Module      : Botan.Low.Verify
Description : Signature Verification
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
-}

module Botan.Low.PubKey.Verify (

  -- * Public key signature verification
    Verify(..)
  , withVerify
  , verifyCreate
  , verifyDestroy
  , verifyUpdate
  , verifyFinish

  ) where

import           Botan.Bindings.ConstPtr (ConstPtr (..))
import           Botan.Bindings.PubKey.Verify
import           Botan.Low.Error.Internal
import           Botan.Low.Internal.ByteString
import           Botan.Low.PubKey
import           Botan.Low.PubKey.Sign (SigningFlags, signingFlags)
import           Botan.Low.Remake
import           Data.ByteString (ByteString)
import           Foreign.C.Types
import           Foreign.ForeignPtr
import           Foreign.Ptr

-- /*
-- * Signature Verification
-- */

newtype Verify = MkVerify { Verify -> ForeignPtr BotanPKOpVerifyStruct
foreignPtr :: ForeignPtr BotanPKOpVerifyStruct }

withVerify     :: Verify -> (BotanPKOpVerify -> IO a) -> IO a
verifyDestroy  :: Verify -> IO ()
createVerify   :: (Ptr BotanPKOpVerify -> IO CInt) -> IO Verify
(Verify -> (BotanPKOpVerify -> IO a) -> IO a
withVerify, Verify -> IO ()
verifyDestroy, (Ptr BotanPKOpVerify -> IO CInt) -> IO Verify
createVerify)
    = (Ptr BotanPKOpVerifyStruct -> BotanPKOpVerify)
-> (BotanPKOpVerify -> Ptr BotanPKOpVerifyStruct)
-> (ForeignPtr BotanPKOpVerifyStruct -> Verify)
-> (Verify -> ForeignPtr BotanPKOpVerifyStruct)
-> FinalizerPtr BotanPKOpVerifyStruct
-> (Verify -> (BotanPKOpVerify -> IO a) -> IO a, Verify -> IO (),
    (Ptr BotanPKOpVerify -> IO CInt) -> IO Verify)
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 BotanPKOpVerifyStruct -> BotanPKOpVerify
MkBotanPKOpVerify (.ptr)
        ForeignPtr BotanPKOpVerifyStruct -> Verify
MkVerify (.foreignPtr)
        FinalizerPtr BotanPKOpVerifyStruct
botan_pk_op_verify_destroy

verifyCreate
    :: PubKey       -- ^ __key__
    -> EMSAName     -- ^ __hash_and_padding__
    -> SigningFlags -- ^ __flags__
    -> IO Verify    -- ^ __op__
verifyCreate :: PubKey -> EMSAName -> SigningFlags -> IO Verify
verifyCreate PubKey
pk EMSAName
algo SigningFlags
flags =  PubKey -> (BotanPubKey -> IO Verify) -> IO Verify
forall a. PubKey -> (BotanPubKey -> IO a) -> IO a
withPubKey PubKey
pk ((BotanPubKey -> IO Verify) -> IO Verify)
-> (BotanPubKey -> IO Verify) -> IO Verify
forall a b. (a -> b) -> a -> b
$ \ BotanPubKey
pkPtr -> do
    EMSAName -> (Ptr CChar -> IO Verify) -> IO Verify
forall a. EMSAName -> (Ptr CChar -> IO a) -> IO a
asCString EMSAName
algo ((Ptr CChar -> IO Verify) -> IO Verify)
-> (Ptr CChar -> IO Verify) -> IO Verify
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
algoPtr -> do
        (Ptr BotanPKOpVerify -> IO CInt) -> IO Verify
createVerify ((Ptr BotanPKOpVerify -> IO CInt) -> IO Verify)
-> (Ptr BotanPKOpVerify -> IO CInt) -> IO Verify
forall a b. (a -> b) -> a -> b
$ \ Ptr BotanPKOpVerify
out -> Ptr BotanPKOpVerify
-> BotanPubKey -> ConstPtr CChar -> Word32 -> IO CInt
botan_pk_op_verify_create
            Ptr BotanPKOpVerify
out
            BotanPubKey
pkPtr
            (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
algoPtr)
            (SigningFlags -> Word32
signingFlags SigningFlags
flags)

verifyUpdate
    :: Verify       -- ^ __op__
    -> ByteString   -- ^ __in[]__
    -> IO ()
verifyUpdate :: Verify -> EMSAName -> IO ()
verifyUpdate = (forall a. Verify -> (BotanPKOpVerify -> IO a) -> IO a)
-> (BotanPKOpVerify -> ConstPtr Word8 -> CSize -> IO CInt)
-> Verify
-> EMSAName
-> IO ()
forall object botan.
(forall a. object -> (botan -> IO a) -> IO a)
-> (botan -> ConstPtr Word8 -> CSize -> IO CInt)
-> object
-> EMSAName
-> IO ()
mkWithObjectSetterCBytesLen Verify -> (BotanPKOpVerify -> IO a) -> IO a
forall a. Verify -> (BotanPKOpVerify -> IO a) -> IO a
withVerify BotanPKOpVerify -> ConstPtr Word8 -> CSize -> IO CInt
botan_pk_op_verify_update

-- TODO: Signature type
verifyFinish ::
     Verify       -- ^ __op__
  -> ByteString   -- ^ __sig[]__
  -> IO Bool
verifyFinish :: Verify -> EMSAName -> IO Bool
verifyFinish Verify
verify EMSAName
sig =
    Verify -> (BotanPKOpVerify -> IO Bool) -> IO Bool
forall a. Verify -> (BotanPKOpVerify -> IO a) -> IO a
withVerify Verify
verify ((BotanPKOpVerify -> IO Bool) -> IO Bool)
-> (BotanPKOpVerify -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ BotanPKOpVerify
verifyPtr ->
    EMSAName -> (Ptr Word8 -> CSize -> IO Bool) -> IO Bool
forall byte a. EMSAName -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen EMSAName
sig ((Ptr Word8 -> CSize -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> CSize -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
sigPtr CSize
sigLen ->
    HasCallStack => IO CInt -> IO Bool
IO CInt -> IO Bool
throwBotanCatchingInvalidVerifier (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ BotanPKOpVerify -> ConstPtr Word8 -> CSize -> IO CInt
botan_pk_op_verify_finish
      BotanPKOpVerify
verifyPtr
      (Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
sigPtr)
      CSize
sigLen