llarp/crypto/types.hpp

Namespaces

Name
llarp
[crypto.hpp]
std
STL namespace.

Classes

Name
struct llarp::PubKey
struct llarp::SecretKey
Stores a sodium "secret key" value, which is actually the seed concatenated with the public key.
struct llarp::PrivateKey
PrivateKey is similar to SecretKey except that it only stores the private key value and a hash, unlike SecretKey which stores the seed from which the private key and hash value are generated.
struct llarp::IdentitySecret
IdentitySecret is a secret key from a service node secret seed.
struct llarp::Signature
struct std::hash< llarp::PubKey >

Source code

#pragma once

#include "constants.hpp"
#include <llarp/router_id.hpp>
#include <llarp/util/aligned.hpp>
#include <llarp/util/types.hpp>
#include <algorithm>
#include <iostream>

namespace llarp
{
  using SharedSecret = AlignedBuffer<SHAREDKEYSIZE>;
  using KeyExchangeNonce = AlignedBuffer<32>;

  struct PubKey final : public AlignedBuffer<PUBKEYSIZE>
  {
    PubKey() = default;

    explicit PubKey(const byte_t* ptr) : AlignedBuffer<SIZE>(ptr)
    {}

    explicit PubKey(const Data& data) : AlignedBuffer<SIZE>(data)
    {}

    explicit PubKey(const AlignedBuffer<SIZE>& other) : AlignedBuffer<SIZE>(other)
    {}

    std::string
    ToString() const;

    bool
    FromString(const std::string& str);

    operator RouterID() const
    {
      return {as_array()};
    }

    PubKey&
    operator=(const byte_t* ptr)
    {
      std::copy(ptr, ptr + SIZE, begin());
      return *this;
    }
  };

  inline bool
  operator==(const PubKey& lhs, const PubKey& rhs)
  {
    return lhs.as_array() == rhs.as_array();
  }

  inline bool
  operator==(const PubKey& lhs, const RouterID& rhs)
  {
    return lhs.as_array() == rhs.as_array();
  }

  inline bool
  operator==(const RouterID& lhs, const PubKey& rhs)
  {
    return lhs.as_array() == rhs.as_array();
  }

  struct PrivateKey;

  struct SecretKey final : public AlignedBuffer<SECKEYSIZE>
  {
    SecretKey() = default;

    explicit SecretKey(const byte_t* ptr) : AlignedBuffer<SECKEYSIZE>(ptr)
    {}

    // The full data
    explicit SecretKey(const AlignedBuffer<SECKEYSIZE>& seed) : AlignedBuffer<SECKEYSIZE>(seed)
    {}

    // Just the seed, we recalculate the pubkey
    explicit SecretKey(const AlignedBuffer<32>& seed)
    {
      std::copy(seed.begin(), seed.end(), begin());
      Recalculate();
    }

    bool
    Recalculate();

    std::string_view
    ToString() const
    {
      return "[secretkey]";
    }

    PubKey
    toPublic() const
    {
      return PubKey(data() + 32);
    }

    bool
    toPrivate(PrivateKey& key) const;

    template <typename fspath_t>
    bool
    LoadFromFile(const fspath_t& fname);

    template <typename fspath_t>
    bool
    SaveToFile(const fspath_t& fname) const;
  };

  struct PrivateKey final : public AlignedBuffer<64>
  {
    PrivateKey() = default;

    explicit PrivateKey(const byte_t* ptr) : AlignedBuffer<64>(ptr)
    {}

    explicit PrivateKey(const AlignedBuffer<64>& key_and_hash) : AlignedBuffer<64>(key_and_hash)
    {}

    const byte_t*
    signingHash() const
    {
      return data() + 32;
    }

    byte_t*
    signingHash()
    {
      return data() + 32;
    }

    std::string_view
    ToString() const
    {
      return "[privatekey]";
    }

    bool
    toPublic(PubKey& pubkey) const;
  };

  struct IdentitySecret final : public AlignedBuffer<32>
  {
    IdentitySecret() : AlignedBuffer<32>()
    {}

    explicit IdentitySecret(const IdentitySecret&) = delete;
    // no byte data constructor
    explicit IdentitySecret(const byte_t*) = delete;

    template <typename fspath_t>
    bool
    LoadFromFile(const fspath_t& fname);

    std::string_view
    ToString() const
    {
      return "[IdentitySecret]";
    }
  };

  template <>
  constexpr inline bool IsToStringFormattable<PubKey> = true;
  template <>
  constexpr inline bool IsToStringFormattable<SecretKey> = true;
  template <>
  constexpr inline bool IsToStringFormattable<PrivateKey> = true;
  template <>
  constexpr inline bool IsToStringFormattable<IdentitySecret> = true;

  using ShortHash = AlignedBuffer<SHORTHASHSIZE>;
  using LongHash = AlignedBuffer<HASHSIZE>;

  struct Signature final : public AlignedBuffer<SIGSIZE>
  {
    byte_t*
    Hi();

    const byte_t*
    Hi() const;

    byte_t*
    Lo();

    const byte_t*
    Lo() const;
  };

  using TunnelNonce = AlignedBuffer<TUNNONCESIZE>;
  using SymmNonce = AlignedBuffer<NONCESIZE>;
  using SymmKey = AlignedBuffer<32>;

  using PQCipherBlock = AlignedBuffer<PQ_CIPHERTEXTSIZE + 1>;
  using PQPubKey = AlignedBuffer<PQ_PUBKEYSIZE>;
  using PQKeyPair = AlignedBuffer<PQ_KEYPAIRSIZE>;

  using path_dh_func =
      std::function<bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)>;

  using transport_dh_func =
      std::function<bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)>;

  using shorthash_func = std::function<bool(ShortHash&, const llarp_buffer_t&)>;
}  // namespace llarp

namespace std
{
  template <>
  struct hash<llarp::PubKey> : hash<llarp::AlignedBuffer<llarp::PubKey::SIZE>>
  {};
};  // namespace std

Updated on 2026-01-10 at 22:49:45 +0000