hpke
Safe HaskellNone
LanguageHaskell2010

Crypto.HPKE.Internal

Synopsis

Extensible map

data HPKEMap Source #

Constructors

HPKEMap 

Fields

setupS Source #

Arguments

:: 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) 

setupR Source #

Arguments

:: 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 

Unified types

data KEMAlg Source #

A KEM that HPKE can run over, with its identity forgotten, for the table that turns a KEM_ID into one.

Constructors

HPKEKEM kem => KEMAlg (Proxy kem) 

data KDFHash Source #

Constructors

(HashAlgorithm h, KDF h) => KDFHash h 

data AEADCipher Source #

Constructors

Aead a => AEADCipher (Proxy a) 

API

class Aead a where Source #

Methods

sealA :: Proxy a -> Key -> Seal Source #

openA :: Proxy a -> Key -> Open Source #

nK :: Proxy a -> Int Source #

nN :: Proxy a -> Int Source #

nT :: Proxy a -> Int Source #

Instances

Instances details
Aead AES128 Source #

From RFC 9180 A.1

>>> let key = "\x45\x31\x68\x5d\x41\xd6\x5f\x03\xdc\x48\xf6\xb8\x30\x2c\x05\xb0" :: ByteString
>>> let nonce = "\x56\xd8\x90\xe5\xac\xca\xaf\x01\x1c\xff\x4b\x7d" :: ByteString
>>> let aad = "\x43\x6f\x75\x6e\x74\x2d\x30" :: ByteString
>>> let plain = "The quick brown fox jumps over the very lazy dog." :: ByteString
>>> let proxy = Proxy :: Proxy AES128
>>> sealA proxy key nonce aad plain >>= openA proxy key nonce aad
Right "The quick brown fox jumps over the very lazy dog."
Instance details

Defined in Crypto.HPKE.AEAD

Methods

sealA :: Proxy AES128 -> Key -> Seal Source #

openA :: Proxy AES128 -> Key -> Open Source #

nK :: Proxy AES128 -> Int Source #

nN :: Proxy AES128 -> Int Source #

nT :: Proxy AES128 -> Int Source #

Aead AES256 Source #

From RFC 9180 A.6

>>> let key = "\x75\x1e\x34\x6c\xe8\xf0\xdd\xb2\x30\x5c\x8a\x2a\x85\xc7\x0d\x5c\xf5\x59\xc5\x30\x93\x65\x6b\xe6\x36\xb9\x40\x6d\x4d\x7d\x1b\x70" :: ByteString
>>> let nonce = "\x55\xff\x7a\x7d\x73\x9c\x69\xf4\x4b\x25\x44\x7b" :: ByteString
>>> let aad = "\x43\x6f\x75\x6e\x74\x2d\x30" :: ByteString
>>> let plain = "The quick brown fox jumps over the very lazy dog." :: ByteString
>>> let proxy = Proxy :: Proxy AES256
>>> sealA proxy key nonce aad plain >>= openA proxy key nonce aad
Right "The quick brown fox jumps over the very lazy dog."
Instance details

Defined in Crypto.HPKE.AEAD

Methods

sealA :: Proxy AES256 -> Key -> Seal Source #

openA :: Proxy AES256 -> Key -> Open Source #

nK :: Proxy AES256 -> Int Source #

nN :: Proxy AES256 -> Int Source #

nT :: Proxy AES256 -> Int Source #

Aead ChaCha20Poly1305 Source #

From RFC 9180 A.5

>>> let key = "\xa8\xf4\x54\x90\xa9\x2a\x3b\x04\xd1\xdb\xf6\xcf\x2c\x39\x39\xad\x8b\xfc\x9b\xfc\xb9\x7c\x04\xbf\xfe\x11\x67\x30\xc9\xdf\xe3\xfc" :: ByteString
>>> let nonce = "\x72\x6b\x43\x90\xed\x22\x09\x80\x9f\x58\xc6\x93" :: ByteString
>>> let aad = "\x43\x6f\x75\x6e\x74\x2d\x30" :: ByteString
>>> let plain = "The quick brown fox jumps over the very lazy dog." :: ByteString
>>> let proxy = Proxy :: Proxy CCP.ChaCha20Poly1305
>>> sealA proxy key nonce aad plain >>= openA proxy key nonce aad
Right "The quick brown fox jumps over the very lazy dog."
Instance details

Defined in Crypto.HPKE.AEAD

Methods

sealA :: Proxy ChaCha20Poly1305 -> Key -> Seal Source #

openA :: Proxy ChaCha20Poly1305 -> Key -> Open Source #

nK :: Proxy ChaCha20Poly1305 -> Int Source #

nN :: Proxy ChaCha20Poly1305 -> Int Source #

nT :: Proxy ChaCha20Poly1305 -> Int Source #

class KDF h where Source #

Methods

labeledExtract :: Suite -> Salt -> Label -> IKM -> PRK h Source #

labeledExpand :: Suite -> PRK h -> Label -> Info -> Int -> Either HPKEError Key Source #

RFC 9180 section 5.3 allows an output of at most 255 * Nh octets, which is also what HKDF's counter can reach. A longer one is refused here rather than left to the HKDF underneath, whose way of saying so is an exception.

Instances

Instances details
KDF SHA256 Source # 
Instance details

Defined in Crypto.HPKE.KDF

Methods

labeledExtract :: Suite -> Salt -> Label -> IKM -> PRK SHA256 Source #

labeledExpand :: Suite -> PRK SHA256 -> Label -> Info -> Int -> Either HPKEError Key Source #

KDF SHA384 Source # 
Instance details

Defined in Crypto.HPKE.KDF

Methods

labeledExtract :: Suite -> Salt -> Label -> IKM -> PRK SHA384 Source #

labeledExpand :: Suite -> PRK SHA384 -> Label -> Info -> Int -> Either HPKEError Key Source #

KDF SHA512 Source # 
Instance details

Defined in Crypto.HPKE.KDF

Methods

labeledExtract :: Suite -> Salt -> Label -> IKM -> PRK SHA512 Source #

labeledExpand :: Suite -> PRK SHA512 -> Label -> Info -> Int -> Either HPKEError Key Source #

class (KEM kem, EncapsulationKey kem ~ EncodedPublicKey, DecapsulationKey kem ~ EncodedSecretKey, Ciphertext kem ~ EncodedPublicKey, Coins kem ~ EncodedSecretKey) => HPKEKEM kem where Source #

What HPKE needs of a KEM beyond what crypton's KEM class says.

RFC 9180 section 4.1 gives DHKEM four operations and the class has room for two: Encap is encapsulate, Decap is decapsulate. AuthEncap and AuthDecap take a static sender key, which the class has no argument for, so they are here. They default to refusing, which is what a KEM with no authenticated mode -- any post-quantum one -- should do.

The equalities pin the four associated types to the serialized forms of section 4, which is what travels and what this package's entry points take, so that setupS can be written once for every KEM.

Minimal complete definition

toEncapsulationKey

Methods

generateCoins :: MonadRandom m => proxy kem -> m EncodedSecretKey Source #

Draw what encapsulate would have drawn, so that the authenticated forms can generate their ephemeral key the same way.

For every KEM here the coins are a secret key, so the default is to draw a key pair and keep the half that is one.

authEncapWith Source #

Arguments

:: proxy kem 
-> EncodedSecretKey

skS, the sender's static key

-> EncodedPublicKey
pkR
-> EncodedSecretKey
skE
-> Either HPKEError (EncodedPublicKey, SharedSecret) 

AuthEncap of section 4.1, with the ephemeral key supplied.

authDecap Source #

Arguments

:: proxy kem 
-> EncodedPublicKey

pkS, the sender's static public key

-> EncodedSecretKey
skR
-> EncodedPublicKey
enc
-> Either HPKEError SharedSecret 

AuthDecap of section 4.1. The sender is named by its public key: a receiver does not have the other side's secret key, and asking for one would be asking for the wrong thing.

toEncapsulationKey :: proxy kem -> EncodedSecretKey -> Either HPKEError EncodedPublicKey Source #

The encapsulation key a decapsulation key belongs to.

Instances

Instances details
HPKEKEM DHKEM_P256 Source #

DHKEM has all four operations of RFC 9180 section 4.1, so the authenticated pair is defined rather than left to refuse.

Instance details

Defined in Crypto.HPKE.DHKEM

HPKEKEM DHKEM_P384 Source # 
Instance details

Defined in Crypto.HPKE.DHKEM

HPKEKEM DHKEM_P521 Source # 
Instance details

Defined in Crypto.HPKE.DHKEM

HPKEKEM DHKEM_X25519 Source # 
Instance details

Defined in Crypto.HPKE.DHKEM

HPKEKEM DHKEM_X448 Source # 
Instance details

Defined in Crypto.HPKE.DHKEM

Types

data Mode Source #

Instances

Instances details
Eq Mode Source # 
Instance details

Defined in Crypto.HPKE.KeySchedule

Methods

(==) :: Mode -> Mode -> Bool

(/=) :: Mode -> Mode -> Bool

Show Mode Source # 
Instance details

Defined in Crypto.HPKE.KeySchedule

Methods

showsPrec :: Int -> Mode -> ShowS

show :: Mode -> String

showList :: [Mode] -> ShowS

type PublicKey group = Point group Source #

type SecretKey group = Scalar group Source #

type Nonce = ByteString Source #

type Suite = ByteString Source #

type Salt = ByteString Source #

type Label = ByteString Source #

type IKM = ByteString Source #

Generating key pair

genKeyPair :: HPKEMap -> KEM_ID -> IO (EncodedPublicKey, EncodedSecretKey) Source #

Generating a pair of public key and secret key based on KEM_ID.

toPublicKey :: HPKEMap -> KEM_ID -> EncodedSecretKey -> Either HPKEError EncodedPublicKey Source #

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.