1
$\begingroup$

Why is it that most implementations define an sha256_init, sha256_update, and sha256_final function that each operates on a SHA256_CTX object? The specification only mentions returning an almost unique hash for any given string of bits of any given length.

  • What is the reason to have these 3 functions instead of just a standalone function that returns an output hash for an input bitstring?
  • What exactly does CTX stand for?
$\endgroup$
3
  • $\begingroup$ CTX is more implementation specific. Contexts are often used in non-object oriented programming languages to keep state. A similar construction including a context is used for symmetric encryption modes. $\endgroup$
    – Maarten Bodewes
    Commented Dec 20, 2022 at 20:31
  • $\begingroup$ CTX is a programming term, not necessarily a cryptographic one. $\endgroup$
    – forest
    Commented Dec 20, 2022 at 22:16
  • $\begingroup$ mostly dupe crypto.stackexchange.com/questions/52136/… $\endgroup$ Commented Dec 22, 2022 at 1:47

1 Answer 1

6
$\begingroup$

What is the reason to have these 3 functions instead of just a standalone function that returns an output hash for an input bitstring?

This allows to compute the hash of a piece of information that is made available by segments, rather than as a single memory block. This comes handy in many situations, including

  • When the data is too large to fit memory (e.g. hash of a large file)
  • When the data to hash is the content of many network packets (the alternative would require a memory buffer as large as the sum of the length of the packets, and a copy of each packet into that, which is time consuming and kills the cache).
  • When it's desired to perform activities at time intervals smaller than the duration of the whole hash, in a single-thread context. We break the data into pieces, and perform the activity between the hash of individual pieces.
  • When several hashes must be computed concurrently (e.g. multiple users of a server with less threads than users).

What exactly does CTX stand for?

Context. It holds the current state of the hash being computed, typically the 32 bytes of the current chaining value, a number (typically at most 64) of messages bytes not hashed yet, and the length of the data so far (typically on 8 bytes). A typical SHA-256 context is thus 104 bytes, or slightly more.

$\endgroup$

Your Answer

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