Skip to content

Commit cb227a6

Browse files
author
gefeili
committed
Address the remainder of the KdfUtil review. SM9 refuses a non-positive key size at construction with the declared InvalidAlgorithmParameterException, where it escaped newEncapsulator as the generator's own IllegalArgumentException - only positivity, since unlike the other KEMs SM9's KDF produces any requested bit length, so a size that is not a whole number of bytes is a legitimate GM/T 0044.4 request. The three per-family slice tests drive the encapsulator through a non-zero window as well as the decapsulator - both sides are one line into the shared helper, but each SPI's from/to wiring is its own code. KdfUtil gains the class javadoc a four-method public toolkit was missing, naming the intended resolveKemSpec / resolveAlgorithm / makeSecretKey sequence; makeKeyBytes' erase note now says the erase covers the failure paths, which its finally always did; makeSecretKey's range-check note no longer claims the erase depends on it; WrapUtil's clamp carries the pointer to the refuse-don't-clamp convention so the three sites that share the rule all cite each other; and the dead commented-out privateKey fields are gone from the three older decapsulators.
1 parent 2bc7208 commit cb227a6

10 files changed

Lines changed: 69 additions & 10 deletions

File tree

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/util/KdfUtil.java‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
3131
import org.bouncycastle.util.Arrays;
3232

33+
/**
34+
* The KEM secret-key derivation toolkit shared by every KEM in the provider, public so a caller
35+
* building its own KEM integration can use the same pieces. The intended sequence is the one the
36+
* provider's javax.crypto.KEM services follow: {@link #resolveKemSpec} to validate the caller's
37+
* KTSParameterSpec (or build the default) when the encapsulator or decapsulator is created,
38+
* {@link #resolveAlgorithm} to reconcile the requested algorithm name with the spec's per
39+
* operation, and {@link #makeSecretKey} to derive the key - or {@link #makeKeyBytes} where raw
40+
* bytes are wanted, as the KEM KeyGenerator services do. The secret-bearing inputs are erased by
41+
* the methods that consume them; each javadoc says exactly when.
42+
*/
3343
public class KdfUtil
3444
{
3545
/**
@@ -119,8 +129,9 @@ else if (!isSupportedKdf(ktsSpec.getKdfAlgorithm()))
119129
* particular - before calling this, and destroy the SecretWithEncapsulation it came from
120130
* afterwards; that is left to the caller so the ordering stays visible at the call site.
121131
* <p>
122-
* The requested range is validated here rather than left to SecretKeySpec, so that an
123-
* out-of-range request cannot reach it after the secret has been erased.
132+
* The requested range is validated before deriving, so an out-of-range request is reported as
133+
* the range error it is instead of surfacing from SecretKeySpec - the secret is erased either
134+
* way.
124135
*
125136
* @param parameterSpec the KDF and output size to derive with.
126137
* @param kemSecret the mechanism's shared secret (erased before this returns).
@@ -262,10 +273,11 @@ private static boolean isSupportedKdf(AlgorithmIdentifier kdfAlgorithm)
262273
}
263274

264275
/**
265-
* Generate a byte[] secret key from the passed in secret. Note: passed in secret will be erased after use.
276+
* Generate a byte[] secret key from the passed in secret. Note: the passed in secret will be
277+
* erased before this returns, on the failure paths included.
266278
*
267279
* @param kdfSpec definition of the KDF and the output size to produce.
268-
* @param secret the secret value to initialize the KDF with (erased after secret key generation).
280+
* @param secret the secret value to initialize the KDF with (erased before this returns).
269281
* @return a generated secret key.
270282
* @throws IllegalArgumentException if the spec asks for no KDF and the requested key size is
271283
* larger than the shared secret the mechanism produced.

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/util/WrapUtil.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ public static byte[] trimSecret(String algName, byte[] secret)
105105
// requested key size is an upper bound, but a KEM shared secret can be shorter than the
106106
// default 256-bit request (e.g. FrodoKEM-976's secret is 192 bits), so clamp to what the
107107
// secret actually provides rather than over-reading it.
108+
/**
109+
* The requested size is only an upper bound here - a wrapping key is derived the same way by
110+
* both parties from the same secret, so clamping to the secret is harmless. Do not copy this
111+
* shape to a path that hands the caller a key of a size they asked for: KdfUtil.resolveKemSpec
112+
* and KdfUtil.makeKeyBytes both refuse such a request instead, and say why.
113+
*/
108114
private static int kekLength(KTSParameterSpec ktsSpec, byte[] secret)
109115
{
110116
return Math.min((ktsSpec.getKeySize() + 7) / 8, secret.length);

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/mlkem/MLKEMDecapsulatorSpi.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
class MLKEMDecapsulatorSpi
1919
implements KEMSpi.DecapsulatorSpi
2020
{
21-
// private final BCMLKEMPrivateKey privateKey;
2221
private final KTSParameterSpec parameterSpec;
2322
private final MLKEMExtractor kemExt;
2423

2524
MLKEMDecapsulatorSpi(BCMLKEMPrivateKey privateKey, KTSParameterSpec parameterSpec)
2625
{
27-
// this.privateKey = privateKey;
2826
this.parameterSpec = parameterSpec;
2927
this.kemExt = new MLKEMExtractor(privateKey.getKeyParams());
3028
}

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/sm9/SM9KEMSpi.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ else if (((KTSParameterSpec)spec).getKeyAlgorithmName() == null)
5353
// substituted for a "Generic" request and only fail deep inside the derivation.
5454
throw new InvalidAlgorithmParameterException("KTSParameterSpec has no key algorithm name");
5555
}
56+
else if (((KTSParameterSpec)spec).getKeySize() <= 0)
57+
{
58+
// also checked here rather than by resolveKemSpec. Only positivity: unlike the other
59+
// KEMs, SM9's own KDF produces any requested bit length, so a size that is not a whole
60+
// number of bytes is a legitimate GM/T 0044.4 request (secretSize() floors it).
61+
throw new InvalidAlgorithmParameterException("KTSParameterSpec key size must be positive: "
62+
+ ((KTSParameterSpec)spec).getKeySize());
63+
}
5664

5765
return new SM9EncapsulatorSpi(bcPublicKey, (KTSParameterSpec)spec, secureRandom);
5866
}
@@ -82,6 +90,14 @@ else if (((KTSParameterSpec)spec).getKeyAlgorithmName() == null)
8290
// substituted for a "Generic" request and only fail deep inside the derivation.
8391
throw new InvalidAlgorithmParameterException("KTSParameterSpec has no key algorithm name");
8492
}
93+
else if (((KTSParameterSpec)spec).getKeySize() <= 0)
94+
{
95+
// also checked here rather than by resolveKemSpec. Only positivity: unlike the other
96+
// KEMs, SM9's own KDF produces any requested bit length, so a size that is not a whole
97+
// number of bytes is a legitimate GM/T 0044.4 request (secretSize() floors it).
98+
throw new InvalidAlgorithmParameterException("KTSParameterSpec key size must be positive: "
99+
+ ((KTSParameterSpec)spec).getKeySize());
100+
}
85101

86102
return new SM9DecapsulatorSpi(bcPrivateKey, (KTSParameterSpec)spec);
87103
}

‎prov/src/main/jdk17/org/bouncycastle/pqc/jcajce/provider/hqc/HQCDecapsulatorSpi.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
class HQCDecapsulatorSpi
1919
implements KEMSpi.DecapsulatorSpi
2020
{
21-
// private final BCHQCPrivateKey privateKey;
2221
private final KTSParameterSpec parameterSpec;
2322
private final HQCKEMExtractor kemExt;
2423

2524
HQCDecapsulatorSpi(BCHQCPrivateKey privateKey, KTSParameterSpec parameterSpec)
2625
{
27-
// this.privateKey = privateKey;
2826
this.parameterSpec = parameterSpec;
2927
this.kemExt = new HQCKEMExtractor(privateKey.getKeyParams());
3028
}

‎prov/src/main/jdk17/org/bouncycastle/pqc/jcajce/provider/ntru/NTRUDecapsulatorSpi.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
class NTRUDecapsulatorSpi
1919
implements KEMSpi.DecapsulatorSpi
2020
{
21-
// private final BCNTRUPrivateKey privateKey;
2221
private final KTSParameterSpec parameterSpec;
2322
private final NTRUKEMExtractor kemExt;
2423

2524
NTRUDecapsulatorSpi(BCNTRUPrivateKey privateKey, KTSParameterSpec parameterSpec)
2625
{
27-
// this.privateKey = privateKey;
2826
this.parameterSpec = parameterSpec;
2927
this.kemExt = new NTRUKEMExtractor(privateKey.getKeyParams());
3028
}

‎prov/src/test/jdk17/org/bouncycastle/jce/provider/test/SM9KEM17Test.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ public void testGuards()
174174
catch (InvalidAlgorithmParameterException expected)
175175
{
176176
}
177+
// a non-positive key size is refused at construction too; a size that is not a whole
178+
// number of bytes is deliberately allowed - SM9's own KDF produces any bit length
179+
try
180+
{
181+
kem.newEncapsulator(bobPublic, new KTSParameterSpec.Builder("AES", 0).withNoKdf().build(), null);
182+
fail("encapsulator accepted a zero key size");
183+
}
184+
catch (InvalidAlgorithmParameterException expected)
185+
{
186+
}
177187
}
178188

179189
/**

‎prov/src/test/jdk17/org/bouncycastle/pqc/jcajce/provider/test/NTRULPRimeKEM17Test.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,13 @@ public void testSecretSlice()
355355
SecretKey tail = d.decapsulate(enc.encapsulation(), 16, 32, "AES");
356356
assertEquals(16, tail.getEncoded().length);
357357
assertFalse(Arrays.areEqual(secR.getEncoded(), tail.getEncoded()));
358+
359+
// and the encapsulator honours a non-zero window too - both sides go through the same
360+
// helper, but the from/to wiring of each SPI is its own code
361+
KEM.Encapsulated tailEnc = e.encapsulate(16, 32, "AES");
362+
assertEquals(16, tailEnc.key().getEncoded().length);
363+
assertTrue(Arrays.areEqual(tailEnc.key().getEncoded(),
364+
d.decapsulate(tailEnc.encapsulation(), 16, 32, "AES").getEncoded()));
358365
}
359366

360367
private void performKEM(String label, KeyPair kp, KTSParameterSpec spec)

‎prov/src/test/jdk17/org/bouncycastle/pqc/jcajce/provider/test/NTRUPlusKEM17Test.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ public void testSecretSlice()
422422
SecretKey tail = d.decapsulate(enc.encapsulation(), 16, 32, "AES");
423423
assertEquals(16, tail.getEncoded().length);
424424
assertFalse(Arrays.areEqual(secR.getEncoded(), tail.getEncoded()));
425+
426+
// and the encapsulator honours a non-zero window too - both sides go through the same
427+
// helper, but the from/to wiring of each SPI is its own code
428+
KEM.Encapsulated tailEnc = e.encapsulate(16, 32, "AES");
429+
assertEquals(16, tailEnc.key().getEncoded().length);
430+
assertTrue(Arrays.areEqual(tailEnc.key().getEncoded(),
431+
d.decapsulate(tailEnc.encapsulation(), 16, 32, "AES").getEncoded()));
425432
}
426433

427434
private void performKEM(String label, KeyPair kp, KTSParameterSpec spec)

‎prov/src/test/jdk17/org/bouncycastle/pqc/jcajce/provider/test/SmaugTKEM17Test.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ public void testSecretSlice()
421421
SecretKey tail = d.decapsulate(enc.encapsulation(), 16, 32, "AES");
422422
assertEquals(16, tail.getEncoded().length);
423423
assertFalse(Arrays.areEqual(secR.getEncoded(), tail.getEncoded()));
424+
425+
// and the encapsulator honours a non-zero window too - both sides go through the same
426+
// helper, but the from/to wiring of each SPI is its own code
427+
KEM.Encapsulated tailEnc = e.encapsulate(16, 32, "AES");
428+
assertEquals(16, tailEnc.key().getEncoded().length);
429+
assertTrue(Arrays.areEqual(tailEnc.key().getEncoded(),
430+
d.decapsulate(tailEnc.encapsulation(), 16, 32, "AES").getEncoded()));
424431
}
425432

426433
private void performKEM(String label, KeyPair kp, KTSParameterSpec spec)

0 commit comments

Comments
 (0)