From ac4ac85ebf200123793ad23bacfe1d513dde6b32 Mon Sep 17 00:00:00 2001 From: Florian Franzen Date: Fri, 9 Oct 2026 10:45:05 +0200 Subject: [PATCH 1/2] Add Ed25519 key generation, import and signing Co-authored-by: Emanuele Cesena --- CHANGELOG.md | 1 + src/lib.rs | 53 ++++++++++++++++++++++++-- src/piv_types.rs | 38 ++++++++++--------- tests/command_response.ron | 78 ++++++++++++++++++++++++++++++++++++++ tests/command_response.rs | 13 ++++--- tests/pivy.rs | 2 +- 6 files changed, 157 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e1a0096..e241869a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ 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`) ## [v0.7.0][] (2026-08-17) diff --git a/src/lib.rs b/src/lib.rs index 52aefebf..08cc61a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, ]); application_property_template @@ -715,7 +715,12 @@ impl LoadedAuthenticator<'_, T> { let Some(key) = key? else { return Err(Status::ConditionsOfUseNotSatisfied); }; - if key.alg.sign_len() != message.len() { + 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( @@ -919,6 +924,21 @@ impl LoadedAuthenticator<'_, T> { reply.expand(&serialized_key)?; reply.prepend_len(offset)?; } + // Raw 32 byte public key in 0x86, no SEC1 prefix + AsymmetricAlgorithms::Ed25519 => { + 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 @@ -1051,7 +1071,7 @@ impl 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 @@ -1095,6 +1115,33 @@ impl LoadedAuthenticator<'_, T> { ); Ok(()) } + // The 32 byte seed comes in 0x07 + (AsymmetricAlgorithms::Ed25519, _) => { + let seed = tlv::get_do(&[0x07], data).ok_or(Status::IncorrectDataParameter)?; + if seed.len() != 32 { + warn!("Ed25519 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 Ed25519 import: {_err:?}"); + Status::IncorrectDataParameter + })? + .key; + self.state.persistent.replace_asymmetric_key( + key, + algo, + id, + self.trussed, + self.options.storage, + ); + Ok(()) + } _ => Err(Status::FunctionNotSupported), } } diff --git a/src/piv_types.rs b/src/piv_types.rs index 3d6fd051..9d3ab70e 100644 --- a/src/piv_types.rs +++ b/src/piv_types.rs @@ -124,8 +124,9 @@ enum_u8! { // P521 = 0x15, // non-standard! Rsa4096 = 0x16, - // Ed25519 = 0xE2, - // X25519 = 0xE3, + // non-standard, the Yubico/piv-go/pivy identifier + Ed25519 = 0xE0, + // X25519 = 0xE1, // Ed448 = 0xE4, // X448 = 0xE5, @@ -152,17 +153,13 @@ crate::container::enum_subset! { // not standard P384, + // Only append variants here: the variant index is persisted + Ed25519, + // 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, + // X25519 = 0xE1, // Ed448 = 0xE4, // X448 = 0xE5, @@ -180,6 +177,7 @@ impl AsymmetricAlgorithms { Self::Rsa4096 => Mechanism::Rsa4096Raw, Self::P256 => Mechanism::P256, Self::P384 => Mechanism::P384, + Self::Ed25519 => Mechanism::Ed255, } } @@ -188,7 +186,7 @@ impl AsymmetricAlgorithms { match self { P256 => Some(Mechanism::P256), P384 => Some(Mechanism::P384), - /* P384 | P521 | X25519 | X448 */ + /* P521 | X25519 | X448 */ #[allow(unreachable_patterns)] _ => None, } @@ -204,20 +202,23 @@ impl AsymmetricAlgorithms { Self::Rsa4096 => Mechanism::Rsa4096Raw, Self::P256 => Mechanism::P256Prehashed, Self::P384 => Mechanism::P384Prehashed, + Self::Ed25519 => Mechanism::Ed255, } } - /// 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 { 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, } } @@ -227,6 +228,7 @@ impl AsymmetricAlgorithms { Self::Rsa2048 | Self::Rsa3072 | Self::Rsa4096 => SignatureSerialization::Raw, Self::P256 => SignatureSerialization::Asn1Der, Self::P384 => SignatureSerialization::Asn1Der, + Self::Ed25519 => SignatureSerialization::Raw, } } diff --git a/tests/command_response.ron b/tests/command_response.ron index 7bc7ecea..93eea1fc 100644 --- a/tests/command_response.ron +++ b/tests/command_response.ron @@ -255,6 +255,84 @@ ) ] ), + IoTest( + name: "Generate Ed25519 key and sign", + cmd_resp: [ + AuthenticateManagement( + key: ( + algorithm: Tdes, + key: "0102030405060708 0102030405060708 0102030405060708" + ), + mutual: false, + ), + IoData( + input: "00 47 009A 05 + AC 03 + 80 01 E0", + // 7F49 22 { 86 20 } + output: Len(37), + ), + VerifyApplicationPin(), + // Ed25519 signs the message itself, any length up to the trussed limit + Sign( + algo: 0xE0, + key_reference: 0x9A, + data: "", + // 7C 42 { 82 40 <64 byte signature> } + output: Len(68), + ), + Sign( + algo: 0xE0, + key_reference: 0x9A, + data: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F", + output: Len(68), + ), + // Messages above the trussed limit (1024 bytes) are rejected instead of panicking + Sign( + algo: 0xE0, + key_reference: 0x9A, + data: "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00", + output: Len(0), + expected_status: IncorrectDataParameter, + ), + ] + ), + IoTest( + name: "Import Ed25519 key and sign", + cmd_resp: [ + AuthenticateManagement( + key: ( + algorithm: Tdes, + key: "0102030405060708 0102030405060708 0102030405060708" + ), + mutual: false, + ), + // RFC 8032 section 7.1 test 2: the seed goes into 0x07 as with Yubico + IoData( + input: "00 FE E0 9A 22 + 07 20 4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb", + output: Len(0), + ), + VerifyApplicationPin(), + Sign( + algo: 0xE0, + key_reference: 0x9A, + data: "72", + output: Data(" + 7C 42 + 82 40 + 92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00 + "), + ), + // A key of the wrong length is rejected + IoData( + input: "00 FE E0 9D 12 + 07 10 4ccd089b28ff96da9db6c346ec114e0f", + output: Len(0), + expected_status: IncorrectDataParameter, + ), + ] + ), IoTest( name: "PUT DATA", cmd_resp: [ diff --git a/tests/command_response.rs b/tests/command_response.rs index b0e2287d..f1f04922 100644 --- a/tests/command_response.rs +++ b/tests/command_response.rs @@ -76,10 +76,10 @@ pub enum Algorithm { P521 = 0x15, // non-standard! - Rsa3072 = 0xE0, - Rsa4096 = 0xE1, - Ed25519 = 0xE2, - X25519 = 0xE3, + Rsa3072 = 0x05, + Rsa4096 = 0x16, + Ed25519 = 0xE0, + X25519 = 0xE1, Ed448 = 0xE4, X448 = 0xE5, @@ -712,14 +712,14 @@ impl IoCmd { fn run_select(card: &mut setup::Piv) { let matcher = OutputMatcher::Bytes(Cow::Borrowed(&hex!( - "61 69" // Card application property template + "61 6c" // Card application property template "4f 06 000010000100" // Application identifier "50 0c 4e6974726f6b657920504956" // Application label = b\"Nitrokey PIV\" // URL = b\"https://github.com/Nitrokey/piv-authenticator\" "5f50 2d 68747470733a2f2f6769746875622e636f6d2f4e6974726f6b65792f7069762d61757468656e74696361746f72" // Cryptographic Algorithm Identifier Template - "ac 18" + "ac 1b" "80 01 03" // TDES - ECB "80 01 0c" // AES256 - ECB "80 01 11" // P-256 @@ -727,6 +727,7 @@ impl IoCmd { "80 01 05" // RSA 3072 "80 01 16" // RSA 4096 "80 01 14" // P384 + "80 01 e0" // Ed25519 "06 01 00" // Coexistent Tag Allocation Authority Template "79 07" diff --git a/tests/pivy.rs b/tests/pivy.rs index 4483a831..5433c555 100644 --- a/tests/pivy.rs +++ b/tests/pivy.rs @@ -54,7 +54,7 @@ fn list() { p.expect(format!("device: {CARD}")).unwrap(); p.expect("chuid: ok").unwrap(); p.expect(Regex("guid: [0-9A-Z]*")).unwrap(); - p.expect("algos: 3DES AES256 ECCP256 RSA2048 RSA3072 RSA4096 ECCP384") + p.expect("algos: 3DES AES256 ECCP256 RSA2048 RSA3072 RSA4096 ECCP384 ED25519") .unwrap(); p.expect(Eof).unwrap(); assert_eq!( From 8de994fbe8fa4f00ca300a561069536fc4561872 Mon Sep 17 00:00:00 2001 From: Florian Franzen Date: Fri, 9 Oct 2026 12:30:26 +0200 Subject: [PATCH 2/2] Add X25519 key generation, import and key agreement --- CHANGELOG.md | 1 + src/lib.rs | 47 +++++++++++++++------ src/piv_types.rs | 37 +++++++++++----- tests/command_response.ron | 86 ++++++++++++++++++++++++++++++++++++++ tests/command_response.rs | 5 ++- tests/pivy.rs | 2 +- 6 files changed, 151 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e241869a..0ae45f20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - 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) diff --git a/src/lib.rs b/src/lib.rs index 08cc61a5..65f0cc67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, Ed25519, + Tdes, Aes256, P256, Rsa2048, Rsa3072, Rsa4096, P384, Ed25519, X25519, ]); application_property_template @@ -715,6 +715,10 @@ impl LoadedAuthenticator<'_, T> { let Some(key) = key? else { return Err(Status::ConditionsOfUseNotSatisfied); }; + 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 @@ -724,7 +728,7 @@ impl LoadedAuthenticator<'_, T> { return Err(Status::IncorrectDataParameter); } let response = syscall!(trussed.sign( - key.alg.sign_mechanism(), + mechanism, key.key, message, key.alg.sign_serialization(), @@ -783,14 +787,21 @@ impl 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) )) { @@ -818,6 +829,11 @@ impl 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(); @@ -925,7 +941,7 @@ impl LoadedAuthenticator<'_, T> { reply.prepend_len(offset)?; } // Raw 32 byte public key in 0x86, no SEC1 prefix - AsymmetricAlgorithms::Ed25519 => { + AsymmetricAlgorithms::Ed25519 | AsymmetricAlgorithms::X25519 => { let serialized_key = syscall!(self.trussed.serialize_key( parsed_mechanism.key_mechanism(), public_key, @@ -1115,11 +1131,16 @@ impl LoadedAuthenticator<'_, T> { ); Ok(()) } - // The 32 byte seed comes in 0x07 - (AsymmetricAlgorithms::Ed25519, _) => { - let seed = tlv::get_do(&[0x07], data).ok_or(Status::IncorrectDataParameter)?; + // 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!("Ed25519 import with a key of length {}", seed.len()); + warn!("Curve25519 import with a key of length {}", seed.len()); return Err(Status::IncorrectDataParameter); } let id = try_syscall!(self.trussed.unsafe_inject_key( @@ -1129,7 +1150,7 @@ impl LoadedAuthenticator<'_, T> { KeySerialization::Raw )) .map_err(|_err| { - warn!("Failed Ed25519 import: {_err:?}"); + warn!("Failed Curve25519 import: {_err:?}"); Status::IncorrectDataParameter })? .key; diff --git a/src/piv_types.rs b/src/piv_types.rs index 9d3ab70e..383c393c 100644 --- a/src/piv_types.rs +++ b/src/piv_types.rs @@ -124,9 +124,9 @@ enum_u8! { // P521 = 0x15, // non-standard! Rsa4096 = 0x16, - // non-standard, the Yubico/piv-go/pivy identifier + // non-standard, the Yubico/piv-go/pivy identifiers Ed25519 = 0xE0, - // X25519 = 0xE1, + X25519 = 0xE1, // Ed448 = 0xE4, // X448 = 0xE5, @@ -155,11 +155,11 @@ crate::container::enum_subset! { // Only append variants here: the variant index is persisted Ed25519, + X25519, // Not supported // Rsa1024 = 0x6, // P521 = 0x15, - // X25519 = 0xE1, // Ed448 = 0xE4, // X448 = 0xE5, @@ -178,31 +178,42 @@ impl AsymmetricAlgorithms { 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 { use AsymmetricAlgorithms::*; match self { P256 => Some(Mechanism::P256), P384 => Some(Mechanism::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 { 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::Ed25519 => Mechanism::Ed255, + Self::Rsa4096 => Some(Mechanism::Rsa4096Raw), + Self::P256 => Some(Mechanism::P256Prehashed), + Self::P384 => Some(Mechanism::P384Prehashed), + Self::Ed25519 => Some(Mechanism::Ed255), + Self::X25519 => None, } } @@ -219,6 +230,8 @@ impl AsymmetricAlgorithms { Self::P256 => Some(32), Self::P384 => Some(48), Self::Ed25519 => None, + // cannot sign, `sign_mechanism` rejects it first + Self::X25519 => None, } } @@ -229,6 +242,8 @@ impl AsymmetricAlgorithms { Self::P256 => SignatureSerialization::Asn1Der, Self::P384 => SignatureSerialization::Asn1Der, Self::Ed25519 => SignatureSerialization::Raw, + // cannot sign, `sign_mechanism` rejects it first + Self::X25519 => SignatureSerialization::Raw, } } diff --git a/tests/command_response.ron b/tests/command_response.ron index 93eea1fc..7f19dc18 100644 --- a/tests/command_response.ron +++ b/tests/command_response.ron @@ -333,6 +333,92 @@ ), ] ), + IoTest( + name: "Generate X25519 key and agree", + cmd_resp: [ + AuthenticateManagement( + key: ( + algorithm: Tdes, + key: "0102030405060708 0102030405060708 0102030405060708" + ), + mutual: false, + ), + IoData( + input: "00 47 009D 05 + AC 03 + 80 01 E1", + // 7F49 22 { 86 20 } + output: Len(37), + ), + VerifyApplicationPin(), + // Key agreement with a raw 32 byte peer key in 0x85 + IoData( + input: "00 87 E1 9D 26 + 7C 24 + 82 00 + 85 20 de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f", + // 7C 22 { 82 20 <32 byte shared secret> } + output: Len(36), + ), + // X25519 keys cannot sign + Sign( + algo: 0xE1, + key_reference: 0x9D, + data: "72", + output: Len(0), + expected_status: ConditionsOfUseNotSatisfied, + ), + ] + ), + IoTest( + name: "Import X25519 key and agree", + cmd_resp: [ + AuthenticateManagement( + key: ( + algorithm: Tdes, + key: "0102030405060708 0102030405060708 0102030405060708" + ), + mutual: false, + ), + // RFC 7748 section 6.1: Alice's private key goes into 0x08 as with Yubico + IoData( + input: "00 FE E1 9D 22 + 08 20 77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a", + output: Len(0), + ), + VerifyApplicationPin(), + // Bob's public key gives the shared secret K of the RFC + IoData( + input: "00 87 E1 9D 26 + 7C 24 + 82 00 + 85 20 de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f", + output: Data(" + 7C 22 + 82 20 + 4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742 + "), + ), + // A peer key of the wrong length is rejected + IoData( + input: "00 87 E1 9D 16 + 7C 14 + 82 00 + 85 10 de9edb7d7b7dc1b4d35b61c2ece43537", + output: Len(0), + expected_status: IncorrectDataParameter, + ), + // A low-order peer key (order 4) would yield an all-zero secret and is rejected + IoData( + input: "00 87 E1 9D 26 + 7C 24 + 82 00 + 85 20 0100000000000000000000000000000000000000000000000000000000000000", + output: Len(0), + expected_status: IncorrectDataParameter, + ), + ] + ), IoTest( name: "PUT DATA", cmd_resp: [ diff --git a/tests/command_response.rs b/tests/command_response.rs index f1f04922..b2c5d643 100644 --- a/tests/command_response.rs +++ b/tests/command_response.rs @@ -712,14 +712,14 @@ impl IoCmd { fn run_select(card: &mut setup::Piv) { let matcher = OutputMatcher::Bytes(Cow::Borrowed(&hex!( - "61 6c" // Card application property template + "61 6f" // Card application property template "4f 06 000010000100" // Application identifier "50 0c 4e6974726f6b657920504956" // Application label = b\"Nitrokey PIV\" // URL = b\"https://github.com/Nitrokey/piv-authenticator\" "5f50 2d 68747470733a2f2f6769746875622e636f6d2f4e6974726f6b65792f7069762d61757468656e74696361746f72" // Cryptographic Algorithm Identifier Template - "ac 1b" + "ac 1e" "80 01 03" // TDES - ECB "80 01 0c" // AES256 - ECB "80 01 11" // P-256 @@ -728,6 +728,7 @@ impl IoCmd { "80 01 16" // RSA 4096 "80 01 14" // P384 "80 01 e0" // Ed25519 + "80 01 e1" // X25519 "06 01 00" // Coexistent Tag Allocation Authority Template "79 07" diff --git a/tests/pivy.rs b/tests/pivy.rs index 5433c555..71be76b0 100644 --- a/tests/pivy.rs +++ b/tests/pivy.rs @@ -54,7 +54,7 @@ fn list() { p.expect(format!("device: {CARD}")).unwrap(); p.expect("chuid: ok").unwrap(); p.expect(Regex("guid: [0-9A-Z]*")).unwrap(); - p.expect("algos: 3DES AES256 ECCP256 RSA2048 RSA3072 RSA4096 ECCP384 ED25519") + p.expect("algos: 3DES AES256 ECCP256 RSA2048 RSA3072 RSA4096 ECCP384 ED25519 X25519") .unwrap(); p.expect(Eof).unwrap(); assert_eq!(