{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Crypto.HPKE.KEM (
HPKEKEM (..),
KEMAlg (..),
encapEnv,
decapEnv,
)
where
import Crypto.ECC (
EllipticCurve (..),
EllipticCurveDH (..),
)
import Crypto.KEM (KEM (..))
import Crypto.Random (MonadRandom)
import Crypto.HPKE.PublicKey
import Crypto.HPKE.Types
class
( KEM kem
, EncapsulationKey kem ~ EncodedPublicKey
, DecapsulationKey kem ~ EncodedSecretKey
, Ciphertext kem ~ EncodedPublicKey
, Coins kem ~ EncodedSecretKey
) =>
HPKEKEM kem
where
generateCoins :: MonadRandom m => proxy kem -> m EncodedSecretKey
generateCoins proxy kem
p = (EncodedPublicKey, EncodedSecretKey) -> EncodedSecretKey
forall a b. (a, b) -> b
snd ((EncodedPublicKey, EncodedSecretKey) -> EncodedSecretKey)
-> m (EncodedPublicKey, EncodedSecretKey) -> m EncodedSecretKey
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` proxy kem -> m (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
p
authEncapWith
:: proxy kem
-> EncodedSecretKey
-> EncodedPublicKey
-> EncodedSecretKey
-> Either HPKEError (EncodedPublicKey, SharedSecret)
authEncapWith proxy kem
_ EncodedSecretKey
_ EncodedPublicKey
_ EncodedSecretKey
_ = HPKEError -> Either HPKEError (EncodedPublicKey, SharedSecret)
forall a b. a -> Either a b
Left (HPKEError -> Either HPKEError (EncodedPublicKey, SharedSecret))
-> HPKEError -> Either HPKEError (EncodedPublicKey, SharedSecret)
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
Unsupported String
"authenticated mode"
authDecap
:: proxy kem
-> EncodedPublicKey
-> EncodedSecretKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
authDecap proxy kem
_ EncodedPublicKey
_ EncodedSecretKey
_ EncodedPublicKey
_ = HPKEError -> Either HPKEError SharedSecret
forall a b. a -> Either a b
Left (HPKEError -> Either HPKEError SharedSecret)
-> HPKEError -> Either HPKEError SharedSecret
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
Unsupported String
"authenticated mode"
toEncapsulationKey
:: proxy kem -> EncodedSecretKey -> Either HPKEError EncodedPublicKey
data KEMAlg = forall kem. HPKEKEM kem => KEMAlg (Proxy kem)
encap
:: (EllipticCurve group, EllipticCurveDH group)
=> Proxy group
-> KeyDeriveFunction
-> SecretKey group
-> Maybe (SecretKey group)
-> Encap
encap :: forall group.
(EllipticCurve group, EllipticCurveDH group) =>
Proxy group
-> KeyDeriveFunction
-> SecretKey group
-> Maybe (SecretKey group)
-> Encap
encap Proxy group
proxy KeyDeriveFunction
derive SecretKey group
skE Maybe (SecretKey group)
mskS enc0 :: EncodedPublicKey
enc0@(EncodedPublicKey ByteString
pkRm) = do
pkR <- Proxy group -> EncodedPublicKey -> Either HPKEError (Point group)
forall group.
EllipticCurve group =>
Proxy group
-> EncodedPublicKey -> Either HPKEError (PublicKey group)
deserializePublicKey Proxy group
proxy EncodedPublicKey
enc0
dh0 <- ecdh' proxy skE pkR $ EncapError "encap"
(dh, pkSm) <- case mskS of
Maybe (SecretKey group)
Nothing -> (SharedSecret, ByteString)
-> Either HPKEError (SharedSecret, ByteString)
forall a. a -> Either HPKEError a
forall (m :: * -> *) a. Monad m => a -> m a
return (SharedSecret
dh0, ByteString
"")
Just SecretKey group
skS -> do
let pkS :: Point group
pkS = Proxy group -> SecretKey group -> Point group
forall curve (proxy :: * -> *).
EllipticCurve curve =>
proxy curve -> Scalar curve -> Point curve
forall (proxy :: * -> *).
proxy group -> SecretKey group -> Point group
scalarToPoint Proxy group
proxy SecretKey group
skS
dh1 <- Proxy group
-> SecretKey group
-> Point group
-> HPKEError
-> Either HPKEError SharedSecret
forall group a.
EllipticCurveDH group =>
Proxy group
-> SecretKey group -> PublicKey group -> a -> Either a SharedSecret
ecdh' Proxy group
proxy SecretKey group
skS Point group
pkR (HPKEError -> Either HPKEError SharedSecret)
-> HPKEError -> Either HPKEError SharedSecret
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
EncapError String
"encap"
let EncodedPublicKey pk = serializePublicKey proxy pkS
return (dh0 <> dh1, pk)
let pkE = Proxy group -> SecretKey group -> Point group
forall curve (proxy :: * -> *).
EllipticCurve curve =>
proxy curve -> Scalar curve -> Point curve
forall (proxy :: * -> *).
proxy group -> SecretKey group -> Point group
scalarToPoint Proxy group
proxy SecretKey group
skE
let enc@(EncodedPublicKey pkEm) = serializePublicKey proxy pkE
kem_context = ByteString
pkEm ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
pkRm ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
pkSm
shared_secret = ScrubbedBytes -> SharedSecret
SharedSecret (ScrubbedBytes -> SharedSecret) -> ScrubbedBytes -> SharedSecret
forall a b. (a -> b) -> a -> b
$ ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert (ByteString -> ScrubbedBytes) -> ByteString -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ KeyDeriveFunction
derive SharedSecret
dh ByteString
kem_context
return (shared_secret, enc)
encapEnv
:: (EllipticCurve group, EllipticCurveDH group)
=> Proxy group
-> KeyDeriveFunction
-> EncodedSecretKey
-> Maybe EncodedSecretKey
-> Encap
encapEnv :: forall group.
(EllipticCurve group, EllipticCurveDH group) =>
Proxy group
-> KeyDeriveFunction
-> EncodedSecretKey
-> Maybe EncodedSecretKey
-> Encap
encapEnv Proxy group
proxy KeyDeriveFunction
derive EncodedSecretKey
skEm Maybe EncodedSecretKey
mskSm EncodedPublicKey
enc = do
skE <- Proxy group -> EncodedSecretKey -> Either HPKEError (Scalar group)
forall group.
EllipticCurve group =>
Proxy group
-> EncodedSecretKey -> Either HPKEError (SecretKey group)
deserializeSecretKey Proxy group
proxy EncodedSecretKey
skEm
mskS <- traverse (deserializeSecretKey proxy) mskSm
encap proxy derive skE mskS enc
decap
:: (EllipticCurve group, EllipticCurveDH group)
=> Proxy group
-> KeyDeriveFunction
-> SecretKey group
-> Maybe (PublicKey group)
-> Decap
decap :: forall group.
(EllipticCurve group, EllipticCurveDH group) =>
Proxy group
-> KeyDeriveFunction
-> SecretKey group
-> Maybe (PublicKey group)
-> EncodedPublicKey
-> Either HPKEError SharedSecret
decap Proxy group
proxy KeyDeriveFunction
derive SecretKey group
skR Maybe (PublicKey group)
mpkS enc :: EncodedPublicKey
enc@(EncodedPublicKey ByteString
pkEm) = do
pkE <- Proxy group
-> EncodedPublicKey -> Either HPKEError (PublicKey group)
forall group.
EllipticCurve group =>
Proxy group
-> EncodedPublicKey -> Either HPKEError (PublicKey group)
deserializePublicKey Proxy group
proxy EncodedPublicKey
enc
dh0 <- ecdh' proxy skR pkE $ DecapError "decap"
(dh, pkSm) <- case mpkS of
Maybe (PublicKey group)
Nothing -> (SharedSecret, ByteString)
-> Either HPKEError (SharedSecret, ByteString)
forall a. a -> Either HPKEError a
forall (m :: * -> *) a. Monad m => a -> m a
return (SharedSecret
dh0, ByteString
"")
Just PublicKey group
pkS -> do
dh1 <- Proxy group
-> SecretKey group
-> PublicKey group
-> HPKEError
-> Either HPKEError SharedSecret
forall group a.
EllipticCurveDH group =>
Proxy group
-> SecretKey group -> PublicKey group -> a -> Either a SharedSecret
ecdh' Proxy group
proxy SecretKey group
skR PublicKey group
pkS (HPKEError -> Either HPKEError SharedSecret)
-> HPKEError -> Either HPKEError SharedSecret
forall a b. (a -> b) -> a -> b
$ String -> HPKEError
DecapError String
"decap"
let EncodedPublicKey pk = serializePublicKey proxy pkS
return (dh0 <> dh1, pk)
let pkR = Proxy group -> SecretKey group -> PublicKey group
forall curve (proxy :: * -> *).
EllipticCurve curve =>
proxy curve -> Scalar curve -> Point curve
forall (proxy :: * -> *).
proxy group -> SecretKey group -> PublicKey group
scalarToPoint Proxy group
proxy SecretKey group
skR
let EncodedPublicKey pkRm = serializePublicKey proxy pkR
kem_context = ByteString
pkEm ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
pkRm ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
pkSm
shared_secret = ScrubbedBytes -> SharedSecret
SharedSecret (ScrubbedBytes -> SharedSecret) -> ScrubbedBytes -> SharedSecret
forall a b. (a -> b) -> a -> b
$ ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert (ByteString -> ScrubbedBytes) -> ByteString -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ KeyDeriveFunction
derive SharedSecret
dh ByteString
kem_context
return shared_secret
decapEnv
:: (EllipticCurve group, EllipticCurveDH group)
=> Proxy group
-> KeyDeriveFunction
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> Decap
decapEnv :: forall group.
(EllipticCurve group, EllipticCurveDH group) =>
Proxy group
-> KeyDeriveFunction
-> EncodedSecretKey
-> Maybe EncodedPublicKey
-> EncodedPublicKey
-> Either HPKEError SharedSecret
decapEnv Proxy group
proxy KeyDeriveFunction
derive EncodedSecretKey
skRm Maybe EncodedPublicKey
mpkSm EncodedPublicKey
enc = do
skR <- Proxy group -> EncodedSecretKey -> Either HPKEError (Scalar group)
forall group.
EllipticCurve group =>
Proxy group
-> EncodedSecretKey -> Either HPKEError (SecretKey group)
deserializeSecretKey Proxy group
proxy EncodedSecretKey
skRm
mpkS <- traverse (deserializePublicKey proxy) mpkSm
decap proxy derive skR mpkS enc
ecdh'
:: EllipticCurveDH group
=> Proxy group
-> SecretKey group
-> PublicKey group
-> a
-> Either a SharedSecret
ecdh' :: forall group a.
EllipticCurveDH group =>
Proxy group
-> SecretKey group -> PublicKey group -> a -> Either a SharedSecret
ecdh' Proxy group
proxy SecretKey group
sk PublicKey group
pk a
err = case Proxy group
-> SecretKey group
-> PublicKey group
-> CryptoFailable SharedSecret
forall curve (proxy :: * -> *).
EllipticCurveDH curve =>
proxy curve
-> Scalar curve -> Point curve -> CryptoFailable SharedSecret
forall (proxy :: * -> *).
proxy group
-> SecretKey group
-> PublicKey group
-> CryptoFailable SharedSecret
ecdh Proxy group
proxy SecretKey group
sk PublicKey group
pk of
CryptoPassed SharedSecret
a -> SharedSecret -> Either a SharedSecret
forall a b. b -> Either a b
Right SharedSecret
a
CryptoFailed CryptoError
_ -> a -> Either a SharedSecret
forall a b. a -> Either a b
Left a
err