tape: encryption scsi command cleanup

This commit is contained in:
Dietmar Maurer 2021-01-16 18:24:04 +01:00
parent 90950c9c20
commit b22b6c2299

View File

@ -1,4 +1,5 @@
use std::os::unix::prelude::AsRawFd; use std::os::unix::prelude::AsRawFd;
use std::io::Write;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use endian_trait::Endian; use endian_trait::Endian;
@ -75,7 +76,7 @@ struct SspSetDataEncryptionPage {
key_format: u8, key_format: u8,
reserved: [u8; 8], reserved: [u8; 8],
key_len: u16, key_len: u16,
key: [u8; 32], /* key follows */
} }
fn sg_spout_set_encryption<F: AsRawFd>( fn sg_spout_set_encryption<F: AsRawFd>(
@ -86,7 +87,11 @@ fn sg_spout_set_encryption<F: AsRawFd>(
let mut sg_raw = SgRaw::new(file, 0)?; let mut sg_raw = SgRaw::new(file, 0)?;
let outbuf_len = std::mem::size_of::<SspSetDataEncryptionPage>(); let mut outbuf_len = std::mem::size_of::<SspSetDataEncryptionPage>();
if let Some(ref key) = key {
outbuf_len += key.len();
}
let mut outbuf = alloc_page_aligned_buffer(outbuf_len)?; let mut outbuf = alloc_page_aligned_buffer(outbuf_len)?;
let chok: u8 = 0; let chok: u8 = 0;
@ -100,16 +105,16 @@ fn sg_spout_set_encryption<F: AsRawFd>(
algorythm_index, algorythm_index,
key_format: 0, key_format: 0,
reserved: [0u8; 8], reserved: [0u8; 8],
key_len: 32, key_len: if let Some(ref key) = key { key.len() as u16 } else { 0 },
key: match key {
Some(key) => key,
None => [0u8; 32],
}
}; };
let mut writer = &mut outbuf[..]; let mut writer = &mut outbuf[..];
unsafe { writer.write_be_value(page)? }; unsafe { writer.write_be_value(page)? };
if let Some(ref key) = key {
writer.write_all(key)?;
}
let mut cmd = Vec::new(); let mut cmd = Vec::new();
cmd.push(0xB5); // SECURITY PROTOCOL IN (SPOUT) cmd.push(0xB5); // SECURITY PROTOCOL IN (SPOUT)
cmd.push(0x20); // Tape Data Encryption Page cmd.push(0x20); // Tape Data Encryption Page
@ -181,7 +186,7 @@ struct DataEncryptionStatus {
mode: DataEncryptionMode, mode: DataEncryptionMode,
} }
#[derive(Debug, Endian)] #[derive(Endian)]
#[repr(C, packed)] #[repr(C, packed)]
struct SspDataEncryptionCapabilityPage { struct SspDataEncryptionCapabilityPage {
page_code: u16, page_code: u16,
@ -190,7 +195,7 @@ struct SspDataEncryptionCapabilityPage {
reserverd: [u8; 15], reserverd: [u8; 15],
} }
#[derive(Debug, Endian)] #[derive(Endian)]
#[repr(C, packed)] #[repr(C, packed)]
struct SspDataEncryptionAlgorithmDescriptor { struct SspDataEncryptionAlgorithmDescriptor {
algorythm_index: u8, algorythm_index: u8,
@ -254,7 +259,7 @@ fn decode_spin_data_encryption_caps(data: &[u8]) -> Result<u8, Error> {
} }
#[derive(Debug, Endian)] #[derive(Endian)]
#[repr(C, packed)] #[repr(C, packed)]
struct SspDataEncryptionStatusPage { struct SspDataEncryptionStatusPage {
page_code: u16, page_code: u16,