paddle_model_decrypt.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <iostream>
  2. #include <string>
  3. #include <string.h>
  4. #include <fstream>
  5. #include <memory>
  6. #include <iterator>
  7. #include <algorithm>
  8. #include "paddle_model_decrypt.h"
  9. #include "model_code.h"
  10. #include "../util/crypto/aes_gcm.h"
  11. #include "../util/io_utils.h"
  12. #include "../util/log.h"
  13. #include "../constant/constant_model.h"
  14. #include "../util/system_utils.h"
  15. #include "../util/crypto/base64.h"
  16. /**
  17. * 0 - encrypted
  18. * 1 - unencrypt
  19. */
  20. int paddle_check_file_encrypted(const char* file_path) {
  21. return util::SystemUtils::check_file_encrypted(file_path);
  22. }
  23. std::string decrypt_file(const char* file_path, const char* key) {
  24. int ret = paddle_check_file_encrypted(file_path);
  25. if (ret != CODE_OK) {
  26. LOGD("[M]check file encrypted failed, code: %d", ret);
  27. return std::string();
  28. }
  29. // std::string key_str = util::crypto::Base64Utils::decode(std::string(key));
  30. std::string key_str = baidu::base::base64::base64_decode(std::string(key));
  31. int ret_check = util::SystemUtils::check_key_match(key_str.c_str(), file_path);
  32. if (ret_check != CODE_OK) {
  33. LOGD("[M]check key failed in decrypt_file, code: %d", ret_check);
  34. return std::string();
  35. }
  36. unsigned char* aes_key = (unsigned char*) malloc(sizeof(unsigned char) * AES_GCM_KEY_LENGTH);
  37. unsigned char* aes_iv = (unsigned char*) malloc(sizeof(unsigned char) * AES_GCM_IV_LENGTH);
  38. memcpy(aes_key, key_str.c_str(), AES_GCM_KEY_LENGTH);
  39. memcpy(aes_iv, key_str.c_str() + 16, AES_GCM_IV_LENGTH);
  40. size_t pos = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN + constant::TAG_LEN;
  41. // read encrypted data
  42. unsigned char* dataptr = NULL;
  43. size_t data_len = 0;
  44. int ret_read_data = ioutil::read_with_pos(file_path, pos, &dataptr, &data_len);
  45. if (ret_read_data != CODE_OK) {
  46. LOGD("[M]read file failed, code = %d", ret_read_data);
  47. return std::string();
  48. }
  49. // decrypt model data
  50. size_t model_plain_len = data_len - AES_GCM_TAG_LENGTH;
  51. unsigned char* model_plain = (unsigned char*) malloc(sizeof(unsigned char) * model_plain_len);
  52. int ret_decrypt_file =
  53. util::crypto::AesGcm::decrypt_aes_gcm(
  54. dataptr,
  55. data_len,
  56. aes_key,
  57. aes_iv,
  58. model_plain,
  59. reinterpret_cast<int&>(model_plain_len));
  60. free(dataptr);
  61. free(aes_key);
  62. free(aes_iv);
  63. if (ret_decrypt_file != CODE_OK) {
  64. free(model_plain);
  65. LOGD("[M]decrypt file failed, decrypt ret = %d", ret_decrypt_file);
  66. return std::string();
  67. }
  68. std::string result((const char*)model_plain);
  69. free(model_plain);
  70. return result;
  71. }
  72. /**
  73. * support model_file encrypted or unencrypt
  74. * support params_file encrypted or unencrypt
  75. * all in one interface
  76. */
  77. int paddle_security_load_model(
  78. paddle::AnalysisConfig* config,
  79. const char* key,
  80. const char* model_file,
  81. const char* param_file) {
  82. // 0 - file encrypted 1 - file unencrypted
  83. int m_en_flag = util::SystemUtils::check_file_encrypted(model_file);
  84. if (m_en_flag == CODE_OPEN_FAILED) {
  85. return m_en_flag;
  86. }
  87. int p_en_flag = util::SystemUtils::check_file_encrypted(param_file);
  88. if (p_en_flag == CODE_OPEN_FAILED) {
  89. return p_en_flag;
  90. }
  91. unsigned char* aes_key = NULL;
  92. unsigned char* aes_iv = NULL;
  93. if (m_en_flag == 0 || p_en_flag == 0) {
  94. // std::string key_str = util::crypto::Base64Utils::decode(std::string(key));
  95. std::string key_str = baidu::base::base64::base64_decode(std::string(key));
  96. int ret_check = 0;
  97. if (m_en_flag == 0) {
  98. ret_check = util::SystemUtils::check_key_match(key_str.c_str(), model_file);
  99. if (ret_check != CODE_OK) {
  100. LOGD("[M]check key failed in model_file");
  101. return ret_check;
  102. }
  103. }
  104. if (p_en_flag == 0) {
  105. ret_check = util::SystemUtils::check_key_match(key_str.c_str(), param_file);
  106. if (ret_check != CODE_OK) {
  107. LOGD("[M]check key failed in param_file");
  108. return ret_check;
  109. }
  110. }
  111. aes_key = (unsigned char*) malloc(sizeof(unsigned char) * AES_GCM_KEY_LENGTH);
  112. aes_iv = (unsigned char*) malloc(sizeof(unsigned char) * AES_GCM_IV_LENGTH);
  113. memcpy(aes_key, key_str.c_str(), AES_GCM_KEY_LENGTH);
  114. memcpy(aes_iv, key_str.c_str() + 16, AES_GCM_IV_LENGTH);
  115. }
  116. size_t pos = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN + constant::TAG_LEN;
  117. // read encrypted model
  118. unsigned char* model_dataptr = NULL;
  119. size_t model_data_len = 0;
  120. int ret_read_model = ioutil::read_with_pos(model_file, pos, &model_dataptr, &model_data_len);
  121. if (ret_read_model != CODE_OK) {
  122. LOGD("[M]read model failed");
  123. return ret_read_model;
  124. }
  125. size_t model_plain_len = 0;
  126. unsigned char* model_plain = NULL;
  127. if (m_en_flag == 0) {
  128. // decrypt model data
  129. model_plain_len = model_data_len - AES_GCM_TAG_LENGTH;
  130. model_plain = (unsigned char*) malloc(sizeof(unsigned char) * model_plain_len);
  131. int ret_decrypt_model =
  132. util::crypto::AesGcm::decrypt_aes_gcm(model_dataptr,
  133. model_data_len,
  134. aes_key,
  135. aes_iv,
  136. model_plain,
  137. reinterpret_cast<int&>(model_plain_len));
  138. free(model_dataptr);
  139. if (ret_decrypt_model != CODE_OK) {
  140. free(aes_key);
  141. free(aes_iv);
  142. free(model_plain);
  143. LOGD("[M]decrypt model failed, decrypt ret = %d", ret_decrypt_model);
  144. return CODE_AES_GCM_DECRYPT_FIALED;
  145. }
  146. } else {
  147. model_plain = model_dataptr;
  148. model_plain_len = model_data_len;
  149. }
  150. // read encrypted params
  151. unsigned char* params_dataptr = NULL;
  152. size_t params_data_len = 0;
  153. int ret_read_params = ioutil::read_with_pos(param_file, pos, &params_dataptr, &params_data_len);
  154. if (ret_read_params != CODE_OK) {
  155. LOGD("[M]read params failed");
  156. return ret_read_params;
  157. }
  158. size_t params_plain_len = 0;
  159. unsigned char* params_plain = NULL;
  160. if (p_en_flag == 0) {
  161. // decrypt params data
  162. params_plain_len = params_data_len - AES_GCM_TAG_LENGTH;
  163. params_plain = (unsigned char*) malloc(sizeof(unsigned char) * params_plain_len);
  164. int ret_decrypt_params =
  165. util::crypto::AesGcm::decrypt_aes_gcm(params_dataptr,
  166. params_data_len,
  167. aes_key,
  168. aes_iv,
  169. params_plain,
  170. reinterpret_cast<int&>(params_plain_len));
  171. free(params_dataptr);
  172. free(aes_key);
  173. free(aes_iv);
  174. if (ret_decrypt_params != CODE_OK) {
  175. free(params_plain);
  176. LOGD("[M]decrypt params failed, decrypt ret = %d", ret_decrypt_params);
  177. return CODE_AES_GCM_DECRYPT_FIALED;
  178. }
  179. } else {
  180. params_plain = params_dataptr;
  181. params_plain_len = params_data_len;
  182. }
  183. LOGD("Prepare to set config");
  184. config->SetModelBuffer(reinterpret_cast<const char*>(model_plain), model_plain_len,
  185. reinterpret_cast<const char*>(params_plain), params_plain_len);
  186. if (m_en_flag == 1) {
  187. free(model_dataptr);
  188. }
  189. if (p_en_flag == 1) {
  190. free(params_dataptr);
  191. }
  192. return CODE_OK;
  193. }