Skip to content

Commit 57b87bb

Browse files
committed
common: add PBKDF2-HMAC-SHA512 helper
1 parent 6ebd9d4 commit 57b87bb

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/common/crypto.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ int decrypt_rfbdes(void *out, int *out_len, const unsigned char key[8], const vo
3535
/* Encrypts 'in' with the the 16-byte key in 'key' using AES-128-ECB and writes the result to 'out'. */
3636
int encrypt_aes128ecb(void *out, int *out_len, const unsigned char key[16], const void *in, const size_t in_len);
3737

38+
/* Derives key material with PBKDF2-HMAC-SHA512. */
39+
int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint32_t rounds, uint8_t *out, size_t out_len);
40+
3841
/*
3942
Generates a Diffie-Hellman public-private keypair using the generator value 'gen' and prime modulo
4043
'prime', writing the result to 'pub_out' and 'priv_out', which must be 'keylen' in size.

src/common/crypto_included.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ int encrypt_aes128ecb(void *out, int *out_len, const unsigned char key[16], cons
9090
return 0;
9191
}
9292

93+
int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint32_t rounds, uint8_t *out, size_t out_len)
94+
{
95+
(void)password;
96+
(void)password_len;
97+
(void)salt;
98+
(void)salt_len;
99+
(void)rounds;
100+
(void)out;
101+
(void)out_len;
102+
return 0;
103+
}
104+
93105
int dh_generate_keypair(uint8_t *priv_out, uint8_t *pub_out, const uint8_t *gen, const size_t gen_len, const uint8_t *prime, const size_t keylen)
94106
{
95107
return 0;

src/common/crypto_libgcrypt.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,20 @@ int encrypt_aes128ecb(void *out, int *out_len, const unsigned char key[16], cons
220220
return result;
221221
}
222222

223+
int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len,
224+
const uint8_t *salt, size_t salt_len, uint32_t rounds,
225+
uint8_t *out, size_t out_len)
226+
{
227+
gcry_error_t error;
228+
229+
if (!password || !salt || !out || out_len == 0)
230+
return 0;
231+
232+
error = gcry_kdf_derive(password, password_len, GCRY_KDF_PBKDF2, GCRY_MD_SHA512,
233+
salt, salt_len, rounds ? rounds : 1, out_len, out);
234+
return gcry_err_code(error) == GPG_ERR_NO_ERROR;
235+
}
236+
223237
int dh_generate_keypair(uint8_t *priv_out, uint8_t *pub_out, const uint8_t *gen, const size_t gen_len, const uint8_t *prime, const size_t keylen)
224238
{
225239
int result = 0;

src/common/crypto_openssl.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ int encrypt_aes128ecb(void *out, int *out_len, const unsigned char key[16], cons
199199
return result;
200200
}
201201

202+
int pbkdf2_hmac_sha512(const uint8_t *password, size_t password_len,
203+
const uint8_t *salt, size_t salt_len, uint32_t rounds,
204+
uint8_t *out, size_t out_len)
205+
{
206+
if (!password || !salt || !out || out_len == 0)
207+
return 0;
208+
return PKCS5_PBKDF2_HMAC((const char *)password, (int)password_len, salt,
209+
(int)salt_len, rounds ? (int)rounds : 1,
210+
EVP_sha512(), (int)out_len, out) == 1;
211+
}
212+
202213
static void pad_leading_zeros(uint8_t *out, const size_t current_len, const size_t expected_len) {
203214
if (current_len >= expected_len || expected_len < 1)
204215
return;

0 commit comments

Comments
 (0)