Botan 3.14.1
Crypto and TLS for C&
Botan::HPKE::Public_Key Class Referencefinal

#include <hpke.h>

Public Member Functions

KEM_Id kem () const
std::vector< uint8_t > serialize () const
 SerializePublicKey from RFC 9180 Section 7.1.1.
const Botan::Public_Key & underlying () const

Static Public Member Functions

static Public_Key deserialize (KEM_Id kem, std::span< const uint8_t > bytes)
static Public_Key from_key (KEM_Id kem, std::unique_ptr< Botan::Public_Key > key)

Friends

class HPKE::Private_Key
class HPKE::Recipient_Context
class HPKE::Sender_Context
bool operator== (const Public_Key &a, const Public_Key &b)

Detailed Description

An HPKE public key

This is a value type (cheap to copy) wrapping an asymmetric key that has been checked against a particular HPKE KEM: its algorithm (and, for the NIST curves, its group) matches the KEM, and a key deserialized from the wire is well formed, which for the NIST curves includes being a point on the curve. One check is deferred: RFC 9180 Section 7.1.4 specifies the rejection of low order X25519/X448 points in terms of the Diffie-Hellman output, so such a key can be constructed, but any context setup using it throws Invalid_Argument. Holding a Public_Key therefore does not by itself establish that an untrusted peer's key is acceptable.

It has no inheritance relationship with Botan::Public_Key or with HPKE::Private_Key.

Definition at line 290 of file hpke.h.

Member Function Documentation

◆ deserialize()

Public_Key Botan::HPKE::Public_Key::deserialize ( KEM_Id kem,
std::span< const uint8_t > bytes )
static

DeserializePublicKey from RFC 9180 Section 7.1.1

Accepts exactly the wire format the KEM defines (raw bytes for X25519/X448, an uncompressed SEC1 point for the NIST curves). Throws Decoding_Error if the length or encoding is wrong, or if a NIST curve point is not on the curve. Low order X25519/X448 points are not detected here; see the class description.

Definition at line 246 of file hpke.cpp.

246 {
247 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
248 auto key = ops->deserialize_public(bytes);
249 return Public_Key(std::make_shared<Data>(std::move(ops), std::move(key)));
250}
static std::unique_ptr< const KEM_Ops > create(KEM_Id kem)
Definition hpke_kem.cpp:545
KEM_Id kem() const
Definition hpke.cpp:259

References Botan::HPKE::KEM_Ops::create(), and kem().

◆ from_key()

Public_Key Botan::HPKE::Public_Key::from_key ( KEM_Id kem,
std::unique_ptr< Botan::Public_Key > key )
static

Adopt an existing key for use with HPKE

For the NIST curves both ECDH and ECDSA keys are accepted, the latter being how keys with the id-ecPublicKey OID (eg from an X.509 certificate) are loaded; an ECDSA key is converted, so underlying() always returns an ECDH key. Keys of other EC algorithms (SM2, ECGDSA, ...) are not accepted.

Throws Invalid_Argument unless the algorithm (and, for the NIST curves, the group) matches the KEM.

Definition at line 252 of file hpke.cpp.

252 {
253 BOTAN_ARG_CHECK(key != nullptr, "HPKE::Public_Key::from_key: key is null");
254 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
255 auto adopted = ops->adopt_public(std::move(key));
256 return Public_Key(std::make_shared<Data>(std::move(ops), std::move(adopted)));
257}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK, Botan::HPKE::KEM_Ops::create(), and kem().

◆ kem()

KEM_Id Botan::HPKE::Public_Key::kem ( ) const

Definition at line 259 of file hpke.cpp.

259 {
260 return m_data->ops().kem_id();
261}

Referenced by deserialize(), from_key(), and operator==.

◆ serialize()

std::vector< uint8_t > Botan::HPKE::Public_Key::serialize ( ) const

SerializePublicKey from RFC 9180 Section 7.1.1.

Definition at line 263 of file hpke.cpp.

263 {
264 return m_data->serialized();
265}

Referenced by operator==.

◆ underlying()

const Botan::Public_Key & Botan::HPKE::Public_Key::underlying ( ) const

Access the wrapped key, eg for encoding as a X.509 SubjectPublicKeyInfo

For the NIST curves this is an ECDH key, which encodes with the id-ecDH algorithm identifier.

Definition at line 267 of file hpke.cpp.

267 {
268 return m_data->key();
269}

◆ HPKE::Private_Key

friend class HPKE::Private_Key
friend

Definition at line 338 of file hpke.h.

◆ HPKE::Recipient_Context

friend class HPKE::Recipient_Context
friend

Definition at line 340 of file hpke.h.

◆ HPKE::Sender_Context

friend class HPKE::Sender_Context
friend

Definition at line 339 of file hpke.h.

◆ operator==

bool operator== ( const Public_Key & a,
const Public_Key & b )
friend

Definition at line 329 of file hpke.h.

329 {
330 return a.kem() == b.kem() && a.serialize() == b.serialize();
331 }

References kem(), and serialize().


The documentation for this class was generated from the following files: