X-Git-Url: http://git.euphorik.ch/?p=rup.git;a=blobdiff_plain;f=src%2Fcrypto.rs;h=7e707d02a218c64fc99034c8f1ac9205ccac0635;hp=cc8bf848ef326a615dadf4023f278b47cc86fb3d;hb=51aa7c917b3ecfb80d06d5a06cc9f553b8bc91c6;hpb=b1f2c5b803a7e85a3b0c8d999cf9a13d28c6c6c2 diff --git a/src/crypto.rs b/src/crypto.rs index cc8bf84..7e707d0 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,22 +1,53 @@ use openssl::{symm, sha::sha256}; use rand::prelude::*; +use crate::consts; + +#[derive(Debug)] +pub enum KeyError { + UnableToDecodeBase64Key, + WrongKeyLength, +} + +#[derive(Debug)] +pub enum EncryptError { + KeyError(KeyError), + UnableToEncrypt, +} + +#[derive(Debug)] +pub enum DecryptError { + KeyError(KeyError), + WrongMessageVersion, + UnableToDecodeBase64Message, + UnableToDecrypt, + UnableToDecodeMessageAsUTF8String, + HashMismatch, +} + +fn decode_key(key: &str) -> Result, KeyError> { + match base64::decode(key) { + Ok(k) => if k.len() != 16 { return Err(KeyError::WrongKeyLength) } else { Ok(k) }, + Err(_e) => Err(KeyError::UnableToDecodeBase64Key) + } +} + /// Encrypt the given text with the given key. The key length must be 128 bits encoded in base64. /// Ouput format: /// Format "1" + base_64( + + ) /// IV: 16 bytes randomized. /// Mode : CBC. -pub fn encrypt(key: &str, plain_text: &str) -> String { - let key_as_bytes = base64::decode(key).expect("Unable to decode base64 encoded key"); - assert!(key_as_bytes.len() == 16); +pub fn encrypt(key: &str, plain_text: &str) -> Result { + let key_as_bytes = decode_key(key).map_err(|e| EncryptError::KeyError(e))?; let text_as_bytes = plain_text.as_bytes(); - let iv = rand::thread_rng().gen::<[u8; 16]>(); let cipher_text = - symm::encrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(&iv), text_as_bytes) - .expect("Unable to encrypt message"); + match symm::encrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(&iv), text_as_bytes) { + Ok(t) => t, + Err(_e) => return Err(EncryptError::UnableToEncrypt) + }; let hash_text = sha256(&text_as_bytes); @@ -25,14 +56,33 @@ pub fn encrypt(key: &str, plain_text: &str) -> String { result.extend(&hash_text); result.extend(&cipher_text); - String::from("1") + &base64::encode(&result) + Ok(String::from("1") + &base64::encode(&result)) } -pub fn decrypt(key: &str, cipher_text: &str) -> Option { - if cipher_text.chars() != '1' { - return None; - } +/// TODO: return a Result +pub fn decrypt(key: &str, cipher_text: &str) -> Result { + let key_as_bytes = decode_key(key).map_err(|e| DecryptError::KeyError(e))?; + + // Can't decrypt a message with the wrong version. + if !cipher_text.starts_with(consts::CURRENT_MESSAGE_VERSION) { return Err(DecryptError::WrongMessageVersion) } + + let cipher_text_bytes = + base64::decode(&cipher_text.as_bytes()[consts::CURRENT_MESSAGE_VERSION.as_bytes().len()..]) + .map_err(|_e| DecryptError::UnableToDecodeBase64Message)?; + + let iv = &cipher_text_bytes[0..16]; + let hash = &cipher_text_bytes[16..48]; + let encrypted_message = &cipher_text_bytes[48..]; + + let plain_message_bytes = + symm::decrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(iv), encrypted_message) + .map_err(|_e| DecryptError::UnableToDecrypt)?; + + if sha256(&plain_message_bytes) != hash { return Err(DecryptError::HashMismatch) } + + let plain_message = + String::from_utf8(plain_message_bytes) + .map_err(|_e| DecryptError::UnableToDecodeMessageAsUTF8String)?; - println!("cypher: {}", cipher_text); - Some(String::new()) + Ok(plain_message) }