{-# LANGUAGE RecordWildCards #-}

module Crypto.HPKE.KeyPair (
    genKeyPair,
    toPublicKey,
) where

import qualified Control.Exception as E
import Crypto.KEM (generateKeyPair)

import Crypto.HPKE.ID
import Crypto.HPKE.KEM (HPKEKEM (..), KEMAlg (..))
import Crypto.HPKE.Map
import Crypto.HPKE.Types

----------------------------------------------------------------

-- | Generating a pair of public key and secret key based on
-- 'KEM_ID'.
genKeyPair
    :: HPKEMap -> KEM_ID -> IO (EncodedPublicKey, EncodedSecretKey)
genKeyPair :: HPKEMap -> KEM_ID -> IO (EncodedPublicKey, EncodedSecretKey)
genKeyPair HPKEMap{[(AEAD_ID, AEADCipher)]
[(KDF_ID, KDFHash)]
[(KEM_ID, KEMAlg)]
kemMap :: [(KEM_ID, KEMAlg)]
kdfMap :: [(KDF_ID, KDFHash)]
cipherMap :: [(AEAD_ID, AEADCipher)]
cipherMap :: HPKEMap -> [(AEAD_ID, AEADCipher)]
kdfMap :: HPKEMap -> [(KDF_ID, KDFHash)]
kemMap :: HPKEMap -> [(KEM_ID, KEMAlg)]
..} KEM_ID
kem_id = case KEM_ID -> [(KEM_ID, KEMAlg)] -> Maybe KEMAlg
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup KEM_ID
kem_id [(KEM_ID, KEMAlg)]
kemMap of
    Maybe KEMAlg
Nothing -> HPKEError -> IO (EncodedPublicKey, EncodedSecretKey)
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO (HPKEError -> IO (EncodedPublicKey, EncodedSecretKey))
-> HPKEError -> IO (EncodedPublicKey, EncodedSecretKey)
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
Unsupported (String -> HPKEError) -> String -> HPKEError
forall a b. (a -> b) -> a -> b
$ KEM_ID -> String
forall a. Show a => a -> String
show KEM_ID
kem_id
    Just (KEMAlg Proxy kem
kem) -> Proxy kem -> IO (EncapsulationKey kem, DecapsulationKey kem)
forall kem (m :: * -> *) (proxy :: * -> *).
(KEM kem, MonadRandom m) =>
proxy kem -> m (EncapsulationKey kem, DecapsulationKey kem)
forall (m :: * -> *) (proxy :: * -> *).
MonadRandom m =>
proxy kem -> m (EncapsulationKey kem, DecapsulationKey kem)
generateKeyPair Proxy kem
kem

-- | The public key that goes with a secret key, for the KEM named by the
-- 'KEM_ID'.  A receiver authenticating a sender is given the sender's
-- public key; this is how the sender arrives at one to publish.
toPublicKey
    :: HPKEMap
    -> KEM_ID
    -> EncodedSecretKey
    -> Either HPKEError EncodedPublicKey
toPublicKey :: HPKEMap
-> KEM_ID -> EncodedSecretKey -> Either HPKEError EncodedPublicKey
toPublicKey HPKEMap{[(AEAD_ID, AEADCipher)]
[(KDF_ID, KDFHash)]
[(KEM_ID, KEMAlg)]
cipherMap :: HPKEMap -> [(AEAD_ID, AEADCipher)]
kdfMap :: HPKEMap -> [(KDF_ID, KDFHash)]
kemMap :: HPKEMap -> [(KEM_ID, KEMAlg)]
kemMap :: [(KEM_ID, KEMAlg)]
kdfMap :: [(KDF_ID, KDFHash)]
cipherMap :: [(AEAD_ID, AEADCipher)]
..} KEM_ID
kem_id EncodedSecretKey
skm = case KEM_ID -> [(KEM_ID, KEMAlg)] -> Maybe KEMAlg
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup KEM_ID
kem_id [(KEM_ID, KEMAlg)]
kemMap of
    Maybe KEMAlg
Nothing -> HPKEError -> Either HPKEError EncodedPublicKey
forall a b. a -> Either a b
Left (HPKEError -> Either HPKEError EncodedPublicKey)
-> HPKEError -> Either HPKEError EncodedPublicKey
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
Unsupported (String -> HPKEError) -> String -> HPKEError
forall a b. (a -> b) -> a -> b
$ KEM_ID -> String
forall a. Show a => a -> String
show KEM_ID
kem_id
    Just (KEMAlg Proxy kem
kem) -> Proxy kem -> EncodedSecretKey -> Either HPKEError EncodedPublicKey
forall kem (proxy :: * -> *).
HPKEKEM kem =>
proxy kem -> EncodedSecretKey -> Either HPKEError EncodedPublicKey
forall (proxy :: * -> *).
proxy kem -> EncodedSecretKey -> Either HPKEError EncodedPublicKey
toEncapsulationKey Proxy kem
kem EncodedSecretKey
skm