From 1f274be8ca6fb1e8434f100e188431f99f9612bf Mon Sep 17 00:00:00 2001 From: Isaiah Inuwa Date: Fri, 20 Feb 2026 10:55:55 -0600 Subject: [PATCH] Add P-521 test --- .../win_webauthn/src/plugin/crypto.rs | 87 ++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/apps/desktop/desktop_native/win_webauthn/src/plugin/crypto.rs b/apps/desktop/desktop_native/win_webauthn/src/plugin/crypto.rs index 10352d78215..13b706ee3b8 100644 --- a/apps/desktop/desktop_native/win_webauthn/src/plugin/crypto.rs +++ b/apps/desktop/desktop_native/win_webauthn/src/plugin/crypto.rs @@ -430,7 +430,7 @@ mod tests { use windows::Win32::Security::Cryptography::{ BCRYPT_ECCKEY_BLOB, BCRYPT_ECDSA_PUBLIC_P256_MAGIC, BCRYPT_ECDSA_PUBLIC_P384_MAGIC, - BCRYPT_RSAKEY_BLOB, BCRYPT_RSAPUBLIC_MAGIC, + BCRYPT_ECDSA_PUBLIC_P521_MAGIC, BCRYPT_RSAKEY_BLOB, BCRYPT_RSAPUBLIC_MAGIC, }; use super::hash_sha256; @@ -708,4 +708,89 @@ mod tests { // We manually constructed this key for the test, so don't call the WebAuthn free method. std::mem::forget(verify_key); } + + #[test] + fn test_p521_signature_verifies_properly() { + // SHA-256 hash of "abc" + let digest = vec![ + 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, + 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, + 0xf2, 0x00, 0x15, 0xad, + ]; + /* + Test private key used to create expected signature, generated by OpenSSL + + -----BEGIN PRIVATE KEY----- + MIICrwIBADCCAc8GByqGSM49AgEwggHCAgEBME0GByqGSM49AQECQgH///////// + //////////////////////////////////////////////////////////////// + /////////////zCBngRCAf////////////////////////////////////////// + ///////////////////////////////////////////8BEFRlT65YY4cmh+SmiGg + toVA7qLacluZsxXzuLSJkY7xCeFWGTlR7H6TexZSwL07sb8HNXPfiD0sNPHvRR/U + a1A/AAMVANCeiAApHLhTlsxnFzkyhKqg2mS6BIGFBADGhY4GtwQE6c2ePstmI5W0 + QpxkgTkFP7Uh+CivYGtNPbqhS1537+dZKP4dwSei/6jeM0izwYVqQpv5fn4xwuW9 + ZgEYOSlqeJo7wARcil+0LH0b2Zj1RElXm0RoF6+9Fyc+ZiyX7nKZXvQmQMVQuQE/ + rQdhNTxwhqJywkCIvpR2n9FmUAJCAf////////////////////////////////// + ////////+lGGh4O/L5Zrf8wBSPcJpdA7tcm4iZxHrrtvtx6ROGQJAgEBBIHWMIHT + AgEBBEIBiHOVMi7SQiSeY6hqL3ilLEY/AB7pVe3gqcY2I3RPM59eegWRFXds8q5W + 6KS3fVYkvT92vyRbcLLvcjGh429wz2GhgYkDgYYABAE0TENXeEO/G7Swkc1pnfS7 + mfWaoqnl485oXDaRZ3ktCHHcpsfaggM44j8ZNmXm4ePJ3rFZtWW9qxKKn239yms9 + UgGVLHrBoRxicVBiU5gMFOcJOARdHfz59dYORen/alGjQzeoi2ZadbrHl8CXyOkf + B9vqsDMhjFpMh6hps2x3s8EkNA== + -----END PRIVATE KEY----- + + Signature generated with: + openssl pkeyutl -sign -inkey p521.key.pem -in hash.bin -out sig_der.bin + Converted from DER to IEEE P1363 (r || s) format. + */ + // P-521 public key X coordinate (66 bytes, big-endian) + let x: &[u8] = &[ + 0x01, 0x34, 0x4c, 0x43, 0x57, 0x78, 0x43, 0xbf, 0x1b, 0xb4, 0xb0, 0x91, 0xcd, 0x69, + 0x9d, 0xf4, 0xbb, 0x99, 0xf5, 0x9a, 0xa2, 0xa9, 0xe5, 0xe3, 0xce, 0x68, 0x5c, 0x36, + 0x91, 0x67, 0x79, 0x2d, 0x08, 0x71, 0xdc, 0xa6, 0xc7, 0xda, 0x82, 0x03, 0x38, 0xe2, + 0x3f, 0x19, 0x36, 0x65, 0xe6, 0xe1, 0xe3, 0xc9, 0xde, 0xb1, 0x59, 0xb5, 0x65, 0xbd, + 0xab, 0x12, 0x8a, 0x9f, 0x6d, 0xfd, 0xca, 0x6b, 0x3d, 0x52, + ]; + // P-521 public key Y coordinate (66 bytes, big-endian) + let y: &[u8] = &[ + 0x01, 0x95, 0x2c, 0x7a, 0xc1, 0xa1, 0x1c, 0x62, 0x71, 0x50, 0x62, 0x53, 0x98, 0x0c, + 0x14, 0xe7, 0x09, 0x38, 0x04, 0x5d, 0x1d, 0xfc, 0xf9, 0xf5, 0xd6, 0x0e, 0x45, 0xe9, + 0xff, 0x6a, 0x51, 0xa3, 0x43, 0x37, 0xa8, 0x8b, 0x66, 0x5a, 0x75, 0xba, 0xc7, 0x97, + 0xc0, 0x97, 0xc8, 0xe9, 0x1f, 0x07, 0xdb, 0xea, 0xb0, 0x33, 0x21, 0x8c, 0x5a, 0x4c, + 0x87, 0xa8, 0x69, 0xb3, 0x6c, 0x77, 0xb3, 0xc1, 0x24, 0x34, + ]; + let key_header = BCRYPT_ECCKEY_BLOB { + dwMagic: BCRYPT_ECDSA_PUBLIC_P521_MAGIC, + cbKey: 66, // P-521: ceil(521 / 8) = 66 bytes per coordinate + }; + let mut public_key_bytes: Vec = unsafe { + std::slice::from_raw_parts( + std::ptr::from_ref(&key_header).cast::(), + std::mem::size_of::(), + ) + } + .to_vec(); + public_key_bytes.extend_from_slice(x); + public_key_bytes.extend_from_slice(y); + let verify_key = VerifyingKey { + cbPublicKey: public_key_bytes.len() as u32, + pbPublicKey: NonNull::new(public_key_bytes.as_mut_ptr().cast()).unwrap(), + }; + // ECDSA signature in IEEE P1363 format (r || s), each 66 bytes, big-endian + let signature: &[u8] = &[ + 0x00, 0x89, 0x94, 0x84, 0xe2, 0xad, 0xc2, 0x9e, 0x91, 0xc9, 0x5a, 0x18, 0x87, 0xec, + 0x1b, 0x22, 0xa0, 0x4d, 0xab, 0x2e, 0xbb, 0x82, 0x94, 0xdc, 0x51, 0xf7, 0x33, 0x17, + 0x4c, 0xa5, 0x14, 0x65, 0x6a, 0x07, 0x94, 0xfc, 0x4a, 0x43, 0x9a, 0xa4, 0x89, 0xf5, + 0x2e, 0x8c, 0x14, 0x3f, 0x74, 0xa2, 0xc2, 0x42, 0xc8, 0x29, 0xf7, 0x82, 0x47, 0x64, + 0x92, 0xb4, 0xd5, 0x9a, 0x20, 0x84, 0x38, 0xf7, 0x0c, 0xb8, 0x00, 0xeb, 0xec, 0xf3, + 0xeb, 0xc7, 0x5d, 0xbe, 0x36, 0x53, 0xb6, 0xc9, 0xe0, 0xe3, 0xa5, 0xb0, 0x07, 0xda, + 0x8f, 0x97, 0x03, 0x6f, 0xe9, 0x11, 0x14, 0xbf, 0xdc, 0x75, 0x48, 0x36, 0xf5, 0xf2, + 0x03, 0xb7, 0xad, 0x4a, 0xc6, 0x12, 0x05, 0x7a, 0xd2, 0x6f, 0x1a, 0x8f, 0x0d, 0xd9, + 0x84, 0x04, 0x63, 0x47, 0xb7, 0x4b, 0x29, 0xa5, 0x3a, 0xe3, 0x7f, 0x27, 0x87, 0x38, + 0x61, 0xca, 0xe2, 0x85, 0xd5, 0x6d, + ]; + verify_signature(&verify_key, RequestHash(&digest), Signature(signature)) + .expect("a signature to verify properly"); + // We manually constructed this key for the test, so don't call the WebAuthn free method. + std::mem::forget(verify_key); + } }