1 //! SEC1 private key tests
2 
3 #![cfg(feature = "der")]
4 
5 use der::asn1::ObjectIdentifier;
6 use hex_literal::hex;
7 use sec1::{EcParameters, EcPrivateKey};
8 
9 #[cfg(feature = "alloc")]
10 use der::Encode;
11 
12 /// NIST P-256 SEC1 private key encoded as ASN.1 DER.
13 ///
14 /// Note: this key is extracted from the corresponding `p256-priv.der`
15 /// example key in the `pkcs8` crate.
16 const P256_DER_EXAMPLE: &[u8] = include_bytes!("examples/p256-priv.der");
17 
18 #[test]
decode_p256_der()19 fn decode_p256_der() {
20     let key = EcPrivateKey::try_from(P256_DER_EXAMPLE).unwrap();
21 
22     // Extracted using:
23     // $ openssl asn1parse -in tests/examples/p256-priv.pem
24     assert_eq!(
25         key.private_key,
26         hex!("69624171561A63340DE0E7D869F2A05492558E1A04868B6A9F854A866788188D")
27     );
28     assert_eq!(
29         key.parameters,
30         Some(EcParameters::NamedCurve(
31             ObjectIdentifier::new("1.2.840.10045.3.1.7").unwrap()
32         ))
33     );
34     assert_eq!(key.public_key, Some(hex!("041CACFFB55F2F2CEFD89D89EB374B2681152452802DEEA09916068137D839CF7FC481A44492304D7EF66AC117BEFE83A8D08F155F2B52F9F618DD447029048E0F").as_ref()));
35 }
36 
37 #[cfg(feature = "alloc")]
38 #[test]
encode_p256_der()39 fn encode_p256_der() {
40     let key = EcPrivateKey::try_from(P256_DER_EXAMPLE).unwrap();
41     let key_encoded = key.to_der().unwrap();
42     assert_eq!(P256_DER_EXAMPLE, key_encoded);
43 }
44