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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
#include "hybbx/crypto_config.h"
#include "hybbx/config.h"
#include "hybbx/log.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static hybbx_crypto_config_t g_crypto_config;
static int g_crypto_config_ready;
static int str_ieq(const char *a, const char *b)
{
if (a == NULL || b == NULL) {
return 0;
}
while (*a != '\0' && *b != '\0') {
char ca = (char)(*a >= 'A' && *a <= 'Z' ? *a + 32 : *a);
char cb = (char)(*b >= 'A' && *b <= 'Z' ? *b + 32 : *b);
if (ca != cb) {
return 0;
}
a++;
b++;
}
return *a == '\0' && *b == '\0';
}
static hybbx_password_hash_backend_t parse_password_hash(const char *value)
{
if (value == NULL || value[0] == '\0' ||
str_ieq(value, "tinysha256") || str_ieq(value, "bundled") ||
str_ieq(value, "default")) {
return HYBBX_PASSWORD_HASH_TINYSHA256;
}
if (str_ieq(value, "openssl") || str_ieq(value, "libcrypto")) {
return HYBBX_PASSWORD_HASH_OPENSSL;
}
return HYBBX_PASSWORD_HASH_TINYSHA256;
}
static hybbx_aes_gcm_backend_t parse_aes_gcm(const char *value)
{
if (value == NULL || value[0] == '\0' ||
str_ieq(value, "tinyaes") || str_ieq(value, "bundled") ||
str_ieq(value, "default")) {
return HYBBX_AES_GCM_TINYAES;
}
if (str_ieq(value, "openssl") || str_ieq(value, "libcrypto")) {
return HYBBX_AES_GCM_OPENSSL;
}
return HYBBX_AES_GCM_TINYAES;
}
static hybbx_chacha_backend_t parse_chacha(const char *value)
{
if (value == NULL || value[0] == '\0' ||
str_ieq(value, "monocypher") || str_ieq(value, "bundled") ||
str_ieq(value, "default")) {
return HYBBX_CHACHA_MONOCYPHER;
}
if (str_ieq(value, "libsodium") || str_ieq(value, "sodium")) {
return HYBBX_CHACHA_LIBSODIUM;
}
return HYBBX_CHACHA_MONOCYPHER;
}
static hybbx_x25519_backend_t parse_x25519(const char *value)
{
if (value == NULL || value[0] == '\0' ||
str_ieq(value, "monocypher") || str_ieq(value, "bundled") ||
str_ieq(value, "default")) {
return HYBBX_X25519_MONOCYPHER;
}
if (str_ieq(value, "libsodium") || str_ieq(value, "sodium")) {
return HYBBX_X25519_LIBSODIUM;
}
return HYBBX_X25519_MONOCYPHER;
}
static hybbx_random_backend_t parse_random(const char *value)
{
if (value == NULL || value[0] == '\0' ||
str_ieq(value, "system") || str_ieq(value, "bundled") ||
str_ieq(value, "default") || str_ieq(value, "getrandom") ||
str_ieq(value, "urandom")) {
return HYBBX_RANDOM_SYSTEM;
}
if (str_ieq(value, "openssl") || str_ieq(value, "libcrypto")) {
return HYBBX_RANDOM_OPENSSL;
}
return HYBBX_RANDOM_SYSTEM;
}
static void resolve_backends(hybbx_crypto_config_t *cfg)
{
#if !defined(HYBBX_HAVE_OPENSSL)
if (cfg->password_hash == HYBBX_PASSWORD_HASH_OPENSSL) {
hybbx_log_warn("[crypto] password_hash=openssl requested but HyBBX was not "
"built with OpenSSL; using tinysha256");
cfg->password_hash = HYBBX_PASSWORD_HASH_TINYSHA256;
}
if (cfg->aes_gcm == HYBBX_AES_GCM_OPENSSL) {
hybbx_log_warn("[crypto] aes_gcm=openssl requested but HyBBX was not built "
"with OpenSSL; using tinyaes");
cfg->aes_gcm = HYBBX_AES_GCM_TINYAES;
}
if (cfg->random == HYBBX_RANDOM_OPENSSL) {
hybbx_log_warn("[crypto] random=openssl requested but HyBBX was not built "
"with OpenSSL; using system");
cfg->random = HYBBX_RANDOM_SYSTEM;
}
#endif
#if !defined(HYBBX_HAVE_LIBSODIUM)
if (cfg->chacha == HYBBX_CHACHA_LIBSODIUM) {
hybbx_log_warn("[crypto] chacha=libsodium requested but HyBBX was not built "
"with libsodium; using monocypher");
cfg->chacha = HYBBX_CHACHA_MONOCYPHER;
}
if (cfg->x25519 == HYBBX_X25519_LIBSODIUM) {
hybbx_log_warn("[crypto] x25519=libsodium requested but HyBBX was not built "
"with libsodium; using monocypher");
cfg->x25519 = HYBBX_X25519_MONOCYPHER;
}
#endif
}
void hybbx_crypto_config_defaults(hybbx_crypto_config_t *cfg)
{
if (cfg == NULL) {
return;
}
cfg->password_hash = HYBBX_PASSWORD_HASH_TINYSHA256;
cfg->aes_gcm = HYBBX_AES_GCM_TINYAES;
cfg->chacha = HYBBX_CHACHA_MONOCYPHER;
cfg->x25519 = HYBBX_X25519_MONOCYPHER;
cfg->random = HYBBX_RANDOM_SYSTEM;
}
void hybbx_crypto_config_apply(const hybbx_config_t *config)
{
const char *value;
hybbx_crypto_config_defaults(&g_crypto_config);
if (config != NULL) {
value = hybbx_config_get(config, "crypto", "password_hash", NULL);
g_crypto_config.password_hash = parse_password_hash(value);
value = hybbx_config_get(config, "crypto", "aes_gcm", NULL);
g_crypto_config.aes_gcm = parse_aes_gcm(value);
value = hybbx_config_get(config, "crypto", "chacha", NULL);
g_crypto_config.chacha = parse_chacha(value);
value = hybbx_config_get(config, "crypto", "x25519", NULL);
g_crypto_config.x25519 = parse_x25519(value);
value = hybbx_config_get(config, "crypto", "random", NULL);
g_crypto_config.random = parse_random(value);
}
resolve_backends(&g_crypto_config);
g_crypto_config_ready = 1;
hybbx_log_info("[crypto] password_hash=%s aes_gcm=%s chacha=%s x25519=%s random=%s",
hybbx_password_hash_backend_name(g_crypto_config.password_hash),
hybbx_aes_gcm_backend_name(g_crypto_config.aes_gcm),
hybbx_chacha_backend_name(g_crypto_config.chacha),
hybbx_x25519_backend_name(g_crypto_config.x25519),
hybbx_random_backend_name(g_crypto_config.random));
}
const hybbx_crypto_config_t *hybbx_crypto_config_get(void)
{
if (!g_crypto_config_ready) {
hybbx_crypto_config_defaults(&g_crypto_config);
g_crypto_config_ready = 1;
}
return &g_crypto_config;
}
const char *hybbx_password_hash_backend_name(hybbx_password_hash_backend_t b)
{
switch (b) {
case HYBBX_PASSWORD_HASH_OPENSSL:
return "openssl";
default:
return "tinysha256";
}
}
const char *hybbx_aes_gcm_backend_name(hybbx_aes_gcm_backend_t b)
{
switch (b) {
case HYBBX_AES_GCM_OPENSSL:
return "openssl";
default:
return "tinyaes";
}
}
const char *hybbx_chacha_backend_name(hybbx_chacha_backend_t b)
{
switch (b) {
case HYBBX_CHACHA_LIBSODIUM:
return "libsodium";
default:
return "monocypher";
}
}
const char *hybbx_x25519_backend_name(hybbx_x25519_backend_t b)
{
switch (b) {
case HYBBX_X25519_LIBSODIUM:
return "libsodium";
default:
return "monocypher";
}
}
const char *hybbx_random_backend_name(hybbx_random_backend_t b)
{
switch (b) {
case HYBBX_RANDOM_OPENSSL:
return "openssl";
default:
return "system";
}
}
|