3

I currently need to encrypt large files (video, over 6 Gbytes) for our customers. To ensure authenticity and integrity, we chose HMAC-256 as we already use it in our internal messaging system.

But working in embedded systems, we are limitted by the memory. If the message is the whole data file, we could not stored 6Gb in memory to compute the HMAC.

Is there a dedicated protocol that breaks up the message into smaller blocks and create iterative HMAC ? Or any other way to achieve it ?

Mike Edward Moras
  • 18,161
  • 12
  • 87
  • 240
Zyend
  • 155
  • 2
  • 7

1 Answers1

6

A SHA-256 implementation usable on several blocks can be turned it into an HMAC-SHA-256 implementation usable on several blocks, as follows:

  1. If the key is larger than 64 bytes, replace it by its 32-byte SHA-256 hash; now the key is at most 64-byte long.
  2. Start a SHA-256 hash.
  3. Set a 64-bytes buffer to all 0x36; XOR the key into that buffer (leaving unchanged any byte past the key length); hash that buffer.
  4. Hash the blocks of data to authenticate (as several blocks if necessary).
  5. Finalize the hash of steps 2/3/4, giving the 32-byte T.
  6. Start another SHA-256 hash.
  7. Set a 64-bytes buffer to all 0x5c; XOR the key into that buffer (leaving unchanged any byte past the key length); hash that buffer.
  8. Hash the 32-byte T.
  9. Finalize the hash of steps 6/7/8, giving the desired 32-byte result.

That simply applies the definition of HMAC.

fgrieu
  • 149,326
  • 13
  • 324
  • 622