From ea051bce5584e52a70fcb9ca84dbade8cd800916 Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Fri, 23 Oct 2026 18:53:17 +0200 Subject: [PATCH 1/2] fix(hkdf): accept a zero-length input keying material OpenSSL rejects a null key pointer even when the length is 0, so empty IKM derivation failed. Pass a non-null empty buffer instead. Co-authored-by: Cursor --- example/src/tests/hkdf/hkdf_tests.ts | 55 +++++++++++++++++++ .../cpp/hkdf/HybridHkdf.cpp | 11 ++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/example/src/tests/hkdf/hkdf_tests.ts b/example/src/tests/hkdf/hkdf_tests.ts index dfcbf151c..d7a0a88a5 100644 --- a/example/src/tests/hkdf/hkdf_tests.ts +++ b/example/src/tests/hkdf/hkdf_tests.ts @@ -413,3 +413,58 @@ test(SUITE, 'hkdfExpandSync: rejects PRK shorter than HashLen', () => { hkdfExpandSync('sha256', shortPrk, Buffer.alloc(0), 32); }).to.throw(RangeError, /at least HashLen/); }); + +// RFC 5869 ยง2.2 allows a zero-length IKM. A null key pointer makes OpenSSL +// reject the derivation ("passed a null parameter"), so these vectors lock +// the empty-buffer path. Output matches Node.js crypto.hkdfSync. +test(SUITE, 'hkdfSync: zero-length IKM and empty salt', () => { + const key = hkdfSync( + 'sha256', + Buffer.alloc(0), + Buffer.alloc(0), + Buffer.alloc(0), + 32, + ); + expect(key.toString('hex')).to.equal( + 'eb70f01dede9afafa449eee1b1286504e1f62388b3f7dd4f956697b0e828fe18', + ); +}); + +test(SUITE, 'hkdfSync: zero-length IKM with salt', () => { + const key = hkdfSync( + 'sha256', + Buffer.alloc(0), + Buffer.alloc(32, 1), + Buffer.alloc(0), + 32, + ); + expect(key.toString('hex')).to.equal( + 'b5a5789af1d00c74773ef327dc63f0f11c7041252d4916c43a252b92e3358ad2', + ); +}); + +test(SUITE, 'WebCrypto HKDF deriveBits: zero-length IKM', async () => { + const key = await crypto.subtle.importKey( + 'raw', + Buffer.alloc(0), + { name: 'HKDF' }, + false, + ['deriveBits'], + ); + + const bits = await crypto.subtle.deriveBits( + { + name: 'HKDF', + hash: 'SHA-256', + salt: Buffer.alloc(32, 1), + info: Buffer.alloc(0), + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as any, + key, + 32 * 8, + ); + + expect(Buffer.from(bits).toString('hex')).to.equal( + 'b5a5789af1d00c74773ef327dc63f0f11c7041252d4916c43a252b92e3358ad2', + ); +}); diff --git a/packages/react-native-quick-crypto/cpp/hkdf/HybridHkdf.cpp b/packages/react-native-quick-crypto/cpp/hkdf/HybridHkdf.cpp index f74d23fac..ce38194bf 100644 --- a/packages/react-native-quick-crypto/cpp/hkdf/HybridHkdf.cpp +++ b/packages/react-native-quick-crypto/cpp/hkdf/HybridHkdf.cpp @@ -63,15 +63,14 @@ std::shared_ptr HybridHkdf::deriveKeySync(const std::string& algori params[paramIndex++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, const_cast(algorithm.c_str()), 0); - // Key (Input Keying Material) + // Key (input keying material). RFC 5869 allows a zero-length IKM. OpenSSL rejects a null data + // pointer even when the length is 0 ("passed a null parameter"), so pass a non-null empty + // buffer. It must stay alive through EVP_KDF_derive, which copies the parameter. + unsigned char emptyKey = 0; if (baseKey && baseKey->size() > 0) { params[paramIndex++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, baseKey->data(), baseKey->size()); } else { - // Empty key is allowed in HKDF (defaults to zero string of hashLen) but explicit param usually expected if not null - // If we want empty, we can pass generic empty buffer or handle it. - // Node.js crypto allows buffer. - // Assuming key is effectively required or can be empty. - params[paramIndex++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, nullptr, 0); + params[paramIndex++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, &emptyKey, 0); } // Salt From 9755bfbf66fdc38a8c60eb20326e697031a77457 Mon Sep 17 00:00:00 2001 From: Brad Anderson Date: Mon, 26 Oct 2026 09:08:52 -0400 Subject: [PATCH 2/2] test: type empty HKDF derivation parameters --- example/src/tests/hkdf/hkdf_tests.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/example/src/tests/hkdf/hkdf_tests.ts b/example/src/tests/hkdf/hkdf_tests.ts index d7a0a88a5..fe9d214ce 100644 --- a/example/src/tests/hkdf/hkdf_tests.ts +++ b/example/src/tests/hkdf/hkdf_tests.ts @@ -6,6 +6,7 @@ import crypto, { hkdfExtractSync, hkdfExpand, hkdfExpandSync, + type HkdfAlgorithm, } from 'react-native-quick-crypto'; import { expect } from 'chai'; import { test } from '../util'; @@ -452,17 +453,13 @@ test(SUITE, 'WebCrypto HKDF deriveBits: zero-length IKM', async () => { ['deriveBits'], ); - const bits = await crypto.subtle.deriveBits( - { - name: 'HKDF', - hash: 'SHA-256', - salt: Buffer.alloc(32, 1), - info: Buffer.alloc(0), - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } as any, - key, - 32 * 8, - ); + const algorithm = { + name: 'HKDF' as const, + hash: 'SHA-256', + salt: Buffer.alloc(32, 1), + info: Buffer.alloc(0), + } satisfies HkdfAlgorithm; + const bits = await crypto.subtle.deriveBits(algorithm, key, 32 * 8); expect(Buffer.from(bits).toString('hex')).to.equal( 'b5a5789af1d00c74773ef327dc63f0f11c7041252d4916c43a252b92e3358ad2',