| 6 | class AESDcryption { |
| 7 | public: |
| 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); |
nothing calls this directly
no test coverage detected