lowmc-rs 0.1.0

LowMC block cipher implementation in Rust
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use clap::{Parser, Subcommand};
use lowmc_rs::LowMC;
use std::fs;
use std::io::{self, Read, Write};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "lowmc")]
#[command(about = "LowMC block cipher CLI tool")]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate a new LowMC key and save it to ~/.lowmc/
    GenerateKey {
        /// Name for the key file (without extension)
        #[arg(short, long, default_value = "default")]
        name: String,

        /// Overwrite existing key file
        #[arg(short, long)]
        force: bool,
    },

    /// Encrypt data using a saved key
    Encrypt {
        /// Name of the key to use
        #[arg(short, long, default_value = "default")]
        key: String,

        /// Input file (use - for stdin)
        #[arg(short, long, default_value = "-")]
        input: String,

        /// Output file (use - for stdout)
        #[arg(short, long, default_value = "-")]
        output: String,

        /// Format: hex, base64, or raw
        #[arg(short, long, default_value = "hex")]
        format: String,
    },

    /// Decrypt data using a saved key
    Decrypt {
        /// Name of the key to use
        #[arg(short, long, default_value = "default")]
        key: String,

        /// Input file (use - for stdin)
        #[arg(short, long, default_value = "-")]
        input: String,

        /// Output file (use - for stdout)
        #[arg(short, long, default_value = "-")]
        output: String,

        /// Format: hex, base64, or raw
        #[arg(short, long, default_value = "hex")]
        format: String,
    },

    /// List all saved keys
    ListKeys,

    /// Show information about a key
    KeyInfo {
        /// Name of the key
        #[arg(short, long, default_value = "default")]
        name: String,
    },
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::GenerateKey { name, force } => {
            generate_key(&name, force);
        }
        Commands::Encrypt {
            key,
            input,
            output,
            format,
        } => {
            encrypt_data(&key, &input, &output, &format);
        }
        Commands::Decrypt {
            key,
            input,
            output,
            format,
        } => {
            decrypt_data(&key, &input, &output, &format);
        }
        Commands::ListKeys => {
            list_keys();
        }
        Commands::KeyInfo { name } => {
            key_info(&name);
        }
    }
}

fn get_lowmc_dir() -> PathBuf {
    let mut home = dirs::home_dir().expect("Could not find home directory");
    home.push(".lowmc");
    home
}

fn ensure_lowmc_dir() -> io::Result<()> {
    let dir = get_lowmc_dir();
    if !dir.exists() {
        fs::create_dir_all(&dir)?;
    }
    Ok(())
}

fn get_key_path(name: &str) -> PathBuf {
    let mut path = get_lowmc_dir();
    path.push(format!("{}.key", name));
    path
}

fn generate_key(name: &str, force: bool) {
    let key_path = get_key_path(name);

    if key_path.exists() && !force {
        eprintln!("Key '{}' already exists. Use --force to overwrite.", name);
        std::process::exit(1);
    }

    match ensure_lowmc_dir() {
        Ok(()) => {
            let key = LowMC::generate_random_key();
            let key_hex = format!("{:032x}", key);

            match fs::write(&key_path, &key_hex) {
                Ok(()) => {
                    println!("Generated new key '{}': {}", name, key_hex);
                    println!("Key saved to: {}", key_path.display());
                }
                Err(e) => {
                    eprintln!("Failed to save key: {}", e);
                    std::process::exit(1);
                }
            }
        }
        Err(e) => {
            eprintln!("Failed to create .lowmc directory: {}", e);
            std::process::exit(1);
        }
    }
}

fn load_key(name: &str) -> u128 {
    let key_path = get_key_path(name);

    if !key_path.exists() {
        eprintln!(
            "Key '{}' not found. Generate it first with 'lowmc generate-key --name {}'",
            name, name
        );
        std::process::exit(1);
    }

    match fs::read_to_string(&key_path) {
        Ok(key_hex) => {
            let key_hex = key_hex.trim();
            if key_hex.len() != 32 {
                eprintln!("Invalid key format in {}", key_path.display());
                std::process::exit(1);
            }

            match u128::from_str_radix(key_hex, 16) {
                Ok(key) => key,
                Err(_) => {
                    eprintln!("Invalid key format in {}", key_path.display());
                    std::process::exit(1);
                }
            }
        }
        Err(e) => {
            eprintln!("Failed to read key file: {}", e);
            std::process::exit(1);
        }
    }
}

fn read_input(input: &str) -> Vec<u8> {
    if input == "-" {
        let mut data = Vec::new();
        io::stdin()
            .read_to_end(&mut data)
            .expect("Failed to read from stdin");
        data
    } else {
        fs::read(input).expect(&format!("Failed to read file: {}", input))
    }
}

fn write_output(output: &str, data: &[u8]) {
    if output == "-" {
        io::stdout()
            .write_all(data)
            .expect("Failed to write to stdout");
    } else {
        fs::write(output, data).expect(&format!("Failed to write file: {}", output));
    }
}

fn encrypt_data(key_name: &str, input: &str, output: &str, format: &str) {
    let key = load_key(key_name);
    let cipher = LowMC::new(key);

    let input_data = read_input(input);

    // Pad input to 16-byte boundary (128 bits)
    let mut padded_data = input_data.clone();
    let padding_len = 16 - (input_data.len() % 16);
    if padding_len < 16 {
        padded_data.extend_from_slice(&vec![padding_len as u8; padding_len]);
    } else {
        padded_data.extend_from_slice(&vec![16u8; 16]);
    }

    let mut encrypted_data = Vec::new();

    // Encrypt each 16-byte block, output 32 bytes per block (low+high)
    for chunk in padded_data.chunks(16) {
        let mut block = [0u8; 16];
        block.copy_from_slice(chunk);
        let block_u128 = u128::from_le_bytes(block);

        let (low, high) = cipher.encrypt(block_u128);
        encrypted_data.extend_from_slice(&low.to_le_bytes());
        encrypted_data.extend_from_slice(&high.to_le_bytes());
    }

    // Format output
    let formatted_data = match format.to_lowercase().as_str() {
        "hex" => {
            let hex_string = encrypted_data
                .iter()
                .map(|b| format!("{:02x}", b))
                .collect::<String>();
            hex_string.into_bytes()
        }
        "base64" => {
            // Simple base64 implementation - in production use a proper crate
            let mut base64 = Vec::new();
            for chunk in encrypted_data.chunks(3) {
                let mut bytes = [0u8; 3];
                bytes[..chunk.len()].copy_from_slice(chunk);

                let b64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                let mut val = (bytes[0] as u32) << 16 | (bytes[1] as u32) << 8 | bytes[2] as u32;

                for _ in 0..4 {
                    let idx = (val >> 18) & 0x3F;
                    base64.push(b64_chars.as_bytes()[idx as usize]);
                    val <<= 6;
                }
            }
            base64
        }
        "raw" => encrypted_data,
        _ => {
            eprintln!("Unsupported format: {}. Use hex, base64, or raw", format);
            std::process::exit(1);
        }
    };

    write_output(output, &formatted_data);
}

fn decrypt_data(key_name: &str, input: &str, output: &str, format: &str) {
    let key = load_key(key_name);
    let cipher = LowMC::new(key);

    let input_data = read_input(input);

    // Parse input format
    let encrypted_data = match format.to_lowercase().as_str() {
        "hex" => {
            let hex_string = String::from_utf8(input_data).expect("Invalid UTF-8 in hex input");
            let mut data = Vec::new();
            for chunk in hex_string.as_bytes().chunks(2) {
                if chunk.len() == 2 {
                    let byte = u8::from_str_radix(std::str::from_utf8(chunk).unwrap(), 16)
                        .expect("Invalid hex");
                    data.push(byte);
                }
            }
            data
        }
        "base64" => {
            // Simple base64 decoding - in production use a proper crate
            let mut data = Vec::new();
            let b64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

            for chunk in input_data.chunks(4) {
                let mut val = 0u32;
                for &byte in chunk {
                    if let Some(idx) = b64_chars.find(byte as char) {
                        val = (val << 6) | (idx as u32);
                    }
                }

                data.push((val >> 16) as u8);
                if chunk.len() > 1 {
                    data.push((val >> 8) as u8);
                }
                if chunk.len() > 2 {
                    data.push(val as u8);
                }
            }
            data
        }
        "raw" => input_data,
        _ => {
            eprintln!("Unsupported format: {}. Use hex, base64, or raw", format);
            std::process::exit(1);
        }
    };

    if encrypted_data.len() % 32 != 0 {
        eprintln!("Ciphertext length must be a multiple of 32 bytes (256 bits per block)");
        std::process::exit(1);
    }

    let mut decrypted_data = Vec::new();

    // Decrypt each 32-byte block
    for chunk in encrypted_data.chunks(32) {
        let mut low_bytes = [0u8; 16];
        let mut high_bytes = [0u8; 16];
        low_bytes.copy_from_slice(&chunk[..16]);
        high_bytes.copy_from_slice(&chunk[16..]);
        let low = u128::from_le_bytes(low_bytes);
        let high = u128::from_le_bytes(high_bytes);
        let decrypted = cipher.decrypt(low, high);
        decrypted_data.extend_from_slice(&decrypted.to_le_bytes());
    }

    // Remove padding
    if let Some(&padding_len) = decrypted_data.last() {
        if padding_len <= 16 && padding_len > 0 {
            let len = decrypted_data.len();
            if len >= padding_len as usize {
                decrypted_data.truncate(len - padding_len as usize);
            }
        }
    }

    write_output(output, &decrypted_data);
}

fn list_keys() {
    match ensure_lowmc_dir() {
        Ok(()) => {
            let dir = get_lowmc_dir();
            match fs::read_dir(dir) {
                Ok(entries) => {
                    let mut keys = Vec::new();
                    for entry in entries {
                        if let Ok(entry) = entry {
                            if let Some(ext) = entry.path().extension() {
                                if ext == "key" {
                                    if let Some(name) = entry.path().file_stem() {
                                        keys.push(name.to_string_lossy().to_string());
                                    }
                                }
                            }
                        }
                    }

                    if keys.is_empty() {
                        println!("No keys found. Generate one with 'lowmc generate-key'");
                    } else {
                        println!("Available keys:");
                        for key in keys {
                            println!("  {}", key);
                        }
                    }
                }
                Err(e) => {
                    eprintln!("Failed to read .lowmc directory: {}", e);
                    std::process::exit(1);
                }
            }
        }
        Err(e) => {
            eprintln!("Failed to access .lowmc directory: {}", e);
            std::process::exit(1);
        }
    }
}

fn key_info(name: &str) {
    let key_path = get_key_path(name);

    if !key_path.exists() {
        eprintln!("Key '{}' not found", name);
        std::process::exit(1);
    }

    match fs::metadata(&key_path) {
        Ok(metadata) => {
            println!("Key: {}", name);
            println!("Path: {}", key_path.display());
            println!("Size: {} bytes", metadata.len());

            if let Ok(created) = metadata.created() {
                println!("Created: {:?}", created);
            }

            // Show first few characters of the key
            if let Ok(key_content) = fs::read_to_string(&key_path) {
                let key_hex = key_content.trim();
                if key_hex.len() >= 8 {
                    println!("Key (first 8 chars): {}...", &key_hex[..8]);
                }
            }
        }
        Err(e) => {
            eprintln!("Failed to get key info: {}", e);
            std::process::exit(1);
        }
    }
}