@@ -31,7 +31,11 @@ pub enum MlKemPrivateKey {
3131}
3232
3333// RFC 9881 Section 6.5
34- #[ cfg( any( CRYPTOGRAPHY_IS_BORINGSSL , CRYPTOGRAPHY_IS_AWSLC ) ) ]
34+ #[ cfg( any(
35+ CRYPTOGRAPHY_IS_BORINGSSL ,
36+ CRYPTOGRAPHY_IS_AWSLC ,
37+ CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
38+ ) ) ]
3539#[ derive( asn1:: Asn1Read , asn1:: Asn1Write ) ]
3640pub enum MlDsaPrivateKey {
3741 #[ implicit( 0 ) ]
@@ -55,16 +59,31 @@ pub fn mlkem_seed_from_pkey(
5559
5660/// Extract the 32-byte ML-DSA seed from a private key.
5761///
58- /// AWS-LC's `raw_private_key()` returns the expanded key, not the seed.
59- /// This function round-trips through the native PKCS#8 encoding to extract it.
60- /// https://github.com/aws/aws-lc/issues/3072
61- #[ cfg( any( CRYPTOGRAPHY_IS_BORINGSSL , CRYPTOGRAPHY_IS_AWSLC ) ) ]
62+ /// For BoringSSL/AWS-LC, round-trips through PKCS#8 encoding to extract the
63+ /// seed (AWS-LC's `raw_private_key()` returns the expanded key, not the seed:
64+ /// https://github.com/aws/aws-lc/issues/3072).
65+ ///
66+ /// For vanilla OpenSSL 3.5+, calls `PKey::seed_into` to read the seed
67+ /// directly, since OpenSSL 3.5's PKCS#8 inner encoding differs from
68+ /// BoringSSL/AWS-LC.
69+ #[ cfg( any(
70+ CRYPTOGRAPHY_IS_BORINGSSL ,
71+ CRYPTOGRAPHY_IS_AWSLC ,
72+ CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
73+ ) ) ]
6274pub fn mldsa_seed_from_pkey (
6375 pkey : & openssl:: pkey:: PKeyRef < openssl:: pkey:: Private > ,
6476) -> Result < MlDsaPrivateKey , openssl:: error:: ErrorStack > {
65- let pkcs8_der = pkey. private_key_to_pkcs8 ( ) ?;
66- let pki = asn1:: parse_single :: < PrivateKeyInfo < ' _ > > ( & pkcs8_der) . unwrap ( ) ;
67- Ok ( asn1:: parse_single :: < MlDsaPrivateKey > ( pki. private_key ) . unwrap ( ) )
77+ cfg_if:: cfg_if! {
78+ if #[ cfg( any( CRYPTOGRAPHY_IS_BORINGSSL , CRYPTOGRAPHY_IS_AWSLC ) ) ] {
79+ let pkcs8_der = pkey. private_key_to_pkcs8( ) ?;
80+ let pki = asn1:: parse_single:: <PrivateKeyInfo <' _>>( & pkcs8_der) . unwrap( ) ;
81+ Ok ( asn1:: parse_single:: <MlDsaPrivateKey >( pki. private_key) . unwrap( ) )
82+ } else if #[ cfg( CRYPTOGRAPHY_OPENSSL_350_OR_GREATER ) ] {
83+ let seed = cryptography_openssl:: mldsa:: mldsa_seed_raw( pkey) ?;
84+ Ok ( MlDsaPrivateKey :: Seed ( seed) )
85+ }
86+ }
6887}
6988
7089pub fn parse_private_key ( data : & [ u8 ] ) -> KeyParsingResult < ParsedPrivateKey > {
@@ -174,8 +193,11 @@ pub fn parse_private_key(data: &[u8]) -> KeyParsingResult<ParsedPrivateKey> {
174193 ) ?;
175194 Ok ( ParsedPrivateKey :: Pkey ( pkey) )
176195 }
177-
178- #[ cfg( any( CRYPTOGRAPHY_IS_BORINGSSL , CRYPTOGRAPHY_IS_AWSLC ) ) ]
196+ #[ cfg( any(
197+ CRYPTOGRAPHY_IS_BORINGSSL ,
198+ CRYPTOGRAPHY_IS_AWSLC ,
199+ CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
200+ ) ) ]
179201 AlgorithmParameters :: MlDsa44 => {
180202 let MlDsaPrivateKey :: Seed ( seed) = asn1:: parse_single :: < MlDsaPrivateKey > ( k. private_key ) ?;
181203 Ok ( ParsedPrivateKey :: Pkey (
@@ -186,7 +208,11 @@ pub fn parse_private_key(data: &[u8]) -> KeyParsingResult<ParsedPrivateKey> {
186208 ) )
187209 }
188210
189- #[ cfg( any( CRYPTOGRAPHY_IS_BORINGSSL , CRYPTOGRAPHY_IS_AWSLC ) ) ]
211+ #[ cfg( any(
212+ CRYPTOGRAPHY_IS_BORINGSSL ,
213+ CRYPTOGRAPHY_IS_AWSLC ,
214+ CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
215+ ) ) ]
190216 AlgorithmParameters :: MlDsa65 => {
191217 let MlDsaPrivateKey :: Seed ( seed) = asn1:: parse_single :: < MlDsaPrivateKey > ( k. private_key ) ?;
192218 Ok ( ParsedPrivateKey :: Pkey (
@@ -197,7 +223,11 @@ pub fn parse_private_key(data: &[u8]) -> KeyParsingResult<ParsedPrivateKey> {
197223 ) )
198224 }
199225
200- #[ cfg( any( CRYPTOGRAPHY_IS_BORINGSSL , CRYPTOGRAPHY_IS_AWSLC ) ) ]
226+ #[ cfg( any(
227+ CRYPTOGRAPHY_IS_BORINGSSL ,
228+ CRYPTOGRAPHY_IS_AWSLC ,
229+ CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
230+ ) ) ]
201231 AlgorithmParameters :: MlDsa87 => {
202232 let MlDsaPrivateKey :: Seed ( seed) = asn1:: parse_single :: < MlDsaPrivateKey > ( k. private_key ) ?;
203233 Ok ( ParsedPrivateKey :: Pkey (
@@ -555,8 +585,12 @@ pub fn serialize_private_key(key: &ParsedPrivateKey) -> crate::KeySerializationR
555585 } ;
556586 ( params, private_key_der)
557587 }
558- #[ cfg( any( CRYPTOGRAPHY_IS_BORINGSSL , CRYPTOGRAPHY_IS_AWSLC ) ) ]
559- id if cryptography_openssl:: mldsa:: is_mldsa_pkey_type ( id) => {
588+ #[ cfg( any(
589+ CRYPTOGRAPHY_IS_BORINGSSL ,
590+ CRYPTOGRAPHY_IS_AWSLC ,
591+ CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
592+ ) ) ]
593+ _ if cryptography_openssl:: mldsa:: is_mldsa_pkey ( pkey) => {
560594 let private_key_der = asn1:: write_single ( & mldsa_seed_from_pkey ( pkey) ?) ?;
561595 let params = match cryptography_openssl:: mldsa:: MlDsaVariant :: from_pkey ( pkey) {
562596 cryptography_openssl:: mldsa:: MlDsaVariant :: MlDsa44 => {
0 commit comments