HMAC-based challenge-response authentication uses a cryptographic hash function \(H\) with a shared secret key \(K\) to prove possession of \(K\) without transmitting it.
\[\text{Response} = \text{HMAC}(K, \text{Nonce}) = H((K \oplus \text{opad}) \,||\, H((K \oplus \text{ipad}) \,||\, \text{Nonce}))\]
where \(\text{opad} = 0x5c5c...5c\) and \(\text{ipad} = 0x3636...36\) are padding constants.
Working through an example:
Given: IoT sensor authenticates to gateway using HMAC-SHA256 - Shared secret: \(K = \text{32-byte device-specific key}\) - Gateway sends nonce: \(N = \text{0x8F2AC1D3...}\) (16 bytes random) - Hash function: SHA-256 (256-bit output) - Attacker’s computing power: \(10^{12}\) hash operations/second
Step 1: Calculate sensor’s response (simplified)
\[R = \text{HMAC-SHA256}(K, N)\]
Result: 32-byte (256-bit) authentication tag sent to gateway
Step 2: Calculate security against brute force attack
Attacker must guess the 256-bit secret key \(K\). Number of possible keys:
\[\text{Key Space} = 2^{256}\]
Time to brute force with \(10^{12}\) attempts/second:
\[T_{\text{brute force}} = \frac{2^{256}}{10^{12} \text{ attempts/s}}\]
\[T_{\text{brute force}} = \frac{1.16 \times 10^{77}}{10^{12}} = 1.16 \times 10^{65} \text{ seconds}\]
Converting to years (\(3.15 \times 10^7\) seconds/year):
\[T_{\text{years}} = \frac{1.16 \times 10^{65}}{3.15 \times 10^7} \approx 3.7 \times 10^{57} \text{ years}\]
Step 3: Calculate collision probability for nonce reuse
If the same nonce \(N\) is used twice, the attacker can replay the previous valid response. Probability of nonce collision with 128-bit nonces after \(n\) authentications (birthday paradox):
\[P(\text{collision}) \approx \frac{n^2}{2^{129}}\]
For \(n = 1,000,000\) authentications:
\[P(\text{collision}) \approx \frac{(10^6)^2}{2^{129}} = \frac{10^{12}}{6.8 \times 10^{38}} \approx 1.5 \times 10^{-27}\]
Result: HMAC-SHA256 with a 256-bit key is computationally infeasible to brute force (\(3.7 \times 10^{57}\) years). With proper nonce generation (128-bit random), collision probability is negligible (\(1.5 \times 10^{-27}\) for 1 million authentications).
In practice: Challenge-response with HMAC provides strong authentication without transmitting the secret key. Each authentication uses a unique nonce, preventing replay attacks. For IoT devices with pre-provisioned keys, HMAC-SHA256 offers 256-bit security with minimal computational overhead (SHA-256 is hardware-accelerated on most modern MCUs). Never reuse nonces – implement cryptographically secure random number generation for nonce creation.