GCC Code Coverage Report


Directory: src/gate/
File: src/gate/encode/passwords.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 98 112 87.5%
Functions: 9 9 100.0%
Branches: 34 56 60.7%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/encode/passwords.h"
30 #include "gate/results.h"
31 #include "gate/strings.h"
32 #include "gate/randomgen.h"
33 #include "gate/encode/sha256hash.h"
34 #include "gate/encode/base64.h"
35
36 #if defined(GATE_SYS_POSIX)
37 # define GATE_ENCODE_CRYPT_IMPL 1
38 #endif
39
40
41 2 static void generate_salt(char salt[16])
42 {
43 gate_randomsession_t rand_session;
44 gate_result_t result;
45 static gate_string_t const valid_chars = GATE_STRING_INIT_STATIC("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890");
46 2 char const* valid_chars_ptr = gate_string_ptr(&valid_chars, 0);
47 2 gate_size_t const valid_chars_len = gate_string_length(&valid_chars);
48
49 2 result = gate_randomgenerator_create(&rand_session);
50
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_SUCCEEDED(result))
51 {
52 2 gate_uint64_t num = 0;
53 size_t n;
54
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
34 for(n = 0; n != 16; ++n)
55 {
56 gate_size_t pos;
57 32 gate_randomgenerator_get_num(&rand_session, &num);
58 32 pos = (gate_size_t)(num % valid_chars_len);
59 32 salt[n] = valid_chars_ptr[pos];
60 }
61 2 gate_randomgenerator_destroy(&rand_session);
62 }
63 2 }
64
65 #if defined(GATE_ENCODE_CRYPT_IMPL)
66
67 #include <unistd.h>
68 #include "gate/libraries.h"
69 #include "gate/platforms.h"
70
71 typedef char*(*crypt_func_t)(char const* key, char const* salt);
72
73 2 static crypt_func_t load_libcrypt()
74 {
75 static crypt_func_t crypt_func = NULL;
76
77
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(crypt_func != NULL)
78 {
79 1 return crypt_func;
80 }
81 else
82 {
83 static gate_library_t lib_libcrypt = NULL;
84 gate_result_t result;
85
86
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (lib_libcrypt == NULL)
87 {
88 #if defined(GATE_SYS_DARWIN)
89 static gate_string_t const libcrypt_name = GATE_STRING_INIT_STATIC("/usr/lib/libSystem.B.dylib");
90 static gate_string_t const libcrypt_name_alt = GATE_STRING_INIT_STATIC("/usr/lib/libSystem.B.dylib");
91 #else
92 static gate_string_t const libcrypt_name = GATE_STRING_INIT_STATIC("libcrypt.so");
93 static gate_string_t const libcrypt_name_alt = GATE_STRING_INIT_STATIC("libcrypt.so.1");
94 #endif
95 1 result = gate_library_open(&libcrypt_name, &lib_libcrypt, GATE_LIBRARY_FLAG_DEFAULT);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(result))
97 {
98 /* try alternative library name */
99 ✗ result = gate_library_open(&libcrypt_name_alt, &lib_libcrypt, GATE_LIBRARY_FLAG_DEFAULT);
100 }
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(result))
102 {
103 ✗ return false;
104 }
105 }
106 1 result = gate_library_get_function_name(lib_libcrypt, "crypt", &crypt_func);
107
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(GATE_SUCCEEDED(result))
108 {
109 1 return crypt_func;
110 }
111 ✗ return NULL;
112 }
113 }
114
115 1 gate_result_t cryptlib_hash(char const* passwd, gate_size_t passwd_length, gate_uint8_t* passhash, gate_size_t* ptr_passhash_len)
116 {
117 gate_result_t ret;
118
119 do
120 {
121 char input[GATE_PASSWORD_MAX_LENGTH];
122 char setting[64];
123 char salt_buffer[16];
124 char const* ptr_setting;
125 gate_size_t hash_len;
126 1 char* ptr_output = (char*)passhash;
127
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 gate_size_t out_len = ptr_passhash_len ? *ptr_passhash_len : 0;
128 gate_size_t out_used;
129 gate_strbuilder_t builder;
130 1 crypt_func_t crypt_func = load_libcrypt();
131
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(crypt_func == NULL)
133 {
134 ✗ ret = GATE_RESULT_NOTAVAILABLE;
135 ✗ break;
136 }
137
138
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 if (!passwd || !passwd_length || !passhash || !ptr_passhash_len)
139 {
140 ✗ ret = GATE_RESULT_INVALIDARG;
141 ✗ break;
142 }
143 1 gate_str_print_text(input, sizeof(input), passwd, passwd_length);
144
145 1 generate_salt(salt_buffer);
146
147 1 gate_strbuilder_create_static(&builder, setting, sizeof(setting), 0);
148
149 1 gate_strbuilder_append_cstr(&builder, "$5$");
150 1 gate_strbuilder_append_text(&builder, salt_buffer, sizeof(salt_buffer));
151 1 ptr_setting = gate_strbuilder_ptr(&builder, 0);
152
153 /* NOTICE: crypt() uses a global buffer -> global lock */
154 {
155 char* ptr_hash;
156 1 ret = gate_platform_lock();
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
158
159 1 ptr_hash = crypt_func(input, ptr_setting);
160 1 hash_len = gate_str_length(ptr_hash);
161 1 *ptr_passhash_len = gate_str_print_text(ptr_output, out_len, ptr_hash, hash_len);
162
163 1 gate_platform_unlock();
164 }
165
166 1 ret = GATE_RESULT_OK;
167 } while (0);
168
169 1 return ret;
170 }
171
172 1 gate_result_t cryptlib_check(char const* passwd, gate_size_t passwd_length, gate_uint8_t const* passhash, gate_size_t passhash_len)
173 {
174 gate_result_t ret;
175
176 do
177 {
178 char input[GATE_PASSWORD_MAX_LENGTH];
179 char setting[GATE_PASSWORD_MAX_LENGTH];
180 gate_size_t setting_length;
181 char output[GATE_PASSWORD_MAX_LENGTH];
182 gate_size_t output_used;
183 gate_size_t hash_len;
184 1 crypt_func_t crypt_func = load_libcrypt();
185
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(crypt_func == NULL)
187 {
188 ✗ ret = GATE_RESULT_NOTAVAILABLE;
189 ✗ break;
190 }
191
192
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 if (!passwd || !passwd_length || !passhash || !passhash_len)
193 {
194 ✗ ret = GATE_RESULT_INVALIDARG;
195 ✗ break;
196 }
197 1 gate_str_print_text(input, sizeof(input), passwd, passwd_length);
198 1 setting_length = gate_str_print_text(setting, sizeof(setting), (char const*)passhash, passhash_len);
199
200 /* NOTICE: crypt() uses a global buffer -> global lock */
201 {
202 char const* ptr_hash;
203 1 ret = gate_platform_lock();
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
205
206 1 ptr_hash = crypt_func(input, setting);
207 1 hash_len = gate_str_length(ptr_hash);
208 1 output_used = gate_str_print_text(output, sizeof(output), ptr_hash, hash_len);
209
210 1 gate_platform_unlock();
211 }
212
213
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if(gate_str_compare(setting, setting_length, output, output_used) == 0)
214 {
215 1 ret = GATE_RESULT_OK;
216 }
217 else
218 {
219 ✗ ret = GATE_RESULT_NOMATCH;
220 }
221 } while (0);
222
223 1 return ret;
224 }
225
226 #endif /* GATE_ENCODE_CRYPT_IMPL */
227
228 3 static void pass_hash_sha256_salt(
229 char const* passwd, gate_size_t passwd_length,
230 char const* salt, gate_size_t salt_length,
231 gate_uint8_t* passhash, gate_size_t* ptr_passhash_len)
232 {
233 static gate_size_t const rounds = 5000;
234 gate_size_t ndx;
235
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 gate_size_t hash_capacity = ptr_passhash_len ? *ptr_passhash_len : 0;
236
237 gate_strbuilder_t builder;
238 gate_string_t binstr;
239 gate_sha256_t sha;
240 gate_sha256_result_t sharesult;
241
242 3 gate_sha256_init(&sha);
243
244
2/2
✓ Branch 0 taken 15000 times.
✓ Branch 1 taken 3 times.
15003 for(ndx = 0; ndx != rounds; ++ndx)
245 {
246 15000 gate_sha256_update(&sha, passwd, passwd_length);
247 15000 gate_sha256_update(&sha, salt, salt_length);
248 15000 gate_sha256_update(&sha, passwd, passwd_length);
249 15000 gate_sha256_finish(&sha, &sharesult);
250
251 15000 gate_sha256_init(&sha);
252 15000 gate_sha256_update(&sha, sharesult.hash, sizeof(sharesult.hash));
253 }
254
255 3 gate_strbuilder_create_static(&builder, (char*)passhash, hash_capacity, 0);
256 3 gate_strbuilder_append_cstr(&builder, "$5$");
257 3 gate_strbuilder_append_text(&builder, salt, salt_length);
258 3 gate_strbuilder_append_cstr(&builder, "$");
259
260 3 gate_string_create_static_len(&binstr, (char const*)&sharesult.hash[0], sizeof(sharesult.hash));
261
262 3 gate_base64_encode(&binstr, &builder);
263 3 *ptr_passhash_len = gate_strbuilder_length(&builder);
264 3 }
265
266 1 static gate_result_t pass_hash_sha256(char const* passwd, gate_size_t passwd_length, gate_uint8_t* passhash, gate_size_t* ptr_passhash_len)
267 {
268 char salt[16];
269 1 generate_salt(salt);
270 1 pass_hash_sha256_salt(passwd, passwd_length, salt, sizeof(salt), passhash, ptr_passhash_len);
271 1 return GATE_RESULT_OK;
272 }
273
274 2 gate_result_t pass_check_sha256(char const* passwd, gate_size_t passwd_length, gate_uint8_t const* passhash, gate_size_t passhash_len)
275 {
276 gate_size_t pos;
277 char const* ptr_salt;
278 gate_size_t salt_len;
279 gate_uint8_t test_hash[GATE_PASSWORD_MAX_HASH_LENGTH];
280 2 gate_size_t test_hash_len = sizeof(test_hash);
281
282
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!gate_str_starts_with((char const*)passhash, passhash_len, "$5$", 3))
283 {
284 ✗ return GATE_RESULT_NOMATCH;
285 }
286 2 pos = gate_str_char_pos((char const*)passhash, passhash_len, '$', 3);
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_STR_NPOS == pos)
288 {
289 ✗ return GATE_RESULT_NOMATCH;
290 }
291 2 ptr_salt = (char const*)(&passhash[3]);
292 // "$5$salt$hash"
293 2 salt_len = pos - 3;
294
295 2 pass_hash_sha256_salt(passwd, passwd_length, ptr_salt, salt_len, test_hash, &test_hash_len);
296
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 if (gate_str_compare((char const*)passhash, passhash_len, (char const*)test_hash, test_hash_len) != 0)
297 {
298 1 return GATE_RESULT_NOMATCH;
299 }
300 1 return GATE_RESULT_OK;
301 }
302
303
304
305 2 gate_result_t gate_password_hash(
306 char const* passwd, gate_size_t passwd_length,
307 gate_uint8_t* passhash, gate_size_t* ptr_passhash_len,
308 gate_enumint_t flags)
309 {
310 #if defined(GATE_ENCODE_CRYPT_IMPL)
311
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (GATE_FLAG_ENABLED(flags, GATE_PASSWORD_FLAG_SYSTEM))
312 {
313 1 return cryptlib_hash(passwd, passwd_length, passhash, ptr_passhash_len);
314 }
315 #else
316 GATE_UNUSED_ARG(flags);
317 #endif /* GATE_ENCODE_CRYPT_IMPL */
318
319 1 return pass_hash_sha256(passwd, passwd_length, passhash, ptr_passhash_len);
320 }
321
322
323
324 3 gate_result_t gate_password_check(
325 char const* passwd, gate_size_t passwd_length,
326 gate_uint8_t const* passhash, gate_size_t passhash_len,
327 gate_enumint_t flags)
328 {
329 #if defined(GATE_ENCODE_CRYPT_IMPL)
330
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (GATE_FLAG_ENABLED(flags, GATE_PASSWORD_FLAG_SYSTEM))
331 {
332 1 return cryptlib_check(passwd, passwd_length, passhash, passhash_len);
333 }
334 #else
335 GATE_UNUSED_ARG(flags);
336 #endif /* GATE_ENCODE_CRYPT_IMPL */
337
338 2 return pass_check_sha256(passwd, passwd_length, passhash, passhash_len);
339 }
340