Skip to main content

9.4 Cryptographic Hash Functions (SHA-256) & Digital Signatures

πŸ“šModule 09: Network Security & CryptographyTopic 9.4⏱️16 min read
🎯High-Yield For:Semester Exams (All Universities) β€’ Placement Technical Rounds β€’ Engineering Foundations

πŸ’‘ Core Intuition: The Indelible Digital Fingerprint & Notary Stamp​

🍳 The Everyday Analogy: The Scrap Metal Crusher & Notary Seal​

Imagine two security challenges: proving a car was not altered, and proving an author signed a contract:

  1. The Hash Function (The Metal Crusher): You drive a 2,000 kg automobile into an industrial crusher. The machine flattens the car into an exact 10Β cmΓ—10Β cm10\text{ cm} \times 10\text{ cm} metal block. It is trivial to crush the car into the cube, but physically impossible to reconstruct the exact running car back out of the scrap metal (One-Way Property). Furthermore, if you alter a single grain of paint on the car before crushing, the resulting block has a completely different chemical hue and mass (The Avalanche Effect).
  2. The Digital Signature (The Notarized Wax Seal): To sign a 500-page mortgage document, you do not sign every individual word. Instead, you crush the document into its unique 32-byte digital fingerprint, and stamp your personal, unforgeable signet ring onto that fingerprint. Anyone with a magnifying glass can verify that your signet stamped that fingerprint, and that the fingerprint matches the 500-page contract.

πŸ’» Bridging to Computer Science​

In network protocols:

  • A Cryptographic Hash Function produces a fixed-size, irreversible digital fingerprint of arbitrary data. It detects any accidental or malicious alteration.
  • A Digital Signature combines a cryptographic hash with asymmetric cryptography. It proves who created the data (Authenticity), that the data was not tampered with (Integrity), and prevents the signer from denying their signature (Non-Repudiation).


πŸ›οΈ The Three Pillars of Cryptographic Hash Functions​

A cryptographic hash function H:{0,1}βˆ—β†’{0,1}nH: \{0, 1\}^* \to \{0, 1\}^n maps an arbitrary-length message MM to an invariant, fixed-size nn-bit digest h=H(M)h = H(M).

The 3 Essential Cryptographic Hash Properties

The mathematical criteria distinguishing cryptographic hashes from ordinary checksums (CRC32)

πŸ”’

1. Pre-Image Resistance

One-Way Property
  • Given an output hash h, it is computationally infeasible to find any message M such that H(M) = h.
  • Prevents an attacker from reversing stored password hashes back into plaintext.
  • Brute-Force Complexity: Requires approximately 2^n hash evaluations.
πŸ›‘οΈ

2. Second Pre-Image Resistance

Weak Collision Resistance
  • Given a specific input M_1, it is computationally infeasible to find a distinct M_2 != M_1 such that H(M_1) = H(M_2).
  • Prevents an attacker from substituting a malicious binary for a legitimate software patch while keeping the same hash.
  • Brute-Force Complexity: Requires approximately 2^n hash evaluations.
πŸ’₯

3. Collision Resistance

Strong Collision Resistance
  • It is computationally infeasible to find ANY two distinct messages M_1 != M_2 such that H(M_1) = H(M_2).
  • The attacker has full freedom to manipulate both messages simultaneously.
  • Brute-Force Complexity: Requires only approximately 2^(n/2) evaluations (Birthday Paradox).
⚑

4. The Avalanche Effect

Strict Diffusion
  • Flipping a single bit in the input message flips an average of 50% of the output digest bits.
  • Produces completely uncorrelated, pseudorandom output distributions.
  • Eliminates statistical patterns that could assist cryptanalysis.

πŸŽ‚ Mathematical Analysis: The Birthday Paradox & Hash Output Sizing​

Why is finding any collision so much easier than finding a second pre-image? This is governed by the Birthday Paradox.

The Classical Probability Problem​

In a room of kk randomly chosen people, what is the probability that at least two people share the same birthday (assuming N=365N = 365 days)?

  • Most people intuitively guess that kk must be around 365/2β‰ˆ180365 / 2 \approx 180.
  • The actual mathematical answer is that with just k=23k = 23 people, the probability of a shared birthday exceeds 50%50\%!

Mathematical Derivation for Cryptographic Hashes​

For an nn-bit hash function, there are N=2nN = 2^n possible hash values. Let an attacker generate kk distinct messages. The probability that all kk hashes are unique is:

P(NoΒ Collision)=∏i=0kβˆ’1(1βˆ’iN)P(\text{No Collision}) = \prod_{i=0}^{k-1} \left(1 - \frac{i}{N}\right)

Using the Taylor approximation 1βˆ’xβ‰ˆeβˆ’x1 - x \approx e^{-x}:

P(NoΒ Collision)β‰ˆβˆi=0kβˆ’1eβˆ’i/N=eβˆ’βˆ‘i=0kβˆ’1i/N=eβˆ’k(kβˆ’1)2Nβ‰ˆeβˆ’k22NP(\text{No Collision}) \approx \prod_{i=0}^{k-1} e^{-i / N} = e^{-\sum_{i=0}^{k-1} i / N} = e^{-\frac{k(k-1)}{2N}} \approx e^{-\frac{k^2}{2N}}

Therefore, the probability of finding at least one collision is:

P(Collision)=1βˆ’eβˆ’k22NP(\text{Collision}) = 1 - e^{-\frac{k^2}{2N}}

Setting P(Collision)=0.5P(\text{Collision}) = 0.5:

0.5=1βˆ’eβˆ’k22Nβ€…β€ŠβŸΉβ€…β€Šeβˆ’k22N=0.5β€…β€ŠβŸΉβ€…β€Šk22N=ln⁑(2)β‰ˆ0.6930.5 = 1 - e^{-\frac{k^2}{2N}} \implies e^{-\frac{k^2}{2N}} = 0.5 \implies \frac{k^2}{2N} = \ln(2) \approx 0.693

k2β‰ˆ2β‹…ln⁑(2)β‹…Nβ‰ˆ1.177β‹…Nk^2 \approx 2 \cdot \ln(2) \cdot N \approx 1.177 \cdot N

kβ‰ˆ1.177β‹…N=1.177β‹…2n/2\mathbf{k \approx 1.177 \cdot \sqrt{N} = 1.177 \cdot 2^{n/2}}

Crucial Cryptographic Sizing Rule

Because of the Birthday Paradox, an nn-bit hash function provides only n/2n/2 bits of security against collision attacks!

  • A 64-bit hash provides only 232β‰ˆ4.3Γ—1092^{32} \approx 4.3 \times 10^9 collision resistanceβ€”crackable on a laptop in seconds!
  • MD5 (128Β bits128\text{ bits}) provides only 2642^{64} collision resistanceβ€”broken in 2004.
  • SHA-1 (160Β bits160\text{ bits}) provides only 2802^{80} collision resistanceβ€”broken by Google in 2017 (SHAttered attack).
  • SHA-256 (256Β bits256\text{ bits}) provides 21282^{128} collision resistanceβ€”safe against all supercomputers until the end of the universe.

πŸ”‘ Message Authentication Codes: The HMAC Architecture (RFC 2104)​

Suppose Alice sends a message MM and its hash H(M)H(M) to Bob. An active attacker (Eve) intercepts MM, modifies it to Mβ€²M', calculates a fresh hash H(Mβ€²)H(M'), and forwards both to Bob. Because Bob has no secret key, Bob computes H(Mβ€²)H(M'), matches it, and is completely deceived!

To bind integrity to a secret identity, systems use Message Authentication Codes (MACs).

Why Naive Hashing H(Kβˆ₯M)H(K \parallel M) Fails: Length Extension Attacks​

Many developers mistakenly create a MAC by prepending a secret key: MAC=H(Kβˆ₯M)\text{MAC} = H(K \parallel M). In Merkle-DamgΓ₯rd hash functions (MD5, SHA-1, SHA-256):

  • The hash output is simply the internal state of the compression function after processing the final block.
  • An attacker who knows H(Kβˆ₯M)H(K \parallel M) and the length of MM can append extra data MevilM_{\text{evil}} and calculate H(Kβˆ₯Mβˆ₯paddingβˆ₯Mevil)H(K \parallel M \parallel \text{padding} \parallel M_{\text{evil}}) without ever knowing the secret key KK!

The HMAC Construction (RFC 2104)​

To eliminate length extension attacks, HMAC applies a nested, two-pass hashing structure:

HMAC(K,M)=H((K+βŠ•opad)β€…β€Šβˆ₯β€…β€ŠH((K+βŠ•ipad)β€…β€Šβˆ₯β€…β€ŠM))\mathbf{\text{HMAC}(K, M) = H\Big(\big(K^+ \oplus \text{opad}\big) \;\parallel\; H\big((K^+ \oplus \text{ipad}) \;\parallel\; M\big)\Big)}

Where:

  • K+K^+ is the secret key padded with zeros to match the block size (B=64Β bytesB = 64\text{ bytes} in SHA-256).
  • ipad\text{ipad} (inner pad) is the constant byte 0x36 repeated BB times.
  • opad\text{opad} (outer pad) is the constant byte 0x5C repeated BB times.

The outer hash HH acts on the digest of the inner hash, completely severing the linear state chaining and rendering length extension attacks mathematically impossible.


πŸ” Asymmetric Digital Signatures: Integrity, Authenticity & Non-Repudiation​

A Digital Signature is the asymmetric counterpart of a MAC. While an HMAC uses a shared symmetric key (allowing either party to forge the MAC), a digital signature is produced using a unique Private Key (SKSK) and verified using a Public Key (PKPK).

Architecture Flow

Digital Signature Generation and Verification Pipeline

End-to-end cryptographic workflow ensuring authenticity, message integrity, and non-repudiation

πŸ“„Sender Side

1. Hash Message

Sender passes arbitrary-length document M through SHA-256 to produce fixed digest h = H(M).

β†’
Private Key Signing
πŸ”Cryptographic Sign

2. Encrypt Digest

Sender signs digest using private key: S = (h)^d mod n (in RSA). Attaches S to M.

β†’
Transmit (M, S) over Wire
πŸ”Receiver Side

3. Decrypt Signature

Receiver decrypts S using Sender's Public Key: h' = (S)^e mod n.

β†’
Compare Digests
βœ…Verification

4. Integrity Check

Receiver hashes received M to get h''. If h' == h'', message is authentic and untampered!

Why Do We Sign the Hash Instead of the Entire Document?​

  1. Computational Speed: Asymmetric operations on multi-thousand-bit modular numbers are computationally expensive. Signing an entire 500Β MB500\text{ MB} file directly would take minutes. Hashing compresses the 500Β MB500\text{ MB} file into a 32-byte digest in milliseconds, allowing the private key operation to execute in a fraction of a millisecond.
  2. Mathematical Space Limitation: In RSA, the message must satisfy M<nM < n. A large document exceeds the modulus nn and cannot be signed directly.
  3. Algebraic Attack Defense: Signing raw data exposes the system to multiplicative chosen-ciphertext attacks. Hashing destroys the algebraic structure of the input document.

πŸ“Š Comparison: Checksums vs Hashes vs MACs vs Digital Signatures​

Primitive TypePrimary ObjectiveKey Required?Defends Against Active Tampering?Non-Repudiation Guarantee?
CRC32 / ChecksumError Detection (Noise)❌ None❌ No (Trivial to forge matching CRC)❌ None
Cryptographic Hash (SHA-256)Fingerprinting / Integrity❌ None❌ No (Attacker replaces M and Hash)❌ None
HMAC (Symmetric MAC)Integrity & Origin Authenticationβœ… Shared Secret KKβœ… Yes (Attacker cannot forge MAC)❌ No (Both parties share KK)
Digital Signature (RSA / ECDSA)Integrity, Origin & Accountabilityβœ… Asymmetric Key Pairβœ… Yes (Only private key holder can sign)βœ… Yes (Full Non-Repudiation)

🏭 Real-World Production Context: Bitcoin Merkle Trees, Git SHA-1 Migration & API Webhooks​

Cryptographic hash functions and digital signatures form the cryptographic bedrock of global digital infrastructure:

  • Bitcoin Blockchain & Merkle Trees:
    • A Bitcoin block aggregates up to 4,000 transactions into a single 32-byte Merkle root using double-SHA-256 (SHA-256(SHA-256(M))\text{SHA-256}(\text{SHA-256}(M))).
    • SPV Light Wallets: Mobile smartphone wallets do not download the 500+ GB full ledger. By requesting an authentication path of just k=⌈log⁑2NβŒ‰β‰ˆ12k = \lceil \log_2 N \rceil \approx 12 hashes (384 bytes), a phone mathematically verifies that a specific transaction exists inside a block validated by miners.
  • Git Content-Addressable Object Store & The SHA-1 to SHA-256 Transition:
    • Git tracks file contents, trees, and commit histories via hash object IDs (blob, tree, commit).
    • In 2017, the SHAttered attack proved practical collisions against SHA-1 (263.12^{63.1} operations, generating two distinct PDFs with identical SHA-1 hashes).
    • The Git core project re-architected its storage engine to support SHA-256 (git init --object-format=sha256), guaranteeing that malicious commits cannot overwrite legitimate source code commits with identical hash pointers.
  • SaaS API Webhooks via HMAC-SHA256 (Stripe, GitHub, Shopify):
    • When a payment completes, Stripe dispatches an HTTP POST payload to the merchant's endpoint with the header Stripe-Signature: t=1614555000,v1=5257a8....
    • The merchant server takes the raw payload, prepends the timestamp tt, and computes HMAC-SHA256(Kwebhook_secret,tβˆ₯payload)\text{HMAC-SHA256}(K_{\text{webhook\_secret}}, t \parallel \text{payload}).
    • Comparing the signature using constant-time equality (crypto.timingSafeEqual) prevents both replay attacks and timing side-channel exploits while verifying message integrity and authenticity without public key overhead.

πŸ“ Numerical Applications & Worked Examples​

Example 1: Birthday Paradox Hash Collision Probability​

Problem Statement: A legacy archival system uses an n=64n = 64-bit cryptographic hash function to assign unique identifiers to files. The repository stores k=5Γ—109k = 5 \times 10^9 unique files.

  1. Compute the probability of encountering at least one hash collision across this repository using the Birthday Paradox approximation.
  2. Calculate the exact number of files k50k_{50} required for the collision probability to reach 50%50\%.
  3. Contrast this with the collision margin of a modern 256256-bit hash function (SHA-256).

Step-by-Step Solution:

  1. Collision Probability for k=5Γ—109k = 5 \times 10^9 Files:
    • Total possible hash values: N=264β‰ˆ1.84467Γ—1019N = 2^{64} \approx 1.84467 \times 10^{19}
    • The probability of at least one collision across kk samples is given by: P(Collision)β‰ˆ1βˆ’eβˆ’k22NP(\text{Collision}) \approx 1 - e^{-\frac{k^2}{2N}}
    • Calculate the exponent: k2=(5Γ—109)2=2.5Γ—1019k^2 = (5 \times 10^9)^2 = 2.5 \times 10^{19} k22N=2.5Γ—10192Γ—1.84467Γ—1019=2.53.6893β‰ˆ0.6776\frac{k^2}{2N} = \frac{2.5 \times 10^{19}}{2 \times 1.84467 \times 10^{19}} = \frac{2.5}{3.6893} \approx 0.6776
    • Evaluate the exponential: P(Collision)β‰ˆ1βˆ’eβˆ’0.6776β‰ˆ1βˆ’0.5078=0.4922Β (49.22%)P(\text{Collision}) \approx 1 - e^{-0.6776} \approx 1 - 0.5078 = \mathbf{0.4922 \ (49.22\%)}
    • Despite 2642^{64} being over 18 quintillion, storing just 5 billion files yields a nearly 50%50\% chance of a fatal collision!
  2. Number of Files for 50%50\% Collision Probability (k50k_{50}):
    • Setting P(Collision)=0.5P(\text{Collision}) = 0.5: k50β‰ˆ2ln⁑(2)β‹…Nβ‰ˆ1.1774Γ—264=1.1774Γ—232k_{50} \approx \sqrt{2 \ln(2) \cdot N} \approx 1.1774 \times \sqrt{2^{64}} = 1.1774 \times 2^{32} k50β‰ˆ1.1774Γ—4.294967Γ—109β‰ˆ5.057Γ—109Β filesk_{50} \approx 1.1774 \times 4.294967 \times 10^9 \approx \mathbf{5.057 \times 10^9 \text{ files}}
  3. Collision Resistance for SHA-256 (n=256n = 256):
    • Modulus space N=2256β‰ˆ1.1579Γ—1077N = 2^{256} \approx 1.1579 \times 10^{77}.
    • Sizing for 50%50\% collision probability: k50β‰ˆ1.1774Γ—2128β‰ˆ1.1774Γ—3.4028Γ—1038β‰ˆ4.006Γ—1038Β filesk_{50} \approx 1.1774 \times 2^{128} \approx 1.1774 \times 3.4028 \times 10^{38} \approx \mathbf{4.006 \times 10^{38} \text{ files}}
    • Generating 4Γ—10384 \times 10^{38} hashes would require all the computers on Earth running for billions of years, confirming that 256-bit hashes provide absolute collision resistance.

Example 2: HMAC-SHA256 Internal Buffer Sizing & Key Pre-Processing​

Problem Statement: An enterprise gateway authenticates JSON web requests using HMAC-SHA256 (RFC 2104). The underlying hash function has an input block size B=64Β bytesB = 64\text{ bytes} (512Β bits512\text{ bits}) and an output digest size L=32Β bytesL = 32\text{ bytes} (256Β bits256\text{ bits}).

  1. If the shared secret key KK is 32Β bytes32\text{ bytes} long, explain how the padded key K+K^+ is constructed.
  2. If an administrator accidentally configures an oversized secret key KlongK_{\text{long}} of 96Β bytes96\text{ bytes}, specify how HMAC prepares K+K^+.
  3. Compute the exact byte size of the inner hash input and outer hash input when signing a request message MM of size 1,200Β bytes1,200\text{ bytes}.

Step-by-Step Solution:

  1. Normal Key Formatting (∣Kβˆ£β‰€B|K| \le B):
    • Since ∣K∣=32Β bytes≀64Β bytes|K| = 32\text{ bytes} \le 64\text{ bytes}, RFC 2104 appends (Bβˆ’βˆ£K∣)=(64βˆ’32)=32(B - |K|) = (64 - 32) = 32 zero bytes (0x00) to the right of KK: K+=Kβˆ₯0x00Β 0x00 … 0x00⏟32Β bytesβ€…β€ŠβŸΉβ€…β€Šβˆ£K+∣=64Β bytesK^+ = K \parallel \underbrace{0\text{x}00 \ 0\text{x}00 \ \dots \ 0\text{x}00}_{32\text{ bytes}} \implies |K^+| = \mathbf{64\text{ bytes}}
  2. Oversized Key Formatting (∣K∣>B|K| > B):
    • Since ∣Klong∣=96Β bytes>64Β bytes|K_{\text{long}}| = 96\text{ bytes} > 64\text{ bytes}, HMAC first hashes the oversized key using the underlying hash function: Kβ€²=H(Klong)=SHA-256(Klong)β€…β€ŠβŸΉβ€…β€Šβˆ£Kβ€²βˆ£=L=32Β bytesK' = H(K_{\text{long}}) = \text{SHA-256}(K_{\text{long}}) \implies |K'| = L = 32\text{ bytes}
    • Then Kβ€²K' is padded with 64βˆ’32=3264 - 32 = 32 zero bytes to produce: K+=Kβ€²βˆ₯032β€…β€ŠβŸΉβ€…β€Šβˆ£K+∣=64Β bytesK^+ = K' \parallel 0^{32} \implies |K^+| = \mathbf{64\text{ bytes}}
  3. Inner and Outer Hash Buffer Sizes for ∣M∣=1,200 Bytes|M| = 1,200\text{ Bytes}:
    • Inner Hash Pass:
      • Inner pad XOR: ∣K+βŠ•ipad∣=64Β bytes|K^+ \oplus \text{ipad}| = 64\text{ bytes} (where ipad=0x36\text{ipad} = \text{0x36} repeated 64 times).
      • Inner hash input: (K+βŠ•ipad)βˆ₯M(K^+ \oplus \text{ipad}) \parallel M.
      • Total inner input size: Sizeinner_in=64+1200=1,264Β bytes\text{Size}_{\text{inner\_in}} = 64 + 1200 = \mathbf{1,264\text{ bytes}}
      • Output of inner hash: hinner=H((K+βŠ•ipad)βˆ₯M)β€…β€ŠβŸΉβ€…β€Šβˆ£hinner∣=32Β bytesh_{\text{inner}} = H\big((K^+ \oplus \text{ipad}) \parallel M\big) \implies |h_{\text{inner}}| = \mathbf{32\text{ bytes}}
    • Outer Hash Pass:
      • Outer pad XOR: ∣K+βŠ•opad∣=64Β bytes|K^+ \oplus \text{opad}| = 64\text{ bytes} (where opad=0x5C\text{opad} = \text{0x5C} repeated 64 times).
      • Outer hash input: (K+βŠ•opad)βˆ₯hinner(K^+ \oplus \text{opad}) \parallel h_{\text{inner}}.
      • Total outer input size: Sizeouter_in=64+32=96Β bytes\text{Size}_{\text{outer\_in}} = 64 + 32 = \mathbf{96\text{ bytes}}
      • Final output: HMAC(K,M)=H((K+βŠ•opad)βˆ₯hinner)β€…β€ŠβŸΉβ€…β€Š32Β bytesΒ (256Β bits)\text{HMAC}(K, M) = H\big((K^+ \oplus \text{opad}) \parallel h_{\text{inner}}\big) \implies \mathbf{32\text{ bytes (256 bits)}}

Example 3: Throughput Benchmark: HMAC-SHA256 vs RSA-2048 vs ECDSA Signatures​

Problem Statement: A microservices gateway signs outbound audit payloads of average size S=2Β KBS = 2\text{ KB} (2,048Β bytes2,048\text{ bytes}). The server core has the following execution benchmarks:

  • SHA-256 hardware pipeline: 500Β MB/s500\text{ MB/s} throughput.
  • HMAC-SHA256 hashing rate: 450Β MB/s450\text{ MB/s} (including key derivation and dual hash passes).
  • RSA-2048 private key signature: 1,200Β signΒ operations/second1,200\text{ sign operations/second} (833.3Β ΞΌs833.3\ \mu\text{s} per signature).
  • ECDSA (secp256r1) private key signature: 25,000Β signΒ operations/second25,000\text{ sign operations/second} (40.0Β ΞΌs40.0\ \mu\text{s} per signature).
  1. Calculate the maximum signatures per second achievable on a single core using HMAC-SHA256.
  2. Calculate the maximum signatures per second achievable using RSA-2048.
  3. Calculate the maximum signatures per second achievable using ECDSA.
  4. Explain why cloud microservices prefer HMAC or ECDSA over RSA-2048 for high-frequency internal RPC authentication.

Step-by-Step Solution:

  1. HMAC-SHA256 Single-Core Throughput:
    • Time to compute HMAC for 2Β KB2\text{ KB}: THMAC=2,048Β bytes450Γ—106Β bytes/sec=4.551Γ—10βˆ’6Β s=4.551Β ΞΌsT_{\text{HMAC}} = \frac{2,048\text{ bytes}}{450 \times 10^6\text{ bytes/sec}} = 4.551 \times 10^{-6}\text{ s} = 4.551\ \mu\text{s}
    • Maximum operations per second: ThroughputHMAC=14.551Γ—10βˆ’6Β sβ‰ˆ219,730Β ops/sec\text{Throughput}_{\text{HMAC}} = \frac{1}{4.551 \times 10^{-6}\text{ s}} \approx \mathbf{219,730\text{ ops/sec}}
  2. RSA-2048 Throughput:
    • Time to hash payload: Thash=2,048Β bytes500Γ—106Β bytes/sec=4.096Β ΞΌsT_{\text{hash}} = \frac{2,048\text{ bytes}}{500 \times 10^6\text{ bytes/sec}} = 4.096\ \mu\text{s}
    • Time for modular exponentiation (S=hdβ€Šmodβ€ŠnS = h^d \bmod n): TRSA_sign=833.33Β ΞΌsT_{\text{RSA\_sign}} = 833.33\ \mu\text{s}.
    • Total latency: Ttotal_RSA=4.096Β ΞΌs+833.33Β ΞΌs=837.43Β ΞΌsT_{\text{total\_RSA}} = 4.096\ \mu\text{s} + 833.33\ \mu\text{s} = 837.43\ \mu\text{s}
    • Maximum throughput: ThroughputRSA=1837.43Γ—10βˆ’6Β sβ‰ˆ1,194Β ops/sec\text{Throughput}_{\text{RSA}} = \frac{1}{837.43 \times 10^{-6}\text{ s}} \approx \mathbf{1,194\text{ ops/sec}}
  3. ECDSA (secp256r1) Throughput:
    • Time to hash: Thash=4.096Β ΞΌsT_{\text{hash}} = 4.096\ \mu\text{s}.
    • Time for elliptic curve point scalar multiplication: TECDSA_sign=40.0Β ΞΌsT_{\text{ECDSA\_sign}} = 40.0\ \mu\text{s}.
    • Total latency: Ttotal_ECDSA=4.096Β ΞΌs+40.0Β ΞΌs=44.10Β ΞΌsT_{\text{total\_ECDSA}} = 4.096\ \mu\text{s} + 40.0\ \mu\text{s} = 44.10\ \mu\text{s}
    • Maximum throughput: ThroughputECDSA=144.10Γ—10βˆ’6Β sβ‰ˆ22,675Β ops/sec\text{Throughput}_{\text{ECDSA}} = \frac{1}{44.10 \times 10^{-6}\text{ s}} \approx \mathbf{22,675\text{ ops/sec}}
  4. Engineering Trade-Off:
    • RSA-2048 signing is extremely bottlenecked by modular exponentiation with a 2048-bit private exponent (1,194Β ops/sec1,194\text{ ops/sec}), imposing severe CPU load on microservice sidecars (like Envoy or Istio).
    • ECDSA delivers nearly 19Γ—19\times higher signing throughput than RSA with equivalent cryptographic strength (128 bits of security).
    • For symmetric internal service-to-service communication where a shared secret is maintained in a secret vault, HMAC-SHA256 provides 184Γ—184\times higher throughput than RSA and 9.7Γ—9.7\times higher throughput than ECDSA, making it the clear choice when non-repudiation across mutual services is not legally required.

🎯 Exam & Interview Pitfall Check​

Question 1: Define the three security properties required of a cryptographic hash function: Pre-image Resistance, Second Pre-image Resistance, and Collision Resistance. Explain mathematically why a 128-bit hash function is vulnerable to collision attacks even though 21282^{128} is an astronomically large number.

Answer:

  • The 3 Security Properties:
    1. Pre-image Resistance (One-Way): Given a hash digest hh, it must be computationally infeasible to find any input message MM such that H(M)=hH(M) = h.
    2. Second Pre-image Resistance (Weak Collision): Given a specific input message M1M_1, it must be computationally infeasible to find a distinct message M2β‰ M1M_2 \neq M_1 such that H(M1)=H(M2)H(M_1) = H(M_2).
    3. Collision Resistance (Strong Collision): It must be computationally infeasible to find any arbitrary pair of distinct messages (M1,M2)(M_1, M_2) such that H(M1)=H(M2)H(M_1) = H(M_2).
  • Vulnerability of 128-bit Hashes via Birthday Paradox:
    • In a second pre-image attack, the message M1M_1 is fixed by the victim; the attacker must test random messages until hitting that specific target hash value, requiring approximately 21282^{128} operations.
    • In a collision attack, the attacker does not have a fixed target; they generate two sets of messages simultaneously. Under the Birthday Paradox, when choosing from N=2128N = 2^{128} possible hash values, the number of random messages kk required to achieve a 50%50\% probability of at least one collision is: kβ‰ˆ1.177Γ—N=1.177Γ—2128/2=1.177Γ—264k \approx 1.177 \times \sqrt{N} = 1.177 \times 2^{128/2} = 1.177 \times 2^{64}
    • An operation count of 2642^{64} (approximately 1.8Γ—10191.8 \times 10^{19}) is entirely within reach of modern distributed GPU mining clusters and botnets (achieved in 2004 against MD5). Thus, a 128-bit hash provides only 64 bits of effective collision security.

Question 2: Explain the operational workflow of generating and verifying an RSA digital signature. Why does a digital signature provide Non-Repudiation while an HMAC does not?

Answer:

  • Operational Workflow:
    1. Generation (Sender Alice):
      • Alice passes plaintext document MM through a cryptographic hash function (SHA-256) to produce digest h=H(M)h = H(M).
      • Alice encrypts the digest hh using her private key (SKA=dSK_A = d): S=hdβ€Šmodβ€ŠnS = h^d \bmod n
      • Alice transmits the bundle (M,S)(M, S) to Bob.
    2. Verification (Receiver Bob):
      • Bob receives (M,S)(M, S) and computes his own hash of the received message: hcalc=H(M)h_{\text{calc}} = H(M).
      • Bob decrypts the signature SS using Alice's public key (PKA=ePK_A = e): hdec=Seβ€Šmodβ€Šn=(hd)eβ€Šmodβ€Šn≑hh_{\text{dec}} = S^e \bmod n = (h^d)^e \bmod n \equiv h
      • If hcalc==hdech_{\text{calc}} == h_{\text{dec}}, Bob has mathematical proof that the message was created by Alice and has not been altered by a single bit.
  • Why Signatures Provide Non-Repudiation While HMACs Do Not:
    • An HMAC relies on a symmetric shared secret key KK known by both Alice and Bob. If a dispute arises and Bob takes the message to a court of law, Alice can claim: "Bob holds the exact same key KK; Bob forged this message himself!" A third-party arbiter cannot prove which of the two parties generated the HMAC.
    • In a Digital Signature, the private key dd is possessed exclusively by Alice. Nobody else in the world possesses dd. Because only Alice's private key could have produced a signature that validates against Alice's public key (e,n)(e, n), Alice cannot deny having signed the message (Non-Repudiation).

Core Conceptual Questions
  • What is a Hash Salt and why is it used in password storage?: When storing passwords, calculating a simple hash SHA256(password)\text{SHA256}(\text{password}) is vulnerable to Rainbow Table attacks (precomputed tables of billions of hash-to-password mappings). A Salt is a cryptographically random string (e.g. 16 bytes) appended to the password before hashing: Hash(Saltβˆ₯password)\text{Hash}(\text{Salt} \parallel \text{password}). The salt is stored alongside the hash in the database. It guarantees that identical passwords produce completely different hashes and forces attackers to attack each user's hash individually, completely neutralizing rainbow tables.
  • What is the difference between SHA-2 and SHA-3?: SHA-2 (developed by NSA) uses the Merkle-DamgΓ₯rd iterative compression structure, making it vulnerable to length-extension attacks if used without HMAC. SHA-3 (Keccak) uses a revolutionary Sponge Construction (absorbing phase followed by squeezing phase) with a state of 1600 bits, providing native immunity against length extension.
Common Interview Traps
  • Trap: Confusing Digital Signatures with Public Key Encryption:
    • In Public Key Encryption: Senders encrypt with the Receiver's Public Key; only the receiver can decrypt (Confidentiality).
    • In Digital Signatures: Senders sign with their Own Private Key; anyone with the sender's public key can verify (Authenticity and Non-Repudiation).
  • Trap: Believing Base64 is an Encryption or Hashing Algorithm: Base64 is merely an encoding scheme (mapping 8-bit binary to printable ASCII). It provides zero security, zero confidentiality, and zero integrity. Anyone can instantly decode Base64 back to raw bytes without a key.

πŸ’¬

Discussion & Doubts