kstone-core 0.1.0

Core storage engine for KeystoneDB - LSM tree, WAL, and DynamoDB-compatible API
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/// Block-based I/O with optional encryption
///
/// All disk I/O is performed in 4KB blocks with CRC32C checksums.
/// Optional AES-256-GCM encryption can be enabled per-block.

use bytes::{Bytes, BytesMut, BufMut};
use std::fs::File;
use std::io::{Read, Write, Seek, SeekFrom};
use crate::{Error, Result, layout::BLOCK_SIZE, types::checksum};

/// Block ID - logical block number
pub type BlockId = u64;

/// Block header (16 bytes)
///
/// Format:
/// ```text
/// [flags(1)] [reserved(3)] [data_len(4)] [reserved(8)]
/// ```
const BLOCK_HEADER_SIZE: usize = 16;

/// Block flags
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BlockFlags(u8);

impl BlockFlags {
    const ENCRYPTED: u8 = 0x01;
    const COMPRESSED: u8 = 0x02; // Reserved for future use

    pub fn new() -> Self {
        Self(0)
    }

    pub fn with_encryption(mut self) -> Self {
        self.0 |= Self::ENCRYPTED;
        self
    }

    pub fn is_encrypted(&self) -> bool {
        self.0 & Self::ENCRYPTED != 0
    }

    pub fn is_compressed(&self) -> bool {
        self.0 & Self::COMPRESSED != 0
    }

    fn as_byte(&self) -> u8 {
        self.0
    }

    fn from_byte(b: u8) -> Self {
        Self(b)
    }
}

/// In-memory block
#[derive(Debug, Clone)]
pub struct Block {
    pub id: BlockId,
    pub data: Bytes,
    pub flags: BlockFlags,
}

impl Block {
    /// Create a new block
    pub fn new(id: BlockId, data: Bytes) -> Self {
        Self {
            id,
            data,
            flags: BlockFlags::new(),
        }
    }

    /// Create with encryption flag
    pub fn with_encryption(id: BlockId, data: Bytes) -> Self {
        Self {
            id,
            data,
            flags: BlockFlags::new().with_encryption(),
        }
    }

    /// Maximum data size in a block (accounting for header + footer)
    pub fn max_data_size() -> usize {
        BLOCK_SIZE - BLOCK_HEADER_SIZE - 4 // -4 for CRC at end
    }
}

/// Block writer
pub struct BlockWriter {
    file: File,
    encryption_key: Option<[u8; 32]>,
}

impl BlockWriter {
    /// Create a new block writer
    pub fn new(file: File) -> Self {
        Self {
            file,
            encryption_key: None,
        }
    }

    /// Create with encryption key (AES-256)
    pub fn with_encryption(file: File, key: [u8; 32]) -> Self {
        Self {
            file,
            encryption_key: Some(key),
        }
    }

    /// Write a block at the given offset
    pub fn write(&mut self, block: &Block, offset: u64) -> Result<()> {
        self.file.seek(SeekFrom::Start(offset))?;

        let mut buf = BytesMut::with_capacity(BLOCK_SIZE);

        // Determine if we should encrypt
        let should_encrypt = self.encryption_key.is_some() && block.flags.is_encrypted();

        let (final_data, final_flags) = if should_encrypt {
            let key = self.encryption_key.unwrap();
            let encrypted = encrypt_data(&block.data, &key, block.id)?;
            (encrypted, block.flags)
        } else {
            (block.data.clone(), block.flags)
        };

        // Write header (16 bytes total)
        buf.put_u8(final_flags.as_byte());
        buf.put_bytes(0, 3); // reserved
        buf.put_u32_le(final_data.len() as u32);
        buf.put_bytes(0, 8); // reserved (complete 16-byte header)

        // Write data
        buf.put_slice(&final_data);

        // Pad to block size (minus CRC)
        let padding = BLOCK_SIZE - buf.len() - 4;
        buf.put_bytes(0, padding);

        // Compute and write CRC (excluding CRC field itself)
        let crc = checksum::compute(&buf);
        buf.put_u32_le(crc);

        self.file.write_all(&buf)?;
        Ok(())
    }

    /// Flush writes
    pub fn flush(&mut self) -> Result<()> {
        self.file.sync_all()?;
        Ok(())
    }
}

/// Block reader
pub struct BlockReader {
    file: File,
    encryption_key: Option<[u8; 32]>,
}

impl BlockReader {
    /// Create a new block reader
    pub fn new(file: File) -> Self {
        Self {
            file,
            encryption_key: None,
        }
    }

    /// Create with encryption key
    pub fn with_encryption(file: File, key: [u8; 32]) -> Self {
        Self {
            file,
            encryption_key: Some(key),
        }
    }

    /// Read a block from the given offset
    pub fn read(&mut self, block_id: BlockId, offset: u64) -> Result<Block> {
        self.file.seek(SeekFrom::Start(offset))?;

        let mut buf = vec![0u8; BLOCK_SIZE];
        self.file.read_exact(&mut buf)?;

        // Verify CRC
        let expected_crc = u32::from_le_bytes([
            buf[BLOCK_SIZE - 4],
            buf[BLOCK_SIZE - 3],
            buf[BLOCK_SIZE - 2],
            buf[BLOCK_SIZE - 1],
        ]);

        if !checksum::verify(&buf[..BLOCK_SIZE - 4], expected_crc) {
            return Err(Error::ChecksumMismatch);
        }

        // Parse header
        let flags = BlockFlags::from_byte(buf[0]);
        let data_len = u32::from_le_bytes([buf[4], buf[5], buf[6], buf[7]]) as usize;

        if data_len > Block::max_data_size() {
            return Err(Error::Corruption(format!("Invalid data length: {}", data_len)));
        }

        // Extract data
        let data_start = BLOCK_HEADER_SIZE;
        let data_end = data_start + data_len;
        let data = Bytes::copy_from_slice(&buf[data_start..data_end]);

        // Decrypt if necessary
        let final_data = if flags.is_encrypted() {
            if let Some(key) = self.encryption_key {
                decrypt_data(&data, &key, block_id)?
            } else {
                return Err(Error::EncryptionError("Block is encrypted but no key provided".to_string()));
            }
        } else {
            data
        };

        Ok(Block {
            id: block_id,
            data: final_data,
            flags,
        })
    }
}

/// Encrypt data using AES-256-GCM
fn encrypt_data(data: &[u8], key: &[u8; 32], block_id: BlockId) -> Result<Bytes> {
    use aes_gcm::{
        aead::{Aead, KeyInit},
        Aes256Gcm, Nonce,
    };

    let cipher = Aes256Gcm::new(key.into());

    // Use block_id as part of nonce (12 bytes)
    let mut nonce_bytes = [0u8; 12];
    nonce_bytes[0..8].copy_from_slice(&block_id.to_le_bytes());
    let nonce = Nonce::from_slice(&nonce_bytes);

    let ciphertext = cipher
        .encrypt(nonce, data)
        .map_err(|e| Error::EncryptionError(format!("Encryption failed: {}", e)))?;

    Ok(Bytes::from(ciphertext))
}

/// Decrypt data using AES-256-GCM
fn decrypt_data(data: &[u8], key: &[u8; 32], block_id: BlockId) -> Result<Bytes> {
    use aes_gcm::{
        aead::{Aead, KeyInit},
        Aes256Gcm, Nonce,
    };

    let cipher = Aes256Gcm::new(key.into());

    // Use block_id as part of nonce (12 bytes)
    let mut nonce_bytes = [0u8; 12];
    nonce_bytes[0..8].copy_from_slice(&block_id.to_le_bytes());
    let nonce = Nonce::from_slice(&nonce_bytes);

    let plaintext = cipher
        .decrypt(nonce, data)
        .map_err(|e| Error::EncryptionError(format!("Decryption failed: {}", e)))?;

    Ok(Bytes::from(plaintext))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;
    use std::io::Write;

    #[test]
    fn test_block_flags() {
        let flags = BlockFlags::new();
        assert!(!flags.is_encrypted());
        assert!(!flags.is_compressed());

        let flags = flags.with_encryption();
        assert!(flags.is_encrypted());
        assert!(!flags.is_compressed());
    }

    #[test]
    fn test_block_write_read() {
        let tmp = NamedTempFile::new().unwrap();
        let data = Bytes::from("hello world");

        // Write
        {
            let file = tmp.reopen().unwrap();
            let mut writer = BlockWriter::new(file);
            let block = Block::new(1, data.clone());
            writer.write(&block, 0).unwrap();
            writer.flush().unwrap();
        }

        // Read
        {
            let file = tmp.reopen().unwrap();
            let mut reader = BlockReader::new(file);
            let block = reader.read(1, 0).unwrap();
            assert_eq!(block.id, 1);
            assert_eq!(block.data, data);
            assert!(!block.flags.is_encrypted());
        }
    }

    #[test]
    fn test_block_encryption() {
        let tmp = NamedTempFile::new().unwrap();
        let data = Bytes::from("secret data");
        let key = [42u8; 32];

        // Write encrypted
        {
            let file = tmp.reopen().unwrap();
            let mut writer = BlockWriter::with_encryption(file, key);
            let block = Block::with_encryption(1, data.clone());
            writer.write(&block, 0).unwrap();
            writer.flush().unwrap();
        }

        // Read encrypted
        {
            let file = tmp.reopen().unwrap();
            let mut reader = BlockReader::with_encryption(file, key);
            let block = reader.read(1, 0).unwrap();
            assert_eq!(block.id, 1);
            assert_eq!(block.data, data);
            assert!(block.flags.is_encrypted());
        }
    }

    #[test]
    fn test_block_encryption_wrong_key() {
        let tmp = NamedTempFile::new().unwrap();
        let data = Bytes::from("secret data");
        let key1 = [1u8; 32];
        let key2 = [2u8; 32];

        // Write with key1
        {
            let file = tmp.reopen().unwrap();
            let mut writer = BlockWriter::with_encryption(file, key1);
            let block = Block::with_encryption(1, data.clone());
            writer.write(&block, 0).unwrap();
            writer.flush().unwrap();
        }

        // Try to read with key2
        {
            let file = tmp.reopen().unwrap();
            let mut reader = BlockReader::with_encryption(file, key2);
            let result = reader.read(1, 0);
            assert!(matches!(result, Err(Error::EncryptionError(_))));
        }
    }

    #[test]
    fn test_block_checksum_corruption() {
        let tmp = NamedTempFile::new().unwrap();
        let data = Bytes::from("test");

        // Write valid block
        {
            let file = tmp.reopen().unwrap();
            let mut writer = BlockWriter::new(file);
            let block = Block::new(1, data.clone());
            writer.write(&block, 0).unwrap();
            writer.flush().unwrap();
        }

        // Corrupt the checksum
        {
            let mut file = tmp.reopen().unwrap();
            file.seek(SeekFrom::Start(BLOCK_SIZE as u64 - 1)).unwrap();
            file.write_all(&[0xFF]).unwrap();
            file.sync_all().unwrap();
        }

        // Try to read
        {
            let file = tmp.reopen().unwrap();
            let mut reader = BlockReader::new(file);
            let result = reader.read(1, 0);
            assert!(matches!(result, Err(Error::ChecksumMismatch)));
        }
    }

    #[test]
    fn test_block_max_data_size() {
        let max_size = Block::max_data_size();
        assert!(max_size > 0);
        assert!(max_size < BLOCK_SIZE);

        // Should be able to fit this much data
        let tmp = NamedTempFile::new().unwrap();
        let data = Bytes::from(vec![0xAB; max_size]);

        let file = tmp.reopen().unwrap();
        let mut writer = BlockWriter::new(file);
        let block = Block::new(1, data.clone());
        writer.write(&block, 0).unwrap();

        let file = tmp.reopen().unwrap();
        let mut reader = BlockReader::new(file);
        let read_block = reader.read(1, 0).unwrap();
        assert_eq!(read_block.data, data);
    }
}