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
//! SQLCrypto is a port of [pysqlsimplecipher](https://github.com/bssthu/pysqlsimplecipher), which is a utility to decrypt and encrypt SQLite databases.

#![deny(missing_docs)]
#![feature(test)]
mod decrypt;
mod error;
mod encrypt;

pub use error::*;
pub use decrypt::*;
pub use encrypt::*;
use aes::Aes256;
use block_modes::block_padding::NoPadding;
use block_modes::Cbc;
use sha1::Sha1;

pub(crate) type Aes = Cbc<Aes256, NoPadding>;
pub(crate) type Hmac = hmac::Hmac<Sha1>;

pub(crate) fn key_derive(key: &[u8], salt: &[u8], hmac: bool) -> ([u8; 32], [u8; 32]) {
    let mut derived_key = [0u8; 32];
    pbkdf2::pbkdf2::<Hmac>(key, salt, 64000, &mut derived_key);
    let mut hmac_salt = [0u8; 16];
    salt.iter().zip(hmac_salt.iter_mut()).for_each(|(byte, slot)| {
        *slot = byte ^ 0x3a;
    });
    let mut hmac_key = [0u8; 32];
    if hmac {
        pbkdf2::pbkdf2::<Hmac>(&derived_key, &hmac_salt, 2, &mut hmac_key);
    }
    (derived_key, hmac_key)
}

#[cfg(test)]
mod tests {
    extern crate test;
    use test::Bencher;
    use wasm_bindgen_test::*;

    #[bench]
    fn decrypt(b: &mut Bencher) {
        let test = std::fs::read("decrypted-sqlcrypto.db").unwrap(); // 100 ms

        b.iter(|| {
            super::decrypt(&mut test.clone(), b"test", 1024).unwrap()
        });
    }

    #[bench]
    fn encrypt(b: &mut Bencher) {
        let test = std::fs::read("bomb-dec.db").unwrap();
        b.iter(|| {
            super::encrypt(&mut test.clone(), b"test").unwrap()
        })
    }

    #[wasm_bindgen_test]
    fn wasm_comp_dec() {
        let mut test = include_bytes!("../decrypted-sqlcrypto.db").to_vec();
        super::decrypt(&mut test[..], b"test", 1024).unwrap();
        assert_eq!(&test[..], include_bytes!("../bomb-dec.db"))
    }

    #[wasm_bindgen_test]
    fn wasm_comp_enc() {
        let mut test = include_bytes!("../bomb-dec.db").to_vec();
        super::encrypt(&mut test[..], b"test").unwrap();
        //assert_eq!(&test[..], include_bytes!("../decrypted-sqlcrypto.db"))

        // can't compare anymore since we generate random ivs and noise

    }

}