8#include <botan/bcrypt.h>
10#include <botan/base64.h>
12#include <botan/internal/blowfish.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/parsing.h>
22uint8_t base64_to_bcrypt_encoding(uint8_t c) {
36 ret = is_ab.select(c -
'a' +
'Y', ret);
37 ret = is_cz.select(c - 2, ret);
38 ret = is_CZ.select(c - 2, ret);
39 ret = is_01.select(c -
'0' +
'y', ret);
40 ret = is_29.select(c -
'2' +
'0', ret);
41 ret = is_A.select(
'.', ret);
42 ret = is_B.select(
'/', ret);
43 ret = is_plus.select(
'8', ret);
44 ret = is_slash.select(
'9', ret);
49uint8_t bcrypt_encoding_to_base64(uint8_t c) {
63 ret = is_ax.select(c -
'a' +
'c', ret);
64 ret = is_yz.select(c -
'y' +
'0', ret);
65 ret = is_AX.select(c -
'A' +
'C', ret);
66 ret = is_YZ.select(c -
'Y' +
'a', ret);
67 ret = is_07.select(c -
'0' +
'2', ret);
68 ret = is_8.select(
'+', ret);
69 ret = is_9.select(
'/', ret);
70 ret = is_dot.select(
'A', ret);
71 ret = is_slash.select(
'B', ret);
76std::string bcrypt_base64_encode(
const uint8_t input[],
size_t length) {
79 while(!b64.empty() && b64[b64.size() - 1] ==
'=') {
80 b64 = b64.substr(0, b64.size() - 1);
83 for(
size_t i = 0; i != b64.size(); ++i) {
84 b64[i] =
static_cast<char>(base64_to_bcrypt_encoding(
static_cast<uint8_t
>(b64[i])));
90std::vector<uint8_t> bcrypt_base64_decode(std::string_view input) {
91 std::string translated;
92 for(
size_t i = 0; i != input.size(); ++i) {
93 char c = bcrypt_encoding_to_base64(
static_cast<uint8_t
>(input[i]));
94 translated.push_back(c);
100std::string make_bcrypt(std::string_view pass,
const std::vector<uint8_t>& salt, uint16_t work_factor,
char version) {
107 BOTAN_ARG_CHECK(work_factor >= 4 && work_factor <= 18,
"Invalid bcrypt work factor");
109 alignas(64)
static const uint8_t BCRYPT_MAGIC[8 * 3] = {0x4F, 0x72, 0x70, 0x68, 0x65, 0x61, 0x6E, 0x42,
110 0x65, 0x68, 0x6F, 0x6C, 0x64, 0x65, 0x72, 0x53,
111 0x63, 0x72, 0x79, 0x44, 0x6F, 0x75, 0x62, 0x74};
119 blowfish.salted_set_key(
120 pass_with_trailing_null.data(), pass_with_trailing_null.size(), salt.data(), salt.size(), work_factor);
122 std::vector<uint8_t> ctext(BCRYPT_MAGIC, BCRYPT_MAGIC + 8 * 3);
124 for(
size_t i = 0; i != 64; ++i) {
125 blowfish.encrypt_n(ctext.data(), ctext.data(), 3);
128 std::string salt_b64 = bcrypt_base64_encode(salt.data(), salt.size());
130 std::string work_factor_str = std::to_string(work_factor);
131 if(work_factor_str.length() == 1) {
132 work_factor_str =
"0" + work_factor_str;
135 return fmt(
"$2{}${}${}{}",
138 salt_b64.substr(0, 22),
139 bcrypt_base64_encode(ctext.data(), ctext.size() - 1));
150 if(version !=
'a' && version !=
'b' && version !=
'y') {
151 throw Invalid_Argument(
"Unknown bcrypt version '" + std::string(1, version) +
"'");
154 std::vector<uint8_t> salt;
156 return make_bcrypt(pass, salt, work_factor, version);
160 if(hash.size() != 60 || hash[0] !=
'$' || hash[1] !=
'2' || hash[3] !=
'$' || hash[6] !=
'$') {
164 const char bcrypt_version = hash[2];
166 if(bcrypt_version !=
'a' && bcrypt_version !=
'b' && bcrypt_version !=
'y') {
170 const uint16_t workfactor =
to_uint16(hash.substr(4, 2));
172 const std::vector<uint8_t> salt = bcrypt_base64_decode(hash.substr(7, 22));
173 if(salt.size() != 16) {
177 const std::string compare = make_bcrypt(pass, salt, workfactor, bcrypt_version);
#define BOTAN_ARG_CHECK(expr, msg)
static constexpr Mask< T > is_within_range(T v, T l, T u)
static constexpr Mask< T > is_equal(T x, T y)
void random_vec(std::span< uint8_t > v)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
uint16_t to_uint16(std::string_view str)
std::string fmt(std::string_view format, const T &... args)
std::string generate_bcrypt(std::string_view pass, RandomNumberGenerator &rng, uint16_t work_factor, char version)
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
std::vector< T > unlock(const secure_vector< T > &in)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)
const uint8_t * cast_char_ptr_to_uint8(const char *s)
bool check_bcrypt(std::string_view pass, std::string_view hash)