Over a million developers have joined DZone.

Preventing Reverse Engineering: Enabling Flash Security

· IoT Zone

Now I have invested a lot of time into my application, ready to be flashed on the devices and shipped. But wait: I don’t want that someone can read out the code from my device and have it reverse engineered. For this, I can ‘secure’ the device.

Flash Security Settings

Flash Security Settings

There are several ways to prevent unauthorized access with a debugger to the device:

  1. Disabling the debug interface: this is a rather weak method, as there might be still a small time slot after reset of the device until the device has disabled the debug interface. And if the debugger is fast enough, it might stop the target before it disables the debug interface. But this method in combination with other methods is still very valuable.
  2. Enabling Flash Security: this is typically a setting which prevents the debug block on the chip to access the flash memory. You only can regain access to the device if a complete erase of the flash memory is done first. This still protects the code on the target (it gets erased), and allows to regain access to the device during the development process.
  3. Disabling FLASH Mass Erase. Only having the Flash Security set still would allow a person to gain access to the device and program it with a different (malicious) firmware. To prevent this, the mass erase can be disabled. So disabling flash mass erasing in combination with the security setting is a one way thing: once set, there is no way back to regain access to the device, except some backdoor has been implemented.

Accidentally setting the wrong bits can be very bad, as outlined in “How (not) to Secure my Microcontroller“. Every microcontroller has some specific bits and settings which need to be set, and different ways how to implement a back door to regain access. Typically it is best if the backdoor is implemented through another hidden channel, e.g. with an encrypted password sent over USB or RS-232.

I show in this example how to enable the security for the Freescale KL25Z which is an ARM Cortex-M0+, using Processor Expert. The setting is in the CPU component. I enable the ‘Flash security’ option, and as explained above, I keep ‘Mass erase’ enabled:

Enabled Flash Security

Enabled Flash Security

After downloading such an application to the microcontroller will prevent that I can debug it: the P&E interface tells me that the device is secure:

Device is Secure

Device is Secure

I only get access to the device again with erasing the device with a new binary, and because Mass Erase is *not* disabled.

If somehow that does not work, there is an option in the P&E GDB panel which does an ‘early’ mass erase:

Always Mass Erase on Connect

Always Mass Erase on Connect

:idea: I do not have a board to spare now, so I do not show here an example with the Mass Erase disabled. ;-)

With Segger J-Link, I get a warning:

Segger Security Warning

Segger Security Warning

:idea: That dialog might be hidden behind Eclipse, so make sure you move that dialog to the foreground.

So the Segger J-Link will automatically disable the security bit to protect me from doing a mistake. To bypass that safety check, I need to specify the device with “(allow security)”:

Segger J-Link Device Name to Allow Security Bit Programming

Segger J-Link Device Name to Allow Security Bit Programming

But just to be clear: with this I need to make sure that I do not accidentally brick my board!


Enabling the Flash Security setting will prevent that someone is able to read out the (assembly/binary) code from the device for reverse engineering. Segger has a protection built in to prevent me doing a mistake. If mass erase is still enabled, I can erase the memory and get access to my device again. All in all, this is a very handy feature, but like a sharp knife: used with care ;-)

Happy Securing :-)


Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}