-
Notifications
You must be signed in to change notification settings - Fork 142
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
// 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; | ||
Comment on lines
+333
to
+342
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. We would also need a pair-consistency check, otherwise the secret and public keys might be unrelated. (?) 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. Good idea -- I'll have to make changes upstream in mlkem-native to support this at it would require exposing/namspacing the pct tests, i.e. would need to add
to and some changes to make sure they are available, as currently the PCTs are only defined in FIPS builds (when ooor we avoid all of that, and could just do a cheeky little encap decap in this function itself? I'll go check out the other algorithm variants of this function. |
||
|
||
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; | ||
} | ||
|
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?