Skip to content

Switch to 2024 edition #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2983,7 +2983,7 @@ extern "C" fn fn_get_info(info: CK_INFO_PTR) -> CK_RV {
///
/// Version 3.1 Specification: [https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203258](https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203258)

#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn C_GetFunctionList(fnlist: CK_FUNCTION_LIST_PTR_PTR) -> CK_RV {
unsafe {
*fnlist = &FNLIST_240 as *const _ as *mut _;
Expand Down Expand Up @@ -3821,7 +3821,7 @@ static INTERFACE_SET: Lazy<Vec<InterfaceData>> = Lazy::new(|| {
///
/// Version 3.1 Specification: [https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203259](https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203259)

#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn C_GetInterfaceList(
interfaces_list: CK_INTERFACE_PTR,
count: CK_ULONG_PTR,
Expand Down Expand Up @@ -3864,7 +3864,7 @@ pub extern "C" fn C_GetInterfaceList(
///
/// Version 3.1 Specification: [https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203260](https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203260)

#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn C_GetInterface(
interface_name: CK_UTF8CHAR_PTR,
version: CK_VERSION_PTR,
Expand Down
14 changes: 6 additions & 8 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,16 +1441,14 @@ pub fn default_key_attributes(
key.set_attr(Attribute::from_bool(CKA_LOCAL, true))?;
key.set_attr(Attribute::from_ulong(CKA_KEY_GEN_MECHANISM, mech))?;

let extractable = if let Ok(b) = key.get_attr_as_bool(CKA_EXTRACTABLE) {
b
} else {
true
let extractable = match key.get_attr_as_bool(CKA_EXTRACTABLE) {
Ok(b) => b,
_ => true,
};
key.set_attr(Attribute::from_bool(CKA_NEVER_EXTRACTABLE, !extractable))?;
let sensitive = if let Ok(b) = key.get_attr_as_bool(CKA_SENSITIVE) {
b
} else {
false
let sensitive = match key.get_attr_as_bool(CKA_SENSITIVE) {
Ok(b) => b,
_ => false,
};
key.set_attr(Attribute::from_bool(CKA_ALWAYS_SENSITIVE, sensitive))?;

Expand Down
35 changes: 18 additions & 17 deletions src/ossl/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ impl AesCipher {
}

pub fn get_cipher(&self) -> Result<&EvpCipher> {
if let Some(ref ec) = self.cipher {
Ok(ec)
} else {
return Err(CKR_MECHANISM_INVALID)?;
match self.cipher {
Some(ref ec) => Ok(ec),
_ => {
return Err(CKR_MECHANISM_INVALID)?;
}
}
}
}
Expand Down Expand Up @@ -165,7 +166,7 @@ fn new_mechanism(flags: CK_FLAGS) -> Box<dyn Mechanism> {
struct AesIvData {
buf: Vec<u8>,
fixedbits: usize,
gen: CK_GENERATOR_FUNCTION,
generator: CK_GENERATOR_FUNCTION,
counter: u64,
maxcount: u64,
}
Expand All @@ -175,7 +176,7 @@ impl AesIvData {
Ok(AesIvData {
buf: Vec::new(),
fixedbits: 0,
gen: CKG_NO_GENERATE,
generator: CKG_NO_GENERATE,
counter: 0,
maxcount: 0,
})
Expand All @@ -185,7 +186,7 @@ impl AesIvData {
Ok(AesIvData {
buf: iv,
fixedbits: 0,
gen: CKG_NO_GENERATE,
generator: CKG_NO_GENERATE,
counter: 0,
maxcount: 0,
})
Expand Down Expand Up @@ -635,7 +636,7 @@ impl AesOperation {
let mask = u8::try_from(genbits % 8)?;
let genbytes = (genbits + 7) / 8;

match self.params.iv.gen {
match self.params.iv.generator {
CKG_GENERATE | CKG_GENERATE_COUNTER => {
let cntbuf = self.params.iv.counter.to_be_bytes();
self.params.iv.buf[genidx] &= !mask;
Expand Down Expand Up @@ -686,7 +687,7 @@ impl AesOperation {
/// It may generate a new IV or use what is provided (eg in the
/// decryption case)
fn prep_iv(&mut self) -> Result<()> {
if self.params.iv.gen != CKG_NO_GENERATE {
if self.params.iv.generator != CKG_NO_GENERATE {
self.generate_iv()?;
}

Expand Down Expand Up @@ -1017,15 +1018,15 @@ impl AesOperation {
self.params.iv = AesIvData {
buf: bytes_to_vec!(params.pNonce, noncelen),
fixedbits: noncefixedbits,
gen: params.nonceGenerator,
generator: params.nonceGenerator,
counter: 0,
maxcount: 0,
};
} else {
self.params.iv = AesIvData {
buf: bytes_to_vec!(params.pNonce, noncelen),
fixedbits: 0,
gen: CKG_NO_GENERATE,
generator: CKG_NO_GENERATE,
counter: 0,
maxcount: 0,
};
Expand Down Expand Up @@ -1092,15 +1093,15 @@ impl AesOperation {
self.params.iv = AesIvData {
buf: bytes_to_vec!(params.pIv, ivlen),
fixedbits: ivfixedbits,
gen: params.ivGenerator,
generator: params.ivGenerator,
counter: 0,
maxcount: 0,
};
} else {
self.params.iv = AesIvData {
buf: bytes_to_vec!(params.pIv, ivlen),
fixedbits: 0,
gen: CKG_NO_GENERATE,
generator: CKG_NO_GENERATE,
counter: 0,
maxcount: 0,
};
Expand Down Expand Up @@ -1149,7 +1150,7 @@ impl AesOperation {
if self.params.iv.fixedbits != noncefixedbits {
return Err(self.op_err(CKR_ARGUMENTS_BAD));
}
if self.params.iv.gen != params.nonceGenerator {
if self.params.iv.generator != params.nonceGenerator {
return Err(self.op_err(CKR_ARGUMENTS_BAD));
}
}
Expand Down Expand Up @@ -1191,7 +1192,7 @@ impl AesOperation {
if self.params.iv.fixedbits != ivfixedbits {
return Err(self.op_err(CKR_ARGUMENTS_BAD));
}
if self.params.iv.gen != params.ivGenerator {
if self.params.iv.generator != params.ivGenerator {
return Err(self.op_err(CKR_ARGUMENTS_BAD));
}
}
Expand Down Expand Up @@ -1261,7 +1262,7 @@ impl AesOperation {

self.cipher_initialize(true)?;

if self.params.iv.gen != CKG_NO_GENERATE {
if self.params.iv.generator != CKG_NO_GENERATE {
let iv = bytes_to_slice!(mut iv_ptr, self.params.iv.buf.len(), u8);
iv.copy_from_slice(&self.params.iv.buf);
}
Expand Down Expand Up @@ -1349,7 +1350,7 @@ impl AesOperation {
}

/* The IV must be generated in FIPS mode */
self.fips_approved = match self.params.iv.gen {
self.fips_approved = match self.params.iv.generator {
CKG_NO_GENERATE => match self.op {
CKF_MESSAGE_ENCRYPT => Some(false),
CKF_MESSAGE_DECRYPT => Some(true),
Expand Down
65 changes: 35 additions & 30 deletions src/ossl/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,40 @@ pub struct HKDFOperation {

impl HKDFOperation {
fn verify_key(&self, key: &Object, matchlen: usize) -> Result<()> {
if let Ok(class) = key.get_attr_as_ulong(CKA_CLASS) {
match class {
CKO_SECRET_KEY => {
if let Ok(kt) = key.get_attr_as_ulong(CKA_KEY_TYPE) {
match kt {
CKK_GENERIC_SECRET | CKK_HKDF => key
.check_key_ops(
CKO_SECRET_KEY,
CK_UNAVAILABLE_INFORMATION,
CKA_DERIVE,
)?,
_ => return Err(CKR_KEY_TYPE_INCONSISTENT)?,
match key.get_attr_as_ulong(CKA_CLASS) {
Ok(class) => {
match class {
CKO_SECRET_KEY => {
match key.get_attr_as_ulong(CKA_KEY_TYPE) {
Ok(kt) => match kt {
CKK_GENERIC_SECRET | CKK_HKDF => key
.check_key_ops(
CKO_SECRET_KEY,
CK_UNAVAILABLE_INFORMATION,
CKA_DERIVE,
)?,
_ => return Err(CKR_KEY_TYPE_INCONSISTENT)?,
},
_ => {
return Err(CKR_KEY_TYPE_INCONSISTENT)?;
}
}
} else {
return Err(CKR_KEY_TYPE_INCONSISTENT)?;
}
}
CKO_DATA => {
/* HKDF also allow a DATA object as input key ... */
if !self.extract
|| self.salt_type == CKF_HKDF_SALT_NULL
|| self.salt.len() == 0
{
return Err(CKR_MECHANISM_PARAM_INVALID)?;
CKO_DATA => {
/* HKDF also allow a DATA object as input key ... */
if !self.extract
|| self.salt_type == CKF_HKDF_SALT_NULL
|| self.salt.len() == 0
{
return Err(CKR_MECHANISM_PARAM_INVALID)?;
}
}
_ => return Err(CKR_KEY_HANDLE_INVALID)?,
}
_ => return Err(CKR_KEY_HANDLE_INVALID)?,
}
} else {
return Err(CKR_KEY_HANDLE_INVALID)?;
_ => {
return Err(CKR_KEY_HANDLE_INVALID)?;
}
}

if matchlen > 0 {
Expand Down Expand Up @@ -180,11 +184,12 @@ impl MechOperation for HKDFOperation {
return Err(CKR_GENERAL_ERROR)?;
}
self.verify_key(objs[0], 0)?;
if let Ok(salt) = objs[0].get_attr_as_bytes(CKA_VALUE) {
self.salt.clone_from(salt);
Ok(())
} else {
Err(CKR_KEY_HANDLE_INVALID)?
match objs[0].get_attr_as_bytes(CKA_VALUE) {
Ok(salt) => {
self.salt.clone_from(salt);
Ok(())
}
_ => Err(CKR_KEY_HANDLE_INVALID)?,
}
}
}
Expand Down
42 changes: 22 additions & 20 deletions src/storage/aci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ fn decrypt_key(
};

let kkbps1 = match &pdata.algorithm.params {
KAlgorithmParameters::Kkbps1(ref params) => params,
KAlgorithmParameters::Kkbps1(params) => params,
_ => return Err(CKR_MECHANISM_INVALID)?,
};

let key = match &kkbps1.key_derivation_func.params {
KAlgorithmParameters::Pbkdf2(ref params) => {
KAlgorithmParameters::Pbkdf2(params) => {
if let Some(keylen) = params.key_length {
if keylen != AES_KEYLEN as u64 {
return Err(CKR_MECHANISM_PARAM_INVALID)?;
Expand All @@ -281,9 +281,9 @@ fn decrypt_key(
_ => return Err(CKR_MECHANISM_INVALID)?,
};
let params = match &kkbps1.encryption_scheme.params {
KAlgorithmParameters::Aes128Gcm(ref gcm) => gcm,
KAlgorithmParameters::Aes192Gcm(ref gcm) => gcm,
KAlgorithmParameters::Aes256Gcm(ref gcm) => gcm,
KAlgorithmParameters::Aes128Gcm(gcm) => gcm,
KAlgorithmParameters::Aes192Gcm(gcm) => gcm,
KAlgorithmParameters::Aes256Gcm(gcm) => gcm,
_ => return Err(CKR_MECHANISM_INVALID)?,
};
Ok((
Expand Down Expand Up @@ -356,15 +356,15 @@ fn decrypt_data(
};

let kkbps1 = match &pdata.algorithm.params {
KAlgorithmParameters::Kkbps1(ref params) => params,
KAlgorithmParameters::Kkbps1(params) => params,
_ => return Err(CKR_MECHANISM_INVALID)?,
};
if kkbps1.key_version_number != key_version {
return Err(CKR_KEY_CHANGED)?;
}

let dek = match &kkbps1.key_derivation_func.params {
KAlgorithmParameters::Kkdf1(ref params) => {
KAlgorithmParameters::Kkdf1(params) => {
if params.key_length != AES_KEYLEN as u64 {
return Err(CKR_MECHANISM_PARAM_INVALID)?;
}
Expand All @@ -375,9 +375,9 @@ fn decrypt_data(
_ => return Err(CKR_MECHANISM_INVALID)?,
};
let params = match &kkbps1.encryption_scheme.params {
KAlgorithmParameters::Aes128Gcm(ref gcm) => gcm,
KAlgorithmParameters::Aes192Gcm(ref gcm) => gcm,
KAlgorithmParameters::Aes256Gcm(ref gcm) => gcm,
KAlgorithmParameters::Aes128Gcm(gcm) => gcm,
KAlgorithmParameters::Aes192Gcm(gcm) => gcm,
KAlgorithmParameters::Aes256Gcm(gcm) => gcm,
_ => return Err(CKR_MECHANISM_INVALID)?,
};
aes_gcm_decrypt(facilities, &dek, params, data_id.as_bytes(), pdata.data)
Expand Down Expand Up @@ -475,16 +475,17 @@ impl StorageACI {
uid: &String,
val: &Vec<u8>,
) -> Result<Vec<u8>> {
if let Some(ref key) = self.key {
encrypt_data(
match self.key {
Some(ref key) => encrypt_data(
facilities,
self.key_version,
key,
uid.as_str(),
val.as_slice(),
)
} else {
return Err(CKR_GENERAL_ERROR)?;
),
_ => {
return Err(CKR_GENERAL_ERROR)?;
}
}
}

Expand All @@ -494,16 +495,17 @@ impl StorageACI {
uid: &String,
val: &Vec<u8>,
) -> Result<Vec<u8>> {
if let Some(ref key) = self.key {
decrypt_data(
match self.key {
Some(ref key) => decrypt_data(
facilities,
self.key_version,
key,
uid.as_str(),
val.as_slice(),
)
} else {
return Err(CKR_GENERAL_ERROR)?;
),
_ => {
return Err(CKR_GENERAL_ERROR)?;
}
}
}

Expand Down
Loading