aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / ed25519 / tests / pkcs8.rs
| 1 | //! PKCS#8 private key tests |
| 2 | |
| 3 | #![cfg(feature = "pkcs8")] |
| 4 | |
| 5 | use ed25519::pkcs8::{DecodePrivateKey, DecodePublicKey, KeypairBytes, PublicKeyBytes}; |
| 6 | use hex_literal::hex; |
| 7 | |
| 8 | #[cfg(feature = "alloc")] |
| 9 | use ed25519::pkcs8::{EncodePrivateKey, EncodePublicKey}; |
| 10 | |
| 11 | /// Ed25519 PKCS#8 v1 private key encoded as ASN.1 DER. |
| 12 | const PKCS8_V1_DER: &[u8] = include_bytes!("examples/pkcs8-v1.der"); |
| 13 | |
| 14 | /// Ed25519 PKCS#8 v2 private key + public key encoded as ASN.1 DER. |
| 15 | const PKCS8_V2_DER: &[u8] = include_bytes!("examples/pkcs8-v2.der"); |
| 16 | |
| 17 | /// Ed25519 SubjectPublicKeyInfo encoded as ASN.1 DER. |
| 18 | const PUBLIC_KEY_DER: &[u8] = include_bytes!("examples/pubkey.der"); |
| 19 | |
| 20 | #[test] |
| 21 | fn decode_pkcs8_v1() { |
| 22 | let keypair = KeypairBytes::from_pkcs8_der(PKCS8_V1_DER).unwrap(); |
| 23 | |
| 24 | // Extracted with: |
| 25 | // $ openssl asn1parse -inform der -in tests/examples/pkcs8-v1.der |
| 26 | assert_eq!( |
| 27 | keypair.secret_key, |
| 28 | &hex!("D4EE72DBF913584AD5B6D8F1F769F8AD3AFE7C28CBF1D4FBE097A88F44755842")[..] |
| 29 | ); |
| 30 | |
| 31 | assert_eq!(keypair.public_key, None); |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn decode_pkcs8_v2() { |
| 36 | let keypair = KeypairBytes::from_pkcs8_der(PKCS8_V2_DER).unwrap(); |
| 37 | |
| 38 | // Extracted with: |
| 39 | // $ openssl asn1parse -inform der -in tests/examples/pkcs8-v2.der |
| 40 | assert_eq!( |
| 41 | keypair.secret_key, |
| 42 | &hex!("D4EE72DBF913584AD5B6D8F1F769F8AD3AFE7C28CBF1D4FBE097A88F44755842")[..] |
| 43 | ); |
| 44 | |
| 45 | assert_eq!( |
| 46 | keypair.public_key.unwrap().0, |
| 47 | hex!("19BF44096984CDFE8541BAC167DC3B96C85086AA30B6B6CB0C5C38AD703166E1") |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn decode_public_key() { |
| 53 | let public_key = PublicKeyBytes::from_public_key_der(PUBLIC_KEY_DER).unwrap(); |
| 54 | |
| 55 | // Extracted with: |
| 56 | // $ openssl pkey -inform der -in tests/examples/pkcs8-v1.der -pubout -text |
| 57 | assert_eq!( |
| 58 | public_key.as_ref(), |
| 59 | &hex!("19BF44096984CDFE8541BAC167DC3B96C85086AA30B6B6CB0C5C38AD703166E1") |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | #[cfg(feature = "alloc")] |
| 64 | #[test] |
| 65 | fn encode_pkcs8_v1() { |
| 66 | let pk = KeypairBytes::from_pkcs8_der(PKCS8_V1_DER).unwrap(); |
| 67 | let pk_der = pk.to_pkcs8_der().unwrap(); |
| 68 | assert_eq!(pk_der.as_bytes(), PKCS8_V1_DER); |
| 69 | } |
| 70 | |
| 71 | #[cfg(feature = "alloc")] |
| 72 | #[test] |
| 73 | fn encode_pkcs8_v2() { |
| 74 | let pk = KeypairBytes::from_pkcs8_der(PKCS8_V2_DER).unwrap(); |
| 75 | let pk2 = KeypairBytes::from_pkcs8_der(pk.to_pkcs8_der().unwrap().as_bytes()).unwrap(); |
| 76 | assert_eq!(pk.secret_key, pk2.secret_key); |
| 77 | assert_eq!(pk.public_key, pk2.public_key); |
| 78 | } |
| 79 | |
| 80 | #[cfg(feature = "alloc")] |
| 81 | #[test] |
| 82 | fn encode_public_key() { |
| 83 | let pk = PublicKeyBytes::from_public_key_der(PUBLIC_KEY_DER).unwrap(); |
| 84 | let pk_der = pk.to_public_key_der().unwrap(); |
| 85 | assert_eq!(pk_der.as_ref(), PUBLIC_KEY_DER); |
| 86 | } |