Skip to content

Commit 4c78802

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents 83e5ee5 + 97499b9 commit 4c78802

35 files changed

Lines changed: 1630 additions & 693 deletions

‎core/src/main/java/org/bouncycastle/crypto/generators/HSSKeyPairGenerator.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.bouncycastle.crypto.KeyGenerationParameters;
66
import org.bouncycastle.crypto.params.HSSKeyGenerationParameters;
77
import org.bouncycastle.crypto.params.HSSPrivateKeyParameters;
8-
import org.bouncycastle.crypto.signers.lms.LMSEngine;
98

109
public class HSSKeyPairGenerator
1110
implements AsymmetricCipherKeyPairGenerator
@@ -19,7 +18,7 @@ public void init(KeyGenerationParameters param)
1918

2019
public AsymmetricCipherKeyPair generateKeyPair()
2120
{
22-
HSSPrivateKeyParameters privKey = LMSEngine.generateHSSKeyPair(param);
21+
HSSPrivateKeyParameters privKey = HSSPrivateKeyParameters.generate(param);
2322

2423
return new AsymmetricCipherKeyPair(privKey.getPublicKey(), privKey);
2524
}

‎core/src/main/java/org/bouncycastle/crypto/generators/LMSKeyPairGenerator.java‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package org.bouncycastle.crypto.generators;
22

3-
import java.security.SecureRandom;
4-
53
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
64
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
75
import org.bouncycastle.crypto.KeyGenerationParameters;
86
import org.bouncycastle.crypto.params.LMSKeyGenerationParameters;
97
import org.bouncycastle.crypto.params.LMSPrivateKeyParameters;
10-
import org.bouncycastle.crypto.params.LMSigParameters;
118

129
public class LMSKeyPairGenerator
1310
implements AsymmetricCipherKeyPairGenerator
@@ -21,21 +18,7 @@ public void init(KeyGenerationParameters param)
2118

2219
public AsymmetricCipherKeyPair generateKeyPair()
2320
{
24-
SecureRandom source = param.getRandom();
25-
26-
byte[] I = new byte[16];
27-
source.nextBytes(I);
28-
29-
LMSigParameters sigParameter = param.getParameters().getLMSigParam();
30-
byte[] rootSecret = new byte[sigParameter.getM()];
31-
source.nextBytes(rootSecret);
32-
33-
// TODO[lms] Use guarded style from HSSPrivateKeyParameters.generateKey (factor out); though
34-
// the config above passes the guard, we want to avoid drift
35-
36-
// RFC 8554 sec. 5.2, Algorithm 5: a fresh tree starts at q = 0 and has 2^h one-time keys.
37-
LMSPrivateKeyParameters privKey = new LMSPrivateKeyParameters(
38-
sigParameter, param.getParameters().getLMOTSParam(), 0, I, 1 << sigParameter.getH(), rootSecret);
21+
LMSPrivateKeyParameters privKey = LMSPrivateKeyParameters.generate(param.getParameters(), param.getRandom());
3922

4023
return new AsymmetricCipherKeyPair(privKey.getPublicKey(), privKey);
4124
}

‎core/src/main/java/org/bouncycastle/crypto/params/HSSKeyGenerationParameters.java‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@
77
public class HSSKeyGenerationParameters
88
extends KeyGenerationParameters
99
{
10+
private static LMSParameters[] validateParameters(LMSParameters[] lmsParameters)
11+
{
12+
if (lmsParameters == null)
13+
{
14+
throw new NullPointerException("lmsParameters cannot be null");
15+
}
16+
if (lmsParameters.length < 1 || lmsParameters.length > 8) // RFC 8554, Section 6.
17+
{
18+
throw new IllegalArgumentException("lmsParameters length should be between 1 and 8 inclusive");
19+
}
20+
21+
// copy before checking the elements, so what was validated is what is kept
22+
LMSParameters[] copy = (LMSParameters[])lmsParameters.clone();
23+
24+
for (int i = 0; i < copy.length; ++i)
25+
{
26+
if (copy[i] == null)
27+
{
28+
throw new NullPointerException("HSS level " + i + " has no parameters");
29+
}
30+
31+
// TODO Consider the SP 800-208 sec. 4 restrictions: one hash function within each level (LMS tree and
32+
// LM-OTS keys) and across the hierarchy. bc-csharp enforces both here; it is unclear what BC was aiming
33+
// for, since the RFC 8554 parameter sets allow mixing.
34+
}
35+
36+
return copy;
37+
}
38+
1039
private final LMSParameters[] lmsParameters;
1140

1241
/**
@@ -18,12 +47,13 @@ public class HSSKeyGenerationParameters
1847
public HSSKeyGenerationParameters(
1948
LMSParameters[] lmsParameters,
2049
SecureRandom random)
50+
{
51+
this(random, validateParameters(lmsParameters));
52+
}
53+
54+
private HSSKeyGenerationParameters(SecureRandom random, LMSParameters[] lmsParameters)
2155
{
2256
super(random, LMSParameters.calculateStrength(lmsParameters[0]));
23-
if (lmsParameters.length == 0 || lmsParameters.length > 8) // RFC 8554, Section 6.
24-
{
25-
throw new IllegalArgumentException("lmsParameters length should be between 1 and 8 inclusive");
26-
}
2757
this.lmsParameters = lmsParameters;
2858
}
2959

@@ -32,8 +62,18 @@ public int getDepth()
3262
return lmsParameters.length;
3363
}
3464

65+
/**
66+
* The parameters of one level of the hierarchy, 0 being the root; {@link #getDepth()} gives the range.
67+
*
68+
* @throws ArrayIndexOutOfBoundsException if index is outside 0 .. getDepth() - 1.
69+
*/
70+
public LMSParameters getLmsParameters(int index)
71+
{
72+
return lmsParameters[index];
73+
}
74+
3575
public LMSParameters[] getLmsParameters()
3676
{
37-
return lmsParameters;
77+
return (LMSParameters[])lmsParameters.clone();
3878
}
3979
}

0 commit comments

Comments
 (0)