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
//! Interfaces for decrypting data
//!
//! # Examples
//! ## Simple
//! ```rust
//! use ubiq::credentials::Credentials;
//! use ubiq::encryption::encrypt;
//! use ubiq::decryption::decrypt;
//!
//! let creds = Credentials::new(None, None).unwrap();
//!
//! let ct = encrypt(&creds, b"abc").unwrap();
//! let pt = decrypt(&creds, &ct).unwrap();
//!
//! assert!(pt != ct);
//! assert!(pt == b"abc");
//! ```
//! ## Piecewise
//! ```rust
//! use ubiq::credentials::Credentials;
//! use ubiq::encryption::encrypt;
//! use ubiq::decryption::Decryption;
//!
//! let creds = Credentials::new(None, None).unwrap();
//! let ct = encrypt(&creds, b"abc").unwrap();
//!
//! let mut dec = Decryption::new(&creds).unwrap();
//!
//! /*
//! * ct can be passed to the decryption process in
//! * as many or as few pieces as desired
//! */
//!
//! let mut pt = dec.begin().unwrap();
//! pt.extend(dec.update(&ct[0..3]).unwrap());
//! pt.extend(dec.update(&ct[3..9]).unwrap());
//! pt.extend(dec.update(&ct[9..24]).unwrap());
//! pt.extend(dec.update(&ct[24..]).unwrap());
//! pt.extend(dec.end().unwrap());
//!
//! assert!(pt != ct);
//! assert!(pt == b"abc");
//!
//! /*
//! * the dec object can now be reused by following the
//! * begin(), update()..., end() process shown above for
//! * as many times as desired.
//! */
//! ```
use crate::algorithm;
use crate::base64;
use crate::cipher;
use crate::client::Client;
use crate::credentials::Credentials;
use crate::error::Error;
use crate::header::Header;
use crate::result::Result;
use crate::session;
const DECRYPTION_KEY_PATH: &str = "api/v0/decryption/key";
#[derive(serde::Deserialize)]
struct NewDecryptionResponse {
encrypted_private_key: String,
// this should just be a String, but the server is currently
// returning `null` in this variable.
// see also: DecryptionSession::close()
encryption_session: Option<String>,
key_fingerprint: String,
wrapped_data_key: String,
}
/// Structure encompassing parameters used for decrypting data
///
/// Using this structure, a caller is able to decrypt a ciphertext
/// by feeding it to the member functions in a piecewise fashion
/// (or by doing so all at once). In addition, if multiple, unique
/// ciphertexts were encrypted with the same key, this object can
/// be used to decrypt them without having to communicate with the
/// server multiple times.
pub struct Decryption<'a> {
client: std::rc::Rc<Client>,
host: std::rc::Rc<String>,
srsa: String,
session: Option<session::Session<'a>>,
buf: Vec<u8>,
}
impl Decryption<'_> {
/// Create a new decryption object
pub fn new<'a>(creds: &Credentials) -> Result<Decryption<'a>> {
Ok(Decryption {
client: std::rc::Rc::new(Client::new(creds)),
host: std::rc::Rc::new(creds.host().clone()),
srsa: creds.srsa().clone(),
session: None,
buf: Vec::new(),
})
}
fn s_close(
client: &Client,
host: &String,
id: &Option<String>,
fp: &String,
uses: &session::SessionKeyUses,
) -> Result<()> {
if uses.cur > 0 && id.as_ref().is_some() {
client.patch(
&format!(
"{}/{}/{}/{}",
host,
DECRYPTION_KEY_PATH,
fp,
id.as_ref().unwrap(),
),
"application/json".to_string(),
format!(
"{{\
\"uses\": {}\
}}",
uses.cur,
),
)?;
}
Ok(())
}
fn new_session<'a>(
&self,
algoid: usize,
edk: &[u8],
) -> Result<session::Session<'a>> {
let s_edk = base64::encode(edk);
let rsp = self.client.post(
&format!("{}/{}", self.host, DECRYPTION_KEY_PATH),
"application/json".to_string(),
format!(
"{{\
\"encrypted_data_key\": \"{}\"\
}}",
s_edk,
),
)?;
match rsp.json::<NewDecryptionResponse>() {
Err(e) => Err(Error::new(&e.to_string())),
Ok(msg) => Ok(session::Session::new(
std::rc::Rc::clone(&self.client),
std::rc::Rc::clone(&self.host),
msg.encryption_session,
&msg.key_fingerprint,
algorithm::get_by_id(algoid)?,
&msg.encrypted_private_key,
&msg.wrapped_data_key,
&s_edk,
0,
&self.srsa,
Self::s_close,
)?),
}
}
/// Begin a new decryption "session"
///
/// Decryption of a ciphertext consists of a `begin()`, some number
/// of `update()` calls, and an `end()`. It is an error to call
/// `begin()` more than once without an intervening `end()`.
pub fn begin(&mut self) -> Result<Vec<u8>> {
if self.session.is_some()
&& self.session.as_ref().unwrap().ctx.is_some()
{
return Err(Error::new("decryption already in progress"));
}
// because no ciphertext has been input yet, no plaintext
// is returned, but this is done so that the Encryption and
// Decryption interfaces are the same/similar
Ok(Vec::<u8>::new())
}
/// Input (some) ciphertext for decryption
///
/// The update function writes data into the Decryption object.
/// Plaintext data may or may not be returned with each call to
/// this function.
pub fn update(&mut self, ct: &[u8]) -> Result<Vec<u8>> {
self.buf.extend(ct);
if self.session.is_none()
|| self.session.as_ref().unwrap().ctx.is_none()
{
let hlen = Header::can_deserialize(&self.buf)?;
if hlen > 0 {
let mut buf = std::mem::take(&mut self.buf);
let hdr = Header::deserialize(&buf)?;
if hdr.version != 0 {
return Err(Error::new("unsupported header version"));
}
if self.session.is_some()
&& self.session.as_ref().unwrap().key.enc != hdr.key
{
self.session = None;
}
if self.session.is_none() {
self.session =
Some(self.new_session(hdr.algorithm, &hdr.key)?);
}
let mut s = self.session.as_mut().unwrap();
s.ctx = Some(cipher::CipherCtx::new(
cipher::CipherOp::Decrypt,
s.algo.name,
&s.key.raw,
hdr.iv,
if (hdr.flags & crate::header::V0_FLAG_AAD) != 0 {
Some(&buf[0..hlen])
} else {
None
},
)?);
s.key.uses.cur += 1;
buf.drain(0..hlen);
self.buf = buf;
}
}
let mut pt = Vec::<u8>::new();
if self.session.is_some()
&& self.session.as_ref().unwrap().ctx.is_some()
{
let s = self.session.as_mut().unwrap();
if self.buf.len() > s.algo.len.tag {
let sz = self.buf.len() - s.algo.len.tag;
pt = s.ctx.as_mut().unwrap().update(&self.buf[0..sz])?;
self.buf.drain(0..sz);
}
}
Ok(pt)
}
/// End a decryption "session"
///
/// After all ciphertext has been written to the object via the
/// `update()` function, the caller must call this function to finalize
/// the decryption. Any remaining plaintext will be returned. If the
/// algorithm in use is authenticated, an error may be returned instead
/// if the authentication process fails.
///
/// Note that if the algorithm in use is authenticated, any error in
/// the authentication process will not be reported until this function
/// is called. Therefore, when using an authenticated algorithm, output
/// should not be trusted until this function returns successfully.
pub fn end(&mut self) -> Result<Vec<u8>> {
let mut pt = Vec::<u8>::new();
if self.session.is_some()
&& self.session.as_ref().unwrap().ctx.is_some()
{
let s = self.session.as_mut().unwrap();
pt = s.ctx.as_mut().unwrap().finalize(if self.buf.len() > 0 {
Some(&self.buf)
} else {
None
})?;
self.buf.truncate(0);
s.ctx = None;
}
Ok(pt)
}
/// Decrypt a single ciphertext in one shot
///
/// This function is equivalent to calling `begin()`, `update(ct)`,
/// and `end()`
pub fn cipher(&mut self, ct: &[u8]) -> Result<Vec<u8>> {
let mut pt = self.begin()?;
pt.extend(self.update(ct)?);
pt.extend(self.end()?);
Ok(pt)
}
}
/// Decrypt a single ciphertext
///
/// This function is equivalent to creating a new Decryption object
/// calling `begin()`, `update(pt)`, and `end()`.
pub fn decrypt(c: &Credentials, ct: &[u8]) -> Result<Vec<u8>> {
Decryption::new(&c)?.cipher(&ct)
}
#[cfg(test)]
mod tests {
#[test]
fn reuse_session() -> crate::result::Result<()> {
let pt = b"abc";
let creds = crate::credentials::Credentials::new(None, None)?;
let ct = crate::encryption::encrypt(&creds, &pt[..])?;
let mut dec = crate::decryption::Decryption::new(&creds)?;
let rec = dec.cipher(&ct)?;
assert!(pt[..] == rec, "{}", "recovered plaintext does not match");
let fp1 = dec.session.as_ref().unwrap().key.fingerprint.clone();
let s1 =
dec.session.as_ref().unwrap() as *const crate::session::Session<'_>;
let rec = dec.cipher(&ct)?;
assert!(pt[..] == rec, "{}", "recovered plaintext does not match");
let fp2 = dec.session.as_ref().unwrap().key.fingerprint.clone();
let s2 =
dec.session.as_ref().unwrap() as *const crate::session::Session<'_>;
/*
* we really want to compare the session.id, but
* the server is currently returning `null` in that
* field which in unhelpful.
*/
assert!(fp1 == fp2 && s1 == s2);
Ok(())
}
#[test]
fn change_session() -> crate::result::Result<()> {
let pt = b"abc";
let creds = crate::credentials::Credentials::new(None, None)?;
let mut dec = crate::decryption::Decryption::new(&creds)?;
let ct = crate::encryption::encrypt(&creds, &pt[..])?;
let rec = dec.cipher(&ct)?;
assert!(pt[..] == rec, "{}", "recovered plaintext does not match");
let fp1 = dec.session.as_ref().unwrap().key.fingerprint.clone();
let ct = crate::encryption::encrypt(&creds, &pt[..])?;
let rec = dec.cipher(&ct)?;
assert!(pt[..] == rec, "{}", "recovered plaintext does not match");
let fp2 = dec.session.as_ref().unwrap().key.fingerprint.clone();
/* different key fingerprints means different sessions */
assert!(fp1 != fp2);
Ok(())
}
}