procfs_core/
crypto.rs

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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use crate::{expect, FromBufRead, ProcError, ProcResult};

#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    convert::TryFrom,
    io::BufRead,
    iter::{once, Peekable},
    str::FromStr,
};

/// Represents the data from `/proc/crypto`.
///
/// Each block represents a cryptographic implementation that has been registered with the kernel.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct CryptoTable {
    pub crypto_blocks: HashMap<String, Vec<CryptoBlock>>,
}

/// Format of a crypto implementation represented in /proc/crypto.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct CryptoBlock {
    pub name: String,
    pub driver: String,
    pub module: String,
    pub priority: isize,
    pub ref_count: isize,
    pub self_test: SelfTest,
    pub internal: bool,
    pub fips_enabled: bool,
    pub crypto_type: Type,
}

impl FromBufRead for CryptoTable {
    fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> {
        let mut lines = r.lines().peekable();
        let mut crypto_blocks: HashMap<String, Vec<CryptoBlock>> = HashMap::new();
        while let Some(line) = lines.next() {
            let line = line?;
            // Just skip empty lines
            if !line.is_empty() {
                let mut split = line.split(':');
                let name = expect!(split.next());
                if name.trim() == "name" {
                    let name = expect!(split.next()).trim().to_string();
                    let block = CryptoBlock::from_iter(&mut lines, name.as_str())?;
                    let blocks = crypto_blocks.entry(name).or_insert(Vec::new());
                    blocks.push(block);
                }
            }
        }

        Ok(CryptoTable { crypto_blocks })
    }
}

impl CryptoTable {
    pub fn get<T: AsRef<str>>(&self, target: T) -> Option<&Vec<CryptoBlock>> {
        self.crypto_blocks.get(target.as_ref())
    }
}

impl CryptoBlock {
    fn from_iter<T: Iterator<Item = Result<String, std::io::Error>>>(
        iter: &mut Peekable<T>,
        name: &str,
    ) -> ProcResult<Self> {
        let driver = parse_line(iter, "driver", name)?;
        let module = parse_line(iter, "module", name)?;
        let priority = from_str!(isize, &parse_line(iter, "priority", name)?);
        let ref_count = from_str!(isize, &parse_line(iter, "refcnt", name)?);
        let self_test = SelfTest::try_from(parse_line(iter, "selftest", name)?.as_str())?;
        let internal = parse_bool(iter, "internal", name)?;
        let fips_enabled = parse_fips(iter, name)?;
        let crypto_type = Type::from_iter(iter, name)?;
        Ok(CryptoBlock {
            name: name.to_string(),
            driver,
            module,
            priority,
            ref_count,
            self_test,
            internal,
            fips_enabled,
            crypto_type,
        })
    }
}

/// Potential results for selftest.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub enum SelfTest {
    Passed,
    Unknown,
}

impl TryFrom<&str> for SelfTest {
    type Error = ProcError;

    fn try_from(value: &str) -> Result<Self, ProcError> {
        Ok(match value {
            "passed" => Self::Passed,
            "unknown" => Self::Unknown,
            _ => {
                return Err(build_internal_error!(format!(
                    "Could not recognise self test string {value}"
                )))
            }
        })
    }
}

/// Enumeration of potential types and their associated data. Unknown at end to catch unrecognised types.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub enum Type {
    /// Symmetric Key Cipher
    Skcipher(Skcipher),
    /// Single Block Cipher
    Cipher(Cipher),
    /// Syncronous Hash
    Shash(Shash),
    /// Asyncronous Hash
    Ahash(Ahash),
    /// Authenticated Encryption with Associated Data
    Aead(Aead),
    /// Random Number Generator
    Rng(Rng),
    /// Test algorithm
    Larval(Larval),
    /// Synchronous Compression
    Scomp,
    /// General Compression
    Compression,
    /// Asymmetric Cipher
    AkCipher,
    /// Key-agreement Protocol Primitive
    Kpp,
    /// Signature
    Sig,
    /// Unrecognised type, associated data collected in to a hash map
    Unknown(Unknown),
}

impl Type {
    fn from_iter<T: Iterator<Item = Result<String, std::io::Error>>>(
        iter: &mut Peekable<T>,
        name: &str,
    ) -> ProcResult<Self> {
        let type_name = parse_line(iter, "type", name)?;
        Ok(match type_name.as_str() {
            "skcipher" => Self::Skcipher(Skcipher::parse(iter, name)?),
            "cipher" => Self::Cipher(Cipher::parse(iter, name)?),
            "shash" => Self::Shash(Shash::parse(iter, name)?),
            "scomp" => Self::Scomp,
            "compression" => Self::Compression,
            "akcipher" => Self::AkCipher,
            "kpp" => Self::Kpp,
            "ahash" => Self::Ahash(Ahash::parse(iter, name)?),
            "aead" => Self::Aead(Aead::parse(iter, name)?),
            "rng" => Self::Rng(Rng::parse(iter, name)?),
            "larval" => Self::Larval(Larval::parse(iter, name)?),
            "sig" => Self::Sig,
            unknown_name => Self::Unknown(Unknown::parse(iter, unknown_name)),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Skcipher {
    pub async_capable: bool,
    pub block_size: usize,
    pub min_key_size: usize,
    pub max_key_size: usize,
    pub iv_size: usize,
    pub chunk_size: usize,
    pub walk_size: usize,
}

impl Skcipher {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(iter: &mut T, name: &str) -> ProcResult<Self> {
        let async_capable = parse_bool(iter, "async", name)?;
        let block_size = from_str!(usize, &parse_line(iter, "blocksize", name)?);
        let min_key_size = from_str!(usize, &parse_line(iter, "min keysize", name)?);
        let max_key_size = from_str!(usize, &parse_line(iter, "max keysize", name)?);
        let iv_size = from_str!(usize, &parse_line(iter, "ivsize", name)?);
        let chunk_size = from_str!(usize, &parse_line(iter, "chunksize", name)?);
        let walk_size = from_str!(usize, &parse_line(iter, "walksize", name)?);
        Ok(Self {
            async_capable,
            block_size,
            min_key_size,
            max_key_size,
            iv_size,
            chunk_size,
            walk_size,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Cipher {
    pub block_size: usize,
    pub min_key_size: usize,
    pub max_key_size: usize,
}

impl Cipher {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(iter: &mut T, name: &str) -> ProcResult<Self> {
        let block_size = from_str!(usize, &parse_line(iter, "blocksize", name)?);
        let min_key_size = from_str!(usize, &parse_line(iter, "min keysize", name)?);
        let max_key_size = from_str!(usize, &parse_line(iter, "max keysize", name)?);
        Ok(Self {
            block_size,
            min_key_size,
            max_key_size,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Shash {
    pub block_size: usize,
    pub digest_size: usize,
}

impl Shash {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(iter: &mut T, name: &str) -> ProcResult<Self> {
        let block_size = from_str!(usize, &parse_line(iter, "blocksize", name)?);
        let digest_size = from_str!(usize, &parse_line(iter, "digestsize", name)?);
        Ok(Self {
            block_size,
            digest_size,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Ahash {
    pub async_capable: bool,
    pub block_size: usize,
    pub digest_size: usize,
}

impl Ahash {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(iter: &mut T, name: &str) -> ProcResult<Self> {
        let async_capable = parse_bool(iter, "async", name)?;
        let block_size = from_str!(usize, &parse_line(iter, "blocksize", name)?);
        let digest_size = from_str!(usize, &parse_line(iter, "digestsize", name)?);
        Ok(Self {
            async_capable,
            block_size,
            digest_size,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Aead {
    pub async_capable: bool,
    pub block_size: usize,
    pub iv_size: usize,
    pub max_auth_size: usize,
    pub gen_iv: Option<usize>,
}

impl Aead {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(
        iter: &mut Peekable<T>,
        name: &str,
    ) -> ProcResult<Self> {
        let async_capable = parse_bool(iter, "async", name)?;
        let block_size = from_str!(usize, &parse_line(iter, "blocksize", name)?);
        let iv_size = from_str!(usize, &parse_line(iter, "ivsize", name)?);
        let max_auth_size = from_str!(usize, &parse_line(iter, "maxauthsize", name)?);
        let gen_iv = parse_gen_iv(iter, name)?;
        Ok(Self {
            async_capable,
            block_size,
            iv_size,
            max_auth_size,
            gen_iv,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Rng {
    pub seed_size: usize,
}

impl Rng {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(iter: &mut T, name: &str) -> ProcResult<Self> {
        let seed_size = from_str!(usize, &parse_line(iter, "seedsize", name)?);
        Ok(Self { seed_size })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Larval {
    pub flags: u32,
}

impl Larval {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(iter: &mut T, name: &str) -> ProcResult<Self> {
        let flags = from_str!(u32, &parse_line(iter, "flags", name)?);
        Ok(Self { flags })
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Unknown {
    pub fields: HashMap<String, String>,
}

impl Unknown {
    fn parse<T: Iterator<Item = Result<String, std::io::Error>>>(iter: &mut T, unknown_name: &str) -> Self {
        let fields = iter
            .map_while(|line| {
                let line = match line {
                    Ok(line) => line,
                    Err(_) => return None,
                };
                (!line.is_empty()).then(|| {
                    line.split_once(':')
                        .map(|(k, v)| (k.trim().to_string(), v.trim().to_string()))
                })
            })
            .flatten()
            .chain(once((String::from("name"), unknown_name.to_string())))
            .collect();
        Self { fields }
    }
}

fn parse_line<T: Iterator<Item = Result<String, std::io::Error>>>(
    iter: &mut T,
    to_find: &str,
    name: &str,
) -> ProcResult<String> {
    let line = expect!(iter.next())?;
    let (key, val) = expect!(line.split_once(':'));
    if key.trim() != to_find {
        return Err(build_internal_error!(format!(
            "could not locate {to_find} in /proc/crypto, block {name}"
        )));
    }
    Ok(val.trim().to_string())
}

fn parse_fips<T: Iterator<Item = Result<String, std::io::Error>>>(
    iter: &mut Peekable<T>,
    name: &str,
) -> ProcResult<bool> {
    if iter
        .peek()
        .map(|line| line.as_ref().is_ok_and(|line| line.contains("fips")))
        .unwrap_or(false)
    {
        let fips = parse_line(iter, "fips", name)?;
        if fips == "yes" {
            return Ok(true);
        }
    }
    Ok(false)
}

fn parse_bool<T: Iterator<Item = Result<String, std::io::Error>>>(
    iter: &mut T,
    to_find: &str,
    name: &str,
) -> ProcResult<bool> {
    match parse_line(iter, to_find, name)?.as_str() {
        "yes" => Ok(true),
        "no" => Ok(false),
        _ => Err(build_internal_error!(format!(
            "{to_find} for {name} was unrecognised term"
        ))),
    }
}

fn parse_gen_iv<T: Iterator<Item = Result<String, std::io::Error>>>(
    iter: &mut Peekable<T>,
    name: &str,
) -> ProcResult<Option<usize>> {
    if iter
        .peek()
        .map(|line| line.as_ref().is_ok_and(|line| line.contains("geniv")))
        .unwrap_or(false)
    {
        let val = parse_line(iter, "geniv", name)?;
        if val != "<none>" {
            return Ok(Some(expect!(usize::from_str(&val))));
        }
    }
    Ok(None)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn parse_line_correct() {
        let line = Ok("name         : ghash".to_string());
        let mut iter = std::iter::once(line);
        let val = match parse_line(&mut iter, "name", "parse_line_correct") {
            Ok(val) => val,
            Err(e) => panic!("{}", e),
        };
        assert_eq!("ghash", val);
    }

    #[test]
    fn parse_line_incorrect() {
        let line = Ok("name         : ghash".to_string());
        let mut iter = std::iter::once(line);
        let val = match parse_line(&mut iter, "name", "parse_line_incorrect") {
            Ok(val) => val,
            Err(e) => panic!("{}", e),
        };
        assert_ne!("hash", val);
    }

    #[test]
    fn parse_block() {
        let block = r#"driver       : deflate-generic
module       : kernel
priority     : 0
refcnt       : 2
selftest     : passed
internal     : no
type         : compression"#;
        let mut iter = block.lines().map(|s| Ok(s.to_string())).peekable();
        let block = CryptoBlock::from_iter(&mut iter, "deflate");
        let block = block.expect("Should be have read one block");
        assert_eq!(block.name, "deflate");
        assert_eq!(block.driver, "deflate-generic");
        assert_eq!(block.module, "kernel");
        assert_eq!(block.priority, 0);
        assert_eq!(block.ref_count, 2);
        assert_eq!(block.self_test, SelfTest::Passed);
        assert_eq!(block.internal, false);
        assert_eq!(block.crypto_type, Type::Compression);
    }

    #[test]
    fn parse_bad_block() {
        let block = r#"driver       : deflate-generic
module       : kernel
priority     : 0
refcnt       : 2
selftest     : passed
internal     : no
type         : aead"#;
        let mut iter = block.lines().map(|s| Ok(s.to_string())).peekable();
        let block = CryptoBlock::from_iter(&mut iter, "deflate");
        eprintln!("{block:?}");
        assert!(block.is_err());
    }

    #[test]
    fn parse_two() {
        let block = r#"name         : ccm(aes)
driver       : ccm_base(ctr(aes-aesni),cbcmac(aes-aesni))
module       : ccm
priority     : 300
refcnt       : 4
selftest     : passed
internal     : no
type         : aead
async        : no
blocksize    : 1
ivsize       : 16
maxauthsize  : 16
geniv        : <none>

name         : ctr(aes)
driver       : ctr(aes-aesni)
module       : kernel
priority     : 300
refcnt       : 4
selftest     : passed
internal     : no
type         : skcipher
async        : no
blocksize    : 1
min keysize  : 16
max keysize  : 32
ivsize       : 16
chunksize    : 16
walksize     : 16

"#;
        let blocks = CryptoTable::from_buf_read(block.as_bytes());
        let blocks = blocks.expect("Should be have read two blocks");
        assert_eq!(blocks.crypto_blocks.len(), 2);
    }

    #[test]
    fn parse_duplicate_name() {
        let block = r#"name         : deflate
driver       : deflate-generic
module       : kernel
priority     : 0
refcnt       : 2
selftest     : passed
internal     : no
type         : compression

name         : deflate
driver       : deflate-non-generic
module       : kernel
priority     : 0
refcnt       : 2
selftest     : passed
internal     : no
type         : compression
"#;
        let blocks = CryptoTable::from_buf_read(block.as_bytes());
        let blocks = blocks.expect("Should be have read two blocks");
        assert_eq!(blocks.crypto_blocks.len(), 1);
        let deflate_vec = blocks
            .crypto_blocks
            .get("deflate")
            .expect("Should have created a vec of deflates");
        assert_eq!(deflate_vec.len(), 2);
    }

    #[test]
    fn parse_unknown() {
        let block = r#"driver       : ccm_base(ctr(aes-aesni),cbcmac(aes-aesni))
module       : ccm
priority     : 300
refcnt       : 4
selftest     : passed
internal     : no
type         : unknown
key          : val
key2         : val2
"#;
        let mut iter = block.lines().map(|s| Ok(s.to_string())).peekable();
        let block = CryptoBlock::from_iter(&mut iter, "ccm(aes)");
        let block = block.expect("Should be have read one block");
        let mut compare = HashMap::new();
        compare.insert(String::from("key"), String::from("val"));
        compare.insert(String::from("key2"), String::from("val2"));
        compare.insert(String::from("name"), String::from("unknown"));
        assert_eq!(block.crypto_type, Type::Unknown(Unknown { fields: compare }));
    }

    #[test]
    fn parse_unknown_top() {
        let block = r#"name         : ccm(aes)
driver       : ccm_base(ctr(aes-aesni),cbcmac(aes-aesni))
module       : ccm
priority     : 300
refcnt       : 4
selftest     : passed
internal     : no
type         : unknown
key          : val
key2         : val2

name         : ctr(aes)
driver       : ctr(aes-aesni)
module       : kernel
priority     : 300
refcnt       : 4
selftest     : passed
internal     : no
type         : skcipher
async        : no
blocksize    : 1
min keysize  : 16
max keysize  : 32
ivsize       : 16
chunksize    : 16
walksize     : 16
"#;
        let blocks = CryptoTable::from_buf_read(block.as_bytes());
        let blocks = blocks.expect("Should be have read one block");
        assert_eq!(blocks.crypto_blocks.len(), 2);
    }
}