Skip to content

Commit 8233216

Browse files
committed
LMS: LMSPublicKeyParameters.generateLMSContext parses the signature itself, with the RFC 8554 sec. 5.4.2 checks in LMSEngine.generateVerifyContext(LMSSignature) made public for it to reach; the byte[] form of generateVerifyContext is deprecated and forwards to the key
1 parent 784df7d commit 8233216

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import org.bouncycastle.crypto.signers.LMSContextBasedVerifier;
1010
import org.bouncycastle.crypto.signers.lms.LMSContext;
1111
import org.bouncycastle.crypto.signers.lms.LMSEngine;
12+
import org.bouncycastle.crypto.signers.lms.LMSSignature;
1213
import org.bouncycastle.util.Arrays;
14+
import org.bouncycastle.util.Exceptions;
1315
import org.bouncycastle.util.io.Streams;
1416

1517
public class LMSPublicKeyParameters
@@ -182,9 +184,22 @@ byte[] toByteArray()
182184
return bOut.toByteArray();
183185
}
184186

187+
/**
188+
* The context a message is absorbed into before verifying an encoded LMS signature against this
189+
* key. Consumed by {@link #verify(LMSContext)}.
190+
*
191+
* @throws IllegalStateException if the signature does not decode.
192+
*/
185193
public LMSContext generateLMSContext(byte[] signature)
186194
{
187-
return LMSEngine.generateVerifyContext(this, signature);
195+
try
196+
{
197+
return LMSEngine.generateVerifyContext(this, LMSSignature.getInstance(signature));
198+
}
199+
catch (IOException e)
200+
{
201+
throw Exceptions.illegalStateException("cannot parse signature", e);
202+
}
188203
}
189204

190205
public boolean verify(LMSContext context)

‎core/src/main/java/org/bouncycastle/crypto/signers/lms/LMSEngine.java‎

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,35 +150,32 @@ public static byte[] generateHSSSignature(int L, LMSContext context)
150150
//
151151

152152
/**
153-
* The context a message is absorbed into before verifying an encoded LMS signature against a
154-
* public key. Consumed by {@link #verifySignature(LMSPublicKeyParameters, LMSContext)}.
155-
*
156-
* @throws IllegalStateException if the signature does not decode.
153+
* @deprecated use {@link LMSPublicKeyParameters#generateLMSContext(byte[])} instead.
157154
*/
155+
@Deprecated
158156
public static LMSContext generateVerifyContext(LMSPublicKeyParameters publicKey, byte[] signature)
159157
{
160-
try
161-
{
162-
return generateVerifyContext(publicKey, LMSSignature.getInstance(signature));
163-
}
164-
catch (IOException e)
165-
{
166-
throw Exceptions.illegalStateException("cannot parse signature", e);
167-
}
158+
return publicKey.generateLMSContext(signature);
168159
}
169160

170161
/**
162+
* The context a message is absorbed into before verifying a decoded LMS signature against a
163+
* public key: the LM-OTS verification context of the leaf the signature names. Public only
164+
* because {@link LMSPublicKeyParameters#generateLMSContext(byte[])} lives in another package;
165+
* callers holding an encoded signature use that.
166+
* <p>
171167
* The typecode and leaf-number checks RFC 8554 sec. 5.4.2 requires before a signature is
172-
* processed: step 2g refuses a signature whose LMS typecode is not the one from the public key,
173-
* and step 2i refuses a leaf number q outside the tree. Without the first, the path computation
174-
* below took its height and tree digest from the parameter set the signature named rather than
175-
* the key's, so a signature claiming h25 drove a 25-level computation against an h5 key; without
176-
* the second, an out-of-range q flowed into the node arithmetic and was left to be caught by the
177-
* candidate-root comparison. Neither was a forgery under a secure hash - the domain separation
178-
* and the final comparison saw to that - but both are work the specification says to refuse up
179-
* front.
168+
* processed happen here: step 2g refuses a signature whose LMS typecode is not the one from the
169+
* public key, and step 2i refuses a leaf number q outside the tree. Without the first, the path
170+
* computation took its height and tree digest from the parameter set the signature named rather
171+
* than the key's, so a signature claiming h25 drove a 25-level computation against an h5 key;
172+
* without the second, an out-of-range q flowed into the node arithmetic and was left to be caught
173+
* by the candidate-root comparison. Neither was a forgery under a secure hash - the domain
174+
* separation and the final comparison saw to that - but both are work the specification says to
175+
* refuse up front.
176+
* </p>
180177
*/
181-
static LMSContext generateVerifyContext(LMSPublicKeyParameters publicKey, LMSSignature signature)
178+
public static LMSContext generateVerifyContext(LMSPublicKeyParameters publicKey, LMSSignature signature)
182179
{
183180
LMSigParameters sigParameters = publicKey.getSigParameters();
184181
if (signature.getParameter().getType() != sigParameters.getType())
@@ -207,8 +204,8 @@ static LMSContext generateVerifyContext(LMSPublicKeyParameters publicKey, LMSSig
207204
}
208205

209206
/**
210-
* Verify the LMS signature a context from {@link #generateVerifyContext} carries over the
211-
* message absorbed into it (RFC 8554 sec. 5.4.2, Algorithm 6).
207+
* Verify the LMS signature a context from {@link LMSPublicKeyParameters#generateLMSContext(byte[])}
208+
* carries over the message absorbed into it (RFC 8554 sec. 5.4.2, Algorithm 6).
212209
*/
213210
public static boolean verifySignature(LMSPublicKeyParameters publicKey, LMSContext context)
214211
{

0 commit comments

Comments
 (0)