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

JCE: prepend zero byte to DH shared secret if less than prime length #69

Merged
merged 1 commit into from
Mar 30, 2024

Conversation

cconlon
Copy link
Member

@cconlon cconlon commented Mar 19, 2024

This PR adjusts wolfJCE's KeyAgreement implementation for generating DH shared secrets.

Native wolfCrypt generates and returns DH shared secrets that have any zero bytes stripped off. This follows RFC 5246 (8.1.2) which instructs to do so. Other Java KeyAgreement implementations (SunJCE, Bouncy Castle) for DH follow RFC 2631 (2.1.2) which prepends zero bytes if the total secret size is less than the prime length.

This PR adjusts our wolfJCE implementation to match other existing Java implementations, for interop compatibility.

This fixes a sporadic JUnit failure which looks similar to this:

Testcase: testDHKeyAgreementInterop took 0.016 sec
	FAILED
array lengths differed, expected.length=63 actual.length=64; arrays first differed at element [0]; expected:<-18> but was:<0>
junit.framework.AssertionFailedError: array lengths differed, expected.length=63 actual.length=64; arrays first differed at element [0]; expected:<-18> but was:<0>
	at com.wolfssl.provider.jce.test.WolfCryptKeyAgreementTest.testDHKeyAgreementInterop(WolfCryptKeyAgreementTest.java:333)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
Caused by: java.lang.AssertionError: expected:<-18> but was:<0>

------------- Standard Output ---------------
secretA.length != secretB.length
secretA (wolfJCE, 63 bytes):
eed8b5ff4038cb32bff900b4596b8992e6c6d3f3f98f68155c5897eaa5762837af14989f741f8d55ea36b2ceb17d04530abd54bd5ee5219006950bb0569cfc
secretB (SunJCE, 64 bytes):
00eed8b5ff4038cb32bff900b4596b8992e6c6d3f3f98f68155c5897eaa5762837af14989f741f8d55ea36b2ceb17d04530abd54bd5ee5219006950bb0569cfc
------------- ---------------- ---------------

@douzzer douzzer merged commit 4ac446e into wolfSSL:master Mar 30, 2024
32 checks passed
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.

4 participants