|
| 1 | +# Symmetric Data Encapsulation Scheme (DES) |
| 2 | + |
| 3 | +Polykey’s state is completely stored in the database. This means everything |
| 4 | +critical to the system's function—keys, metadata, and configurations—is written |
| 5 | +to disk. To ensure the security and integrity of this data, we encrypt the |
| 6 | +database using a **32-byte Data Encryption Key (DB Key)** and the |
| 7 | +**XChaCha20-Poly1305** symmetric encryption algorithm, provided by |
| 8 | +**libsodium**. |
| 9 | + |
| 10 | +## Why We Use Symmetric Encryption |
| 11 | + |
| 12 | +Encryption is non-negotiable for a system like Polykey, where data security is a |
| 13 | +core priority. Symmetric encryption is used for: |
| 14 | + |
| 15 | +- **Confidentiality** - Ensuring database contents are only accessible to the |
| 16 | + rightful owner. |
| 17 | +- **Integrity** - Preventing unauthorized tampering by detecting any |
| 18 | + modifications. |
| 19 | +- **Performance** - Symmetric encryption is significantly faster than asymmetric |
| 20 | + encryption for large datasets. |
| 21 | + |
| 22 | +## Database Encryption Key (DB Key) |
| 23 | + |
| 24 | +### **How the DB Key Works** |
| 25 | + |
| 26 | +The DB Key is a **randomly generated 32-byte key** that encrypts all database |
| 27 | +content. This key is stored on disk, but never in plaintext—it is always |
| 28 | +encrypted before being written to storage. |
| 29 | + |
| 30 | +The DB Key is used to encrypt and decrypt the entire database, meaning that |
| 31 | +without it, **all data in the system is inaccessible**. |
| 32 | + |
| 33 | +### **Why the DB Key is Not Derived from the Root Key** |
| 34 | + |
| 35 | +A common approach in cryptographic systems is to derive all keys from a single |
| 36 | +root key. However, this is not how Polykey manages database encryption. The DB |
| 37 | +Key is independent from the root key for the following reasons: |
| 38 | + |
| 39 | +1. **Key Rotation & Flexibility** |
| 40 | + If the root key were used to derive the DB Key, changing the root key would |
| 41 | + require re-encrypting the entire database. This would be a **catastrophic** |
| 42 | + operation in terms of efficiency and usability. Instead, by keeping the DB |
| 43 | + Key separate, the database can remain encrypted even when the root key is |
| 44 | + changed. |
| 45 | + |
| 46 | +2. **Minimizing Attack Surfaces** |
| 47 | + If an attacker compromises the root key, they still cannot access the |
| 48 | + database contents without the DB Key. This adds an extra layer of security. |
| 49 | + |
| 50 | +3. **Persistence Across Keypair Changes** |
| 51 | + In Polykey, root keypairs can be swapped out periodically for security |
| 52 | + reasons (like how passwords should be rotated). By decoupling the DB Key, the |
| 53 | + database encryption remains stable across key rotations. |
| 54 | + |
| 55 | +## Encryption Algorithm: XChaCha20-Poly1305 |
| 56 | + |
| 57 | +Polykey uses **XChaCha20-Poly1305**, a widely respected symmetric encryption |
| 58 | +scheme known for its: |
| 59 | + |
| 60 | +- **256-bit key size** - Strong encryption without performance tradeoffs. |
| 61 | +- **192-bit nonce** - Ensures nonce reuse attacks are virtually impossible. |
| 62 | +- **Authenticated encryption** - Includes integrity checks, so any unauthorized |
| 63 | + modifications are detected. |
| 64 | + |
| 65 | +### **Why XChaCha20-Poly1305?** |
| 66 | + |
| 67 | +1. **Nonce Misuse Resistance** |
| 68 | + Unlike AES-GCM, which has strict nonce reuse requirements, XChaCha20 allows |
| 69 | + for random nonce generation with no risk of catastrophic failures. |
| 70 | + |
| 71 | +2. **High Performance** |
| 72 | + Stream ciphers like XChaCha20 are significantly faster than block ciphers |
| 73 | + like AES in many scenarios, making them well-suited for encrypting large |
| 74 | + datasets. |
| 75 | + |
| 76 | +3. **Compatibility with Libsodium** |
| 77 | + Libsodium is a battle-tested cryptographic library that provides a simple and |
| 78 | + secure implementation of XChaCha20-Poly1305, reducing the risk of |
| 79 | + implementation errors. |
| 80 | + |
| 81 | +## Key Rotation & Secure Storage |
| 82 | + |
| 83 | +### **Key Rotation Strategy** |
| 84 | + |
| 85 | +Regular key rotation is a fundamental security practice. However, since |
| 86 | +re-encrypting an entire database is computationally expensive, Polykey manages |
| 87 | +key rotations in a way that minimizes disruption: |
| 88 | + |
| 89 | +1. **The DB Key is swapped periodically** - This reduces long-term key exposure. |
| 90 | +2. **New keys are securely generated and encrypted using KEM (Key Encapsulation |
| 91 | + Mechanism)** - This ensures a smooth transition without exposing old |
| 92 | + encrypted data. |
| 93 | +3. **Polykey handles the transition automatically** - Users do not need to worry |
| 94 | + about database re-encryption when keypairs are rotated. |
| 95 | + |
| 96 | +### **How the DB Key is Stored** |
| 97 | + |
| 98 | +The DB Key is **never stored in plaintext**. Instead, it is encrypted and saved |
| 99 | +as a **JWE (JSON Web Encryption) file**. This file is securely managed to ensure |
| 100 | +that only authorized nodes can decrypt and use it. |
| 101 | + |
| 102 | +## Security Considerations |
| 103 | + |
| 104 | +- **Loss of the DB Key = Complete Data Loss** |
| 105 | + If the encrypted DB Key is lost, **all data within Polykey is permanently |
| 106 | + inaccessible**. Users must ensure backups are properly managed. |
| 107 | + |
| 108 | +- **Frequent Key Rotation is Good Practice** |
| 109 | + Regularly rotating the DB Key prevents long-term exposure and mitigates risks |
| 110 | + from potential future cryptographic weaknesses. |
| 111 | + |
| 112 | +- **Secure Backup Management is Critical** |
| 113 | + Since Polykey does not store plaintext recovery keys, **users must securely |
| 114 | + back up their 24-word recovery code** to ensure they can regain access if |
| 115 | + needed. |
| 116 | + |
| 117 | +## Conclusion |
| 118 | + |
| 119 | +The **Symmetric Data Encapsulation Scheme (DES)** is a critical component of |
| 120 | +Polykey's security model. By leveraging XChaCha20-Poly1305 for high-speed |
| 121 | +encryption and ensuring the **DB Key remains independent from the root key**, |
| 122 | +Polykey maintains a **balance between security, performance, and usability**. |
0 commit comments