diff options
Diffstat (limited to 'source/core')
-rw-r--r-- | source/core/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/core/StarConfig.hpp | 1 | ||||
-rw-r--r-- | source/core/StarCurve25519.cpp | 50 | ||||
-rw-r--r-- | source/core/StarCurve25519.hpp | 25 | ||||
-rw-r--r-- | source/core/StarDataStream.cpp | 4 | ||||
-rw-r--r-- | source/core/StarDataStreamDevices.cpp | 7 | ||||
-rw-r--r-- | source/core/StarDataStreamDevices.hpp | 3 |
7 files changed, 90 insertions, 2 deletions
diff --git a/source/core/CMakeLists.txt b/source/core/CMakeLists.txt index 08701d7..1fd2aac 100644 --- a/source/core/CMakeLists.txt +++ b/source/core/CMakeLists.txt @@ -21,6 +21,7 @@ SET (star_core_HEADERS StarColor.hpp StarCompression.hpp StarConfig.hpp + StarCurve25519.hpp StarDataStream.hpp StarDataStreamDevices.hpp StarDataStreamExtra.hpp @@ -133,6 +134,7 @@ SET (star_core_SOURCES StarByteArray.cpp StarColor.cpp StarCompression.cpp + StarCurve25519.cpp StarDataStream.cpp StarDataStreamDevices.cpp StarDirectives.cpp diff --git a/source/core/StarConfig.hpp b/source/core/StarConfig.hpp index f070df4..5a95569 100644 --- a/source/core/StarConfig.hpp +++ b/source/core/StarConfig.hpp @@ -44,6 +44,7 @@ using std::mem_fn; using std::ref; using std::cref; using namespace std::placeholders; +using namespace std::string_literals; using std::prev; // using std::next; diff --git a/source/core/StarCurve25519.cpp b/source/core/StarCurve25519.cpp new file mode 100644 index 0000000..3fdddea --- /dev/null +++ b/source/core/StarCurve25519.cpp @@ -0,0 +1,50 @@ +#include "StarCurve25519.hpp" +#include "StarRandom.hpp" +#include "StarLogging.hpp" + +#include "curve25519/include/curve25519_dh.h" +#include "curve25519/include/ed25519_signature.h" + +namespace Star::Curve25519 { + +struct KeySet { + PrivateKey privateKey; + PublicKey publicKey; + + KeySet() { + SecretKey secret; + Random::randBytes(SecretKeySize).copyTo((char*)secret.data()); + + secret[0] &= 248; + secret[31] &= 127; + secret[31] |= 64; + + ed25519_CreateKeyPair(publicKey.data(), privateKey.data(), nullptr, secret.data()); + + Logger::info("Generated Curve25519 key-pair"); + } +}; + +static KeySet const& staticKeys() { + static KeySet keys; + + return keys; +} + +PrivateKey const& privateKey() { return staticKeys().privateKey; } + + + +Signature sign(void* data, size_t len) { + Signature signature; + ed25519_SignMessage(signature.data(), privateKey().data(), nullptr, (unsigned char*)data, len); + return signature; +} + +bool verify(uint8_t const* signature, uint8_t const* publicKey, void* data, size_t len) { + return ed25519_VerifySignature(signature, publicKey, (unsigned char*)data, len); +} + +PublicKey const& publicKey() { return staticKeys().publicKey; } + +}
\ No newline at end of file diff --git a/source/core/StarCurve25519.hpp b/source/core/StarCurve25519.hpp new file mode 100644 index 0000000..15fe4d1 --- /dev/null +++ b/source/core/StarCurve25519.hpp @@ -0,0 +1,25 @@ +#ifndef STAR_CURVE_25519_HPP +#define STAR_CURVE_25519_HPP +#include "StarEncode.hpp" +#include "StarByteArray.hpp" +#include "StarArray.hpp" + +namespace Star::Curve25519 { + +constexpr size_t PublicKeySize = 32; +constexpr size_t SecretKeySize = 32; +constexpr size_t PrivateKeySize = 64; +constexpr size_t SignatureSize = 64; + +typedef Array<uint8_t, PublicKeySize> PublicKey; +typedef Array<uint8_t, SecretKeySize> SecretKey; +typedef Array<uint8_t, PrivateKeySize> PrivateKey; +typedef Array<uint8_t, SignatureSize> Signature; + +PublicKey const& publicKey(); +Signature sign(void* data, size_t len); +bool verify(uint8_t const* signature, uint8_t const* publicKey, void* data, size_t len); + +} + +#endif
\ No newline at end of file diff --git a/source/core/StarDataStream.cpp b/source/core/StarDataStream.cpp index c8d50a4..834f4de 100644 --- a/source/core/StarDataStream.cpp +++ b/source/core/StarDataStream.cpp @@ -205,7 +205,7 @@ size_t DataStream::readVlqU(uint64_t& i) { size_t bytesRead = Star::readVlqU(i, makeFunctionInputIterator([this]() { return this->read<uint8_t>(); })); if (bytesRead == NPos) - throw DataStreamException("Error reading VLQ encoded intenger!"); + throw DataStreamException("Error reading VLQ encoded integer!"); return bytesRead; } @@ -214,7 +214,7 @@ size_t DataStream::readVlqI(int64_t& i) { size_t bytesRead = Star::readVlqI(i, makeFunctionInputIterator([this]() { return this->read<uint8_t>(); })); if (bytesRead == NPos) - throw DataStreamException("Error reading VLQ encoded intenger!"); + throw DataStreamException("Error reading VLQ encoded integer!"); return bytesRead; } diff --git a/source/core/StarDataStreamDevices.cpp b/source/core/StarDataStreamDevices.cpp index 0c37d8d..85e6d3f 100644 --- a/source/core/StarDataStreamDevices.cpp +++ b/source/core/StarDataStreamDevices.cpp @@ -164,4 +164,11 @@ void DataStreamExternalBuffer::reset(char const* externalData, size_t len) { m_buffer.reset(externalData, len); } +void DataStreamExternalBuffer::readData(char* data, size_t len) { + m_buffer.readFull(data, len); +} +void DataStreamExternalBuffer::writeData(char const* data, size_t len) { + m_buffer.writeFull(data, len); +} + } diff --git a/source/core/StarDataStreamDevices.hpp b/source/core/StarDataStreamDevices.hpp index 88ee0e9..4a12d24 100644 --- a/source/core/StarDataStreamDevices.hpp +++ b/source/core/StarDataStreamDevices.hpp @@ -140,6 +140,9 @@ public: void reset(char const* externalData, size_t len); + void readData(char* data, size_t len) override; + void writeData(char const* data, size_t len) override; + private: ExternalBuffer m_buffer; }; |