-2

I was wondering if something like compile time function encryption is possible and if it's possible how can someone achieve it ? And by "compile time function encryption" I mean encrypting the function code during compile time and later on at runtime decrypt the code when you need to call that function.

Thanks in advance!

bogding
  • 11
  • 1
  • After you [strip](https://en.wikipedia.org/wiki/Strip_%28Unix%29) binary of all symbols, it's pretty much encrypted. Even if someone disassembles the binary, they will not be able to understand what each assembly line does. – Yksisarvinen Apr 20 '20 at 16:25
  • What would be the purpose of this? If an attacker has access to your physical device, then encrypting it on the HD won't matter - they can just read the memory when you load the library. If it's for encryption over a transmission line, just encrypt it with a public key on the sending end and decrypt with the corresponding private key on the receiving end. No need to store encrypted on the HD. – JohnFilleau Apr 20 '20 at 16:30
  • 1
    @Yksisarvinen _Even if someone disassembles the binary, they will not be able to understand what each assembly line does._ People and machines have been doing it for decades. It is called _reverse engineering_. One such effort from the early '80s was to create an IBM-PC compatible BIOS that helped create the "clone" PC market. – 1201ProgramAlarm Apr 20 '20 at 16:33

2 Answers2

0

No, neither compile-time encryption nor any code self-modification is possible in c++.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
user2807083
  • 2,962
  • 4
  • 29
  • 37
0

Standard C++ does not allow access code as data.

That is,

int f(int);

reinterpret_cast<int*>(&f) = 1;

Is not valid, you cannot access "code" as data. Sure you cannot access to code as data at at compile time too. So you cannot neither encrypt nor decrypt your unction.

Still there are some tools that actually do this. But they rely on implementation-specific behavior at runtime. At compile time they just add additional step, which is usually not known to compiler and happens after compilation by tampering with compiler output.

And something may work in portable C++, at least in theory, it is not what you want, but it is a "compile time function encryption" you're asking for.

If you define some grammar for your functions, like, you can parse, say, const char* function = "(a, b, c) { return a + b * c; }", then if you can add constexpr encrypting function, you'll have function in your program that encrypts at compile time, and can be decrypted before execution.

Sure Standard also does not require that calling a constexpr function to produce static initialization data indeed happens at compile time, but it is something expected from a good implementation.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
  • I saw an example of xoring functions at runtime using assembly. http://www.rohitab.com/discuss/topic/41342-simple-self-modifying-code-example And I was wondering if something like this could be achievable during compile time. – bogding Apr 20 '20 at 20:32
  • Standard C++ does not allows that. So the example in the _implementation-defined area_. In theory, an implementation could have defined how to change function at compile time. In practice you cannot force compiler to do that, but still it can be done as extra build step – Alex Guteniev Apr 21 '20 at 03:10