mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
rust/pv*: Move u8_to_hexstring to pv_core
Move pvapconfig::helper::u8_to_hexstring to pv_core::utils::encode_hex. Discard pvapconfig::helper::hexstring_to_u8 in favor of pv_core::utils::decode_hex. Signed-off-by: Jakob Naucke <naucke@linux.ibm.com> Reviewed-by: Steffen Eiden <seiden@linux.ibm.com> Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
152f446d76
commit
66a10d5e3e
@@ -21,8 +21,9 @@ pub mod attest {
|
||||
pub mod misc {
|
||||
pub use crate::utils::pv_guest_bit_set;
|
||||
pub use crate::utils::{create_file, open_file, read_exact_file, read_file, write_file};
|
||||
pub use crate::utils::{decode_hex, parse_hex, to_u16, to_u32, try_parse_u128, try_parse_u64};
|
||||
pub use crate::utils::{decode_hex, encode_hex, parse_hex};
|
||||
pub use crate::utils::{read, write};
|
||||
pub use crate::utils::{to_u16, to_u32, try_parse_u128, try_parse_u64};
|
||||
pub use crate::utils::{Flags, Lsb0Flags64, Msb0Flags64};
|
||||
}
|
||||
|
||||
|
||||
@@ -355,6 +355,15 @@ usize_to_ui! {
|
||||
#[doc = r"u16"]
|
||||
=> u16, to_u16}
|
||||
|
||||
/// Converts the u8 slice into (lowercase) hexstring
|
||||
pub fn encode_hex<S: AsRef<[u8]>>(s: S) -> String {
|
||||
let slice = s.as_ref();
|
||||
let string = String::with_capacity(2 * slice.len());
|
||||
slice
|
||||
.iter()
|
||||
.fold(string, |acc, e| acc + &format!("{e:02x}"))
|
||||
}
|
||||
|
||||
/// Converts the hexstring into a byte vector.
|
||||
///
|
||||
/// # Errors
|
||||
@@ -504,6 +513,14 @@ mod tests {
|
||||
fn lsb_flags_unset_panic() {
|
||||
Lsb0Flags64::default().unset_bit(64)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_hex() {
|
||||
let arr = [0x12, 0x34, 0x56, 0xac, 0xbe, 0xf0];
|
||||
let exp = "123456acbef0";
|
||||
assert_eq!(super::encode_hex(arr), exp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_hex() {
|
||||
let s = "123456acbef0";
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
use openssl::sha::sha256;
|
||||
use pv_core::misc::encode_hex;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml::{self};
|
||||
@@ -48,7 +49,7 @@ impl ApConfigEntry {
|
||||
return Ok(());
|
||||
}
|
||||
let hash = sha256(self.name.as_bytes());
|
||||
let hashstr = crate::helper::u8_to_hexstring(&hash);
|
||||
let hashstr = encode_hex(hash);
|
||||
// if there is a secretid given, this must match to the hash
|
||||
if !self.secretid.is_empty() {
|
||||
if self.secretid != hashstr {
|
||||
@@ -377,15 +378,15 @@ mod tests {
|
||||
#[test]
|
||||
fn test_sha256() {
|
||||
assert!(
|
||||
crate::helper::u8_to_hexstring(&sha256("Hello".as_bytes()))
|
||||
encode_hex(sha256("Hello".as_bytes()))
|
||||
== "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969"
|
||||
);
|
||||
assert!(
|
||||
crate::helper::u8_to_hexstring(&sha256("SECRET1".as_bytes()))
|
||||
encode_hex(sha256("SECRET1".as_bytes()))
|
||||
== "03153249db7ce46b0330ffb1a760b59710531af08ec4d7f8424a6870fae49360"
|
||||
);
|
||||
assert!(
|
||||
crate::helper::u8_to_hexstring(&sha256("SECRET2".as_bytes()))
|
||||
encode_hex(sha256("SECRET2".as_bytes()))
|
||||
== "258499e710e0bd3bb878d6bac7e478b30f3f3e72566989f638c4143d14f6c0b6"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,48 +14,6 @@ use std::path::PathBuf;
|
||||
|
||||
pub const PATH_PVAPCONFIG_LOCK: &str = "/run/lock/pvapconfig.lock";
|
||||
|
||||
/// Convert u8 slice to (lowercase) hex string
|
||||
pub fn u8_to_hexstring(slice: &[u8]) -> String {
|
||||
let s = String::with_capacity(2 * slice.len());
|
||||
slice.iter().fold(s, |acc, e| acc + &format!("{e:02x}"))
|
||||
}
|
||||
|
||||
/// Convert hexstring to u8 vector
|
||||
/// The hexstring may contain whitespaces which are ignored.
|
||||
/// If there are other characters in there or if the number
|
||||
/// of hex characters is uneven panic() is called.
|
||||
/// # Panics
|
||||
/// Panics if the given string contains characters other than
|
||||
/// hex digits and whitespace. Panics if the number of hex digits
|
||||
/// is not even.
|
||||
#[cfg(test)] // currently only used in test code
|
||||
pub fn hexstring_to_u8(hex: &str) -> Vec<u8> {
|
||||
let mut s = String::new();
|
||||
for c in hex.chars() {
|
||||
if c.is_ascii_hexdigit() {
|
||||
s.push(c);
|
||||
} else if c.is_whitespace() {
|
||||
// ignore
|
||||
} else {
|
||||
panic!("Invalid character '{c}'");
|
||||
}
|
||||
}
|
||||
if s.len() % 2 == 1 {
|
||||
panic!("Uneven # of hex characters in '{s}'");
|
||||
}
|
||||
let mut hex_bytes = s.as_bytes().iter().map_while(|b| match b {
|
||||
b'0'..=b'9' => Some(b - b'0'),
|
||||
b'a'..=b'f' => Some(b - b'a' + 10),
|
||||
b'A'..=b'F' => Some(b - b'A' + 10),
|
||||
_ => None,
|
||||
});
|
||||
let mut bytes = Vec::with_capacity(s.len());
|
||||
while let (Some(h), Some(l)) = (hex_bytes.next(), hex_bytes.next()) {
|
||||
bytes.push(h << 4 | l)
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
/// Read sysfs file into string
|
||||
pub fn sysfs_read_string(fname: &str) -> Result<String, Box<dyn Error>> {
|
||||
let mut file = File::open(fname)?;
|
||||
@@ -203,19 +161,6 @@ mod tests {
|
||||
|
||||
// Only very simple tests
|
||||
|
||||
const TEST_BYTES: [u8; 8] = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
|
||||
const TEST_HEXSTR: &str = "0123456789abcdef";
|
||||
|
||||
#[test]
|
||||
fn test_u8_to_hexstring() {
|
||||
let str = u8_to_hexstring(&TEST_BYTES);
|
||||
assert!(str == TEST_HEXSTR);
|
||||
}
|
||||
#[test]
|
||||
fn test_hexstring_to_u8() {
|
||||
let bytes = hexstring_to_u8(TEST_HEXSTR);
|
||||
assert!(bytes.as_slice() == TEST_BYTES);
|
||||
}
|
||||
#[test]
|
||||
fn test_sysfs_read_string() {
|
||||
let r = sysfs_read_string("/proc/cpuinfo");
|
||||
|
||||
@@ -16,6 +16,7 @@ use ap::{Apqn, ApqnList};
|
||||
use cli::ARGS;
|
||||
use config::{ApConfigEntry, ApConfigList};
|
||||
use helper::{LockFile, PATH_PVAPCONFIG_LOCK};
|
||||
use pv_core::misc::encode_hex;
|
||||
use pv_core::uv::{ListableSecretType, SecretList};
|
||||
use std::process::ExitCode;
|
||||
use utils::print_version;
|
||||
@@ -266,7 +267,7 @@ fn do_ap_config(
|
||||
se.stype() == ListableSecretType::Association
|
||||
&& se.id().len() == uv::AP_ASSOC_SECRET_ID_SIZE
|
||||
&& se.index() == assoc_idx
|
||||
&& helper::u8_to_hexstring(se.id()) == apc.secretid
|
||||
&& encode_hex(se.id()) == apc.secretid
|
||||
});
|
||||
if r.is_none() {
|
||||
continue;
|
||||
@@ -353,7 +354,7 @@ fn do_ap_config(
|
||||
let se = match secrets.iter().find(|&se| {
|
||||
se.stype() == ListableSecretType::Association
|
||||
&& se.id().len() == uv::AP_ASSOC_SECRET_ID_SIZE
|
||||
&& helper::u8_to_hexstring(se.id()) == apc.secretid
|
||||
&& encode_hex(se.id()) == apc.secretid
|
||||
}) {
|
||||
None => {
|
||||
eprintln!("Warning: Secret id '{}' from config entry {} not found in UV secrets list.",
|
||||
@@ -450,8 +451,7 @@ fn config_and_apqn_match(apc: &ApConfigEntry, apqn: &Apqn) -> bool {
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
use helper::hexstring_to_u8;
|
||||
use pv_core::uv::SecretEntry;
|
||||
use pv_core::{misc::decode_hex, uv::SecretEntry};
|
||||
|
||||
// This is more or less only a test for the do_ap_config() function
|
||||
// However, this is THE main functionality of the whole application.
|
||||
@@ -522,7 +522,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn make_assoc_secretentry(idx: u16, hexidstr: &str) -> SecretEntry {
|
||||
let id = hexstring_to_u8(hexidstr);
|
||||
let id = decode_hex(hexidstr).unwrap();
|
||||
let idlen: u32 = id.len().try_into().unwrap();
|
||||
let idarray: [u8; 32] = id.try_into().unwrap();
|
||||
SecretEntry::new(idx, ListableSecretType::Association, idarray.into(), idlen)
|
||||
|
||||
Reference in New Issue
Block a user