---
title: llarp/crypto/types.hpp

---

# llarp/crypto/types.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[std](Namespaces/namespacestd.md)** <br>STL namespace.  |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::PubKey](Classes/structllarp_1_1PubKey.md)**  |
| struct | **[llarp::SecretKey](Classes/structllarp_1_1SecretKey.md)** <br>Stores a sodium "secret key" value, which is actually the seed concatenated with the public key.  |
| struct | **[llarp::PrivateKey](Classes/structllarp_1_1PrivateKey.md)** <br>[PrivateKey]() is similar to [SecretKey](Classes/structllarp_1_1SecretKey.md) except that it only stores the private key value and a hash, unlike [SecretKey](Classes/structllarp_1_1SecretKey.md) which stores the seed from which the private key and hash value are generated.  |
| struct | **[llarp::IdentitySecret](Classes/structllarp_1_1IdentitySecret.md)** <br>[IdentitySecret]() is a secret key from a service node secret seed.  |
| struct | **[llarp::Signature](Classes/structllarp_1_1Signature.md)**  |
| struct | **[std::hash< llarp::PubKey >](Classes/structstd_1_1hash_3_01llarp_1_1PubKey_01_4.md)**  |




## Source code

```cpp
#pragma once

#include "constants.hpp"
#include <llarp/router_id.hpp>
#include <llarp/util/aligned.hpp>
#include <llarp/util/types.hpp>
#include <llarp/util/fs.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;

    bool
    LoadFromFile(const fs::path& fname);

    bool
    SaveToFile(const fs::path& 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;

    bool
    LoadFromFile(const fs::path& 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 2022-10-26 at 17:18:04 +0000
