mini-rcrypt 0.1.1

A minimal Rust implementation of OpenBSD Blowfish password hashing code.
Documentation
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use crate::{
    base64::{decode_base64, encode_base64},
    utils::{generate_random_numbers, get_byte_from_char, get_byte_from_number},
    BCRYPT_SALT_LEN, BF_CRYPT_CIPHERTEXT, BLOWFISH_NUM_ROUNDS, P_ORIG, S_ORIG,
};

#[allow(non_snake_case)]
pub struct BCrypt {
    pub P: Vec<isize>,
    pub S: Vec<isize>,
}

impl BCrypt {
    fn new() -> Self {
        Self {
            P: vec![],
            S: vec![],
        }
    }

    fn init_key(&mut self) -> () {
        self.P = P_ORIG.clone().to_vec();
        self.S = S_ORIG.clone().to_vec();
    }

    fn encipher(&mut self, mut lr: Vec<isize>, off: usize) -> Vec<isize> {
        let mut i;
        let mut n;
        let mut l = lr[off];
        let mut r = lr[off + 1];

        l ^= self.P[0];

        i = 0;

        while i <= BLOWFISH_NUM_ROUNDS - 2 {
            // Feistel substitution on left word
            n = self.S[(l >> 24) as usize & 0xff];
            n = n
                .checked_add(self.S[0x100 | ((l >> 16) as usize & 0xff)])
                .unwrap_or(0);
            n ^= self.S[0x200 | ((l >> 8) as usize & 0xff)];
            n = n
                .checked_add(self.S[0x300 | (l & 0xff) as usize])
                .unwrap_or(0);

            i = i + 1;

            r ^= n ^ self.P[i];

            // Feistel substitution on right word
            n = self.S[(r >> 24) as usize & 0xff];
            n = n
                .checked_add(self.S[0x100 | ((r >> 16) as usize & 0xff)])
                .unwrap_or(0);
            n ^= self.S[0x200 | ((r >> 8) as usize & 0xff)];
            n = n
                .checked_add(self.S[0x300 | (r & 0xff) as usize])
                .unwrap_or(0);

            i = i + 1;

            l ^= n ^ self.P[i];
        }

        lr[off] = r ^ self.P[BLOWFISH_NUM_ROUNDS + 1];
        lr[off + 1] = l;

        lr
    }

    fn streamtoword(data: Vec<isize>, mut offp: Vec<usize>) -> (isize, Vec<usize>) {
        let mut i = 0;
        let mut word = 0;
        let mut off = offp[0];

        while i < 4 {
            word = (word << 8) | (data[off] & 0xff);
            off = (off + 1) % data.len();

            i = i + 1;
        }

        offp[0] = off;

        return (word, offp);
    }

    fn key(&mut self, key: Vec<isize>) {
        let mut i;
        let mut koffp = vec![0];
        let mut lr = vec![0, 0];
        let plen = self.P.len();
        let slen = self.S.len();

        i = 0;

        while i < plen {
            let (word, offp) = BCrypt::streamtoword(key.clone(), koffp);

            self.P[i] = self.P[i] ^ word;
            koffp = offp;

            i = i + 1;
        }

        i = 0;

        while i < plen {
            lr = self.encipher(lr.clone(), 0);
            self.P[i] = lr[0];
            self.P[i + 1] = lr[1];

            i = i + 2;
        }

        i = 0;

        while i < slen {
            lr = self.encipher(lr, 0);
            self.S[i] = lr[0];
            self.S[i + 1] = lr[1];

            i = i + 2;
        }
    }

    fn ekskey(&mut self, data: Vec<isize>, key: Vec<isize>) {
        let mut i;
        let mut koffp = vec![0];
        let mut doffp = vec![0];
        let mut lr = vec![0, 0];
        let plen = self.P.len();
        let slen = self.S.len();

        i = 0;

        while i < plen {
            let (word, offp) = BCrypt::streamtoword(key.clone(), koffp);

            self.P[i] = self.P[i] ^ word;
            koffp = offp;

            i = i + 1;
        }

        i = 0;

        while i < plen {
            let (word, offp) = BCrypt::streamtoword(data.clone(), doffp.clone());

            lr[0] ^= word;
            doffp = offp;

            let (word, offp) = BCrypt::streamtoword(data.clone(), doffp.clone());

            lr[1] ^= word;
            doffp = offp;

            lr = self.encipher(lr, 0);

            self.P[i] = lr[0];
            self.P[i + 1] = lr[1];

            i = i + 2;
        }

        i = 0;

        while i < slen {
            let (word, offp) = BCrypt::streamtoword(data.clone(), doffp.clone());

            lr[0] ^= word;
            doffp = offp;

            let (word, offp) = BCrypt::streamtoword(data.clone(), doffp.clone());

            lr[1] ^= word;
            doffp = offp;

            lr = self.encipher(lr, 0);

            self.S[i] = lr[0];
            self.S[i + 1] = lr[1];

            i = i + 2;
        }
    }

    fn crypt_raw<'a>(
        &mut self,
        password: Vec<isize>,
        salt: Vec<isize>,
        log_rounds: usize,
        data: Vec<isize>,
    ) -> Result<Vec<isize>, &'a str> {
        let mut i;
        let mut j;
        let mut cdata = data;
        let clen = cdata.len();
        let mut ret: Vec<isize> = vec![];

        let rounds = 1 << log_rounds;

        if log_rounds < 4 || log_rounds > 30 {
            return Err("Bad number of rounds");
        }

        if salt.len() != BCRYPT_SALT_LEN as usize {
            return Err("Bad salt length");
        }

        self.init_key();
        self.ekskey(salt.clone(), password.clone());

        i = 0;

        while i != rounds {
            self.key(password.clone());
            self.key(salt.clone());

            i = i + 1;
        }

        i = 0;
        j = 0;

        while i < 64 {
            while j < (clen >> 1) {
                cdata = self.encipher(cdata.clone(), j << 1);

                j = j + 1;
            }

            i = i + 1;
        }

        i = 0;

        while i < clen {
            ret.push(get_byte_from_number((cdata[i] >> 24) & 0xff));
            ret.push(get_byte_from_number((cdata[i] >> 16) & 0xff));
            ret.push(get_byte_from_number((cdata[i] >> 8) & 0xff));
            ret.push(get_byte_from_number(cdata[i] & 0xff));

            i = i + 1;
        }

        Ok(ret)
    }

    fn password_to_bytes<'a>(password: String) -> Result<Vec<isize>, &'a str> {
        let mut passwordb: Vec<isize> = vec![];

        for c in password.chars() {
            let code = c as isize;

            if code < 128 {
                passwordb.push(code);
            } else if code > 127 && code < 2048 {
                passwordb.push((code >> 6) | 192);
                passwordb.push((code & 63) | 128);
            } else if code >= 55296 && code <= 56319 {
                let next_code = password.chars().nth(1).unwrap_or('\0') as isize;

                if next_code < 56320 || next_code > 57343 {
                    return Err("utf-16 Decoding error: trail surrogate not in the range of 0xdc00 through 0xdfff");
                }

                let decoded = ((code - 55296) << 10) + (next_code - 56320) + 65536;
                passwordb.push((decoded >> 18) | 240);
                passwordb.push(((decoded >> 12) & 63) | 128);
                passwordb.push(((decoded >> 6) & 63) | 128);
                passwordb.push((decoded & 63) | 128);
            } else {
                passwordb.push((code >> 12) | 224);
                passwordb.push(((code >> 6) & 63) | 128);
                passwordb.push((code & 63) | 128);
            }
        }

        Ok(passwordb)
    }

    pub fn hashpw<'a>(password: String, salt: String) -> Result<String, &'a str> {
        let mut bcrypt = BCrypt::new();
        let real_salt: String;
        let passwordb: Vec<isize>;
        let saltb: Vec<isize>;
        let hashed: Vec<isize>;
        let mut minor: char = '0';
        let rounds: usize;
        let off;
        let mut result: String = "".into();

        if salt.chars().nth(0) != Some('$') || salt.chars().nth(1) != Some('2') {
            return Err("Invalid salt version");
        }

        if salt.chars().nth(2) == Some('$') {
            off = 3;
        } else {
            match salt.chars().nth(2) {
                Some(c) => {
                    minor = c;

                    if minor != 'a' || salt.chars().nth(3) != Some('$') {
                        return Err("Invalid salt revision");
                    }

                    off = 4;
                }
                None => return Err("Invalid salt revision"),
            }
        }

        if salt.chars().nth(off + 2) > Some('$') {
            return Err("Missing salt rounds");
        }

        match salt[off..off + 2].parse::<usize>() {
            Ok(x) => rounds = x,
            Err(_) => return Err("Missing salt rounds"),
        }

        real_salt = salt[off + 3..off + 25].to_owned();
        let password_ = format!("{}{}", password, if minor >= 'a' { "\0" } else { "" });

        match BCrypt::password_to_bytes(password_) {
            Ok(x) => passwordb = x,
            Err(err) => return Err(err),
        }

        match decode_base64(real_salt, BCRYPT_SALT_LEN as usize) {
            Ok(x) => saltb = x,
            Err(err) => return Err(err),
        }

        match bcrypt.crypt_raw(
            passwordb,
            saltb.clone(),
            rounds,
            BF_CRYPT_CIPHERTEXT.to_vec().clone(),
        ) {
            Ok(x) => hashed = x,
            Err(err) => return Err(err),
        }

        result.push_str("$2");

        if minor >= 'a' {
            result.push(minor);
        }

        result.push('$');

        if rounds < 10 {
            result.push('0');
        }

        if rounds > 30 {
            return Err("Rounds exceeds maximum (30)");
        }

        result.push_str(rounds.to_string().as_str());
        result.push('$');

        match encode_base64(saltb.clone(), saltb.len()) {
            Ok(x) => result.push_str(x.as_str()),
            Err(err) => return Err(err),
        }

        match encode_base64(hashed, BF_CRYPT_CIPHERTEXT.to_vec().clone().len() * 4 - 1) {
            Ok(x) => result.push_str(x.as_str()),
            Err(err) => return Err(err),
        }

        Ok(result)
    }

    pub fn gensalt<'a>(rounds: u32) -> Result<String, &'a str> {
        if rounds < 4 || rounds > 30 {
            return Err("Rounds excededs maximum (30)!");
        }

        let mut output: String = "".into();

        output.push_str("$2a$");

        if rounds < 10 {
            output.push('0');
        }

        output.push_str(rounds.to_string().as_str());
        output.push('$');

        match encode_base64(generate_random_numbers(), BCRYPT_SALT_LEN as usize) {
            Ok(x) => output.push_str(x.as_str()),
            Err(err) => return Err(err),
        }

        Ok(output)
    }

    pub fn checkpw(plaintext: String, hashed: String) -> bool {
        let off;

        if hashed.chars().nth(0) != Some('$') || hashed.chars().nth(1) != Some('2') {
            return false;
        }

        if hashed.chars().nth(2) == Some('$') {
            off = 3;
        } else {
            match hashed.chars().nth(2) {
                Some(minor) => {
                    if (minor != 'a' && minor != 'b') || hashed.chars().nth(3) != Some('$') {
                        return false;
                    }

                    off = 4;
                }
                None => return false,
            }
        }

        let salt = hashed[..off + 25].to_owned();

        match BCrypt::hashpw(plaintext, salt) {
            Ok(try_pass) => {
                let mut ret = 0;

                let mut i = 0;

                while i < hashed.len() {
                    match (hashed.chars().nth(i), try_pass.chars().nth(i)) {
                        (Some(x), Some(y)) => ret |= get_byte_from_char(x) ^ get_byte_from_char(y),
                        _ => return false,
                    }

                    i = i + 1;
                }

                return ret == 0;
            }
            Err(_) => return false,
        }
    }
}