(Note. I am sure that there are strict checklists and official instructions on how to handle passwords in secure software from people and authorities who really know something about security. This is not one of them!)
I donβt think that there is a cryptographically secure way to have passwords in your processes memory, to be able to use them, but not to provide access to it to a user who can run your application under a debugger or check your kernel dumps.
What you can do is an obscure password. Here are some methods you can use:
- Do not store the password as a simple line anywhere in the memory (scatter characters around, etc.).
- Scrub all the variables that are stored in the password after they are used (for example, if you pass the password to a function, you must set all the characters of this variable to NUL inside the function after you finish with it.
- Encrypt password.
- Replace the encryption key each time the application starts (or periodically if it is a long application.)
- Create an encryption key procedurally based on some aspect of the system / hardware, rather than saving the encryption key for the password anywhere in your process memory.
- Use hardware, such as a Trusted Platform Module (TPM), if available.
Implementing the above sequentially and effectively is quite complex and affects all your code that deals with the password. And sometimes you even have to deliberately make your code more obscure and go against all your instincts as a programmer (for example, do not pass the password to functions as a parameter, but use hard-coded addresses inside the function.)
Once again, I want to emphasize that it may not be possible to protect your passwords only in software, when the adversary has full access to the physical machine.
As for the second part of your question, I donβt know a single encryption library for the header only, but for encrypting the password you probably only need a cipher and probably a hash. And all the best algorithms have public or other free implementations in the wild. You can get one of them and copy / paste into your application. Remember to seriously check it out though!
source share