1diff --git a/cmake/package_lite.cmake b/cmake/package_lite.cmake 2index f15724f1e5..4175c44c02 100644 3--- a/cmake/package_lite.cmake 4+++ b/cmake/package_lite.cmake 5@@ -458,10 +458,6 @@ if(PLATFORM_ARM64) 6 DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME}) 7 endif() 8 endif() 9- if(MSLITE_ENABLE_MODEL_OBF) 10- install(FILES ${TOP_DIR}/mindspore/lite/tools/obfuscator/lib/android-aarch64/libmsdeobfuscator-lite.so 11- DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME}) 12- endif() 13 install(FILES ${TOP_DIR}/mindspore/core/ir/dtype/type_id.h DESTINATION ${RUNTIME_INC_DIR}/ir/dtype 14 COMPONENT ${RUNTIME_COMPONENT_NAME}) 15 install(FILES 16@@ -705,10 +701,6 @@ elseif(PLATFORM_ARM32) 17 DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME}) 18 endif() 19 endif() 20- if(MSLITE_ENABLE_MODEL_OBF) 21- install(FILES ${TOP_DIR}/mindspore/lite/tools/obfuscator/lib/android-aarch32/libmsdeobfuscator-lite.so 22- DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME}) 23- endif() 24 install(FILES ${TOP_DIR}/mindspore/core/ir/dtype/type_id.h DESTINATION ${RUNTIME_INC_DIR}/ir/dtype 25 COMPONENT ${RUNTIME_COMPONENT_NAME}) 26 install(FILES 27@@ -906,13 +898,6 @@ else() 28 DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME}) 29 endif() 30 endif() 31- if(MSLITE_ENABLE_MODEL_OBF) 32- install(FILES ${TOP_DIR}/mindspore/lite/tools/obfuscator/bin/linux-x64/msobfuscator 33- DESTINATION ${OBFUSCATOR_ROOT_DIR} PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ 34- GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE COMPONENT ${RUNTIME_COMPONENT_NAME}) 35- install(FILES ${TOP_DIR}/mindspore/lite/tools/obfuscator/lib/linux-x64/libmsdeobfuscator-lite.so 36- DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME}) 37- endif() 38 if(MSLITE_ENABLE_RUNTIME_GLOG) 39 install(FILES ${glog_LIBPATH}/libmindspore_glog.so.0.4.0 DESTINATION ${GLOG_DIR} RENAME libmindspore_glog.so.0 40 COMPONENT ${RUNTIME_COMPONENT_NAME}) 41diff --git a/mindspore/lite/include/model.h b/mindspore/lite/include/model.h 42index b96c7e35bf..629978f1bc 100644 43--- a/mindspore/lite/include/model.h 44+++ b/mindspore/lite/include/model.h 45@@ -57,12 +57,6 @@ struct MS_API LiteGraph { 46 std::vector<mindspore::schema::Tensor *> all_tensors_; 47 std::vector<Node *> all_nodes_; 48 std::vector<SubGraph *> sub_graphs_; 49-#ifdef ENABLE_MODEL_OBF 50- std::vector<uint32_t> all_prims_type_; 51- std::vector<uint32_t> all_nodes_stat_; 52- bool model_obfuscated_ = false; 53- std::vector<unsigned char *> deobf_prims_; 54-#endif 55 56 std::string ToString() const; 57 }; 58@@ -72,6 +66,7 @@ struct MS_API Model { 59 char *buf = nullptr; 60 size_t buf_size_ = 0; 61 LiteModelType model_type_ = mindspore::lite::ModelType_MSLite; 62+ void *deobf = nullptr; 63 64 /// \brief Static method to create a Model pointer. 65 static Model *Import(const char *model_buf, size_t size); 66diff --git a/mindspore/lite/include/registry/deobf_processor.h b/mindspore/lite/include/registry/deobf_processor.h 67new file mode 100644 68index 0000000000..5987233fd7 69--- /dev/null 70+++ b/mindspore/lite/include/registry/deobf_processor.h 71@@ -0,0 +1,77 @@ 72+/** 73+ * Copyright 2021 Huawei Technologies Co., Ltd 74+ * 75+ * Licensed under the Apache License, Version 2.0 (the "License"); 76+ * you may not use this file except in compliance with the License. 77+ * You may obtain a copy of the License at 78+ * 79+ * http://www.apache.org/licenses/LICENSE-2.0 80+ * 81+ * Unless required by applicable law or agreed to in writing, software 82+ * distributed under the License is distributed on an "AS IS" BASIS, 83+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 84+ * See the License for the specific language governing permissions and 85+ * limitations under the License. 86+ */ 87+ 88+#ifndef MINDSPORE_LITE_INCLUDE_REGISTRY_DEOBFPROCESSOR_H_ 89+#define MINDSPORE_LITE_INCLUDE_REGISTRY_DEOBFPROCESSOR_H_ 90+ 91+#include <vector> 92+#include <string> 93+#include <numeric> 94+#include "src/common/prim_util.h" 95+#include "src/common/log.h" 96+#include "include/model.h" 97+#include "schema/inner/model_generated.h" 98+ 99+namespace mindspore::lite { 100+ 101+ enum DeObfRet : uint32_t { 102+ kDeObfFailed = 0, ///<Deobfuscator failed 103+ kNoObf = 1, ///<The node has not been obfuscated 104+ kDeObfSuccess = 2, ///<Deobfuscate success 105+ }; 106+ 107+ class DeObfProcessor { 108+ public: 109+ DeObfProcessor() = default; 110+ 111+ bool GetModelDeObf(const void *meta_graph, Model *model); 112+ 113+ void DeObfuscate(Model *model); 114+ 115+ DeObfRet CreateDeObfNode(const schema::Primitive *&src_prim, int i, int schema__version); 116+ 117+ std::vector<uint32_t> all_prims_type_; 118+ std::vector<uint32_t> all_nodes_stat_; 119+ bool model_obfuscated_ = false; 120+ void *model_deobf = nullptr; 121+ }; 122+ 123+ typedef void (*ObfCreateFunc)(Model &model); 124+ 125+ class MS_API DeObfRegister { 126+ public: 127+ static bool (DeObfProcessor::*GetModelDeObfReg)(const void *meta_graph, Model *model); 128+ static void (DeObfProcessor::*DeObfuscateReg)(Model *model); 129+ static DeObfRet (DeObfProcessor::*CreateDeObfNodeReg)(const schema::Primitive *&src_prim, int i, int schema__version); 130+ static void *deobf_handle; 131+ 132+ DeObfRegister() = default; 133+ ~DeObfRegister() = default; 134+ 135+ static ObfCreateFunc NewDeObfProcessor; 136+ 137+ static void Fail(Model &model){MS_LOG(INFO) << "DeObfuscator not registered!";} 138+ 139+ MS_API static void RegisterDeObfuscator(ObfCreateFunc func){ 140+ if(func == nullptr){ 141+ MS_LOG(WARNING) << "Register invalid deobfuscator"; 142+ return; 143+ } 144+ NewDeObfProcessor = func; 145+ } 146+ }; 147+} 148+#endif 149diff --git a/mindspore/lite/schema/inner/model_generated.h b/mindspore/lite/schema/inner/model_generated.h 150index c3bc5dbfb0..6f4bd14bd6 100644 151--- a/mindspore/lite/schema/inner/model_generated.h 152+++ b/mindspore/lite/schema/inner/model_generated.h 153@@ -2661,7 +2661,9 @@ struct MetaGraphT : public flatbuffers::NativeTable { 154 std::vector<std::unique_ptr<mindspore::schema::TensorT>> allTensors{}; 155 std::vector<std::unique_ptr<mindspore::schema::SubGraphT>> subGraph{}; 156 bool obfuscate = false; 157+ bool encrypt = false; 158 std::vector<uint8_t> obfMetaData{}; 159+ std::vector<uint8_t> decryptTable{}; 160 }; 161 162 struct MetaGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { 163@@ -2681,7 +2683,9 @@ struct MetaGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { 164 VT_ALLTENSORS = 18, 165 VT_SUBGRAPH = 20, 166 VT_OBFUSCATE = 22, 167- VT_OBFMETADATA = 24 168+ VT_ENCRYPT = 24, 169+ VT_OBFMETADATA = 26, 170+ VT_DECRYPTTABLE = 28 171 }; 172 const flatbuffers::String *name() const { 173 return GetPointer<const flatbuffers::String *>(VT_NAME); 174@@ -2743,12 +2747,24 @@ struct MetaGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { 175 bool mutate_obfuscate(bool _obfuscate) { 176 return SetField<uint8_t>(VT_OBFUSCATE, static_cast<uint8_t>(_obfuscate), 0); 177 } 178+ bool encrypt() const { 179+ return GetField<uint8_t>(VT_ENCRYPT, 0) != 0; 180+ } 181+ bool mutate_encrypt(bool _encrypt) { 182+ return SetField<uint8_t>(VT_ENCRYPT, static_cast<uint8_t>(_encrypt), 0); 183+ } 184 const flatbuffers::Vector<uint8_t> *obfMetaData() const { 185 return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_OBFMETADATA); 186 } 187 flatbuffers::Vector<uint8_t> *mutable_obfMetaData() { 188 return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_OBFMETADATA); 189 } 190+ const flatbuffers::Vector<uint8_t> *decryptTable() const { 191+ return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_DECRYPTTABLE); 192+ } 193+ flatbuffers::Vector<uint8_t> *mutable_decryptTable() { 194+ return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_DECRYPTTABLE); 195+ } 196 bool Verify(flatbuffers::Verifier &verifier) const { 197 return VerifyTableStart(verifier) && 198 VerifyOffset(verifier, VT_NAME) && 199@@ -2771,8 +2787,11 @@ struct MetaGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { 200 verifier.VerifyVector(subGraph()) && 201 verifier.VerifyVectorOfTables(subGraph()) && 202 VerifyField<uint8_t>(verifier, VT_OBFUSCATE) && 203+ VerifyField<uint8_t>(verifier, VT_ENCRYPT) && 204 VerifyOffset(verifier, VT_OBFMETADATA) && 205 verifier.VerifyVector(obfMetaData()) && 206+ VerifyOffset(verifier, VT_DECRYPTTABLE) && 207+ verifier.VerifyVector(decryptTable()) && 208 verifier.EndTable(); 209 } 210 MetaGraphT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; 211@@ -2814,9 +2833,15 @@ struct MetaGraphBuilder { 212 void add_obfuscate(bool obfuscate) { 213 fbb_.AddElement<uint8_t>(MetaGraph::VT_OBFUSCATE, static_cast<uint8_t>(obfuscate), 0); 214 } 215+ void add_encrypt(bool encrypt) { 216+ fbb_.AddElement<uint8_t>(MetaGraph::VT_OBFUSCATE, static_cast<uint8_t>(encrypt), 0); 217+ } 218 void add_obfMetaData(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> obfMetaData) { 219 fbb_.AddOffset(MetaGraph::VT_OBFMETADATA, obfMetaData); 220 } 221+ void add_decryptTable(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> decryptTable) { 222+ fbb_.AddOffset(MetaGraph::VT_DECRYPTTABLE, decryptTable); 223+ } 224 explicit MetaGraphBuilder(flatbuffers::FlatBufferBuilder &_fbb) 225 : fbb_(_fbb) { 226 start_ = fbb_.StartTable(); 227@@ -2840,8 +2865,11 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraph( 228 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<mindspore::schema::Tensor>>> allTensors = 0, 229 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<mindspore::schema::SubGraph>>> subGraph = 0, 230 bool obfuscate = false, 231- flatbuffers::Offset<flatbuffers::Vector<uint8_t>> obfMetaData = 0) { 232+ bool encrypt = false, 233+ flatbuffers::Offset<flatbuffers::Vector<uint8_t>> obfMetaData = 0, 234+ flatbuffers::Offset<flatbuffers::Vector<uint8_t>> decryptTable = 0) { 235 MetaGraphBuilder builder_(_fbb); 236+ builder_.add_decryptTable(decryptTable); 237 builder_.add_obfMetaData(obfMetaData); 238 builder_.add_subGraph(subGraph); 239 builder_.add_allTensors(allTensors); 240@@ -2853,6 +2881,7 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraph( 241 builder_.add_version(version); 242 builder_.add_name(name); 243 builder_.add_obfuscate(obfuscate); 244+ builder_.add_encrypt(encrypt); 245 return builder_.Finish(); 246 } 247 248@@ -2868,7 +2897,9 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraphDirect( 249 const std::vector<flatbuffers::Offset<mindspore::schema::Tensor>> *allTensors = nullptr, 250 const std::vector<flatbuffers::Offset<mindspore::schema::SubGraph>> *subGraph = nullptr, 251 bool obfuscate = false, 252- const std::vector<uint8_t> *obfMetaData = nullptr) { 253+ bool encrypt = false, 254+ const std::vector<uint8_t> *obfMetaData = nullptr, 255+ const std::vector<uint8_t> *decryptTable = nullptr) { 256 auto name__ = name ? _fbb.CreateString(name) : 0; 257 auto version__ = version ? _fbb.CreateString(version) : 0; 258 auto inputIndex__ = inputIndex ? _fbb.CreateVector<uint32_t>(*inputIndex) : 0; 259@@ -2877,6 +2908,7 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraphDirect( 260 auto allTensors__ = allTensors ? _fbb.CreateVector<flatbuffers::Offset<mindspore::schema::Tensor>>(*allTensors) : 0; 261 auto subGraph__ = subGraph ? _fbb.CreateVector<flatbuffers::Offset<mindspore::schema::SubGraph>>(*subGraph) : 0; 262 auto obfMetaData__ = obfMetaData ? _fbb.CreateVector<uint8_t>(*obfMetaData) : 0; 263+ auto decryptTable__ = decryptTable ? _fbb.CreateVector<uint8_t>(*decryptTable) : 0; 264 return mindspore::schema::CreateMetaGraph( 265 _fbb, 266 name__, 267@@ -2889,7 +2921,9 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraphDirect( 268 allTensors__, 269 subGraph__, 270 obfuscate, 271- obfMetaData__); 272+ encrypt, 273+ obfMetaData__, 274+ decryptTable__); 275 } 276 277 flatbuffers::Offset<MetaGraph> CreateMetaGraph(flatbuffers::FlatBufferBuilder &_fbb, const MetaGraphT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); 278@@ -3177,7 +3211,9 @@ inline void MetaGraph::UnPackTo(MetaGraphT *_o, const flatbuffers::resolver_func 279 { auto _e = allTensors(); if (_e) { _o->allTensors.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->allTensors[_i] = std::unique_ptr<mindspore::schema::TensorT>(_e->Get(_i)->UnPack(_resolver)); } } } 280 { auto _e = subGraph(); if (_e) { _o->subGraph.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->subGraph[_i] = std::unique_ptr<mindspore::schema::SubGraphT>(_e->Get(_i)->UnPack(_resolver)); } } } 281 { auto _e = obfuscate(); _o->obfuscate = _e; } 282+ { auto _e = encrypt(); _o->encrypt = _e; } 283 { auto _e = obfMetaData(); if (_e) { _o->obfMetaData.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->obfMetaData.begin()); } } 284+ { auto _e = decryptTable(); if (_e) { _o->decryptTable.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->decryptTable.begin()); } } 285 } 286 287 inline flatbuffers::Offset<MetaGraph> MetaGraph::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MetaGraphT* _o, const flatbuffers::rehasher_function_t *_rehasher) { 288@@ -3198,7 +3234,9 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraph(flatbuffers::FlatBufferBui 289 auto _allTensors = _o->allTensors.size() ? _fbb.CreateVector<flatbuffers::Offset<mindspore::schema::Tensor>> (_o->allTensors.size(), [](size_t i, _VectorArgs *__va) { return CreateTensor(*__va->__fbb, __va->__o->allTensors[i].get(), __va->__rehasher); }, &_va ) : 0; 290 auto _subGraph = _o->subGraph.size() ? _fbb.CreateVector<flatbuffers::Offset<mindspore::schema::SubGraph>> (_o->subGraph.size(), [](size_t i, _VectorArgs *__va) { return CreateSubGraph(*__va->__fbb, __va->__o->subGraph[i].get(), __va->__rehasher); }, &_va ) : 0; 291 auto _obfuscate = _o->obfuscate; 292+ auto _encrypt = _o->encrypt; 293 auto _obfMetaData = _o->obfMetaData.size() ? _fbb.CreateVector(_o->obfMetaData) : 0; 294+ auto _decryptTable = _o->decryptTable.size() ? _fbb.CreateVector(_o->decryptTable) : 0; 295 return mindspore::schema::CreateMetaGraph( 296 _fbb, 297 _name, 298@@ -3211,7 +3249,9 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraph(flatbuffers::FlatBufferBui 299 _allTensors, 300 _subGraph, 301 _obfuscate, 302- _obfMetaData); 303+ _encrypt, 304+ _obfMetaData, 305+ _decryptTable); 306 } 307 308 inline const flatbuffers::TypeTable *WeightQuantCompressTypeTypeTable() { 309@@ -3374,6 +3414,8 @@ inline const flatbuffers::TypeTable *MetaGraphTypeTable() { 310 { flatbuffers::ET_SEQUENCE, 1, 1 }, 311 { flatbuffers::ET_SEQUENCE, 1, 2 }, 312 { flatbuffers::ET_BOOL, 0, -1 }, 313+ { flatbuffers::ET_BOOL, 0, -1 }, 314+ { flatbuffers::ET_UCHAR, 1, -1 }, 315 { flatbuffers::ET_UCHAR, 1, -1 } 316 }; 317 static const flatbuffers::TypeFunction type_refs[] = { 318diff --git a/mindspore/lite/schema/model.fbs b/mindspore/lite/schema/model.fbs 319index 0b03db5bf1..7a221bdce9 100644 320--- a/mindspore/lite/schema/model.fbs 321+++ b/mindspore/lite/schema/model.fbs 322@@ -116,7 +116,9 @@ table MetaGraph { 323 allTensors: [Tensor]; // weight + input + output 324 subGraph : [SubGraph]; 325 obfuscate: bool = false; 326+ encrypt : bool = false; 327 obfMetaData: [ubyte]; 328+ decryptTable: [ubyte]; 329 } 330 331 root_type MetaGraph; 332diff --git a/mindspore/lite/schema/model_generated.h b/mindspore/lite/schema/model_generated.h 333index 7692acbe3e..7ef01e863e 100644 334--- a/mindspore/lite/schema/model_generated.h 335+++ b/mindspore/lite/schema/model_generated.h 336@@ -2399,7 +2399,9 @@ struct MetaGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { 337 VT_ALLTENSORS = 18, 338 VT_SUBGRAPH = 20, 339 VT_OBFUSCATE = 22, 340- VT_OBFMETADATA = 24 341+ VT_ENCRYPT = 24, 342+ VT_OBFMETADATA = 26, 343+ VT_DECRYPTTABLE = 28 344 }; 345 const flatbuffers::String *name() const { 346 return GetPointer<const flatbuffers::String *>(VT_NAME); 347@@ -2431,9 +2433,15 @@ struct MetaGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { 348 bool obfuscate() const { 349 return GetField<uint8_t>(VT_OBFUSCATE, 0) != 0; 350 } 351+ bool encrypt() const { 352+ return GetField<uint8_t>(VT_ENCRYPT, 0) != 0; 353+ } 354 const flatbuffers::Vector<uint8_t> *obfMetaData() const { 355 return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_OBFMETADATA); 356 } 357+ const flatbuffers::Vector<uint8_t> *decryptTable() const { 358+ return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_DECRYPTTABLE); 359+ } 360 bool Verify(flatbuffers::Verifier &verifier) const { 361 return VerifyTableStart(verifier) && 362 VerifyOffset(verifier, VT_NAME) && 363@@ -2456,8 +2464,11 @@ struct MetaGraph FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { 364 verifier.VerifyVector(subGraph()) && 365 verifier.VerifyVectorOfTables(subGraph()) && 366 VerifyField<uint8_t>(verifier, VT_OBFUSCATE) && 367+ VerifyField<uint8_t>(verifier, VT_ENCRYPT) && 368 VerifyOffset(verifier, VT_OBFMETADATA) && 369 verifier.VerifyVector(obfMetaData()) && 370+ VerifyOffset(verifier, VT_DECRYPTTABLE) && 371+ verifier.VerifyVector(decryptTable()) && 372 verifier.EndTable(); 373 } 374 }; 375@@ -2496,9 +2507,15 @@ struct MetaGraphBuilder { 376 void add_obfuscate(bool obfuscate) { 377 fbb_.AddElement<uint8_t>(MetaGraph::VT_OBFUSCATE, static_cast<uint8_t>(obfuscate), 0); 378 } 379+ void add_encrypt(bool encrypt) { 380+ fbb_.AddElement<uint8_t>(MetaGraph::VT_ENCRYPT, static_cast<uint8_t>(encrypt), 0); 381+ } 382 void add_obfMetaData(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> obfMetaData) { 383 fbb_.AddOffset(MetaGraph::VT_OBFMETADATA, obfMetaData); 384 } 385+ void add_decryptTable(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> decryptTable) { 386+ fbb_.AddOffset(MetaGraph::VT_DECRYPTTABLE, decryptTable); 387+ } 388 explicit MetaGraphBuilder(flatbuffers::FlatBufferBuilder &_fbb) 389 : fbb_(_fbb) { 390 start_ = fbb_.StartTable(); 391@@ -2522,8 +2539,11 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraph( 392 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<mindspore::schema::Tensor>>> allTensors = 0, 393 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<mindspore::schema::SubGraph>>> subGraph = 0, 394 bool obfuscate = false, 395- flatbuffers::Offset<flatbuffers::Vector<uint8_t>> obfMetaData = 0) { 396+ bool encrypt = false, 397+ flatbuffers::Offset<flatbuffers::Vector<uint8_t>> obfMetaData = 0, 398+ flatbuffers::Offset<flatbuffers::Vector<uint8_t>> decryptTable = 0) { 399 MetaGraphBuilder builder_(_fbb); 400+ builder_.add_decryptTable(decryptTable); 401 builder_.add_obfMetaData(obfMetaData); 402 builder_.add_subGraph(subGraph); 403 builder_.add_allTensors(allTensors); 404@@ -2534,6 +2554,7 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraph( 405 builder_.add_fmkType(fmkType); 406 builder_.add_version(version); 407 builder_.add_name(name); 408+ builder_.add_encrypt(encrypt); 409 builder_.add_obfuscate(obfuscate); 410 return builder_.Finish(); 411 } 412@@ -2550,7 +2571,9 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraphDirect( 413 const std::vector<flatbuffers::Offset<mindspore::schema::Tensor>> *allTensors = nullptr, 414 const std::vector<flatbuffers::Offset<mindspore::schema::SubGraph>> *subGraph = nullptr, 415 bool obfuscate = false, 416- const std::vector<uint8_t> *obfMetaData = nullptr) { 417+ bool encrypt = false, 418+ const std::vector<uint8_t> *obfMetaData = nullptr, 419+ const std::vector<uint8_t> *decryptTable = nullptr) { 420 auto name__ = name ? _fbb.CreateString(name) : 0; 421 auto version__ = version ? _fbb.CreateString(version) : 0; 422 auto inputIndex__ = inputIndex ? _fbb.CreateVector<uint32_t>(*inputIndex) : 0; 423@@ -2559,6 +2582,7 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraphDirect( 424 auto allTensors__ = allTensors ? _fbb.CreateVector<flatbuffers::Offset<mindspore::schema::Tensor>>(*allTensors) : 0; 425 auto subGraph__ = subGraph ? _fbb.CreateVector<flatbuffers::Offset<mindspore::schema::SubGraph>>(*subGraph) : 0; 426 auto obfMetaData__ = obfMetaData ? _fbb.CreateVector<uint8_t>(*obfMetaData) : 0; 427+ auto decryptTable__ = decryptTable ? _fbb.CreateVector<uint8_t>(*decryptTable) : 0; 428 return mindspore::schema::CreateMetaGraph( 429 _fbb, 430 name__, 431@@ -2571,7 +2595,9 @@ inline flatbuffers::Offset<MetaGraph> CreateMetaGraphDirect( 432 allTensors__, 433 subGraph__, 434 obfuscate, 435- obfMetaData__); 436+ encrypt, 437+ obfMetaData__, 438+ decryptTable__); 439 } 440 441 inline const mindspore::schema::MetaGraph *GetMetaGraph(const void *buf) { 442diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt 443index 470334730f..4293918967 100644 444--- a/mindspore/lite/src/CMakeLists.txt 445+++ b/mindspore/lite/src/CMakeLists.txt 446@@ -633,11 +633,6 @@ if(NOT WIN32) 447 target_link_libraries(mindspore-lite dl) 448 endif() 449 450-if(MSLITE_ENABLE_MODEL_OBF) 451- target_link_libraries(mindspore-lite ${OBF_LIB_DIR}/libmsdeobfuscator-lite.so) 452- target_link_libraries(mindspore-lite_static ${OBF_LIB_DIR}/libmsdeobfuscator-lite.so) 453-endif() 454- 455 if(MSLITE_ENABLE_KERNEL_EXECUTOR) 456 target_link_libraries(mindspore-lite mindspore_core) 457 target_link_libraries(mindspore-lite_static mindspore_core) 458diff --git a/mindspore/lite/src/executor/CMakeLists.txt b/mindspore/lite/src/executor/CMakeLists.txt 459index deadda8e36..5ccea72dfb 100644 460--- a/mindspore/lite/src/executor/CMakeLists.txt 461+++ b/mindspore/lite/src/executor/CMakeLists.txt 462@@ -289,6 +289,3 @@ if(NOT WIN32) 463 target_link_libraries(lite-unified-executor dl) 464 endif() 465 466-if(ENABLE_MODEL_OBF) 467- target_link_libraries(lite-unified-executor ${OBF_LIB_DIR}/libmsdeobfuscator-lite.so) 468-endif() 469diff --git a/mindspore/lite/src/extendrt/delegate/graph_executor/litert/CMakeLists.txt b/mindspore/lite/src/extendrt/delegate/graph_executor/litert/CMakeLists.txt 470index 6dc3a944ef..6b92bfcd74 100644 471--- a/mindspore/lite/src/extendrt/delegate/graph_executor/litert/CMakeLists.txt 472+++ b/mindspore/lite/src/extendrt/delegate/graph_executor/litert/CMakeLists.txt 473@@ -301,6 +301,3 @@ if(NOT WIN32) 474 target_link_libraries(msplugin-ge-litert dl) 475 endif() 476 477-if(ENABLE_MODEL_OBF) 478- target_link_libraries(msplugin-ge-litert ${OBF_LIB_DIR}/libmsdeobfuscator-lite.so) 479-endif() 480diff --git a/mindspore/lite/src/litert/lite_model.cc b/mindspore/lite/src/litert/lite_model.cc 481index 13652633b8..b038831b1f 100644 482--- a/mindspore/lite/src/litert/lite_model.cc 483+++ b/mindspore/lite/src/litert/lite_model.cc 484@@ -32,6 +32,7 @@ 485 #include "src/tensor.h" 486 #include "extendrt/mindir_loader/model_loader.h" 487 #include "src/common/mmap_utils.h" 488+#include <dlfcn.h> 489 490 namespace mindspore::lite { 491 namespace { 492@@ -69,12 +70,9 @@ void LiteModel::Free() { 493 } 494 inner_all_tensors_.clear(); 495 496-#ifdef ENABLE_MODEL_OBF 497- for (auto &prim : graph_.deobf_prims_) { 498- free(prim); 499+ if(this->deobf != nullptr){ 500+ delete(reinterpret_cast<DeObfProcessor *>(this->deobf)); 501 } 502- graph_.deobf_prims_.resize(0); 503-#endif 504 } 505 506 void LiteModel::Destroy() { 507@@ -385,6 +383,12 @@ bool LiteModel::ModelVerify() const { 508 509 return NodeVerify() == RET_OK && SubGraphVerify() == RET_OK; 510 } 511+//static variable used for deobfuscator 512+ObfCreateFunc DeObfRegister::NewDeObfProcessor = DeObfRegister::Fail; 513+bool (DeObfProcessor::*DeObfRegister::GetModelDeObfReg)(const void *meta_graph, Model *model); 514+void (DeObfProcessor::*DeObfRegister::DeObfuscateReg)(Model *model); 515+DeObfRet (DeObfProcessor::*DeObfRegister::CreateDeObfNodeReg)(const schema::Primitive *&src_prim, int i, int schema__version); 516+void *DeObfRegister::deobf_handle = nullptr; 517 518 int LiteModel::GenerateModelByVersion() { 519 if (this->buf == nullptr) { 520@@ -397,33 +401,37 @@ int LiteModel::GenerateModelByVersion() { 521 } 522 MS_ASSERT(meta_graph != nullptr); 523 int status = RET_ERROR; 524-#ifdef ENABLE_MODEL_OBF 525- DeObfuscator *model_deobf = nullptr; 526-#endif 527+ if(dlopen("libdeobfuscator_lib.z.so", RTLD_NOLOAD) == nullptr) { 528+ DeObfRegister::deobf_handle = dlopen("libdeobfuscator_lib.z.so", RTLD_NOW | RTLD_GLOBAL); 529+ } 530+ if(DeObfRegister::deobf_handle == nullptr) { 531+ MS_LOG(WARNING) << "Deobfuscate ability is disabled, so obfuscated models can not be executed."; 532+ } else { 533+ auto CreateDeObfFunc = reinterpret_cast<ObfCreateFunc>(dlsym(DeObfRegister::deobf_handle, "CreateDeObfFunc")); 534+ if (CreateDeObfFunc == nullptr) { 535+ MS_LOG(WARNING) << "cannot fetch CreateDeObfFunc"; 536+ } else { 537+ DeObfRegister::RegisterDeObfuscator(CreateDeObfFunc); 538+ DeObfRegister::NewDeObfProcessor(*this); 539+ } 540+ } 541 if (schema_version_ == SCHEMA_VERSION::SCHEMA_CUR) { 542-#ifdef ENABLE_MODEL_OBF 543- if (IsMetaGraphObfuscated<schema::MetaGraph>(*reinterpret_cast<const schema::MetaGraph *>(meta_graph))) { 544- model_deobf = GetModelDeObfuscator<schema::MetaGraph>(*reinterpret_cast<const schema::MetaGraph *>(meta_graph), 545- this, this->buf_size_); 546- this->graph_.model_obfuscated_ = true; 547- if (model_deobf == nullptr) { 548+ if(this->deobf != nullptr) { 549+ auto deobf_ptr = reinterpret_cast<DeObfProcessor *>(this->deobf); 550+ auto ret = (deobf_ptr->*DeObfRegister::GetModelDeObfReg)(meta_graph, this); 551+ if(!ret){ 552 return RET_ERROR; 553 } 554 } 555-#endif 556 status = GenerateModel<schema::MetaGraph, schema::CNode>(*reinterpret_cast<const schema::MetaGraph *>(meta_graph)); 557 } 558-#ifdef ENABLE_MODEL_OBF 559- if (this->graph_.model_obfuscated_) { 560- MS_ASSERT(model_deobf != nullptr); 561- status = DeObfuscateModel(this, model_deobf); 562- if (status != RET_OK) { 563- MS_LOG(ERROR) << "deobfuscate model wrong."; 564- std::cerr << "deobfuscate model wrong." << std::endl; 565- } 566- delete (model_deobf); 567+ if(this->deobf != nullptr) { 568+ auto deobf_ptr = reinterpret_cast<DeObfProcessor *>(this->deobf); 569+ (deobf_ptr->*DeObfRegister::DeObfuscateReg)(this); 570+ } 571+ if(DeObfRegister::deobf_handle != nullptr) { 572+ dlclose(DeObfRegister::deobf_handle); 573 } 574-#endif 575 if (this->graph_.version_ != Version()) { 576 MS_LOG(INFO) << "model version is " << this->graph_.version_ << ", inference version is " << Version() 577 << " not equal"; 578diff --git a/mindspore/lite/src/litert/lite_model.h b/mindspore/lite/src/litert/lite_model.h 579index 635b529a5f..647746a23a 100644 580--- a/mindspore/lite/src/litert/lite_model.h 581+++ b/mindspore/lite/src/litert/lite_model.h 582@@ -29,13 +29,11 @@ 583 #include "src/litert/schema_tensor_wrapper.h" 584 #include "nnacl/op_base.h" 585 #include "src/common/prim_util.h" 586-#ifdef ENABLE_MODEL_OBF 587-#include "tools/obfuscator/include/deobfuscator.h" 588-#endif 589 #include "include/api/types.h" 590 #ifdef ENABLE_LITE_HELPER 591 #include "src/common/helper/infer_helpers.h" 592 #endif 593+#include "include/registry/deobf_processor.h" 594 595 namespace mindspore { 596 namespace lite { 597@@ -147,38 +145,23 @@ class MS_API LiteModel : public Model { 598 auto c_node = meta_graph.nodes()->template GetAs<U>(i); 599 MS_CHECK_TRUE_MSG(c_node != nullptr, false, "get as cnode fail!"); 600 node->node_type_ = GetPrimitiveType(c_node->primitive(), schema_version_); 601-#ifdef ENABLE_MODEL_OBF 602- auto src_prim = reinterpret_cast<const schema::Primitive *>(c_node->primitive()); 603- if (src_prim == nullptr) { 604- delete node; 605- return false; 606- } 607- auto src_prim_type = src_prim->value_type(); 608- unsigned char *dst_prim = nullptr; 609- if (src_prim_type == schema::PrimitiveType_GenOP) { 610- if (i >= this->graph_.all_nodes_stat_.size() || i >= this->graph_.all_prims_type_.size()) { 611+ if(this->deobf != nullptr){ 612+ auto src_prim = reinterpret_cast<const schema::Primitive *>(c_node->primitive()); 613+ auto deobf_ptr = reinterpret_cast<DeObfProcessor *>(this->deobf); 614+ DeObfRet ret = (deobf_ptr->*DeObfRegister::CreateDeObfNodeReg)(src_prim,i,schema_version_); 615+ if(ret == kDeObfFailed){ 616 delete node; 617 return false; 618 } 619- auto src_node_stat = this->graph_.all_nodes_stat_[i]; 620- auto dst_prim_type = this->graph_.all_prims_type_[i]; 621- auto ret = DeObfuscatePrimitive(src_prim, src_node_stat, &dst_prim, schema::PrimitiveType(dst_prim_type)); 622- if (!ret) { 623- MS_LOG(ERROR) << "Deobfuscate primitive failed!"; 624- delete node; 625- return false; 626- } 627- if (dst_prim == nullptr) { 628+ if(ret == kNoObf){ 629 this->graph_.all_nodes_.push_back(node); 630 continue; 631 } 632- this->graph_.deobf_prims_.push_back(dst_prim); 633- src_prim = reinterpret_cast<const schema::Primitive *>(flatbuffers::GetRoot<schema::Primitive>(dst_prim)); 634+ node->primitive_ = const_cast<schema::Primitive *>(src_prim); 635+ } 636+ else{ 637+ node->primitive_ = c_node->primitive(); 638 } 639- node->primitive_ = const_cast<schema::Primitive *>(src_prim); 640-#else 641- node->primitive_ = c_node->primitive(); 642-#endif 643 auto status = SetQuantType(meta_graph, c_node, node); 644 if (status == RET_ERROR) { 645 return false; 646diff --git a/mindspore/lite/test/CMakeLists.txt b/mindspore/lite/test/CMakeLists.txt 647index 78dab536e6..3f12ec807b 100644 648--- a/mindspore/lite/test/CMakeLists.txt 649+++ b/mindspore/lite/test/CMakeLists.txt 650@@ -240,10 +240,6 @@ if(MSLITE_ENABLE_CONVERTER) 651 onnx_parser_mid tf_parser_mid third_party_parser_mid) 652 endif() 653 654-if(MSLITE_ENABLE_MODEL_OBF) 655- target_link_libraries(lite-test ${OBF_LIB_DIR}/libmsdeobfuscator-lite.so) 656-endif() 657- 658 if(MSLITE_ENABLE_KERNEL_EXECUTOR) 659 target_link_libraries(lite-test kernel_executor) 660 endif() 661diff --git a/mindspore/lite/tools/obfuscator/include/deobf_creator.h b/mindspore/lite/tools/obfuscator/include/deobf_creator.h 662new file mode 100644 663index 0000000000..506eccd8d1 664--- /dev/null 665+++ b/mindspore/lite/tools/obfuscator/include/deobf_creator.h 666@@ -0,0 +1,7 @@ 667+#include "include/registry/deobf_processor.h" 668+ 669+namespace mindspore::lite{ 670+ 671+ extern "C" void CreateDeObfFunc(Model &model); 672+ 673+}//mindspore::lite 674\ No newline at end of file 675