<p>They implement 3 distinct cryptographic schemes:</p>
<ul>
<li><code>HMAC-SHA256</code> is a MAC (message authentication code, or a keyed hash function) based on SHA-256. It is used in the RFC6979 nonce generation function for deterministic ECDSA signing.</li>
<li><code>HMAC-SHA512</code> is a MAC based on SHA-512. It is used in the BIP32 key derivation logic.</li>
<li><code>HKDF-SHA256</code> is a (symmetric) key derivation function (KDF) based on HMAC-256. It is currently unused, but was added in anticipation of being used in the BIP324 (v2 P2P transport layer) implementation.</li>
</ul>

CC BY-SA 4.0
2022-11-02T09:33:07.090Z
2022-11-02T09:33:07.090Z
Pieter Wuille
<p>HMAC in the name stands for <a href="https://en.wikipedia.org/wiki/HMAC" rel="nofollow noreferrer">&quot;<strong>H</strong>ash-based <strong>M</strong>essage <strong>A</strong>uthentication <strong>C</strong>ode&quot;</a> and the hmac_sha_512 is used to <a href="https://en.bitcoin.it/wiki/BIP_0032#Specification:_Key_derivation" rel="nofollow noreferrer">generate keys</a> in hierarchic-deterministic BIP-0032 wallets, and to <a href="https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#from-mnemonic-to-seed" rel="nofollow noreferrer">generate a master key</a> from the seed phrase (BIP-0039), for example <a href="https://github.com/bitcoin/bitcoin/blob/93999a5fbe317852307455b6a42d137cc08a0edc/src/key.cpp#L348" rel="nofollow noreferrer">here</a>.</p>
<p>HKDF in the name stands for <a href="https://su21.cs161.org/proj2/crypto/hkdf.html" rel="nofollow noreferrer">&quot;<strong>H</strong>ash-based <strong>K</strong>ey-<strong>D</strong>erivation <strong>F</strong>unction&quot;</a>, and there's a comment in the code which describes it:</p>
<blockquote>
<p>A rfc5869 HKDF implementation with HMAC_SHA256 and fixed key output length of 32 bytes (L=32)</p>
</blockquote>
<p>If you look at <a href="https://www.rfc-editor.org/rfc/rfc5869" rel="nofollow noreferrer">rfc5869 specification</a>, it uses some HMAC as a primitive in the key derivation scheme, and in <code>hkdf_sha256</code> the <code>hmac_sha_256</code> is the primitive i.e. <code>hmac_sha_256</code> is used for <code>hkdf_sha256</code>. The <code>hmac_sha_256</code> is also used in <a href="https://github.com/bitcoin/bitcoin/blob/0de36941eca1bff91420dd878eb097db2b1a596c/src/httprpc.cpp#L113" rel="nofollow noreferrer">httprpc.cpp</a> and <a href="https://github.com/bitcoin/bitcoin/blob/6dc3084eec912cf2abfe18d1c05655defaa45e20/src/torcontrol.cpp#L481" rel="nofollow noreferrer">torcontrol.cpp</a>.</p>
<p>But what is <code>hkdf_sha256</code> used for? It <a href="https://github.com/bitcoin/bitcoin/search?q=CHKDF_HMAC_SHA256_L32" rel="nofollow noreferrer">doesn't seem to be used anywhere</a>.</p>

CC BY-SA 4.0
2022-11-02T09:14:50.293Z
2022-11-02T09:14:50.293Z
bca-0353f40e
<p>In the open source (<a href="https://github.com/bitcoin/bitcoin/tree/master/src/crypto" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/tree/master/src/crypto</a>) there are these algrithms that as I know connected with gerenation the new keys and master key. So I try to understand what exactly each algorithm does and how they related in Bitcoin Core.</p>

<bitcoin-core-development><hash><bip32-hd-wallets><bip324-p2p-encryption>
What is difference between hmac_sha256, hkdf_sha256 and hmac_sha_512?
Gregory Morychev
# What is difference between hmac_sha256, hkdf_sha256 and hmac_sha_512?

(Asked by: username_0 on 2022-11-01)

In the open source (<https://github.com/bitcoin/bitcoin/tree/master/src/crypto>) there are these algrithms that as I know connected with gerenation the new keys and master key. So I try to understand what exactly each algorithm does and how they related in Bitcoin Core.

**Tags:** bitcoin-core-development, hash, bip32-hd-wallets, bip324-p2p-encryption


---

## 2 answers

### Answer by username_1 on 2022-11-02. Score: 5

They implement 3 distinct cryptographic schemes:

* `HMAC-SHA256` is a MAC (message authentication code, or a keyed hash function) based on SHA-256. It is used in the RFC6979 nonce generation function for deterministic ECDSA signing.
* `HMAC-SHA512` is a MAC based on SHA-512. It is used in the BIP32 key derivation logic.
* `HKDF-SHA256` is a (symmetric) key derivation function (KDF) based on HMAC-256. It is currently unused, but was added in anticipation of being used in the BIP324 (v2 P2P transport layer) implementation.


---

### Answer by username_2 on 2022-11-02. Score: 1

HMAC in the name stands for ["**H**ash-based **M**essage **A**uthentication **C**ode"](https://en.wikipedia.org/wiki/HMAC) and the hmac\_sha\_512 is used to [generate keys](https://en.bitcoin.it/wiki/BIP_0032#Specification:_Key_derivation) in hierarchic-deterministic BIP-0032 wallets, and to [generate a master key](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#from-mnemonic-to-seed) from the seed phrase (BIP-0039), for example [here](https://github.com/bitcoin/bitcoin/blob/93999a5fbe317852307455b6a42d137cc08a0edc/src/key.cpp#L348).

HKDF in the name stands for ["**H**ash-based **K**ey-**D**erivation **F**unction"](https://su21.cs161.org/proj2/crypto/hkdf.html), and there's a comment in the code which describes it:

> A rfc5869 HKDF implementation with HMAC\_SHA256 and fixed key output length of 32 bytes (L=32)

If you look at [rfc5869 specification](https://www.rfc-editor.org/rfc/rfc5869), it uses some HMAC as a primitive in the key derivation scheme, and in `hkdf_sha256` the `hmac_sha_256` is the primitive i.e. `hmac_sha_256` is used for `hkdf_sha256`. The `hmac_sha_256` is also used in [httprpc.cpp](https://github.com/bitcoin/bitcoin/blob/0de36941eca1bff91420dd878eb097db2b1a596c/src/httprpc.cpp#L113) and [torcontrol.cpp](https://github.com/bitcoin/bitcoin/blob/6dc3084eec912cf2abfe18d1c05655defaa45e20/src/torcontrol.cpp#L481).

But what is `hkdf_sha256` used for? It [doesn't seem to be used anywhere](https://github.com/bitcoin/bitcoin/search?q=CHKDF_HMAC_SHA256_L32).


---

