1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.gordianknot.api.keypair;
18
19 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
20 import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
21 import org.bouncycastle.pqc.crypto.ntruprime.NTRULPRimeParameters;
22 import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimeParameters;
23 import org.bouncycastle.pqc.jcajce.spec.NTRULPRimeParameterSpec;
24 import org.bouncycastle.pqc.jcajce.spec.SNTRUPrimeParameterSpec;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Objects;
29
30
31
32
33 public class GordianNTRUPrimeSpec {
34
35
36
37 private static final String SEP = "-";
38
39
40
41
42 private final GordianNTRUPrimeType theType;
43
44
45
46
47 private final GordianNTRUPrimeParams theParams;
48
49
50
51
52 private final boolean isValid;
53
54
55
56
57 private String theName;
58
59
60
61
62
63
64
65 public GordianNTRUPrimeSpec(final GordianNTRUPrimeType pType,
66 final GordianNTRUPrimeParams pParams) {
67
68 theType = pType;
69 theParams = pParams;
70
71
72 isValid = checkValidity();
73 }
74
75
76
77
78
79
80 public GordianNTRUPrimeType getType() {
81 return theType;
82 }
83
84
85
86
87
88
89 public GordianNTRUPrimeParams getParams() {
90 return theParams;
91 }
92
93
94
95
96
97
98 public boolean isValid() {
99 return isValid;
100 }
101
102
103
104
105
106
107 private boolean checkValidity() {
108 return theType != null && theParams != null;
109 }
110
111 @Override
112 public String toString() {
113
114 if (theName == null) {
115
116 if (isValid) {
117
118 theName = theType.toString() + "Prime" + SEP + theParams.toString();
119 } else {
120
121 theName = "InvalidLMSKeySpec: " + theType + SEP + theParams;
122 }
123 }
124
125
126 return theName;
127 }
128
129 @Override
130 public boolean equals(final Object pThat) {
131
132 if (this == pThat) {
133 return true;
134 }
135 if (pThat == null) {
136 return false;
137 }
138
139
140 return pThat instanceof GordianNTRUPrimeSpec myThat
141 && theType == myThat.theType
142 && theParams == myThat.theParams;
143 }
144
145 @Override
146 public int hashCode() {
147 return Objects.hash(theType, theParams);
148 }
149
150
151
152
153
154
155 public static List<GordianNTRUPrimeSpec> listPossibleKeySpecs() {
156
157 final List<GordianNTRUPrimeSpec> mySpecs = new ArrayList<>();
158
159
160 for (final GordianNTRUPrimeType myType : GordianNTRUPrimeType.values()) {
161 for (final GordianNTRUPrimeParams myParams : GordianNTRUPrimeParams.values()) {
162 mySpecs.add(new GordianNTRUPrimeSpec(myType, myParams));
163 }
164 }
165
166
167 return mySpecs;
168 }
169
170
171
172
173 public enum GordianNTRUPrimeType {
174
175
176
177 NTRUL,
178
179
180
181
182 SNTRU;
183 }
184
185
186
187
188 public enum GordianNTRUPrimeParams {
189
190
191
192 PR653,
193
194
195
196
197 PR761,
198
199
200
201
202 PR857,
203
204
205
206
207 PR953,
208
209
210
211
212 PR1013,
213
214
215
216
217 PR1277;
218
219
220
221
222
223
224 public NTRULPRimeParameters getNTRULParameters() {
225 switch (this) {
226 case PR653:
227 return NTRULPRimeParameters.ntrulpr653;
228 case PR761:
229 return NTRULPRimeParameters.ntrulpr761;
230 case PR857:
231 return NTRULPRimeParameters.ntrulpr857;
232 case PR953:
233 return NTRULPRimeParameters.ntrulpr953;
234 case PR1013:
235 return NTRULPRimeParameters.ntrulpr1013;
236 case PR1277:
237 return NTRULPRimeParameters.ntrulpr1277;
238 default:
239 throw new IllegalArgumentException();
240 }
241 }
242
243
244
245
246
247
248 public NTRULPRimeParameterSpec getNTRULParameterSpec() {
249 switch (this) {
250 case PR653:
251 return NTRULPRimeParameterSpec.ntrulpr653;
252 case PR761:
253 return NTRULPRimeParameterSpec.ntrulpr761;
254 case PR857:
255 return NTRULPRimeParameterSpec.ntrulpr857;
256 case PR953:
257 return NTRULPRimeParameterSpec.ntrulpr953;
258 case PR1013:
259 return NTRULPRimeParameterSpec.ntrulpr1013;
260 case PR1277:
261 return NTRULPRimeParameterSpec.ntrulpr1277;
262 default:
263 throw new IllegalArgumentException();
264 }
265 }
266
267
268
269
270
271
272 public ASN1ObjectIdentifier getNTRULIdentifier() {
273 switch (this) {
274 case PR653:
275 return BCObjectIdentifiers.ntrulpr653;
276 case PR761:
277 return BCObjectIdentifiers.ntrulpr761;
278 case PR857:
279 return BCObjectIdentifiers.ntrulpr857;
280 case PR953:
281 return BCObjectIdentifiers.ntrulpr953;
282 case PR1013:
283 return BCObjectIdentifiers.ntrulpr1013;
284 case PR1277:
285 return BCObjectIdentifiers.ntrulpr1277;
286 default:
287 throw new IllegalArgumentException();
288 }
289 }
290
291
292
293
294
295
296 public SNTRUPrimeParameters getSNTRUParameters() {
297 switch (this) {
298 case PR653:
299 return SNTRUPrimeParameters.sntrup653;
300 case PR761:
301 return SNTRUPrimeParameters.sntrup761;
302 case PR857:
303 return SNTRUPrimeParameters.sntrup857;
304 case PR953:
305 return SNTRUPrimeParameters.sntrup953;
306 case PR1013:
307 return SNTRUPrimeParameters.sntrup1013;
308 case PR1277:
309 return SNTRUPrimeParameters.sntrup1277;
310 default:
311 throw new IllegalArgumentException();
312 }
313 }
314
315
316
317
318
319
320 public SNTRUPrimeParameterSpec getSNTRUParameterSpec() {
321 switch (this) {
322 case PR653:
323 return SNTRUPrimeParameterSpec.sntrup653;
324 case PR761:
325 return SNTRUPrimeParameterSpec.sntrup761;
326 case PR857:
327 return SNTRUPrimeParameterSpec.sntrup857;
328 case PR953:
329 return SNTRUPrimeParameterSpec.sntrup953;
330 case PR1013:
331 return SNTRUPrimeParameterSpec.sntrup1013;
332 case PR1277:
333 return SNTRUPrimeParameterSpec.sntrup1277;
334 default:
335 throw new IllegalArgumentException();
336 }
337 }
338
339
340
341
342
343
344 public ASN1ObjectIdentifier getSNTRUIdentifier() {
345 switch (this) {
346 case PR653:
347 return BCObjectIdentifiers.sntrup653;
348 case PR761:
349 return BCObjectIdentifiers.sntrup761;
350 case PR857:
351 return BCObjectIdentifiers.sntrup857;
352 case PR953:
353 return BCObjectIdentifiers.sntrup953;
354 case PR1013:
355 return BCObjectIdentifiers.sntrup1013;
356 case PR1277:
357 return BCObjectIdentifiers.sntrup1277;
358 default:
359 throw new IllegalArgumentException();
360 }
361 }
362 }
363 }