aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/crypto/rsa.cpp
diff options
context:
space:
mode:
authoralexvru <alexvru@ydb.tech>2023-08-15 21:09:36 +0300
committeralexvru <alexvru@ydb.tech>2023-08-15 21:42:49 +0300
commitd6f67906ea5b369b47bce8e0a7125d66114fdbde (patch)
treec9c44a3a1a396a6cab33e1260c67f2e5b8b76ea4 /library/cpp/openssl/crypto/rsa.cpp
parentf096c967c8a4b645763f901c889ca0335a0e5412 (diff)
downloadydb-d6f67906ea5b369b47bce8e0a7125d66114fdbde.tar.gz
Support BS autoconfig KIKIMR-19031
Diffstat (limited to 'library/cpp/openssl/crypto/rsa.cpp')
-rw-r--r--library/cpp/openssl/crypto/rsa.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/library/cpp/openssl/crypto/rsa.cpp b/library/cpp/openssl/crypto/rsa.cpp
new file mode 100644
index 0000000000..4b1d664826
--- /dev/null
+++ b/library/cpp/openssl/crypto/rsa.cpp
@@ -0,0 +1,56 @@
+#include "rsa.h"
+
+#include <library/cpp/openssl/big_integer/big_integer.h>
+#include <library/cpp/openssl/init/init.h>
+
+#include <util/generic/yexception.h>
+#include <util/generic/buffer.h>
+
+#include <openssl/bn.h>
+#include <openssl/rsa.h>
+
+using namespace NOpenSsl;
+using namespace NOpenSsl::NRsa;
+
+namespace {
+ struct TInit {
+ inline TInit() {
+ InitOpenSSL();
+ }
+ } INIT;
+}
+
+TPublicKey::TPublicKey(const TBigInteger& e, const TBigInteger& n)
+ : Key_(RSA_new())
+{
+ Y_ENSURE(Key_, "RSA_new() failed");
+
+ RSA_set0_key(Key_, BN_dup(n.Impl()), BN_dup(e.Impl()), nullptr);
+}
+
+TPublicKey::~TPublicKey() noexcept {
+ RSA_free(Key_);
+}
+
+size_t TPublicKey::OutputLength() const noexcept {
+ return RSA_size(Key_);
+}
+
+size_t TPublicKey::EncryptNoPad(void* dst, const void* src, size_t size) const {
+ auto len = RSA_public_encrypt(size, (const ui8*)src, (ui8*)dst, Key_, RSA_NO_PADDING);
+
+ Y_ENSURE(len >= 0, "RSA_public_encrypt() failed");
+
+ return len;
+}
+
+TBigInteger TPublicKey::EncryptNoPad(const TBigInteger& src) const {
+ const auto len1 = OutputLength();
+ const auto len2 = src.NumBytes();
+ TBuffer buf(len1 + len2);
+
+ char* buf1 = (char*)buf.Data();
+ char* buf2 = buf1 + len1;
+
+ return TBigInteger::FromRegion(buf1, EncryptNoPad(buf1, buf2, src.ToRegion(buf2)));
+}