diff --git a/example/src/tests/hkdf/hkdf_tests.ts b/example/src/tests/hkdf/hkdf_tests.ts index dfcbf151c..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'; @@ -413,3 +414,54 @@ 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 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', + ); +}); 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