mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
rust: Upgrade zerocopy dependency to 0.8.X
This enables some const constructors, Dataful Enums, Dynamically Sized Types and much more. v0.8 introduces breaking changes including, but not limited to: - Rename AsBytes to IntoBytes - Fine-grain (derive) Traits that need to be implemented on top. - Rename FromZeroes to FromZeros for which this patch takes care of as well. Also a direct FromZeros derive is no longer necessary. As it is touched anyways, remove it where appropriate. See: https://github.com/google/zerocopy/discussions/1680 Signed-off-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
f7bba3a687
commit
8929d21948
+11
-11
@@ -8,7 +8,7 @@ use std::{
|
||||
};
|
||||
|
||||
use log::{debug, warn};
|
||||
use zerocopy::{AsBytes, BigEndian, FromBytes, FromZeroes, U32, U64};
|
||||
use zerocopy::{BigEndian, FromBytes, Immutable, IntoBytes, KnownLayout, U32, U64};
|
||||
|
||||
// (SE) boot request control block aka SE header
|
||||
use crate::{assert_size, request::MagicValue, static_assert, Error, Result, PAGESIZE};
|
||||
@@ -21,7 +21,7 @@ use crate::{assert_size, request::MagicValue, static_assert, Error, Result, PAGE
|
||||
/// Tweak List Digest (tld)
|
||||
/// SE-Header Tag (tag)
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, AsBytes, PartialEq, Eq, FromBytes, FromZeroes)]
|
||||
#[derive(Debug, Clone, Copy, IntoBytes, PartialEq, Eq, FromBytes, Immutable, KnownLayout)]
|
||||
pub struct BootHdrTags {
|
||||
pld: [u8; BootHdrHead::DIGEST_SIZE],
|
||||
ald: [u8; BootHdrHead::DIGEST_SIZE],
|
||||
@@ -40,8 +40,8 @@ impl TryFrom<Vec<u8>> for BootHdrTags {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
Self::ref_from(&value)
|
||||
.ok_or(Error::InvBootHdrSize(value.len()))
|
||||
Self::ref_from_bytes(&value)
|
||||
.map_err(|_| Error::InvBootHdrSize(value.len()))
|
||||
.copied()
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ impl TryFrom<Vec<u8>> for BootHdrTags {
|
||||
/// Struct representing the Secure Execution boot image metadata
|
||||
#[allow(unused)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Clone, FromBytes, FromZeroes, AsBytes, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, FromBytes, IntoBytes, PartialEq, Eq, Immutable, KnownLayout)]
|
||||
pub struct SeImgMetaData {
|
||||
/// Magic value
|
||||
magic: [u8; 8],
|
||||
@@ -106,7 +106,7 @@ impl SeImgMetaData {
|
||||
/// Gets the bytes of this value.
|
||||
#[inline(always)]
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
<Self as AsBytes>::as_bytes(self)
|
||||
<Self as IntoBytes>::as_bytes(self)
|
||||
}
|
||||
|
||||
/// Returns the version of this [`SeImgMetaData`].
|
||||
@@ -154,7 +154,7 @@ where
|
||||
// read in the header
|
||||
img.read_exact(&mut img_metadata_bytes)?;
|
||||
// Cannot fail because the buffer has the same size as SeImgMetaData.
|
||||
let img_metadata = SeImgMetaData::ref_from(&img_metadata_bytes).unwrap();
|
||||
let img_metadata = SeImgMetaData::ref_from_bytes(&img_metadata_bytes).unwrap();
|
||||
let img_metadata_version = img_metadata.version();
|
||||
if img_metadata_version != SeImgMetaData::V1 {
|
||||
warn!("Unknown Secure Execution boot image version {img_metadata_version}");
|
||||
@@ -226,8 +226,8 @@ impl BootHdrTags {
|
||||
}
|
||||
|
||||
let hdr_head = match BootHdrHead::read_from_prefix(hdr.as_mut_slice()) {
|
||||
Some(hdr) => hdr,
|
||||
None => {
|
||||
Ok((hdr, _)) => hdr,
|
||||
Err(_) => {
|
||||
debug!("Boot hdr is too small");
|
||||
return Err(Error::InvBootHdr);
|
||||
}
|
||||
@@ -260,7 +260,7 @@ impl BootHdrTags {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, FromBytes, FromZeroes)]
|
||||
#[derive(Debug, Clone, FromBytes)]
|
||||
struct BootHdrHead {
|
||||
magic: U64<BigEndian>,
|
||||
version: U32<BigEndian>,
|
||||
@@ -388,7 +388,7 @@ mod tests {
|
||||
0, 1, 96, 0,
|
||||
];
|
||||
assert_eq!(metadata.as_bytes(), &data);
|
||||
assert_eq!(SeImgMetaData::ref_from(&data), Some(&metadata));
|
||||
assert_eq!(SeImgMetaData::ref_from_bytes(&data), Ok(&metadata));
|
||||
|
||||
assert_eq!(metadata.version(), SeImgMetaData::V1);
|
||||
}
|
||||
|
||||
+6
-6
@@ -12,7 +12,7 @@ use openssl::{
|
||||
pkey::{PKey, PKeyRef, Private, Public},
|
||||
};
|
||||
use pv_core::request::{RequestMagic, RequestVersion};
|
||||
use zerocopy::{AsBytes, BigEndian, FromBytes, FromZeroes, U32};
|
||||
use zerocopy::{BigEndian, FromBytes, Immutable, IntoBytes, KnownLayout, U32};
|
||||
|
||||
use crate::{
|
||||
assert_size,
|
||||
@@ -358,7 +358,7 @@ ecdh_from!(Public);
|
||||
/// Representation of the shared parts of the request header.
|
||||
/// Used by [`ReqEncrCtx`]
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(Debug, Copy, Clone, IntoBytes, FromBytes, Immutable)]
|
||||
struct RequestHdr {
|
||||
magic: [u8; 8],
|
||||
rqvn: U32<BigEndian>,
|
||||
@@ -449,7 +449,7 @@ impl<'a> BinReqValues<'a> {
|
||||
/// Does minimal sanity test, just tests to prevent panics.
|
||||
/// `req` may be larger than the actual request.
|
||||
pub(crate) fn get(req: &'a [u8]) -> Result<Self> {
|
||||
let hdr = RequestHdr::read_from_prefix(req).ok_or(Error::BinRequestSmall)?;
|
||||
let (hdr, _) = RequestHdr::read_from_prefix(req).map_err(|_| Error::BinRequestSmall)?;
|
||||
let rql = hdr.rql.get() as usize;
|
||||
let sea = hdr.sea.get() as usize;
|
||||
|
||||
@@ -506,9 +506,9 @@ impl<'a> BinReqValues<'a> {
|
||||
/// [`FromBytes::ref_from_prefix`]
|
||||
pub(crate) fn req_dep_aad<T>(&self) -> Option<&T>
|
||||
where
|
||||
T: FromBytes + Sized,
|
||||
T: FromBytes + Sized + Immutable + KnownLayout,
|
||||
{
|
||||
T::ref_from_prefix(self.req_dep_aad)
|
||||
T::ref_from_prefix(self.req_dep_aad).map(|s| s.0).ok()
|
||||
}
|
||||
|
||||
/// Returns a reference to the tag of this [`BinReqValues`].
|
||||
@@ -615,7 +615,7 @@ mod tests {
|
||||
#[test]
|
||||
fn req_hdr2() {
|
||||
let mut hdr = RequestHdr::new(0x200, 0x1234, [0x11; 12], 15, 44, Some(TEST_MAGIC));
|
||||
let hdr_bin = hdr.as_bytes_mut();
|
||||
let hdr_bin = hdr.as_mut_bytes();
|
||||
let hdr_bin_exp = [
|
||||
0x12, 0x34, 0x56, 0x89, 0xab, 0xcd, 0xef, 0, // magic
|
||||
0, 0, 2, 0, // vers
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
use openssl::pkey::{PKey, Public};
|
||||
use std::mem::size_of;
|
||||
use zerocopy::{AsBytes, BigEndian, FromBytes, FromZeroes, U32};
|
||||
use zerocopy::{BigEndian, FromBytes, Immutable, IntoBytes, KnownLayout, U32};
|
||||
|
||||
use crate::{
|
||||
assert_size,
|
||||
@@ -276,7 +276,7 @@ impl From<AttestationVersion> for RequestVersion {
|
||||
|
||||
/// Authenticated additional Data of an [`AttestationRequest`]
|
||||
#[repr(C)]
|
||||
#[derive(Debug, AsBytes, FromZeroes, FromBytes, Clone, Copy)]
|
||||
#[derive(Debug, IntoBytes, FromBytes, Clone, Copy, Immutable, KnownLayout)]
|
||||
pub struct AttestationAuthenticated {
|
||||
flags: AttestationFlags,
|
||||
mai: U32<BigEndian>,
|
||||
@@ -310,7 +310,7 @@ impl AttestationAuthenticated {
|
||||
|
||||
/// Attestation flags
|
||||
#[repr(C)]
|
||||
#[derive(Default, Debug, AsBytes, FromZeroes, FromBytes, Clone, Copy)]
|
||||
#[derive(Default, Debug, IntoBytes, FromBytes, Clone, Copy, Immutable)]
|
||||
pub struct AttestationFlags(UvFlags);
|
||||
static_assert!(AttestationFlags::FLAG_TO_ADD_SIZE.len() < 64);
|
||||
|
||||
@@ -393,7 +393,7 @@ impl AttestationFlags {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, AsBytes)]
|
||||
#[derive(Debug, IntoBytes, Immutable)]
|
||||
struct ReqConfData {
|
||||
meas_key: [u8; 64],
|
||||
nonce: AttNonce,
|
||||
|
||||
@@ -12,7 +12,7 @@ use openssl::{
|
||||
pkey::{PKeyRef, Private},
|
||||
};
|
||||
use std::mem::size_of;
|
||||
use zerocopy::{AsBytes, BigEndian, U16, U32};
|
||||
use zerocopy::{BigEndian, IntoBytes, U16, U32};
|
||||
|
||||
#[cfg(doc)]
|
||||
use crate::attest::AttestationRequest;
|
||||
|
||||
@@ -18,11 +18,11 @@ use openssl::{
|
||||
pkey::{PKey, Private, Public},
|
||||
};
|
||||
use pv_core::{request::RequestVersion, secret::AddSecretMagic, uv::SecretId};
|
||||
use zerocopy::AsBytes;
|
||||
use zerocopy::{Immutable, IntoBytes};
|
||||
|
||||
/// Authenticated data w/o user data
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, AsBytes)]
|
||||
#[derive(Debug, Clone, Copy, IntoBytes, Immutable)]
|
||||
struct ReqAuthData {
|
||||
flags: UvFlags,
|
||||
boot_tags: BootHdrTags,
|
||||
|
||||
@@ -17,7 +17,6 @@ use crate::{
|
||||
},
|
||||
Error, Result,
|
||||
};
|
||||
use byteorder::BigEndian;
|
||||
use openssl::{
|
||||
hash::MessageDigest,
|
||||
nid::Nid,
|
||||
@@ -26,7 +25,8 @@ use openssl::{
|
||||
use pv_core::static_assert;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Display;
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes, U16, U32};
|
||||
use zerocopy::{BigEndian, KnownLayout};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, U16, U32};
|
||||
|
||||
const ASSOC_SECRET_SIZE: usize = 32;
|
||||
/// Maximum size of a plain-text secret payload (8190)
|
||||
@@ -380,7 +380,7 @@ impl SecretAuth {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, AsBytes, FromZeroes, FromBytes)]
|
||||
#[derive(Debug, IntoBytes, FromBytes, Immutable, KnownLayout)]
|
||||
pub(crate) struct ListableSecretHdr {
|
||||
res0: u16,
|
||||
kind: U16<BigEndian>,
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
use crate::{pem::Pem, uvsecret::guest_secret::MAX_SIZE_PLAIN_PAYLOAD, Result};
|
||||
|
||||
use byteorder::BigEndian;
|
||||
use log::warn;
|
||||
use pv_core::{
|
||||
request::Confidential,
|
||||
uv::{ListableSecretType, RetrievableSecret, RetrieveCmd},
|
||||
};
|
||||
use zerocopy::BigEndian;
|
||||
use zerocopy::{FromBytes, U16};
|
||||
|
||||
/// An IBM Protected Key
|
||||
@@ -76,6 +76,7 @@ impl From<RetrieveCmd> for RetrievedSecret {
|
||||
// minimum size
|
||||
let len = U16::<BigEndian>::read_from_prefix(key.value())
|
||||
.unwrap_or_default()
|
||||
.0
|
||||
.get() as usize;
|
||||
|
||||
// Test if the plain text secret has a size:
|
||||
|
||||
@@ -15,7 +15,7 @@ use crate::{
|
||||
};
|
||||
use openssl::hash::MessageDigest;
|
||||
use openssl::nid::Nid;
|
||||
use zerocopy::{AsBytes, BigEndian, FromBytes, FromZeroes, U16};
|
||||
use zerocopy::{BigEndian, FromBytes, IntoBytes, KnownLayout, U16};
|
||||
|
||||
/// User data.
|
||||
///
|
||||
@@ -59,7 +59,7 @@ pub(super) enum UserData {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(Debug, IntoBytes, FromBytes, KnownLayout)]
|
||||
struct EcUserData {
|
||||
data: [u8; 256],
|
||||
signature: [u8; EC_SIGN_MAX_SIZE],
|
||||
@@ -175,7 +175,7 @@ impl UserData {
|
||||
// insert signature
|
||||
if let UserDataType::SgnEcSECP521R1 = self.data_type() {
|
||||
// Panic: will not panic buffer is 512+ bytes long
|
||||
let buf_ec = EcUserData::mut_from_prefix(&mut buf[user_data_offset..]).unwrap();
|
||||
let (buf_ec, _) = EcUserData::mut_from_prefix(&mut buf[user_data_offset..]).unwrap();
|
||||
buf_ec.set_signature(&sgn);
|
||||
} else {
|
||||
// Panic: will not panic buffer is 512+ bytes long
|
||||
@@ -328,12 +328,15 @@ impl VerifiedUserData {
|
||||
|
||||
let (ret, sgn) = match kind {
|
||||
UserDataType::SgnEcSECP521R1 => {
|
||||
let EcUserData {
|
||||
data,
|
||||
signature,
|
||||
sgn_size,
|
||||
..
|
||||
} = EcUserData::mut_from_prefix(buf).unwrap();
|
||||
let (
|
||||
EcUserData {
|
||||
data,
|
||||
signature,
|
||||
sgn_size,
|
||||
..
|
||||
},
|
||||
_,
|
||||
) = EcUserData::mut_from_prefix(buf).unwrap();
|
||||
let data_len: usize = data.len();
|
||||
let data = data.to_vec();
|
||||
let mut signature = signature.to_vec();
|
||||
@@ -480,7 +483,7 @@ mod test {
|
||||
buf[..0x80].copy_from_slice(data);
|
||||
|
||||
user_data.sign(&mut buf, 0).unwrap();
|
||||
let buf_ec = EcUserData::mut_from(&mut buf).unwrap();
|
||||
let buf_ec = EcUserData::mut_from_bytes(&mut buf).unwrap();
|
||||
let EcUserData {
|
||||
data,
|
||||
signature,
|
||||
|
||||
Reference in New Issue
Block a user