Skip to content

Commit b7f03ad

Browse files
committed
LMS: LMSPublicKeyParameters stores its parameter sets as an LMSParameters and gains a package-private constructor that takes ownership of T1 and I, used by the decoder and by LMSPrivateKeyParameters.getPublicKey to share the cached root and I (immutable once published) instead of copying them
1 parent 8233216 commit b7f03ad

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ public LMSPublicKeyParameters getPublicKey()
759759
{
760760
retainFirstPath();
761761

762-
pk = new LMSPublicKeyParameters(lmsParameters.getLMSigParam(), lmsParameters.getLMOTSParam(), findT(1), I);
762+
// Tree nodes and I are immutable once published, so the public key shares them rather than copying.
763+
pk = new LMSPublicKeyParameters(lmsParameters, findT(1), I);
763764
publicKey = pk;
764765
}
765766
return pk;

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ public class LMSPublicKeyParameters
1818
extends LMSKeyParameters
1919
implements LMSContextBasedVerifier
2020
{
21-
private final LMSigParameters parameterSet;
22-
private final LMOtsParameters lmOtsType;
21+
private final LMSParameters lmsParameters;
2322
private final byte[] I;
2423
private final byte[] T1;
2524

2625
public LMSPublicKeyParameters(LMSigParameters parameterSet, LMOtsParameters lmOtsType, byte[] T1, byte[] I)
26+
{
27+
this(LMSParameters.create(parameterSet, lmOtsType), Arrays.clone(T1), Arrays.clone(I));
28+
}
29+
30+
/**
31+
* Takes ownership of T1 and I: the caller must not modify them afterwards.
32+
*/
33+
LMSPublicKeyParameters(LMSParameters lmsParameters, byte[] T1, byte[] I)
2734
{
2835
super(false);
2936

30-
this.parameterSet = parameterSet;
31-
this.lmOtsType = lmOtsType;
32-
this.I = Arrays.clone(I);
33-
this.T1 = Arrays.clone(T1);
37+
this.lmsParameters = lmsParameters;
38+
this.T1 = T1;
39+
this.I = I;
3440
}
3541

3642
public static LMSPublicKeyParameters getInstance(Object src)
@@ -61,7 +67,7 @@ else if (src instanceof DataInputStream)
6167

6268
byte[] T1 = new byte[sigParameters.getM()];
6369
((DataInputStream)src).readFully(T1);
64-
return new LMSPublicKeyParameters(sigParameters, ostTypeCode, T1, I);
70+
return new LMSPublicKeyParameters(LMSParameters.create(sigParameters, ostTypeCode), T1, I);
6571
}
6672
else if (src instanceof byte[])
6773
{
@@ -102,17 +108,17 @@ public byte[] getEncoded()
102108

103109
public LMSigParameters getSigParameters()
104110
{
105-
return parameterSet;
111+
return lmsParameters.getLMSigParam();
106112
}
107113

108114
public LMOtsParameters getOtsParameters()
109115
{
110-
return lmOtsType;
116+
return lmsParameters.getLMOTSParam();
111117
}
112118

113119
public LMSParameters getLMSParameters()
114120
{
115-
return LMSParameters.create(this.getSigParameters(), this.getOtsParameters());
121+
return lmsParameters;
116122
}
117123

118124
public byte[] getT1()
@@ -144,11 +150,7 @@ public boolean equals(Object o)
144150

145151
LMSPublicKeyParameters publicKey = (LMSPublicKeyParameters)o;
146152

147-
if (!parameterSet.equals(publicKey.parameterSet))
148-
{
149-
return false;
150-
}
151-
if (!lmOtsType.equals(publicKey.lmOtsType))
153+
if (!lmsParameters.equals(publicKey.lmsParameters))
152154
{
153155
return false;
154156
}
@@ -162,8 +164,7 @@ public boolean equals(Object o)
162164
@Override
163165
public int hashCode()
164166
{
165-
int result = parameterSet.hashCode();
166-
result = 31 * result + lmOtsType.hashCode();
167+
int result = lmsParameters.hashCode();
167168
result = 31 * result + Arrays.hashCode(I);
168169
result = 31 * result + Arrays.hashCode(T1);
169170
return result;
@@ -176,8 +177,8 @@ byte[] toByteArray()
176177
{
177178
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
178179

179-
u32str(parameterSet.getType(), bOut);
180-
u32str(lmOtsType.getType(), bOut);
180+
u32str(getSigParameters().getType(), bOut);
181+
u32str(getOtsParameters().getType(), bOut);
181182
bytes(I, bOut);
182183
bytes(T1, bOut);
183184

‎core/src/test/java/org/bouncycastle/crypto/params/TypeTests.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void testTypeForType()
2525
}
2626

2727
{
28-
Object o = new HSSPublicKeyParameters(0, new LMSPublicKeyParameters(null, null, null, null));
28+
Object o = new HSSPublicKeyParameters(0, lmsPublicKey());
2929
assertTrue(o == HSSPublicKeyParameters.getInstance(o));
3030
}
3131

@@ -35,7 +35,7 @@ public void testTypeForType()
3535
}
3636

3737
{
38-
Object o = new LMSPublicKeyParameters(null, null, null, null);
38+
Object o = lmsPublicKey();
3939
assertTrue(o == LMSPublicKeyParameters.getInstance(o));
4040
}
4141
}
@@ -44,6 +44,12 @@ public void testTypeForType()
4444
* The key parameter constructors validate their arguments, so these are real - the point of the
4545
* test is only that getInstance() hands back an object of its own type unchanged.
4646
*/
47+
private static LMSPublicKeyParameters lmsPublicKey()
48+
{
49+
return new LMSPublicKeyParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w1,
50+
null, null);
51+
}
52+
4753
private static LMSPrivateKeyParameters lmsKey()
4854
{
4955
return new LMSPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5,

0 commit comments

Comments
 (0)