-
Notifications
You must be signed in to change notification settings - Fork 13
Add Pure ML-DSA to the SymCrypt provider #161
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
mamckee
wants to merge
18
commits into
main
Choose a base branch
from
mamckee-ml-dsa
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.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8622484
ML-DSA stubs
mamckee 39e2ed6
Keymgmt function stubs
mamckee a3455ca
ML-DSA key management
mamckee 97b881d
ML-DSA signatures
mamckee e4540d9
ML-DSA tests
mamckee 853338b
ML-DSA decoder
mamckee 9cb196d
ML-DSA encoder
mamckee e060c56
Fix compat with OpenSSL 3.3.5
mamckee 36a4663
Move ML-DSA test files
mamckee a2fae87
Cleanup. Use fallthrough comment instead of attribute
mamckee c2262d2
Add TLS SIGALG capability
mamckee 018a75f
Add missing ml-dsa param
mamckee 085040c
PR comments
mamckee 4e0252e
Additional fixes to encoder interface
mamckee bd7394a
PR comments
mamckee 914e306
PR comments
mamckee c3079b8
Fix type for OSSL_PKEY_PARAM_MAX_SIZE
mamckee 506c950
PR comments
mamckee 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // | ||
| // Copyright (c) Microsoft Corporation. Licensed under the MIT license. | ||
| // | ||
|
|
||
| #include "scossl_provider.h" | ||
| #include "p_scossl_decode_common.h" | ||
| #include "keymgmt/p_scossl_mldsa_keymgmt.h" | ||
|
|
||
| #include <openssl/proverr.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| static SCOSSL_MLDSA_KEY_CTX *p_scossl_mldsa_decode_key_bytes(ossl_unused SCOSSL_DECODE_CTX *ctx, _In_ const ASN1_OBJECT *algorithm, | ||
| SYMCRYPT_MLDSA_PARAMS mldsaParams, SYMCRYPT_MLDSAKEY_FORMAT format, | ||
| _In_reads_bytes_(cbKey) PCBYTE pbKey, SIZE_T cbKey) | ||
| { | ||
| SCOSSL_MLDSA_KEY_CTX *keyCtx = NULL; | ||
| SCOSSL_STATUS status = SCOSSL_FAILURE; | ||
| SCOSSL_MLDSA_ALG_INFO *algInfo = NULL; | ||
|
|
||
| if (pbKey == NULL || cbKey == 0) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| if ((algInfo = p_scossl_mldsa_get_alg_info_by_nid(OBJ_obj2nid(algorithm))) == NULL) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| if (mldsaParams != algInfo->mldsaParams) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| if ((keyCtx = p_scossl_mldsa_keymgmt_new_ctx(mldsaParams)) == NULL) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| status = p_scossl_mldsa_keymgmt_set_encoded_key(keyCtx, format, pbKey, cbKey); | ||
|
|
||
| cleanup: | ||
| if (status != SCOSSL_SUCCESS) | ||
| { | ||
| p_scossl_mldsa_keymgmt_free_key_ctx(keyCtx); | ||
| keyCtx = NULL; | ||
| } | ||
|
|
||
| return keyCtx; | ||
| } | ||
|
|
||
| static SCOSSL_MLDSA_KEY_CTX *p_scossl_PrivateKeyInfo_to_mldsa(_In_ SCOSSL_DECODE_CTX *ctx, SYMCRYPT_MLDSA_PARAMS mldsaParams, _In_ BIO *bio) | ||
| { | ||
| PKCS8_PRIV_KEY_INFO *p8Info = NULL; | ||
| const ASN1_OBJECT *algorithm; | ||
| const unsigned char *pbKey; | ||
| int cbKey; | ||
| ASN1_OCTET_STRING *p8Data = NULL; | ||
| SCOSSL_MLDSA_KEY_CTX *keyCtx = NULL; | ||
| SYMCRYPT_MLDSAKEY_FORMAT format; | ||
|
|
||
| if (d2i_PKCS8_PRIV_KEY_INFO_bio(bio, &p8Info) == NULL || | ||
| !PKCS8_pkey_get0(&algorithm, &pbKey, &cbKey, NULL, p8Info) || | ||
| d2i_ASN1_OCTET_STRING(&p8Data, &pbKey, cbKey) == NULL) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| cbKey = ASN1_STRING_length(p8Data); | ||
|
|
||
| format = cbKey == 64 ? SYMCRYPT_MLDSAKEY_FORMAT_PRIVATE_SEED : SYMCRYPT_MLDSAKEY_FORMAT_PRIVATE_KEY; | ||
mamckee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| keyCtx = p_scossl_mldsa_decode_key_bytes(ctx, algorithm, | ||
| mldsaParams, format, | ||
| ASN1_STRING_get0_data(p8Data), ASN1_STRING_length(p8Data)); | ||
|
|
||
| cleanup: | ||
| ASN1_OCTET_STRING_free(p8Data); | ||
| PKCS8_PRIV_KEY_INFO_free(p8Info); | ||
|
|
||
| return keyCtx; | ||
| } | ||
|
|
||
| static SCOSSL_MLDSA_KEY_CTX *p_scossl_SubjectPublicKeyInfo_to_mldsa(_In_ SCOSSL_DECODE_CTX *ctx, SYMCRYPT_MLDSA_PARAMS mldsaParams, _In_ BIO *bio) | ||
| { | ||
| OSSL_LIB_CTX *libCtx = ctx->provctx == NULL ? NULL : ctx->provctx->libctx; | ||
| SUBJECT_PUBKEY_INFO *subjPubKeyInfo = NULL; | ||
| const ASN1_OBJECT *algorithm; | ||
| SCOSSL_MLDSA_KEY_CTX *keyCtx = NULL; | ||
|
|
||
| if ((subjPubKeyInfo = OPENSSL_zalloc(sizeof(SUBJECT_PUBKEY_INFO))) == NULL) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| if (ASN1_item_d2i_bio_ex(p_scossl_decode_subject_pubkey_asn1_item(), bio, (ASN1_VALUE **)&subjPubKeyInfo, libCtx, NULL) == NULL) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING); | ||
| goto cleanup; | ||
| } | ||
|
|
||
| X509_ALGOR_get0(&algorithm, NULL, NULL, subjPubKeyInfo->x509Alg); | ||
|
|
||
| keyCtx = p_scossl_mldsa_decode_key_bytes(ctx, algorithm, | ||
| mldsaParams, SYMCRYPT_MLDSAKEY_FORMAT_PUBLIC_KEY, | ||
| ASN1_STRING_get0_data(subjPubKeyInfo->subjectPublicKey), ASN1_STRING_length(subjPubKeyInfo->subjectPublicKey)); | ||
|
|
||
| cleanup: | ||
| ASN1_item_free((ASN1_VALUE *)subjPubKeyInfo, p_scossl_decode_subject_pubkey_asn1_item()); | ||
|
|
||
| return keyCtx; | ||
| } | ||
|
|
||
| static SCOSSL_STATUS p_scossl_der_to_mldsa_export_object(_In_ SCOSSL_DECODE_CTX *ctx, | ||
| _In_reads_bytes_(cbObjRef) const void *pbObjRef, _In_ size_t cbObjRef, | ||
| _In_ OSSL_CALLBACK *exportCb, _In_ void *exportCbArg) | ||
| { | ||
| SCOSSL_MLDSA_KEY_CTX *keyCtx = *(SCOSSL_MLDSA_KEY_CTX **)pbObjRef; | ||
|
|
||
| if (cbObjRef != sizeof(SCOSSL_MLDSA_KEY_CTX *) || keyCtx == NULL) | ||
| { | ||
| ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | ||
| return SCOSSL_FAILURE; | ||
| } | ||
|
|
||
| return p_scossl_mldsa_keymgmt_export(keyCtx, ctx->desc->selection, exportCb, exportCbArg); | ||
| } | ||
|
|
||
| #define SCOSSL_MAKE_MLDSA_DECODER(decoderType, bits) \ | ||
| static SCOSSL_MLDSA_KEY_CTX \ | ||
| *p_scossl_##decoderType##_to_mldsa##bits(_In_ SCOSSL_DECODE_CTX *ctx, \ | ||
| _In_ BIO *bio) \ | ||
| { \ | ||
| return p_scossl_##decoderType##_to_mldsa(ctx, SYMCRYPT_MLDSA_PARAMS_MLDSA##bits, bio); \ | ||
| } \ | ||
| \ | ||
| static const SCOSSL_DECODE_KEYTYPE_DESC p_scossl_mldsa##bits##_##decoderType##_desc = { \ | ||
| "ML-DSA-"#bits, \ | ||
| select_##decoderType, \ | ||
| (PSCOSSL_DECODE_INTERNAL_FN)p_scossl_##decoderType##_to_mldsa##bits, \ | ||
| (OSSL_FUNC_keymgmt_free_fn *)p_scossl_mldsa_keymgmt_free_key_ctx}; \ | ||
| \ | ||
| static SCOSSL_DECODE_CTX * \ | ||
| p_scossl_der_to_mldsa##bits##_##decoderType##_newctx(_In_ SCOSSL_PROVCTX *provctx) \ | ||
| { \ | ||
| return p_scossl_decode_newctx( \ | ||
| provctx, \ | ||
| &p_scossl_mldsa##bits##_##decoderType##_desc); \ | ||
| } \ | ||
| \ | ||
| static BOOL \ | ||
| p_scossl_der_to_mldsa##bits##_##decoderType##_does_selection( \ | ||
| ossl_unused void *provctx, \ | ||
| int selection) \ | ||
| { \ | ||
| return p_scossl_decode_does_selection( \ | ||
| &p_scossl_mldsa##bits##_##decoderType##_desc, \ | ||
| selection); \ | ||
| } \ | ||
| \ | ||
| const OSSL_DISPATCH p_scossl_der_to_mldsa##bits##_##decoderType##_functions[] = { \ | ||
| {OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))p_scossl_der_to_mldsa##bits##_##decoderType##_newctx}, \ | ||
| {OSSL_FUNC_DECODER_FREECTX, (void (*)(void))p_scossl_decode_freectx}, \ | ||
| {OSSL_FUNC_DECODER_SET_CTX_PARAMS, (void (*)(void))p_scossl_decode_set_ctx_params}, \ | ||
| {OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, (void (*)(void))p_scossl_decode_settable_ctx_params}, \ | ||
| {OSSL_FUNC_DECODER_DOES_SELECTION, (void (*)(void)) \ | ||
| p_scossl_der_to_mldsa##bits##_##decoderType##_does_selection}, \ | ||
| {OSSL_FUNC_DECODER_DECODE, (void (*)(void))p_scossl_decode}, \ | ||
| {OSSL_FUNC_DECODER_EXPORT_OBJECT, (void (*)(void))p_scossl_der_to_mldsa_export_object}, \ | ||
| {0, NULL}}; | ||
|
|
||
| SCOSSL_MAKE_MLDSA_DECODER(PrivateKeyInfo, 44); | ||
| SCOSSL_MAKE_MLDSA_DECODER(SubjectPublicKeyInfo, 44); | ||
| SCOSSL_MAKE_MLDSA_DECODER(PrivateKeyInfo, 65); | ||
| SCOSSL_MAKE_MLDSA_DECODER(SubjectPublicKeyInfo, 65); | ||
| SCOSSL_MAKE_MLDSA_DECODER(PrivateKeyInfo, 87); | ||
| SCOSSL_MAKE_MLDSA_DECODER(SubjectPublicKeyInfo, 87); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
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.
Uh oh!
There was an error while loading. Please reload this page.