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
use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
/// A cryptographic implementation registered with the kernel.
#[derive(Debug)]
pub struct Crypto {
/// Algorithm name (e.g. `gcm(aes)` or `sha256`).
pub name: Box<str>,
/// Driver implementing the algorithm.
pub driver: Box<str>,
/// Kernel module providing the driver.
pub module: Box<str>,
/// Algorithm priority.
pub priority: i64,
/// Number of active references.
pub ref_count: i64,
/// Self-test result (usually `passed`).
pub self_test: Box<str>,
/// Whether the implementation is internal to the kernel.
pub internal: bool,
/// Whether FIPS mode is enabled for this implementation.
pub fips_enabled: bool,
/// Kernel crypto API type (e.g. `aead`, `skcipher`, or `shash`).
pub crypto_type: Box<str>,
}
/// Reads `/proc/crypto` and returns all registered cryptographic
/// implementations.
pub fn crypto() -> Result<Vec<Crypto>> {
let path = Path::new("/proc/crypto");
let bytes = parse::read_file(path)?;
let mut out = Vec::new();
let mut name = None;
let mut driver = None;
let mut module = None;
let mut priority = None;
let mut ref_count = None;
let mut self_test = None;
let mut internal = None;
let mut fips_enabled = false;
let mut crypto_type = None;
let mut block_line = 1;
macro_rules! finish {
($line:expr) => {{
let missing = |msg| Error::Parse {
path: path.to_path_buf(),
line: $line,
msg,
};
out.push(Crypto {
name: name.take().ok_or_else(|| missing("missing name"))?,
driver: driver.take().ok_or_else(|| missing("missing driver"))?,
module: module.take().ok_or_else(|| missing("missing module"))?,
priority: priority.take().ok_or_else(|| missing("missing priority"))?,
ref_count: ref_count.take().ok_or_else(|| missing("missing refcnt"))?,
self_test: self_test
.take()
.ok_or_else(|| missing("missing selftest"))?,
internal: internal.take().ok_or_else(|| missing("missing internal"))?,
fips_enabled,
crypto_type: crypto_type.take().ok_or_else(|| missing("missing type"))?,
});
}};
}
for (line_num, raw_line) in bytes.split(|&b| b == b'\n').enumerate() {
let line = parse::trim_end(raw_line);
if line.is_empty() {
if name.is_some() {
finish!(block_line);
fips_enabled = false;
}
block_line = line_num + 2;
continue;
}
let (key, value) = parse::parse_key_value_line(line).ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected '<key> : <value>'",
})?;
let value = parse::trim_start(value);
if value.is_empty() {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "empty crypto field",
});
}
match key {
b"name" => {
name = Some(
std::str::from_utf8(value)
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid name",
})?
.into(),
)
}
b"driver" => {
driver = Some(
std::str::from_utf8(value)
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid driver",
})?
.into(),
)
}
b"module" => {
module = Some(
std::str::from_utf8(value)
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid module",
})?
.into(),
)
}
b"selftest" => {
self_test = Some(
std::str::from_utf8(value)
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid selftest",
})?
.into(),
)
}
b"type" => {
crypto_type = Some(
std::str::from_utf8(value)
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid type",
})?
.into(),
)
}
b"priority" | b"refcnt" => {
let number = std::str::from_utf8(value)
.ok()
.and_then(|v| v.parse::<i64>().ok())
.ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: if key == b"priority" {
"invalid priority"
} else {
"invalid reference count"
},
})?;
if key == b"priority" {
priority = Some(number);
} else {
ref_count = Some(number);
}
}
b"internal" | b"fips" => {
let value = match value {
b"yes" => true,
b"no" => false,
_ => {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid boolean value",
});
}
};
if key == b"internal" {
internal = Some(value);
} else {
fips_enabled = value;
}
}
_ => {}
}
}
if name.is_some() {
finish!(block_line);
}
Ok(out)
}