Skip to content
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

(initial) ML-DSA support #84

Merged
merged 3 commits into from
Nov 20, 2024
Merged

Conversation

bigbrett
Copy link
Contributor

@bigbrett bigbrett commented Nov 14, 2024

Adds initial support for ML-DSA to wolfHSM. Note that this requires wolfSSL/wolfssl#8177 for wolfCrypt "blind deserialization" of ML-DSA DER keys. CI tests will fail until this merges. You can manually test against this branch if desired.

This PR does not include DMA support, which will be required (due to large key/signature sizes) for this to be reasonable to use on real hardware. I'm working on that part now, but wanted initial support to be merged in first.

This PR also fixes a few bugs in the transport mem and comm context layers, where improperly sized buffers could result in buffer overflows.

@bigbrett bigbrett requested a review from billphipps November 14, 2024 16:42
@bigbrett bigbrett self-assigned this Nov 14, 2024
Copy link
Contributor

@billphipps billphipps left a comment

Choose a reason for hiding this comment

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

Minor nits that can be tweaked moving forward. Very tasty!

@@ -2072,4 +2072,492 @@ int wh_Client_CmacCancelableResponse(whClientContext* c, Cmac* cmac,
return ret;
}
#endif /* WOLFSSL_CMAC */

#ifdef HAVE_DILITHIUM
Copy link
Contributor

Choose a reason for hiding this comment

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

Yuck. This should be spelled HAVE_MLDSA or HAVE_FIPS204. No change. Just complaining

return WH_ERROR_BADARGS;
}

*outId = (whKeyId)((intptr_t)key->devCtx);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
*outId = (whKeyId)((intptr_t)key->devCtx);
*outId = WH_DEVCTX_TO_KEYID(key->devCtx);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in #86

return WH_ERROR_BADARGS;
}

key->devCtx = (void*)((intptr_t)keyId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
key->devCtx = (void*)((intptr_t)keyId);
key->devCtx = WH_KEYID_TO_DEVCTX(keyId);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in #86

return ret;
}

static int _MlDsaMakeKey(whClientContext* ctx, int size, int level,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a declaration of this at the top? I swear we were getting complaints about this in the HighTec compiler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in #86

}

int wh_Client_MlDsaMakeExportKey(whClientContext* ctx, int level, MlDsaKey* key,
int size, WC_RNG* rng)
Copy link
Contributor

Choose a reason for hiding this comment

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

RNG should be removed. The server will use its own rng.

Copy link
Contributor Author

@bigbrett bigbrett Nov 21, 2024

Choose a reason for hiding this comment

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

fixed in #86. Not sure what I was thinking here...

int ret = CRYPTOCB_UNAVAILABLE;

/* Extract info parameters */
WC_RNG* rng = info->pk.pqc_sig_kg.rng;
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for client-side rng

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in #86

word32 in_len = info->pk.pqc_sign.inlen;
byte* out = info->pk.pqc_sign.out;
word32* out_len = info->pk.pqc_sign.outlen;
WC_RNG* rng = info->pk.pqc_sign.rng;
Copy link
Contributor

Choose a reason for hiding this comment

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

No client-side rng needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in #86

@@ -108,6 +108,11 @@ int wh_CommClient_SendRequest(whCommClient* context, uint16_t magic,
return WH_ERROR_BADARGS;
}

/* Check if the data size is within allowed limits */
if (data_size > WOLFHSM_CFG_COMM_DATA_LEN) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How did we miss this?? I assume the lower-level does a sanity check but we should have caught it here. Nice!

whNvmMetadata* cacheMeta;
uint16_t der_size;

const uint16_t MAX_MLDSA_DER_SIZE = 5000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this defined somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in #86


#define WOLFHSM_CFG_NVM_OBJECT_COUNT 30
#define WOLFHSM_CFG_SERVER_KEYCACHE_COUNT 9
#define WOLFHSM_CFG_SERVER_KEYCACHE_BUFSIZE 300
#define WOLFHSM_CFG_SERVER_KEYCACHE_BIG_COUNT 2
#define WOLFHSM_CFG_SERVER_KEYCACHE_BIG_BUFSIZE 1280
#define WOLFHSM_CFG_SERVER_KEYCACHE_BIG_BUFSIZE WOLFHSM_CFG_COMM_DATA_LEN
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a clever change. :)

@billphipps billphipps merged commit 77bebc4 into wolfSSL:main Nov 20, 2024
2 checks passed
@bigbrett bigbrett mentioned this pull request Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants