HMAC Authentication - how to secure private keys in flash? - Page 1
Forum section: electronics / general

author: mikerj
author_rank: Super Contributor
timestamp: November 06, 2014, 10:29:18 am
content: Is there a method of securing private keys (or any secure data come to that) that need to be stored within, say, flash memory that could be accessible via e.g. debug port or bootloader?There is a mechanism for protecting the memory against reads through the debug port, but I don't know how secure/reliable this is. The device has a bootloader built in, and the application has a bootloader, either of which could be vulnerable to exploits, so storing the keys as plain text is a bad idea. I'm assuming some kind of encryption would be needed, but that leads to the problem of storing the encryption key itself. The application must be able to gain access to these keys at any time without requiring any kind of external password to be sent.I've spent quite a long time googling this, but I'm not really getting anywhere. As always, I might just need to know a phrase to search for and the answer will be obvious.

author: Kjelt
author_rank: Super Contributor
timestamp: November 06, 2014, 11:19:02 am
content: If the internal flash memory can be read out or allow 3rd party to debug than there is no way of making the described situation secure unless you use a dedicated secure microcontroller with its own keylocker.So step 1 will be to make sure there is no way an attacker can read out (part of) your flash memory (unless through great expense like opening the chip).Step 2 is that an attacker is not allowed to run his own code or debug your code (simply set a breakpoint after the decryption and read the key from RAM).Without those steps you can stop, its no use.If you have these steps than there are some methods of obfuscating the keys. Plainly encrypting them is ok but relative easy to hack because as you said there has to be some key somewhere. You can use (part of) the unique ID of the device to make the key only usefull on that device.You can also use a simple form of whiteboxing (hide the key in a big block of random noise, extractable through a secret algorithm) but to make it very effective it needs large amounts of storage, too large for an embedded device but it is better than nothing I guess.I guess you have to start thinking about security from the getgo to make it effective. Making an implementation secure afterwards is sometimes impossible or very hard.

author: mikerj
author_rank: Super Contributor
timestamp: November 06, 2014, 12:13:08 pm
content: Quote from: Kjelt on November 06, 2014, 11:19:02 amThanks for the reply. This reinforces my suspicions that this is pretty much a non-starter. The hardware is already defined and implemented as is the application upgrade protocol which includes a basic level of security, but was primarily designed to ensure the firmware image is not corrupted rather than for authenticity.This is a last second feature request from a customer. I've never had similar requests for any other customer, and parts of the hardware would be extraordinarily difficult and very expensive to clone (it uses in-house designed and manufactured semiconductor and optical devices) so the risk of this happening is negligible IMO. If a company could clone these parts, then they would have little problem working their way around any half-assed security that could be added at this stage.

author: jeremy
author_rank: Super Contributor
timestamp: November 06, 2014, 12:38:32 pm
content: I am by no means an expert, but all effective key storage I have ever seen relied on PUC (physically unclonable circuits) or write only memory (to the host, it was on-die RAM for the tiny shielded crypto coprocessor). I'm pretty sure this "write only memory" approach is the way Apple does it in their iOS devices.

author: Jeroen3
author_rank: Super Contributor
timestamp: November 06, 2014, 12:50:36 pm
content: Assuming you're using regular arm chips running from flash you could do the following:OTP out the JTAG interface, goodluck working around that.MPU the entire RAM to no-execute, don't include a bootloader or MPU to no-execute the bootloader in the startup file conditions are not met.Don't use a "pull pin low to enter bootloader" method, have software jump to a special routine (after receiving a hash from your unique key for example), to reset the chip and set conditions in the backup registers for the bootloader. Make sure the binary you provide is encrypted, scrambled and/or signed.Or get a chip with a write-only AES key OTP area in the crypto peripheral. These chips are usually capable of running from an encrypted binary.You might experience customs with these chips.

author: Kjelt
author_rank: Super Contributor
timestamp: November 06, 2014, 01:01:21 pm
content: You might think about writing a firmware update bootloader that itself can not be updated (so should be tested thoroughly or you have a brick device in the future), protecting that flash page (where the bootloader resides) and only allowing updates to be stored first in external flash and let the bootloader verify the cryptographic secure authenticity of that firmware (so it is cryptographically authenticated and preferred encrypted so it is tough to see what a valid firmware looks like) before doing the update. The attacker should hack the bootloader to be able to retrieve the key and create its own firmware update. But as said there are always ways around that.

author: madires
author_rank: Super Contributor
timestamp: November 06, 2014, 02:16:53 pm
content: Another idea is to obfuscate or encrypt the private key. With encryption the user might have to enter a password/secret to enable the key.

author: amyk
author_rank: Super Contributor
timestamp: November 06, 2014, 02:40:08 pm
content: How secure do you want it to be? There's always a way to crack it.https://www.eevblog.com/forum/chat/hacking-ic's-and-pcb's-for-crooks/

author: Jeroen3
author_rank: Super Contributor
timestamp: November 06, 2014, 02:58:57 pm
content: As the rule of thumb for most computer systems dictates: physical access is full access.Another approach: put the key in the memory protected by the TAMPER pins and memory battery.If housing is opened or memory battery/capacitor is drained, the key is erased.

author: mikerj
author_rank: Super Contributor
timestamp: November 06, 2014, 03:42:16 pm
content: No memory backup battery or external storage is present. There are no special security features within the (ARM Cortex) micro other than the usual read/write memory protection and the ability to disable reads via the debug port. I can't rely on an external password to decrypt anything stored within the micro since this isn't part of the authentication protocol which has been defined. I may be able to enhance security of the application bootloader, but the bootloader in the micro's ROM obviously can't be modified.Obfuscation (e.g. spread the keys throughout the memory rather than all in one place) and/or a basic level of encryption e.g. using the serial number of the device as a key is doable, but provides only a small level of protection.From what I can see the customer has two options; I can either implement the HMAC, but it won't be very secure, or they can drop the requirement.I'm kind of pleased there wasn't some blatantly obvious solution that I'd overlooked though.

author: nctnico
author_rank: Super Contributor
timestamp: November 06, 2014, 04:21:16 pm
content: Quote from: mikerj on November 06, 2014, 10:29:18 amUsually that is reliable enough to need fancy $$$ equipment like on-die probing to circumvent the security measures (on modern chips that is).

author: Jeroen3
author_rank: Super Contributor
timestamp: November 07, 2014, 06:35:41 am
content: QuoteYou can run your own bootloader, it's just regular code without special magic, no need to use the rom version.

author: mikerj
author_rank: Super Contributor
timestamp: November 07, 2014, 08:27:23 am
content: Quote from: Jeroen3 on November 07, 2014, 06:35:41 amWe do use our own bootloader, but that doesn't stop the one in ROM being invoked if one of the micro pins is held at the correct level during reset.

author: nctnico
author_rank: Super Contributor
timestamp: November 07, 2014, 02:51:58 pm
content: If the ROM bootloader offers security against reading then you should be OK. I don't see the problem here.

author: Jeroen3
author_rank: Super Contributor
timestamp: November 07, 2014, 07:03:32 pm
content: Depending on the brand, there is either a OTP bit disabling any boot-time (of full) access to the bootloader.Or there is a magic pattern that needs to be put at an arbitrary location in flash.If both are unavailable, you should seriously reconsider the choice of chip. These are basic protection mechanisms.

author: Skashkash
author_rank: Regular Contributor
timestamp: November 08, 2014, 01:16:17 pm
content: If the hardware design is completely done, then your options are limited.If you make a small change, you could add an external authenticator chip.They are small (sot-23) , and cheap. I2C or a single wire interface. Does HMAC and SHA256.http://www.atmel.com/Images/Atmel-8885-CryptoAuth-ATSHA204A-Datasheet.pdf

author: Kjelt
author_rank: Super Contributor
timestamp: November 08, 2014, 01:28:27 pm
content: Quote from: Skashkash on November 08, 2014, 01:16:17 pmAFAIK this is useless if the internal firmware of the microcontroller is not secure. If this chip would be integratedINthe microcontroller itself it would be an excellent security measure but now the secret keys are in the open in the internal firmware, so what is the point of storing another key or data on an external protected device?

author: Skashkash
author_rank: Regular Contributor
timestamp: November 08, 2014, 01:38:16 pm
content: The internal keys of the atsha204 are never exposed once the chip is locked.You just use it to confirm the MAC to be sure the 32byte master key is the same.You can do other operations too, like diversify the keys or secure the boot process.http://www.atmel.com/Images/doc8753.pdf

author: Kjelt
author_rank: Super Contributor
timestamp: November 08, 2014, 01:44:40 pm
content: Again if a hacker has access to the internal firmware of the microcontroller he can simply modify anything he wants. Such as in the "secured" boot process:QuoteAnd the processor just accepts any answer from the device and continues.The whole point of a secure boot process is a chain of trust that starts with an uncompromised starting point, in a PC this is the BIOS, in an embedded device it is the bootstrap then the bootloader. In this particular case the TS can not guarantee the validity of the bootloader, a hacker has acces to debug it, to change it so it can be any program.It is already hopeless from that point onwards.

author: Skashkash
author_rank: Regular Contributor
timestamp: November 08, 2014, 02:22:34 pm
content: Just linked the secure boot pdf to show that the chip could be used for other purposes.I thought the OP just wanted to secure the keys used for the MAC/HMAC. As in prevent them from being copied. The atsha204 would do that, at the expense of some additional hardware and bandwidth (it's slow).I agree, if somebody has physical access to an unsecured micro, then they can pretty much bypass anything.So, if the OP can't make any hardware changes, I don't think they can really secure the keys or the system.

author: mikerj
author_rank: Super Contributor
timestamp: November 09, 2014, 11:19:20 am
content: Quote from: Skashkash on November 08, 2014, 02:22:34 pmThe Atmel device looks to bealmostperfect. Speed is not important within reason, there is handshaking between our device and the host system so it waits until the HMAC operation has completed, and it's only done once after power on/reset. Even if it took a several seconds it would be fine.I statealmostsince the secure hash I require is SHA1 (not very secure any more apparently) which isn't supported by the Atmel devices. That is a shame because it otherwise seems to be a good fit, and is small enough that a future respin could be considered (space on the PCB is tight, we are using 0201 passives and UCSP packages extensively).

author: Kjelt
author_rank: Super Contributor
timestamp: November 09, 2014, 11:45:38 am
content: Then please enlighten me what is your future security scenario with this device because you said that you could not protect the internal firmware?SHA-1 has been broken for over 9,5 years. I am not sure what you think you are doing but apparently you are not up to date with current security requirements, and let me assure you that security is one thing you have to do as good as you can get it, or you can just as easily not do it at all.https://www.schneier.com/blog/archives/2005/02/sha1_broken.html

author: gmb42
author_rank: Frequent Contributor
timestamp: November 09, 2014, 12:00:26 pm
content: Quote from: Kjelt on November 09, 2014, 11:45:38 amNot to invalidate your general point, but as noted in the blog entry you referred to,Quotewhich I believe is the OP's intended use.

author: ovnr
author_rank: Frequent Contributor
timestamp: November 09, 2014, 12:30:54 pm
content: Quote from: Kjelt on November 09, 2014, 11:45:38 am"Broken" in this instance doesn't imply "trivially broken", just "not as secure as it could be".Let me quote Wikipedia:QuoteSo, you know, unless someone's willing to spend a million bucks or so on the problem, it'snot a big deal.Back to the original topic: How secure does this need to be against intrusions? I'd consider finding a good potting compound. It won't stop a determined attacker - nothing will - but it is better than nothing.

author: Kjelt
author_rank: Super Contributor
timestamp: November 09, 2014, 01:50:24 pm
content: Quote from: gmb42 on November 09, 2014, 12:00:26 pmYes you are absolutely right. As HMAC it is still NIST recommended till 2030 (or unless some other attack will take place) see link below.Quote from: ovnr on November 09, 2014, 12:30:54 pmYes in that point you are also right. I am only allowed to use the cryptographic functions that will be NIST recommended secure till 2030+ for new products. That is why I reacted so strongly I often see people use old obsolete ciphers because they had some open source software and keep on using it.But for this particular use as HMAC it is still recommended.http://www.keylength.com/en/4/

author: Bored@Work
author_rank: Super Contributor
timestamp: November 09, 2014, 03:00:03 pm
content: Quote from: Kjelt on November 09, 2014, 01:50:24 pmWell NIST. Yes. The US agency compromised by the NSA when it comes to security. Very clever to insist on NIST-recommended algorithms if you want to make sure the US has easy access.

author: nctnico
author_rank: Super Contributor
timestamp: November 09, 2014, 03:07:06 pm
content: Actually the point of using broken or non-broken encryption should not be an issue. Good security builds on three pillars:- Authentification- Authorisation- AccountingFor example: in the NL they used Mifare cards for the public transport where the account balance is kept on the card in a non-encrypted way. Since Mifare has been cracked it is extremely easy to increase the balance yourself. Or isn't it? The authentification and authorisation are clearly broken in this system but the accounting part (which keeps track on which card travels where) allows to pin point people trying to scam the system and arrest them.

author: ovnr
author_rank: Frequent Contributor
timestamp: November 09, 2014, 04:35:04 pm
content: Quote from: nctnico on November 09, 2014, 03:07:06 pmReasonably easy to deal with: Devices that will fill up everyone's card by small amounts every time they're in range, placed at strategic points (close to the proper reader, etc - you can boost the NFC gain a bit). That way everyone's guilty all of a sudden.

author: mikerj
author_rank: Super Contributor
timestamp: November 09, 2014, 05:07:13 pm
content: Quote from: Kjelt on November 09, 2014, 11:45:38 amI can't, at least not the degree required. My understanding is that the Atmel device IS secure, holds the private keys and implements the HMAC. All our device would need to do it pass the authentication message +key number from the host to the Atmel device, and then read the the message digest back and pass it to the host. This means no secure comms are required between our micro and the Atmel device, and no storage of private is required within out micro.Are you enlightened, or have I misunderstood the operation of the Atmel device?Just for context, this security isn't going to have any significant consequences in the unlikely event that someone feels making the effort of breaking it which is why I think it's simply unnecessary (as does everyone working on the project). As always however, the customer is always right, especially when they're wrong.

author: Jeroen3
author_rank: Super Contributor
timestamp: November 09, 2014, 05:12:47 pm
content: Look at physical locks for example. You know, those in your doors.They are classified in the number of minutes a skilled lock picker needs to pick the lock. Almost all locks can be picked, except those few that are very expensive.And the weakest part of the lock system is people who lose their keys, or don't lock the door at all.You need to find a balance in the security you need vs which you can afford.

author: mikerj
author_rank: Super Contributor
timestamp: November 09, 2014, 05:16:11 pm
content: Quote from: Jeroen3 on November 09, 2014, 05:12:47 pmWe neither need, nor want this security on our device. It's a potential customer being a pain in the ass.

author: Kjelt
author_rank: Super Contributor
timestamp: November 09, 2014, 07:04:05 pm
content: In that case just do whatever meets the customer requirements and if it is usefull, if it is secure or not, so be it.

author: marshallh
author_rank: Unknown
timestamp: November 10, 2014, 02:47:04 am
content: Check out these to get some ideas. Without going into more crypto, the easy solution is to obfuscate the key as much as possible. You can also store up to 16 such keys in the external device and increment the key index in a new firmware update if a previous one was leakedhttp://www.atmel.com/Images/doc8753.pdfhttp://www.atmel.com/Images/doc8666.pdf