API: add failure mode support for randombytes()#689
Conversation
bc4af2c to
535e75e
Compare
21c81f1 to
562f956
Compare
|
Thank you @L-series!
So long as OQS' randombytes has return type #define MLD_CONFIG_CUSTOM_RANDOMBYTES
#if !defined(__ASSEMBLER__)
#include <oqs/rand.h>
#include <stdint.h>
#include "../../mldsa/src/sys.h"
static MLD_INLINE int mld_randombytes(uint8_t *ptr, size_t len)
{
OQS_randombytes(ptr, len);
return 0;
}or am I missing something?
Yes, I see no problem with that.
The CBMC proofs will need to cover the new behavior, but yes, we also need have specific tests that trigger failure of |
No, this is how I implemented it, it does seem like the best solution currently - however it feels slightly wrong.
I'll look into modifying the proofs and adding a testing example. |
562f956 to
cf649d3
Compare
|
Hey @L-series! How are you getting on? Let us know if you need any input. Otherwise, can you mark the PR as draft until it's ready for review? |
|
Hey @hanno-becker, sorry for the delay. Haven't had much time outside of work recently. Will mark as a draft till I fix the failing CI tests. |
3ef6eab to
7ce7aad
Compare
|
I've added a test case as well as fixed the failing CI tests. Please let me know what needs to be modified regarding the proofs as I'm not sure what to search for. |
d7dda7b to
650d981
Compare
mkannwischer
left a comment
There was a problem hiding this comment.
Thanks @L-series.
Could you please rebase on top of main - there has been some restructuring to the config.h?
ALso could you please add the new failure cases to the documentation of the top level functions in sign.h and mldsa_native.h?
cbaac8e to
33bfe92
Compare
hanno-becker
left a comment
There was a problem hiding this comment.
Thank you @L-series for pushing this, I think this will be a valuable addition!
I don't think we should have a MLD_CONFIG_RANDOMBYTES_FAILURE_TEST option. I agree that there is some similarity to the PCT breakage test, but the difference here is that randombytes is externally controlled, so an injection of error can happen through a dedicated implementation of randombytes and need not happen from the mldsa-native source.
I think we should remove MLD_CONFIG_RANDOMBYTES_FAILURE_TEST and instead have a separate test file test/test_rng_fail.c or similar, which uses a custom randombytes implementation injecting failures at various points. One way this could be achieved is by first doing a 'dry-run' of ML-DSA API from test_rng_fail.c and having the instrumented randombytes implementation merely count (in a global variable) how many times it has been invoked. And then, say there have been N invocations of randombytes, test_rng_fail.c runs the standard tests again N times, and in the i-th invocation, randombytes is configured to fail at the i-th invocation, but working beforehand. This way, we can trigger the failure handling for all invocations of randombytes.
What do you think?
33bfe92 to
c1bfb58
Compare
mkannwischer
left a comment
There was a problem hiding this comment.
Thanks @L-series. The test looks much better to me now.
Please also add it to scripts/tests - otherwise large parts of the CI don't actually run it.
Do you want some help with the CBMC proofs? I can take a look later.
c1bfb58 to
e351a73
Compare
|
Added the two missing CBMC proofs and added the test to scripts/tests. Please let me know if i should follow what is done in 34281c3 regarding the integration with CI. I saw that the |
hanno-becker
left a comment
There was a problem hiding this comment.
Thank you very much @L-series, mostly looks good! Some smaller comments.
d56cc46 to
34ae6de
Compare
mkannwischer
left a comment
There was a problem hiding this comment.
Thanks @L-series. We are almost there. A few small nits, then I am happy with this PR.
|
Please also clean-up the commit history. |
Change randombytes() to return int (0 on success, non-zero on failure) instead of void, allowing callers to detect and handle RNG failures. This commit: * Updates function signatures. * All call sites to check return values. * Changes test files to use CHECK macro. * Adds documentation of the new failure mode to sign.h and mldsa_native.h * Adds a new error code MLD_ERR_RNG_FAIL. * Declares src/randombytes with MLD_MUST_CHECK_RETURN_VALUE. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
Tests that crypto_sign_keypair, crypto_sign_signature, and crypto_sign_signature_extmu correctly return MLD_ERR_RNG_FAIL when randombytes() fails. We systematically inject failures at each invocation point. This test is based off the work from commit 0fda772. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
Add the rng failure test to the CI. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
|
@mkannwischer Cleaned up the code in accordance with your comments and I fixed the commit history to not include an autogen commit. Willl run autogen on a per-commit basis from now onwards! |
mkannwischer
left a comment
There was a problem hiding this comment.
Thanks @L-series for the persistence and changes. This looks excellent to me now
@hanno-becker, please take another look.
| define ADD_SOURCE_RNG_FAIL | ||
| $(BUILD_DIR)/$(1)/bin/test_rng_fail$(subst mldsa,,$(1)): LDLIBS += -L$(BUILD_DIR) -l$(1)_rng_fail | ||
| $(BUILD_DIR)/$(1)/bin/test_rng_fail$(subst mldsa,,$(1)): $(BUILD_DIR)/$(1)/test/test_rng_fail.c.o $(BUILD_DIR)/lib$(1)_rng_fail.a | ||
| endef |
There was a problem hiding this comment.
Do we need a custom build here? Can we not use the default build but just link against the customized randombytes?
| $(BUILD_DIR)/mldsa44/rng_fail/%.c.o: %.c $(CONFIG) | ||
| $(Q)echo " CC $@" | ||
| $(Q)[ -d $(@D) ] || mkdir -p $(@D) | ||
| $(Q)$(CC) -c -o $@ $(CFLAGS) $< |
There was a problem hiding this comment.
Same comment here: I'm not sure we need a separate build.
There was a problem hiding this comment.
Looks great, thanks a lot @L-series
The only thing that should be improved is the build: We don't need a separate build for the fail-RNG test, but can just link a different randombytes. This will avoid the build time for make test creep up even further.
I leave it to you whether you want to do this now or as a follow-up.
@L-series could you do that as a follow-up, please? Then we can already get the restructuring PRs going without conflicting with this PR. |
This PR adds failure mode support for the randombytes() interface.
Marking as draft as there are a few points for which I need clarifications. These are the following:
voidon the liboqs side. Do we make a PR to change this on their end also or do we simply modify the return type of our inlinemld_randombytesfunctions defined in filesintegration/liboqs/config_*.h?CHECK(x)macro tobench/test_components_mldsa.c?randombytes()fails?As an aside. It could be a good idea to factor our the
CHECKmacro into its own test header file as the code is currently duplicated in many files across the project. @hanno-becker @mkannwischer please let me know your thoughts.