Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
pkcs5.h File Reference
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include "hash.h"
Include dependency graph for pkcs5.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define YH_INTERNAL   __attribute__((visibility("hidden")))
 

Functions

bool YH_INTERNAL pkcs5_pbkdf2_hmac (const uint8_t *password, size_t cb_password, const uint8_t *salt, size_t cb_salt, uint64_t iterations, hash_t hash, uint8_t *key, size_t cb_key)
 

Macro Definition Documentation

◆ YH_INTERNAL

#define YH_INTERNAL   __attribute__((visibility("hidden")))

Definition at line 36 of file pkcs5.h.

Function Documentation

◆ pkcs5_pbkdf2_hmac()

bool YH_INTERNAL pkcs5_pbkdf2_hmac ( const uint8_t * password,
size_t cb_password,
const uint8_t * salt,
size_t cb_salt,
uint64_t iterations,
hash_t hash,
uint8_t * key,
size_t cb_key )

Definition at line 27 of file pkcs5.c.

29 {
30 bool res = false;
31
32#ifdef _WIN32_BCRYPT
33 NTSTATUS status = 0;
34 LPCWSTR alg = NULL;
35 BCRYPT_ALG_HANDLE hAlg = 0;
36
37 /* mingw64 defines the BCryptDeriveKeyPBKDF2 function, but its import library
38 *doesn't include the export.
39 **
40 ** Once this is fixed, we can just call the function directly. Until then,
41 *we need to dynamically load the function.
42 */
43
44 typedef NTSTATUS WINAPI (
45 *PFN_BCryptDeriveKeyPBKDF2)(BCRYPT_ALG_HANDLE hPrf, PUCHAR pbPassword,
46 ULONG cbPassword, PUCHAR pbSalt, ULONG cbSalt,
47 ULONGLONG cIterations, PUCHAR pbDerivedKey,
48 ULONG cbDerivedKey, ULONG dwFlags);
49 HMODULE hBCrypt = NULL;
50 PFN_BCryptDeriveKeyPBKDF2 fnBCryptDeriveKeyPBKDF2 = NULL;
51
52 if (!(hBCrypt = LoadLibrary("bcrypt.dll"))) {
53 goto cleanup;
54 }
55
56 if (!(fnBCryptDeriveKeyPBKDF2 = (PFN_BCryptDeriveKeyPBKDF2)(
57 (void (*)(void)) GetProcAddress(hBCrypt, "BCryptDeriveKeyPBKDF2")))) {
58 goto cleanup;
59 }
60
61 if (!(alg = get_hash(hash))) {
62 goto cleanup;
63 }
64
65 if (!BCRYPT_SUCCESS(
66 status = BCryptOpenAlgorithmProvider(&hAlg, alg, NULL,
67 BCRYPT_ALG_HANDLE_HMAC_FLAG))) {
68 goto cleanup;
69 }
70
71 if (!BCRYPT_SUCCESS(
72 status =
73 fnBCryptDeriveKeyPBKDF2(hAlg, (PUCHAR) password, (ULONG) cb_password,
74 (PUCHAR) salt, (ULONG) cb_salt, iterations,
75 key, (ULONG) cb_key, 0))) {
76 goto cleanup;
77 }
78
79 res = true;
80
81cleanup:
82
83 if (hAlg) {
84 BCryptCloseAlgorithmProvider(hAlg, 0);
85 }
86 if (hBCrypt) {
87 FreeLibrary(hBCrypt);
88 }
89
90#else
91 const EVP_MD *md = NULL;
92
93 if (!(md = get_hash(hash))) {
94 return false;
95 }
96
97 /* for some reason openssl always returns 1 for PBKDF2 */
98 if (1 != PKCS5_PBKDF2_HMAC((const char *) password, cb_password, salt,
99 cb_salt, iterations, md, cb_key, key)) {
100 return false;
101 }
102
103 res = true;
104
105#endif
106 return res;
107}
const YH_INTERNAL EVP_MD * get_hash(hash_t hash)
Definition hash.c:42
Here is the call graph for this function: