Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/ed25519-dalek/tests/pkcs8.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / ed25519-dalek / tests / pkcs8.rs` 87 lines ยท 3185 bytes Raw Blame History 1 //! PKCS#8 private key and SPKI public key tests. 2 //! 3 //! These are standard formats for storing public and private keys, defined in 4 //! RFC5958 (PKCS#8) and RFC5280 (SPKI). 5 6 #![cfg(feature = "pkcs8")] 7 use `ed25519`_dalek::pkcs8::{DecodePrivateKey, DecodePublicKey}; 8 use `ed25519`_dalek::{SigningKey, VerifyingKey}; 9 use hex_literal::hex; 10 11 #[cfg(feature = "alloc")] 12 use `ed25519`_dalek::pkcs8::{EncodePrivateKey, EncodePublicKey}; 13 14 #[cfg(all(feature = "alloc", feature = "pkcs8"))] 15 use `ed25519`_dalek::pkcs8::spki::DynSignatureAlgorithmIdentifier; 16 17 /// Ed25519 PKCS#8 v1 private key encoded as ASN.1 DER. 18 const PKCS8_V1_DER: &[u8] = include_bytes!("examples/pkcs8-v1.der"); 19 20 /// Ed25519 PKCS#8 v2 private key + public key encoded as ASN.1 DER. 21 const PKCS8_V2_DER: &[u8] = include_bytes!("examples/pkcs8-v2.der"); 22 23 /// Ed25519 SubjectVerifyingKeyInfo encoded as ASN.1 DER. 24 const PUBLIC_KEY_DER: &[u8] = include_bytes!("examples/pubkey.der"); 25 26 /// Secret key bytes. 27 /// 28 /// Extracted with: 29 /// $ openssl asn1parse -inform der -in tests/examples/pkcs8-v1.der 30 const SK_BYTES: [u8; 32] = hex!("D4EE72DBF913584AD5B6D8F1F769F8AD3AFE7C28CBF1D4FBE097A88F44755842"); 31 32 /// Public key bytes. 33 const PK_BYTES: [u8; 32] = hex!("19BF44096984CDFE8541BAC167DC3B96C85086AA30B6B6CB0C5C38AD703166E1"); 34 35 #[test] 36 fn decode_pkcs8_v1() { 37 let keypair = SigningKey::from_pkcs8_der(PKCS8_V1_DER).unwrap(); 38 assert_eq!(SK_BYTES, keypair.to_bytes()); 39 assert_eq!(PK_BYTES, keypair.verifying_key().to_bytes()); 40 } 41 42 #[test] 43 fn decode_pkcs8_v2() { 44 let keypair = SigningKey::from_pkcs8_der(PKCS8_V2_DER).unwrap(); 45 assert_eq!(SK_BYTES, keypair.to_bytes()); 46 assert_eq!(PK_BYTES, keypair.verifying_key().to_bytes()); 47 } 48 49 #[test] 50 fn decode_verifying_key() { 51 let verifying_key = VerifyingKey::from_public_key_der(PUBLIC_KEY_DER).unwrap(); 52 assert_eq!(PK_BYTES, verifying_key.to_bytes()); 53 } 54 55 #[test] 56 #[cfg(feature = "alloc")] 57 fn encode_pkcs8() { 58 let keypair = SigningKey::from_bytes(&SK_BYTES); 59 let pkcs8_key = keypair.to_pkcs8_der().unwrap(); 60 61 let keypair2 = SigningKey::from_pkcs8_der(pkcs8_key.as_bytes()).unwrap(); 62 assert_eq!(keypair.to_bytes(), keypair2.to_bytes()); 63 } 64 65 #[test] 66 #[cfg(feature = "alloc")] 67 fn encode_verifying_key() { 68 let verifying_key = VerifyingKey::from_bytes(&PK_BYTES).unwrap(); 69 let verifying_key_der = verifying_key.to_public_key_der().unwrap(); 70 71 let verifying_key2 = VerifyingKey::from_public_key_der(verifying_key_der.as_bytes()).unwrap(); 72 assert_eq!(verifying_key, verifying_key2); 73 } 74 75 #[test] 76 #[cfg(feature = "alloc")] 77 fn get_algo_identifier() { 78 let verifying_key = VerifyingKey::from_public_key_der(PUBLIC_KEY_DER).unwrap(); 79 let identifier = verifying_key.signature_algorithm_identifier().unwrap(); 80 assert!(identifier.parameters.is_none()); // According to rfc8410 this must be None 81 assert_eq!(identifier.oid, `ed25519`::pkcs8::ALGORITHM_OID); 82 83 let signing_key = SigningKey::from_bytes(&SK_BYTES); 84 let identifier = signing_key.signature_algorithm_identifier().unwrap(); 85 assert!(identifier.parameters.is_none()); // According to rfc8410 this must be None 86 assert_eq!(identifier.oid, `ed25519`::pkcs8::ALGORITHM_OID); 87 }