Skip to content

Commit c55f221

Browse files
MRXLTYanghello
andauthored
[cherry pick]support encryption model (#25811)
* fix conflict * fix conflict * fix code cherry pick encryption api Co-authored-by: Yanghello <[email protected]> Co-authored-by: Yanghello <[email protected]>
1 parent c7c4752 commit c55f221

16 files changed

+977
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ option(SANITIZER_TYPE "Choose the type of sanitizer, options are: Address, Leak,
8989
option(WITH_LITE "Compile Paddle Fluid with Lite Engine" OFF)
9090
option(WITH_NCCL "Compile PaddlePaddle with NCCL support" ON)
9191
option(WITH_ARM "Compile PaddlePaddle with arm support" OFF)
92+
option(WITH_CRYPTO "Compile PaddlePaddle with crypto support" ON)
9293

9394
# PY_VERSION
9495
if(NOT PY_VERSION)

cmake/external/cryptopp.cmake

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
INCLUDE(ExternalProject)
16+
17+
SET(CRYPTOPP_PREFIX_DIR ${THIRD_PARTY_PATH}/cryptopp)
18+
SET(CRYPTOPP_INSTALL_DIR ${THIRD_PARTY_PATH}/install/cryptopp)
19+
SET(CRYPTOPP_INCLUDE_DIR "${CRYPTOPP_INSTALL_DIR}/include" CACHE PATH "cryptopp include directory." FORCE)
20+
SET(CRYPTOPP_REPOSITORY https://github.com/weidai11/cryptopp.git)
21+
SET(CRYPTOPP_TAG CRYPTOPP_8_2_0)
22+
23+
IF(WIN32)
24+
SET(CRYPTOPP_LIBRARIES "${CRYPTOPP_INSTALL_DIR}/lib/cryptopp-static.lib" CACHE FILEPATH "cryptopp library." FORCE)
25+
SET(CRYPTOPP_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
26+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
27+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
28+
set(CompilerFlags
29+
CMAKE_CXX_FLAGS
30+
CMAKE_CXX_FLAGS_DEBUG
31+
CMAKE_CXX_FLAGS_RELEASE
32+
CMAKE_C_FLAGS
33+
CMAKE_C_FLAGS_DEBUG
34+
CMAKE_C_FLAGS_RELEASE
35+
)
36+
foreach(CompilerFlag ${CompilerFlags})
37+
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
38+
endforeach()
39+
ELSE(WIN32)
40+
SET(CRYPTOPP_LIBRARIES "${CRYPTOPP_INSTALL_DIR}/lib/libcryptopp.a" CACHE FILEPATH "cryptopp library." FORCE)
41+
SET(CRYPTOPP_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
42+
ENDIF(WIN32)
43+
44+
set(CRYPTOPP_CMAKE_ARGS ${COMMON_CMAKE_ARGS}
45+
-DBUILD_SHARED=ON
46+
-DBUILD_STATIC=ON
47+
-DBUILD_TESTING=OFF
48+
-DCMAKE_INSTALL_LIBDIR=${CRYPTOPP_INSTALL_DIR}/lib
49+
-DCMAKE_INSTALL_PREFIX=${CRYPTOPP_INSTALL_DIR}
50+
-DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
51+
-DCMAKE_CXX_FLAGS=${CRYPTOPP_CMAKE_CXX_FLAGS}
52+
-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
53+
)
54+
55+
INCLUDE_DIRECTORIES(${CRYPTOPP_INCLUDE_DIR})
56+
57+
cache_third_party(extern_cryptopp
58+
REPOSITORY ${CRYPTOPP_REPOSITORY}
59+
TAG ${CRYPTOPP_TAG}
60+
DIR CRYPTOPP_SOURCE_DIR)
61+
62+
ExternalProject_Add(
63+
extern_cryptopp
64+
${EXTERNAL_PROJECT_LOG_ARGS}
65+
${SHALLOW_CLONE}
66+
"${CRYPTOPP_DOWNLOAD_CMD}"
67+
PREFIX ${CRYPTOPP_PREFIX_DIR}
68+
SOURCE_DIR ${CRYPTOPP_SOURCE_DIR}
69+
PATCH_COMMAND
70+
COMMAND ${CMAKE_COMMAND} -E remove_directory "<SOURCE_DIR>/cmake/"
71+
COMMAND git clone https://github.com/noloader/cryptopp-cmake "<SOURCE_DIR>/cmake"
72+
COMMAND cd "<SOURCE_DIR>/cmake" && git checkout tags/${CRYPTOPP_TAG} -b ${CRYPTOPP_TAG}
73+
COMMAND ${CMAKE_COMMAND} -E copy_directory "<SOURCE_DIR>/cmake/" "<SOURCE_DIR>/"
74+
INSTALL_DIR ${CRYPTOPP_INSTALL_DIR}
75+
CMAKE_ARGS ${CRYPTOPP_CMAKE_ARGS}
76+
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CRYPTOPP_INSTALL_DIR}
77+
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
78+
-DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
79+
)
80+
81+
ADD_LIBRARY(cryptopp STATIC IMPORTED GLOBAL)
82+
SET_PROPERTY(TARGET cryptopp PROPERTY IMPORTED_LOCATION ${CRYPTOPP_LIBRARIES})
83+
ADD_DEPENDENCIES(cryptopp extern_cryptopp)

cmake/generic.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989

9090
# including binary directory for generated headers.
9191
include_directories(${CMAKE_CURRENT_BINARY_DIR})
92+
# including io directory for inference lib paddle_api.h
93+
include_directories("${PADDLE_SOURCE_DIR}/paddle/fluid/framework/io")
9294

9395
if(NOT APPLE)
9496
find_package(Threads REQUIRED)

cmake/inference_lib.cmake

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ function(copy_part_of_thrid_party TARGET DST)
105105
SRCS ${GLOG_INCLUDE_DIR} ${GLOG_LIBRARIES}
106106
DSTS ${dst_dir} ${dst_dir}/lib)
107107

108+
set(dst_dir "${DST}/third_party/install/cryptopp")
109+
copy(${TARGET}
110+
SRCS ${CRYPTOPP_INCLUDE_DIR} ${CRYPTOPP_LIBRARIES}
111+
DSTS ${dst_dir} ${dst_dir}/lib)
112+
108113
set(dst_dir "${DST}/third_party/install/xxhash")
109114
copy(${TARGET}
110115
SRCS ${XXHASH_INCLUDE_DIR} ${XXHASH_LIBRARIES}
@@ -161,7 +166,10 @@ copy(inference_lib_dist
161166
copy(inference_lib_dist
162167
SRCS ${CMAKE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h
163168
DSTS ${FLUID_INFERENCE_INSTALL_DIR}/paddle/include/internal)
164-
169+
copy(inference_lib_dist
170+
SRCS ${CMAKE_BINARY_DIR}/../paddle/fluid/framework/io/crypto/cipher.h
171+
DSTS ${FLUID_INFERENCE_INSTALL_DIR}/paddle/include/crypto/)
172+
include_directories(${CMAKE_BINARY_DIR}/../paddle/fluid/framework/io)
165173
# CAPI inference library for only inference
166174
set(FLUID_INFERENCE_C_INSTALL_DIR "${CMAKE_BINARY_DIR}/fluid_inference_c_install_dir" CACHE STRING
167175
"A path setting CAPI fluid inference shared")

cmake/third_party.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,8 @@ if (WITH_LITE)
305305
include(external/lite)
306306
endif (WITH_LITE)
307307

308+
if (WITH_CRYPTO)
309+
include(external/cryptopp) # download, build, install cryptopp
310+
endif (WITH_CRYPTO)
311+
308312
add_custom_target(third_party ALL DEPENDS ${third_party_deps})

paddle/fluid/framework/io/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ cc_library(fs SRCS fs.cc DEPS string_helper glog boost)
22
cc_library(shell SRCS shell.cc DEPS string_helper glog timer enforce)
33

44
cc_test(test_fs SRCS test_fs.cc DEPS fs shell)
5+
if (WITH_CRYPTO)
6+
add_subdirectory(crypto)
7+
endif (WITH_CRYPTO)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cc_library(paddle_crypto SRCS cipher_utils.cc cipher.cc aes_cipher.cc DEPS cryptopp enforce)
2+
cc_test(aes_cipher_test SRCS aes_cipher_test.cc DEPS paddle_crypto)
3+
cc_test(cipher_utils_test SRCS cipher_utils_test.cc DEPS paddle_crypto)

0 commit comments

Comments
 (0)