I have a C function which uses a private key and want to prevent that anyone can read out this key. Not only from the binary - but also from the source repository. The key is not in plaintext, but still it should be as difficult as possible to get access to it. My plan is to compile this c file to an object file and place this object file (with the obfuscated privateKey array) into the source repository. But I'm not sure about in which storage-class/scope I should put the key (local stack variable, static variable - local/global, ...).
1.) Global static
static unsigned char const privateKey[] = {
0x44, 0x8e, 0x54, 0xae, 0x64, 0x74, 0xbe, ...
};
void myFunction(){
//do something with privateKey
}
2.) Local Stackvariable
void myFunction(){
unsigned char const privateKey[] = {
0x44, 0x8e, 0x54, 0xae, 0x64, 0x74, 0xbe, ...
};
//do something with privateKey
}
3.) Local Static
void myFunction(){
static unsigned char const privateKey[] = {
0x44, 0x8e, 0x54, 0xae, 0x64, 0x74, 0xbe, ...
};
//do something with privateKey
}
I just viewed the object files of these 3 solutions with nm - at the 2nd solution I don't even see a symbol for the keys (but I'm afraid with using a Debugger it's easy to read the key, cause it's placed on the stack at runtime). Solution 3 looks in nm like:
00000000 T myFunction
00000200 t __6_privateKey.2
and Solution 1 like:
00000000 T myFunction
00000200 t privateKey
Where is the most secure storage-class to put the key in - or is there no difference in terms of security at all? Is there another more secure solution?