Skip to content

Commit 54714d5

Browse files
committed
Make PBKDF hash field optional
1 parent bbf478b commit 54714d5

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

src/settings.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5-
use std::{ffi::CString, marker::PhantomData, os::raw::c_int};
5+
use std::{ffi::CString, marker::PhantomData, os::raw::c_int, ptr};
66

77
use libcryptsetup_rs_sys::crypt_pbkdf_type;
88

@@ -20,7 +20,7 @@ pub struct CryptPbkdfType {
2020
#[allow(missing_docs)]
2121
pub type_: CryptKdf,
2222
#[allow(missing_docs)]
23-
pub hash: String,
23+
pub hash: Option<String>,
2424
#[allow(missing_docs)]
2525
pub time_ms: u32,
2626
#[allow(missing_docs)]
@@ -36,28 +36,23 @@ pub struct CryptPbkdfType {
3636
impl TryFrom<libcryptsetup_rs_sys::crypt_pbkdf_type> for CryptPbkdfType {
3737
type Error = LibcryptErr;
3838

39-
fn try_from(
40-
type_: libcryptsetup_rs_sys::crypt_pbkdf_type,
41-
) -> Result<CryptPbkdfType, LibcryptErr> {
42-
Ok(CryptPbkdfType {
43-
type_: CryptKdf::from_ptr(type_.type_)?,
44-
hash: String::from(from_str_ptr!(type_.hash)?),
45-
time_ms: type_.time_ms,
46-
iterations: type_.iterations,
47-
max_memory_kb: type_.max_memory_kb,
48-
parallel_threads: type_.parallel_threads,
49-
flags: CryptPbkdf::from_bits(type_.flags).ok_or(LibcryptErr::InvalidConversion)?,
50-
})
39+
fn try_from(v: libcryptsetup_rs_sys::crypt_pbkdf_type) -> Result<Self, Self::Error> {
40+
CryptPbkdfType::try_from(&v)
5141
}
5242
}
5343

5444
impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_pbkdf_type> for CryptPbkdfType {
5545
type Error = LibcryptErr;
5646

5747
fn try_from(v: &'a libcryptsetup_rs_sys::crypt_pbkdf_type) -> Result<Self, Self::Error> {
48+
let hash = if v.hash.is_null() {
49+
None
50+
} else {
51+
Some(from_str_ptr!(v.hash)?.to_string())
52+
};
5853
Ok(CryptPbkdfType {
5954
type_: CryptKdf::from_ptr(v.type_)?,
60-
hash: from_str_ptr!(v.hash)?.to_string(),
55+
hash,
6156
time_ms: v.time_ms,
6257
iterations: v.iterations,
6358
max_memory_kb: v.max_memory_kb,
@@ -73,18 +68,25 @@ pub struct CryptPbkdfTypeRef<'a> {
7368
/// Field containing a `crypt_pbkdf_type` that contains pointers valid for the supplied struct lifetime
7469
pub inner: crypt_pbkdf_type,
7570
#[allow(dead_code)]
76-
hash_cstring: CString,
71+
hash_cstring: Option<CString>,
7772
phantomdata: PhantomData<&'a ()>,
7873
}
7974

8075
impl<'a> TryInto<CryptPbkdfTypeRef<'a>> for &'a CryptPbkdfType {
8176
type Error = LibcryptErr;
8277

8378
fn try_into(self) -> Result<CryptPbkdfTypeRef<'a>, Self::Error> {
84-
let hash_cstring = CString::new(self.hash.as_bytes()).map_err(LibcryptErr::NullError)?;
79+
let hash_cstring = self
80+
.hash
81+
.as_ref()
82+
.map(|h| CString::new(h.as_bytes()).map_err(LibcryptErr::NullError))
83+
.transpose()?;
8584
let inner = libcryptsetup_rs_sys::crypt_pbkdf_type {
8685
type_: self.type_.as_ptr(),
87-
hash: hash_cstring.as_ptr(),
86+
hash: hash_cstring
87+
.as_ref()
88+
.map(|c| c.as_ptr())
89+
.unwrap_or(ptr::null()),
8890
time_ms: self.time_ms,
8991
iterations: self.iterations,
9092
max_memory_kb: self.max_memory_kb,

0 commit comments

Comments
 (0)