2016-02-01 04:23:06 -05:00
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
namespace Cryptography
|
|
|
|
{
|
2016-02-08 08:27:15 -05:00
|
|
|
class Rand
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static uint32_t GenerateInt();
|
|
|
|
};
|
|
|
|
|
|
|
|
class ECDSA
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Key
|
|
|
|
{
|
|
|
|
public:
|
2016-02-08 12:43:31 -05:00
|
|
|
Key() : KeyStorage(new ecc_key)
|
|
|
|
{
|
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->KeyStorage.get()));
|
|
|
|
};
|
2016-02-09 16:13:49 -05:00
|
|
|
Key(ecc_key* key) : Key() { if(key) std::memmove(this->KeyStorage.get(), key, sizeof(*key)); };
|
2016-02-08 12:43:31 -05:00
|
|
|
Key(ecc_key key) : Key(&key) {};
|
|
|
|
~Key()
|
|
|
|
{
|
|
|
|
if (this->KeyStorage.use_count() <= 1)
|
|
|
|
{
|
|
|
|
this->Free();
|
|
|
|
}
|
|
|
|
};
|
2016-02-08 08:27:15 -05:00
|
|
|
|
2016-02-08 12:43:31 -05:00
|
|
|
bool IsValid()
|
|
|
|
{
|
|
|
|
return (!Utils::MemIsSet(this->KeyStorage.get(), 0, sizeof(*this->KeyStorage.get())));
|
|
|
|
}
|
2016-02-08 08:27:15 -05:00
|
|
|
|
|
|
|
ecc_key* GetKeyPtr()
|
|
|
|
{
|
2016-02-08 12:43:31 -05:00
|
|
|
return this->KeyStorage.get();
|
2016-02-08 08:27:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetPublicKey()
|
|
|
|
{
|
2016-02-08 12:43:31 -05:00
|
|
|
uint8_t buffer[512] = { 0 };
|
2016-02-08 08:27:15 -05:00
|
|
|
DWORD length = sizeof(buffer);
|
|
|
|
|
|
|
|
if (ecc_ansi_x963_export(this->GetKeyPtr(), buffer, &length) == CRYPT_OK)
|
|
|
|
{
|
|
|
|
return std::string(reinterpret_cast<char*>(buffer), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-02-08 12:43:31 -05:00
|
|
|
void Set(std::string pubKeyBuffer)
|
|
|
|
{
|
|
|
|
this->Free();
|
|
|
|
|
|
|
|
if (ecc_ansi_x963_import(reinterpret_cast<const uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->KeyStorage.get()) != CRYPT_OK)
|
|
|
|
{
|
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->KeyStorage.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free()
|
|
|
|
{
|
|
|
|
if (this->IsValid())
|
|
|
|
{
|
|
|
|
ecc_free(this->KeyStorage.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->KeyStorage.get()));
|
|
|
|
}
|
|
|
|
|
2016-02-08 08:27:15 -05:00
|
|
|
private:
|
2016-02-08 12:43:31 -05:00
|
|
|
std::shared_ptr<ecc_key> KeyStorage;
|
2016-02-08 08:27:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2016-02-01 04:23:06 -05:00
|
|
|
class RSA
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Key
|
|
|
|
{
|
|
|
|
public:
|
2016-02-08 12:43:31 -05:00
|
|
|
Key() : KeyStorage(new rsa_key)
|
|
|
|
{
|
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->KeyStorage.get()));
|
|
|
|
};
|
2016-02-09 16:13:49 -05:00
|
|
|
Key(rsa_key* key) : Key() { if (key) std::memmove(this->KeyStorage.get(), key, sizeof(*key)); };
|
2016-02-08 12:43:31 -05:00
|
|
|
Key(rsa_key key) : Key(&key) {};
|
|
|
|
~Key()
|
|
|
|
{
|
|
|
|
if (this->KeyStorage.use_count() <= 1)
|
|
|
|
{
|
|
|
|
this->Free();
|
|
|
|
}
|
|
|
|
};
|
2016-02-01 04:23:06 -05:00
|
|
|
|
|
|
|
rsa_key* GetKeyPtr()
|
|
|
|
{
|
2016-02-08 12:43:31 -05:00
|
|
|
return this->KeyStorage.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid()
|
|
|
|
{
|
|
|
|
return (!Utils::MemIsSet(this->KeyStorage.get(), 0, sizeof(*this->KeyStorage.get())));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free()
|
|
|
|
{
|
|
|
|
if (this->IsValid())
|
|
|
|
{
|
|
|
|
rsa_free(this->KeyStorage.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->KeyStorage.get()));
|
2016-02-01 04:23:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-02-08 12:43:31 -05:00
|
|
|
std::shared_ptr<rsa_key> KeyStorage;
|
2016-02-01 04:23:06 -05:00
|
|
|
};
|
|
|
|
|
2016-02-08 08:27:15 -05:00
|
|
|
static Key GenerateKey(int bits);
|
2016-02-01 04:23:06 -05:00
|
|
|
static std::string SignMessage(Key key, std::string message);
|
|
|
|
static bool VerifyMessage(Key key, std::string message, std::string signature);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|