Add crypto api (#24694)
parent
dbcd7c69e9
commit
5a7a517cde
@ -0,0 +1,82 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
INCLUDE(ExternalProject)
|
||||
|
||||
SET(CRYPTOPP_PREFIX_DIR ${THIRD_PARTY_PATH}/cryptopp)
|
||||
SET(CRYPTOPP_INSTALL_DIR ${THIRD_PARTY_PATH}/install/cryptopp)
|
||||
SET(CRYPTOPP_INCLUDE_DIR "${CRYPTOPP_INSTALL_DIR}/include" CACHE PATH "cryptopp include directory." FORCE)
|
||||
SET(CRYPTOPP_REPOSITORY https://github.com/weidai11/cryptopp.git)
|
||||
SET(CRYPTOPP_TAG CRYPTOPP_8_2_0)
|
||||
|
||||
IF(WIN32)
|
||||
SET(CRYPTOPP_LIBRARIES "${CRYPTOPP_INSTALL_DIR}/lib/cryptopp-static.lib" CACHE FILEPATH "cryptopp library." FORCE)
|
||||
SET(CRYPTOPP_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
|
||||
set(CompilerFlags
|
||||
CMAKE_CXX_FLAGS
|
||||
CMAKE_CXX_FLAGS_DEBUG
|
||||
CMAKE_CXX_FLAGS_RELEASE
|
||||
CMAKE_C_FLAGS
|
||||
CMAKE_C_FLAGS_DEBUG
|
||||
CMAKE_C_FLAGS_RELEASE
|
||||
)
|
||||
foreach(CompilerFlag ${CompilerFlags})
|
||||
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
|
||||
endforeach()
|
||||
ELSE(WIN32)
|
||||
SET(CRYPTOPP_LIBRARIES "${CRYPTOPP_INSTALL_DIR}/lib/libcryptopp.a" CACHE FILEPATH "cryptopp library." FORCE)
|
||||
SET(CRYPTOPP_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
|
||||
ENDIF(WIN32)
|
||||
|
||||
set(CRYPTOPP_CMAKE_ARGS ${COMMON_CMAKE_ARGS}
|
||||
-DBUILD_SHARED=ON
|
||||
-DBUILD_STATIC=ON
|
||||
-DBUILD_TESTING=OFF
|
||||
-DCMAKE_INSTALL_LIBDIR=${CRYPTOPP_INSTALL_DIR}/lib
|
||||
-DCMAKE_INSTALL_PREFIX=${CRYPTOPP_INSTALL_DIR}
|
||||
-DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
|
||||
-DCMAKE_CXX_FLAGS=${CRYPTOPP_CMAKE_CXX_FLAGS}
|
||||
-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
|
||||
)
|
||||
|
||||
INCLUDE_DIRECTORIES(${CRYPTOPP_INCLUDE_DIR})
|
||||
|
||||
cache_third_party(extern_cryptopp
|
||||
REPOSITORY ${CRYPTOPP_REPOSITORY}
|
||||
TAG ${CRYPTOPP_TAG}
|
||||
DIR CRYPTOPP_SOURCE_DIR)
|
||||
|
||||
ExternalProject_Add(
|
||||
extern_cryptopp
|
||||
${EXTERNAL_PROJECT_LOG_ARGS}
|
||||
${SHALLOW_CLONE}
|
||||
"${CRYPTOPP_DOWNLOAD_CMD}"
|
||||
PREFIX ${CRYPTOPP_PREFIX_DIR}
|
||||
SOURCE_DIR ${CRYPTOPP_SOURCE_DIR}
|
||||
PATCH_COMMAND
|
||||
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 ${CMAKE_COMMAND} -E copy_directory "<SOURCE_DIR>/cmake/" "<SOURCE_DIR>/"
|
||||
INSTALL_DIR ${CRYPTOPP_INSTALL_DIR}
|
||||
CMAKE_ARGS ${CRYPTOPP_CMAKE_ARGS}
|
||||
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CRYPTOPP_INSTALL_DIR}
|
||||
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
|
||||
-DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
|
||||
)
|
||||
|
||||
ADD_LIBRARY(cryptopp STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET cryptopp PROPERTY IMPORTED_LOCATION ${CRYPTOPP_LIBRARIES})
|
||||
ADD_DEPENDENCIES(cryptopp extern_cryptopp)
|
@ -0,0 +1,3 @@
|
||||
cc_library(paddle_crypto SRCS cipher_utils.cc cipher.cc aes_cipher.cc DEPS cryptopp enforce)
|
||||
cc_test(aes_cipher_test SRCS aes_cipher_test.cc DEPS paddle_crypto)
|
||||
cc_test(cipher_utils_test SRCS cipher_utils_test.cc DEPS paddle_crypto)
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,100 @@
|
||||
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "paddle/fluid/framework/io/crypto/cipher.h"
|
||||
|
||||
namespace CryptoPP {
|
||||
|
||||
class StreamTransformationFilter;
|
||||
class SymmetricCipher;
|
||||
class AuthenticatedSymmetricCipher;
|
||||
class AuthenticatedDecryptionFilter;
|
||||
class AuthenticatedEncryptionFilter;
|
||||
template <class CryptoppCipher>
|
||||
class member_ptr;
|
||||
|
||||
} // namespace CryptoPP
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
class AESCipher : public Cipher {
|
||||
public:
|
||||
AESCipher() = default;
|
||||
~AESCipher() {}
|
||||
|
||||
std::string Encrypt(const std::string& input,
|
||||
const std::string& key) override;
|
||||
std::string Decrypt(const std::string& input,
|
||||
const std::string& key) override;
|
||||
|
||||
void EncryptToFile(const std::string& input, const std::string& key,
|
||||
const std::string& filename) override;
|
||||
std::string DecryptFromFile(const std::string& key,
|
||||
const std::string& filename) override;
|
||||
|
||||
void Init(const std::string& cipher_name, const int& iv_size,
|
||||
const int& tag_size);
|
||||
|
||||
private:
|
||||
std::string EncryptInternal(const std::string& plaintext,
|
||||
const std::string& key);
|
||||
std::string DecryptInternal(const std::string& ciphertext,
|
||||
const std::string& key);
|
||||
|
||||
std::string AuthenticatedEncryptInternal(const std::string& plaintext,
|
||||
const std::string& key);
|
||||
std::string AuthenticatedDecryptInternal(const std::string& ciphertext,
|
||||
const std::string& key);
|
||||
|
||||
void BuildCipher(
|
||||
bool for_encrypt, bool* need_iv,
|
||||
CryptoPP::member_ptr<CryptoPP::SymmetricCipher>* m_cipher,
|
||||
CryptoPP::member_ptr<CryptoPP::StreamTransformationFilter>* m_filter);
|
||||
|
||||
void BuildAuthEncCipher(
|
||||
bool* need_iv,
|
||||
CryptoPP::member_ptr<CryptoPP::AuthenticatedSymmetricCipher>* m_cipher,
|
||||
CryptoPP::member_ptr<CryptoPP::AuthenticatedEncryptionFilter>* m_filter);
|
||||
|
||||
void BuildAuthDecCipher(
|
||||
bool* need_iv,
|
||||
CryptoPP::member_ptr<CryptoPP::AuthenticatedSymmetricCipher>* m_cipher,
|
||||
CryptoPP::member_ptr<CryptoPP::AuthenticatedDecryptionFilter>* m_filter);
|
||||
|
||||
std::string aes_cipher_name_;
|
||||
int iv_size_;
|
||||
int tag_size_;
|
||||
std::string iv_;
|
||||
bool is_authenticated_cipher_{false};
|
||||
};
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,117 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/fluid/framework/io/crypto/aes_cipher.h"
|
||||
|
||||
#include <cryptopp/cryptlib.h>
|
||||
#include <glog/logging.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "paddle/fluid/framework/io/crypto/cipher.h"
|
||||
#include "paddle/fluid/framework/io/crypto/cipher_utils.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
class AESTest : public ::testing::Test {
|
||||
public:
|
||||
std::string key;
|
||||
|
||||
void SetUp() override { key = CipherUtils::GenKey(256); }
|
||||
static void GenConfigFile(const std::string& cipher_name);
|
||||
};
|
||||
|
||||
void AESTest::GenConfigFile(const std::string& cipher_name) {
|
||||
std::ofstream fout("aes_test.conf");
|
||||
fout << "cipher_name : " << cipher_name << std::endl;
|
||||
fout.close();
|
||||
}
|
||||
|
||||
TEST_F(AESTest, security_string) {
|
||||
std::vector<std::string> name_list(
|
||||
{"AES_CTR_NoPadding", "AES_CBC_PKCSPadding", "AES_ECB_PKCSPadding",
|
||||
"AES_GCM_NoPadding"});
|
||||
const std::string plaintext("hello world.");
|
||||
bool is_throw = false;
|
||||
for (auto& i : name_list) {
|
||||
AESTest::GenConfigFile(i);
|
||||
try {
|
||||
auto cipher = CipherFactory::CreateCipher("aes_test.conf");
|
||||
std::string ciphertext = cipher->Encrypt(plaintext, AESTest::key);
|
||||
|
||||
std::string plaintext1 = cipher->Decrypt(ciphertext, AESTest::key);
|
||||
EXPECT_EQ(plaintext, plaintext1);
|
||||
} catch (CryptoPP::Exception& e) {
|
||||
is_throw = true;
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
EXPECT_FALSE(is_throw);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(AESTest, security_vector) {
|
||||
std::vector<std::string> name_list(
|
||||
{"AES_CTR_NoPadding", "AES_CBC_PKCSPadding", "AES_ECB_PKCSPadding",
|
||||
"AES_GCM_NoPadding"});
|
||||
std::vector<int> input{1, 2, 3, 4};
|
||||
bool is_throw = false;
|
||||
for (auto& i : name_list) {
|
||||
AESTest::GenConfigFile(i);
|
||||
try {
|
||||
auto cipher = CipherFactory::CreateCipher("aes_test.conf");
|
||||
for (auto& i : input) {
|
||||
std::string ciphertext =
|
||||
cipher->Encrypt(std::to_string(i), AESTest::key);
|
||||
|
||||
std::string plaintext = cipher->Decrypt(ciphertext, AESTest::key);
|
||||
|
||||
int output = std::stoi(plaintext);
|
||||
|
||||
EXPECT_EQ(i, output);
|
||||
}
|
||||
} catch (CryptoPP::Exception& e) {
|
||||
is_throw = true;
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
EXPECT_FALSE(is_throw);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(AESTest, encrypt_to_file) {
|
||||
std::vector<std::string> name_list(
|
||||
{"AES_CTR_NoPadding", "AES_CBC_PKCSPadding", "AES_ECB_PKCSPadding",
|
||||
"AES_GCM_NoPadding"});
|
||||
const std::string plaintext("hello world.");
|
||||
std::string filename("aes_test.ciphertext");
|
||||
bool is_throw = false;
|
||||
for (auto& i : name_list) {
|
||||
AESTest::GenConfigFile(i);
|
||||
try {
|
||||
auto cipher = CipherFactory::CreateCipher("aes_test.conf");
|
||||
cipher->EncryptToFile(plaintext, AESTest::key, filename);
|
||||
std::string plaintext1 = cipher->DecryptFromFile(AESTest::key, filename);
|
||||
EXPECT_EQ(plaintext, plaintext1);
|
||||
} catch (CryptoPP::Exception& e) {
|
||||
is_throw = true;
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
EXPECT_FALSE(is_throw);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,60 @@
|
||||
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/framework/io/crypto/cipher.h"
|
||||
#include "paddle/fluid/framework/io/crypto/aes_cipher.h"
|
||||
#include "paddle/fluid/framework/io/crypto/cipher_utils.h"
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
std::shared_ptr<Cipher> CipherFactory::CreateCipher(
|
||||
const std::string& config_file) {
|
||||
std::string cipher_name;
|
||||
int iv_size;
|
||||
int tag_size;
|
||||
std::unordered_map<std::string, std::string> config;
|
||||
if (!config_file.empty()) {
|
||||
config = CipherUtils::LoadConfig(config_file);
|
||||
CipherUtils::GetValue<std::string>(config, "cipher_name", &cipher_name);
|
||||
} else {
|
||||
// set default cipher name
|
||||
cipher_name = "AES_CTR_NoPadding";
|
||||
}
|
||||
if (cipher_name.find("AES") != cipher_name.npos) {
|
||||
auto ret = std::make_shared<AESCipher>();
|
||||
// if not set iv_size, set default value
|
||||
if (config_file.empty() ||
|
||||
!CipherUtils::GetValue<int>(config, "iv_size", &iv_size)) {
|
||||
iv_size = CipherUtils::AES_DEFAULT_IV_SIZE;
|
||||
}
|
||||
// if not set tag_size, set default value
|
||||
if (config_file.empty() ||
|
||||
!CipherUtils::GetValue<int>(config, "tag_size", &tag_size)) {
|
||||
tag_size = CipherUtils::AES_DEFAULT_IV_SIZE;
|
||||
}
|
||||
ret->Init(cipher_name, iv_size, tag_size);
|
||||
return ret;
|
||||
} else {
|
||||
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
|
||||
"Invalid cipher name is specied. "
|
||||
"Please check you have specified valid cipher"
|
||||
" name in CryptoProperties."));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,51 @@
|
||||
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
class Cipher {
|
||||
public:
|
||||
Cipher() = default;
|
||||
virtual ~Cipher() {}
|
||||
// encrypt string
|
||||
virtual std::string Encrypt(const std::string& plaintext,
|
||||
const std::string& key) = 0;
|
||||
// decrypt string
|
||||
virtual std::string Decrypt(const std::string& ciphertext,
|
||||
const std::string& key) = 0;
|
||||
|
||||
// encrypt strings and read them to file,
|
||||
virtual void EncryptToFile(const std::string& plaintext,
|
||||
const std::string& key,
|
||||
const std::string& filename) = 0;
|
||||
// read from file and decrypt them
|
||||
virtual std::string DecryptFromFile(const std::string& key,
|
||||
const std::string& filename) = 0;
|
||||
};
|
||||
|
||||
class CipherFactory {
|
||||
public:
|
||||
CipherFactory() = default;
|
||||
static std::shared_ptr<Cipher> CreateCipher(const std::string& config_file);
|
||||
};
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,117 @@
|
||||
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/framework/io/crypto/cipher_utils.h"
|
||||
|
||||
#include <cryptopp/osrng.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
std::string CipherUtils::GenKey(int length) {
|
||||
CryptoPP::AutoSeededRandomPool prng;
|
||||
int bit_length = length / 8;
|
||||
std::string rng;
|
||||
rng.resize(bit_length);
|
||||
// CryptoPP::byte key[length];
|
||||
prng.GenerateBlock(reinterpret_cast<unsigned char*>(&(rng.at(0))),
|
||||
rng.size());
|
||||
return rng;
|
||||
}
|
||||
|
||||
std::string CipherUtils::GenKeyToFile(int length, const std::string& filename) {
|
||||
CryptoPP::AutoSeededRandomPool prng;
|
||||
std::string rng;
|
||||
int bit_length = length / 8;
|
||||
rng.resize(bit_length);
|
||||
// CryptoPP::byte key[length];
|
||||
prng.GenerateBlock(reinterpret_cast<unsigned char*>(&(rng.at(0))),
|
||||
rng.size());
|
||||
std::ofstream fout(filename);
|
||||
PADDLE_ENFORCE_EQ(fout.is_open(), true,
|
||||
paddle::platform::errors::Unavailable(
|
||||
"Failed to open file : %s, "
|
||||
"make sure input filename is available.",
|
||||
filename));
|
||||
fout.write(rng.c_str(), rng.size());
|
||||
fout.close();
|
||||
return rng;
|
||||
}
|
||||
|
||||
std::string CipherUtils::ReadKeyFromFile(const std::string& filename) {
|
||||
std::ifstream fin(filename, std::ios::binary);
|
||||
std::string ret{std::istreambuf_iterator<char>(fin),
|
||||
std::istreambuf_iterator<char>()};
|
||||
fin.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, std::string> CipherUtils::LoadConfig(
|
||||
const std::string& config_file) {
|
||||
std::ifstream fin(config_file);
|
||||
PADDLE_ENFORCE_EQ(fin.is_open(), true,
|
||||
paddle::platform::errors::Unavailable(
|
||||
"Failed to open file : %s, "
|
||||
"make sure input filename is available.",
|
||||
config_file));
|
||||
std::unordered_map<std::string, std::string> ret;
|
||||
char c;
|
||||
std::string line;
|
||||
std::istringstream iss;
|
||||
while (std::getline(fin, line)) {
|
||||
if (line.at(0) == '#') {
|
||||
continue;
|
||||
}
|
||||
iss.clear();
|
||||
iss.str(line);
|
||||
std::string key;
|
||||
std::string value;
|
||||
if (!(iss >> key >> c >> value) && (c == ':')) {
|
||||
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
|
||||
"Parse config file error, "
|
||||
"check the format of configure in file %s.",
|
||||
config_file));
|
||||
}
|
||||
ret.insert({key, value});
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <>
|
||||
bool CipherUtils::GetValue<bool>(
|
||||
const std::unordered_map<std::string, std::string>& config,
|
||||
const std::string& key, bool* output) {
|
||||
auto itr = config.find(key);
|
||||
if (itr == config.end()) {
|
||||
return false;
|
||||
}
|
||||
std::istringstream iss(itr->second);
|
||||
*output = false;
|
||||
iss >> *output;
|
||||
if (iss.fail()) {
|
||||
iss.clear();
|
||||
iss >> std::boolalpha >> *output;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const int CipherUtils::AES_DEFAULT_IV_SIZE = 128;
|
||||
const int CipherUtils::AES_DEFAULT_TAG_SIZE = 128;
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,63 @@
|
||||
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
class CipherUtils {
|
||||
public:
|
||||
CipherUtils() = default;
|
||||
static std::string GenKey(int length);
|
||||
static std::string GenKeyToFile(int length, const std::string& filename);
|
||||
static std::string ReadKeyFromFile(const std::string& filename);
|
||||
|
||||
static std::unordered_map<std::string, std::string> LoadConfig(
|
||||
const std::string& config_file);
|
||||
|
||||
template <typename val_type>
|
||||
static bool GetValue(
|
||||
const std::unordered_map<std::string, std::string>& config,
|
||||
const std::string& key, val_type* output);
|
||||
|
||||
static const int AES_DEFAULT_IV_SIZE;
|
||||
static const int AES_DEFAULT_TAG_SIZE;
|
||||
};
|
||||
|
||||
template <>
|
||||
bool CipherUtils::GetValue<bool>(
|
||||
const std::unordered_map<std::string, std::string>& config,
|
||||
const std::string& key, bool* output);
|
||||
|
||||
template <typename val_type>
|
||||
bool CipherUtils::GetValue(
|
||||
const std::unordered_map<std::string, std::string>& config,
|
||||
const std::string& key, val_type* output) {
|
||||
auto itr = config.find(key);
|
||||
if (itr == config.end()) {
|
||||
return false;
|
||||
}
|
||||
std::istringstream iss(itr->second);
|
||||
iss >> *output;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,77 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "paddle/fluid/framework/io/crypto/cipher_utils.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
TEST(CipherUtils, load_config) {
|
||||
std::string filename("cryptotest_config_file.conf");
|
||||
|
||||
std::ofstream fout(filename, std::ios::out);
|
||||
fout << "# anotation test line:"
|
||||
" must have two space along ':'."
|
||||
<< std::endl;
|
||||
std::vector<std::string> key_value;
|
||||
key_value.emplace_back("key_str : ciphername");
|
||||
key_value.emplace_back("key_int : 1");
|
||||
key_value.emplace_back("key_bool : true");
|
||||
key_value.emplace_back("key_bool1 : false");
|
||||
key_value.emplace_back("key_bool2 : 0");
|
||||
for (auto& i : key_value) {
|
||||
fout << i << std::endl;
|
||||
}
|
||||
fout.close();
|
||||
|
||||
auto config = CipherUtils::LoadConfig(filename);
|
||||
|
||||
std::string out_str;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<std::string>(config, "key_str", &out_str));
|
||||
EXPECT_EQ(out_str, std::string("ciphername"));
|
||||
|
||||
int out_int;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<int>(config, "key_int", &out_int));
|
||||
EXPECT_EQ(out_int, 1);
|
||||
|
||||
bool out_bool;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<bool>(config, "key_bool", &out_bool));
|
||||
EXPECT_EQ(out_bool, true);
|
||||
|
||||
bool out_bool1;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<bool>(config, "key_bool1", &out_bool1));
|
||||
EXPECT_EQ(out_bool1, false);
|
||||
|
||||
bool out_bool2;
|
||||
EXPECT_TRUE(CipherUtils::GetValue<bool>(config, "key_bool2", &out_bool2));
|
||||
EXPECT_EQ(out_bool2, false);
|
||||
}
|
||||
|
||||
TEST(CipherUtils, gen_key) {
|
||||
std::string filename("test_keyfile");
|
||||
std::string key = CipherUtils::GenKey(256);
|
||||
std::string key1 = CipherUtils::GenKeyToFile(256, filename);
|
||||
EXPECT_NE(key, key1);
|
||||
std::string key2 = CipherUtils::ReadKeyFromFile(filename);
|
||||
EXPECT_EQ(key1, key2);
|
||||
EXPECT_EQ(static_cast<int>(key.size()), 32);
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
Loading…
Reference in new issue