未验证 提交 62b4ff7d 编写于 作者: Y Yanghello 提交者: GitHub

Aes_cipher_test and cipher_utils_test failed fixed (#24816)

上级 b490e41c
...@@ -88,7 +88,7 @@ option(WITH_DGC "Use DGC(Deep Gradient Compression) or not" ${WITH_DISTRIBUTE} ...@@ -88,7 +88,7 @@ option(WITH_DGC "Use DGC(Deep Gradient Compression) or not" ${WITH_DISTRIBUTE}
option(SANITIZER_TYPE "Choose the type of sanitizer, options are: Address, Leak, Memory, Thread, Undefined" OFF) option(SANITIZER_TYPE "Choose the type of sanitizer, options are: Address, Leak, Memory, Thread, Undefined" OFF)
option(WITH_LITE "Compile Paddle Fluid with Lite Engine" OFF) option(WITH_LITE "Compile Paddle Fluid with Lite Engine" OFF)
option(WITH_NCCL "Compile PaddlePaddle with NCCL support" ON) option(WITH_NCCL "Compile PaddlePaddle with NCCL support" ON)
option(WITH_CRYPTO "Compile PaddlePaddle with crypto support" OFF) option(WITH_CRYPTO "Compile PaddlePaddle with crypto support" ON)
# PY_VERSION # PY_VERSION
if(NOT PY_VERSION) if(NOT PY_VERSION)
......
...@@ -68,7 +68,8 @@ ExternalProject_Add( ...@@ -68,7 +68,8 @@ ExternalProject_Add(
SOURCE_DIR ${CRYPTOPP_SOURCE_DIR} SOURCE_DIR ${CRYPTOPP_SOURCE_DIR}
PATCH_COMMAND PATCH_COMMAND
COMMAND ${CMAKE_COMMAND} -E remove_directory "<SOURCE_DIR>/cmake/" COMMAND ${CMAKE_COMMAND} -E remove_directory "<SOURCE_DIR>/cmake/"
COMMAND git clone -b ${CRYPTOPP_TAG} https://github.com/noloader/cryptopp-cmake "<SOURCE_DIR>/cmake" COMMAND git clone https://github.com/noloader/cryptopp-cmake "<SOURCE_DIR>/cmake"
COMMAND cd "<SOURCE_DIR>/cmake" && git checkout tags/${CRYPTOPP_TAG} -b ${CRYPTOPP_TAG}
COMMAND ${CMAKE_COMMAND} -E copy_directory "<SOURCE_DIR>/cmake/" "<SOURCE_DIR>/" COMMAND ${CMAKE_COMMAND} -E copy_directory "<SOURCE_DIR>/cmake/" "<SOURCE_DIR>/"
INSTALL_DIR ${CRYPTOPP_INSTALL_DIR} INSTALL_DIR ${CRYPTOPP_INSTALL_DIR}
CMAKE_ARGS ${CRYPTOPP_CMAKE_ARGS} CMAKE_ARGS ${CRYPTOPP_CMAKE_ARGS}
......
...@@ -60,7 +60,6 @@ std::string AESCipher::EncryptInternal(const std::string& plaintext, ...@@ -60,7 +60,6 @@ std::string AESCipher::EncryptInternal(const std::string& plaintext,
CryptoPP::member_ptr<CryptoPP::SymmetricCipher> m_cipher; CryptoPP::member_ptr<CryptoPP::SymmetricCipher> m_cipher;
CryptoPP::member_ptr<CryptoPP::StreamTransformationFilter> m_filter; CryptoPP::member_ptr<CryptoPP::StreamTransformationFilter> m_filter;
bool need_iv = false; bool need_iv = false;
std::string iv = "";
const unsigned char* key_char = const unsigned char* key_char =
reinterpret_cast<const unsigned char*>(&(key.at(0))); reinterpret_cast<const unsigned char*>(&(key.at(0)));
BuildCipher(true, &need_iv, &m_cipher, &m_filter); BuildCipher(true, &need_iv, &m_cipher, &m_filter);
...@@ -77,7 +76,7 @@ std::string AESCipher::EncryptInternal(const std::string& plaintext, ...@@ -77,7 +76,7 @@ std::string AESCipher::EncryptInternal(const std::string& plaintext,
m_filter->Attach(new CryptoPP::StringSink(ciphertext)); m_filter->Attach(new CryptoPP::StringSink(ciphertext));
CryptoPP::StringSource(plaintext, true, new CryptoPP::Redirector(*m_filter)); CryptoPP::StringSource(plaintext, true, new CryptoPP::Redirector(*m_filter));
if (need_iv) { if (need_iv) {
ciphertext = iv_.append(ciphertext); return iv_ + ciphertext;
} }
return ciphertext; return ciphertext;
...@@ -88,7 +87,6 @@ std::string AESCipher::DecryptInternal(const std::string& ciphertext, ...@@ -88,7 +87,6 @@ std::string AESCipher::DecryptInternal(const std::string& ciphertext,
CryptoPP::member_ptr<CryptoPP::SymmetricCipher> m_cipher; CryptoPP::member_ptr<CryptoPP::SymmetricCipher> m_cipher;
CryptoPP::member_ptr<CryptoPP::StreamTransformationFilter> m_filter; CryptoPP::member_ptr<CryptoPP::StreamTransformationFilter> m_filter;
bool need_iv = false; bool need_iv = false;
std::string iv = "";
const unsigned char* key_char = const unsigned char* key_char =
reinterpret_cast<const unsigned char*>(&(key.at(0))); reinterpret_cast<const unsigned char*>(&(key.at(0)));
BuildCipher(false, &need_iv, &m_cipher, &m_filter); BuildCipher(false, &need_iv, &m_cipher, &m_filter);
...@@ -115,7 +113,6 @@ std::string AESCipher::AuthenticatedEncryptInternal( ...@@ -115,7 +113,6 @@ std::string AESCipher::AuthenticatedEncryptInternal(
CryptoPP::member_ptr<CryptoPP::AuthenticatedSymmetricCipher> m_cipher; CryptoPP::member_ptr<CryptoPP::AuthenticatedSymmetricCipher> m_cipher;
CryptoPP::member_ptr<CryptoPP::AuthenticatedEncryptionFilter> m_filter; CryptoPP::member_ptr<CryptoPP::AuthenticatedEncryptionFilter> m_filter;
bool need_iv = false; bool need_iv = false;
std::string iv = "";
const unsigned char* key_char = const unsigned char* key_char =
reinterpret_cast<const unsigned char*>(&(key.at(0))); reinterpret_cast<const unsigned char*>(&(key.at(0)));
BuildAuthEncCipher(&need_iv, &m_cipher, &m_filter); BuildAuthEncCipher(&need_iv, &m_cipher, &m_filter);
...@@ -143,7 +140,6 @@ std::string AESCipher::AuthenticatedDecryptInternal( ...@@ -143,7 +140,6 @@ std::string AESCipher::AuthenticatedDecryptInternal(
CryptoPP::member_ptr<CryptoPP::AuthenticatedSymmetricCipher> m_cipher; CryptoPP::member_ptr<CryptoPP::AuthenticatedSymmetricCipher> m_cipher;
CryptoPP::member_ptr<CryptoPP::AuthenticatedDecryptionFilter> m_filter; CryptoPP::member_ptr<CryptoPP::AuthenticatedDecryptionFilter> m_filter;
bool need_iv = false; bool need_iv = false;
std::string iv = "";
const unsigned char* key_char = const unsigned char* key_char =
reinterpret_cast<const unsigned char*>(&(key.at(0))); reinterpret_cast<const unsigned char*>(&(key.at(0)));
BuildAuthDecCipher(&need_iv, &m_cipher, &m_filter); BuildAuthDecCipher(&need_iv, &m_cipher, &m_filter);
...@@ -266,14 +262,15 @@ std::string AESCipher::Decrypt(const std::string& ciphertext, ...@@ -266,14 +262,15 @@ std::string AESCipher::Decrypt(const std::string& ciphertext,
void AESCipher::EncryptToFile(const std::string& plaintext, void AESCipher::EncryptToFile(const std::string& plaintext,
const std::string& key, const std::string& key,
const std::string& filename) { const std::string& filename) {
std::ofstream fout(filename); std::ofstream fout(filename, std::ios::binary);
fout << this->Encrypt(plaintext, key); std::string ciphertext = this->Encrypt(plaintext, key);
fout.write(ciphertext.data(), ciphertext.size());
fout.close(); fout.close();
} }
std::string AESCipher::DecryptFromFile(const std::string& key, std::string AESCipher::DecryptFromFile(const std::string& key,
const std::string& filename) { const std::string& filename) {
std::ifstream fin(filename); std::ifstream fin(filename, std::ios::binary);
std::string ciphertext{std::istreambuf_iterator<char>(fin), std::string ciphertext{std::istreambuf_iterator<char>(fin),
std::istreambuf_iterator<char>()}; std::istreambuf_iterator<char>()};
fin.close(); fin.close();
......
...@@ -43,7 +43,7 @@ std::string CipherUtils::GenKeyToFile(int length, const std::string& filename) { ...@@ -43,7 +43,7 @@ std::string CipherUtils::GenKeyToFile(int length, const std::string& filename) {
// CryptoPP::byte key[length]; // CryptoPP::byte key[length];
prng.GenerateBlock(reinterpret_cast<unsigned char*>(&(rng.at(0))), prng.GenerateBlock(reinterpret_cast<unsigned char*>(&(rng.at(0))),
rng.size()); rng.size());
std::ofstream fout(filename); std::ofstream fout(filename, std::ios::binary);
PADDLE_ENFORCE_EQ(fout.is_open(), true, PADDLE_ENFORCE_EQ(fout.is_open(), true,
paddle::platform::errors::Unavailable( paddle::platform::errors::Unavailable(
"Failed to open file : %s, " "Failed to open file : %s, "
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册