Information Technology is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Cryptographic hash functions and SHA-256

Clear, exam-focused notes on cryptographic hash functions and SHA-256: definitions, properties (preimage/second-preimage/collision resistance), Merkle–Damgård, SHA-256 algorithm outline with formulas, worked examples (including SHA-256("abc")), common student mistakes and exam guidance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Cryptographic hash functions provide fixed-size fingerprints of arbitrary data; engineers use them in blockchain for block identifiers, Merkle trees, proof-of-work, transaction IDs and integrity checks. SHA-256 is the standard hash used by Bitcoin and many systems — understanding its design, security limits and how padding and message scheduling work is essential for correct protocol implementation and for reasoning about attacks (collisions, preimage, length-extension).

Key ideas

Start with definitions (each bolded term is defined when first used):

  • Cryptographic hash function: a deterministic function H that maps a binary string of arbitrary finite length to a fixed-length binary string (the digest). Common notation: H: {0,1}* → {0,1}^n. Assumption: H is efficiently computable for any input.

  • Digest (or hash): the fixed-length output H(m), usually represented in bits or hex.

  • Preimage resistance (one-way): Given a digest y in {0,1}^n, it should be computationally infeasible to find any input m such that H(m) = y. Formal claim often used in exams: effort ≈ 2^n operations for an ideal n-bit hash.

  • Second-preimage resistance: Given m1, it should be infeasible to find m2 ≠ m1 with H(m2) = H(m1). For an ideal n-bit hash, effort ≈ 2^n.

  • Collision resistance: It should be infeasible to find any pair m1 ≠ m2 with H(m1) = H(m2). By the birthday paradox, effort ≈ 2^(n/2) for an ideal n-bit hash.

  • Avalanche effect: A small change in input should flip about half the output bits unpredictably.

  • Compression function: A fixed-input-length function f: {0,1}^n × {0,1}^b → {0,1}^n that compresses a chaining state (n bits) and a block (b bits) to a new chaining state; building block of many hash designs.

  • Merkle–Damgård construction: A method to build H from a compression function f. Steps (what to draw): draw a sequence of blocks m1, m2, ..., mk feeding into boxes labeled f with arrows; initial box input is IV (initial value); final box output is H(m). Label block size b (for SHA-256, b = 512) and chaining value size n (for SHA-256, n = 256). Include padding bits appended before the final 64-bit length field (MD strengthening).

    Exact statement used in universities: If the compression function f is collision-resistant and padding is injective on block boundaries, then the Merkle–Damgård hash function H built from f is collision-resistant. Assumptions: padding must prevent two different full-length messages producing identical block sequences; proof sketch: any collision in H gives a collision in f by looking at the first index from the left where chaining values differ.

  • Length-extension attack: For Merkle–Damgård hashes, given H(m) and the length of m, an attacker can compute H(m || pad(m) || m2) for chosen m2 without knowing m; consequence: naive H(secret || message) is insecure — instead use HMAC.

SHA-256 specifics (design principles):

  • SHA-256 is an instance of the Merkle–Damgård family with output length n = 256 bits, block size b = 512 bits, and internal word size w = 32 bits.

  • It uses an initial IV (eight 32-bit words) and 64 round constants K[0..63] (each 32 bits). The constants are derived from fractional parts of cube roots of the first 64 prime numbers; the IV uses fractional parts of square roots of first 8 primes.

  • Core functions (define exactly):

    • Ch(x,y,z) = (x AND y) XOR ((NOT x) AND z)
    • Maj(x,y,z) = (x AND y) XOR (x AND z) XOR (y AND z)
    • Σ0(x) = ROTR^2(x) XOR ROTR^13(x) XOR ROTR^22(x)
    • Σ1(x) = ROTR^6(x) XOR ROTR^11(x) XOR ROTR^25(x)
    • σ0(x) = ROTR^7(x) XOR ROTR^18(x) XOR SHR^3(x)
    • σ1(x) = ROTR^17(x) XOR ROTR^19(x) XOR SHR^10(x)

    Where ROTR^r is bitwise rotate right by r positions, and SHR^s is logical shift right by s positions.

Algorithm outline (high level exam style):

  1. Preprocess (padding): append a single '1' bit, then k '0' bits so the length ≡ 448 (mod 512), then append 64-bit big-endian integer of original message length in bits. Result length is multiple of 512.
  2. Parse padded message into 512-bit blocks; each block split into sixteen 32-bit big-endian words W0..W15.
  3. Message schedule: extend to W0..W63 where for t = 16..63, Wt = σ1(Wt-2) + Wt-7 + σ0(Wt-15) + Wt-16 (mod 2^32).
  4. Initialize working variables a..h to IV[0..7].
  5. For t = 0..63: T1 = h + Σ1(e) + Ch(e,f,g) + K[t] + W[t]; T2 = Σ0(a) + Maj(a,b,c); then h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2 (all mod 2^32).
  6. After processing all blocks, add working variables to IV (component-wise mod 2^32); concatenation of these eight 32-bit words is the final 256-bit digest.

Security model and limits: SHA-256 is considered collision-resistant against classical attackers at roughly 2^128 cost (birthday), preimage cost ≈ 2^256. However, quantum attacks reduce preimage to about 2^(n/2) by Grover’s algorithm (for SHA-256 ≈ 2^128), and collisions to about 2^(n/3) time and memory by the Brassard–Høyer–Tapp algorithm (for SHA-256 ≈ 2^85) — but exact quantum trade-offs are more nuanced. For exam answers, state classical costs unless quantum explicitly asked.

Formulas

H: {0,1}* -> {0,1}^n — H maps arbitrary-length bitstrings to n-bit digests; n is output length in bits.

work_factor_preimage ≈ 2^n — expected operations to find a preimage for an ideal n-bit hash.

work_factor_collision ≈ 2^(n/2) — expected operations to find any collision (birthday attack).

Block size (SHA-256): b = 512 bits — block length processed per compression call.

Word size (SHA-256): w = 32 bits — internal word for operations.

Message schedule: W[t] = σ1(W[t-2]) + W[t-7] + σ0(W[t-15]) + W[t-16] (mod 2^32) — for t = 16..63.

Round updates: T1 = h + Σ1(e) + Ch(e,f,g) + K[t] + W[t] (mod 2^32)

T2 = Σ0(a) + Maj(a,b,c) (mod 2^32)

a = T1 + T2 (mod 2^32) — new a after a round; other variables rotated as in algorithm.

Padding length rule: final 64 bits = L where L is original message length in bits, encoded as a 64-bit big-endian integer.

Definitions of rotations/shifts used (for 32-bit x):

ROTR^r(x) = (x >>> r) OR (x << (32 - r)) (bitwise rotate right)

SHR^s(x) = x >> s (logical shift right)

Explain symbols: + is addition modulo 2^32, XOR is bitwise exclusive-or, AND is bitwise and, NOT is bitwise complement.

Worked examples

Example 1 — Padding and block count (numeric)

Given ASCII message m = "abc". ASCII bytes: 0x61 0x62 0x63 → 3 bytes = 24 bits. Step 1: original length L = 3 * 8 = 24 (bits). Step 2: append '1' bit then k zeros so (L + 1 + k) ≡ 448 (mod 512). Compute required total before length: 448 bits. Currently L = 24, so need 448 - (24 + 1) = 423 zeros. k = 423. Step 3: append 64-bit big-endian representation of 24: 0x0000000000000018. Total padded length = 24 + 1 + 423 + 64 = 512 bits → exactly one block. Answer: number of 512-bit blocks = 1.

Example 2 — SHA-256 of "abc" (model-answer style, give known result and show where it comes from)

You are not expected to compute all 64 rounds by hand in an exam. Show padding and state initial IVs, then quote final digest. Steps:

  1. Pad message as in Example 1 — one 512-bit block.
  2. Initialize a..h to IV values (hex): IV = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19].
  3. Run message schedule and 64 rounds — standard implementation does this. Final digest (hex, lowercase) is: ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad. Give this as the expected final answer: SHA-256("abc") = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad.

Example 3 — Toy compression/tracing (small algorithmic trace)

To illustrate one round of compression with small word size, define toy hash with 8-bit words and two rounds. Let a=0x01, b=0x23, c=0x45, d=0x67, e=0x89, f=0xab, g=0xcd, h=0xef, W0=0x10, K0=0x20. Define Σ1(e) = ROTR^2(e) XOR ROTR^3(e) (toy), Ch(e,f,g) = (e AND f) XOR ((NOT e) AND g). Compute ROTRs on 8 bits and follow T1 = h + Σ1(e) + Ch(e,f,g) + K0 + W0 (mod 256). Do the arithmetic step-by-step showing hex results. (Reviewer: this is an exercise; include detailed arithmetic when using in teaching.)

Common mistakes

  • Forgetting SHA-256 uses big-endian word order — byte order errors in W0..W15 are common.
  • Assuming Merkle–Damgård resists length extension; actually it is vulnerable.
  • Forgetting that additions are modulo 2^32 (overflow wraps) in SHA-256 rounds.
  • Trying to compute full SHA-256 rounds by hand for non-trivial messages — show padding and IV, then quote or use an implementation.
  • Using 64-bit length field incorrectly (little vs big endian) when implementing padding; specification uses 64-bit big-endian.

For your exam

Types of questions:

  • Definitions: state and distinguish preimage, second-preimage and collision resistance.
  • Short notes: describe Merkle–Damgård, length-extension attacks, and how HMAC mitigates them.
  • Derivations: show why collision resistance of f implies that of H (Merkle–Damgård) — give proof sketch by contradiction and first-differing-block argument.
  • Numericals: compute number of blocks after padding, compute expected work factors (powers of two) for attacks.
  • Programs/pseudocode: write high-level pseudocode for SHA-256 (padding, parsing, message schedule and main loop).
  • Diagrams: draw the compression function chaining (IV -> f -> chaining -> f -> … -> H).

What full-marks answers include:

  • Precise definitions with formulas for complexity (e.g., 2^n, 2^(n/2)).
  • For SHA-256 questions: mention block size 512, word size 32, IV values, 64 rounds, and the exact round functions (Ch, Maj, Σ, σ) with rotations/shifts.
  • For proofs: clearly state assumptions (e.g., padding injective), and give stepwise reasoning.

Quick check

  1. What is the block size (bits) of SHA-256?
  2. State the computational cost (approx.) to find a collision for an ideal 256-bit hash.
  3. Why is H(secret || message) insecure for Merkle–Damgård hashes?

Answers: 1) 512 bits. 2) ≈ 2^128 operations (birthday bound). 3) Because of length-extension attack: given H(secret || message) and the length of secret || message, an attacker can compute H(secret || message || pad || extra) without knowing secret.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?