{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Crypto.HPKE.Setup (
    setupBaseS,
    setupBaseR,
    setupPSKS,
    setupPSKR,
    setupS,
    setupR,
) where

import qualified Control.Exception as E

import Crypto.KEM (encapsulate, encapsulateWith, decapsulate)

import Crypto.HPKE.AEAD
import Crypto.HPKE.Context
import Crypto.HPKE.ID
import Crypto.HPKE.KDF
import Crypto.HPKE.KEM
import Crypto.HPKE.KeySchedule
import Crypto.HPKE.Map
import Crypto.HPKE.Types

-- | Setting up base/auth mode for a sender.
--   This throws 'HPKEError'.
setupBaseS
    :: KEM_ID
    -> KDF_ID
    -> AEAD_ID
    -> Maybe EncodedSecretKey
    -- ^ My ephemeral secret key. Automatically generated if 'Nothing'
    -> Maybe EncodedSecretKey
    -- ^ My secret key for authentication.
    --   @mode_base@ is used if 'Nothing'. @mode_auth@ is used, otherwise.
    -> EncodedPublicKey
    -- ^ Peer's public key.
    -> Info
    -> IO (EncodedPublicKey, ContextS)
setupBaseS :: KEM_ID
-> KDF_ID
-> AEAD_ID
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> ByteString
-> IO (EncodedPublicKey, ContextS)
setupBaseS KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id Maybe EncodedSecretKey
mskEm Maybe EncodedSecretKey
mskSm EncodedPublicKey
pkRm ByteString
info =
    HPKEMap
-> Mode
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO (EncodedPublicKey, ContextS)
setupS HPKEMap
defaultHPKEMap Mode
mode KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id Maybe EncodedSecretKey
mskEm Maybe EncodedSecretKey
mskSm EncodedPublicKey
pkRm ByteString
info ByteString
"" ByteString
""
  where
    mode :: Mode
mode = case Maybe EncodedSecretKey
mskSm of
        Maybe EncodedSecretKey
Nothing -> Mode
ModeBase
        Maybe EncodedSecretKey
_ -> Mode
ModeAuth

-- | Setting up base/auth mode for a receiver with its key pair.
--   This throws 'HPKEError'.
setupBaseR
    :: KEM_ID
    -> KDF_ID
    -> AEAD_ID
    -> EncodedSecretKey
    -- ^ My secret key
    -> Maybe EncodedPublicKey
    -- ^ The sender's public key, for authentication.
    --   @mode_base@ is used if 'Nothing'. @mode_auth@ is used, otherwise.
    -> EncodedPublicKey
    -- ^ The encapsulated key, @enc@.
    -> Info
    -> IO ContextR
setupBaseR :: KEM_ID
-> KDF_ID
-> AEAD_ID
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> ByteString
-> IO ContextR
setupBaseR KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id EncodedSecretKey
skRm Maybe EncodedPublicKey
mpkSm EncodedPublicKey
enc ByteString
info =
    HPKEMap
-> Mode
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO ContextR
setupR HPKEMap
defaultHPKEMap Mode
mode KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id EncodedSecretKey
skRm Maybe EncodedPublicKey
mpkSm EncodedPublicKey
enc ByteString
info ByteString
"" ByteString
""
  where
    mode :: Mode
mode = case Maybe EncodedPublicKey
mpkSm of
        Maybe EncodedPublicKey
Nothing -> Mode
ModeBase
        Maybe EncodedPublicKey
_ -> Mode
ModeAuth

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

-- | Setting up psk/auth_psk mode for a sender.
--   This throws 'HPKEError'.
setupPSKS
    :: KEM_ID
    -> KDF_ID
    -> AEAD_ID
    -> Maybe EncodedSecretKey
    -- ^ My ephemeral secret key. Automatically generated if 'Nothing'
    -> Maybe EncodedSecretKey
    -- ^ My secret key for authentication.
    --   @mode_psk@ is used if 'Nothing'. @mode_auth_psk@ is used, otherwise.
    -> EncodedPublicKey
    -- ^ Peer's public key.
    -> Info
    -> PSK
    -> PSK_ID
    -> IO (EncodedPublicKey, ContextS)
setupPSKS :: KEM_ID
-> KDF_ID
-> AEAD_ID
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO (EncodedPublicKey, ContextS)
setupPSKS KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id Maybe EncodedSecretKey
mskEm Maybe EncodedSecretKey
mskSm =
    HPKEMap
-> Mode
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO (EncodedPublicKey, ContextS)
setupS HPKEMap
defaultHPKEMap Mode
mode KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id Maybe EncodedSecretKey
mskEm Maybe EncodedSecretKey
mskSm
  where
    mode :: Mode
mode = case Maybe EncodedSecretKey
mskSm of
        Maybe EncodedSecretKey
Nothing -> Mode
ModePsk
        Maybe EncodedSecretKey
_ -> Mode
ModeAuthPsk

-- | Setting up psk/auth_psk mode for a receiver with its key pair.
--   This throws 'HPKEError'.
setupPSKR
    :: KEM_ID
    -> KDF_ID
    -> AEAD_ID
    -> EncodedSecretKey
    -- ^ My secret key
    -> Maybe EncodedPublicKey
    -- ^ The sender's public key, for authentication.
    --   @mode_psk@ is used if 'Nothing'. @mode_auth_psk@ is used, otherwise.
    -> EncodedPublicKey
    -- ^ The encapsulated key, @enc@.
    -> Info
    -> PSK
    -> PSK_ID
    -> IO ContextR
setupPSKR :: KEM_ID
-> KDF_ID
-> AEAD_ID
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO ContextR
setupPSKR KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id EncodedSecretKey
skRm Maybe EncodedPublicKey
mpkSm =
    HPKEMap
-> Mode
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO ContextR
setupR HPKEMap
defaultHPKEMap Mode
mode KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id EncodedSecretKey
skRm Maybe EncodedPublicKey
mpkSm
  where
    mode :: Mode
mode = case Maybe EncodedPublicKey
mpkSm of
        Maybe EncodedPublicKey
Nothing -> Mode
ModePsk
        Maybe EncodedPublicKey
_ -> Mode
ModeAuthPsk

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

setupS
    :: HPKEMap
    -> Mode
    -> KEM_ID
    -> KDF_ID
    -> AEAD_ID
    -> Maybe EncodedSecretKey
    -- ^ My ephemeral secret key. Automatically generated if 'Nothing'
    -> Maybe EncodedSecretKey
    -- ^ My secret key, for the authenticated modes.
    -> EncodedPublicKey
    -- ^ Peer's public key.
    -> Info
    -> PSK
    -> PSK_ID
    -> IO (EncodedPublicKey, ContextS)
setupS :: HPKEMap
-> Mode
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO (EncodedPublicKey, ContextS)
setupS HPKEMap
hpkeMap Mode
mode KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id Maybe EncodedSecretKey
mskEm Maybe EncodedSecretKey
mskSm EncodedPublicKey
pkRm ByteString
info ByteString
psk ByteString
psk_id = do
    Mode -> ByteString -> ByteString -> IO ()
verifyPSKInput Mode
mode ByteString
psk ByteString
psk_id
    let r :: Either HPKEError (KEMAlg, KDFHash, AEADCipher)
r = HPKEMap
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> Either HPKEError (KEMAlg, KDFHash, AEADCipher)
look HPKEMap
hpkeMap KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id
    Either HPKEError (KEMAlg, KDFHash, AEADCipher)
-> ((KEMAlg, KDFHash, AEADCipher)
    -> IO (EncodedPublicKey, ContextS))
-> IO (EncodedPublicKey, ContextS)
forall v a. Either HPKEError v -> (v -> IO a) -> IO a
throwOnError Either HPKEError (KEMAlg, KDFHash, AEADCipher)
r (((KEMAlg, KDFHash, AEADCipher) -> IO (EncodedPublicKey, ContextS))
 -> IO (EncodedPublicKey, ContextS))
-> ((KEMAlg, KDFHash, AEADCipher)
    -> IO (EncodedPublicKey, ContextS))
-> IO (EncodedPublicKey, ContextS)
forall a b. (a -> b) -> a -> b
$ \(KEMAlg Proxy kem
kem, KDFHash h
h', AEADCipher Proxy a
c) -> do
        encapped <- Proxy kem
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
forall kem (proxy :: * -> *).
HPKEKEM kem =>
proxy kem
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
hpkeEncap Proxy kem
kem Maybe EncodedSecretKey
mskEm Maybe EncodedSecretKey
mskSm EncodedPublicKey
pkRm
        throwOnError encapped $ \(EncodedPublicKey
enc, SharedSecret
shared_secret) -> do
            let (Int
nk, Int
nn, ByteString -> Seal
seal', ByteString -> Seal
_) = Proxy a -> (Int, Int, ByteString -> Seal, ByteString -> Seal)
forall a.
Aead a =>
Proxy a -> (Int, Int, ByteString -> Seal, ByteString -> Seal)
aeadParams Proxy a
c
                suite' :: ByteString
suite' = KEM_ID -> KDF_ID -> AEAD_ID -> ByteString
suiteHPKE KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id
                keys :: Either HPKEError (ByteString, ByteString, PRK h)
keys = h
-> ByteString
-> Int
-> Int
-> Mode
-> ByteString
-> ByteString
-> ByteString
-> SharedSecret
-> Either HPKEError (ByteString, ByteString, PRK h)
forall h.
(HashAlgorithm h, KDF h) =>
h
-> ByteString
-> Int
-> Int
-> Mode
-> ByteString
-> ByteString
-> ByteString
-> SharedSecret
-> Either HPKEError (ByteString, ByteString, PRK h)
keySchedule h
h' ByteString
suite' Int
nk Int
nn Mode
mode ByteString
info ByteString
psk ByteString
psk_id SharedSecret
shared_secret
            Either HPKEError (ByteString, ByteString, PRK h)
-> ((ByteString, ByteString, PRK h)
    -> IO (EncodedPublicKey, ContextS))
-> IO (EncodedPublicKey, ContextS)
forall v a. Either HPKEError v -> (v -> IO a) -> IO a
throwOnError Either HPKEError (ByteString, ByteString, PRK h)
keys (((ByteString, ByteString, PRK h)
  -> IO (EncodedPublicKey, ContextS))
 -> IO (EncodedPublicKey, ContextS))
-> ((ByteString, ByteString, PRK h)
    -> IO (EncodedPublicKey, ContextS))
-> IO (EncodedPublicKey, ContextS)
forall a b. (a -> b) -> a -> b
$ \(ByteString
key, ByteString
nonce, PRK h
prk) -> do
                let expand' :: ByteString -> Int -> Either HPKEError ByteString
expand' = ByteString
-> PRK h
-> ByteString
-> ByteString
-> Int
-> Either HPKEError ByteString
forall h.
KDF h =>
ByteString
-> PRK h
-> ByteString
-> ByteString
-> Int
-> Either HPKEError ByteString
labeledExpand ByteString
suite' PRK h
prk ByteString
"sec"
                ctx <- ByteString
-> ByteString
-> (ByteString -> Seal)
-> (ByteString -> Int -> Either HPKEError ByteString)
-> IO ContextS
newContextS ByteString
key ByteString
nonce ByteString -> Seal
seal' ByteString -> Int -> Either HPKEError ByteString
expand'
                return (enc, ctx)

setupR
    :: HPKEMap
    -> Mode
    -> KEM_ID
    -> KDF_ID
    -> AEAD_ID
    -> EncodedSecretKey
    -- ^ My secret key
    -> Maybe EncodedPublicKey
    -- ^ The sender's public key, for the authenticated modes.
    -> EncodedPublicKey
    -- ^ The encapsulated key, @enc@.
    -> Info
    -> PSK
    -> PSK_ID
    -> IO ContextR
setupR :: HPKEMap
-> Mode
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> ByteString
-> ByteString
-> ByteString
-> IO ContextR
setupR HPKEMap
hpkeMap Mode
mode KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id EncodedSecretKey
skRm Maybe EncodedPublicKey
mpkSm EncodedPublicKey
enc ByteString
info ByteString
psk ByteString
psk_id = do
    Mode -> ByteString -> ByteString -> IO ()
verifyPSKInput Mode
mode ByteString
psk ByteString
psk_id
    let r :: Either HPKEError (KEMAlg, KDFHash, AEADCipher)
r = HPKEMap
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> Either HPKEError (KEMAlg, KDFHash, AEADCipher)
look HPKEMap
hpkeMap KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id
    Either HPKEError (KEMAlg, KDFHash, AEADCipher)
-> ((KEMAlg, KDFHash, AEADCipher) -> IO ContextR) -> IO ContextR
forall v a. Either HPKEError v -> (v -> IO a) -> IO a
throwOnError Either HPKEError (KEMAlg, KDFHash, AEADCipher)
r (((KEMAlg, KDFHash, AEADCipher) -> IO ContextR) -> IO ContextR)
-> ((KEMAlg, KDFHash, AEADCipher) -> IO ContextR) -> IO ContextR
forall a b. (a -> b) -> a -> b
$ \(KEMAlg Proxy kem
kem, KDFHash h
h', AEADCipher Proxy a
c) -> do
        Either HPKEError SharedSecret
-> (SharedSecret -> IO ContextR) -> IO ContextR
forall v a. Either HPKEError v -> (v -> IO a) -> IO a
throwOnError (Proxy kem
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
forall kem (proxy :: * -> *).
HPKEKEM kem =>
proxy kem
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
hpkeDecap Proxy kem
kem EncodedSecretKey
skRm Maybe EncodedPublicKey
mpkSm EncodedPublicKey
enc) ((SharedSecret -> IO ContextR) -> IO ContextR)
-> (SharedSecret -> IO ContextR) -> IO ContextR
forall a b. (a -> b) -> a -> b
$ \SharedSecret
shared_secret -> do
            let (Int
nk, Int
nn, ByteString -> Seal
_, ByteString -> Seal
open') = Proxy a -> (Int, Int, ByteString -> Seal, ByteString -> Seal)
forall a.
Aead a =>
Proxy a -> (Int, Int, ByteString -> Seal, ByteString -> Seal)
aeadParams Proxy a
c
                suite' :: ByteString
suite' = KEM_ID -> KDF_ID -> AEAD_ID -> ByteString
suiteHPKE KEM_ID
kem_id KDF_ID
kdf_id AEAD_ID
aead_id
                keys :: Either HPKEError (ByteString, ByteString, PRK h)
keys = h
-> ByteString
-> Int
-> Int
-> Mode
-> ByteString
-> ByteString
-> ByteString
-> SharedSecret
-> Either HPKEError (ByteString, ByteString, PRK h)
forall h.
(HashAlgorithm h, KDF h) =>
h
-> ByteString
-> Int
-> Int
-> Mode
-> ByteString
-> ByteString
-> ByteString
-> SharedSecret
-> Either HPKEError (ByteString, ByteString, PRK h)
keySchedule h
h' ByteString
suite' Int
nk Int
nn Mode
mode ByteString
info ByteString
psk ByteString
psk_id SharedSecret
shared_secret
            Either HPKEError (ByteString, ByteString, PRK h)
-> ((ByteString, ByteString, PRK h) -> IO ContextR) -> IO ContextR
forall v a. Either HPKEError v -> (v -> IO a) -> IO a
throwOnError Either HPKEError (ByteString, ByteString, PRK h)
keys (((ByteString, ByteString, PRK h) -> IO ContextR) -> IO ContextR)
-> ((ByteString, ByteString, PRK h) -> IO ContextR) -> IO ContextR
forall a b. (a -> b) -> a -> b
$ \(ByteString
key, ByteString
nonce, PRK h
prk) -> do
                let expand' :: ByteString -> Int -> Either HPKEError ByteString
expand' = ByteString
-> PRK h
-> ByteString
-> ByteString
-> Int
-> Either HPKEError ByteString
forall h.
KDF h =>
ByteString
-> PRK h
-> ByteString
-> ByteString
-> Int
-> Either HPKEError ByteString
labeledExpand ByteString
suite' PRK h
prk ByteString
"sec"
                ByteString
-> ByteString
-> (ByteString -> Seal)
-> (ByteString -> Int -> Either HPKEError ByteString)
-> IO ContextR
newContextR ByteString
key ByteString
nonce ByteString -> Seal
open' ByteString -> Int -> Either HPKEError ByteString
expand'

-- | The four ways RFC 9180 reaches a shared secret on the sending side.
-- The two unauthenticated ones are @crypton@'s 'encapsulate' and
-- 'encapsulateWith'; the two authenticated ones are what 'HPKEKEM' adds,
-- and a KEM without them refuses here rather than further down.
hpkeEncap
    :: HPKEKEM kem
    => proxy kem
    -> Maybe EncodedSecretKey
    -- ^ @skE@, drawn here if absent
    -> Maybe EncodedSecretKey
    -- ^ @skS@, which makes it an authenticated mode
    -> EncodedPublicKey
    -> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
hpkeEncap :: forall kem (proxy :: * -> *).
HPKEKEM kem =>
proxy kem
-> Maybe EncodedSecretKey
-> Maybe EncodedSecretKey
-> EncodedPublicKey
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
hpkeEncap proxy kem
kem Maybe EncodedSecretKey
mskEm Maybe EncodedSecretKey
mskSm EncodedPublicKey
pkRm = case (Maybe EncodedSecretKey
mskEm, Maybe EncodedSecretKey
mskSm) of
    (Maybe EncodedSecretKey
Nothing, Maybe EncodedSecretKey
Nothing) -> (String -> HPKEError)
-> CryptoFailable (EncodedPublicKey, SharedSecret)
-> Either HPKEError (EncodedPublicKey, SharedSecret)
forall a.
(String -> HPKEError) -> CryptoFailable a -> Either HPKEError a
toHPKEError String -> HPKEError
EncapError (CryptoFailable (EncodedPublicKey, SharedSecret)
 -> Either HPKEError (EncodedPublicKey, SharedSecret))
-> IO (CryptoFailable (EncodedPublicKey, SharedSecret))
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> proxy kem
-> EncapsulationKey kem
-> IO (CryptoFailable (Ciphertext kem, SharedSecret))
forall kem (m :: * -> *) (proxy :: * -> *).
(KEM kem, MonadRandom m) =>
proxy kem
-> EncapsulationKey kem
-> m (CryptoFailable (Ciphertext kem, SharedSecret))
forall (m :: * -> *) (proxy :: * -> *).
MonadRandom m =>
proxy kem
-> EncapsulationKey kem
-> m (CryptoFailable (Ciphertext kem, SharedSecret))
encapsulate proxy kem
kem EncodedPublicKey
EncapsulationKey kem
pkRm
    (Just EncodedSecretKey
skEm, Maybe EncodedSecretKey
Nothing) ->
        Either HPKEError (EncodedPublicKey, SharedSecret)
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either HPKEError (EncodedPublicKey, SharedSecret)
 -> IO (Either HPKEError (EncodedPublicKey, SharedSecret)))
-> Either HPKEError (EncodedPublicKey, SharedSecret)
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
forall a b. (a -> b) -> a -> b
$ (String -> HPKEError)
-> CryptoFailable (EncodedPublicKey, SharedSecret)
-> Either HPKEError (EncodedPublicKey, SharedSecret)
forall a.
(String -> HPKEError) -> CryptoFailable a -> Either HPKEError a
toHPKEError String -> HPKEError
EncapError (CryptoFailable (EncodedPublicKey, SharedSecret)
 -> Either HPKEError (EncodedPublicKey, SharedSecret))
-> CryptoFailable (EncodedPublicKey, SharedSecret)
-> Either HPKEError (EncodedPublicKey, SharedSecret)
forall a b. (a -> b) -> a -> b
$ proxy kem
-> EncapsulationKey kem
-> Coins kem
-> CryptoFailable (Ciphertext kem, SharedSecret)
forall kem (proxy :: * -> *).
KEM kem =>
proxy kem
-> EncapsulationKey kem
-> Coins kem
-> CryptoFailable (Ciphertext kem, SharedSecret)
forall (proxy :: * -> *).
proxy kem
-> EncapsulationKey kem
-> Coins kem
-> CryptoFailable (Ciphertext kem, SharedSecret)
encapsulateWith proxy kem
kem EncodedPublicKey
EncapsulationKey kem
pkRm EncodedSecretKey
Coins kem
skEm
    (Maybe EncodedSecretKey
Nothing, Just EncodedSecretKey
skSm) -> do
        skEm <- proxy kem -> IO EncodedSecretKey
forall kem (m :: * -> *) (proxy :: * -> *).
(HPKEKEM kem, MonadRandom m) =>
proxy kem -> m EncodedSecretKey
forall (m :: * -> *) (proxy :: * -> *).
MonadRandom m =>
proxy kem -> m EncodedSecretKey
generateCoins proxy kem
kem
        return $ authEncapWith kem skSm pkRm skEm
    (Just EncodedSecretKey
skEm, Just EncodedSecretKey
skSm) -> Either HPKEError (EncodedPublicKey, SharedSecret)
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either HPKEError (EncodedPublicKey, SharedSecret)
 -> IO (Either HPKEError (EncodedPublicKey, SharedSecret)))
-> Either HPKEError (EncodedPublicKey, SharedSecret)
-> IO (Either HPKEError (EncodedPublicKey, SharedSecret))
forall a b. (a -> b) -> a -> b
$ proxy kem
-> EncodedSecretKey
-> EncodedPublicKey
-> EncodedSecretKey
-> Either HPKEError (EncodedPublicKey, SharedSecret)
forall kem (proxy :: * -> *).
HPKEKEM kem =>
proxy kem
-> EncodedSecretKey
-> EncodedPublicKey
-> EncodedSecretKey
-> Either HPKEError (EncodedPublicKey, SharedSecret)
forall (proxy :: * -> *).
proxy kem
-> EncodedSecretKey
-> EncodedPublicKey
-> EncodedSecretKey
-> Either HPKEError (EncodedPublicKey, SharedSecret)
authEncapWith proxy kem
kem EncodedSecretKey
skSm EncodedPublicKey
pkRm EncodedSecretKey
skEm

-- | And the two on the receiving side.
hpkeDecap
    :: HPKEKEM kem
    => proxy kem
    -> EncodedSecretKey
    -> Maybe EncodedPublicKey
    -- ^ @pkS@, which makes it an authenticated mode
    -> EncodedPublicKey
    -> Either HPKEError SharedSecret
hpkeDecap :: forall kem (proxy :: * -> *).
HPKEKEM kem =>
proxy kem
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
hpkeDecap proxy kem
kem EncodedSecretKey
skRm Maybe EncodedPublicKey
Nothing EncodedPublicKey
enc =
    (String -> HPKEError)
-> CryptoFailable SharedSecret -> Either HPKEError SharedSecret
forall a.
(String -> HPKEError) -> CryptoFailable a -> Either HPKEError a
toHPKEError String -> HPKEError
DecapError (CryptoFailable SharedSecret -> Either HPKEError SharedSecret)
-> CryptoFailable SharedSecret -> Either HPKEError SharedSecret
forall a b. (a -> b) -> a -> b
$ proxy kem
-> DecapsulationKey kem
-> Ciphertext kem
-> CryptoFailable SharedSecret
forall kem (proxy :: * -> *).
KEM kem =>
proxy kem
-> DecapsulationKey kem
-> Ciphertext kem
-> CryptoFailable SharedSecret
forall (proxy :: * -> *).
proxy kem
-> DecapsulationKey kem
-> Ciphertext kem
-> CryptoFailable SharedSecret
decapsulate proxy kem
kem EncodedSecretKey
DecapsulationKey kem
skRm EncodedPublicKey
Ciphertext kem
enc
hpkeDecap proxy kem
kem EncodedSecretKey
skRm (Just EncodedPublicKey
pkSm) EncodedPublicKey
enc = proxy kem
-> EncodedPublicKey
-> EncodedSecretKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
forall kem (proxy :: * -> *).
HPKEKEM kem =>
proxy kem
-> EncodedPublicKey
-> EncodedSecretKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
forall (proxy :: * -> *).
proxy kem
-> EncodedPublicKey
-> EncodedSecretKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
authDecap proxy kem
kem EncodedPublicKey
pkSm EncodedSecretKey
skRm EncodedPublicKey
enc

-- | The class answers with a 'CryptoFailable', which has a reason and no
-- message.  Everything above here wants an 'HPKEError', so the reason is
-- spelled back out.
toHPKEError :: (String -> HPKEError) -> CryptoFailable a -> Either HPKEError a
toHPKEError :: forall a.
(String -> HPKEError) -> CryptoFailable a -> Either HPKEError a
toHPKEError String -> HPKEError
_ (CryptoPassed a
a) = a -> Either HPKEError a
forall a b. b -> Either a b
Right a
a
toHPKEError String -> HPKEError
con (CryptoFailed CryptoError
e) = HPKEError -> Either HPKEError a
forall a b. a -> Either a b
Left (HPKEError -> Either HPKEError a)
-> HPKEError -> Either HPKEError a
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
con (String -> HPKEError) -> String -> HPKEError
forall a b. (a -> b) -> a -> b
$ CryptoError -> String
forall a. Show a => a -> String
show CryptoError
e

aeadParams
    :: Aead a
    => Proxy a -> (Int, Int, Key -> Seal, Key -> Open)
aeadParams :: forall a.
Aead a =>
Proxy a -> (Int, Int, ByteString -> Seal, ByteString -> Seal)
aeadParams Proxy a
c = (Proxy a -> Int
forall a. Aead a => Proxy a -> Int
nK Proxy a
c, Proxy a -> Int
forall a. Aead a => Proxy a -> Int
nN Proxy a
c, Proxy a -> ByteString -> Seal
forall a. Aead a => Proxy a -> ByteString -> Seal
sealA Proxy a
c, Proxy a -> ByteString -> Seal
forall a. Aead a => Proxy a -> ByteString -> Seal
openA Proxy a
c)

throwOnError :: Either HPKEError v -> (v -> IO a) -> IO a
throwOnError :: forall v a. Either HPKEError v -> (v -> IO a) -> IO a
throwOnError (Left HPKEError
err) v -> IO a
_body = HPKEError -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO HPKEError
err
throwOnError (Right v
ss) v -> IO a
body = v -> IO a
body v
ss

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

look
    :: HPKEMap
    -> KEM_ID
    -> KDF_ID
    -> AEAD_ID
    -> Either HPKEError (KEMAlg, KDFHash, AEADCipher)
look :: HPKEMap
-> KEM_ID
-> KDF_ID
-> AEAD_ID
-> Either HPKEError (KEMAlg, KDFHash, AEADCipher)
look 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 KDF_ID
kdf_id AEAD_ID
aead_id = do
    k <- KEM_ID -> [(KEM_ID, KEMAlg)] -> Either HPKEError KEMAlg
forall k v. (Eq k, Show k) => k -> [(k, v)] -> Either HPKEError v
lookupE KEM_ID
kem_id [(KEM_ID, KEMAlg)]
kemMap
    h <- lookupE kdf_id kdfMap
    a <- lookupE aead_id cipherMap
    return (k, h, a)

verifyPSKInput :: Mode -> PSK -> PSK_ID -> IO ()
verifyPSKInput :: Mode -> ByteString -> ByteString -> IO ()
verifyPSKInput Mode
mode ByteString
psk ByteString
psk_id
    | Bool
got_psk Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
/= Bool
got_psk_id =
        HPKEError -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO (HPKEError -> IO ()) -> HPKEError -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
ValidationError String
"mismatch for psk and psk_id"
    | Bool
got_psk Bool -> Bool -> Bool
&& Mode
mode Mode -> [Mode] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Mode
ModeBase, Mode
ModeAuth] =
        HPKEError -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO (HPKEError -> IO ()) -> HPKEError -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
ValidationError String
"invalid mode (1)"
    | (Bool -> Bool
not Bool
got_psk) Bool -> Bool -> Bool
&& Mode
mode Mode -> [Mode] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Mode
ModePsk, Mode
ModeAuthPsk] =
        HPKEError -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO (HPKEError -> IO ()) -> HPKEError -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
ValidationError String
"invalid mode (2)"
    | Bool
otherwise = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    got_psk :: Bool
got_psk = ByteString
psk ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= ByteString
""
    got_psk_id :: Bool
got_psk_id = ByteString
psk_id ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= ByteString
""

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

suiteHPKE :: KEM_ID -> KDF_ID -> AEAD_ID -> Suite
suiteHPKE :: KEM_ID -> KDF_ID -> AEAD_ID -> ByteString
suiteHPKE KEM_ID
kem_id KDF_ID
hkdf_id AEAD_ID
aead_id = ByteString
"HPKE" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
i0 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
i1 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
i2
  where
    i0 :: ByteString
i0 = Int -> Integer -> ByteString
forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
2 (Integer -> ByteString) -> Integer -> ByteString
forall a b. (a -> b) -> a -> b
$ Word16 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Integer) -> Word16 -> Integer
forall a b. (a -> b) -> a -> b
$ KEM_ID -> Word16
fromKEM_ID KEM_ID
kem_id
    i1 :: ByteString
i1 = Int -> Integer -> ByteString
forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
2 (Integer -> ByteString) -> Integer -> ByteString
forall a b. (a -> b) -> a -> b
$ Word16 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Integer) -> Word16 -> Integer
forall a b. (a -> b) -> a -> b
$ KDF_ID -> Word16
fromKDF_ID KDF_ID
hkdf_id
    i2 :: ByteString
i2 = Int -> Integer -> ByteString
forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
2 (Integer -> ByteString) -> Integer -> ByteString
forall a b. (a -> b) -> a -> b
$ Word16 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Integer) -> Word16 -> Integer
forall a b. (a -> b) -> a -> b
$ AEAD_ID -> Word16
fromAEAD_ID AEAD_ID
aead_id