diff options
Diffstat (limited to 'src/common/Cryptography')
-rw-r--r-- | src/common/Cryptography/RSA.cpp | 17 | ||||
-rw-r--r-- | src/common/Cryptography/RSA.h | 18 |
2 files changed, 33 insertions, 2 deletions
diff --git a/src/common/Cryptography/RSA.cpp b/src/common/Cryptography/RSA.cpp index 11bbbe1dac5..96199d34bb0 100644 --- a/src/common/Cryptography/RSA.cpp +++ b/src/common/Cryptography/RSA.cpp @@ -16,6 +16,8 @@ */ #include "RSA.h" +#include "BigNumber.h" +#include <openssl/bn.h> #include <openssl/pem.h> #include <algorithm> #include <iterator> @@ -88,6 +90,13 @@ bool Trinity::Crypto::RSA::LoadFromString(std::string const& keyPem, KeyTag) return true; } +BigNumber Trinity::Crypto::RSA::GetModulus() const +{ + BigNumber bn; + BN_copy(bn.BN(), _rsa->n); + return bn; +} + template <typename KeyTag> bool Trinity::Crypto::RSA::Encrypt(uint8 const* data, std::size_t dataLength, uint8* output, int32 paddingType) { @@ -97,6 +106,14 @@ bool Trinity::Crypto::RSA::Encrypt(uint8 const* data, std::size_t dataLength, ui return result != -1; } +bool Trinity::Crypto::RSA::Sign(int32 hashType, uint8 const* dataHash, std::size_t dataHashLength, uint8* output) +{ + uint32 signatureLength = 0; + int result = RSA_sign(hashType, dataHash, dataHashLength, output, &signatureLength, _rsa); + std::reverse(output, output + GetOutputSize()); + return result != -1; +} + namespace Trinity { namespace Crypto diff --git a/src/common/Cryptography/RSA.h b/src/common/Cryptography/RSA.h index 42849c03e4d..c0066ccbcfa 100644 --- a/src/common/Cryptography/RSA.h +++ b/src/common/Cryptography/RSA.h @@ -16,10 +16,13 @@ */ #include "Define.h" +#include <openssl/objects.h> #include <openssl/rsa.h> #include <string> #include <type_traits> +class BigNumber; + namespace Trinity { namespace Crypto @@ -27,11 +30,13 @@ namespace Crypto class TC_COMMON_API RSA { public: + struct PublicKey {}; + struct PrivateKey {}; + struct NoPadding : std::integral_constant<int32, RSA_NO_PADDING> {}; struct PKCS1Padding : std::integral_constant<int32, RSA_PKCS1_PADDING> {}; - struct PrivateKey {}; - struct PublicKey {}; + struct SHA256 : std::integral_constant<int32, NID_sha256> {}; RSA(); RSA(RSA&& rsa); @@ -44,6 +49,7 @@ public: bool LoadFromString(std::string const& keyPem, KeyTag); uint32 GetOutputSize() const { return uint32(RSA_size(_rsa)); } + BigNumber GetModulus() const; template <typename KeyTag, typename PaddingTag> bool Encrypt(uint8 const* data, std::size_t dataLength, uint8* output, KeyTag, PaddingTag) @@ -51,10 +57,18 @@ public: return Encrypt<KeyTag>(data, dataLength, output, PaddingTag::value); } + template <typename HashTag> + bool Sign(uint8 const* dataHash, std::size_t dataHashLength, uint8* output, HashTag) + { + return Sign(HashTag::value, dataHash, dataHashLength, output); + } + private: template <typename KeyTag> bool Encrypt(uint8 const* data, std::size_t dataLength, uint8* output, int32 paddingType); + bool Sign(int32 hashType, uint8 const* dataHash, std::size_t dataHashLength, uint8* output); + RSA(RSA const& rsa) = delete; RSA& operator=(RSA const& rsa) = delete; |