Skip to content

Commit b99edf6

Browse files
committed
encrypt
1 parent 3e9fccb commit b99edf6

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

tensorflow/cc/saved_model/reader.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717

1818
#include <unordered_set>
1919

20+
#include <iomanip>
21+
#include <sstream>
22+
2023
#include "absl/memory/memory.h"
2124
#include "tensorflow/cc/saved_model/constants.h"
2225
#include "tensorflow/core/lib/io/path.h"

tensorflow/core/platform/default/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ cc_library(
126126
"//third_party/eigen3",
127127
"@com_google_absl//absl/time",
128128
"@com_google_absl//absl/types:optional",
129+
"@cryptopp",
129130
],
130131
)
131132

tensorflow/core/platform/env.cc

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,36 @@ limitations under the License.
4848
#include <unistd.h>
4949
#endif
5050

51+
#include "aes.h"
52+
#include "modes.h"
53+
#include "cryptlib.h"
54+
#include "filters.h"
55+
#include "secblock.h"
56+
5157
namespace tensorflow {
5258

59+
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = {0x8b, 0x8b, 0x8b, 0x8b, 0x8b, 0x8b,
60+
0x8b, 0x8b, 0x8b, 0x8b, 0x8b, 0x8b,
61+
0x8b, 0x8b, 0x8b, 0x8b};
62+
byte iv[CryptoPP::AES::BLOCKSIZE] = {0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
63+
0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37};
64+
65+
Status decryptCBC(std::string cipher, byte key[], int keySize, byte iv[], std::string& recovered) {
66+
// Decryption
67+
try {
68+
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption d;
69+
d.SetKeyWithIV(key, keySize, iv);
70+
// The StreamTransformationFilter removes
71+
// padding as required.
72+
CryptoPP::StringSource s(cipher, true,
73+
new CryptoPP::StreamTransformationFilter(
74+
d, new CryptoPP::StringSink(recovered)));
75+
} catch (const CryptoPP::Exception& e) {
76+
return errors::DataLoss("Can't decrypt binary proto");
77+
}
78+
return Status::OK();
79+
}
80+
5381
// 128KB copy buffer
5482
constexpr size_t kCopyFileBufferSize = 128 * 1024;
5583

@@ -541,14 +569,23 @@ Status WriteBinaryProto(Env* env, const string& fname,
541569

542570
Status ReadBinaryProto(Env* env, const string& fname,
543571
protobuf::MessageLite* proto) {
544-
std::unique_ptr<RandomAccessFile> file;
545-
TF_RETURN_IF_ERROR(env->NewRandomAccessFile(fname, &file));
546-
std::unique_ptr<FileStream> stream(new FileStream(file.get()));
572+
// std::unique_ptr<RandomAccessFile> file;
573+
// TF_RETURN_IF_ERROR(env->NewRandomAccessFile(fname, &file));
574+
// std::unique_ptr<FileStream> stream(new FileStream(file.get()));
575+
576+
using google::protobuf::io::ArrayInputStream;
577+
string fileData;
578+
TF_RETURN_IF_ERROR(ReadFileToString(env, fname, &fileData));
579+
string plain;
580+
TF_RETURN_IF_ERROR(decryptCBC(fileData, key, sizeof(key), iv, plain));
581+
std::unique_ptr<ArrayInputStream> stream(
582+
new ArrayInputStream(plain.data(), plain.length()));
583+
547584
protobuf::io::CodedInputStream coded_stream(stream.get());
548585

549586
if (!proto->ParseFromCodedStream(&coded_stream) ||
550587
!coded_stream.ConsumedEntireMessage()) {
551-
TF_RETURN_IF_ERROR(stream->status());
588+
// TF_RETURN_IF_ERROR(stream->status());
552589
return errors::DataLoss("Can't parse ", fname, " as binary proto");
553590
}
554591
return Status::OK();

tensorflow/core/platform/windows/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ cc_library(
7373
"//third_party/eigen3",
7474
"@com_google_absl//absl/time",
7575
"@com_google_absl//absl/types:optional",
76+
"@cryptopp",
7677
],
7778
)
7879

tensorflow/workspace.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ load("//third_party/toolchains/cpus/arm:arm_compiler_configure.bzl", "arm_compil
1313
load("//third_party/toolchains/embedded/arm-linux:arm_linux_toolchain_configure.bzl", "arm_linux_toolchain_configure")
1414
load("//third_party:repo.bzl", "tf_http_archive")
1515
load("//third_party/clang_toolchain:cc_configure_clang.bzl", "cc_download_clang_toolchain")
16-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
16+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file", "http_archive")
1717
load("@bazel_tools//tools/build_defs/repo:java.bzl", "java_import_external")
1818
load("@io_bazel_rules_closure//closure:defs.bzl", "filegroup_external")
1919
load(
@@ -1126,6 +1126,19 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""):
11261126
"https://github.com/GrahamDumpleton/wrapt/archive/1.11.1.tar.gz",
11271127
],
11281128
)
1129+
1130+
# add cryptopp
1131+
tf_http_archive(
1132+
name = "cryptopp",
1133+
build_file = clean_dep("//third_party/cryptopp:BUILD"),
1134+
sha256 = "c934d2c427a0ef197ea989a00f7b6d866d110dd55257d2944d0513b382b7e2b4",
1135+
strip_prefix = "cryptopp-CRYPTOPP_5_6_5",
1136+
urls = [
1137+
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/weidai11/cryptopp/archive/CRYPTOPP_5_6_5.zip",
1138+
"https://github.com/weidai11/cryptopp/archive/CRYPTOPP_5_6_5.zip",
1139+
],
1140+
)
1141+
11291142
tf_http_archive(
11301143
name = "coremltools",
11311144
sha256 = "0d594a714e8a5fd5bd740ad112ef59155c0482e25fdc8f8efa5758f90abdcf1e",

third_party/cryptopp/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cc_library(
2+
name = "cryptopp",
3+
srcs = glob(["*.cpp"]),
4+
hdrs = glob(["*.h"]) + [
5+
"algebra.cpp",
6+
"eprecomp.cpp",
7+
"eccrypto.cpp",
8+
"strciphr.cpp",
9+
"polynomi.cpp",
10+
],
11+
textual_hdrs = [
12+
"algebra.cpp",
13+
"eprecomp.cpp",
14+
"eccrypto.cpp",
15+
"strciphr.cpp",
16+
"polynomi.cpp",
17+
],
18+
visibility = ["//visibility:public"],
19+
)

0 commit comments

Comments
 (0)