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.

aes_decrypt.h 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * \file src/decryption/aes_decrypt.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "./mbedtls/aes.h"
  12. #include "decrypt_base.h"
  13. namespace lite {
  14. class AESDcryption {
  15. public:
  16. static std::vector<uint8_t> decrypt_model(const void* model_mem,
  17. size_t size,
  18. const std::vector<uint8_t>& key) {
  19. mbedtls_aes_context ctx;
  20. mbedtls_aes_init(&ctx);
  21. mbedtls_aes_setkey_dec(&ctx, key.data(), 256);
  22. auto data = static_cast<const uint8_t*>(model_mem);
  23. //! first 16 bytes is IV
  24. uint8_t iv[16];
  25. //! last 8 bytes is file size(length)
  26. auto length_ptr = data + size - 8;
  27. size_t length = 0;
  28. for (int i = 0; i < 8; i++) {
  29. length |= length_ptr[i] << (8 * (7 - i));
  30. }
  31. std::copy(data, data + 16, iv);
  32. auto output = std::vector<uint8_t>(size - 24);
  33. mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, size - 24, iv,
  34. data + 16, output.data());
  35. mbedtls_aes_free(&ctx);
  36. output.erase(output.begin() + length, output.end());
  37. return output;
  38. }
  39. static std::vector<uint8_t> get_decrypt_key() {
  40. std::vector<uint8_t> key = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  41. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
  42. 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
  43. 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
  44. 0x1C, 0x1D, 0x1E, 0x1F};
  45. return key;
  46. }
  47. };
  48. } // namespace lite
  49. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台