HMAC and Key Derivation | Practical Cryptography for Developers (2024)

Simply calculating hash_func(key + msg) to obtain a MAC (message authentication code) is considered insecure (see the details). It is recommended to use the HMAC algorithm instead, e.g. HMAC-SHA256 or HMAC-SHA3-512 or other secure MAC algorithm.

What is HMAC?

HMAC = Hash-based Message Authentication Code (MAC code, calculated using a cryptographic hash function):

HMAC(key, msg, hash_func) -> hash

The results MAC code is a message hash mixed with a secret key. It has the cryptographic properties of hashes: irreversible, collision resistant, etc.

The hash_func can be any cryptographic hash function like SHA-256, SHA-512, RIPEMD-160, SHA3-256 or BLAKE2s.

HMAC is used for message authenticity, message integrity and sometimes for key derivation.

Key Derivation Functions (KDF)

Key derivation function (KDF) is a function which transforms a variable-length password to fixed-length key (sequence of bits):

function(password) -> key

As very simple KDF function, we can use SHA256: just hash the password. Don't do this, because it is insecure. Simple hashes are vulnerable to dictionary attacks.

As more complicated KDF function, you can derive a password by calculating HMAC(salt, msg, SHA256) using some random value called "salt", which is stored along with the derived key and used later to derive the same key again from the password.

Using HKDF (HMAC-based key derivation) for key derivation is less secure than modern KDFs, so experts recommend using stronger key derivation functions like PBKDF2, Bcrypt, Scrypt and Argon2. We shall discuss all these KDF functions later.

HMAC Calculation - Example

To get a better idea of HMAC and how it is calculated, try this online tool: https://www.freeformatter.com/hmac-generator.html

Play with calculating HMAC('sample message', '12345', 'SHA256'):

HMAC('sample message', '12345', 'SHA256') = 'ee40ca7bc90df844d2f5b5667b27361a2350fad99352d8a6ce061c69e41e5d32'

Try the above example yourself.

HMAC and Key Derivation | Practical Cryptography for Developers (2024)

FAQs

What is HMAC for key derivation? ›

Hashed message authentication code (HMAC)-based key derivation function (HKDF) was designed as a standard KDF for use in various protocols and applications. You can use HKDF to extract, then expand, a key as a sequence of two operations, or to extract and expand a key in a single operation.

How to generate a key for HMAC? ›

Create an HMAC key
  1. In the Google Cloud console, go to the Cloud Storage Settings page. Go to Settings.
  2. Select the Interoperability tab.
  3. Click add_box Create a key for a service account.
  4. Select the service account you want the HMAC key to be associated with.
  5. Click Create key.

What is the encryption key for HMAC? ›

Hash-based message authentication code (or HMAC) is a cryptographic authentication technique that uses a hash function and a secret key. With HMAC, you can achieve authentication and verify that data is correct and authentic with shared secrets, as opposed to approaches that use signatures and asymmetric cryptography.

What is the use of HMAC in cryptography? ›

HMAC can be used to check for data integrity and to authenticate the parties involved in a transaction. Many communication and transfer protocols use HMAC, including HTTPS, SFTP and FTPS. The cryptographic hash function in HMAC is typically SHA-1, SHA-256, MD5 or RIPEMD-128/160.

What is the purpose of key derivation? ›

Key derivation functions take a password, a salt, and a cost factor as inputs then generate a password hash. Their purpose is to make each password guessing trial by an attacker who has obtained a password hash file expensive and therefore the cost of a guessing attack high or prohibitive."

What is the difference between HMAC and JWT? ›

A JWT (when using HMAC as the signing scheme) is basically just an HMAC message where the message data is a JSON object. The interesting thing about the JWT system is that the sender and the receiver of the JWT are typically the same entity, that is, the webserver.

How do HMAC keys work? ›

Overview. An HMAC key is a type of credential associated with an account, typically a service account. You use an HMAC key to create signatures using the HMAC-SHA256 signing algorithm. The signatures you create are then included in requests to the Cloud Storage XML API.

How many keys does HMAC use? ›

See this HMAC definition. In there, indeed two keys are used and the mac algorithm is H(k1‖H(k2‖m)).

How is a cryptographic key generated? ›

The generation of a cryptographic key either as a single process using a random bit generator and an approved set of rules, or as created during key agreement or key derivation.

How do you verify the HMAC key? ›

Go to Developers > Webhooks. Select the edit icon for the Standard webhook you want to enable HMAC for. Under Security, select the edit icon for HMAC key. Select Generate.

What are the key elements needed to implement HMAC? ›

One mechanism for checking integrity is hash-based message authentication codes (HMAC) which use a cryptographic technique that uses combines three elements: a message, a secret key, and a hash function. The goal of HMAC is to aid companies with data integrity, ensuring information isn't tampered with.

How do I authenticate HMAC? ›

This is achieved by including both a public (key-identifier) and private key (key-secret) in each message, the latter of which is only known to the server and client. Using these values, the client will generate a unique HMAC (a hashed code) representing its request to the server.

How to generate a HMAC key? ›

First, enter the plain-text and the cryptographic key to generate the code. Then, you can use select the hash function you want to apply for hashing. The default is SHA-256. Then you can submit your request by clicking on the compute hash button to generate the HMAC authentication code for you.

What are the disadvantages of HMAC? ›

One of the main drawbacks of HMAC is that it requires both parties to share a secret key in advance, which can be challenging in some scenarios, such as public key infrastructures or distributed systems.

Is HMAC still secure? ›

One of the most commonly used hash functions in HMAC is SHA-256, which is considered secure and efficient.

What is key derivation in SSL? ›

Key derivation is the process of deriving one or more secret keys from a secret value such as a password or a passphrase. Several key derivation algoirthms have been standardized, and they are usually referred to a Key Derivation Functions (KDFs).

Is HMAC a keyed hash? ›

The Keyed-Hash Message Authentication Code (HMAC) standard (FIPS PUB 198-1) describes a mechanism for message authentication using cryptographic hash functions. HMAC can be used with a hash function in combination with a shared secret key.

What is the difference between HMAC and public key? ›

With an HMAC function, the recipient of the message could alter the message however they please, because they have the key to do so. The recipient could then write whatever they want as the sender. However, with a Public Key Cryptography system, only the sender has the private key.

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6052

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.