@@ -124,10 +124,7 @@ public static LMSContext withSignedPublicKeys(LMSContext context, LMSSignature[]
124124 */
125125 public static LMSSignature generateSign (LMSContext context )
126126 {
127- // Step 1.
128- LMOtsSignature ots_signature = LM_OTS .lm_ots_generate_signature (context .getPrivateKey (), context .getQ (), context .getC ());
129-
130- return new LMSSignature (context .getPrivateKey ().getQ (), ots_signature , context .getSigParams (), context .getPath ());
127+ return context .generateSignature ();
131128 }
132129
133130 /**
@@ -181,30 +178,32 @@ public static LMSContext generateVerifyContext(LMSPublicKeyParameters publicKey,
181178 * and the final comparison saw to that - but both are work the specification says to refuse up
182179 * front.
183180 */
184- static LMSContext generateVerifyContext (LMSPublicKeyParameters publicKey , LMSSignature S )
181+ static LMSContext generateVerifyContext (LMSPublicKeyParameters publicKey , LMSSignature signature )
185182 {
186183 LMSigParameters sigParameters = publicKey .getSigParameters ();
187- if (S .getParameter ().getType () != sigParameters .getType ())
184+ if (signature .getParameter ().getType () != sigParameters .getType ())
188185 {
189186 throw new IllegalArgumentException ("lms type from lms signature does not match lms type" +
190187 " from public key" );
191188 }
192189
193- int q = S .getQ ();
190+ int q = signature .getQ ();
194191 if (q < 0 || q >= (1 << sigParameters .getH ()))
195192 {
196193 throw new IllegalArgumentException ("lms leaf number q from lms signature is outside the" +
197194 " public key's tree" );
198195 }
199196
200197 int ots_typecode = publicKey .getOtsParameters ().getType ();
201- if (S .getOtsSignature ().getType ().getType () != ots_typecode )
198+ if (signature .getOtsSignature ().getType ().getType () != ots_typecode )
202199 {
203200 throw new IllegalArgumentException ("ots type from lsm signature does not match ots" +
204201 " signature type from embedded ots signature" );
205202 }
206203
207- return new LMOtsPublicKey (LMOtsParameters .getParametersForType (ots_typecode ), publicKey .getI (), S .getQ (), null ).createOtsContext (S );
204+ LMOtsParameters otsParameters = LMOtsParameters .getParametersForType (ots_typecode );
205+ LMOtsPublicKey otsPublicKey = new LMOtsPublicKey (otsParameters , publicKey .getI (), q , null );
206+ return otsPublicKey .createOtsContext (signature );
208207 }
209208
210209 /**
@@ -213,65 +212,72 @@ static LMSContext generateVerifyContext(LMSPublicKeyParameters publicKey, LMSSig
213212 */
214213 public static boolean verifySignature (LMSPublicKeyParameters publicKey , LMSContext context )
215214 {
216- LMSSignature S = (LMSSignature )context .getSignature ();
217- LMSigParameters sigParameters = S .getParameter ();
218- int h = sigParameters .getH ();
219- byte [][] path = S .getY ();
220- byte [] Kc = LM_OTS .lm_ots_validate_signature_calculate (context );
221- // Step 4
222- // node_num = 2^h + q
223- int node_num = (1 << h ) + S .getQ ();
224-
225- // tmp = H(I || u32str(node_num) || u16str(D_LEAF) || Kc)
215+ Object contextSignature = context .getSignature ();
216+
217+ // Guaranteed by every route the library has to a verification context, all of which reach
218+ // LMOtsPublicKey.createOtsContext with a decoded LMS signature. No public path to mis-construction.
219+ if (!(contextSignature instanceof LMSSignature ))
220+ {
221+ throw new IllegalStateException ("context was not created from an LMS signature" );
222+ }
223+
224+ LMSSignature signature = (LMSSignature )contextSignature ;
225+ LMSigParameters sigParameters = signature .getParameter ();
226+ byte [][] path = signature .getY ();
227+
228+ // Kc, the LM-OTS public key the signature computes for itself
229+ byte [] Kc = context .calculateKc ();
230+
226231 byte [] I = publicKey .getI ();
227- Digest H = DigestUtil .getDigest (sigParameters );
228- byte [] tmp = new byte [H .getDigestSize ()];
232+ Digest digest = DigestUtil .getDigest (sigParameters );
229233
230- H .update (I , 0 , I .length );
231- LmsUtils .u32str (node_num , H );
232- LmsUtils .u16str (D_LEAF , H );
233- H .update (Kc , 0 , Kc .length );
234- H .doFinal (tmp , 0 );
234+ // The node the walk is at, and the hash it has computed for it: the leaf of the one-time key that
235+ // signed, then each parent in turn, leaving the root the signature claims.
236+ // RFC 8554 sec. 5.4.2 step 4: node_num = 2^h + q, tmp = H(I || u32str(node_num) || u16str(D_LEAF) || Kc)
237+ int node_num = (1 << sigParameters .getH ()) + signature .getQ ();
238+ byte [] nodeHash = new byte [digest .getDigestSize ()];
239+
240+ digest .update (I , 0 , I .length );
241+ LmsUtils .u32str (node_num , digest );
242+ LmsUtils .u16str (D_LEAF , digest );
243+ digest .update (Kc , 0 , Kc .length );
244+ digest .doFinal (nodeHash , 0 );
235245
236246 int i = 0 ;
237247
238248 while (node_num > 1 )
239249 {
240- if ((node_num & 1 ) == 1 )
241- {
242- // is odd
243- H .update (I , 0 , I .length );
244- LmsUtils .u32str (node_num / 2 , H );
245- LmsUtils .u16str (D_INTR , H );
246- H .update (path [i ], 0 , path [i ].length );
247- H .update (tmp , 0 , tmp .length );
248- H .doFinal (tmp , 0 );
249- }
250- else
251- {
252- H .update (I , 0 , I .length );
253- LmsUtils .u32str (node_num / 2 , H );
254- LmsUtils .u16str (D_INTR , H );
255- H .update (tmp , 0 , tmp .length );
256- H .update (path [i ], 0 , path [i ].length );
257- H .doFinal (tmp , 0 );
258- }
259- node_num = node_num / 2 ;
260- i ++;
261- // these two can get out of sync with an invalid signature, we'll
262- // try and fail gracefully
263- if (i == path .length && node_num > 1 )
250+ // The path and the node count can get out of sync with an invalid signature, so fail gracefully
251+ // rather than index past the path the signature carries.
252+ if (i >= path .length )
264253 {
265254 return false ;
266255 }
256+
257+ byte [] siblingHash = path [i ++];
258+
259+ // The node's parity decides which side its sibling from the path goes on - left for an odd node,
260+ // right for an even one - while the hash itself is over the parent (RFC 8554 sec. 5.4.2 step 4).
261+ boolean isOdd = (node_num & 1 ) == 1 ;
262+ node_num >>= 1 ;
263+
264+ byte [] left = isOdd ? siblingHash : nodeHash ;
265+ byte [] right = isOdd ? nodeHash : siblingHash ;
266+
267+ digest .update (I , 0 , I .length );
268+ LmsUtils .u32str (node_num , digest );
269+ LmsUtils .u16str (D_INTR , digest );
270+ digest .update (left , 0 , left .length );
271+ digest .update (right , 0 , right .length );
272+ digest .doFinal (nodeHash , 0 );
267273 }
268- byte [] Tc = tmp ;
269- return Arrays .constantTimeAreEqual (publicKey .getT1 (), Tc );
274+
275+ return Arrays .constantTimeAreEqual (publicKey .getT1 (), nodeHash );
270276 }
271277
272- static boolean verifySignature (LMSPublicKeyParameters publicKey , LMSSignature S , byte [] message )
278+ static boolean verifySignature (LMSPublicKeyParameters publicKey , LMSSignature signature , byte [] message )
273279 {
274- LMSContext context = generateVerifyContext (publicKey , S );
280+ LMSContext context = generateVerifyContext (publicKey , signature );
275281
276282 LmsUtils .byteArray (message , context );
277283
0 commit comments