Skip to content

Commit c2e3896

Browse files
committed
Merge branch '2197-xmss-bds-initial' into 'main'
Fix the issue of XMSSPrivateKeyParameters when try to build BDS with seeds by... See merge request root/bc-java!121
2 parents 5a4ccfd + a8f5a15 commit c2e3896

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/xmss/XMSSPrivateKeyParameters.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,7 @@ private XMSSPrivateKeyParameters(Builder builder)
162162
}
163163
else
164164
{
165-
if (builder.index < ((1 << params.getHeight()) - 2) && tmpPublicSeed != null && tmpSecretKeySeed != null)
166-
{
167-
bdsState = new BDS(params, tmpPublicSeed, tmpSecretKeySeed, (OTSHashAddress)new OTSHashAddress.Builder().build(), builder.index);
168-
}
169-
else
170-
{
171-
bdsState = new BDS(params, (1 << params.getHeight()) - 1, builder.index);
172-
}
165+
bdsState = new BDS(params, tmpPublicSeed, tmpSecretKeySeed, (OTSHashAddress)new OTSHashAddress.Builder().build(), builder.index);
173166
}
174167
if (builder.maxIndex >= 0 && builder.maxIndex != bdsState.getMaxIndex())
175168
{
@@ -228,6 +221,7 @@ public XMSSPrivateKeyParameters getNextKey()
228221
* <p>
229222
* Note: this will use the range [index...index + usageCount) for the current key.
230223
* </p>
224+
*
231225
* @param usageCount the number of usages the key should have.
232226
* @return a key based on the current key that can be used usageCount times.
233227
*/
@@ -343,6 +337,10 @@ public Builder withPrivateKey(byte[] privateKeyVal)
343337

344338
public XMSSPrivateKeyParameters build()
345339
{
340+
if (!((privateKey != null) || (publicSeed != null && secretKeySeed != null)))
341+
{
342+
throw new IllegalStateException("publicSeed or secretKeySeed is null");
343+
}
346344
return new XMSSPrivateKeyParameters(this);
347345
}
348346
}

core/src/test/java/org/bouncycastle/pqc/crypto/test/XMSSPrivateKeyTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ private void parsingTest(Digest digest)
3131
{
3232
XMSSParameters params = new XMSSParameters(10, digest);
3333
byte[] root = generateRoot(digest);
34-
XMSSPrivateKeyParameters privateKey = new XMSSPrivateKeyParameters.Builder(params).withRoot(root).build();
34+
XMSSPrivateKeyParameters privateKey = new XMSSPrivateKeyParameters.Builder(params).withRoot(root)
35+
.withPublicSeed(new byte[digest.getDigestSize()]).withSecretKeySeed(new byte[digest.getDigestSize()]).build();
3536

3637
byte[] export = privateKey.toByteArray();
3738

prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/XMSSTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,19 +488,19 @@ public void testKeyRebuild()
488488
{
489489
KeyPairGenerator kpg = KeyPairGenerator.getInstance("XMSS", "BCPQC");
490490

491-
kpg.initialize(new XMSSParameterSpec(10, XMSSParameterSpec.SHA256), new SecureRandom());
491+
kpg.initialize(new XMSSParameterSpec(3, XMSSParameterSpec.SHA256), new SecureRandom());
492492

493493
KeyPair kp = kpg.generateKeyPair();
494494

495495
Signature sig = Signature.getInstance("SHA256withXMSS", "BCPQC");
496496

497497
assertTrue(sig instanceof StateAwareSignature);
498498

499-
PrivateKey pKey1 = ((XMSSPrivateKey)kp.getPrivate()).extractKeyShard(5);
499+
PrivateKey pKey1 = ((XMSSPrivateKey)kp.getPrivate()).extractKeyShard(7);
500500

501501
sig.initSign(pKey1);
502502

503-
for (int i = 0; i != 5; i++)
503+
for (int i = 0; i != 7; i++)
504504
{
505505
sig.update(msg, 0, msg.length);
506506

0 commit comments

Comments
 (0)