1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#![no_std]
extern crate byte_tools;
extern crate block_cipher_trait;
extern crate generic_array;

use byte_tools::{read_u32v_be, write_u32_be};
use block_cipher_trait::{Block, BlockCipher, BlockCipherVarKey};
use generic_array::typenum::U8;

mod consts;

#[derive(Clone,Copy)]
pub struct Blowfish {
    s: [[u32; 256]; 4],
    p: [u32; 18]
}

fn next_u32_wrap(buf: &[u8], offset: &mut usize) -> u32 {
    let mut v = 0;
    for _ in 0..4 {
        if *offset >= buf.len() {
            *offset = 0;
        }
        v = (v << 8) | buf[*offset] as u32;
        *offset += 1;
    }
    v
}

impl Blowfish {

    fn init_state() -> Blowfish {
        Blowfish {
            p: consts::P,
            s: consts::S,
        }
    }

    fn expand_key(&mut self, key: &[u8]) {
        let mut key_pos = 0;
        for i in 0..18 {
            self.p[i] ^= next_u32_wrap(key, &mut key_pos);
        }
        let mut lr = (0u32, 0u32);
        for i in 0..9 {
            lr = self.encrypt(lr.0, lr.1);
            self.p[2*i] = lr.0;
            self.p[2*i+1] = lr.1;
        }
        for i in 0..4 {
            for j in 0..128 {
                lr = self.encrypt(lr.0, lr.1);
                self.s[i][2*j] = lr.0;
                self.s[i][2*j+1] = lr.1;
            }
        }
    }


    fn round_function(&self, x: u32) -> u32 {
        let a = self.s[0][(x >> 24) as usize];
        let b = self.s[1][((x >> 16) & 0xff) as usize];
        let c = self.s[2][((x >> 8) & 0xff) as usize];
        let d = self.s[3][(x & 0xff) as usize];
        (a.wrapping_add(b) ^ c).wrapping_add(d)
    }

    fn encrypt(&self, mut l: u32, mut r: u32) -> (u32, u32) {
        for i in 0..8 {
            l ^= self.p[2*i];
            r ^= self.round_function(l);
            r ^= self.p[2*i+1];
            l ^= self.round_function(r);
        }
        l ^= self.p[16];
        r ^= self.p[17];
        (r, l)
    }

    fn decrypt(&self, mut l: u32, mut r: u32) -> (u32, u32) {
        for i in (1..9).rev() {
            l ^= self.p[2*i+1];
            r ^= self.round_function(l);
            r ^= self.p[2*i];
            l ^= self.round_function(r);
        }
        l ^= self.p[1];
        r ^= self.p[0];
        (r, l)
    }
}

impl BlockCipher for Blowfish {
    type BlockSize = U8;

    fn encrypt_block(&self, input: &Block<U8>, output: &mut Block<U8>) {
        let mut block = [0u32, 0u32];
        read_u32v_be(&mut block, input);
        let (l, r) = self.encrypt(block[0], block[1]);
        write_u32_be(&mut output[0..4], l);
        write_u32_be(&mut output[4..8], r);
    }

    fn decrypt_block(&self, input: &Block<U8>, output: &mut Block<U8>) {
        let mut block = [0u32, 0u32];
        read_u32v_be(&mut block, input);
        let (l, r) = self.decrypt(block[0], block[1]);
        write_u32_be(&mut output[0..4], l);
        write_u32_be(&mut output[4..8], r);
    }
}

impl BlockCipherVarKey for Blowfish {
    fn new(key: &[u8]) -> Blowfish {
        assert!(4 <= key.len() && key.len() <= 56);
        let mut blowfish = Blowfish::init_state();
        blowfish.expand_key(key);
        blowfish
    }
}

/// Bcrypt extension of blowfish
#[cfg(feature = "bcrypt")]
impl Blowfish {
    pub fn salted_expand_key(&mut self, salt: &[u8], key: &[u8]) {
        let mut key_pos = 0;
        for i in 0..18 {
            self.p[i] ^= next_u32_wrap(key, &mut key_pos);
        }
        let mut lr = (0u32, 0u32);
        let mut salt_pos = 0;
        for i in 0..9 {
            let lk = next_u32_wrap(salt, &mut salt_pos);
            let rk = next_u32_wrap(salt, &mut salt_pos);
            lr = self.encrypt(lr.0 ^ lk, lr.1 ^ rk);

            self.p[2*i] = lr.0;
            self.p[2*i+1] = lr.1;
        }
        for i in 0..4 {
            for j in 0..64 {
                let lk = next_u32_wrap(salt, &mut salt_pos);
                let rk = next_u32_wrap(salt, &mut salt_pos);
                lr = self.encrypt(lr.0 ^ lk, lr.1 ^ rk);

                self.s[i][4*j] = lr.0;
                self.s[i][4*j+1] = lr.1;

                let lk = next_u32_wrap(salt, &mut salt_pos);
                let rk = next_u32_wrap(salt, &mut salt_pos);
                lr = self.encrypt(lr.0 ^ lk, lr.1 ^ rk);

                self.s[i][4*j+2] = lr.0;
                self.s[i][4*j+3] = lr.1;
            }
        }
    }

    pub fn bc_init_state() -> Blowfish {
        Blowfish::init_state()
    }

    pub fn bc_encrypt(&self, l: u32, r: u32) -> (u32, u32) {
        self.encrypt(l, r)
    }

    pub fn bc_expand_key(&mut self, key: &[u8]) {
        self.expand_key(key)
    }
}