|
NAME
SYNOPSIS
void
extern const crypto_argon2_extras crypto_argon2_no_extras; DESCRIPTIONArgon2 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. The version provided by Monocypher has no threading support, so the degree of parallelism is limited to 1. This is considered good enough for most purposes. The arguments to
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:
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:
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:
The arguments in the config and extras structs may overlap or point at the same buffer. Use crypto_verify16(3monocypher), crypto_verify32(3monocypher), or crypto_verify64(3monocypher) 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. RETURN VALUES
EXAMPLESThe following example assumes the existence of
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[14] = "Okay Password!";
crypto_argon2_inputs inputs = {
.pass = password, /* User password */
.pass_size = sizeof(password), /* Password length */
.salt = salt, /* Salt for the password */
.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);
}
SEE ALSOcrypto_aead_lock(3monocypher), crypto_verify16(3monocypher), crypto_wipe(3monocypher), intro(3monocypher) STANDARDS
HISTORYIn Monocypher 0.1, CAVEATSMonocypher 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.
|