diff options
author | komels <komels@yandex-team.ru> | 2022-04-15 16:53:39 +0300 |
---|---|---|
committer | komels <komels@yandex-team.ru> | 2022-04-15 16:53:39 +0300 |
commit | 703a2fb6e100d202d1c7fcd052d73bd5affef408 (patch) | |
tree | 22b7320c06bb04d86dbf7b9af9ae44281331cd15 /library/cpp/openssl/crypto | |
parent | 3375bbfda1e2afb03aa2072bf5f2f2c3a26026e8 (diff) | |
download | ydb-703a2fb6e100d202d1c7fcd052d73bd5affef408.tar.gz |
Move 'kikimr/yndx'-depending tests out of ydb/core
ref:0a380e13308d579e0545a76924330d1ca5129c43
Diffstat (limited to 'library/cpp/openssl/crypto')
-rw-r--r-- | library/cpp/openssl/crypto/CMakeLists.txt | 22 | ||||
-rw-r--r-- | library/cpp/openssl/crypto/rsa.cpp | 56 | ||||
-rw-r--r-- | library/cpp/openssl/crypto/rsa.h | 34 | ||||
-rw-r--r-- | library/cpp/openssl/crypto/sha.cpp | 62 | ||||
-rw-r--r-- | library/cpp/openssl/crypto/sha.h | 78 | ||||
-rw-r--r-- | library/cpp/openssl/crypto/sha_ut.cpp | 62 | ||||
-rw-r--r-- | library/cpp/openssl/crypto/ut/rsa_ut.cpp | 28 |
7 files changed, 0 insertions, 342 deletions
diff --git a/library/cpp/openssl/crypto/CMakeLists.txt b/library/cpp/openssl/crypto/CMakeLists.txt deleted file mode 100644 index 5f6664acd8..0000000000 --- a/library/cpp/openssl/crypto/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ - -# This file was gererated by the build system used internally in the Yandex monorepo. -# Only simple modifications are allowed (adding source-files to targets, adding simple properties -# like target_include_directories). These modifications will be ported to original -# ya.make files by maintainers. Any complex modifications which can't be ported back to the -# original buildsystem will not be accepted. - - -find_package(OpenSSL REQUIRED) - -add_library(cpp-openssl-crypto) -target_link_libraries(cpp-openssl-crypto PUBLIC - contrib-libs-cxxsupp - yutil - OpenSSL::OpenSSL - cpp-openssl-big_integer - cpp-openssl-init -) -target_sources(cpp-openssl-crypto PRIVATE - ${CMAKE_SOURCE_DIR}/library/cpp/openssl/crypto/sha.cpp - ${CMAKE_SOURCE_DIR}/library/cpp/openssl/crypto/rsa.cpp -) diff --git a/library/cpp/openssl/crypto/rsa.cpp b/library/cpp/openssl/crypto/rsa.cpp deleted file mode 100644 index 350e0c0815..0000000000 --- a/library/cpp/openssl/crypto/rsa.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#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 <contrib/libs/openssl/include/openssl/bn.h> -#include <contrib/libs/openssl/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))); -} diff --git a/library/cpp/openssl/crypto/rsa.h b/library/cpp/openssl/crypto/rsa.h deleted file mode 100644 index 3bf9e4a233..0000000000 --- a/library/cpp/openssl/crypto/rsa.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include <util/generic/utility.h> -#include <util/generic/noncopyable.h> - -struct rsa_st; - -namespace NOpenSsl { - class TBigInteger; - - namespace NRsa { - class TPublicKey: public TNonCopyable { - public: - inline TPublicKey(TPublicKey&& other) noexcept { - Swap(other); - } - - TPublicKey(const TBigInteger& e, const TBigInteger& n); - ~TPublicKey() noexcept; - - size_t OutputLength() const noexcept; - - TBigInteger EncryptNoPad(const TBigInteger& src) const; - size_t EncryptNoPad(void* dst, const void* src, size_t size) const; - - inline void Swap(TPublicKey& other) noexcept { - DoSwap(Key_, other.Key_); - } - - private: - rsa_st* Key_ = nullptr; - }; - }; -} diff --git a/library/cpp/openssl/crypto/sha.cpp b/library/cpp/openssl/crypto/sha.cpp deleted file mode 100644 index 8e7e88ccde..0000000000 --- a/library/cpp/openssl/crypto/sha.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "sha.h" - -#include <util/generic/yexception.h> - -#include <contrib/libs/openssl/include/openssl/sha.h> - -namespace NOpenSsl { - namespace NSha1 { - static_assert(DIGEST_LENGTH == SHA_DIGEST_LENGTH); - - TDigest Calc(const void* data, size_t dataSize) { - TDigest digest; - Y_ENSURE(SHA1(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr); - return digest; - } - - TCalcer::TCalcer() - : Context{new SHAstate_st} { - Y_ENSURE(SHA1_Init(Context.Get()) == 1); - } - - TCalcer::~TCalcer() { - } - - void TCalcer::Update(const void* data, size_t dataSize) { - Y_ENSURE(SHA1_Update(Context.Get(), data, dataSize) == 1); - } - - TDigest TCalcer::Final() { - TDigest digest; - Y_ENSURE(SHA1_Final(digest.data(), Context.Get()) == 1); - return digest; - } - } - namespace NSha256 { - static_assert(DIGEST_LENGTH == SHA256_DIGEST_LENGTH); - - TDigest Calc(const void* data, size_t dataSize) { - TDigest digest; - Y_ENSURE(SHA256(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr); - return digest; - } - - TCalcer::TCalcer() - : Context{new SHA256state_st} { - Y_ENSURE(SHA256_Init(Context.Get()) == 1); - } - - TCalcer::~TCalcer() { - } - - void TCalcer::Update(const void* data, size_t dataSize) { - Y_ENSURE(SHA256_Update(Context.Get(), data, dataSize) == 1); - } - - TDigest TCalcer::Final() { - TDigest digest; - Y_ENSURE(SHA256_Final(digest.data(), Context.Get()) == 1); - return digest; - } - } -} diff --git a/library/cpp/openssl/crypto/sha.h b/library/cpp/openssl/crypto/sha.h deleted file mode 100644 index dbc2dfa526..0000000000 --- a/library/cpp/openssl/crypto/sha.h +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/generic/strbuf.h> -#include <util/system/types.h> - -#include <array> - -struct SHAstate_st; -struct SHA256state_st; - -namespace NOpenSsl::NSha1 { - constexpr size_t DIGEST_LENGTH = 20; - using TDigest = std::array<ui8, DIGEST_LENGTH>; - - // not fragmented input - TDigest Calc(const void* data, size_t dataSize); - - inline TDigest Calc(TStringBuf s) { - return Calc(s.data(), s.length()); - } - - // fragmented input - class TCalcer { - public: - TCalcer(); - ~TCalcer(); - void Update(const void* data, size_t dataSize); - - void Update(TStringBuf s) { - Update(s.data(), s.length()); - } - - template <typename T> - void UpdateWithPodValue(const T& value) { - Update(&value, sizeof(value)); - } - - TDigest Final(); - - private: - THolder<SHAstate_st> Context; - }; -} - -namespace NOpenSsl::NSha256 { - constexpr size_t DIGEST_LENGTH = 32; - using TDigest = std::array<ui8, DIGEST_LENGTH>; - - // not fragmented input - TDigest Calc(const void* data, size_t dataSize); - - inline TDigest Calc(TStringBuf s) { - return Calc(s.data(), s.length()); - } - - // fragmented input - class TCalcer { - public: - TCalcer(); - ~TCalcer(); - void Update(const void* data, size_t dataSize); - - void Update(TStringBuf s) { - Update(s.data(), s.length()); - } - - template <typename T> - void UpdateWithPodValue(const T& value) { - Update(&value, sizeof(value)); - } - - TDigest Final(); - - private: - THolder<SHA256state_st> Context; - }; -} diff --git a/library/cpp/openssl/crypto/sha_ut.cpp b/library/cpp/openssl/crypto/sha_ut.cpp deleted file mode 100644 index 4b6117f960..0000000000 --- a/library/cpp/openssl/crypto/sha_ut.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include <library/cpp/testing/unittest/registar.h> - -#include "sha.h" - -constexpr TStringBuf SomeAlignedShaTestData = "some _aligned_ test data for SHA-family: align align align align"; - -Y_UNIT_TEST_SUITE(SHA){ - Y_UNIT_TEST(CheckOfTestDataAlignment){ - UNIT_ASSERT_VALUES_EQUAL(SomeAlignedShaTestData.size() % sizeof(ui32), 0); - } - - Y_UNIT_TEST(Sha1Value) { - // bash$ echo -n $SomeAlignedShaTestData | sha1sum - const TStringBuf precalculatedDigest = - "\xA2\x29\x8E\xE2\xEA\x06\x27\x45" - "\x27\xC7\x78\x87\x16\x21\x8A\xA5" - "\x0D\xBA\xBA\xB2"sv; - - auto digest = NOpenSsl::NSha1::Calc(SomeAlignedShaTestData.data(), SomeAlignedShaTestData.size()); - - UNIT_ASSERT_VALUES_EQUAL(precalculatedDigest.size(), digest.size()); - UNIT_ASSERT_VALUES_EQUAL(memcmp(precalculatedDigest.data(), digest.data(), digest.size()), 0); - } - - Y_UNIT_TEST(Sha256Value) { - // bash$ echo -n $SomeAlignedShaTestData | sha256sum - const TStringBuf precalculatedDigest = - "\xED\x64\x0D\x43\xF7\x6D\x71\x98" - "\x39\x19\xF6\xE6\x70\x21\x82\x11" - "\xEF\x3B\xF0\xF4\x35\xBF\x42\xAB" - "\x1C\x5C\x01\xCD\x20\x33\xD2\xFA"sv; - - auto digest = NOpenSsl::NSha256::Calc(SomeAlignedShaTestData.data(), SomeAlignedShaTestData.size()); - - UNIT_ASSERT_VALUES_EQUAL(precalculatedDigest.size(), digest.size()); - UNIT_ASSERT_VALUES_EQUAL(memcmp(precalculatedDigest.data(), digest.data(), digest.size()), 0); - } - - Y_UNIT_TEST(FragmentedEqualNotFragmented) { - const char* head = SomeAlignedShaTestData.data(); - const char* current = head; - NOpenSsl::NSha1::TCalcer sha; - int intValue; - std::copy_n(current, sizeof(intValue), (char*)&intValue); - current += sizeof(intValue); - sha.UpdateWithPodValue(intValue); - double doubleValue; - std::copy_n(current, sizeof(doubleValue), (char*)&doubleValue); - current += sizeof(doubleValue); - sha.UpdateWithPodValue(doubleValue); - char str[7]; - std::copy_n(current, std::size(str), str); - current += std::size(str); - sha.UpdateWithPodValue(str); - sha.Update(current, SomeAlignedShaTestData.size() - (current - head)); - auto fragmentedDigest = sha.Final(); - - auto notFragmentedDigest = NOpenSsl::NSha1::Calc(SomeAlignedShaTestData.data(), SomeAlignedShaTestData.size()); - - UNIT_ASSERT_VALUES_EQUAL(memcmp(fragmentedDigest.data(), notFragmentedDigest.data(), notFragmentedDigest.size()), 0); - } -}; // UNITTEST_SIMPLE_SUITE(SHA) diff --git a/library/cpp/openssl/crypto/ut/rsa_ut.cpp b/library/cpp/openssl/crypto/ut/rsa_ut.cpp deleted file mode 100644 index 99f0377432..0000000000 --- a/library/cpp/openssl/crypto/ut/rsa_ut.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "rsa.h" - -#include <library/cpp/testing/unittest/registar.h> -#include <library/cpp/openssl/big_integer/big_integer.h> - -#include <util/system/byteorder.h> - -using namespace NOpenSsl; -using namespace NOpenSsl::NRsa; - -Y_UNIT_TEST_SUITE(Rsa) { - Y_UNIT_TEST(Encrypt) { - // example from Ru.Wikipedia - const auto originData = TBigInteger::FromULong(111111); - - const auto n = TBigInteger::FromULong(3); - const auto e = TBigInteger::FromULong(9173503); - - // check key reuse - for (size_t i = 0; i < 10; ++i) { - UNIT_ASSERT_VALUES_EQUAL(TBigInteger::FromULong(4051753), TPublicKey(n, e).EncryptNoPad(originData)); - } - - UNIT_ASSERT_VALUES_EQUAL(originData, TBigInteger::FromULong(111111)); - UNIT_ASSERT_VALUES_EQUAL(n, TBigInteger::FromULong(3)); - UNIT_ASSERT_VALUES_EQUAL(e, TBigInteger::FromULong(9173503)); - } -}; |