Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
default (5)
- Fix the inverted parsing of the touch requirement in SET MANAGEMENT KEY's P2
(0xFF means no touch, 0xFE means touch required)
- Add Ed25519 key generation, import and signing (algorithm identifier `0xE0`)
- Add X25519 key generation, import and key agreement (algorithm identifier `0xE1`)

## [v0.7.0][] (2026-08-17)

Expand Down
86 changes: 77 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ where
.with_application_label(self.options.label)
.with_application_url(self.options.url)
.with_supported_cryptographic_algorithms(&[
Tdes, Aes256, P256, Rsa2048, Rsa3072, Rsa4096, P384,
Tdes, Aes256, P256, Rsa2048, Rsa3072, Rsa4096, P384, Ed25519, X25519,
]);

application_property_template
Expand Down Expand Up @@ -715,11 +715,20 @@ impl<T: Client> LoadedAuthenticator<'_, T> {
let Some(key) = key? else {
return Err(Status::ConditionsOfUseNotSatisfied);
};
if key.alg.sign_len() != message.len() {
let Some(mechanism) = key.alg.sign_mechanism() else {
warn!("Attempt to sign with a key agreement only algorithm");
return Err(Status::ConditionsOfUseNotSatisfied);
};
let message_ok = match key.alg.sign_input_len() {
Some(len) => message.len() == len,
// Arbitrary-length message, bounded by what trussed accepts
None => message.len() <= trussed_core::config::MAX_MESSAGE_LENGTH,
};
if !message_ok {
return Err(Status::IncorrectDataParameter);
}
let response = syscall!(trussed.sign(
key.alg.sign_mechanism(),
mechanism,
key.key,
message,
key.alg.sign_serialization(),
Expand Down Expand Up @@ -778,14 +787,21 @@ impl<T: Client> LoadedAuthenticator<'_, T> {
return Err(Status::ConditionsOfUseNotSatisfied);
};

if data.first() != Some(&0x04) {
warn!("Bad data forat for ECDH");
return Err(Status::IncorrectDataParameter);
}
// X25519 peers send the raw 32 byte u-coordinate, the NIST curves
// a SEC1 uncompressed point
let peer_key = if key.alg.ecdh_peer_key_is_raw() {
data
} else {
if data.first() != Some(&0x04) {
warn!("Bad data format for ECDH");
return Err(Status::IncorrectDataParameter);
}
&data[1..]
};

let public_key = match try_syscall!(trussed.deserialize_key(
mechanism,
&data[1..],
peer_key,
KeySerialization::Raw,
StorageAttributes::default().set_persistence(Location::Volatile)
)) {
Expand Down Expand Up @@ -813,6 +829,11 @@ impl<T: Client> LoadedAuthenticator<'_, T> {
.serialized_key;
syscall!(trussed.delete(public_key));
syscall!(trussed.delete(shared_secret));
// A low-order X25519 peer key yields an all-zero secret (RFC 7748, section 6.1)
if serialized_secret.iter().all(|&b| b == 0) {
warn!("Key agreement produced an all-zero secret");
return Err(Status::IncorrectDataParameter);
}

reply.expand(&[0x7C])?;
let offset = reply.len();
Expand Down Expand Up @@ -919,6 +940,21 @@ impl<T: Client> LoadedAuthenticator<'_, T> {
reply.expand(&serialized_key)?;
reply.prepend_len(offset)?;
}
// Raw 32 byte public key in 0x86, no SEC1 prefix
AsymmetricAlgorithms::Ed25519 | AsymmetricAlgorithms::X25519 => {
let serialized_key = syscall!(self.trussed.serialize_key(
parsed_mechanism.key_mechanism(),
public_key,
KeySerialization::Raw
))
.serialized_key;
reply.expand(&[0x7F, 0x49])?;
let offset = reply.len();
reply.expand(&[0x86])?;
reply.append_len(serialized_key.len())?;
reply.expand(&serialized_key)?;
reply.prepend_len(offset)?;
}
#[cfg(feature = "rsa")]
AsymmetricAlgorithms::Rsa2048
| AsymmetricAlgorithms::Rsa3072
Expand Down Expand Up @@ -1051,7 +1087,7 @@ impl<T: Client> LoadedAuthenticator<'_, T> {
&mut self,
algo: AsymmetricAlgorithms,
key: AsymmetricKeyReference,
#[cfg_attr(not(feature = "rsa"), allow(unused))] data: &[u8],
data: &[u8],
mut _reply: Reply<'_>,
) -> Result {
if !self
Expand Down Expand Up @@ -1095,6 +1131,38 @@ impl<T: Client> LoadedAuthenticator<'_, T> {
);
Ok(())
}
// The raw 32 byte private key comes in 0x07 for Ed25519 and 0x08 for X25519
(AsymmetricAlgorithms::Ed25519 | AsymmetricAlgorithms::X25519, _) => {
let tag = if algo == AsymmetricAlgorithms::Ed25519 {
0x07
} else {
0x08
};
let seed = tlv::get_do(&[tag], data).ok_or(Status::IncorrectDataParameter)?;
if seed.len() != 32 {
warn!("Curve25519 import with a key of length {}", seed.len());
return Err(Status::IncorrectDataParameter);
}
let id = try_syscall!(self.trussed.unsafe_inject_key(
algo.key_mechanism(),
seed,
key.storage(self.options.storage),
KeySerialization::Raw
))
.map_err(|_err| {
warn!("Failed Curve25519 import: {_err:?}");
Status::IncorrectDataParameter
})?
.key;
self.state.persistent.replace_asymmetric_key(
key,
algo,
id,
self.trussed,
self.options.storage,
);
Ok(())
}
_ => Err(Status::FunctionNotSupported),
}
}
Expand Down
65 changes: 41 additions & 24 deletions src/piv_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ enum_u8! {
// P521 = 0x15,
// non-standard!
Rsa4096 = 0x16,
// Ed25519 = 0xE2,
// X25519 = 0xE3,
// non-standard, the Yubico/piv-go/pivy identifiers
Ed25519 = 0xE0,
X25519 = 0xE1,
// Ed448 = 0xE4,
// X448 = 0xE5,

Expand All @@ -152,17 +153,13 @@ crate::container::enum_subset! {
// not standard
P384,

// Only append variants here: the variant index is persisted
Ed25519,
X25519,

// Not supported
// Rsa1024 = 0x6,
// Rsa3072 = 0xE0,
// P521 = 0x15,

// non-standard! in piv-go though!
// Ed255_prev = 0x22,
// https://globalplatform.org/wp-content/uploads/2014/03/GPC_ISO_Framework_v1.0.pdf#page=15
// non-standard!
// Ed25519 = 0xE2,
// X25519 = 0xE3,
// Ed448 = 0xE4,
// X448 = 0xE5,

Expand All @@ -180,44 +177,61 @@ impl AsymmetricAlgorithms {
Self::Rsa4096 => Mechanism::Rsa4096Raw,
Self::P256 => Mechanism::P256,
Self::P384 => Mechanism::P384,
Self::Ed25519 => Mechanism::Ed255,
Self::X25519 => Mechanism::X255,
}
}

/// Key agreement mechanism, `None` for algorithms that cannot do key agreement
pub fn ecdh_mechanism(self) -> Option<Mechanism> {
use AsymmetricAlgorithms::*;
match self {
P256 => Some(Mechanism::P256),
P384 => Some(Mechanism::P384),
/* P384 | P521 | X25519 | X448 */
X25519 => Some(Mechanism::X255),
/* P521 | X448 */
#[allow(unreachable_patterns)]
_ => None,
}
}

pub fn sign_mechanism(self) -> Mechanism {
/// Whether the peer public key of a key agreement is a raw u-coordinate
/// (X25519) rather than a SEC1 uncompressed point
pub fn ecdh_peer_key_is_raw(self) -> bool {
matches!(self, Self::X25519)
}

/// Signature mechanism, `None` for algorithms that cannot sign
pub fn sign_mechanism(self) -> Option<Mechanism> {
match self {
#[cfg(feature = "rsa")]
Self::Rsa2048 => Mechanism::Rsa2048Raw,
Self::Rsa2048 => Some(Mechanism::Rsa2048Raw),
#[cfg(feature = "rsa")]
Self::Rsa3072 => Mechanism::Rsa3072Raw,
Self::Rsa3072 => Some(Mechanism::Rsa3072Raw),
#[cfg(feature = "rsa")]
Self::Rsa4096 => Mechanism::Rsa4096Raw,
Self::P256 => Mechanism::P256Prehashed,
Self::P384 => Mechanism::P384Prehashed,
Self::Rsa4096 => Some(Mechanism::Rsa4096Raw),
Self::P256 => Some(Mechanism::P256Prehashed),
Self::P384 => Some(Mechanism::P384Prehashed),
Self::Ed25519 => Some(Mechanism::Ed255),
Self::X25519 => None,
}
}

/// Accepted length for signature
pub fn sign_len(self) -> usize {
/// Required length of the data to sign: the digest for ECDSA, the padded
/// block for RSA. `None` for algorithms that sign a message of any length.
pub fn sign_input_len(self) -> Option<usize> {
match self {
#[cfg(feature = "rsa")]
Self::Rsa2048 => 256,
Self::Rsa2048 => Some(256),
#[cfg(feature = "rsa")]
Self::Rsa3072 => 384,
Self::Rsa3072 => Some(384),
#[cfg(feature = "rsa")]
Self::Rsa4096 => 512,
Self::P256 => 32,
Self::P384 => 48,
Self::Rsa4096 => Some(512),
Self::P256 => Some(32),
Self::P384 => Some(48),
Self::Ed25519 => None,
// cannot sign, `sign_mechanism` rejects it first
Self::X25519 => None,
}
}

Expand All @@ -227,6 +241,9 @@ impl AsymmetricAlgorithms {
Self::Rsa2048 | Self::Rsa3072 | Self::Rsa4096 => SignatureSerialization::Raw,
Self::P256 => SignatureSerialization::Asn1Der,
Self::P384 => SignatureSerialization::Asn1Der,
Self::Ed25519 => SignatureSerialization::Raw,
// cannot sign, `sign_mechanism` rejects it first
Self::X25519 => SignatureSerialization::Raw,
}
}

Expand Down
Loading
Loading