paddle_model_decrypt.cpp 8.7 KB

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