-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdf.hpp
170 lines (127 loc) · 4.07 KB
/
kdf.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <openssl/core_names.h> /* OSSL_KDF_* */
#include <openssl/params.h> /* OSSL_PARAM_* */
#include <openssl/thread.h> /* OSSL_set_max_threads */
#include <openssl/kdf.h> /* EVP_KDF_* */
#include <openssl/evp.h>
#include <cmath>
#include "./types.h"
#include "./error.hpp"
// aes256
#define KEY_SIZE (256 / 8)
#define SALT_SIZE (128 / 8)
#include <chrono>
using namespace std::chrono;
// @note multiply by double to allow fractional units
#define MiB(x) 1024. * x
#define GiB(x) MiB(1024) * x
void kdf(byte *password, byte password_size, byte *salt, byte salt_size, byte *key)
{
EVP_KDF *kdf = EVP_KDF_fetch(NULL, "ARGON2D", NULL);
if (kdf == NULL)
handle_ossl_error();
EVP_KDF_CTX *ctx = EVP_KDF_CTX_new(kdf);
if (ctx == NULL)
handle_ossl_error();
// https://www.rfc-editor.org/rfc/rfc9106.html#name-parameter-choice
size_t
lanes = 4,
threads = 2,
iterations = 3;
// https://www.openssl.org/docs/manmaster/man7/EVP_KDF-ARGON2.html#:~:text=%22memcost%22%20(-,OSSL_KDF_PARAM_ARGON2_MEMCOST,-)%20%3Cunsigned%20integer%3E
// mem cost is 1k blocks
// first increase memory to max affordable
// then increase number of iterations
size_t
memcost = GiB(4);
// 1gb 1466ms
// 2gb 2769ms
// 3gb 3926ms
// 4gb 6242ms
#pragma region @todo config tweak
// @todo store values in .mars file
// utilize struct
// printf("\e[33m\e[3m\e[1m"
// "Do you want to tweak KDF parameters? "
// "(y/"
// "\e[4m"
// "N"
// "\e[0m"
// "\e[33m\e[3m\e[1m"
// "):\e[0m ");
// std::string input;
// getline(std::cin, input);
// if (input == "y" || input == "yes")
// {
// printf("\e[96m"
// "Mem cost (GiB):"
// "\e[0m ");
// getline(std::cin, input);
// memcost = GiB(std::stod(input));
// ///
// printf("\e[96m"
// "Lanes:"
// "\e[0m ");
// getline(std::cin, input);
// lanes = std::stoul(input);
// threads = std::ceil(lanes / 2.);
// ///
// printf("\e[96m"
// "Iterations:"
// "\e[0m ");
// getline(std::cin, input);
// iterations = std::stoul(input);
// }
#pragma endregion
printf(
"\e[95m[KDF config]\e[0m\n"
"\e[3m"
"Iterations: %zu, memcost: %zu, lanes: %zu, threads: %zu"
"\e[0m\n",
iterations, memcost, lanes, threads);
// https://github.com/openssl/openssl/issues/21305
/* required if threads > 1 */
if (!OSSL_set_max_threads(NULL, threads))
handle_ossl_error();
// @note @bug this caused stack smashing error!!!
// i had 6 params instaed of 7
OSSL_PARAM params[7];
OSSL_PARAM *p = params;
*p++ = OSSL_PARAM_construct_size_t(
OSSL_KDF_PARAM_THREADS,
&threads);
*p++ = OSSL_PARAM_construct_size_t(
OSSL_KDF_PARAM_ARGON2_LANES,
&lanes);
*p++ = OSSL_PARAM_construct_size_t(
OSSL_KDF_PARAM_ITER,
&iterations);
*p++ = OSSL_PARAM_construct_size_t(
OSSL_KDF_PARAM_ARGON2_MEMCOST,
&memcost);
*p++ = OSSL_PARAM_construct_octet_string(
OSSL_KDF_PARAM_PASSWORD,
password,
password_size);
*p++ = OSSL_PARAM_construct_octet_string(
OSSL_KDF_PARAM_SALT,
salt,
salt_size);
*p++ = OSSL_PARAM_construct_end();
auto start = high_resolution_clock::now();
printf("\e[3m"
"KDF is running..."
"\e[0m");
// force output before starting kdf
fflush(stdout);
if (!EVP_KDF_derive(ctx, key, KEY_SIZE, params))
handle_ossl_error();
auto finish = high_resolution_clock::now();
// @note use \r to overwrite current line
printf("\r\e[3m"
"KDF duration = %ld ms"
"\e[0m\n",
duration_cast<milliseconds>(finish - start).count());
EVP_KDF_free(kdf);
EVP_KDF_CTX_free(ctx);
// OSSL_set_max_threads(NULL, 0);
}