Skip to content

Commit

Permalink
Add EdDSA mechanism
Browse files Browse the repository at this point in the history
Co-authored-by: legounix <[email protected]>
Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
  • Loading branch information
wiktor-k and legounix committed May 9, 2023
1 parent 2d1db88 commit 657d64c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cryptoki/src/mechanism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ impl MechanismType {
val: CKM_EC_MONTGOMERY_KEY_PAIR_GEN,
};

/// EDDSA mechanism
///
/// Note: EdDSA is not part of the PKCS#11 v2.40 standard and as
/// such may not be understood by the backend. It is included here
/// because some vendor implementations support it through the
/// v2.40 interface.
pub const EDDSA: MechanismType = MechanismType { val: CKM_EDDSA };
/// ECDH key derivation mechanism
pub const ECDH1_DERIVE: MechanismType = MechanismType {
val: CKM_ECDH1_DERIVE,
Expand Down Expand Up @@ -587,6 +594,7 @@ impl MechanismType {
CKM_EC_MONTGOMERY_KEY_PAIR_GEN => {
String::from(stringify!(CKM_EC_MONTGOMERY_KEY_PAIR_GEN))
}
CKM_EDDSA => String::from(stringify!(CKM_EDDSA)),
_ => format!("unknown {mech:08x}"),
}
}
Expand Down Expand Up @@ -631,6 +639,7 @@ impl TryFrom<CK_MECHANISM_TYPE> for MechanismType {
CKM_EC_KEY_PAIR_GEN => Ok(MechanismType::ECC_KEY_PAIR_GEN),
CKM_EC_EDWARDS_KEY_PAIR_GEN => Ok(MechanismType::ECC_EDWARDS_KEY_PAIR_GEN),
CKM_EC_MONTGOMERY_KEY_PAIR_GEN => Ok(MechanismType::ECC_MONTGOMERY_KEY_PAIR_GEN),
CKM_EDDSA => Ok(MechanismType::EDDSA),
CKM_ECDH1_DERIVE => Ok(MechanismType::ECDH1_DERIVE),
CKM_ECDSA => Ok(MechanismType::ECDSA),
CKM_SHA256_RSA_PKCS => Ok(MechanismType::SHA256_RSA_PKCS),
Expand Down Expand Up @@ -761,6 +770,13 @@ pub enum Mechanism<'a> {
EcdsaSha384,
/// ECDSA with SHA-512 mechanism
EcdsaSha512,
/// EDDSA mechanism
///
/// Note: EdDSA is not part of the PKCS#11 v2.40 standard and as
/// such may not be understood by the backend. It is included here
/// because some vendor implementations support it through the
/// v2.40 interface.
Eddsa,

// SHA-n
/// SHA-1 mechanism
Expand Down Expand Up @@ -827,6 +843,7 @@ impl Mechanism<'_> {
Mechanism::EccKeyPairGen => MechanismType::ECC_KEY_PAIR_GEN,
Mechanism::EccEdwardsKeyPairGen => MechanismType::ECC_EDWARDS_KEY_PAIR_GEN,
Mechanism::EccMontgomeryKeyPairGen => MechanismType::ECC_MONTGOMERY_KEY_PAIR_GEN,
Mechanism::Eddsa => MechanismType::EDDSA,
Mechanism::Ecdh1Derive(_) => MechanismType::ECDH1_DERIVE,
Mechanism::Ecdsa => MechanismType::ECDSA,
Mechanism::EcdsaSha1 => MechanismType::ECDSA_SHA1,
Expand Down Expand Up @@ -895,6 +912,7 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
| Mechanism::EccKeyPairGen
| Mechanism::EccEdwardsKeyPairGen
| Mechanism::EccMontgomeryKeyPairGen
| Mechanism::Eddsa
| Mechanism::Ecdsa
| Mechanism::EcdsaSha1
| Mechanism::EcdsaSha224
Expand Down
39 changes: 39 additions & 0 deletions cryptoki/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ fn sign_verify() -> TestResult {
Ok(())
}

#[test]
#[serial]
fn sign_verify_ed25519() -> TestResult {
let (pkcs11, slot) = init_pins();

let session = pkcs11.open_rw_session(slot)?;

session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;

let mechanism = Mechanism::EccEdwardsKeyPairGen;

let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::Verify(true),
// Ed25519 OID
// See: https://github.com/opendnssec/SoftHSMv2/blob/ac70dc398b236e4522101930e790008936489e2d/src/lib/test/SignVerifyTests.cpp#L173
Attribute::EcParams(vec![
0x13, 0x0c, 0x65, 0x64, 0x77, 0x61, 0x72, 0x64, 0x73, 0x32, 0x35, 0x35, 0x31, 0x39,
]),
];

let priv_key_template = vec![Attribute::Token(true)];

let (public, private) =
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;

let data = [0xFF, 0x55, 0xDD];

let signature = session.sign(&Mechanism::Eddsa, private, &data)?;

session.verify(&Mechanism::Eddsa, public, &data, &signature)?;

session.destroy_object(public)?;
session.destroy_object(private)?;

Ok(())
}

#[test]
#[serial]
fn encrypt_decrypt() -> TestResult {
Expand Down

0 comments on commit 657d64c

Please sign in to comment.