Skip to main content

idb/innodb/
encryption.rs

1//! Tablespace encryption detection.
2//!
3//! Detects whether a tablespace is encrypted by inspecting bit 13 of the FSP
4//! flags (MySQL 5.7.11+). Currently only AES encryption is recognized.
5
6/// Encryption algorithm detected from FSP flags.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum EncryptionAlgorithm {
9    None,
10    Aes,
11}
12
13/// Detect encryption from FSP space flags.
14///
15/// Encryption flag is at bit 13 of FSP flags (MySQL 5.7.11+).
16pub fn detect_encryption(fsp_flags: u32) -> EncryptionAlgorithm {
17    if (fsp_flags >> 13) & 0x01 != 0 {
18        EncryptionAlgorithm::Aes
19    } else {
20        EncryptionAlgorithm::None
21    }
22}
23
24/// Check if a tablespace is encrypted based on its FSP flags.
25pub fn is_encrypted(fsp_flags: u32) -> bool {
26    detect_encryption(fsp_flags) != EncryptionAlgorithm::None
27}
28
29impl std::fmt::Display for EncryptionAlgorithm {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            EncryptionAlgorithm::None => write!(f, "None"),
33            EncryptionAlgorithm::Aes => write!(f, "AES"),
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_detect_encryption() {
44        assert_eq!(detect_encryption(0), EncryptionAlgorithm::None);
45        assert_eq!(detect_encryption(1 << 13), EncryptionAlgorithm::Aes);
46        // Other bits shouldn't affect encryption detection
47        assert_eq!(detect_encryption(0xFF), EncryptionAlgorithm::None);
48        assert_eq!(detect_encryption(0xFF | (1 << 13)), EncryptionAlgorithm::Aes);
49    }
50
51    #[test]
52    fn test_is_encrypted() {
53        assert!(!is_encrypted(0));
54        assert!(is_encrypted(1 << 13));
55    }
56}