mirror of https://github.com/oxen-io/lokinet
Split crypto.hpp into 3 different files
parent
04e0fe3ad7
commit
79157414f3
@ -0,0 +1 @@
|
||||
#include <crypto/constants.hpp>
|
@ -0,0 +1,25 @@
|
||||
#ifndef LLARP_CRYPTO_CONSTANTS_HPP
|
||||
#define LLARP_CRYPTO_CONSTANTS_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <libntrup/ntru.h>
|
||||
|
||||
static constexpr uint32_t PUBKEYSIZE = 32;
|
||||
static constexpr uint32_t SECKEYSIZE = 64;
|
||||
static constexpr uint32_t NONCESIZE = 24;
|
||||
static constexpr uint32_t SHAREDKEYSIZE = 32;
|
||||
static constexpr uint32_t HASHSIZE = 64;
|
||||
static constexpr uint32_t SHORTHASHSIZE = 32;
|
||||
static constexpr uint32_t HMACSECSIZE = 32;
|
||||
static constexpr uint32_t SIGSIZE = 64;
|
||||
static constexpr uint32_t TUNNONCESIZE = 32;
|
||||
static constexpr uint32_t HMACSIZE = 32;
|
||||
static constexpr uint32_t PATHIDSIZE = 16;
|
||||
|
||||
static constexpr uint32_t PQ_CIPHERTEXTSIZE = crypto_kem_CIPHERTEXTBYTES;
|
||||
static constexpr uint32_t PQ_PUBKEYSIZE = crypto_kem_PUBLICKEYBYTES;
|
||||
static constexpr uint32_t PQ_SECRETKEYSIZE = crypto_kem_SECRETKEYBYTES;
|
||||
static constexpr uint32_t PQ_KEYPAIRSIZE = (PQ_SECRETKEYSIZE + PQ_PUBKEYSIZE);
|
||||
|
||||
#endif
|
@ -0,0 +1,71 @@
|
||||
#include <crypto/types.hpp>
|
||||
|
||||
#include <util/buffer.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
bool
|
||||
PubKey::FromString(const std::string& str)
|
||||
{
|
||||
return HexDecode(str.c_str(), begin(), size());
|
||||
}
|
||||
|
||||
std::string
|
||||
PubKey::ToString() const
|
||||
{
|
||||
char buf[(PUBKEYSIZE + 1) * 2] = {0};
|
||||
return HexEncode(*this, buf);
|
||||
}
|
||||
|
||||
bool
|
||||
SecretKey::LoadFromFile(const char* fname)
|
||||
{
|
||||
std::ifstream f;
|
||||
f.open(fname, std::ios::binary);
|
||||
if(!f.is_open())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
size_t sz = 0;
|
||||
f.seekg(0, std::ios::end);
|
||||
sz = f.tellg();
|
||||
f.seekg(0, std::ios::beg);
|
||||
if(sz == size())
|
||||
{
|
||||
// is raw buffer
|
||||
std::copy(std::istream_iterator< byte_t >(f),
|
||||
std::istream_iterator< byte_t >(), begin());
|
||||
return true;
|
||||
}
|
||||
byte_t tmp[128];
|
||||
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
||||
if(sz > sizeof(tmp))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
f.read((char*)tmp, sz);
|
||||
return BDecode(&buf);
|
||||
}
|
||||
|
||||
bool
|
||||
SecretKey::SaveToFile(const char* fname) const
|
||||
{
|
||||
byte_t tmp[128];
|
||||
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
||||
if(!BEncode(&buf))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ofstream f;
|
||||
f.open(fname, std::ios::binary);
|
||||
if(!f.is_open())
|
||||
return false;
|
||||
f.write((char*)buf.base, buf.cur - buf.base);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace llarp
|
@ -0,0 +1,111 @@
|
||||
#ifndef LLARP_CRYPTO_TYPES_HPP
|
||||
#define LLARP_CRYPTO_TYPES_HPP
|
||||
|
||||
#include <crypto/constants.hpp>
|
||||
#include <router_id.hpp>
|
||||
#include <util/aligned.hpp>
|
||||
#include <util/types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
using SharedSecret = AlignedBuffer< SHAREDKEYSIZE >;
|
||||
using KeyExchangeNonce = AlignedBuffer< 32 >;
|
||||
|
||||
struct PubKey final : public AlignedBuffer< PUBKEYSIZE >
|
||||
{
|
||||
PubKey() : AlignedBuffer< SIZE >()
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
friend std::ostream &
|
||||
operator<<(std::ostream &out, const PubKey &k)
|
||||
{
|
||||
return out << k.ToString();
|
||||
}
|
||||
|
||||
operator RouterID() const
|
||||
{
|
||||
return RouterID(as_array());
|
||||
}
|
||||
|
||||
PubKey &
|
||||
operator=(const byte_t *ptr)
|
||||
{
|
||||
std::copy(ptr, ptr + SIZE, begin());
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
struct SecretKey final : public AlignedBuffer< SECKEYSIZE >
|
||||
{
|
||||
SecretKey() : AlignedBuffer< SECKEYSIZE >(){};
|
||||
|
||||
explicit SecretKey(const SecretKey &k) : AlignedBuffer< SECKEYSIZE >(k)
|
||||
{
|
||||
}
|
||||
|
||||
explicit SecretKey(const byte_t *ptr) : AlignedBuffer< SECKEYSIZE >(ptr)
|
||||
{
|
||||
}
|
||||
|
||||
friend std::ostream &
|
||||
operator<<(std::ostream &out, const SecretKey &)
|
||||
{
|
||||
// make sure we never print out secret keys
|
||||
return out << "[secretkey]";
|
||||
}
|
||||
|
||||
PubKey
|
||||
toPublic() const
|
||||
{
|
||||
return PubKey(data() + 32);
|
||||
}
|
||||
|
||||
bool
|
||||
LoadFromFile(const char *fname);
|
||||
|
||||
bool
|
||||
SaveToFile(const char *fname) const;
|
||||
|
||||
SecretKey &
|
||||
operator=(const byte_t *ptr)
|
||||
{
|
||||
std::copy(ptr, ptr + SIZE, begin());
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
using ShortHash = AlignedBuffer< SHORTHASHSIZE >;
|
||||
using Signature = AlignedBuffer< SIGSIZE >;
|
||||
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 >;
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue