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.
|
2021-11-28 13:40:50 +03:00
|
|
|
pub fn expand_key(key []byte, mut bf Blowfish) {
|
|
|
|
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.
|
2021-11-28 13:40:50 +03:00
|
|
|
pub fn expand_key_with_salt(key []byte, salt []byte, mut bf Blowfish) {
|
|
|
|
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]
|
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[1])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xr)] ^ bf.p[2])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[3])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xr)] ^ bf.p[4])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[5])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xr)] ^ bf.p[6])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[7])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xr)] ^ bf.p[8])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[9])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xr)] ^ bf.p[10])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[11])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xr)] ^ bf.p[12])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[13])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xr)] ^ bf.p[14])
|
2021-11-28 13:40:50 +03:00
|
|
|
xr ^= ((bf.s[0][byte(xl >> 24)] + bf.s[1][byte(xl >> 16)]) ^ bf.s[2][byte(xl >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(xl)] ^ bf.p[15])
|
2021-11-28 13:40:50 +03:00
|
|
|
xl ^= ((bf.s[0][byte(xr >> 24)] + bf.s[1][byte(xr >> 16)]) ^ bf.s[2][byte(xr >> 8)]) +
|
2021-11-28 16:30:13 +03:00
|
|
|
(bf.s[3][byte(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.
|
2021-11-28 13:40:50 +03:00
|
|
|
fn get_next_word(b []byte, pos &int) u32 {
|
|
|
|
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
|
|
|
|
}
|