2021-11-28 13:40:50 +03:00
|
|
|
module blowfish
|
|
|
|
|
2021-11-29 23:19:52 +03:00
|
|
|
// expand_key performs a key expansion on the given Blowfish cipher.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn expand_key(key []u8, mut bf Blowfish) {
|
2021-11-28 13:40:50 +03:00
|
|
|
mut j := 0
|
|
|
|
for i := 0; i < 18; i++ {
|
|
|
|
mut d := u32(0)
|
|
|
|
for k := 0; k < 4; k++ {
|
|
|
|
d = d << 8 | u32(key[j])
|
|
|
|
j++
|
|
|
|
if j >= key.len {
|
|
|
|
j = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bf.p[i] ^= d
|
|
|
|
}
|
|
|
|
|
|
|
|
mut l := u32(0)
|
|
|
|
mut r := u32(0)
|
|
|
|
for i := 0; i < 18; i += 2 {
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.p[i], bf.p[i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 256; i += 2 {
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[0][i], bf.s[0][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
for i := 0; i < 256; i += 2 {
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[1][i], bf.s[1][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
for i := 0; i < 256; i += 2 {
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[2][i], bf.s[2][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
for i := 0; i < 256; i += 2 {
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[3][i], bf.s[3][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 23:19:52 +03:00
|
|
|
// expand_key_with_salt using salt to expand the key.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn expand_key_with_salt(key []u8, salt []u8, mut bf Blowfish) {
|
2021-11-28 13:40:50 +03:00
|
|
|
mut j := 0
|
|
|
|
for i := 0; i < 18; i++ {
|
|
|
|
bf.p[i] ^= get_next_word(key, &j)
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0
|
|
|
|
|
|
|
|
mut l := u32(0)
|
|
|
|
mut r := u32(0)
|
|
|
|
for i := 0; i < 18; i += 2 {
|
|
|
|
l ^= get_next_word(key, &j)
|
|
|
|
r ^= get_next_word(key, &j)
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.p[i], bf.p[i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 256; i += 2 {
|
|
|
|
l ^= get_next_word(key, &j)
|
|
|
|
r ^= get_next_word(key, &j)
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[0][i], bf.s[0][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
for i := 0; i < 256; i += 2 {
|
|
|
|
l ^= get_next_word(key, &j)
|
|
|
|
r ^= get_next_word(key, &j)
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[1][i], bf.s[1][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
for i := 0; i < 256; i += 2 {
|
|
|
|
l ^= get_next_word(key, &j)
|
|
|
|
r ^= get_next_word(key, &j)
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[2][i], bf.s[2][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
for i := 0; i < 256; i += 2 {
|
|
|
|
l ^= get_next_word(key, &j)
|
|
|
|
r ^= get_next_word(key, &j)
|
2021-11-29 23:19:52 +03:00
|
|
|
arr := setup_tables(l, r, mut bf)
|
2021-11-28 13:40:50 +03:00
|
|
|
bf.s[3][i], bf.s[3][i + 1] = arr[0], arr[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 23:19:52 +03:00
|
|
|
// setup_tables sets up the Blowfish cipher's pi and substitution tables.
|
|
|
|
fn setup_tables(l u32, r u32, mut bf Blowfish) []u32 {
|
2021-11-28 13:40:50 +03:00
|
|
|
mut xl := l
|
|
|
|
mut xr := r
|
|
|
|
xl ^= bf.p[0]
|
2022-04-15 14:58:56 +03:00
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[1])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[2])
|
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[3])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[4])
|
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[5])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[6])
|
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[7])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[8])
|
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[9])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[10])
|
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[11])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[12])
|
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[13])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[14])
|
|
|
|
xr ^= ((bf.s[0][u8(xl >> 24)] + bf.s[1][u8(xl >> 16)]) ^ bf.s[2][u8(xl >> 8)]) +
|
|
|
|
(bf.s[3][u8(xl)] ^ bf.p[15])
|
|
|
|
xl ^= ((bf.s[0][u8(xr >> 24)] + bf.s[1][u8(xr >> 16)]) ^ bf.s[2][u8(xr >> 8)]) +
|
|
|
|
(bf.s[3][u8(xr)] ^ bf.p[16])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= bf.p[17]
|
2021-11-28 21:35:18 +03:00
|
|
|
res := [xl, xr]
|
|
|
|
return res
|
2021-11-28 13:40:50 +03:00
|
|
|
}
|
|
|
|
|
2021-11-29 23:19:52 +03:00
|
|
|
// get_next_word returns the next big-endian u32 value from the byte
|
|
|
|
// slice at the given position in a circular manner, updating the position.
|
2022-04-15 15:35:35 +03:00
|
|
|
fn get_next_word(b []u8, pos &int) u32 {
|
2021-11-28 13:40:50 +03:00
|
|
|
mut w := u32(0)
|
|
|
|
mut j := 0
|
|
|
|
unsafe {
|
|
|
|
j = *pos
|
|
|
|
}
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
w = w << 8 | u32(b[j])
|
|
|
|
j++
|
|
|
|
if j >= b.len {
|
|
|
|
j = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
*pos = j
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|