How to securely hide a cryptographic key?
Posted by davitb on November 10th, 2009
In my previous article I was discussing how a “non expert” security engineer should think about choosing the right security protocol for his system. I have tried to present that complicated task in an easier manner.
However it’s important to also understand that when we integrate a secret based protocol in our system, we will get another, more serious problem – how to securely hide the secret that is used in the protocol for authentication?
For example, suppose you have two components which are establishing an SSL protocol. As SSL is based on public key cryptography they both need to possess private keys (if the authentication is mutual). So how are they going to protect their private keys locally in their systems?
Of course the problem of hiding cryptographic keys or other secret information doesn’t only apply to systems which are implementing security protocols. Encryption is the most popular and convenient way to protect the confidentiality of information. However when the system encrypts something there should be a way to protect the encryption key (or the decryption key) and this, believe me, is a much, much harder problem.
In this article we will discuss the ways how one can hide secret information in:
- Windows environment
- Inside a software
- Inside a hardware
I have been doing research on this topic more than a year now and would like to share my knowledge and experience with you.
All the mentioned topics are heavy and one cannot describe all of them in a single article. I’m planning to have several articles describing different aspects of these themes (such as obfuscation and tamper-proofing in software, tamper resistance in hardware, etc).
So in this article we will discuss the “theoretical” part of the topic and then will go through an example and try to apply this knowledge in real life.
The Problem
The problem of hiding a cryptographic key in a system is a widespread problem and lot of research is going on over it worldwide. In general we can state that this problem is not solved. Whatever technique you are going to use to protect your information, be sure that it will be hacked over time. So engineers, instead of looking for an ideal solution, should look for protection techniques which will increase the time required for breaking their systems and thus gain business benefits.
Nowadays obfuscation is the only technique to protect secret information in an un-trusted environment. People often say (or assume by default) that putting a key in a hardware component is much more secure than implementing protection schemes in software itself however I don’t always agree with that opinion. If you think about it – putting a key in a hardware device is just another way of obfuscation… Yes, sometimes breaking hardware is more difficult than software, but it really depends on the protection schemes that have been used on both sides. I think people usually have this default opinion as most of them don’t have electrical/hardware engineering education/experience and that’s why they consider hardware related attacks to be much more difficult.
That is not true.
There are well known and easy attacks on hardware protections and most of the devices are prone to these attacks. I’m planning to discuss these kinds of attacks in one of my upcoming articles.
In my opinion there is only one big difference between these two systems which really makes sense. The difference is in fact that in case of hardware the system is able to physically remove the cryptographic key stored internally once it detected an active attack. For software systems this is not possible as you always can copy the software before starting attack on it. You can always put it under emulator while conducting an attack and in this case there is just no way for it to be able to delete the key. So you will be able to conduct the attack as many times as you want and if during one of these experiments one of your actions brings to deletion of the key – you can use the copy of software and continue experiments. In case of a hardware device – it’s little harder as you can’t physically copy the device and once the key is deleted there may be no way to restore the data back.
So, in general, hardware protection schemes are more reliable, but only if they are properly implemented. Software obfuscation and tamper proofing schemes have been significantly evolved in the past years and breaking these techniques is also considered to be a big challenge. For example the well known Skype software has been broken only after one year of its production which gave the developer company enough time to penetrate the market without letting the competitors to steal Skype protocol algorithm and make an illegal copy.
From other point of view there is no example of an unbroken software system while such examples exist for hardware. IBM 4758 device is a good example.
So let’s see what kind of software and hardware protection schemes exists currently in the industry. We will start discussing Windows, then go to general obfuscation and tamper proofing techniques in software and end with hardware based protection schemes.
Hiding cryptographic key in Windows
We will focus on Windows environment as I’m more handy with this operating system when it comes to security.
There is a strong opinion among the engineering community who are not experts in security that Windows operating system provides solid mechanism for protecting secret information in the system. That is not quite true. Windows does a great job by providing different mechanisms but it only does it in the scope of the resources it has access to. An ordinary laptop or a PC doesn’t possess a special device or a special hardware component for protecting your information… in fact what is has is just a hard disc and a RAM. So if Windows, by its definition, just cannot have a solid way for securely protecting your secret data.
However, as I said, it provides interesting opportunities, which, if properly used, can give a significant improvement for your application.
Account based protection
Windows provides API to encrypt data using Windows user account credentials under which the current application is running. CryptProtectData API receives a buffer as an input argument and returns the encrypted buffer as a result of successful operation. CryptUnprotectData API, in its turn, decrypts the encrypted data and returns it to the caller. Only applications which run under the same windows account are able to decrypt data.
From first point of view this is a very limited offering. However if your application is installed under administrator account then only limited applications would be able to access the data that you have encrypted.
Process based protection
I you want to protect your data in the context of a running process memory you can use CryptoProtectMemory and CryptoUnprotectMemory APIs. The first will encrypt the given buffer and return it and the second – decrypt and return.
Note that the buffer will be encrypted only in the context of the current process. If you run the same application again – the new process cannot decrypt it. These API are useful when you don’t want your in-memory data to be swapped to the hard disc.
Another technique for disallowing swapping is to mark the appropriate page as not swappable via VirtualLock API.
This is basically all that I’m aware of the mechanisms that Windows provides for protecting application specific data.
Hiding a cryptographic key inside software
If you don’t have a hardware component in your system – you are doomed to protect the secret information through obfuscation and tamper proofing.
Although there is a well formed opinion that whatever is obfuscated – is not secure, I wouldn’t completely agree with it. Currently there is no alternative to obfuscation. People who are saying that obfuscation should not be used need to invent better protection mechanisms before making such statements.
You can achieve a solid protection by properly using different techniques of obfuscation and tamper proofing but you also need to realize that it will have an impact on overall development, testing and deployment of the application. If the information you are going to protect is very valuable – I wouldn’t recommend using homegrown solutions. It’s better to integrate solutions form companies such as Arxan, Cloakware, Syncrosoft and put the security responsibility on them. Believe me these guys have a very good story for their secure technology and your customers will be definitely satisfied. However, most probably, you won’t be satisfied with their pricing…
Anyway, if you decide to design and implement a homegrown solution for obfuscation and tamper proofing there are many already known techniques that will help you to get to the approach which is the best for you.
Let’s outline what kind of techniques exists currently. We will just go over high-level descriptions of these techniques. I’m planning to dedicate a standalone article to these topics so there is no need to provide exhaustive explanation at this point.
Obfuscation
- Semantics-preserving transformations
- Complicating control flow
- Opaque predicates
- Data encodings
- Breaking abstractions
- Moving code around
- Encrypting code
Tamper-proofing techniques
- Checker Network
- Hiding hash values
- Response mechanisms
- Overlapping instructions
Hiding a cryptographic key inside hardware
If you are really targeting high-end security market you have to consider hardware level protection for your secret information. By saying hardware I mean a hardware device with non-volatile memory (such as OTP, ROM, EPROM, EEPROM, Flash, etc). However, as I said earlier, you will need to implement different complicated security measures in order to protect the data stored on this memory.
Remember, there are many attacks which are much easier to conduct for a medium trained electrical engineer on a bad designed hardware device rather than an expert software engineer on well obfuscated software.
Another problem related to hardware based protection is the deployment and adoption through users.
You can also achieve the goal of having an encryption key in hardware token without using internal memory in the device itself. You can have a hardware device which generates a unique, secure key on each boot so there is no need for a non-volatile memory. There are currently IPs available for this purpose with their proprietary technologies.
Many times during my experience I have heard statements like “well, we can utilize TPM to protect our cryptographic keys”. There are people, who are making such statements without really knowing what TPM is, and how and where you can use it. TPM won’t help you in protecting secret information. It’s only useful when used within the scope of a trusted stack. It can store your key securely however only if your application is a part of trusted booting chain. If it’s not – forget about TPM.

