forked from cppalliance/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcrypt.cpp
More file actions
566 lines (481 loc) · 15 KB
/
bcrypt.cpp
File metadata and controls
566 lines (481 loc) · 15 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http
//
#include <boost/http/bcrypt.hpp>
#include <boost/capy/ex/execution_context.hpp>
#include <boost/capy/ex/run_async.hpp>
#include <boost/capy/ex/system_context.hpp>
#include <boost/capy/task.hpp>
#include "test_helpers.hpp"
#include <semaphore>
namespace boost {
namespace http {
namespace {
class test_io_context;
inline test_io_context&
default_test_io_context() noexcept;
struct test_executor
{
test_io_context* ctx_ = nullptr;
test_executor() = default;
explicit
test_executor(test_io_context& ctx) noexcept
: ctx_(&ctx)
{
}
bool operator==(test_executor const& other) const noexcept
{
return ctx_ == other.ctx_;
}
test_io_context& context() const noexcept;
void on_work_started() const noexcept {}
void on_work_finished() const noexcept {}
std::coroutine_handle<> dispatch(std::coroutine_handle<void> h) const
{
return h;
}
void post(std::coroutine_handle<void> h) const
{
h.resume();
}
};
class test_io_context : public capy::execution_context
{
public:
using executor_type = test_executor;
executor_type get_executor() noexcept
{
return test_executor(*this);
}
};
inline test_io_context&
default_test_io_context() noexcept
{
static test_io_context ctx;
return ctx;
}
inline test_io_context&
test_executor::context() const noexcept
{
return ctx_ ? *ctx_ : default_test_io_context();
}
static_assert(capy::Executor<test_executor>);
} // namespace
struct bcrypt_test
{
void
test_error_code()
{
// Test error codes can be created
system::error_code ec1 = bcrypt::make_error_code(bcrypt::error::ok);
BOOST_TEST(! ec1);
system::error_code ec2 = bcrypt::make_error_code(bcrypt::error::invalid_salt);
BOOST_TEST(ec2);
BOOST_TEST(ec2.message() == "invalid salt");
system::error_code ec3 = bcrypt::make_error_code(bcrypt::error::invalid_hash);
BOOST_TEST(ec3);
BOOST_TEST(ec3.message() == "invalid hash");
}
void
test_result()
{
// Default construction
bcrypt::result r;
BOOST_TEST(r.empty());
BOOST_TEST(r.size() == 0);
BOOST_TEST(! r);
// After hashing
r = bcrypt::hash("password", 4);
BOOST_TEST(! r.empty());
BOOST_TEST(r.size() == 60);
BOOST_TEST(static_cast<bool>(r));
BOOST_TEST(r.c_str()[60] == '\0');
}
void
test_gen_salt()
{
// Default rounds (10)
bcrypt::result r = bcrypt::gen_salt();
BOOST_TEST(r.size() == 29);
BOOST_TEST(r.str().substr(0, 4) == "$2b$");
BOOST_TEST(r.str().substr(4, 2) == "10");
// Custom rounds
r = bcrypt::gen_salt(12);
BOOST_TEST(r.str().substr(4, 2) == "12");
// Version 2a
r = bcrypt::gen_salt(10, bcrypt::version::v2a);
BOOST_TEST(r.str().substr(0, 4) == "$2a$");
// Different salts each time
bcrypt::result r1 = bcrypt::gen_salt(4);
bcrypt::result r2 = bcrypt::gen_salt(4);
BOOST_TEST(r1.str() != r2.str());
}
void
test_hash_with_rounds()
{
// Basic hash
bcrypt::result r = bcrypt::hash("password", 4);
BOOST_TEST(r.size() == 60);
BOOST_TEST(r.str().substr(0, 7) == "$2b$04$");
// Different passwords produce different hashes
bcrypt::result r1 = bcrypt::hash("password1", 4);
bcrypt::result r2 = bcrypt::hash("password2", 4);
BOOST_TEST(r1.str() != r2.str());
// Same password with different salts produces different hashes
r1 = bcrypt::hash("password", 4);
r2 = bcrypt::hash("password", 4);
BOOST_TEST(r1.str() != r2.str());
}
void
test_hash_with_salt()
{
bcrypt::result salt = bcrypt::gen_salt(4);
// Generate salt, then hash
{
system::error_code ec;
bcrypt::result h = bcrypt::hash("password", salt.str(), ec);
BOOST_TEST(! ec);
BOOST_TEST(h.size() == 60);
}
// Same password + salt = same hash
{
system::error_code ec1;
bcrypt::result hash1 = bcrypt::hash("password", salt.str(), ec1);
BOOST_TEST(! ec1);
system::error_code ec2;
bcrypt::result hash2 = bcrypt::hash("password", salt.str(), ec2);
BOOST_TEST(! ec2);
BOOST_TEST(hash1.str() == hash2.str());
}
// Invalid salt
{
system::error_code ec;
bcrypt::result h = bcrypt::hash("password", "invalid", ec);
BOOST_TEST(ec == bcrypt::error::invalid_salt);
BOOST_TEST(h.empty());
}
// Malformed salt
{
system::error_code ec;
bcrypt::result h = bcrypt::hash("password", "$2b$04$", ec);
BOOST_TEST(ec == bcrypt::error::invalid_salt);
BOOST_TEST(h.empty());
}
}
void
test_compare()
{
bcrypt::result r = bcrypt::hash("correct_password", 4);
// Correct password
{
system::error_code ec;
bool match = bcrypt::compare("correct_password", r.str(), ec);
BOOST_TEST(! ec);
BOOST_TEST(match);
}
// Wrong password
{
system::error_code ec;
bool match = bcrypt::compare("wrong_password", r.str(), ec);
BOOST_TEST(! ec);
BOOST_TEST(! match);
}
// Empty password (should not match)
{
system::error_code ec;
bool match = bcrypt::compare("", r.str(), ec);
BOOST_TEST(! ec);
BOOST_TEST(! match);
}
// Invalid hash
{
system::error_code ec;
bool match = bcrypt::compare("password", "invalid", ec);
BOOST_TEST(ec == bcrypt::error::invalid_hash);
BOOST_TEST(! match);
}
// Malformed hash (wrong length)
{
system::error_code ec;
bool match = bcrypt::compare("password", "$2b$04$abcdefghij", ec);
BOOST_TEST(ec == bcrypt::error::invalid_hash);
BOOST_TEST(! match);
}
}
void
test_get_rounds()
{
// Valid hash
{
system::error_code ec;
unsigned rounds = bcrypt::get_rounds(
"$2b$12$abcdefghijklmnopqrstuuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", ec);
BOOST_TEST(! ec);
BOOST_TEST(rounds == 12);
}
// Different versions
{
system::error_code ec;
unsigned rounds = bcrypt::get_rounds(
"$2a$10$abcdefghijklmnopqrstuuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", ec);
BOOST_TEST(! ec);
BOOST_TEST(rounds == 10);
}
// Invalid format
{
system::error_code ec;
unsigned rounds = bcrypt::get_rounds("invalid", ec);
BOOST_TEST(ec == bcrypt::error::invalid_hash);
BOOST_TEST(rounds == 0);
}
// Missing prefix
{
system::error_code ec;
unsigned rounds = bcrypt::get_rounds("2b$10$abc", ec);
BOOST_TEST(ec == bcrypt::error::invalid_hash);
BOOST_TEST(rounds == 0);
}
}
void
test_known_vectors()
{
// Test vectors verified against reference implementations
// U*U with all-C salt
{
system::error_code ec;
BOOST_TEST(bcrypt::compare("U*U",
"$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW", ec));
BOOST_TEST(! ec);
}
// Empty password
{
system::error_code ec;
BOOST_TEST(bcrypt::compare("",
"$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s.", ec));
BOOST_TEST(! ec);
}
// Test that wrong password fails
{
system::error_code ec;
BOOST_TEST(! bcrypt::compare("wrong",
"$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW", ec));
BOOST_TEST(! ec);
}
}
void
test_password_truncation()
{
// bcrypt truncates passwords to 72 bytes
std::string long_pw(100, 'a');
std::string truncated_pw(72, 'a');
bcrypt::result salt = bcrypt::gen_salt(4);
system::error_code ec1;
bcrypt::result r1 = bcrypt::hash(long_pw, salt.str(), ec1);
BOOST_TEST(! ec1);
system::error_code ec2;
bcrypt::result r2 = bcrypt::hash(truncated_pw, salt.str(), ec2);
BOOST_TEST(! ec2);
// Both should produce the same hash
BOOST_TEST(r1.str() == r2.str());
}
//------------------------------------------------
// Tier 2 -- task API
//------------------------------------------------
void
test_hash_task()
{
test_io_context ctx;
auto ex = ctx.get_executor();
// hash_task produces valid 60-char result
{
bool completed = false;
capy::run_async(ex,
[&](bcrypt::result r)
{
BOOST_TEST(r.size() == 60);
BOOST_TEST(r.str().substr(0, 7) == "$2b$04$");
completed = true;
},
[](std::exception_ptr) {}
)(bcrypt::hash_task("password", 4));
BOOST_TEST(completed);
}
// Different passwords yield different hashes
{
bcrypt::result r1, r2;
bool done1 = false, done2 = false;
capy::run_async(ex,
[&](bcrypt::result r) { r1 = r; done1 = true; },
[](std::exception_ptr) {}
)(bcrypt::hash_task("password1", 4));
capy::run_async(ex,
[&](bcrypt::result r) { r2 = r; done2 = true; },
[](std::exception_ptr) {}
)(bcrypt::hash_task("password2", 4));
BOOST_TEST(done1);
BOOST_TEST(done2);
BOOST_TEST(r1.str() != r2.str());
}
}
void
test_compare_task()
{
test_io_context ctx;
auto ex = ctx.get_executor();
bcrypt::result hashed = bcrypt::hash("correct", 4);
// Correct password matches
{
bool completed = false;
capy::run_async(ex,
[&](bool ok)
{
BOOST_TEST(ok);
completed = true;
},
[](std::exception_ptr) {}
)(bcrypt::compare_task("correct", hashed.str()));
BOOST_TEST(completed);
}
// Wrong password does not match
{
bool completed = false;
capy::run_async(ex,
[&](bool ok)
{
BOOST_TEST(! ok);
completed = true;
},
[](std::exception_ptr) {}
)(bcrypt::compare_task("wrong", hashed.str()));
BOOST_TEST(completed);
}
// Malformed hash throws
{
bool got_exception = false;
capy::run_async(ex,
[](bool) {},
[&](std::exception_ptr ep)
{
got_exception = (ep != nullptr);
}
)(bcrypt::compare_task("pw", "invalid"));
BOOST_TEST(got_exception);
}
}
//------------------------------------------------
// Tier 3 -- friendly async API
//------------------------------------------------
void
test_hash_async()
{
test_io_context ctx;
auto ex = ctx.get_executor();
// hash_async offloads to system pool and produces valid result
{
std::binary_semaphore done(0);
bool pass = false;
auto do_test = []() -> capy::task<>
{
bcrypt::result r =
co_await bcrypt::hash_async("password", 4);
BOOST_TEST(r.size() == 60);
BOOST_TEST(r.str().substr(0, 7) == "$2b$04$");
};
capy::run_async(ex,
[&]() { pass = true; done.release(); },
[&](std::exception_ptr) { done.release(); }
)(do_test());
done.acquire();
BOOST_TEST(pass);
}
}
void
test_compare_async()
{
test_io_context ctx;
auto ex = ctx.get_executor();
bcrypt::result hashed = bcrypt::hash("secret", 4);
std::string stored(hashed.str());
// Correct password
{
std::binary_semaphore done(0);
bool pass = false;
auto do_test = [](
std::string stored_hash) -> capy::task<>
{
bool ok = co_await bcrypt::compare_async(
"secret", stored_hash);
BOOST_TEST(ok);
};
capy::run_async(ex,
[&]() { pass = true; done.release(); },
[&](std::exception_ptr) { done.release(); }
)(do_test(stored));
done.acquire();
BOOST_TEST(pass);
}
// Wrong password
{
std::binary_semaphore done(0);
bool pass = false;
auto do_test = [](
std::string stored_hash) -> capy::task<>
{
bool ok = co_await bcrypt::compare_async(
"wrong", stored_hash);
BOOST_TEST(! ok);
};
capy::run_async(ex,
[&]() { pass = true; done.release(); },
[&](std::exception_ptr) { done.release(); }
)(do_test(stored));
done.acquire();
BOOST_TEST(pass);
}
// Malformed hash throws
{
std::binary_semaphore done(0);
bool got_exception = false;
auto do_test = []() -> capy::task<>
{
co_await bcrypt::compare_async(
"password", "invalid");
};
capy::run_async(ex,
[&]() { done.release(); },
[&](std::exception_ptr ep)
{
got_exception = (ep != nullptr);
done.release();
}
)(do_test());
done.acquire();
BOOST_TEST(got_exception);
}
}
void
run()
{
test_error_code();
test_result();
test_gen_salt();
test_hash_with_rounds();
test_hash_with_salt();
test_compare();
test_get_rounds();
test_known_vectors();
test_password_truncation();
test_hash_task();
test_compare_task();
test_hash_async();
test_compare_async();
}
};
TEST_SUITE(bcrypt_test, "boost.http.bcrypt");
} // http
} // boost