ECDSA and PRGN stuff.
This commit is contained in:
@ -6,6 +6,65 @@ namespace Utils
|
||||
{
|
||||
namespace Cryptography
|
||||
{
|
||||
#pragma region Rand
|
||||
|
||||
uint32_t Rand::GenerateInt()
|
||||
{
|
||||
size_t length = 0;
|
||||
uint8_t buffer[4] = { 0 };
|
||||
|
||||
while (length != 4)
|
||||
{
|
||||
length = sprng_read(buffer, sizeof(buffer), NULL);
|
||||
}
|
||||
|
||||
return *reinterpret_cast<uint32_t*>(buffer);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region ECDSA
|
||||
|
||||
ECDSA::Key ECDSA::GenerateKey(int bits)
|
||||
{
|
||||
ECDSA::Key key;
|
||||
|
||||
register_prng(&sprng_desc);
|
||||
register_hash(&sha1_desc);
|
||||
|
||||
ltc_mp = ltm_desc;
|
||||
|
||||
ecc_make_key(NULL, find_prng("sprng"), bits / 8, key.GetKeyPtr());
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
std::string ECDSA::SignMessage(Key key, std::string message)
|
||||
{
|
||||
uint8_t buffer[0x200]; // Default size is 512
|
||||
DWORD length = sizeof(buffer);
|
||||
|
||||
register_prng(&sprng_desc);
|
||||
|
||||
ltc_mp = ltm_desc;
|
||||
|
||||
ecc_sign_hash(reinterpret_cast<const uint8_t*>(message.data()), message.size(), buffer, &length, NULL, find_prng("sprng"), key.GetKeyPtr());
|
||||
|
||||
return std::string(reinterpret_cast<char*>(buffer), length);
|
||||
}
|
||||
|
||||
bool ECDSA::VerifyMessage(Key key, std::string message, std::string signature)
|
||||
{
|
||||
ltc_mp = ltm_desc;
|
||||
|
||||
int result = 0;
|
||||
return (ecc_verify_hash(reinterpret_cast<const uint8_t*>(signature.data()), signature.size(), reinterpret_cast<const uint8_t*>(message.data()), message.size(), &result, key.GetKeyPtr()) == CRYPT_OK && result != 0);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region RSA
|
||||
|
||||
RSA::Key RSA::GenerateKey(int bits)
|
||||
{
|
||||
RSA::Key key;
|
||||
@ -45,4 +104,7 @@ namespace Utils
|
||||
return (rsa_verify_hash(reinterpret_cast<const uint8_t*>(signature.data()), signature.size(), reinterpret_cast<const uint8_t*>(message.data()), message.size(), find_hash("sha1"), 0, &result, key.GetKeyPtr()) == CRYPT_OK && result != 0);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,52 @@ namespace Utils
|
||||
{
|
||||
namespace Cryptography
|
||||
{
|
||||
class Rand
|
||||
{
|
||||
public:
|
||||
static uint32_t GenerateInt();
|
||||
};
|
||||
|
||||
class ECDSA
|
||||
{
|
||||
public:
|
||||
class Key
|
||||
{
|
||||
public:
|
||||
Key() { ZeroMemory(&this->KeyStorage, sizeof(this->KeyStorage)); };
|
||||
Key(ecc_key* key) : Key(*key) {};
|
||||
Key(ecc_key key) : KeyStorage(key) {};
|
||||
Key(const Key& obj) : KeyStorage(obj.KeyStorage) {};
|
||||
|
||||
~Key() {}
|
||||
|
||||
ecc_key* GetKeyPtr()
|
||||
{
|
||||
return &this->KeyStorage;
|
||||
}
|
||||
|
||||
std::string GetPublicKey()
|
||||
{
|
||||
uint8_t buffer[0x1000] = { 0 };
|
||||
DWORD length = sizeof(buffer);
|
||||
|
||||
if (ecc_ansi_x963_export(this->GetKeyPtr(), buffer, &length) == CRYPT_OK)
|
||||
{
|
||||
return std::string(reinterpret_cast<char*>(buffer), length);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private:
|
||||
ecc_key KeyStorage;
|
||||
};
|
||||
|
||||
static Key GenerateKey(int bits);
|
||||
static std::string SignMessage(Key key, std::string message);
|
||||
static bool VerifyMessage(Key key, std::string message, std::string signature);
|
||||
};
|
||||
|
||||
class RSA
|
||||
{
|
||||
public:
|
||||
@ -24,7 +70,7 @@ namespace Utils
|
||||
rsa_key KeyStorage;
|
||||
};
|
||||
|
||||
static RSA::Key GenerateKey(int bits);
|
||||
static Key GenerateKey(int bits);
|
||||
static std::string SignMessage(Key key, std::string message);
|
||||
static bool VerifyMessage(Key key, std::string message, std::string signature);
|
||||
};
|
||||
|
Reference in New Issue
Block a user