dixscript 1.0.0

Config, code, and encryption in one file — a data interchange format with compile-time functions, AES-256/ChaCha20 built-in, and cross-platform FFI
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

//! Semantic validation of the @SECURITY section.

use crate::Compiler::AST::{SecuritySection, SecurityEntry, SecurityField, Value, Position};
use crate::Compiler::Utilities::SymbolTable;
use crate::Compiler::Core::{OperationalSettings, ErrorHandlingStrategy};
use crate::ErrorManager::{ErrorManager, DebugConfig};
use rustc_hash::FxHashMap;
use lazy_static::lazy_static;

use super::{SectionAnalysisResult, SemanticWarningInfo};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum EncryptionMode {
    Password,
    Keyfile,
    Manual,
}

impl EncryptionMode {
    #[inline]
    fn from_str(s: &str) -> Option<Self> {
        match s {
            "password" => Some(Self::Password),
            "keyfile"  => Some(Self::Keyfile),
            "manual"   => Some(Self::Manual),
            _          => None,
        }
    }

    #[inline]
    fn as_str(self) -> &'static str {
        match self {
            Self::Password => "password",
            Self::Keyfile  => "keyfile",
            Self::Manual   => "manual",
        }
    }

    #[inline]
    fn required_fields(self) -> &'static [&'static str] {
        match self {
            Self::Password => &["mode", "algorithm", "kdf"],
            Self::Keyfile  => &["mode", "algorithm"],
            Self::Manual   => &["mode", "key", "iv"],
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum Algorithm {
    Xor,
    Aes128Gcm,
    Aes128,
    Aes256Gcm,
    Aes256,
    Chacha20Poly1305,
    Chacha20,
}

impl Algorithm {
    #[inline]
    fn from_str(s: &str) -> Option<Self> {
        match s {
            "xor"                => Some(Self::Xor),
            "aes128-gcm"         => Some(Self::Aes128Gcm),
            "aes128"             => Some(Self::Aes128),
            "aes256-gcm"         => Some(Self::Aes256Gcm),
            "aes256"             => Some(Self::Aes256),
            "chacha20-poly1305"  => Some(Self::Chacha20Poly1305),
            "chacha20"           => Some(Self::Chacha20),
            _                    => None,
        }
    }

    #[inline]
    fn is_high_security(self) -> bool {
        matches!(self, Self::Aes256Gcm | Self::Aes256 | Self::Chacha20Poly1305 | Self::Chacha20)
    }

    #[inline]
    fn is_low_security(self) -> bool {
        matches!(self, Self::Xor)
    }
}

lazy_static! {
    static ref VALID_BLOCK_KEYS: FxHashMap<&'static str, ()> = {
        let mut m = FxHashMap::default();
        m.insert("encryption", ());
        m.insert("validation", ());
        m.insert("keystore",   ());
        m.insert("override",   ());
        m.insert("metadata",   ());
        m
    };

    static ref VALID_CHECKSUM_ALGORITHMS: FxHashMap<&'static str, ()> = {
        let mut m = FxHashMap::default();
        m.insert("sha256",       ());
        m.insert("sha512",       ());
        m.insert("hmac-sha256",  ());
        m.insert("hmac-sha512",  ());
        m
    };

    static ref VALID_KDF_ALGORITHMS: FxHashMap<&'static str, ()> = {
        let mut m = FxHashMap::default();
        m.insert("argon2id", ());
        m.insert("pbkdf2",   ());
        m
    };
}

const WARN_EMPTY_SECTION:           &str = "SEC_WARN001";
const WARN_EMPTY_BLOCK:             &str = "SEC_WARN002";
const WARN_MANUAL_MODE_CRITICAL:    &str = "SEC_WARN003";
const WARN_XOR_LOW_SECURITY:        &str = "SEC_WARN004";
const WARN_KDF_PARAM_BELOW_MIN:     &str = "SEC_WARN005";
const WARN_MANUAL_MODE_ENABLED:     &str = "SEC_WARN006";
const WARN_MISSING_FIELD_DEFAULT:   &str = "SEC_WARN007";
const WARN_BACKUP_COUNT_RANGE:      &str = "SEC_WARN008";

struct ParsedEncryptionConfig<'a> {
    mode:      Option<EncryptionMode>,
    algorithm: Option<Algorithm>,
    kdf:       Option<&'a str>,
    field_map: FxHashMap<&'a str, &'a SecurityField>,
}

impl<'a> ParsedEncryptionConfig<'a> {
    fn from_entry(entry: &'a SecurityEntry) -> Self {
        let mut field_map: FxHashMap<&str, &SecurityField> = FxHashMap::default();
        for field in &entry.fields {
            field_map.insert(field.key.as_str(), field);
        }

        let mode = field_map.get("mode")
            .and_then(|f| extract_str(&f.value))
            .and_then(EncryptionMode::from_str);

        let algorithm = field_map.get("algorithm")
            .and_then(|f| extract_str(&f.value))
            .and_then(Algorithm::from_str);

        let kdf = field_map.get("kdf")
            .and_then(|f| extract_str(&f.value));

        ParsedEncryptionConfig { mode, algorithm, kdf, field_map }
    }

    #[inline] fn has_field(&self, key: &str) -> bool { self.field_map.contains_key(key) }

    #[inline] fn get_field(&self, key: &str) -> Option<&'a SecurityField> {
        self.field_map.get(key).copied()
    }

    #[inline] fn get_int_field(&self, key: &str) -> Option<i32> {
        self.get_field(key).and_then(|f| match &f.value {
            Value::Integer { value, .. } => Some(*value),
            _ => None,
        })
    }
    #[inline] fn get_long_field(&self, key: &str) -> Option<i64> {
        self.get_field(key).and_then(|f| match &f.value {
            Value::Long { value, .. } => Some(*value),
            _ => None,
        })
    }
    #[inline] fn get_bool_field(&self, key: &str) -> Option<bool> {
        self.get_field(key).and_then(|f| match &f.value {
            Value::Boolean { value, .. } => Some(*value),
            _ => None,
        })
    }
}

#[inline]
fn extract_str(value: &Value) -> Option<&str> {
    match value {
        Value::String { value, .. } => Some(value.as_str()),
        _ => None,
    }
}

#[inline]
fn extract_int(value: &Value) -> Option<i32> {
    match value {
        Value::Integer { value, .. } => Some(*value),
        _ => None,
    }
}

#[inline]
fn extract_bool(value: &Value) -> Option<bool> {
    match value {
        Value::Boolean { value, .. } => Some(*value),
        _ => None,
    }
}

/// Case-insensitive lookup against a pre-built lowercase key map.
#[inline]
fn is_valid_key_ci(key: &str, valid: &FxHashMap<&'static str, ()>) -> bool {
    valid.contains_key(key) || valid.keys().any(|k| k.eq_ignore_ascii_case(key))
}

pub struct SecuritySectionAnalyzer<'a> {
    operational_settings: &'a OperationalSettings,
    error_manager: ErrorManager,
    debug_config: DebugConfig,
}

impl<'a> SecuritySectionAnalyzer<'a> {
    pub fn new(operational_settings: &'a OperationalSettings) -> Self {
      Self::new_with_error_manager(operational_settings,ErrorManager::get_shared_instance())
    }
pub fn new_with_error_manager(
    operational_settings: &'a OperationalSettings,
    error_manager: ErrorManager,
) -> Self {
    SecuritySectionAnalyzer {
        error_manager,
        debug_config: DebugConfig::from_debug_mode(operational_settings.debug_mode),
        operational_settings,
    }
}
    pub fn analyze(
        &mut self,
        section: &SecuritySection,
        _symbol_table: &mut SymbolTable,
    ) -> SectionAnalysisResult {
        let mut result = SectionAnalysisResult::new("SECURITY");

        if self.debug_config.is_enabled {
            self.error_manager.log_info(&format!(
                "Analyzing SECURITY section with {} entries",
                section.entries.len()
            ));
        }

        if section.entries.is_empty() {
            self.add_warning(&mut result, WARN_EMPTY_SECTION,
                "SECURITY section is empty - default settings will be used", None);
            result.is_success = true;
            return result;
        }

        let entry_map = Self::build_entry_map(&section.entries);

        self.validate_structure(&section.entries, &mut result);

        if self.should_halt(&result) {
            return result;
        }

        let encryption_config = entry_map.get("encryption")
            .map(|e| ParsedEncryptionConfig::from_entry(e));

        let mode = match encryption_config {
            Some(ref cfg) => self.validate_encryption_mode(cfg, &mut result),
            None => {
                self.add_warning(&mut result, WARN_MISSING_FIELD_DEFAULT,
                    "No encryption configuration found - defaults will be applied", None);
                Some(EncryptionMode::Keyfile)
            }
        };

        if let Some(m) = mode {
            if let Some(ref cfg) = encryption_config {
                self.validate_mode_requirements(m, cfg, &mut result);
                self.validate_algorithm(cfg, &mut result);
            }

            match m {
                EncryptionMode::Password => {
                    if let Some(ref cfg) = encryption_config {
                        self.validate_kdf_parameters(cfg, &mut result);
                    }
                }
                EncryptionMode::Keyfile => {
                    if let Some(entry) = entry_map.get("keystore") {
                        self.validate_keystore(entry, &mut result);
                    }
                }
                EncryptionMode::Manual => {
                    if let Some(entry) = entry_map.get("override") {
                        self.validate_manual_mode(entry, &mut result);
                    }
                }
            }

            if let Some(entry) = entry_map.get("validation") {
                self.validate_validation_config(entry, &mut result);
            }

            if self.debug_config.is_enabled {
                self.log_security_level(m);
            }
        }

        result.is_success = result.errors.is_empty();

        if self.debug_config.is_enabled {
            self.error_manager.log_info(&format!(
                "SECURITY analysis complete: {} — errors: {}, warnings: {}",
                if result.is_success { "SUCCESS" } else { "FAILURE" },
                result.errors.len(),
                result.warnings.len()
            ));
        }

        result
    }

    fn build_entry_map<'e>(entries: &'e [SecurityEntry]) -> FxHashMap<&'static str, &'e SecurityEntry> {
        let mut map: FxHashMap<&'static str, &'e SecurityEntry> = FxHashMap::default();
        for entry in entries {
            let key = entry.block_key.as_str();
            let canonical: Option<&'static str> = if key.eq_ignore_ascii_case("encryption") {
                Some("encryption")
            } else if key.eq_ignore_ascii_case("validation") {
                Some("validation")
            } else if key.eq_ignore_ascii_case("keystore") {
                Some("keystore")
            } else if key.eq_ignore_ascii_case("override") {
                Some("override")
            } else if key.eq_ignore_ascii_case("metadata") {
                Some("metadata")
            } else {
                None
            };
            if let Some(k) = canonical {
                map.insert(k, entry);
            }
        }
        map
    }

    fn validate_structure(
        &mut self,
        entries: &[SecurityEntry],
        result: &mut SectionAnalysisResult,
    ) {
        for entry in entries {
            if !is_valid_key_ci(entry.block_key.as_str(), &VALID_BLOCK_KEYS) {
                if self.debug_config.is_enabled {
                    self.error_manager.log_warning(&format!(
                        "Unknown security block key: {} (ignored)", entry.block_key
                    ));
                }
                self.add_warning(result, WARN_EMPTY_BLOCK,
                    &format!("Unknown security block key: {} (ignored)", entry.block_key),
                    Some(entry.position));
            }
            if entry.fields.is_empty() {
                self.add_warning(result, WARN_EMPTY_BLOCK,
                    &format!("Security block '{}' is empty", entry.block_key),
                    Some(entry.position));
            }
        }
    }

    fn validate_encryption_mode(
        &mut self,
        config: &ParsedEncryptionConfig,
        result: &mut SectionAnalysisResult,
    ) -> Option<EncryptionMode> {
        if let Some(mode) = config.mode {
            if self.debug_config.is_enabled {
                self.error_manager.log_info(&format!("Encryption mode: {}", mode.as_str()));
            }
            Some(mode)
        } else {
            let pos = config.get_field("mode").map(|f| f.position);
            let msg = if config.has_field("mode") {
                "Encryption mode must be a string - defaulting to 'keyfile'"
            } else {
                "Encryption mode not specified - defaulting to 'keyfile'"
            };
            self.add_warning(result, WARN_MISSING_FIELD_DEFAULT, msg, pos);
            Some(EncryptionMode::Keyfile)
        }
    }

    fn validate_mode_requirements(
        &mut self,
        mode: EncryptionMode,
        config: &ParsedEncryptionConfig,
        result: &mut SectionAnalysisResult,
    ) {
        for &field in mode.required_fields() {
            if !config.has_field(field) {
                self.add_warning(result, WARN_MISSING_FIELD_DEFAULT,
                    &format!("Required field '{}' missing for mode '{}' - will use default",
                        field, mode.as_str()),
                    None);
            }
        }
        if mode == EncryptionMode::Manual {
            self.add_warning(result, WARN_MANUAL_MODE_CRITICAL,
                "CRITICAL: Manual mode stores the encryption key in PLAINTEXT in source", None);
        }
    }

    fn validate_algorithm(
        &mut self,
        config: &ParsedEncryptionConfig,
        result: &mut SectionAnalysisResult,
    ) {
        if let Some(alg) = config.algorithm {
            if alg.is_low_security() {
                self.add_warning(result, WARN_XOR_LOW_SECURITY,
                    "Algorithm 'xor' provides LOW security - obfuscation only", None);
            } else if self.debug_config.is_enabled {
                self.error_manager.log_info(&format!(
                    "Encryption algorithm: {:?} ({} security)",
                    alg,
                    if alg.is_high_security() { "HIGH" } else { "MEDIUM" }
                ));
            }
        } else {
            let pos = config.get_field("algorithm").map(|f| f.position);
            let msg = if config.has_field("algorithm") {
                "Algorithm must be a string - will default to 'aes256-gcm'"
            } else {
                "Encryption algorithm not specified - will default to 'aes256-gcm'"
            };
            self.add_warning(result, WARN_MISSING_FIELD_DEFAULT, msg, pos);
        }
    }

    fn validate_kdf_parameters(
        &mut self,
        config: &ParsedEncryptionConfig,
        result: &mut SectionAnalysisResult,
    ) {
        if let Some(kdf) = config.kdf {
            if !is_valid_key_ci(kdf, &VALID_KDF_ALGORITHMS) {
                self.add_warning(result, WARN_MISSING_FIELD_DEFAULT,
                    &format!("Unknown KDF algorithm: {} - will default to 'argon2id'", kdf), None);
            } else if self.debug_config.is_verbose {
                self.error_manager.log_info(&format!("Key derivation function: {}", kdf));
            }
        } else {
            self.add_warning(result, WARN_MISSING_FIELD_DEFAULT,
                "Key derivation function not specified - will default to 'argon2id'", None);
        }

        self.validate_kdf_param(config, "kdf_memory",      65536, result);
        self.validate_kdf_param(config, "kdf_iterations",      3, result);
        self.validate_kdf_param(config, "kdf_parallelism",     4, result);
    }

    #[inline]
    fn validate_kdf_param(
        &mut self,
        config: &ParsedEncryptionConfig,
        param: &str,
        min: i32,
        result: &mut SectionAnalysisResult,
    ) {
        if let Some(v) = config.get_int_field(param) {
            if v < min {
                self.add_warning(result, WARN_KDF_PARAM_BELOW_MIN,
                    &format!("KDF parameter '{}' value {} below recommended minimum {}",
                        param, v, min),
                    None);
            }
        } else if self.debug_config.is_verbose && config.has_field(param) {
            self.error_manager.log_debug(&format!(
                "KDF parameter '{}' must be integer - will use default", param
            ));
        }
    }

    fn validate_keystore(
        &mut self,
        entry: &SecurityEntry,
        result: &mut SectionAnalysisResult,
    ) {
        let mut field_map: FxHashMap<&str, &SecurityField> = FxHashMap::default();
        for f in &entry.fields { field_map.insert(f.key.as_str(), f); }

        if let Some(f) = field_map.get("auto_generate") {
            if let Some(v) = extract_bool(&f.value) {
                if self.debug_config.is_enabled {
                    self.error_manager.log_info(&format!("Keystore auto-generation: {}", v));
                }
            }
        }

        if let Some(f) = field_map.get("backup_count") {
            if let Some(v) = extract_int(&f.value) {
                if !(0..=10).contains(&v) {
                    self.add_warning(result, WARN_BACKUP_COUNT_RANGE,
                        &format!("Backup count {} out of range (0-10) - will use default 3", v),
                        Some(f.position));
                } else if self.debug_config.is_enabled {
                    self.error_manager.log_info(&format!("Keystore backup count: {}", v));
                }
            }
        }
    }

    fn validate_manual_mode(
        &mut self,
        entry: &SecurityEntry,
        result: &mut SectionAnalysisResult,
    ) {
        let accepted = entry.fields.iter()
            .find(|f| f.key == "manual_key_warning_accepted")
            .and_then(|f| extract_bool(&f.value))
            .unwrap_or(false);

        let msg = if accepted {
            "Manual mode enabled - encryption key stored in PLAINTEXT"
        } else {
            "Manual mode key warning not explicitly accepted - encryption may fail"
        };
        self.add_warning(result, WARN_MANUAL_MODE_ENABLED, msg, Some(entry.position));
    }

    fn validate_validation_config(
        &mut self,
        entry: &SecurityEntry,
        result: &mut SectionAnalysisResult,
    ) {
        let mut field_map: FxHashMap<&str, &SecurityField> = FxHashMap::default();
        for f in &entry.fields { field_map.insert(f.key.as_str(), f); }

        if let Some(f) = field_map.get("checksum_algorithm") {
            if let Some(alg) = extract_str(&f.value) {
                if !is_valid_key_ci(alg, &VALID_CHECKSUM_ALGORITHMS) {
                    self.add_warning(result, WARN_MISSING_FIELD_DEFAULT,
                        &format!("Unknown checksum algorithm: {} - will default to 'sha256'", alg),
                        Some(f.position));
                } else if self.debug_config.is_enabled {
                    self.error_manager.log_info(&format!("Checksum algorithm: {}", alg));
                }
            }
        }
    }

    #[inline]
    fn log_security_level(&self, mode: EncryptionMode) {
        let level = match mode {
            EncryptionMode::Password => "HIGH (Argon2id-derived key)",
            EncryptionMode::Keyfile  => "HIGH (randomly generated key)",
            EncryptionMode::Manual   => "CRITICAL_RISK (plaintext key)",
        };
        self.error_manager.log_info(&format!("Security level: {}", level));
    }

    #[inline]
    fn should_halt(&self, result: &SectionAnalysisResult) -> bool {
        !result.errors.is_empty()
            && self.operational_settings.error_handling_strategy == ErrorHandlingStrategy::Halt
    }

    fn add_warning(
        &mut self,
        result: &mut SectionAnalysisResult,
        warning_id: &str,
        message: &str,
        position: Option<Position>,
    ) {
        result.warnings.push(SemanticWarningInfo {
            warning_id:   warning_id.to_string(),
            message:      message.to_string(),
            section_name: "SECURITY".to_string(),
            position,
        });
        if self.debug_config.is_enabled {
            self.error_manager.log_warning(message);
        }
    }
}