Skip to content

Commit

Permalink
chore(XXXXL): huge cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie-Cui committed Nov 15, 2024
1 parent f4401f6 commit db0668f
Show file tree
Hide file tree
Showing 102 changed files with 304 additions and 343 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ target_link_libraries(
ExtFourQ::fourq)

if(YACL_WITH_EXT_OPENSSL)
target_link_libraries(yacl PUBLIC ExtOpenSSL::Crypto ExtOpenSSL::SSL)
target_link_libraries(yacl PUBLIC Extossl::Crypto Extossl::SSL)
else()
target_link_libraries(yacl PUBLIC OpenSSL::Crypto OpenSSL::SSL)
target_link_libraries(yacl PUBLIC ossl::Crypto ossl::SSL)
endif()
2 changes: 1 addition & 1 deletion bazel/hash_drbg.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ cc_library(
copts = ["-Wno-parentheses"],
visibility = ["//visibility:public"],
deps = [
"@yacl//yacl/crypto:openssl_wrappers",
"@yacl//yacl/crypto:ossl_wrappers",
],
)
4 changes: 2 additions & 2 deletions cmake/ExternalOpenSSL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ if(YACL_WITH_EXT_OPENSSL)
set_target_properties(
ExtOpenSSL_Crypto PROPERTIES IMPORTED_LOCATION
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libcrypto.a)
add_library(ExtOpenSSL::Crypto ALIAS ExtOpenSSL_Crypto)
add_library(Extossl::Crypto ALIAS ExtOpenSSL_Crypto)

add_library(ExtOpenSSL_SSL STATIC IMPORTED)
set_target_properties(
ExtOpenSSL_SSL PROPERTIES IMPORTED_LOCATION
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libssl.a)
add_library(ExtOpenSSL::SSL ALIAS ExtOpenSSL_SSL)
add_library(Extossl::SSL ALIAS ExtOpenSSL_SSL)
endif()
Empty file added examples/cmake-project/.keep
Empty file.
39 changes: 0 additions & 39 deletions yacl/BUILD.bazel

This file was deleted.

29 changes: 0 additions & 29 deletions yacl/CMakeLists.txt

This file was deleted.

22 changes: 22 additions & 0 deletions yacl/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,25 @@ yacl_cc_library(
name = "aligned_vector",
srcs = ["aligned_vector.h"],
)

yacl_cc_library(
name = "secparam",
srcs = ["secparam.cc"],
hdrs = [
"secparam.h",
],
deps = [
":exception",
":int128",
"//yacl/math:gadget",
"//yacl/utils:compile_time_utils",
],
)

yacl_cc_test(
name = "secparam_test",
srcs = ["secparam_test.cc"],
deps = [
":secparam",
],
)
9 changes: 8 additions & 1 deletion yacl/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

set(YACL_SOURCE_FILES
${YACL_SOURCE_FILES} ${CMAKE_CURRENT_LIST_DIR}/exception.cc
${CMAKE_CURRENT_LIST_DIR}/int128.cc)
${CMAKE_CURRENT_LIST_DIR}/int128.cc
${CMAKE_CURRENT_LIST_DIR}/secparam.cc)

# Add header files for installation
install(
FILES ${CMAKE_CURRENT_LIST_DIR}/aligened_vector.h
${CMAKE_CURRENT_LIST_DIR}/block.h
${CMAKE_CURRENT_LIST_DIR}/secparam.h
${CMAKE_CURRENT_LIST_DIR}/buffer.h
${CMAKE_CURRENT_LIST_DIR}/byte_container_view.h
${CMAKE_CURRENT_LIST_DIR}/dynamic_bitset.h
Expand All @@ -35,3 +37,8 @@ add_executable(buffer_test buffer_test.cc)
target_link_libraries(buffer_test PRIVATE yacl gtest_main)
target_link_options(buffer_test PRIVATE "-all_load") # for spi
add_test(NAME buffer_test COMMAND buffer_test)

add_executable(secparam_test secparam_test.cc)
target_link_libraries(secparam_test PRIVATE yacl gtest_main)
target_link_options(secparam_test PRIVATE "-all_load") # for spi
add_test(NAME secparam_test COMMAND secparam_test)
2 changes: 1 addition & 1 deletion yacl/secparam.cc → yacl/base/secparam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "yacl/secparam.h"
#include "yacl/base/secparam.h"

namespace yacl::crypto {
SecParam::C SecParam::glob_c = SecParam::C::INF; // init to infinity
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion yacl/secparam_test.cc → yacl/base/secparam_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "yacl/secparam.h"
#include "yacl/base/secparam.h"

#include "gtest/gtest.h"

Expand Down
8 changes: 4 additions & 4 deletions yacl/crypto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ load("//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test")
package(default_visibility = ["//visibility:public"])

yacl_cc_library(
name = "openssl_wrappers",
name = "ossl_wrappers",
hdrs = [
"openssl_wrappers.h",
"ossl_wrappers.h",
],
deps = [
"//yacl/base:byte_container_view",
Expand All @@ -38,8 +38,8 @@ yacl_cc_library(
"key_utils.h",
],
deps = [
":openssl_wrappers",
"//yacl:secparam",
":ossl_wrappers",
"//yacl/base:secparam",
"//yacl/crypto/hash:hash_interface",
"//yacl/io/stream",
],
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set(YACL_SOURCE_FILES ${YACL_SOURCE_FILES}

# Add header files for installation
install(FILES ${CMAKE_CURRENT_LIST_DIR}/key_utils.h
${CMAKE_CURRENT_LIST_DIR}/openssl_wrappers.h
${CMAKE_CURRENT_LIST_DIR}/ossl_wrappers.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/yacl/crypto)

add_subdirectory(aead)
Expand Down
4 changes: 2 additions & 2 deletions yacl/crypto/aead/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ yacl_cc_library(
srcs = ["all_gcm.cc"],
hdrs = ["all_gcm.h"],
deps = [
"//yacl:secparam",
"//yacl/base:secparam",
"//yacl/base:int128",
"//yacl/crypto:key_utils",
],
Expand All @@ -40,7 +40,7 @@ yacl_cc_library(
srcs = ["sm4_mte.cc"],
hdrs = ["sm4_mte.h"],
deps = [
"//yacl:secparam",
"//yacl/base:secparam",
"//yacl/crypto/block_cipher:symmetric_crypto",
"//yacl/crypto/hash:ssl_hash",
"//yacl/crypto/hmac:hmac_sm3",
Expand Down
10 changes: 5 additions & 5 deletions yacl/crypto/aead/all_gcm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "yacl/crypto/aead/all_gcm.h"

#include "yacl/crypto/openssl_wrappers.h"
#include "yacl/crypto/ossl_wrappers.h"

namespace yacl::crypto {

Expand Down Expand Up @@ -47,9 +47,9 @@ void GcmCrypto::Encrypt(ByteContainerView plaintext, ByteContainerView aad,
YACL_ENFORCE_EQ(mac.size(), GetMacSize(schema_));

// init openssl evp cipher context
auto ctx = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
auto ctx = ossl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
YACL_ENFORCE(ctx != nullptr, "Failed to new evp cipher context.");
const auto cipher = openssl::FetchEvpCipher(ToString(schema_));
const auto cipher = ossl::FetchEvpCipher(ToString(schema_));
YACL_ENFORCE(cipher != nullptr);
YACL_ENFORCE(key_.size() == (size_t)EVP_CIPHER_key_length(cipher.get()));
YACL_ENFORCE(iv_.size() == (size_t)EVP_CIPHER_iv_length(cipher.get()));
Expand Down Expand Up @@ -83,11 +83,11 @@ void GcmCrypto::Decrypt(ByteContainerView ciphertext, ByteContainerView aad,
YACL_ENFORCE_EQ(mac.size(), GetMacSize(schema_));

// init openssl evp cipher context
auto ctx = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
auto ctx = ossl::UniqueCipherCtx(EVP_CIPHER_CTX_new());

YACL_ENFORCE(ctx.get(), "Failed to new evp cipher context.");

const auto cipher = openssl::FetchEvpCipher(ToString(schema_));
const auto cipher = ossl::FetchEvpCipher(ToString(schema_));
YACL_ENFORCE_EQ(key_.size(), (size_t)EVP_CIPHER_key_length(cipher.get()));
YACL_ENFORCE_EQ(iv_.size(), (size_t)EVP_CIPHER_iv_length(cipher.get()));
YACL_ENFORCE(EVP_DecryptInit_ex(ctx.get(), cipher.get(), nullptr, key_.data(),
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/aead/all_gcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "absl/types/span.h"

#include "yacl/base/byte_container_view.h"
#include "yacl/secparam.h"
#include "yacl/base/secparam.h"

/* security parameter declaration */
YACL_MODULE_DECLARE("all_gcm", SecParam::C::k128, SecParam::S::INF);
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/aead/sm4_mte.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <vector>

#include "yacl/base/byte_container_view.h"
#include "yacl/secparam.h"
#include "yacl/base/secparam.h"

/* submodules */
#include "yacl/crypto/block_cipher/symmetric_crypto.h"
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/aes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ yacl_cc_library(
],
copts = AES_COPT_FLAGS,
deps = [
"//yacl:secparam",
"//yacl/base:secparam",
"//yacl/base:int128",
] + select({
"@platforms//cpu:aarch64": [
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/aes/aes_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

#include "yacl/base/exception.h"
#include "yacl/base/int128.h"
#include "yacl/secparam.h"
#include "yacl/base/secparam.h"

#ifndef __aarch64__
// sse
Expand Down
4 changes: 2 additions & 2 deletions yacl/crypto/block_cipher/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ yacl_cc_library(
],
copts = AES_COPT_FLAGS,
deps = [
"//yacl:secparam",
"//yacl/base:secparam",
"//yacl/base:int128",
"//yacl/crypto:openssl_wrappers",
"//yacl/crypto:ossl_wrappers",
],
)

Expand Down
14 changes: 7 additions & 7 deletions yacl/crypto/block_cipher/symmetric_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
#include "spdlog/spdlog.h"

#include "yacl/base/exception.h"
#include "yacl/crypto/openssl_wrappers.h"
#include "yacl/crypto/ossl_wrappers.h"

namespace yacl::crypto {
namespace {

void SetupEVPCipherCtx(openssl::UniqueCipherCtx* ctx,
void SetupEVPCipherCtx(ossl::UniqueCipherCtx* ctx,
SymmetricCrypto::CryptoType type, uint128_t key,
uint128_t iv, int enc) {
// This uses AES-128, so the key must be 128 bits.
const auto cipher = openssl::FetchEvpCipher(ToString(type));
const auto cipher = ossl::FetchEvpCipher(ToString(type));
YACL_ENFORCE(sizeof(key) == EVP_CIPHER_key_length(cipher.get()));
const auto* key_data = reinterpret_cast<const uint8_t*>(&key);
const auto* iv_data = reinterpret_cast<const uint8_t*>(&iv);
Expand Down Expand Up @@ -72,8 +72,8 @@ uint128_t CopyDataAsUint128(const uint8_t* data) {
SymmetricCrypto::SymmetricCrypto(CryptoType type, uint128_t key, uint128_t iv)
: type_(type), key_(key), iv_(iv) {
// Init openssl encryption/decryption context
enc_ctx_ = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
dec_ctx_ = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
enc_ctx_ = ossl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
dec_ctx_ = ossl::UniqueCipherCtx(EVP_CIPHER_CTX_new());

SetupEVPCipherCtx(&enc_ctx_, type_, key_, iv_, 1);
SetupEVPCipherCtx(&dec_ctx_, type_, key_, iv_, 0);
Expand All @@ -85,8 +85,8 @@ SymmetricCrypto::SymmetricCrypto(CryptoType type, ByteContainerView key,
key_(CopyDataAsUint128(key.data())),
iv_(CopyDataAsUint128(iv.data())) {
// Init openssl encryption/decryption context
enc_ctx_ = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
dec_ctx_ = openssl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
enc_ctx_ = ossl::UniqueCipherCtx(EVP_CIPHER_CTX_new());
dec_ctx_ = ossl::UniqueCipherCtx(EVP_CIPHER_CTX_new());

SetupEVPCipherCtx(&enc_ctx_, type_, key_, iv_, 1);
SetupEVPCipherCtx(&dec_ctx_, type_, key_, iv_, 0);
Expand Down
8 changes: 4 additions & 4 deletions yacl/crypto/block_cipher/symmetric_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

#include "yacl/base/byte_container_view.h"
#include "yacl/base/int128.h"
#include "yacl/crypto/openssl_wrappers.h"
#include "yacl/secparam.h"
#include "yacl/crypto/ossl_wrappers.h"
#include "yacl/base/secparam.h"

/* security parameter declaration */
YACL_MODULE_DECLARE("aes_all_modes", SecParam::C::k128, SecParam::S::INF);
Expand Down Expand Up @@ -91,8 +91,8 @@ class SymmetricCrypto {
const uint128_t iv_; // Initialize vector

// openssl cipher contexts
openssl::UniqueCipherCtx enc_ctx_;
openssl::UniqueCipherCtx dec_ctx_;
ossl::UniqueCipherCtx enc_ctx_;
ossl::UniqueCipherCtx dec_ctx_;
};

class AesCbcCrypto : public SymmetricCrypto {
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/ecc/openssl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ yacl_cc_library(
"openssl_group.h",
],
deps = [
"//yacl/crypto:openssl_wrappers",
"//yacl/crypto:ossl_wrappers",
"//yacl/crypto/ecc:spi",
"//yacl/crypto/hash:blake3",
"//yacl/crypto/hash:ssl_hash",
Expand Down
4 changes: 2 additions & 2 deletions yacl/crypto/ecc/openssl/openssl_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "yacl/crypto/ecc/openssl/openssl_group.h"

namespace yacl::crypto::openssl {
namespace yacl::crypto::ossl {

static const std::string kLibName = "OpenSSL";

Expand Down Expand Up @@ -156,4 +156,4 @@ bool OpensslGroup::IsSupported(const CurveMeta &meta) {

std::string OpensslGroup::GetLibraryName() const { return kLibName; }

} // namespace yacl::crypto::openssl
} // namespace yacl::crypto::ossl
Loading

0 comments on commit db0668f

Please sign in to comment.