Skip to content

Fix/hkdf null salt openssl3#76

Open
seanjin99 wants to merge 3 commits intordkcentral:mainfrom
seanjin99:fix/hkdf-null-salt-openssl3
Open

Fix/hkdf null salt openssl3#76
seanjin99 wants to merge 3 commits intordkcentral:mainfrom
seanjin99:fix/hkdf-null-salt-openssl3

Conversation

@seanjin99
Copy link
Copy Markdown
Contributor

@seanjin99 seanjin99 commented Mar 6, 2026

Summary

Fix 8 DH/ECDH key exchange test failures on OpenSSL 3.x when useSalt=SEC_FALSE.

Problem

The hkdf() function in exchange.cpp passes nullptr to
EVP_PKEY_CTX_set1_hkdf_salt() when no salt is used. OpenSSL 3.x rejects
a NULL pointer even with length 0, causing all testKeyExchangeDH and
testKeyExchangeECDH tests with SEC_FALSE to fail.

Fix

Replace nullptr with "" (empty string) for the no-salt case. This provides
a valid non-NULL pointer with length 0, which both OpenSSL 1.1.x and 3.x accept.
The version-specific #if/#else guard for this call is no longer needed and
has been removed.

Also added descriptive comments to all remaining #if/#else/#endif blocks.

Testing

Verified 8/8 affected tests pass on:

  • OpenSSL 1.1.1w
  • OpenSSL 3.0.15
  • OpenSSL 3.0.18
  • OpenSSL 3.6.1

Fixes #75

Test Results: DH/ECDH Key Exchange (Tests 1193–1207)

Affected Tests

Test # Function Key Type Salt Type
1193 testKeyExchangeDH AES_128 SEC_FALSE DH
1195 testKeyExchangeDH AES_256 SEC_FALSE DH
1197 testKeyExchangeDH HMAC_128 SEC_FALSE DH
1199 testKeyExchangeDH HMAC_256 SEC_FALSE DH
1201 testKeyExchangeECDH AES_128 SEC_FALSE ECDH
1203 testKeyExchangeECDH AES_256 SEC_FALSE ECDH
1205 testKeyExchangeECDH HMAC_128 SEC_FALSE ECDH
1207 testKeyExchangeECDH HMAC_256 SEC_FALSE ECDH

Results Matrix

Without fix (main branch)

Platform OpenSSL Result
macOS (ARM64) 1.1.1w 8/8 passed
macOS (ARM64) 3.0.15 8/8 passed
macOS (ARM64) 3.0.18 0/8 passed
macOS (ARM64) 3.6.1 0/8 passed
ARM32 device 3.0.18 0/8 passed

With fix (PR #76)

Platform OpenSSL Result
macOS (ARM64) 1.1.1w 8/8 passed
macOS (ARM64) 3.0.15 8/8 passed
macOS (ARM64) 3.0.18 8/8 passed
macOS (ARM64) 3.6.1 8/8 passed
ARM32 device 3.0.18 8/8 passed
ARM32 device 3.6.1 8/8 passed

@seanjin99 seanjin99 force-pushed the fix/hkdf-null-salt-openssl3 branch 3 times, most recently from de88a89 to 4e74881 Compare March 6, 2026 21:35
…ests

OpenSSL 3.x rejects a NULL pointer passed to EVP_PKEY_CTX_set1_hkdf_salt()
even with length 0. Use an empty string "" instead of nullptr when
useSalt is false. This works on both OpenSSL 1.1.x and 3.x.

Also added version-guard comments to all #if/#else/#endif blocks for clarity.

Fixes rdkcentral#75
@seanjin99 seanjin99 force-pushed the fix/hkdf-null-salt-openssl3 branch from 4e74881 to 610caf8 Compare March 6, 2026 22:33
Comment thread test/main/cpp/exchange.cpp
@riwoh riwoh requested a review from mhabrat March 11, 2026 14:10
Copilot AI review requested due to automatic review settings April 13, 2026 18:27
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes HKDF salt handling in the OpenSSL-backed hkdf() helper used by DH/ECDH key exchange tests so that the no-salt (useSalt=SEC_FALSE) path works on OpenSSL 3.x.

Changes:

  • Avoid passing a NULL salt pointer to EVP_PKEY_CTX_set1_hkdf_salt() by using an empty string pointer with length 0.
  • Remove the OpenSSL-version conditional around the salt setter call.
  • Update the file copyright header year range.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +396 to 401
// OpenSSL 3.x OSSL_PARAM_construct_octet_string() rejects NULL data pointer
// even when length is 0, causing EVP_PKEY_CTX_set1_hkdf_salt() to fail.
// Pass "" instead of nullptr to satisfy the non-NULL check.
if (EVP_PKEY_CTX_set1_hkdf_salt(pctx,
reinterpret_cast<const unsigned char*>(use_salt ? "salt" : ""),
use_salt ? 4 : 0) <= 0) {
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

The PR description says descriptive comments were added to all remaining #if/#else/#endif blocks, but in this file there are still preprocessor blocks without explanatory comments (e.g., the #if OPENSSL_VERSION_NUMBER >= 0x30000000 around EVP_PKEY_CTX_add1_hkdf_info later in hkdf()). Either update the PR description to match the actual change set, or add the missing comments for consistency.

Copilot uses AI. Check for mistakes.
// Pass "" instead of nullptr to satisfy the non-NULL check.
if (EVP_PKEY_CTX_set1_hkdf_salt(pctx,
reinterpret_cast<const unsigned char*>(use_salt ? "salt" : ""),
use_salt ? 4 : 0) <= 0) {
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

use_salt ? 4 : 0 hard-codes the length of the string literal "salt". To avoid accidental mismatches if the literal changes (and to mirror common practice elsewhere), prefer deriving the length from the literal (e.g., sizeof("salt") - 1) or a named constant.

Suggested change
use_salt ? 4 : 0) <= 0) {
use_salt ? sizeof("salt") - 1 : 0) <= 0) {

Copilot uses AI. Check for mistakes.
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.

Bug: DH/ECDH key exchange tests fail on OpenSSL 3.x when salt is not used

3 participants