Skip to content

ParsedPrivateKey/ParsedPublicKey enums in key parsing#14593

Merged
alex merged 5 commits intopyca:mainfrom
trail-of-forks:parse-key-refactor
Apr 7, 2026
Merged

ParsedPrivateKey/ParsedPublicKey enums in key parsing#14593
alex merged 5 commits intopyca:mainfrom
trail-of-forks:parse-key-refactor

Conversation

@DarkaMaul
Copy link
Copy Markdown
Contributor

The key parsing functions currently return openssl::pkey::PKey directly, which assumes all parsed keys can be represented as an OpenSSL EVP_PKEY.

This does not hold for ML-KEM, where the 64-byte seed is required for serialization but cannot be recovered from the EVP_PKEY. AWS-LC's kem_priv_encode writes the expanded secret key (2400 bytes) into PKCS#8 - not the seed. While kem_priv_decode can read both the seed and expanded key formats, the seed is lost after key generation and cannot be extracted back from the PKey.

Add ParsedPrivateKey and ParsedPublicKey enums with a single Pkey variant for now.

let pkey = parsers.iter().find_map(|parser| match parser(data) {
let parsed = parsers.iter().find_map(|parser| match parser(data) {
Ok(key) => Some(Ok(key)),
// Try next parser
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing these comments?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored them.

}

pub type KeyParsingResult<T> = Result<T, KeyParsingError>;
pub type PrivateKeyParser = fn(&[u8]) -> KeyParsingResult<ParsedPrivateKey>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make much sense to export this type alias here, it's just a random function signature.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to keys.rs as an inline type (but that's required as a separate type because clippy complains otherwise).

@alex alex merged commit 751207f into pyca:main Apr 7, 2026
68 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants