aboutsummaryrefslogtreecommitdiffstats
path: root/src/cipherstring.rs
blob: 75edec421bd654fab7f71785196765ec8c9742f6 (plain) (blame)
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
use crate::prelude::*;

use block_modes::BlockMode as _;
use hmac::Mac as _;

pub struct CipherString {
    ty: u8,
    iv: Vec<u8>,
    ciphertext: Vec<u8>,
    mac: Option<Vec<u8>>,
}

impl CipherString {
    pub fn new(s: &str) -> Result<Self> {
        let parts: Vec<&str> = s.split('.').collect();
        if parts.len() != 2 {
            return Err(Error::InvalidCipherString);
        }

        let ty = parts[0].as_bytes();
        if ty.len() != 1 {
            return Err(Error::InvalidCipherString);
        }

        let ty = ty[0] - b'0';
        let contents = parts[1];

        let parts: Vec<&str> = contents.split('|').collect();
        if parts.len() < 2 || parts.len() > 3 {
            return Err(Error::InvalidCipherString);
        }

        let iv =
            base64::decode(parts[0]).context(crate::error::InvalidBase64)?;
        let ciphertext =
            base64::decode(parts[1]).context(crate::error::InvalidBase64)?;
        let mac = if parts.len() > 2 {
            Some(
                base64::decode(parts[2])
                    .context(crate::error::InvalidBase64)?,
            )
        } else {
            None
        };

        Ok(Self {
            ty,
            iv,
            ciphertext,
            mac,
        })
    }

    pub fn decrypt(&self, enc_key: &[u8], mac_key: &[u8]) -> Result<Vec<u8>> {
        if self.ty != 2 {
            unimplemented!()
        }

        if let Some(mac) = &self.mac {
            let mut digest = hmac::Hmac::<sha2::Sha256>::new_varkey(mac_key)
                .map_err(|_| Error::InvalidMacKey)?;
            digest.input(&self.iv);
            digest.input(&self.ciphertext);
            let calculated_mac = digest.result().code();

            if !macs_equal(mac, &calculated_mac, mac_key)? {
                return Err(Error::InvalidMac);
            }
        }

        let cipher = block_modes::Cbc::<
            aes::Aes256,
            block_modes::block_padding::Pkcs7,
        >::new_var(enc_key, &self.iv)
        .context(crate::error::CreateBlockMode)?;

        cipher
            .decrypt_vec(&self.ciphertext)
            .context(crate::error::Decrypt)
    }
}

fn macs_equal(mac1: &[u8], mac2: &[u8], mac_key: &[u8]) -> Result<bool> {
    let mut digest = hmac::Hmac::<sha2::Sha256>::new_varkey(mac_key)
        .map_err(|_| Error::InvalidMacKey)?;
    digest.input(mac1);
    let hmac1 = digest.result().code();

    let mut digest = hmac::Hmac::<sha2::Sha256>::new_varkey(mac_key)
        .map_err(|_| Error::InvalidMacKey)?;
    digest.input(mac2);
    let hmac2 = digest.result().code();

    Ok(hmac1 == hmac2)
}