tor  master
Macros | Functions
crypto_s2k.c File Reference

Functions for deriving keys from human-readable passphrases. More...

#include "compat.h"
#include "crypto.h"
#include "crypto_digest.h"
#include "crypto_rand.h"
#include "crypto_s2k.h"
#include "crypto_util.h"
#include "util.h"
#include <openssl/evp.h>
Include dependency graph for crypto_s2k.c:

Macros

#define CRYPTO_S2K_PRIVATE
 
#define S2K_TYPE_RFC2440   0
 
#define S2K_TYPE_PBKDF2   1
 
#define S2K_TYPE_SCRYPT   2
 
#define PBKDF2_SPEC_LEN   17
 
#define PBKDF2_KEY_LEN   20
 
#define SCRYPT_SPEC_LEN   18
 
#define SCRYPT_KEY_LEN   32
 
#define EXPBIAS   6
 

Functions

void secret_to_key_rfc2440 (char *key_out, size_t key_out_len, const char *secret, size_t secret_len, const char *s2k_specifier)
 
STATIC int secret_to_key_compute_key (uint8_t *key_out, size_t key_out_len, const uint8_t *spec, size_t spec_len, const char *secret, size_t secret_len, int type)
 
int secret_to_key_derivekey (uint8_t *key_out, size_t key_out_len, const uint8_t *spec, size_t spec_len, const char *secret, size_t secret_len)
 
int secret_to_key_make_specifier (uint8_t *buf, size_t buf_len, unsigned flags)
 
int secret_to_key_new (uint8_t *buf, size_t buf_len, size_t *len_out, const char *secret, size_t secret_len, unsigned flags)
 
int secret_to_key_check (const uint8_t *spec_and_key, size_t spec_and_key_len, const char *secret, size_t secret_len)
 

Detailed Description

Functions for deriving keys from human-readable passphrases.

Function Documentation

◆ secret_to_key_check()

int secret_to_key_check ( const uint8_t *  spec_and_key,
size_t  spec_and_key_len,
const char *  secret,
size_t  secret_len 
)

Given a hashed passphrase in spec_and_key of length spec_and_key_len as generated by secret_to_key_new(), verify whether it is a hash of the passphrase secret of length secret_len. Return S2K_OKAY on a match, S2K_BAD_SECRET on a well-formed hash that doesn't match this secret, and another error code on other errors.

◆ secret_to_key_compute_key()

STATIC int secret_to_key_compute_key ( uint8_t *  key_out,
size_t  key_out_len,
const uint8_t *  spec,
size_t  spec_len,
const char *  secret,
size_t  secret_len,
int  type 
)

Helper: given a valid specifier without prefix type byte in spec, whose length must be correct, and given a secret passphrase secret of length secret_len, compute the key and store it into key_out, which must have enough room for secret_to_key_key_len(type) bytes. Return the number of bytes written on success and an error code on failure.

◆ secret_to_key_derivekey()

int secret_to_key_derivekey ( uint8_t *  key_out,
size_t  key_out_len,
const uint8_t *  spec,
size_t  spec_len,
const char *  secret,
size_t  secret_len 
)

Given a specifier previously constructed with secret_to_key_make_specifier in spec of length spec_len, and a secret password in secret of length secret_len, generate key_out_len bytes of cryptographic material in key_out. The native output of the secret-to-key function will be truncated if key_out_len is short, and expanded with HKDF if key_out_len is long. Returns S2K_OKAY on success, and an error code on failure.

Here is the caller graph for this function:

◆ secret_to_key_make_specifier()

int secret_to_key_make_specifier ( uint8_t *  buf,
size_t  buf_len,
unsigned  flags 
)

Construct a new s2k algorithm specifier and salt in buf, according to the bitwise-or of some S2K_FLAG_* options in flags. Up to buf_len bytes of storage may be used in buf. Return the number of bytes used on success and an error code on failure.

◆ secret_to_key_new()

int secret_to_key_new ( uint8_t *  buf,
size_t  buf_len,
size_t *  len_out,
const char *  secret,
size_t  secret_len,
unsigned  flags 
)

Hash a passphrase from secret of length secret_len, according to the bitwise-or of some S2K_FLAG_* options in flags, and store the hash along with salt and hashing parameters into buf. Up to buf_len bytes of storage may be used in buf. Set *len_out to the number of bytes used and return S2K_OKAY on success; and return an error code on failure.

◆ secret_to_key_rfc2440()

void secret_to_key_rfc2440 ( char *  key_out,
size_t  key_out_len,
const char *  secret,
size_t  secret_len,
const char *  s2k_specifier 
)

Implement RFC2440-style iterated-salted S2K conversion: convert the secret_len-byte secret into a key_out_len byte key_out. As in RFC2440, the first 8 bytes of s2k_specifier are a salt; the 9th byte describes how much iteration to do. If key_out_len > DIGEST_LEN, use HDKF to expand the result.