Progress pill
Hash Functions

The algorithms used for derivation

Bitcoin Wallet Architecture

The algorithms used for derivation

  • HMAC-SHA512
  • PBKDF2
In Bitcoin at the application level, in addition to hash functions, cryptographic derivation algorithms are used to generate secure data from initial inputs. Although these algorithms rely on hash functions, they serve different purposes, especially in terms of authentication and key generation. These algorithms retain some of the characteristics of hash functions, such as irreversibility, tamper resistance, and collision resistance.
In Bitcoin wallets, mainly 2 derivation algorithms are used:
  • HMAC (Hash-based Message Authentication Code)
  • PBKDF2 (Password-Based Key Derivation Function 2)
We will explore together the functioning and role of each of them.

HMAC-SHA512

HMAC is a cryptographic algorithm that calculates an authentication code based on a combination of a hash function and a secret key. Bitcoin uses HMAC-SHA512, the variant of HMAC that uses the SHA512 hash function. We have already seen in the previous chapter that SHA512 is part of the same family of hash functions as SHA256, but it produces a 512-bit output.
Here is its general operating scheme with being the input message and a secret key:
Let's study in more detail what happens in this HMAC-SHA512 black box. The HMAC-SHA512 function with:
  • : the arbitrarily sized message chosen by the user (first input);
  • : the arbitrary secret key chosen by the user (second input);
  • : the key adjusted to the size of the hash function blocks (1024 bits for SHA512, or 128 bytes);
  • : the SHA512 hash function;
  • : the XOR (exclusive or) operation;
  • : the concatenation operator, linking bit strings end-to-end;
  • : constant composed of the byte repeated 128 times
  • : constant composed of the byte repeated 128 times.
Before calculating the HMAC, it is necessary to equalize the key and constants according to the block size . For example, if the key is shorter than 128 bytes, it is padded with zeros to reach the size . If is longer than 128 bytes, it is compressed using SHA512, and then zeros are added until it reaches 128 bytes. In this way, an equalized key named is obtained. The values of and are obtained by repeating their base byte ( for , for ) until the size is reached. Thus, with bytes, we have:
Once the preprocessing is done, the HMAC-SHA512 algorithm is defined by the following equation:
This equation is broken down into the following steps:
  • XOR the adjusted key with to obtain ;
  • XOR the adjusted key with to obtain ;
  • Concatenate with the message .
  • Hash this result with SHA512 to obtain an intermediate hash .
  • Concatenate with .
  • Hash this result with SHA512 to obtain the final result .
These steps can be summarized schematically as follows:
HMAC is used in Bitcoin notably for key derivation in HD (Hierarchical Deterministic) wallets (we will talk about this in more detail in the coming chapters) and as a component of PBKDF2.

PBKDF2

PBKDF2 (Password-Based Key Derivation Function 2) is a key derivation algorithm designed to enhance the security of passwords. The algorithm applies a pseudo-random function (here HMAC-SHA512) on a password and a cryptographic salt, and then repeats this operation a certain number of times to produce an output key.
In Bitcoin, PBKDF2 is used to generate the seed of an HD wallet from a mnemonic phrase and a passphrase (but we will talk about this in more detail in the coming chapters).
The PBKDF2 process is as follows, with:
  • : the user's mnemonic phrase;
  • : the optional passphrase to increase security (empty field if no passphrase);
  • : the number of iterations of the function, in our case, it's 2048.
The PBKDF2 function is defined iteratively. Each iteration takes the result of the previous one, passes it through HMAC-SHA512, and combines the successive results to produce the final key:
Schematically, PBKDF2 can be represented as follows:
In this chapter, we have explored the HMAC-SHA512 and PBKDF2 functions, which use hashing functions to ensure the integrity and security of key derivations in the Bitcoin protocol. In the next part, we will look into digital signatures, another cryptographic method widely used in Bitcoin.
Quiz
Quiz1/5
What is the primary role of PBKDF2 in Bitcoin?