8 #include <botan/blowfish.h>
9 #include <botan/loadstor.h>
19 const u32bit* S2 = &S[256];
20 const u32bit* S3 = &S[512];
21 const u32bit* S4 = &S[768];
23 for(
size_t i = 0; i != blocks; ++i)
28 for(
size_t j = 0; j != 16; j += 2)
39 L ^= P[16]; R ^= P[17];
54 const u32bit* S2 = &S[256];
55 const u32bit* S3 = &S[512];
56 const u32bit* S4 = &S[768];
58 for(
size_t i = 0; i != blocks; ++i)
63 for(
size_t j = 17; j != 1; j -= 2)
86 void Blowfish::key_schedule(
const byte key[],
size_t length)
90 const byte null_salt[16] = { 0 };
92 key_expansion(key, length, null_salt);
95 void Blowfish::key_expansion(
const byte key[],
99 for(
size_t i = 0, j = 0; i != 18; ++i, j += 4)
100 P[i] ^=
make_u32bit(key[(j ) % length], key[(j+1) % length],
101 key[(j+2) % length], key[(j+3) % length]);
104 generate_sbox(P, L, R, salt, 0);
105 generate_sbox(S, L, R, salt, 2);
112 const byte salt[16],
size_t workfactor)
114 if(length == 0 || length >= 56)
118 throw std::invalid_argument(
"Bcrypt work factor must be at least 1");
126 throw std::invalid_argument(
"Requested Bcrypt work factor too large");
130 const byte null_salt[16] = { 0 };
132 key_expansion(key, length, salt);
134 const size_t rounds = 1 << workfactor;
136 for(
size_t r = 0;
r != rounds; ++
r)
138 key_expansion(key, length, null_salt);
139 key_expansion(salt, 16, null_salt);
149 size_t salt_off)
const
152 const u32bit* S2 = &S[256];
153 const u32bit* S3 = &S[512];
154 const u32bit* S4 = &S[768];
156 for(
size_t i = 0; i != box.
size(); i += 2)
161 for(
size_t j = 0; j != 16; j += 2)
172 u32bit T = R; R = L ^ P[16]; L = T ^ P[17];
183 std::copy(P_INIT, P_INIT + 18, P.
begin());
184 std::copy(S_INIT, S_INIT + 1024, S.
begin());
void eks_key_schedule(const byte key[], size_t key_length, const byte salt[16], size_t workfactor)
void encrypt_n(const byte in[], byte out[], size_t blocks) const
byte get_byte(size_t byte_num, T input)
void decrypt_n(const byte in[], byte out[], size_t blocks) const
u32bit load_be< u32bit >(const byte in[], size_t off)
void store_be(u16bit in, byte out[2])
u32bit make_u32bit(byte i0, byte i1, byte i2, byte i3)