git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 4/7] hash: make git_hash_discard() idempotent

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jul 7, 2026, 21:41 UTC
Message-ID
<ak1yazHtP_OazDaO@fruit.crustytoothpaste.net>
In-Reply-To
<20260707201808.GD11780@coredump.intra.peff.net>
On 2026-07-07 at 20:18:08, Jeff King wrote:
Show 9 quoted lines
> On Tue, Jul 07, 2026 at 09:22:04AM -0700, Junio C Hamano wrote:
> > But the change nevertheless looks mostly good except for one "hmph".
> > When _init() is called, active gets turned on automatically, and
> > either _discard() or _final() turns it off.  Only _discard() is
> > protected from getting called multiple times.  Is this because
> > it is already a no-op to call _final() multiple times?
> 
> No, it's a bug to call _final() multiple times. See my response
> elsewhere in the thread.

This is almost always the case in hash function libraries. Let me explain why.

A context for SHA-256 contains the 8 32-bit words in the state, a bit or byte counter (as a 64-bit quantity or two 32-bit quantities), and a 64-byte buffer for unprocessed bytes—and that's it. When finalizing a hash, you must always pad with a 0x80 byte and then optionally some zero bytes, plus a 64-bit counter of bits in the message. That may result in one or two iterations of the hash to process the remaining bytes and the padding, and that almost always updates the state words in the context in place. (SHA-1 functions identically but for the state size.)

So if you call the final function multiple times, you're not computing the final value the second time, but instead trying to re-pad and re-compute the final hash value, which results in a _different_, incorrect value. In SHA-256, this is a valid hash value for a different message (which is the original message with the padding and length tacked on and is effectively a length-extension attack), but in hashes that don't allow length-extension attacks, such as SHA-3 and BLAKE2, what you get is simply corrupt data.

So most hash function libraries that allocate memory are going to free it in the final function because you can't really call final multiple times and get a sensible response. If you want to do that, then you need to clone the context and call final on each context once.

Our Rust code makes calling final a second time impossible because finalization takes `self`, not `&mut self`, so the object is _moved_ into the final method and you no longer have access to it after that.

-- 
brian m. carlson (they/them)
Toronto, Ontario, CA
Previous: Jeff KingNext: Junio C Hamano
Message 17 of 35 in “git_hash_*() quality-of-life improvements”
  1. 0/7 git_hash_*() quality-of-life improvementsJeff King, Jul 7, 2026
  2. 1/7 hash: use git_hash_init() consistentlyJeff King, Jul 7, 2026
  3. Junio C HamanoJul 7, 2026
  4. Jeff KingJul 7, 2026
  5. Junio C HamanoJul 7, 2026
  6. Jeff KingJul 7, 2026
  7. brian m. carlsonJul 7, 2026
  8. Jeff KingJul 8, 2026
  9. 2/7 hash: convert remaining direct function callsJeff King, Jul 7, 2026
  10. Junio C HamanoJul 7, 2026
  11. 3/7 hash: document function pointers and wrappersJeff King, Jul 7, 2026
  12. Patrick SteinhardtJul 7, 2026
  13. Jeff KingJul 7, 2026
  14. 4/7 hash: make git_hash_discard() idempotentJeff King, Jul 7, 2026
  15. Junio C HamanoJul 7, 2026
  16. Jeff KingJul 7, 2026
  17. brian m. carlsonJul 7, 2026
  18. Junio C HamanoJul 7, 2026
  19. 5/7 csum-file: use idempotent git_hash_discard()Jeff King, Jul 7, 2026
  20. 6/7 http: use idempotent git_hash_discard()Jeff King, Jul 7, 2026
  21. Junio C HamanoJul 7, 2026
  22. 7/7 hash: check ctx->active flag in all wrapper functionsJeff King, Jul 7, 2026
  23. Patrick SteinhardtJul 7, 2026
  24. Junio C HamanoJul 7, 2026
  25. Jeff KingJul 7, 2026
  26. Patrick SteinhardtJul 7, 2026
  27. 0/7 git_hash_*() quality-of-life improvementsJeff King, Jul 8, 2026
  28. 1/7 hash: use git_hash_init() consistentlyJeff King, Jul 8, 2026
  29. 2/7 hash: convert remaining direct function callsJeff King, Jul 8, 2026
  30. 3/7 hash: document function pointers and wrappersJeff King, Jul 8, 2026
  31. 4/7 hash: make git_hash_discard() idempotentJeff King, Jul 8, 2026
  32. 5/7 csum-file: use idempotent git_hash_discard()Jeff King, Jul 8, 2026
  33. 6/7 http: use idempotent git_hash_discard()Jeff King, Jul 8, 2026
  34. 7/7 hash: check ctx->active flag in all wrapper functionsJeff King, Jul 8, 2026
  35. Patrick SteinhardtJul 8, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.