Message authentication codes

HMAC

The standard way to build a MAC from a cryptographic hash (RFC 2104, FIPS 198-1)

standard Safe API Specified in a FIPS standard

About HMAC

HMAC is a message authentication code. It uses a 1–268,435,456-byte key and a 16–64-byte authentication tag. It takes the parameter hash (default sha256). Its registry entry cites RFC 2104, FIPS 198-1 and RFC 4231 and others. NextSSL lists it in the standard tier and exposes it through the safe API.

Overview

HMAC is a message authentication code built from a cryptographic hash function and a secret key. It is the most widely deployed MAC, used in TLS, IPsec, JWT, TOTP and countless APIs, and is the basis of HKDF and PBKDF2.

HMAC computes H((K ⊕ opad) ‖ H((K ⊕ ipad) ‖ m)), where K is the key padded to the hash block size and ipad and opad are fixed byte patterns. The nested structure means HMAC remains secure even when the underlying hash is vulnerable to length-extension, and its security proof requires only that the hash's compression function behaves as a pseudo-random function, a weaker property than collision resistance.

NextSSL provides a generic HMAC over its hash functions and named instances HMAC-MD5, HMAC-SHA1, HMAC-SHA256, HMAC-SHA512 and HMAC-SHA3-256/384/512. HMAC was introduced by Mihir Bellare, Ran Canetti and Hugo Krawczyk in 1996, specified in RFC 2104 and standardised by NIST in FIPS 198-1; RFC 4231 gives SHA-2 test vectors and NIST SP 800-224 covers HMAC with SHA-3.

History

1996–2008
  1. HMAC published by Bellare, Canetti and Krawczyk

  2. Specified in RFC 2104

  3. Specified in RFC 2202

  4. Standardised in FIPS 198

  5. Specified in RFC 4231

  6. FIPS 198-1 published

How it works

  1. Pad or hash the key to the block size
  2. Hash (key XOR ipad) followed by the message
  3. Hash (key XOR opad) followed by the inner digest
  4. Output the outer digest, optionally truncated

Security notes

HMAC with SHA-2 or SHA-3 has no known practical weaknesses. HMAC-MD5 and HMAC-SHA1 are not practically broken as MACs because they do not depend on collision resistance, but they are deprecated and should be replaced. Tags must be compared in constant time to avoid timing leaks.

Typical uses

API request signingTLS and IPsec record integrity (legacy suites)HKDF and PBKDF2TOTP/HOTP one-time passwords

Sizes

FieldBytesMeaning
key1 – 268,435,456Secret key
tag16 – 64Authentication tag

Parameters

NameTypeRangeDefaultRequired
hash enum 0 – sha256 no

Parameters are passed as a string, for example nextssl_mac("hmac", "hash=sha512", …). Omitted parameters take their declared default.

Use it

from nextssl_demo import NextSSL

ns = NextSSL()              # safe API: safe and standard tiers
key = ns.random(1)
tag = ns.call("mac", "hmac", None, key, b"message", 0)[0]
ok  = ns.check("mac_verify", "hmac", None, key, b"message", tag)

Calls follow the NextSSL C ABI and its language bindings. Buffer sizes are validated against the sizes above.

Frequently asked questions

What is HMAC?

HMAC is a MAC in the HMAC family. HMAC is a message authentication code built from a cryptographic hash function and a secret key.

What are the HMAC sizes?

In NextSSL, HMAC uses: key 1 to 268,435,456 bytes, tag 16 to 64 bytes.

Who designed HMAC and when?

HMAC belongs to the HMAC family, first published in 1996 by Mihir Bellare, Ran Canetti, Hugo Krawczyk. Its origin: RFC 2104; FIPS 198-1.

Which standards define HMAC?

HMAC is referenced by RFC 2104, FIPS 198-1, RFC 4231, NIST SP 800-224 (HMAC-SHA3). It is specified in a FIPS standard.

Is HMAC safe to use?

HMAC is in the standard tier. Standardised and sound. Reachable from the safe API. It can be called from the safe API.

What other HMAC variants are there?

NextSSL also registers hmac-md5, hmac-sha1, hmac-sha256, hmac-sha3-256, hmac-sha3-384, hmac-sha3-512 and more in the HMAC family.