proxygen
fizz::X25519KeyExchange Class Reference

#include <X25519.h>

Inheritance diagram for fizz::X25519KeyExchange:
fizz::KeyExchange

Public Member Functions

 ~X25519KeyExchange () override=default
 
void generateKeyPair () override
 
std::unique_ptr< folly::IOBufgetKeyShare () const override
 
std::unique_ptr< folly::IOBufgenerateSharedSecret (folly::ByteRange keyShare) const override
 
- Public Member Functions inherited from fizz::KeyExchange
virtual ~KeyExchange ()=default
 

Private Types

using PrivKey = std::array< uint8_t, crypto_scalarmult_SCALARBYTES >
 
using PubKey = std::array< uint8_t, crypto_scalarmult_BYTES >
 

Private Attributes

folly::Optional< PrivKeyprivKey_
 
folly::Optional< PubKeypubKey_
 

Detailed Description

X25519 key exchange implementation using libsodium.

Definition at line 23 of file X25519.h.

Member Typedef Documentation

using fizz::X25519KeyExchange::PrivKey = std::array<uint8_t, crypto_scalarmult_SCALARBYTES>
private

Definition at line 32 of file X25519.h.

using fizz::X25519KeyExchange::PubKey = std::array<uint8_t, crypto_scalarmult_BYTES>
private

Definition at line 33 of file X25519.h.

Constructor & Destructor Documentation

fizz::X25519KeyExchange::~X25519KeyExchange ( )
overridedefault

Member Function Documentation

void fizz::X25519KeyExchange::generateKeyPair ( )
overridevirtual

Generates an ephemeral key pair.

Implements fizz::KeyExchange.

Definition at line 20 of file X25519.cpp.

References folly::gen::move.

Referenced by fizz::test::TEST().

20  {
21  auto privKey = PrivKey();
22  auto pubKey = PubKey();
23  auto err = crypto_box_curve25519xsalsa20poly1305_keypair(
24  pubKey.data(), privKey.data());
25  if (err != 0) {
26  throw std::runtime_error(to<std::string>("Could not generate keys ", err));
27  }
28  privKey_ = std::move(privKey);
29  pubKey_ = std::move(pubKey);
30 }
folly::Optional< PubKey > pubKey_
Definition: X25519.h:36
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::Optional< PrivKey > privKey_
Definition: X25519.h:35
std::array< uint8_t, crypto_scalarmult_BYTES > PubKey
Definition: X25519.h:33
std::array< uint8_t, crypto_scalarmult_SCALARBYTES > PrivKey
Definition: X25519.h:32
std::unique_ptr< folly::IOBuf > fizz::X25519KeyExchange::generateSharedSecret ( folly::ByteRange  keyShare) const
overridevirtual

Generate a shared secret with our key pair and a peer's public key share.

Performs all necessary validation of the public key share and throws on error.

generateKeyPair() must be called before.

Implements fizz::KeyExchange.

Definition at line 39 of file X25519.cpp.

References folly::IOBuf::create(), folly::Range< Iter >::data(), and folly::Range< Iter >::size().

Referenced by fizz::test::TEST().

40  {
41  if (!privKey_ || !pubKey_) {
42  throw std::runtime_error("Key not generated");
43  }
44  if (keyShare.size() != crypto_scalarmult_BYTES) {
45  throw std::runtime_error("Invalid external public key");
46  }
47  auto key = IOBuf::create(crypto_scalarmult_BYTES);
48  key->append(crypto_scalarmult_BYTES);
49  int err =
50  crypto_scalarmult(key->writableData(), privKey_->data(), keyShare.data());
51  if (err != 0) {
52  throw std::runtime_error("Invalid point");
53  }
54  return key;
55 }
folly::Optional< PubKey > pubKey_
Definition: X25519.h:36
constexpr size_type size() const
Definition: Range.h:431
folly::Optional< PrivKey > privKey_
Definition: X25519.h:35
constexpr Iter data() const
Definition: Range.h:446
std::unique_ptr< IOBuf > fizz::X25519KeyExchange::getKeyShare ( ) const
overridevirtual

Returns the public key to share with peers.

generateKeyPair() must be called before.

Implements fizz::KeyExchange.

Definition at line 32 of file X25519.cpp.

References folly::IOBuf::copyBuffer().

32  {
33  if (!privKey_ || !pubKey_) {
34  throw std::runtime_error("Key not generated");
35  }
36  return IOBuf::copyBuffer(pubKey_->data(), pubKey_->size());
37 }
folly::Optional< PubKey > pubKey_
Definition: X25519.h:36
folly::Optional< PrivKey > privKey_
Definition: X25519.h:35
std::unique_ptr< IOBuf > copyBuffer(const folly::IOBuf &buf)

Member Data Documentation

folly::Optional<PrivKey> fizz::X25519KeyExchange::privKey_
private

Definition at line 35 of file X25519.h.

folly::Optional<PubKey> fizz::X25519KeyExchange::pubKey_
private

Definition at line 36 of file X25519.h.


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