Known-answer test (source code)

= Known-answer test

A \b[known-answer test] checks a cryptographic implementation by supplying fixed, completely specified inputs and comparing its output with an independently established expected value. The input and expected output together form a <cryptographic test vector>; the test is the act of executing the implementation and checking that agreement. Published <HMAC> vectors explicitly support conformance testing.<Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512 (RFC 4231)>[§1 and §§4.1–4.3]{ref}

For example, take the four key bytes `4a 65 66 65`, representing `Jefe`, and the 28 ASCII message bytes representing `what do ya want for nothing?`. The published <HMAC> with <SHA-256> result is

`5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843`.

A known-answer test decodes that 64-digit <hexadecimal> value to 32 expected bytes, computes the keyed result, and compares all 32 bytes. Neither the message's terminating string-storage zero nor a newline is part of these input bytes.<Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512 (RFC 4231)>[§4.3, Test Case 2]{ref}

Several different errors can cause failure: decoding hexadecimal as text, including an extra byte, using the wrong key, selecting another <cryptographic hash function>, or serializing the result incorrectly. A failure establishes disagreement with the vector; it does not identify the responsible step by itself. A useful test harness reports the selected function, input lengths and first differing output position.

Passing also has a precise limit. A defective implementation that returns this expected value for this one input and zero for every other input passes this test. Thus a finite set of known-answer tests is evidence about those cases, not a proof of correctness for all inputs or a proof of cryptographic security. Boundary cases complement ordinary short examples: the primary vector set includes output truncation and keys longer than a hash's input block.<Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512 (RFC 4231)>[§§4.6–4.8]{ref}

An independently fixed expected value is essential. Computing the “expected” output with the same implementation under test can reproduce the same defect on both sides. Known-answer tests are especially useful for reproducible checks of exact byte conventions across different implementations.