generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 144
Add EVP_PKEY_check and EVP_PKEY_public_check for KEMs #2709
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
Open
jakemas
wants to merge
10
commits into
aws:main
Choose a base branch
from
jakemas:mlkem-evp-pkey-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+497
−6
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1d332de
Add EVP_PKEY_check and EVP_PKEY_public_check for KEMs
jakemas a09e674
remove extern and fix up documentation
jakemas 9f59888
Merge branch 'main' into mlkem-evp-pkey-check
jakemas 1fa9fe8
Merge branch 'main' into mlkem-evp-pkey-check
jakemas d24a116
add PCT tests to KEM check
jakemas 0616873
drafted public key check function
jakemas f30bfd4
cleaner solution and more negative testing
jakemas 5b01330
doc nit
jakemas c419507
Merge branch 'main' into mlkem-evp-pkey-check
jakemas 416f603
remove unnecessary tests
jakemas 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
Some comments aren't visible on the classic Files Changed page.
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 |
---|---|---|
|
@@ -306,3 +306,75 @@ int KEM_KEY_set_raw_key(KEM_KEY *key, const uint8_t *in_public, | |
|
||
return 1; | ||
} | ||
|
||
int KEM_check_key(const KEM_KEY *key) { | ||
if (key == NULL) { | ||
OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER); | ||
return 0; | ||
} | ||
|
||
// Check that the KEM method and parameters are valid | ||
if (key->kem == NULL || key->kem->method == NULL) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); | ||
return 0; | ||
} | ||
|
||
// Check that at least the public key exists | ||
if (key->public_key == NULL) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); | ||
return 0; | ||
} | ||
|
||
// Call appropriate ML-KEM check functions based on KEM NID | ||
switch (key->kem->nid) { | ||
case NID_MLKEM512: | ||
case NID_KYBER512_R3: | ||
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. I don't think we should mix-up Kyber with ML-KEM, technically they are not the same algorithms. I'd drop Kyber NIDs from here. |
||
// Check public key validity | ||
if (ml_kem_512_check_pk(key->public_key) != 0) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); | ||
return 0; | ||
} | ||
// Check secret key validity if present | ||
if (key->secret_key != NULL && ml_kem_512_check_sk(key->secret_key) != 0) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); | ||
return 0; | ||
} | ||
break; | ||
justsmth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
case NID_MLKEM768: | ||
case NID_KYBER768_R3: | ||
// Check public key validity | ||
if (ml_kem_768_check_pk(key->public_key) != 0) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); | ||
return 0; | ||
} | ||
// Check secret key validity if present | ||
if (key->secret_key != NULL && ml_kem_768_check_sk(key->secret_key) != 0) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); | ||
return 0; | ||
} | ||
break; | ||
|
||
case NID_MLKEM1024: | ||
case NID_KYBER1024_R3: | ||
// Check public key validity | ||
if (ml_kem_1024_check_pk(key->public_key) != 0) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); | ||
return 0; | ||
} | ||
// Check secret key validity if present | ||
if (key->secret_key != NULL && ml_kem_1024_check_sk(key->secret_key) != 0) { | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); | ||
return 0; | ||
} | ||
break; | ||
|
||
default: | ||
// For unsupported KEM variants | ||
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | ||
return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
|
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.
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.
NP: It's currently possible (although I wish it weren't) for the secret_key to be set but not the public_key. For those, this would always return an error.
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.
Currently intended behaviour, my idea is to merge the PRs first that ensure we can populate both. Then we fail this check if pub_key isn't set when secrect_key is? How does that sound?
Uh oh!
There was an error while loading. Please reload this page.
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.
Once caveat is that
EVP_PKEY_public_check
also calls the same function e.g.EC_KEY_check_key
that just handles it. See here. Little bit strange, but if we follow the pattern, we'd have to make KEM_CHECK ok with pub only. Or we could implement two functions, the secondKEM_CHECK_PUBLIC_KEY
and call the it fromEVP_PKEY_public_check
: drafted that idea out here 0616873, but I don't think I like the anti-pattern. Looking at EC check for inspo, I will implement similar to how they do it, If only public key: validates public key only, if secret key is present: requires public key and validates public key, secret key, and PCT.Uh oh!
There was an error while loading. Please reload this page.
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.
I've landed on f30bfd4 that preserves functionality with other
EVP_PKEY_public_check/EVP_PKEY_check
functions. As documented, itI added negative testing for a bunch of cases, including the one you mention: