Skip to main content

idb/innodb/
encryption.rs

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