Skip to content

Commit

Permalink
Merge pull request #1
Browse files Browse the repository at this point in the history
- C interface prefix rename from vapor to bb
  • Loading branch information
tib authored Feb 9, 2024
2 parents f1b8829 + f4f0088 commit aae110a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 65 deletions.
16 changes: 8 additions & 8 deletions Sources/Bcrypt/BCrypt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ extension Collection where Element: Equatable {
///
/// Use BCrypt to create hashes for sensitive information like passwords.
///
/// try BCrypt.hash("vapor", cost: 4)
/// try BCrypt.hash("binary_birds", cost: 4)
///
/// BCrypt uses a random salt each time it creates a hash. To verify hashes, use the `verify(_:matches)` method.
///
/// let hash = try BCrypt.hash("vapor", cost: 4)
/// try BCrypt.verify("vapor", created: hash) // true
/// let hash = try BCrypt.hash("binary_birds", cost: 4)
/// try BCrypt.verify("binary_birds", created: hash) // true
///
/// https://en.wikipedia.org/wiki/Bcrypt
public var Bcrypt: BCryptDigest {
Expand All @@ -84,7 +84,7 @@ public var Bcrypt: BCryptDigest {
/// Creates and verifies BCrypt hashes. Normally you will not need to initialize one of these classes and you will
/// use the global `BCrypt` convenience instead.
///
/// try BCrypt.hash("vapor", cost: 4)
/// try BCrypt.hash("binary_birds", cost: 4)
///
/// See `BCrypt` for more information.
public final class BCryptDigest {
Expand Down Expand Up @@ -134,7 +134,7 @@ public final class BCryptDigest {

let hashedBytes = UnsafeMutablePointer<Int8>.allocate(capacity: 128)
defer { hashedBytes.deallocate() }
let hashingResult = vapor_bcrypt_hashpass(
let hashingResult = bb_bcrypt_hashpass(
plaintext,
normalizedSalt,
hashedBytes,
Expand All @@ -152,8 +152,8 @@ public final class BCryptDigest {
/// Verifies an existing BCrypt hash matches the supplied plaintext value. Verification works by parsing the salt and version from
/// the existing digest and using that information to hash the plaintext data. If hash digests match, this method returns `true`.
///
/// let hash = try BCrypt.hash("vapor", cost: 4)
/// try BCrypt.verify("vapor", created: hash) // true
/// let hash = try BCrypt.hash("binary_birds", cost: 4)
/// try BCrypt.verify("binary_birds", created: hash) // true
/// try BCrypt.verify("foo", created: hash) // false
///
/// - parameters:
Expand Down Expand Up @@ -247,7 +247,7 @@ public final class BCryptDigest {
let encodedBytes = UnsafeMutablePointer<Int8>.allocate(capacity: 25)
defer { encodedBytes.deallocate() }
let res = data.withUnsafeBytes { bytes in
vapor_encode_base64(
bb_encode_base64(
encodedBytes,
bytes.baseAddress?.assumingMemoryBound(to: UInt8.self),
bytes.count
Expand Down
20 changes: 10 additions & 10 deletions Sources/CBcrypt/bcrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static int decode_base64(u_int8_t *, size_t, const char *);
* the core bcrypt function
*/
int
vapor_bcrypt_hashpass(const char *key, const char *salt, char *encrypted,
bb_bcrypt_hashpass(const char *key, const char *salt, char *encrypted,
size_t encryptedlen)
{
blf_ctx state;
Expand Down Expand Up @@ -116,22 +116,22 @@ vapor_bcrypt_hashpass(const char *key, const char *salt, char *encrypted,
salt_len = BCRYPT_MAXSALT;

/* Setting up S-Boxes and Subkeys */
Vapor_Blowfish_initstate(&state);
Vapor_Blowfish_expandstate(&state, csalt, salt_len,
BB_Blowfish_initstate(&state);
BB_Blowfish_expandstate(&state, csalt, salt_len,
(u_int8_t *) key, key_len);
for (k = 0; k < rounds; k++) {
Vapor_Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
Vapor_Blowfish_expand0state(&state, csalt, salt_len);
BB_Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
BB_Blowfish_expand0state(&state, csalt, salt_len);
}

/* This can be precomputed later */
j = 0;
for (i = 0; i < BCRYPT_WORDS; i++)
cdata[i] = Vapor_Blowfish_stream2word(ciphertext, 4 * BCRYPT_WORDS, &j);
cdata[i] = BB_Blowfish_stream2word(ciphertext, 4 * BCRYPT_WORDS, &j);

/* Now do the encryption */
for (k = 0; k < 64; k++)
vapor_blf_enc(&state, cdata, BCRYPT_WORDS / 2);
bb_blf_enc(&state, cdata, BCRYPT_WORDS / 2);

for (i = 0; i < BCRYPT_WORDS; i++) {
ciphertext[4 * i + 3] = cdata[i] & 0xff;
Expand All @@ -145,8 +145,8 @@ vapor_bcrypt_hashpass(const char *key, const char *salt, char *encrypted,


snprintf(encrypted, 8, "$2%c$%2.2u$", minor, logr);
vapor_encode_base64(encrypted + 7, csalt, BCRYPT_MAXSALT);
vapor_encode_base64(encrypted + 7 + 22, ciphertext, 4 * BCRYPT_WORDS - 1);
bb_encode_base64(encrypted + 7, csalt, BCRYPT_MAXSALT);
bb_encode_base64(encrypted + 7 + 22, ciphertext, 4 * BCRYPT_WORDS - 1);
explicit_bzero(&state, sizeof(state));
explicit_bzero(ciphertext, sizeof(ciphertext));
explicit_bzero(csalt, sizeof(csalt));
Expand Down Expand Up @@ -228,7 +228,7 @@ decode_base64(u_int8_t *buffer, size_t len, const char *b64data)
* This works without = padding.
*/
int
vapor_encode_base64(char *b64buffer, const u_int8_t *data, size_t len)
bb_encode_base64(char *b64buffer, const u_int8_t *data, size_t len)
{
u_int8_t *bp = (u_int8_t *)b64buffer;
const u_int8_t *p = data;
Expand Down
4 changes: 2 additions & 2 deletions Sources/CBcrypt/bcrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ typedef uint64_t u_int64_t;
#define BCRYPT_HASHSPACE 61


int vapor_bcrypt_hashpass(const char *key, const char *salt, char *encrypted, size_t encryptedlen);
int vapor_encode_base64(char *, const u_int8_t *, size_t);
int bb_bcrypt_hashpass(const char *key, const char *salt, char *encrypted, size_t encryptedlen);
int bb_encode_base64(char *, const u_int8_t *, size_t);
64 changes: 32 additions & 32 deletions Sources/CBcrypt/blf.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
#define BLFRND(s,p,i,j,n) (i ^= F(s,j) ^ (p)[n])

void
Vapor_Blowfish_encipher(blf_ctx *c, u_int32_t *x)
BB_Blowfish_encipher(blf_ctx *c, u_int32_t *x)
{
u_int32_t Xl;
u_int32_t Xr;
Expand All @@ -86,7 +86,7 @@ Vapor_Blowfish_encipher(blf_ctx *c, u_int32_t *x)
}

void
Vapor_Blowfish_decipher(blf_ctx *c, u_int32_t *x)
BB_Blowfish_decipher(blf_ctx *c, u_int32_t *x)
{
u_int32_t Xl;
u_int32_t Xr;
Expand All @@ -111,7 +111,7 @@ Vapor_Blowfish_decipher(blf_ctx *c, u_int32_t *x)
}

void
Vapor_Blowfish_initstate(blf_ctx *c)
BB_Blowfish_initstate(blf_ctx *c)
{
/* P-box and S-box tables initialized with digits of Pi */

Expand Down Expand Up @@ -391,7 +391,7 @@ Vapor_Blowfish_initstate(blf_ctx *c)
}

u_int32_t
Vapor_Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes,
BB_Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes,
u_int16_t *current)
{
u_int8_t i;
Expand All @@ -412,7 +412,7 @@ Vapor_Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes,
}

void
Vapor_Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes)
BB_Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes)
{
u_int16_t i;
u_int16_t j;
Expand All @@ -423,23 +423,23 @@ Vapor_Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes)
j = 0;
for (i = 0; i < BLF_N + 2; i++) {
/* Extract 4 int8 to 1 int32 from keystream */
temp = Vapor_Blowfish_stream2word(key, keybytes, &j);
temp = BB_Blowfish_stream2word(key, keybytes, &j);
c->P[i] = c->P[i] ^ temp;
}

j = 0;
data[0] = 0x00000000;
data[1] = 0x00000000;
for (i = 0; i < BLF_N + 2; i += 2) {
Vapor_Blowfish_encipher(c, data);
BB_Blowfish_encipher(c, data);

c->P[i] = data[0];
c->P[i + 1] = data[1];
}

for (i = 0; i < 4; i++) {
for (k = 0; k < 256; k += 2) {
Vapor_Blowfish_encipher(c, data);
BB_Blowfish_encipher(c, data);

c->S[i][k] = data[0];
c->S[i][k + 1] = data[1];
Expand All @@ -449,7 +449,7 @@ Vapor_Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes)


void
Vapor_Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes,
BB_Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes,
const u_int8_t *key, u_int16_t keybytes)
{
u_int16_t i;
Expand All @@ -461,27 +461,27 @@ Vapor_Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes
j = 0;
for (i = 0; i < BLF_N + 2; i++) {
/* Extract 4 int8 to 1 int32 from keystream */
temp = Vapor_Blowfish_stream2word(key, keybytes, &j);
temp = BB_Blowfish_stream2word(key, keybytes, &j);
c->P[i] = c->P[i] ^ temp;
}

j = 0;
d[0] = 0x00000000;
d[1] = 0x00000000;
for (i = 0; i < BLF_N + 2; i += 2) {
d[0] ^= Vapor_Blowfish_stream2word(data, databytes, &j);
d[1] ^= Vapor_Blowfish_stream2word(data, databytes, &j);
Vapor_Blowfish_encipher(c, d);
d[0] ^= BB_Blowfish_stream2word(data, databytes, &j);
d[1] ^= BB_Blowfish_stream2word(data, databytes, &j);
BB_Blowfish_encipher(c, d);

c->P[i] = d[0];
c->P[i + 1] = d[1];
}

for (i = 0; i < 4; i++) {
for (k = 0; k < 256; k += 2) {
d[0] ^= Vapor_Blowfish_stream2word(data, databytes, &j);
d[1] ^= Vapor_Blowfish_stream2word(data, databytes, &j);
Vapor_Blowfish_encipher(c, d);
d[0] ^= BB_Blowfish_stream2word(data, databytes, &j);
d[1] ^= BB_Blowfish_stream2word(data, databytes, &j);
BB_Blowfish_encipher(c, d);

c->S[i][k] = d[0];
c->S[i][k + 1] = d[1];
Expand All @@ -491,43 +491,43 @@ Vapor_Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes
}

void
vapor_blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len)
bb_blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len)
{
/* Initialize S-boxes and subkeys with Pi */
Vapor_Blowfish_initstate(c);
BB_Blowfish_initstate(c);

/* Transform S-boxes and subkeys with key */
Vapor_Blowfish_expand0state(c, k, len);
BB_Blowfish_expand0state(c, k, len);
}

void
vapor_blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
bb_blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
{
u_int32_t *d;
u_int16_t i;

d = data;
for (i = 0; i < blocks; i++) {
Vapor_Blowfish_encipher(c, d);
BB_Blowfish_encipher(c, d);
d += 2;
}
}

void
vapor_blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
bb_blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
{
u_int32_t *d;
u_int16_t i;

d = data;
for (i = 0; i < blocks; i++) {
Vapor_Blowfish_decipher(c, d);
BB_Blowfish_decipher(c, d);
d += 2;
}
}

void
vapor_blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
bb_blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
{
u_int32_t l, r, d[2];
u_int32_t i;
Expand All @@ -537,7 +537,7 @@ vapor_blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
d[0] = l;
d[1] = r;
Vapor_Blowfish_encipher(c, d);
BB_Blowfish_encipher(c, d);
l = d[0];
r = d[1];
data[0] = l >> 24 & 0xff;
Expand All @@ -553,7 +553,7 @@ vapor_blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
}

void
vapor_blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
bb_blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
{
u_int32_t l, r, d[2];
u_int32_t i;
Expand All @@ -563,7 +563,7 @@ vapor_blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
d[0] = l;
d[1] = r;
Vapor_Blowfish_decipher(c, d);
BB_Blowfish_decipher(c, d);
l = d[0];
r = d[1];
data[0] = l >> 24 & 0xff;
Expand All @@ -579,7 +579,7 @@ vapor_blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
}

void
vapor_blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len)
bb_blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len)
{
u_int32_t l, r, d[2];
u_int32_t i, j;
Expand All @@ -591,7 +591,7 @@ vapor_blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len)
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
d[0] = l;
d[1] = r;
Vapor_Blowfish_encipher(c, d);
BB_Blowfish_encipher(c, d);
l = d[0];
r = d[1];
data[0] = l >> 24 & 0xff;
Expand All @@ -608,7 +608,7 @@ vapor_blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len)
}

void
vapor_blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len)
bb_blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len)
{
u_int32_t l, r, d[2];
u_int8_t *iv;
Expand All @@ -621,7 +621,7 @@ vapor_blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len)
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
d[0] = l;
d[1] = r;
Vapor_Blowfish_decipher(c, d);
BB_Blowfish_decipher(c, d);
l = d[0];
r = d[1];
data[0] = l >> 24 & 0xff;
Expand All @@ -641,7 +641,7 @@ vapor_blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len)
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
d[0] = l;
d[1] = r;
Vapor_Blowfish_decipher(c, d);
BB_Blowfish_decipher(c, d);
l = d[0];
r = d[1];
data[0] = l >> 24 & 0xff;
Expand Down
26 changes: 13 additions & 13 deletions Sources/CBcrypt/blf.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,25 @@ typedef struct BlowfishContext {
* Blowfish_expand0state( state, key, keylen )
*/

void Vapor_Blowfish_encipher(blf_ctx *, u_int32_t *);
void Vapor_Blowfish_decipher(blf_ctx *, u_int32_t *);
void Vapor_Blowfish_initstate(blf_ctx *);
void Vapor_Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t);
void Vapor_Blowfish_expandstate(blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t);
void BB_Blowfish_encipher(blf_ctx *, u_int32_t *);
void BB_Blowfish_decipher(blf_ctx *, u_int32_t *);
void BB_Blowfish_initstate(blf_ctx *);
void BB_Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t);
void BB_Blowfish_expandstate(blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t);

/* Standard Blowfish */

void vapor_blf_key(blf_ctx *, const u_int8_t *, u_int16_t);
void vapor_blf_enc(blf_ctx *, u_int32_t *, u_int16_t);
void vapor_blf_dec(blf_ctx *, u_int32_t *, u_int16_t);
void bb_blf_key(blf_ctx *, const u_int8_t *, u_int16_t);
void bb_blf_enc(blf_ctx *, u_int32_t *, u_int16_t);
void bb_blf_dec(blf_ctx *, u_int32_t *, u_int16_t);

/* Converts u_int8_t to u_int32_t */
u_int32_t Vapor_Blowfish_stream2word(const u_int8_t *, u_int16_t ,
u_int32_t BB_Blowfish_stream2word(const u_int8_t *, u_int16_t ,
u_int16_t *);

void vapor_blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t);
void vapor_blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t);
void bb_blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t);
void bb_blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t);

void vapor_blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
void vapor_blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
void bb_blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
void bb_blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
#endif

0 comments on commit aae110a

Please sign in to comment.