77public 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