{-|
Module      : Botan.PubKey.Verify
Description : Signature Verification
Copyright   : (c) Leo D, 2023
License     : BSD-3-Clause
Maintainer  : leo@apotheca.io
Stability   : experimental
Portability : POSIX
-}

module Botan.PubKey.Verify
(

-- * Thing
-- $introduction

-- * Usage
-- $usage

-- * Public Key Signature Verification

-- TODO: Rename pkVerifySignature?
  pkVerify

-- ** Data type
, PKVerify(..)

-- ** Destructor
, destroyPKVerify

-- ** Initializers
, newPKVerify

-- ** Algorithm
, pkVerifyUpdate
, pkVerifyFinish

) where

import qualified Data.ByteString as ByteString

import qualified Botan.Low.PubKey.Verify as Low

import Botan.Error
import Botan.Prelude
import Botan.PubKey
import Botan.PubKey.Sign

{- $introduction

-}

{- $usage

-}

--
-- Public Key Signatures
--

pkVerify :: PubKey -> PKSignAlgo -> PKSignatureFormat -> ByteString -> PKSignature -> Bool
pkVerify :: PubKey
-> PKSignAlgo
-> PKSignatureFormat
-> ByteString
-> ByteString
-> Bool
pkVerify PubKey
pk PKSignAlgo
algo PKSignatureFormat
fmt ByteString
msg ByteString
sig = IO Bool -> Bool
forall a. IO a -> a
unsafePerformIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$ do
    PKVerify
verifier <- PubKey -> PKSignAlgo -> PKSignatureFormat -> IO PKVerify
forall (m :: * -> *).
MonadIO m =>
PubKey -> PKSignAlgo -> PKSignatureFormat -> m PKVerify
newPKVerify PubKey
pk PKSignAlgo
algo PKSignatureFormat
fmt
    PKVerify -> ByteString -> IO ()
forall (m :: * -> *). MonadIO m => PKVerify -> ByteString -> m ()
pkVerifyUpdate PKVerify
verifier ByteString
msg
    PKVerify -> ByteString -> IO Bool
forall (m :: * -> *). MonadIO m => PKVerify -> ByteString -> m Bool
pkVerifyFinish PKVerify
verifier ByteString
sig
{-# NOINLINE pkVerify #-}

-- Data type
type PKVerify = Low.Verify

-- ** Destructor
destroyPKVerify :: (MonadIO m) => PKVerify -> m ()
destroyPKVerify :: forall (m :: * -> *). MonadIO m => PKVerify -> m ()
destroyPKVerify = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (PKVerify -> IO ()) -> PKVerify -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PKVerify -> IO ()
Low.verifyDestroy

-- ** Initializers

newPKVerify :: (MonadIO m) => PubKey -> PKSignAlgo -> PKSignatureFormat -> m PKVerify
newPKVerify :: forall (m :: * -> *).
MonadIO m =>
PubKey -> PKSignAlgo -> PKSignatureFormat -> m PKVerify
newPKVerify PubKey
pk PKSignAlgo
algo PKSignatureFormat
fmt = IO PKVerify -> m PKVerify
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO PKVerify -> m PKVerify) -> IO PKVerify -> m PKVerify
forall a b. (a -> b) -> a -> b
$ PubKey -> ByteString -> PKSignatureFormat -> IO PKVerify
Low.verifyCreate PubKey
pk (PKSignAlgo -> ByteString
signAlgoName PKSignAlgo
algo) PKSignatureFormat
fmt

-- ** Mutable Algorithm

pkVerifyUpdate :: (MonadIO m) => PKVerify -> ByteString -> m ()
pkVerifyUpdate :: forall (m :: * -> *). MonadIO m => PKVerify -> ByteString -> m ()
pkVerifyUpdate PKVerify
verifier ByteString
msg = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ PKVerify -> ByteString -> IO ()
Low.verifyUpdate PKVerify
verifier ByteString
msg

pkVerifyFinish :: (MonadIO m) => PKVerify -> PKSignature -> m Bool
pkVerifyFinish :: forall (m :: * -> *). MonadIO m => PKVerify -> ByteString -> m Bool
pkVerifyFinish PKVerify
verifier ByteString
sig = IO Bool -> m Bool
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ PKVerify -> ByteString -> IO Bool
Low.verifyFinish PKVerify
verifier ByteString
sig