Skip to content

Commit

Permalink
fix formatting and docs
Browse files Browse the repository at this point in the history
Signed-off-by: nikita.kalinichenko <thenik185@gmail.com>
  • Loading branch information
Nk185 committed Oct 11, 2023
1 parent 8934682 commit 39fd8d1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cryptoki/src/mechanism/ekdf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
//! Mechanisms of key derivation by data encryption
//! See: https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203514
//! See: <https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203514>
use std::{convert::TryInto, marker::PhantomData, slice};

Expand Down
6 changes: 4 additions & 2 deletions cryptoki/src/mechanism/mechanism_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl MechanismInfo {
}

/// True if the mechanism can be used to digest a message
///
// TODO See [`Session::digest`](crate::session::Session::digest)
pub fn digest(&self) -> bool {
self.flags.contains(MechanismInfoFlags::DIGEST)
Expand Down Expand Up @@ -228,7 +227,10 @@ impl std::fmt::Display for MechanismInfo {
if self.max_key_size == 0 {
format!(", min_key_size={}", self.min_key_size)
} else {
format!(", min_key_size={}, max_key_size={}", self.min_key_size, self.max_key_size)
format!(
", min_key_size={}, max_key_size={}",
self.min_key_size, self.max_key_size
)
}
}
};
Expand Down
31 changes: 15 additions & 16 deletions cryptoki/src/mechanism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
//! Data types for mechanisms
pub mod aead;
pub mod ekdf;
pub mod elliptic_curve;
mod mechanism_info;
pub mod rsa;
pub mod ekdf;

use crate::error::Error;
use cryptoki_sys::*;
Expand All @@ -17,8 +17,8 @@ use std::fmt::Formatter;
use std::ops::Deref;
use std::ptr::null_mut;

use crate::mechanism::rsa::PkcsOaepParams;
pub use mechanism_info::MechanismInfo;
use crate::mechanism::rsa::{PkcsOaepParams, PkcsOaepSource};

#[derive(Copy, Debug, Clone, PartialEq, Eq)]
// transparent so that a vector of MechanismType should have the same layout than a vector of
Expand Down Expand Up @@ -67,7 +67,9 @@ impl MechanismType {
pub const AES_GCM: MechanismType = MechanismType { val: CKM_AES_GCM };

/// Derivation via encryption
pub const AES_CBC_ENCRYPT_DATA: MechanismType = MechanismType { val: CKM_AES_CBC_ENCRYPT_DATA };
pub const AES_CBC_ENCRYPT_DATA: MechanismType = MechanismType {
val: CKM_AES_CBC_ENCRYPT_DATA,
};

// RSA
/// PKCS #1 RSA key pair generation mechanism
Expand Down Expand Up @@ -247,8 +249,8 @@ impl MechanismType {
val: CKM_SHA512_RSA_PKCS_PSS,
};
/// GENERIC-SECRET-KEY-GEN mechanism
pub const GENERIC_SECRET_KEY_GEN: MechanismType = MechanismType{
val: CKM_GENERIC_SECRET_KEY_GEN
pub const GENERIC_SECRET_KEY_GEN: MechanismType = MechanismType {
val: CKM_GENERIC_SECRET_KEY_GEN,
};

pub(crate) fn stringify(mech: CK_MECHANISM_TYPE) -> String {
Expand Down Expand Up @@ -706,7 +708,7 @@ pub enum Mechanism<'a> {
/// derivation of keys using the result of an encryption operation as the key value.
///
/// For derivation, the message length must be a multiple of the block
/// size. See https://www.cryptsoft.com/pkcs11doc/v220/
/// size. See <https://www.cryptsoft.com/pkcs11doc/v220/>.
AesCbcEncryptData(ekdf::AesCbcDeriveParams<'a>),

// RSA
Expand Down Expand Up @@ -837,7 +839,7 @@ pub enum Mechanism<'a> {
Sha512RsaPkcsPss(rsa::PkcsPssParams),

/// GENERIC-SECRET-KEY-GEN mechanism
GenericSecretKeyGen
GenericSecretKeyGen,
}

impl Mechanism<'_> {
Expand All @@ -851,7 +853,7 @@ impl Mechanism<'_> {
Mechanism::AesKeyWrap => MechanismType::AES_KEY_WRAP,
Mechanism::AesKeyWrapPad => MechanismType::AES_KEY_WRAP_PAD,
Mechanism::AesGcm(_) => MechanismType::AES_GCM,
Mechanism::AesCbcEncryptData(_) =>MechanismType::AES_CBC_ENCRYPT_DATA,
Mechanism::AesCbcEncryptData(_) => MechanismType::AES_CBC_ENCRYPT_DATA,
Mechanism::RsaPkcsKeyPairGen => MechanismType::RSA_PKCS_KEY_PAIR_GEN,
Mechanism::RsaPkcs => MechanismType::RSA_PKCS,
Mechanism::RsaPkcsPss(_) => MechanismType::RSA_PKCS_PSS,
Expand Down Expand Up @@ -897,7 +899,7 @@ impl Mechanism<'_> {
Mechanism::Sha384RsaPkcsPss(_) => MechanismType::SHA384_RSA_PKCS_PSS,
Mechanism::Sha512RsaPkcsPss(_) => MechanismType::SHA512_RSA_PKCS_PSS,

Mechanism::GenericSecretKeyGen => MechanismType::GENERIC_SECRET_KEY_GEN
Mechanism::GenericSecretKeyGen => MechanismType::GENERIC_SECRET_KEY_GEN,
}
}
}
Expand All @@ -907,13 +909,10 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
let mechanism = mech.mechanism_type().into();
match mech {
// Mechanisms with parameters
Mechanism::AesCbc(params)
| Mechanism::AesCbcPad(params) => {
Mechanism::AesCbc(params) | Mechanism::AesCbcPad(params) => {
make_mechanism(mechanism, params)
},
Mechanism::AesCbcEncryptData(params) => {
make_mechanism(mechanism, params)
},
}
Mechanism::AesCbcEncryptData(params) => make_mechanism(mechanism, params),
Mechanism::DesCbc(params)
| Mechanism::Des3Cbc(params)
| Mechanism::DesCbcPad(params)
Expand Down Expand Up @@ -1021,7 +1020,7 @@ impl TryFrom<psa_crypto::types::algorithm::Algorithm> for Mechanism<'_> {
Ok(Mechanism::RsaPkcsOaep(PkcsOaepParams::new(
Mechanism::try_from(Algorithm::from(hash_alg))?.mechanism_type(),
rsa::PkcsMgfType::from_psa_crypto_hash(hash_alg)?,
PkcsOaepSource::empty(),
rsa::PkcsOaepSource::empty(),
)))
}
alg => {
Expand Down

0 comments on commit 39fd8d1

Please sign in to comment.