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 ?

  • 4
    $\begingroup$ It is quite usual that an SHA-256 or HMAC-SHA-256 implementation allow to process data by small blocks, much smaller than available memory. In fact, any big block is internally spliced into 64-byte blocks. $\endgroup$
    – fgrieu
    Oct 21 '15 at 15:53
  • $\begingroup$ @fgrieu: I already used SHA256 to compute large ciphertext digest, as the algorithm is clearly iterative. However, I can't figure out how to do the same thing with the HMAC. $\endgroup$
    – Zyend
    Oct 21 '15 at 18:25
  • $\begingroup$ HMAC should just call the hash algorithm internally, if it tries to load all the data, the implementation is flawed, and there may be other issues to look for in the code $\endgroup$ Oct 21 '15 at 19:04
  • $\begingroup$ @RichieFrame I do agree with you. But in the HMAC algo, there is no mention about this use case. I mean, it appears it's up to the developer to implement its own protocol. Would it be acceptable to compute a HMAC-SHA256 with message=SHA256(cipherdata) ? $\endgroup$
    – Zyend
    Oct 21 '15 at 19:25
  • $\begingroup$ Yes you can do that, it will not be "official", but it will perform the same function and give the same level of integrity. If you have a 64-bit CPU, using SHA-512 will be much faster $\endgroup$ Oct 22 '15 at 1:20

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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