| CRYPTO_ARGON2(3MONOCYPHER) | 3MONOCYPHER | CRYPTO_ARGON2(3MONOCYPHER) |
NAME
password-based key derivationSYNOPSIS
#include
<monocypher.h>
void
crypto_argon2(uint8_t *hash,
uint32_t hash_size, void
*work_area, crypto_argon2_config config,
crypto_argon2_inputs inputs,
crypto_argon2_extras extras);
void
crypto_argon2_init(crypto_argon2_ctx
*ctx, uint32_t hash_size, void
*work_area, crypto_argon2_config config,
crypto_argon2_inputs inputs,
crypto_argon2_extras extras);
void
crypto_argon2_segment(const
crypto_argon2_ctx *ctx, uint32_t pass,
uint32_t slice, uint32_t
lane);
void
crypto_argon2_final(const
crypto_argon2_ctx *ctx, uint8_t *hash);
extern const crypto_argon2_extras crypto_argon2_no_extras;
DESCRIPTION
Argon2 is a resource intensive password-based key derivation scheme optimised for the typical x86-like processor. It runs in constant time with respect to the contents of the password.
Typical applications are password checking (for online services) and key derivation (for encryption). Derived keys can be used to encrypt, for example, private keys or password databases.
crypto_argon2()
computes the hash in the calling thread. The parallel interface described
below can compute lanes in separate threads instead.
The arguments to
crypto_argon2()
are:
- hash
- The output hash. If all parameters to
crypto_argon2() are identical between two calls, then the output hash is also identical. In other words, all input parameters passed to the function influence the output value. - hash_size
- Length of hash, in bytes. This argument should be set to 32 or 64 for compatibility with the crypto_verify32() or crypto_verify64() constant time comparison functions.
- work_area
- Temporary buffer for the algorithm, allocated by the caller. It must be
config.nb_blocks × 1024 bytes big and
suitably aligned for 64-bit integers. If you are not sure how to allocate
that buffer, just use malloc(3).
The work area is automatically wiped by
crypto_argon2(). - config
- A struct of type crypto_argon2_config that
determines the base parameters of this particular instance of Argon2.
These are domain parameters and remain constant between multiple
invocations of
crypto_argon2(). - inputs
- A struct of type crypto_argon2_inputs that contains the actual input parameters.
- extras
- A struct of type crypto_argon2_extras that contains optional extra input parameters, which are not commonly used.
The crypto_argon2_config struct is defined as follows:
typedef struct {
uint32_t algorithm;
uint32_t nb_blocks;
uint32_t nb_passes;
uint32_t nb_lanes;
} crypto_argon2_config;
Its members are:
- algorithm
- This value determines which variant of Argon2 should be used.
CRYPTO_ARGON2_Dindicates Argon2d,CRYPTO_ARGON2_Iindicates Argon2i,CRYPTO_ARGON2_IDindicates Argon2id. - nb_blocks
- The number of blocks for the work area. Must be at least 8 × nb_lanes. A value of 100000 (one hundred megabytes) is a good starting point. If the computation takes too long, reduce this number. If it is too fast, increase it. If it is still too fast with all available memory, increase nb_passes.
- nb_passes
- The number of passes. Must be at least 1. A value of 3 is strongly recommended when using Argon2i; any value lower than 3 enables significantly more efficient attacks.
- nb_lanes
- The number of lanes, or level of parallelism. Must be at least 1. With the
parallel interface, each lane can be computed in its own thread.
crypto_argon2() computes them one after the other. nb_lanes is part of the hash: changing it changes the output, regardless of how many threads compute it. nb_blocks must be at least 8 × nb_lanes.
The crypto_argon2_inputs struct is defined as follows:
typedef struct {
const uint8_t *pass;
const uint8_t *salt;
uint32_t pass_size;
uint32_t salt_size;
} crypto_argon2_inputs;
Its members are:
- pass
- The password to hash. It should be wiped with crypto_wipe() after being hashed.
- pass_size
- Length of pass, in bytes.
- salt
- A password salt. This should be filled with random bytes, generated separately for each password to be hashed. See intro() for advice about generating random bytes (use the operating system's random number generator).
- salt_size
- Length of salt, in bytes. Must be at least 8. 16 is recommended.
The crypto_argon2_extras struct is defined as follows:
typedef struct {
const uint8_t *key;
const uint8_t *ad;
uint32_t key_size;
uint32_t ad_size;
} crypto_argon2_extras;
Its members are:
- key
- A key to use in the hash. May be
NULLif key_size is zero. The key is generally not needed, but it does have some uses. In the context of password derivation, it would be stored separately from the password database and would remain secret even if an attacker were to steal the database. Note that changing the key requires rehashing the user's password, which can only be done when the user logs in. - key_size
- Length of key, in bytes. Must be zero if there is no key.
- ad
- Additional data. May be
NULLif ad_size is zero. This is additional data that goes into the hash, similar to the authenticated encryption construction in crypto_aead_lock(). - ad_size
- Length of ad, in bytes. Must be zero if there is no additional data.
The arguments in the config and extras structs may overlap or point at the same buffer.
Use crypto_verify16(), crypto_verify32(), or crypto_verify64() to compare password hashes to prevent timing attacks.
To select the nb_blocks and nb_passes parameters, it should first be decided how long the computation should take. For user authentication, values somewhere between half a second (convenient) and several seconds (paranoid) are recommended. The computation should use as much memory as can be spared.
Since parameter selection depends on your hardware, some trial and error will be required in order to determine the ideal settings. Argon2i with three iterations and 100000 blocks (one hundred megabytes of memory) is a good starting point. So is Argon2id with one iteration and 300000 blocks. Adjust nb_blocks first. If using all available memory is not slow enough, increase nb_passes.
Parallel interface
A parallel interface is provided. It splits the work of
crypto_argon2()
into segments that can be computed in separate threads. Monocypher creates
no threads: the caller decides which thread computes what. This interface
uses three steps:
- Initialisation with
crypto_argon2_init(), called once. It takes the same arguments ascrypto_argon2(), except for hash, initialises the work area, and sets up a crypto_argon2_ctx. The inputs and extras are no longer needed once it returns. - Computation with
crypto_argon2_segment(). For each pass from 0 to config.nb_passes - 1, then for each slice from 0 to 3, callcrypto_argon2_segment() on every lane from 0 to config.nb_lanes - 1. The lanes of a slice can be computed in any order, in any thread. All lanes must finish a slice before any lane starts the next one. The context is only read, and can be shared by all threads. - Finalisation with
crypto_argon2_final(), called once after the last slice of the last pass. It writes the hash and wipes the work area.
Calling
crypto_argon2()
is the same as calling
crypto_argon2_init(),
then
crypto_argon2_segment()
on every pass, slice, and lane in order, then
crypto_argon2_final().
Both produce the same hash from the same parameters.
RETURN VALUES
These functions return nothing.
EXAMPLES
The following example assumes the existence of
arc4random_buf(), which fills the given buffer with
cryptographically secure random bytes. If
arc4random_buf() does not exist on your system, see
intro() for advice about how to
generate cryptographically secure random bytes.
This example shows how to hash a password with the recommended baseline parameters:
uint8_t hash[32]; /* Output hash */
uint8_t salt[16]; /* Random salt */
crypto_argon2_config config = {
.algorithm = CRYPTO_ARGON2_I, /* Argon2i */
.nb_blocks = 100000, /* 100 megabytes */
.nb_passes = 3, /* 3 iterations */
.nb_lanes = 1 /* Single-threaded */
};
uint8_t password[] = "Okay Password!";
crypto_argon2_inputs inputs = {
.pass = password, /* User password */
.salt = salt, /* Salt for the password */
.pass_size = sizeof(password) - 1, /* Password length */
.salt_size = 16
};
crypto_argon2_extras extras = {0}; /* Extra parameters unused */
/* Allocate work area.
* Note the conversion to size_t.
* Without it we cannot allocate more than 4GiB.
*/
void *work_area = malloc((size_t)config.nb_blocks * 1024);
if (work_area == NULL) {
/* Handle malloc() failure */
/* Wipe secrets if they are no longer needed */
crypto_wipe(password, sizeof(password));
} else {
arc4random_buf(salt, 16);
crypto_argon2(hash, 32, work_area,
config, inputs, extras);
/* Wipe secrets if they are no longer needed */
crypto_wipe(password, sizeof(password));
free(work_area);
}
This example hashes a password with 4 lanes, using the parallel
interface. Each call to crypto_argon2_segment() in
the inner loop may run in its own thread:
uint8_t hash[32]; /* Output hash */
uint8_t salt[16]; /* Random salt */
crypto_argon2_config config = {
.algorithm = CRYPTO_ARGON2_I, /* Argon2i */
.nb_blocks = 100000, /* 100 megabytes */
.nb_passes = 3, /* 3 iterations */
.nb_lanes = 4 /* 4 lanes */
};
uint8_t password[] = "Okay Password!";
crypto_argon2_inputs inputs = {
.pass = password, /* User password */
.salt = salt, /* Salt for the password */
.pass_size = sizeof(password) - 1, /* Password length */
.salt_size = 16
};
crypto_argon2_ctx ctx;
uint32_t pass, slice, lane;
void *work_area = malloc((size_t)config.nb_blocks * 1024);
if (work_area == NULL) {
/* Handle malloc() failure */
/* Wipe secrets if they are no longer needed */
crypto_wipe(password, sizeof(password));
} else {
arc4random_buf(salt, 16);
crypto_argon2_init(&ctx, 32, work_area,
config, inputs, crypto_argon2_no_extras);
/* Inputs are no longer needed after init: wipe secrets */
crypto_wipe(password, sizeof(password));
for (pass = 0; pass < config.nb_passes; pass++) {
for (slice = 0; slice < 4; slice++) {
/* Each lane may run in its own thread */
for (lane = 0; lane < config.nb_lanes; lane++) {
crypto_argon2_segment(&ctx, pass, slice, lane);
}
/* Sync point: wait for all lanes to finish */
}
}
crypto_argon2_final(&ctx, hash); /* Also wipes work_area */
free(work_area);
}
This example shows how one might implement Argon2 using pthreads. Because of the dependencies, it cannot be included in Monocypher itself.
#include <assert.h>
#include <errno.h>
#include <pthread.h>
typedef struct {
pthread_t thread;
pthread_barrier_t *barrier;
crypto_argon2_ctx *ctx;
uint32_t lane;
} thread_data;
static void *argon2_lane(void *ptr)
{
thread_data *data = ptr;
uint32_t lane = data->lane;
uint32_t nb_passes = data->ctx->config.nb_passes;
for (uint32_t pass = 0; pass < nb_passes; pass++) {
for (uint32_t slice = 0; slice < 4; slice++) {
/* Compute current segment */
crypto_argon2_segment(data->ctx, pass, slice, lane);
/* Wait for other threads to do their own segment */
int code = pthread_barrier_wait(data->barrier);
assert(code == 0 ||
code == PTHREAD_BARRIER_SERIAL_THREAD);
}
}
return NULL;
}
/* Argon2, truly parallel.
*
* May fail allocation or pthreads calls.
* Possible return values are:
* - 0: success
* - 1: failure: malloc()
* - 2: failure: pthread_barrier_init()
* - 3: failure: pthread_create()
*/
int argon2_phtreads(uint8_t *hash, uint32_t hash_size,
crypto_argon2_config config,
crypto_argon2_inputs inputs,
crypto_argon2_extras extras)
{
/* Allocate memory (work area and thread context) */
int error = 0;
size_t work_size = config.nb_blocks * 1024;
size_t data_size = config.nb_lanes * sizeof(thread_data);
void *work_area = malloc(work_size);
thread_data *lane_data = malloc(data_size);
if (work_area == NULL || lane_data == NULL) {
error = 1;
goto free;
}
/* Init synchronisation barrier */
pthread_barrier_t barrier;
if (pthread_barrier_init(&barrier, NULL, config.nb_lanes)) {
errno = error;
error = 2;
goto free;
}
/* Init Argon 2 */
crypto_argon2_ctx ctx;
crypto_argon2_init(&ctx, hash_size, work_area,
config, inputs, extras);
/* Launch worker threads */
for (size_t lane = 0; lane < config.nb_lanes; lane++) {
lane_data[lane].barrier = &barrier;
lane_data[lane].ctx = &ctx;
lane_data[lane].lane = lane;
if (pthread_create(&lane_data[lane].thread, NULL,
argon2_lane, lane_data + lane)) {
/* Could not create thread, stop the others */
for (size_t i = 0; i < lane; i++) {
assert(!pthread_cancel(lane_data[i].thread));
}
for (size_t i = 0; i < lane; i++) {
assert(!pthread_join(lane_data[i].thread, NULL));
}
errno = error;
error = 3;
goto barrier;
}
}
/* Wait for worker threads */
for (size_t lane = 0; lane < config.nb_lanes; lane++) {
assert(!pthread_join(lane_data[lane].thread, NULL));
}
crypto_argon2_final(&ctx, hash);
barrier:
pthread_barrier_destroy(&barrier);
free:
free(work_area);
free(lane_data);
return error;
}
SEE ALSO
crypto_aead_lock(), crypto_verify16(), crypto_wipe(), intro()
STANDARDS
These functions implement Argon2 as described in RFC 9106.
HISTORY
In Monocypher 0.1, crypto_argon2i()
implemented Argon2i and had all extra parameters as input. It was then split
into a crypto_argon2i_general() and a simplified
crypto_argon2i() function in Monocypher 1.1.0. Both
were replaced by crypto_argon2() in Monocypher
4.0.0. The crypto_argon2_init(),
crypto_argon2_segment(), and
crypto_argon2_final() functions first appeared in
Monocypher 4.1.0.
CAVEATS
Monocypher does not perform any input validation. Any deviation from the algorithm constants, specified input and output length ranges results in undefined behaviour. Make sure your inputs are correct.
| October 16, 2026 | Debian |