MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / decrypt_model

Method decrypt_model

lite/src/decryption/aes_decrypt.h:8–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6class AESDcryption {
7public:
8 static std::vector<uint8_t> decrypt_model(
9 const void* model_mem, size_t size, const std::vector<uint8_t>& key) {
10 mbedtls_aes_context ctx;
11 mbedtls_aes_init(&ctx);
12 mbedtls_aes_setkey_dec(&ctx, key.data(), 256);
13
14 auto data = static_cast<const uint8_t*>(model_mem);
15 //! first 16 bytes is IV
16 uint8_t iv[16];
17 //! last 8 bytes is file size(length)
18 auto length_ptr = data + size - 8;
19 size_t length = 0;
20 for (int i = 0; i < 8; i++) {
21 length |= length_ptr[i] << (8 * (7 - i));
22 }
23 std::copy(data, data + 16, iv);
24 auto output = std::vector<uint8_t>(size - 24);
25 mbedtls_aes_crypt_cbc(
26 &ctx, MBEDTLS_AES_DECRYPT, size - 24, iv, data + 16, output.data());
27 mbedtls_aes_free(&ctx);
28 output.erase(output.begin() + length, output.end());
29 return output;
30 }
31
32 static std::vector<uint8_t> get_decrypt_key() {
33 std::vector<uint8_t> key(32);

Callers

nothing calls this directly

Calls 9

mbedtls_aes_initFunction · 0.85
mbedtls_aes_setkey_decFunction · 0.85
mbedtls_aes_crypt_cbcFunction · 0.85
mbedtls_aes_freeFunction · 0.85
copyFunction · 0.50
dataMethod · 0.45
eraseMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected