I am working in designing of a prepaid energy meter. One of the most things I should store are (private key, credit and cumulative energy).

The private key is stored only once. The credit and cumulative energy are stored every few hours or when the power goes off (it is normal in my situation).

I was using a flash memory in my previous design and I discovered its disadvantages in such application. I got some memory corruption and deletion when writing while the d.c supply is going down to 0.

If I stored the credit in the EEPROM, any one can access it. The microcontroller (dsPIC33CH128MP508) don't come with internal one.

I have only few seconds (~2s) to store these data before the power supply dies. My clock is 4MHz and the dsPIC is 16bit. Therefore, if I need to encrypt the data in EEPROM it should not cost more than 2 seconds.

My question is: How to overcome the security issue when storing in external EEPROM in such applications?

  • 4
    \$\begingroup\$ Use a secure eeprom. \$\endgroup\$
    – Steve G
    Oct 15, 2019 at 12:20
  • 3
    \$\begingroup\$ @SteveG: Please do not answer the question in comments, as this bypasses the normal review process for answers, as discussed in meta. \$\endgroup\$
    – Dave Tweed
    Oct 15, 2019 at 12:33
  • \$\begingroup\$ @SteveG the price is a little bit high. when I am talking about producing more than 300k every year. \$\endgroup\$ Oct 15, 2019 at 12:49
  • 2
    \$\begingroup\$ Yes, the price for security is usually high. \$\endgroup\$ Oct 15, 2019 at 12:56
  • 1
    \$\begingroup\$ @SteveG -- lots of cringing over at the Microchip site... (1) the 2 links I tried from the cryptomemory page yielded 404s; (2) is data encrypted before sending over I2C bus? (OP didn't specify threat model, true); (3) site says "64 bit cryptographic algorithm" - ??? - what, like DES? Not criticizing SteveG, just saying that I've seen enough to pass on these chips. \$\endgroup\$
    – Radian
    Oct 15, 2019 at 15:37

2 Answers 2


There's a couple of things going on here:

  1. You have write operations that might be interrupted by a power loss.
  2. You have 2s of time before a loss of external power becomes a problem.
  3. you need to write a couple of bytes

That's basically not a problem – the time to write data should be within the micro- to lower milliseconds. State-of-the-art encryption for this would also not take seconds on a DSP microcontroller, so if this is an issue, you chose the wrong encryption algorithm, or your implementation is slow. (Hint: you need to sign your electricity counter data, not encrypt it it. That's often easier, since you then only need to encrypt a short, fixed length hash).

The question is whether storing confidential or sensitive data externally is a good choice - I don't know the dspic series well enough, but I'd assume there's variants of the same controller with integrated EEPROM that you can write to.

Also, if whatever you use to encrypt is worth bare money, never assume that anything isn't readable by an end user. That applies to your firmware too, so if you safe the user's private in external eeprom, any user can potentially use their own key to en- or decrypt anything.

  • 1
    \$\begingroup\$ Also, don't forget about the possibility of dumping the memory conntents and writing it back later. This can be attempted even if the encryption isn't broken - read the EEPROM, use the device for a while, write the old dump back verbatim. Using on-chip memory makes this much more difficult. \$\endgroup\$ Oct 16, 2019 at 2:59

A late response... Most encryption is simply an xor with a pseudo rando number so basically useless for only a few bytes. Your choice of controller matter in this case. ARM with TrustZone is where I would be thinking...Add a secure bootloader to prevent malicious code. Something like a Microchip SAM L11 for example (I am not an employee of Microchip). Use a page (or 32 pages) of flash to store multiple copies of the credit and cumulative energy as you go to prevent wear AND to provide a history.

NOTE: a 2k page written with 8 bytes every 2 second, 10,000 cycles endurance, will last about 59 days, so maybe save in RAM with good power fail detection write every 120 seconds instead.
Alternatively, a L10 draws only 4uA max in standby.. so a coin cell and RAM retention.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.