Skip to content

Commit 7decad0

Browse files
committed
Store an empty array for a null otherInfo in the KEMKDFSpec constructor, so that getOtherInfo() never returns null and the KMAC-128, KMAC-256 and SHAKE-256 branches of KdfUtil.makeKeyBytes no longer throw NullPointerException out of a KEM operation for a spec built through the protected constructor rather than a Builder, as the deprecated KEMParameterSpec is.
1 parent ef33f9d commit 7decad0

3 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h2>2.0 Release History</h2>
2323
Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2026, TBD
2424
<h3>2.1.2 Defects Fixed</h3>
2525
<ul>
26+
<li>The org.bouncycastle.jcajce.spec.KEMKDFSpec constructor stored a null otherInfo as given, so getOtherInfo() returned null, and three of the KDF branches KdfUtil.makeKeyBytes dispatches to - KMAC-128, KMAC-256 and SHAKE-256 - read the otherInfo length without a guard and threw NullPointerException out of the KEM operation, where the KDF2, KDF3 and HKDF branches tolerate a null through KDFParameters / HKDFParameters. The Builder of every spec in the package already mapped null to empty, so no provider path reached it, but the constructor is protected on a public class and KdfUtil is documented for callers building their own KEM integration; the deprecated KEMParameterSpec passes a null itself and escaped only because it also pins the KDF to null. The constructor now stores empty for a null, so getOtherInfo() never returns null, and a null and an explicitly empty otherInfo derive the same key.</li>
2627
<li>QR-UOV signature verification accepted a signature encoding that was not canonical, so the encoding of a signature was not unique even after the trailing-byte fix of github #2403. Each F_q element of the signature is stored in ceil(log2 q) bits, one more bit pattern than the field has elements: q itself is representable and is arithmetically congruent to zero, so an element written as q verified exactly as the same element written as zero would, and the bits padding the last element out to the byte boundary were never read at all. Every zero element of a signature therefore carried a second encoding, and for the q = 7 parameter sets roughly one element in seven is zero - a single qruov_5_q7_L10 signature measured 256 spare bits, so on the order of 2^256 distinct byte strings verified for the one message and key. Verification now rejects any element outside [0, q) and any set padding bit; a signature produced by this or by the reference implementation is unaffected, as the KAT vectors of every parameter set confirm. (github #2403)</li>
2728
<li>SNOVA signature verification ignored four bits inside the signature for any parameter set whose solution is an odd number of GF(16) nibbles - the SNOVA_24_5_5, SNOVA_25_8_3, SNOVA_29_6_5 and SNOVA_66_15_3 families, sixteen of the forty-four parameter sets. The last byte of the encoded solution carries a single nibble and the signer leaves the top four bits zero, but the decoder did not read them, so sixteen distinct byte strings verified for one signature. This is the same non-unique encoding github #2403 closed for bytes following the signature, applied inside it; the verifier now requires those bits to be zero. (github #2403)</li>
2829
<li>SnovaPrivateKeyParameters did not validate the length of the private key encoding handed to it - the only one of the five schemes of github #2403 that did not - and SnovaParameters.getPrivateKeyLength() reported the expanded ("ESK") length even for a parameter set whose private key is the seed pair. A private key encoding reaches this constructor straight from a PKCS#8 blob, so a wrong length went undetected: a seed-form key with extra bytes appended was accepted and signed under a different derived key, and a short expanded-form key sized the signer's decode buffer negatively, throwing NegativeArraySizeException out of generateSignature() rather than being reported at construction. Related, the signing retry loop could not terminate: the vinegar values are derived from a single-byte counter, so only 256 distinct linear systems can be tried, and an expanded-form private key that is not a real central map is singular for all of them - generateSignature() then span forever rather than failing. The length is now checked at construction, getPrivateKeyLength() reports the length that parameter set's private key actually has, and the retry loop gives up after its 256 attempts as MAYO's does.</li>

‎prov/src/main/java/org/bouncycastle/jcajce/spec/KEMKDFSpec.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,34 @@
55

66
public class KEMKDFSpec
77
{
8+
private static final byte[] EMPTY_OTHER_INFO = new byte[0];
9+
810
private final String keyAlgorithmName;
911
private final int keySizeInBits;
1012
private final AlgorithmIdentifier kdfAlgorithm;
1113
private final byte[] otherInfo;
1214

15+
/**
16+
* Base constructor.
17+
* <p>
18+
* A null otherInfo is stored as empty, so {@link #getOtherInfo()} never returns null. The
19+
* Builder of every spec in this package already does that, but this constructor is reachable
20+
* directly by a subclass - the deprecated {@link KEMParameterSpec} is one - and
21+
* org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil's KMAC and SHAKE-256 branches read
22+
* the otherInfo length without a guard, so a null here would surface as a
23+
* NullPointerException out of a KEM operation.
24+
*
25+
* @param kdfAlgorithm the KDF to derive with, or null for the shared secret as it comes.
26+
* @param otherInfo the otherInfo/IV to feed the KDF, may be null for none.
27+
* @param keyAlgorithmName the algorithm name for the derived key.
28+
* @param keySizeInBits the size of the key to derive.
29+
*/
1330
protected KEMKDFSpec(AlgorithmIdentifier kdfAlgorithm, byte[] otherInfo, String keyAlgorithmName, int keySizeInBits)
1431
{
1532
this.keyAlgorithmName = keyAlgorithmName;
1633
this.keySizeInBits = keySizeInBits;
1734
this.kdfAlgorithm = kdfAlgorithm;
18-
this.otherInfo = otherInfo;
35+
this.otherInfo = (otherInfo == null) ? EMPTY_OTHER_INFO : otherInfo;
1936
}
2037

2138
public String getKeyAlgorithmName()

‎prov/src/test/java/org/bouncycastle/jcajce/provider/test/KdfUtilTest.java‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
import junit.framework.TestCase;
88
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
9+
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
10+
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
911
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
12+
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
1013
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
14+
import org.bouncycastle.jcajce.spec.KEMParameterSpec;
1115
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
1216
import org.bouncycastle.util.Arrays;
1317

@@ -152,4 +156,78 @@ public void testResolveAlgorithm()
152156
assertEquals("AES does not match AES-KWP", expected.getMessage());
153157
}
154158
}
159+
160+
/**
161+
* A spec built through a Builder never carries a null otherInfo, but the KEMKDFSpec
162+
* constructor is protected and reachable by a subclass - the deprecated KEMParameterSpec passes
163+
* null itself - and three of makeKeyBytes' KDF branches read the otherInfo length without a
164+
* guard. So the normalisation belongs in the constructor: kdf2/kdf3 and HKDF happen to tolerate
165+
* a null through KDFParameters/HKDFParameters, but KMAC-128, KMAC-256 and SHAKE-256 threw
166+
* NullPointerException out of the KEM operation before this.
167+
*/
168+
public void testNullOtherInfoIsNormalised()
169+
{
170+
AlgorithmIdentifier[] kdfs = new AlgorithmIdentifier[]
171+
{
172+
// the branches that were null-tolerant already, kept as the compatibility assertion
173+
new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf2,
174+
new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256)),
175+
new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf3,
176+
new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256)),
177+
new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hkdf_with_sha256),
178+
// and the three that read otherInfo.length directly
179+
new AlgorithmIdentifier(NISTObjectIdentifiers.id_Kmac128),
180+
new AlgorithmIdentifier(NISTObjectIdentifiers.id_Kmac256),
181+
new AlgorithmIdentifier(NISTObjectIdentifiers.id_shake256)
182+
};
183+
184+
for (int i = 0; i != kdfs.length; i++)
185+
{
186+
AlgorithmIdentifier kdf = kdfs[i];
187+
String name = kdf.getAlgorithm().toString();
188+
189+
assertEquals("otherInfo must be normalised for " + name,
190+
0, new NullOtherInfoSpec(kdf).getOtherInfo().length);
191+
192+
byte[] secret = secret();
193+
byte[] keyBytes = KdfUtil.makeKeyBytes(new NullOtherInfoSpec(kdf), secret);
194+
195+
assertEquals(name + ": derived key size", 32, keyBytes.length);
196+
assertTrue(name + ": the secret must still be erased", isErased(secret));
197+
198+
// and the derived bytes must match what an explicitly empty otherInfo gives, so the
199+
// normalisation is not quietly changing anyone's key material
200+
byte[] explicit = KdfUtil.makeKeyBytes(new EmptyOtherInfoSpec(kdf), secret());
201+
assertTrue(name + ": null and empty otherInfo must derive the same key",
202+
Arrays.areEqual(keyBytes, explicit));
203+
}
204+
205+
// the in-tree subclass that passes null itself
206+
assertNotNull("KEMParameterSpec must not report a null otherInfo",
207+
new KEMParameterSpec("AES", 256).getOtherInfo());
208+
assertEquals(0, new KEMParameterSpec("AES", 256).getOtherInfo().length);
209+
}
210+
211+
/**
212+
* An out-of-provider subclass reaching the protected constructor with a null otherInfo - the
213+
* shape KdfUtil's javadoc invites when it says the helpers are public for callers building
214+
* their own KEM integration.
215+
*/
216+
private static class NullOtherInfoSpec
217+
extends KTSParameterSpec
218+
{
219+
NullOtherInfoSpec(AlgorithmIdentifier kdfAlgorithm)
220+
{
221+
super("AES", 256, null, kdfAlgorithm, null);
222+
}
223+
}
224+
225+
private static class EmptyOtherInfoSpec
226+
extends KTSParameterSpec
227+
{
228+
EmptyOtherInfoSpec(AlgorithmIdentifier kdfAlgorithm)
229+
{
230+
super("AES", 256, null, kdfAlgorithm, new byte[0]);
231+
}
232+
}
155233
}

0 commit comments

Comments
 (0)