system_utils.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2021 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 <sys/timeb.h>
  15. #include <string.h>
  16. #include <algorithm>
  17. #include <iterator>
  18. #include "encryption/include/model_code.h"
  19. #include "encryption/util/include/system_utils.h"
  20. #include "encryption/util/include/crypto/basic.h"
  21. #include "encryption/util/include/crypto/sha256_utils.h"
  22. #include "encryption/util/include/io_utils.h"
  23. #include "encryption/util/include/log.h"
  24. #include "encryption/util/include/constant/constant_model.h"
  25. const char alphabet[] =
  26. "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(){}[]<>?~";
  27. namespace util {
  28. int SystemUtils::intN(int n) { return rand() % n; }
  29. std::string SystemUtils::random_key_iv(int len) {
  30. unsigned char* tmp = (unsigned char*)malloc(sizeof(unsigned char) * len);
  31. int ret = util::crypto::Basic::random(tmp, len);
  32. std::string tmp_str(reinterpret_cast<const char*>(tmp), len);
  33. free(tmp);
  34. return tmp_str;
  35. }
  36. std::string SystemUtils::random_str(int len) {
  37. unsigned char* tmp = (unsigned char*)malloc(sizeof(unsigned char) * len);
  38. int ret = util::crypto::Basic::random(tmp, len);
  39. std::string tmp_str(reinterpret_cast<const char*>(tmp), len);
  40. free(tmp);
  41. return tmp_str;
  42. }
  43. int SystemUtils::check_key_match(const char* key, const char* filepath) {
  44. std::string aes_key_iv(key);
  45. std::string sha256_aes_key_iv =
  46. util::crypto::SHA256Utils::sha256_string(aes_key_iv);
  47. unsigned char* data_pos = (unsigned char*)malloc(sizeof(unsigned char) * 64);
  48. int ret = ioutil::read_with_pos_and_length(
  49. filepath, data_pos, constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN,
  50. 64);
  51. if (ret != CODE_OK) {
  52. LOGD("[M]read file failed when check key");
  53. return ret;
  54. }
  55. std::string check_str(reinterpret_cast<char*>(data_pos), 64);
  56. if (strcmp(sha256_aes_key_iv.c_str(), check_str.c_str()) != 0) {
  57. return CODE_KEY_NOT_MATCH;
  58. }
  59. free(data_pos);
  60. return CODE_OK;
  61. }
  62. int SystemUtils::check_key_match(const std::string& key,
  63. std::istream& cipher_stream) {
  64. cipher_stream.seekg(0, std::ios::beg);
  65. std::string sha256_aes_key_iv = util::crypto::SHA256Utils::sha256_string(key);
  66. int check_len = 64;
  67. std::string data_pos_str;
  68. cipher_stream.seekg(constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN);
  69. std::copy_n(std::istreambuf_iterator<char>(cipher_stream), check_len,
  70. std::back_inserter(data_pos_str));
  71. if (data_pos_str.size() != check_len) {
  72. LOGD("[M]read file failed when check key");
  73. return CODE_OPEN_FAILED;
  74. }
  75. if (data_pos_str == sha256_aes_key_iv) {
  76. return CODE_OK;
  77. }
  78. return CODE_KEY_NOT_MATCH;
  79. }
  80. /**
  81. *
  82. * @param filepath
  83. * @return 0 - file encrypted 1 - file unencrypted
  84. */
  85. int SystemUtils::check_file_encrypted(const char* filepath) {
  86. size_t read_len = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN;
  87. unsigned char* data_pos =
  88. (unsigned char*)malloc(sizeof(unsigned char) * read_len);
  89. if (ioutil::read_with_pos_and_length(filepath, data_pos, 0, read_len) !=
  90. CODE_OK) {
  91. LOGD("check file failed when read %s(file)", filepath);
  92. return CODE_OPEN_FAILED;
  93. }
  94. std::string tag(constant::MAGIC_NUMBER);
  95. tag.append(constant::VERSION);
  96. std::string check_str(reinterpret_cast<char*>(data_pos), read_len);
  97. int ret_cmp = strcmp(tag.c_str(), check_str.c_str()) == 0 ? 0 : 1;
  98. free(data_pos);
  99. return ret_cmp;
  100. }
  101. int SystemUtils::check_file_encrypted(std::istream& cipher_stream) {
  102. cipher_stream.seekg(0, std::ios::beg);
  103. size_t read_len = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN;
  104. std::string data_pos_str;
  105. std::copy_n(std::istreambuf_iterator<char>(cipher_stream), read_len,
  106. std::back_inserter(data_pos_str));
  107. if (data_pos_str.size() != read_len) {
  108. LOGD("check file failed when read cipher stream");
  109. return CODE_OPEN_FAILED;
  110. }
  111. std::string tag(constant::MAGIC_NUMBER);
  112. tag.append(constant::VERSION);
  113. if (data_pos_str == tag) {
  114. return 0;
  115. }
  116. return 1;
  117. }
  118. int SystemUtils::check_pattern_exist(const std::vector<std::string>& vecs,
  119. const std::string& pattern) {
  120. if (std::find(vecs.begin(), vecs.end(), pattern) == vecs.end()) {
  121. return -1; // not exist
  122. } else {
  123. return 0; // exist
  124. }
  125. }
  126. } // namespace util