-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
crypto: support ML-DSA KeyObject, sign, and verify #59259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
69fc44e
crypto: support ML-DSA KeyObject, sign, and verify
panva ce580f0
check vectors from ietf-cose-dilithium
panva d8d1043
fixup crypto.generateKeyPair doc type
panva 24670db
use primordial
panva aaa2d1d
add benchmarks
panva bba6de7
use native handle init for JWK public inputs
panva a3a2f52
cpp implementation of init from seed
panva 4678673
apply feedback (first of many)
panva e15987f
apply more feedback
panva 7e614d7
fixup unregistered external reference
panva 657b4b2
add benchmark for create*Key
panva fd954bf
resolve lint
panva 7b10239
lint
panva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1897,6 +1897,31 @@ EVPKeyPointer EVPKeyPointer::NewRawPrivate( | |
EVP_PKEY_new_raw_private_key(id, nullptr, data.data, data.len)); | ||
} | ||
|
||
#if OPENSSL_VERSION_MAJOR >= 3 && OPENSSL_VERSION_MINOR >= 5 | ||
EVPKeyPointer EVPKeyPointer::NewRawSeed( | ||
int id, const Buffer<const unsigned char>& data) { | ||
if (id == 0) return {}; | ||
|
||
OSSL_PARAM params[] = { | ||
OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, | ||
const_cast<unsigned char*>(data.data), | ||
data.len), | ||
OSSL_PARAM_END}; | ||
|
||
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(id, nullptr); | ||
if (ctx == nullptr) return {}; | ||
|
||
EVP_PKEY* pkey = nullptr; | ||
if (ctx == nullptr || EVP_PKEY_fromdata_init(ctx) <= 0 || | ||
EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) { | ||
EVP_PKEY_CTX_free(ctx); | ||
return {}; | ||
} | ||
|
||
return EVPKeyPointer(pkey); | ||
} | ||
#endif | ||
|
||
EVPKeyPointer EVPKeyPointer::NewDH(DHPointer&& dh) { | ||
if (!dh) return {}; | ||
auto key = New(); | ||
|
@@ -1942,7 +1967,16 @@ EVP_PKEY* EVPKeyPointer::release() { | |
|
||
int EVPKeyPointer::id(const EVP_PKEY* key) { | ||
if (key == nullptr) return 0; | ||
return EVP_PKEY_id(key); | ||
int type = EVP_PKEY_id(key); | ||
#if OPENSSL_VERSION_MAJOR >= 3 && OPENSSL_VERSION_MINOR >= 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker but, does this also need to have a /cc @codebytere |
||
// https://github.com/openssl/openssl/issues/27738#issuecomment-3013215870 | ||
if (type == -1) { | ||
if (EVP_PKEY_is_a(key, "ML-DSA-44")) return EVP_PKEY_ML_DSA_44; | ||
if (EVP_PKEY_is_a(key, "ML-DSA-65")) return EVP_PKEY_ML_DSA_65; | ||
if (EVP_PKEY_is_a(key, "ML-DSA-87")) return EVP_PKEY_ML_DSA_87; | ||
} | ||
#endif | ||
return type; | ||
} | ||
|
||
int EVPKeyPointer::base_id(const EVP_PKEY* key) { | ||
|
@@ -1998,6 +2032,31 @@ DataPointer EVPKeyPointer::rawPublicKey() const { | |
return {}; | ||
} | ||
|
||
#if OPENSSL_VERSION_MAJOR >= 3 && OPENSSL_VERSION_MINOR >= 5 | ||
DataPointer EVPKeyPointer::rawSeed() const { | ||
if (!pkey_) return {}; | ||
switch (id()) { | ||
case EVP_PKEY_ML_DSA_44: | ||
case EVP_PKEY_ML_DSA_65: | ||
case EVP_PKEY_ML_DSA_87: | ||
break; | ||
default: | ||
unreachable(); | ||
} | ||
|
||
size_t seed_len = 32; | ||
if (auto data = DataPointer::Alloc(seed_len)) { | ||
const Buffer<unsigned char> buf = data; | ||
size_t len = data.size(); | ||
if (EVP_PKEY_get_octet_string_param( | ||
get(), OSSL_PKEY_PARAM_ML_DSA_SEED, buf.data, len, &seed_len) != 1) | ||
return {}; | ||
return data; | ||
} | ||
return {}; | ||
} | ||
#endif | ||
|
||
DataPointer EVPKeyPointer::rawPrivateKey() const { | ||
if (!pkey_) return {}; | ||
if (auto data = DataPointer::Alloc(rawPrivateKeySize())) { | ||
|
@@ -2453,7 +2512,18 @@ bool EVPKeyPointer::isRsaVariant() const { | |
bool EVPKeyPointer::isOneShotVariant() const { | ||
if (!pkey_) return false; | ||
int type = id(); | ||
return type == EVP_PKEY_ED25519 || type == EVP_PKEY_ED448; | ||
switch (type) { | ||
case EVP_PKEY_ED25519: | ||
case EVP_PKEY_ED448: | ||
#if OPENSSL_VERSION_MAJOR >= 3 && OPENSSL_VERSION_MINOR >= 5 | ||
case EVP_PKEY_ML_DSA_44: | ||
case EVP_PKEY_ML_DSA_65: | ||
case EVP_PKEY_ML_DSA_87: | ||
#endif | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
bool EVPKeyPointer::isSigVariant() const { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on line 1912 this part of the condition will always be false, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'll be doing a pass on this in a follow up unless a blocking review comes in.