odf-crypto 0.1.1

LibreOffice-faithful encryption for ODF packages (.odt, .ods, .odp): detect it, decrypt it, write it
Documentation
//! Password bytes as LibreOffice's MS-1252 encoder produces them, for the
//! last rung of the legacy SHA-1 read ladder (see [`crate::kdf::LegacyStartKey`]).
//!
//! **Not WHATWG `windows-1252`, and must not be replaced with `encoding_rs`.**
//! The caller is `OStorageHelper::CreatePackageEncryptionData`
//! (`comphelper/source/misc/storagehelper.cxx`), which converts with
//! `OUStringToOString(.., RTL_TEXTENCODING_MS_1252)` and so with
//! `OUSTRING_TO_OSTRING_CVTFLAGS` (`include/rtl/string.h`). That conversion,
//! `convertUnicodeToChar` in `sal/textenc/convertsimple.cxx`, never fails:
//!
//! 1. ASCII, U+00A0..=U+00FF, and the 27 characters of `aImplMS1252ToCharTabEx`
//!    (`sal/textenc/textenc.cxx`) map directly. U+0080..=U+009F do **not** --
//!    LibreOffice's table leaves 0x81/0x8D/0x8F/0x90/0x9D unmapped, and the C1
//!    controls are not in the reverse table at all.
//! 2. Otherwise `UNDEFINED_REPLACE` retries once through `aImplRepCharTab`, a
//!    "similar character" table (U+2002 EN SPACE becomes a space). Resolved here
//!    to the rows that land on a byte, by `tools/gen_ms1252.py`.
//! 3. Otherwise `PRIVATE_MAPTO0` maps U+F100..=U+F1FF to its low byte
//!    (`handleUndefinedUnicodeToTextChar`, `RTL_TEXTCVT_BYTE_PRIVATE_START`).
//! 4. Otherwise `?`. A non-BMP character is a UTF-16 surrogate pair upstream,
//!    and the pair together becomes **one** `?`.
//!
//! WHATWG's encoder emits `&#NNNN;` for step 4 and maps the C1 controls, so it
//! would produce a different start key for any password that reaches either.

use crate::sensitive::PasswordBytes;

/// `aImplMS1252ToCharTabEx`: the 27 characters above U+00FF that MS-1252 holds.
const TO_CHAR_EX: [(u16, u8); 27] = [
    (0x0152, 0x8C),
    (0x0153, 0x9C),
    (0x0160, 0x8A),
    (0x0161, 0x9A),
    (0x0178, 0x9F),
    (0x017D, 0x8E),
    (0x017E, 0x9E),
    (0x0192, 0x83),
    (0x02C6, 0x88),
    (0x02DC, 0x98),
    (0x2013, 0x96),
    (0x2014, 0x97),
    (0x2018, 0x91),
    (0x2019, 0x92),
    (0x201A, 0x82),
    (0x201C, 0x93),
    (0x201D, 0x94),
    (0x201E, 0x84),
    (0x2020, 0x86),
    (0x2021, 0x87),
    (0x2022, 0x95),
    (0x2026, 0x85),
    (0x2030, 0x89),
    (0x2039, 0x8B),
    (0x203A, 0x9B),
    (0x20AC, 0x80),
    (0x2122, 0x99),
];

/// `aImplRepCharTab` resolved through the direct tables: each character whose
/// "similar character" MS-1252 can hold, and the byte it ends up as.
/// Generated by `python tools/gen_ms1252.py <LibreOffice core>`; regenerate
/// rather than edit.
#[rustfmt::skip]
const REPLACE: &[(u16, u8)] = &[
    (0x2000, 0x20),
    (0x2001, 0x20),
    (0x2002, 0x20),
    (0x2003, 0x20),
    (0x2004, 0x20),
    (0x2005, 0x20),
    (0x2006, 0x20),
    (0x2007, 0x20),
    (0x2008, 0x20),
    (0x2009, 0x20),
    (0x200A, 0x20),
    (0x2010, 0x2D),
    (0x2011, 0x2D),
    (0x2012, 0x2D),
    (0x2015, 0x2D),
    (0x201B, 0x27),
    (0x201F, 0x22),
    (0x2023, 0x2D),
    (0x2024, 0x2D),
    (0x2027, 0x2D),
    (0x2028, 0x0A),
    (0x2029, 0x0D),
    (0x2032, 0x27),
    (0x2033, 0x22),
    (0x2035, 0x27),
    (0x2036, 0x22),
    (0x2043, 0x2D),
    (0x2044, 0x2F),
    (0x2160, 0x49),
    (0x2164, 0x56),
    (0x2169, 0x58),
    (0x216C, 0x4C),
    (0x216D, 0x43),
    (0x216E, 0x44),
    (0x216F, 0x4D),
    (0x2170, 0x69),
    (0x2174, 0x76),
    (0x2179, 0x78),
    (0x217C, 0x6C),
    (0x217D, 0x63),
    (0x217E, 0x64),
    (0x217F, 0x6D),
    (0x2215, 0x2F),
    (0x2217, 0x2A),
    (0xFF00, 0x20),
    (0xFF01, 0x21),
    (0xFF02, 0x22),
    (0xFF03, 0x23),
    (0xFF04, 0x24),
    (0xFF05, 0x25),
    (0xFF06, 0x26),
    (0xFF07, 0x27),
    (0xFF08, 0x28),
    (0xFF09, 0x29),
    (0xFF0A, 0x2A),
    (0xFF0B, 0x2B),
    (0xFF0C, 0x2C),
    (0xFF0D, 0x2D),
    (0xFF0E, 0x2E),
    (0xFF0F, 0x2F),
    (0xFF10, 0x30),
    (0xFF11, 0x31),
    (0xFF12, 0x32),
    (0xFF13, 0x33),
    (0xFF14, 0x34),
    (0xFF15, 0x35),
    (0xFF16, 0x36),
    (0xFF17, 0x37),
    (0xFF18, 0x38),
    (0xFF19, 0x39),
    (0xFF1A, 0x3A),
    (0xFF1B, 0x3B),
    (0xFF1C, 0x3C),
    (0xFF1D, 0x3D),
    (0xFF1E, 0x3E),
    (0xFF1F, 0x3F),
    (0xFF20, 0x40),
    (0xFF21, 0x41),
    (0xFF22, 0x42),
    (0xFF23, 0x43),
    (0xFF24, 0x44),
    (0xFF25, 0x45),
    (0xFF26, 0x46),
    (0xFF27, 0x47),
    (0xFF28, 0x48),
    (0xFF29, 0x49),
    (0xFF2A, 0x4A),
    (0xFF2B, 0x4B),
    (0xFF2C, 0x4C),
    (0xFF2D, 0x4D),
    (0xFF2E, 0x4E),
    (0xFF2F, 0x4F),
    (0xFF30, 0x50),
    (0xFF31, 0x51),
    (0xFF32, 0x52),
    (0xFF33, 0x53),
    (0xFF34, 0x54),
    (0xFF35, 0x55),
    (0xFF36, 0x56),
    (0xFF37, 0x57),
    (0xFF38, 0x58),
    (0xFF39, 0x59),
    (0xFF3A, 0x5A),
    (0xFF3B, 0x5B),
    (0xFF3C, 0x5C),
    (0xFF3D, 0x5D),
    (0xFF3E, 0x5E),
    (0xFF3F, 0x5F),
    (0xFF40, 0x60),
    (0xFF41, 0x61),
    (0xFF42, 0x62),
    (0xFF43, 0x63),
    (0xFF44, 0x64),
    (0xFF45, 0x65),
    (0xFF46, 0x66),
    (0xFF47, 0x67),
    (0xFF48, 0x68),
    (0xFF49, 0x69),
    (0xFF4A, 0x6A),
    (0xFF4B, 0x6B),
    (0xFF4C, 0x6C),
    (0xFF4D, 0x6D),
    (0xFF4E, 0x6E),
    (0xFF4F, 0x6F),
    (0xFF50, 0x70),
    (0xFF51, 0x71),
    (0xFF52, 0x72),
    (0xFF53, 0x73),
    (0xFF54, 0x74),
    (0xFF55, 0x75),
    (0xFF56, 0x76),
    (0xFF57, 0x77),
    (0xFF58, 0x78),
    (0xFF59, 0x79),
    (0xFF5A, 0x7A),
    (0xFF5B, 0x7B),
    (0xFF5C, 0x7C),
    (0xFF5D, 0x7D),
    (0xFF5E, 0x7E),
    (0xFF5F, 0x7F),
    (0xFFE0, 0xA2),
    (0xFFE1, 0xA3),
    (0xFFE2, 0xAC),
    (0xFFE3, 0xAF),
    (0xFFE4, 0xA6),
    (0xFFE5, 0xA5),
    (0xFFFD, 0x3F),
];

fn direct(c: u16) -> Option<u8> {
    match c {
        0..=0x7F | 0xA0..=0xFF => Some(c as u8),
        _ => TO_CHAR_EX
            .binary_search_by_key(&c, |&(u, _)| u)
            .ok()
            .and_then(|i| TO_CHAR_EX.get(i))
            .map(|&(_, b)| b),
    }
}

fn byte_for(c: char) -> u8 {
    // Outside the BMP: a surrogate pair upstream, which converts to one `?`.
    let Ok(c) = u16::try_from(u32::from(c)) else {
        return b'?';
    };
    if let Some(b) = direct(c) {
        return b;
    }
    if let Some(&(_, b)) = REPLACE
        .binary_search_by_key(&c, |&(u, _)| u)
        .ok()
        .and_then(|i| REPLACE.get(i))
    {
        return b;
    }
    match c {
        0xF100..=0xF1FF => (c - 0xF100) as u8,
        _ => b'?',
    }
}

/// `password` as MS-1252 bytes, exactly one byte per `char`.
///
/// Wrapped from the first byte: this is the password itself in another
/// encoding, so it gets the same zeroize-on-drop as the digest made from it.
pub(crate) fn encode(password: &str) -> PasswordBytes {
    let n = password.chars().count();
    PasswordBytes::new_with(n, |slot| {
        for (dst, c) in slot.iter_mut().zip(password.chars()) {
            *dst = byte_for(c);
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use secure_gate::RevealSecret;

    fn enc(s: &str) -> Vec<u8> {
        encode(s).with_secret(|b| b.to_vec())
    }

    #[test]
    fn tables_are_sorted_so_binary_search_finds_every_row() {
        assert!(TO_CHAR_EX.windows(2).all(|w| w[0].0 < w[1].0));
        assert!(REPLACE.windows(2).all(|w| w[0].0 < w[1].0));
        assert!(!REPLACE.is_empty());
    }

    #[test]
    fn each_conversion_step_is_reached() {
        // 1. direct: ASCII, Latin-1 upper half, and the extension table.
        assert_eq!(enc("aä€"), [b'a', 0xE4, 0x80]);
        // C1 controls are not in LibreOffice's reverse table.
        assert_eq!(enc("\u{0081}\u{0090}"), b"??");
        // 2. similar-character fallback: EN QUAD becomes a space.
        assert_eq!(enc("\u{2000}"), b" ");
        // 3. the private-use byte range.
        assert_eq!(enc("\u{F1E4}"), [0xE4]);
        // 4. undefined, and one `?` per non-BMP character, not two.
        assert_eq!(enc("\u{0100}\u{1F600}"), b"??");
    }
}