Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions example/src/tests/hkdf/hkdf_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import crypto, {
hkdfExtractSync,
hkdfExpand,
hkdfExpandSync,
type HkdfAlgorithm,
} from 'react-native-quick-crypto';
import { expect } from 'chai';
import { test } from '../util';
Expand Down Expand Up @@ -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',
);
});
11 changes: 5 additions & 6 deletions packages/react-native-quick-crypto/cpp/hkdf/HybridHkdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ std::shared_ptr<ArrayBuffer> HybridHkdf::deriveKeySync(const std::string& algori

params[paramIndex++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, const_cast<char*>(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
Expand Down
Loading