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

Update SPHINCS+ #1420

Merged
merged 10 commits into from
May 16, 2023
Merged

Update SPHINCS+ #1420

merged 10 commits into from
May 16, 2023

Conversation

dstebila
Copy link
Member

@dstebila dstebila commented Mar 18, 2023

Fixes #1249.

  • [Yes] Does this PR change the input/output behaviour of a cryptographic algorithm (i.e., does it change known answer test values)? (If so, a version bump will be required from x.y.z to x.(y+1).0.)
  • [Yes] Does this PR change the the list of algorithms available -- either adding, removing, or renaming? Does this PR otherwise change an API? (If so, PRs in oqs-provider, OQS-OpenSSL, OQS-BoringSSL, and OQS-OpenSSH will also need to be ready for review and merge by the time this is merged.)
  • TODO: Ensure all references to the removed variants (Haraka) are removed.

@dstebila dstebila added this to the 0.8.0 milestone Mar 18, 2023
@dstebila dstebila self-assigned this Mar 18, 2023
.CMake/alg_support.cmake Outdated Show resolved Hide resolved
Copy link
Member

@baentsch baentsch left a comment

Choose a reason for hiding this comment

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

Didn't we decide to drop the "robust" variants as per NIST's recommendation (#1245 (comment) )?

@dstebila
Copy link
Member Author

Didn't we decide to drop the "robust" variants as per NIST's recommendation (#1245 (comment) )?

Yes, you're right. I've removed the robust variants as well as the Haraka variants (NIST's email also says they only plan to move forward with SHA2 and SHAKE variants).

@baentsch
Copy link
Member

removed the robust variants as well as the Haraka variants

Compile runs down from 1900 to 1300 .... The environment says Thank you :)

But the compile errors still confuse me: I locally have missing symbols

/usr/bin/ld: thash_shake_robustx4.c:(.text.PQCLEAN_SPHINCSSHAKE128FROBUST_AVX2_thashx4+0x2a3): undefined reference to `KeccakP1600times4_PermuteAll_24rounds'
/usr/bin/ld: lib/liboqs.a(hash_shakex4.c.o): in function `PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_prf_addrx4':
hash_shakex4.c:(.text.PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_prf_addrx4+0x17e): undefined reference to `KeccakP1600times4_PermuteAll_24rounds'
/usr/bin/ld: lib/liboqs.a(thash_shake_simplex4.c.o): in function `PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_thashx4':
thash_shake_simplex4.c:(.text.PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_thashx4+0x237): undefined reference to `KeccakP1600times4_PermuteAll_24rounds'
/usr/bin/ld: lib/liboqs.a(hash_shakex4.c.o):hash_shakex4.c:(.text.PQCLEAN_SPHINCSSHAKE128SROBUST_AVX2_prf_addrx4+0x17e): more undefined references to `KeccakP1600times4_PermuteAll_24rounds' follow
collect2: error: ld returned 1 exit status

and the CI runs have even weirder problems. Maybe a fresh look tomorrow resolves these....

@dstebila
Copy link
Member Author

removed the robust variants as well as the Haraka variants

Compile runs down from 1900 to 1300 .... The environment says Thank you :)

But the compile errors still confuse me: I locally have missing symbols

/usr/bin/ld: thash_shake_robustx4.c:(.text.PQCLEAN_SPHINCSSHAKE128FROBUST_AVX2_thashx4+0x2a3): undefined reference to `KeccakP1600times4_PermuteAll_24rounds'
/usr/bin/ld: lib/liboqs.a(hash_shakex4.c.o): in function `PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_prf_addrx4':
hash_shakex4.c:(.text.PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_prf_addrx4+0x17e): undefined reference to `KeccakP1600times4_PermuteAll_24rounds'
/usr/bin/ld: lib/liboqs.a(thash_shake_simplex4.c.o): in function `PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_thashx4':
thash_shake_simplex4.c:(.text.PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_thashx4+0x237): undefined reference to `KeccakP1600times4_PermuteAll_24rounds'
/usr/bin/ld: lib/liboqs.a(hash_shakex4.c.o):hash_shakex4.c:(.text.PQCLEAN_SPHINCSSHAKE128SROBUST_AVX2_prf_addrx4+0x17e): more undefined references to `KeccakP1600times4_PermuteAll_24rounds' follow
collect2: error: ld returned 1 exit status

and the CI runs have even weirder problems. Maybe a fresh look tomorrow resolves these....

I did a quick pull without looking much into the changes. I recall there was a change involving removal of a Keccak-related file from the PQClean directories, but I didn't look into it. I don't think I'll get a chance to do so over the next few days, unfortunately.

@baentsch
Copy link
Member

I recall there was a change involving removal of a Keccak-related file from the PQClean directories, but I didn't look into it.

Yup -- before this PR there was a copy_from_upstream patch file for sphincs removing fips202x4.[c|h]. Seems to be gone for some reason (?). Manually doing these removals only unearthed missing includes like for immintrin.h, so some more work required...

I don't think I'll get a chance to do so over the next few days, unfortunately.

NP; so focusing just on functionality (sphincs+ update) for the IETF hackathon I opted to simply remove the AVX2 optimizations from this PR (see #1422). This solves most build problems, but for example https://github.com/open-quantum-safe/liboqs/actions/runs/4465669616/jobs/7842995392, confirms the same results on my local machine and the (optimized) M1, namely incorrect KATs for 4 algorithms:

FAILED tests/test_kat.py::test_sig[SPHINCS+-SHA256-192f-simple] - AssertionError: assert 'fd4e301339b2...2b414f962f44c' == '5302cb39e848...b4f38caa7b99d'
FAILED tests/test_kat.py::test_sig[SPHINCS+-SHA256-256s-simple] - AssertionError: assert '05d15a742539...fa529071b0748' == 'db9b817748aa...652e1c98cdbf7'
FAILED tests/test_kat.py::test_sig[SPHINCS+-SHA256-192s-simple] - AssertionError: assert '0fa07f3f7775...3924ba2713839' == '9ecda7889d92...0226ebb06daf2'
FAILED tests/test_kat.py::test_sig[SPHINCS+-SHA256-256f-simple] - AssertionError: assert 'bd88b4945316...ae1370ed13480' == '38206d5db048...e76d4ce9fca23'

--> It seems the KATs introduced with this PR are not all correct. Thus, I'm afraid we cannot use this Sphincs+ update in the IETF hackathon. Agreed, @dstebila @xvzcf ? Good ideas how to facilitate that regardless very welcome.

@xvzcf
Copy link
Contributor

xvzcf commented May 1, 2023

@baentsch @dstebila I think at least part of the problem lies with our OpenSSL SHA2 code. In the mb-remove-avx2-from-sphincs branch, compare the focal-std-openssl job triggered by my commit (the job transcript can be found here) and the job from the previous commit.

@baentsch
Copy link
Member

baentsch commented May 2, 2023

@baentsch @dstebila I think at least part of the problem lies with our OpenSSL SHA2 code. In the mb-remove-avx2-from-sphincs branch, compare the focal-std-openssl job triggered by my commit (the job transcript can be found here) and the job from the previous commit.

Interesting, indeed: Just by setting OQS_OPENSSL_OFF the test passes. It looks like our self tests then are also insufficient/not equivalent to the use of the API in liboqs. Not good.

@baentsch
Copy link
Member

baentsch commented May 9, 2023

I now started to look a bit into this issue and concluded that the use of the OQS_SHA2_sha512_inc API causes the problem, right? When trying to understand the API, I wonder why these comments are as they are (referencing SHA3 in SHA2 include comments)? Cut-copy-paste error? I didn't find similar ones in the code, but it might be worth while fixing this (@dstebila?) as part of this PR. Now keeping looking further...

@xvzcf
Copy link
Contributor

xvzcf commented May 9, 2023

I now started to look a bit into this issue and concluded that the use of the OQS_SHA2_sha512_inc API causes the problem, right?

I've been looking into it as well, and yes it's this API that seems to be changing the KAT values.

@Martyrshot
Copy link
Member

I wonder why these comments are as they are (referencing SHA3 in SHA2 include comments)?

I believe that was a fat finger on my part. Oops! Sorry!

@baentsch
Copy link
Member

I wonder why these comments are as they are (referencing SHA3 in SHA2 include comments)?

I believe that was a fat finger on my part. Oops! Sorry!

Good to know :) Will you do a PR fixing this?

Next question: Am I reading correctly that our code is double-freeing MD contexts:

void OQS_SHA2_sha512_inc_finalize(uint8_t *out, OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inlen) {
unsigned int md_len;
if (inlen > 0) {
EVP_DigestUpdate((EVP_MD_CTX *) state->ctx, in, inlen);
}
EVP_DigestFinal_ex((EVP_MD_CTX *) state->ctx, out, &md_len);
EVP_MD_CTX_free((EVP_MD_CTX *) state->ctx);
}
void OQS_SHA2_sha512_inc_ctx_release(OQS_SHA2_sha512_ctx *state) {
EVP_MD_CTX_destroy((EVP_MD_CTX *) state->ctx);
}

(when OQS_SHA2_sha512_inc_finalize and OQS_SHA2_sha512_inc_ctx_release are called in succession)? Is this intentional? (see https://www.openssl.org/docs/man3.1/man3/EVP_MD_CTX_free.html:

The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.

@baentsch
Copy link
Member

The more I read this code, the less happy I am: Many OpenSSL API calls are made without checking their return values. This is not good, considering for example

Ignoring failure returns of EVP_DigestInit_ex(), EVP_DigestInit_ex2(), or EVP_DigestInit() can lead to undefined behavior on subsequent calls updating or finalizing the EVP_MD_CTX such as the EVP_DigestUpdate() or EVP_DigestFinal() functions.

(from https://www.openssl.org/docs/man3.0/man3/EVP_DigestInit_ex.html).

Shall I do a PR for this or does anyone else volunteer?

@dstebila
Copy link
Member Author

Next question: Am I reading correctly that our code is double-freeing MD contexts:

Our API expects that you call one or the other of _inc_finalize, or _inc_ctx_release, but not both. Whether that is obeyed is a different question, of course.

The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.

It is confusing that we sometimes use _destroy and sometimes use _free. It would be be worth simplifying.

@dstebila
Copy link
Member Author

Next question: Am I reading correctly that our code is double-freeing MD contexts:

Our API expects that you call one or the other of _inc_finalize, or _inc_ctx_release, but not both. Whether that is obeyed is a different question, of course.

I suppose we could put some null sets/checks (state->ctx = null on release, and if state->ctx == null before release) to mitigate any damage if they are called twice.

@dstebila
Copy link
Member Author

The more I read this code, the less happy I am: Many OpenSSL API calls are made without checking their return values. This is not good, considering for example

Ignoring failure returns of EVP_DigestInit_ex(), EVP_DigestInit_ex2(), or EVP_DigestInit() can lead to undefined behavior on subsequent calls updating or finalizing the EVP_MD_CTX such as the EVP_DigestUpdate() or EVP_DigestFinal() functions.

(from https://www.openssl.org/docs/man3.0/man3/EVP_DigestInit_ex.html).

Shall I do a PR for this or does anyone else volunteer?

Since most of our hashing API has no return value (void), would you then be putting in something similar to OQS_EXIT_IF_NULL?

@baentsch
Copy link
Member

And another logical question: Why is SHA2_BLOCK_SIZE defined as 64 in the OpenSSL wrapper (

#define SHA2_BLOCK_SIZE 64
and
void OQS_SHA2_sha512_inc_blocks(OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inblocks) {
EVP_DigestUpdate((EVP_MD_CTX *) state->ctx, in, inblocks * SHA2_BLOCK_SIZE);
}
) while 128 bytes seem to be the length of a block used in the C implementation:
crypto_hashblocks_sha512_c(state->ctx, in, 128 * inblocks);
bytes += 128 * inblocks;
Or am I misunderstanding the C code here (didn't dive into it)?

@dstebila
Copy link
Member Author

And another logical question: Why is SHA2_BLOCK_SIZE defined as 64 in the OpenSSL wrapper (

#define SHA2_BLOCK_SIZE 64

and

void OQS_SHA2_sha512_inc_blocks(OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inblocks) {
EVP_DigestUpdate((EVP_MD_CTX *) state->ctx, in, inblocks * SHA2_BLOCK_SIZE);
}

) while 128 bytes seem to be the length of a block used in the C implementation:

crypto_hashblocks_sha512_c(state->ctx, in, 128 * inblocks);
bytes += 128 * inblocks;

Or am I misunderstanding the C code here (didn't dive into it)?

Oh! That's it! The SHA-256 block size is 64 bytes whereas the SHA-512 block size 128 bytes; in the C code it's using 64 and 128 correctly for the two different functions, but in the ossl wrapper it's using 64 everywhere.

@baentsch
Copy link
Member

The last question indeed resolves the problem with the KATs: This diff resolves the issue:

diff --git a/src/common/sha2/sha2_ossl.c b/src/common/sha2/sha2_ossl.c
index b8e71cf9..e5b69e0d 100644
-       EVP_DigestUpdate((EVP_MD_CTX *) state->ctx, in, inblocks * SHA2_BLOCK_SIZE);
+       EVP_DigestUpdate((EVP_MD_CTX *) state->ctx, in, inblocks * 2 * SHA2_BLOCK_SIZE);

@dstebila
Copy link
Member Author

The last question indeed resolves the problem with the KATs: This diff resolves the issue:

diff --git a/src/common/sha2/sha2_ossl.c b/src/common/sha2/sha2_ossl.c
index b8e71cf9..e5b69e0d 100644
-       EVP_DigestUpdate((EVP_MD_CTX *) state->ctx, in, inblocks * SHA2_BLOCK_SIZE);
+       EVP_DigestUpdate((EVP_MD_CTX *) state->ctx, in, inblocks * 2 * SHA2_BLOCK_SIZE);

Hold on, isn't the SHA384 block size also 128 bytes?

@dstebila
Copy link
Member Author

Before we get too excited, about declaring this resolved, I think we should also strengthen the OQS test programs to detect this flaw.

@baentsch
Copy link
Member

we should also strengthen the OQS test programs to detect this flaw.

Most definitely. And strengthen the way we use the OpenSSL APIs. Let's talk about this... now :)

@xvzcf xvzcf requested a review from jschanck as a code owner May 15, 2023 16:29
dstebila and others added 10 commits May 15, 2023 13:19
* correct ARM SHA3 extension addition

* correct compile option for ARM SHA

* following https://developer.arm.com/documentation/101754/0618/armclang-Reference/armclang-Command-line-Options/-march

* correct SHA3 enablement
* AVX2 test

* AVX2 test

* add patch; correct aarch64; correct documentation

* enable Keccak for Sphincs even if OpenSSL shall provide SHA3

* properly handle xkcp enablement if only specific algorithms are selected

* correct conditional setting

* re-enable XKCP for other platforms

* Windows support

* alternate pqcrystals-AES removal
@xvzcf xvzcf force-pushed the ds-update-pqclean-20230317 branch from 2b36cdd to 1f267d5 Compare May 15, 2023 17:26
@xvzcf
Copy link
Contributor

xvzcf commented May 15, 2023

If I can get a once over and 1-2 approvals on this it should be good to merge

@baentsch
Copy link
Member

If I can get a once over and 1-2 approvals on this it should be good to merge

Done. But it doesn't feel quite right that I give an approval on this as all changes done to get this to "green" were done by me in #1460. But if that got a good review, I think we're good to move forward.

@xvzcf xvzcf merged commit fd1b897 into main May 16, 2023
@dstebila dstebila deleted the ds-update-pqclean-20230317 branch May 16, 2023 14:58
@baentsch
Copy link
Member

Thanks for this merge, @xvzcf . Are you going to look into the downstreams now? For oqs-openssl111 it should be as simple as copying over generate.yml from oqs-provider (if open-quantum-safe/oqs-provider#158 still passes OK after the #1420 merge).

@xvzcf
Copy link
Contributor

xvzcf commented May 16, 2023

Thanks for this merge, @xvzcf . Are you going to look into the downstreams now? For oqs-openssl111 it should be as simple as copying over generate.yml from oqs-provider (if open-quantum-safe/oqs-provider#158 still passes OK after the #1420 merge).

I'm working on it, I'm updating the names in liboqs first and then I'll update the downstreams

iyanmv added a commit to iyanmv/wolfssl that referenced this pull request Jul 10, 2023
SPHINCS+ was updated in liboqs (open-quantum-safe/liboqs/pull/1420), and
this makes old keys incompatible with the new implementation.

New keys were generated using the oqs-provider for OpenSSL 3

openssl genpkey \
    -provider default -provider oqsprovider \
    -algorithm sphincsshake128fsimple \
    -outform der \
    -out bench_sphincs_fast_level1_key.der

And certs_test.h was updated with these new keys using xxd

xxd -i -c 10 -u bench_sphincs_fast_level1_key.der

This was repeated for the 6 variants of SPHINCS+ that wolfSSL supports.
iyanmv added a commit to iyanmv/wolfssl that referenced this pull request Jul 10, 2023
This also adds new keys for SPHINCS+. The reason is that SPHINCS+
was updated to 3.1 in liboqs (open-quantum-safe/liboqs/pull/1420),
and old keys are incompatible with the new implementation.

Keys were generated using the oqs-provider for OpenSSL 3

openssl genpkey \
    -provider default -provider oqsprovider \
    -algorithm sphincsshake128fsimple \
    -outform der \
    -out bench_sphincs_fast_level1_key.der

And certs_test.h was updated using xxd

xxd -i -c 10 -u bench_sphincs_fast_level1_key.der

This was repeated for the 6 variants of SPHINCS+ that wolfSSL supports.
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.

Pull SPHINCS+ update
4 participants