Skip to content

Commit

Permalink
add /proc/crypto
Browse files Browse the repository at this point in the history
Signed-off-by: Navid Yaghoobi <[email protected]>
  • Loading branch information
navidys committed Dec 5, 2024
1 parent 55bc819 commit 58f11b0
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ jobs:
run: cargo test --verbose
env:
CARGO_INCREMENTAL: '0'
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
RUSTFLAGS: '-Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
RUSTDOCFLAGS: '-Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
- name: rust-grcov
id: coverage
# You may pin to the exact commit or the version.
Expand Down
2 changes: 2 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Supported Features

*`/proc/cmdline`

*`/proc/crypto`

*`/proc/cpuinfo`

*`/proc/loadavg`
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ PRE_COMMIT = $(shell command -v bin/venv/bin/pre-commit ~/.local/bin/pre-commit
# Testing and validation
#=================================================

.PHONY: validate-all
validate-all: pre-commit codespell validate test ## Validate cargo, pre-commit, codespell and test

.PHONY: validate
validate: ## Validate code
validate: ## Validate cargo fmt and clippy
$(CARGO) fmt --all -- --check
$(CARGO) clippy -p procsys@$(CRATE_VERSION) -- -D warnings

Expand Down
13 changes: 13 additions & 0 deletions examples/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use procsys::crypto;

fn main() {
let crypto_info = crypto::collect().expect("crypto information");

match serde_json::to_string_pretty(&crypto_info) {
Ok(output) => println!("{}", output),
Err(err) => {
log::error!("{}", err);
std::process::exit(1);
}
}
}
251 changes: 251 additions & 0 deletions src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
use serde::Serialize;

use crate::{error::CollectResult, utils};

/// Crypto holds info parsed from /proc/crypto.
#[derive(Debug, Serialize, Clone, Default)]
pub struct Crypto {
#[serde(skip_serializing_if = "Option::is_none")]
pub alignmask: Option<u64>,

pub cryptoasync: bool,

#[serde(skip_serializing_if = "Option::is_none")]
pub blocksize: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub chunksize: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub ctzsize: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub digestsize: Option<u64>,

pub driver: String,
pub geniv: String,
pub internal: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub ivsize: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub max_authsize: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub max_keysize: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub min_keysize: Option<u64>,

pub module: String,

pub name: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<i64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub refcnt: Option<i64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub seedsize: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub statesize: Option<u64>,

pub selftest: String,

pub cryptotype: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub walksize: Option<u64>,
}

impl Crypto {
fn new() -> Self {
Default::default()
}
}

/// collects crypto information
/// # Example
/// ```
/// use procsys::crypto;
///
/// let crypto_info = crypto::collect().expect("crypto information");
/// let json_output = serde_json::to_string_pretty(&crypto_info).unwrap();
/// println!("{}", json_output);
///
/// ```
pub fn collect() -> CollectResult<Vec<Crypto>> {
collect_from("/proc/crypto")
}

fn collect_from(filename: &str) -> CollectResult<Vec<Crypto>> {
let mut crypto_info: Vec<Crypto> = Vec::new();
let mut info_index = 0;

for line in utils::read_file_lines(filename)? {
if line.trim().is_empty() {
continue;
}

let item_fields: Vec<&str> = line.trim().split(':').filter(|s| !s.is_empty()).collect();
if item_fields.len() < 2 {
continue;
}

let metric = item_fields[0].trim();
let metric_value = item_fields[1].trim();

match metric {
"alignmask" => {}
"async" => {
let mut casync = false;
if metric_value == "yes" {
casync = true;
}

crypto_info[info_index].cryptoasync = casync;
}
"blocksize" => {
crypto_info[info_index].blocksize = Some(utils::convert_str_to_u64(metric_value)?);
}
"chunksize" => {
crypto_info[info_index].chunksize = Some(utils::convert_str_to_u64(metric_value)?);
}
"ctxsize" => {}
"digestsize" => {
crypto_info[info_index].digestsize = Some(utils::convert_str_to_u64(metric_value)?);
}
"driver" => {
crypto_info[info_index].driver = metric_value.to_string();
}
"geniv" => {
crypto_info[info_index].geniv = metric_value.to_string();
}
"internal" => {
crypto_info[info_index].internal = metric_value.to_string();
}
"ivsize" => {
crypto_info[info_index].ivsize = Some(utils::convert_str_to_u64(metric_value)?);
}
"maxauthsize" => {
crypto_info[info_index].max_authsize =
Some(utils::convert_str_to_u64(metric_value)?);
}
"max keysize" => {
crypto_info[info_index].max_keysize =
Some(utils::convert_str_to_u64(metric_value)?);
}
"min keysize" => {
crypto_info[info_index].min_keysize =
Some(utils::convert_str_to_u64(metric_value)?);
}
"module" => {
crypto_info[info_index].module = metric_value.to_string();
}
"name" => {
let new_crypto = Crypto::new();
if !crypto_info.is_empty() {
info_index += 1;
}
crypto_info.push(new_crypto);
crypto_info[info_index].name = metric_value.to_string();
}
"priority" => {
crypto_info[info_index].priority = Some(utils::convert_str_to_i64(metric_value)?);
}
"refcnt" => {
crypto_info[info_index].refcnt = Some(utils::convert_str_to_i64(metric_value)?);
}
"seedsize" => {
crypto_info[info_index].seedsize = Some(utils::convert_str_to_u64(metric_value)?);
}
"statesize" => {
crypto_info[info_index].statesize = Some(utils::convert_str_to_u64(metric_value)?);
}
"selftest" => {
crypto_info[info_index].selftest = metric_value.to_string();
}
"type" => {
crypto_info[info_index].cryptotype = metric_value.to_string();
}
"walksize" => {
crypto_info[info_index].walksize = Some(utils::convert_str_to_u64(metric_value)?);
}
_ => {}
}
}

Ok(crypto_info)
}

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

#[test]
fn crypto_information() {
let crypto_info =
collect_from("test_data/fixtures/proc/crypto").expect("collecting crypto information");

assert_eq!(crypto_info.len(), 2);

for cryptinfo in crypto_info {
match cryptinfo.name.as_ref() {
"ccm(aes)" => {
assert_eq!(cryptinfo.alignmask, None);
assert_eq!(cryptinfo.cryptoasync, false);
assert_eq!(cryptinfo.blocksize, Some(1));
assert_eq!(cryptinfo.chunksize, None);
assert_eq!(cryptinfo.ctzsize, None);
assert_eq!(cryptinfo.digestsize, None);
assert_eq!(
cryptinfo.driver,
"ccm_base(ctr(aes-aesni),cbcmac(aes-aesni))",
);
assert_eq!(cryptinfo.geniv, "<none>");
assert_eq!(cryptinfo.internal, "no");
assert_eq!(cryptinfo.ivsize, Some(16));
assert_eq!(cryptinfo.max_authsize, Some(16));
assert_eq!(cryptinfo.max_keysize, None);
assert_eq!(cryptinfo.min_keysize, None);
assert_eq!(cryptinfo.module, "kernel");
assert_eq!(cryptinfo.priority, Some(300));
assert_eq!(cryptinfo.refcnt, Some(3));
assert_eq!(cryptinfo.seedsize, None);
assert_eq!(cryptinfo.statesize, None);
assert_eq!(cryptinfo.selftest, "passed");
assert_eq!(cryptinfo.cryptotype, "aead");
assert_eq!(cryptinfo.walksize, None);
}
"ctr(aes)" => {
assert_eq!(cryptinfo.alignmask, None);
assert_eq!(cryptinfo.cryptoasync, false);
assert_eq!(cryptinfo.blocksize, Some(1));
assert_eq!(cryptinfo.chunksize, Some(16));
assert_eq!(cryptinfo.ctzsize, None);
assert_eq!(cryptinfo.digestsize, None);
assert_eq!(cryptinfo.driver, "ctr(aes-aesni)");
assert_eq!(cryptinfo.geniv.is_empty(), true);
assert_eq!(cryptinfo.internal, "no");
assert_eq!(cryptinfo.ivsize, Some(16));
assert_eq!(cryptinfo.max_authsize, None);
assert_eq!(cryptinfo.max_keysize, Some(32));
assert_eq!(cryptinfo.min_keysize, Some(16));
assert_eq!(cryptinfo.module, "kernel");
assert_eq!(cryptinfo.priority, Some(300));
assert_eq!(cryptinfo.refcnt, Some(3));
assert_eq!(cryptinfo.seedsize, None);
assert_eq!(cryptinfo.selftest, "passed");
assert_eq!(cryptinfo.cryptotype, "skcipher");
assert_eq!(cryptinfo.statesize, Some(0));
assert_eq!(cryptinfo.walksize, Some(16));
}
_ => panic!("invalid crypto name: {}", cryptinfo.name),
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod buddyinfo;
pub mod cmdline;
pub mod cpuinfo;
pub mod crypto;
pub mod error;
pub mod kernel_random;
pub mod loadavg;
Expand Down
34 changes: 34 additions & 0 deletions test_data/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,40 @@ address sizes : 39 bits physical, 48 bits virtual
power management:
Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/proc/crypto
Lines: 30
name : ccm(aes)
driver : ccm_base(ctr(aes-aesni),cbcmac(aes-aesni))
module : kernel
priority : 300
refcnt : 3
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 : 3
selftest : passed
internal : no
type : skcipher
async : no
blocksize : 1
min keysize : 16
max keysize : 32
ivsize : 16
chunksize : 16
walksize : 16
statesize : 0
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/proc/loadavg
Lines: 1
0.02 0.04 0.05 1/497 11947
Expand Down

0 comments on commit 58f11b0

Please sign in to comment.