You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_cryption.cpp 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "example.h"
  2. #if LITE_BUILD_WITH_MGE
  3. using namespace lite;
  4. using namespace example;
  5. namespace {
  6. std::vector<uint8_t> decrypt_model(
  7. const void* model_mem, size_t size, const std::vector<uint8_t>& key) {
  8. if (key.size() == 1) {
  9. std::vector<uint8_t> ret(size, 0);
  10. const uint8_t* ptr = static_cast<const uint8_t*>(model_mem);
  11. uint8_t key_data = key[0];
  12. for (size_t i = 0; i < size; i++) {
  13. ret[i] = ptr[i] ^ key_data ^ key_data;
  14. }
  15. return ret;
  16. } else {
  17. printf("the user define decrypt method key length is wrong.\n");
  18. return {};
  19. }
  20. }
  21. bool register_cryption_method(const Args& args) {
  22. std::string network_path = args.model_path;
  23. std::string input_path = args.input_path;
  24. //! register the decryption method
  25. register_decryption_and_key("just_for_test", decrypt_model, {15});
  26. lite::Config config;
  27. config.bare_model_cryption_name = "just_for_test";
  28. //! create and load the network
  29. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  30. network->load_model(network_path);
  31. //! set input data to input tensor
  32. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  33. auto layout = input_tensor->get_layout();
  34. auto src_tensor = parse_npy(input_path);
  35. void* src = src_tensor->get_memory_ptr();
  36. input_tensor->reset(src, layout);
  37. //! forward
  38. network->forward();
  39. network->wait();
  40. //! get the output data or read tensor set in network_in
  41. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  42. void* out_data = output_tensor->get_memory_ptr();
  43. size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
  44. output_tensor->get_layout().get_elem_size();
  45. float max = -1.0f;
  46. float sum = 0.0f;
  47. for (size_t i = 0; i < out_length; i++) {
  48. float data = static_cast<float*>(out_data)[i];
  49. sum += data;
  50. if (max < data)
  51. max = data;
  52. }
  53. printf("max=%e, sum=%e\n", max, sum);
  54. return true;
  55. }
  56. bool update_cryption_key(const Args& args) {
  57. std::string network_path = args.model_path;
  58. std::string input_path = args.input_path;
  59. //! update the decryption method key
  60. std::vector<uint8_t> key(32, 0);
  61. for (size_t i = 0; i < 32; i++) {
  62. key[i] = 31 - i;
  63. }
  64. update_decryption_or_key("AES_default", nullptr, key);
  65. lite::Config config;
  66. config.bare_model_cryption_name = "AES_default";
  67. //! create and load the network
  68. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  69. network->load_model(network_path);
  70. //! set input data to input tensor
  71. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  72. auto layout = input_tensor->get_layout();
  73. auto src_tensor = parse_npy(input_path);
  74. void* src = src_tensor->get_memory_ptr();
  75. input_tensor->reset(src, layout);
  76. //! forward
  77. network->forward();
  78. network->wait();
  79. //! get the output data or read tensor set in network_in
  80. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  81. void* out_data = output_tensor->get_memory_ptr();
  82. size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
  83. output_tensor->get_layout().get_elem_size();
  84. float max = -1.0f;
  85. float sum = 0.0f;
  86. for (size_t i = 0; i < out_length; i++) {
  87. float data = static_cast<float*>(out_data)[i];
  88. sum += data;
  89. if (max < data)
  90. max = data;
  91. }
  92. printf("max=%e, sum=%e\n", max, sum);
  93. return true;
  94. }
  95. } // namespace
  96. REGIST_EXAMPLE("register_cryption_method", register_cryption_method);
  97. REGIST_EXAMPLE("update_cryption_key", update_cryption_key);
  98. #endif
  99. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}