View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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   * NTRUPRIME KeySpec.
32   */
33  public class GordianNTRUPrimeSpec {
34      /**
35       * The Separator.
36       */
37      private static final String SEP = "-";
38  
39      /**
40       * The type.
41       */
42      private final GordianNTRUPrimeType theType;
43  
44      /**
45       * The params.
46       */
47      private final GordianNTRUPrimeParams theParams;
48  
49      /**
50       * is the spec valid?.
51       */
52      private final boolean isValid;
53  
54      /**
55       * The name.
56       */
57      private String theName;
58  
59      /**
60       * Constructor.
61       *
62       * @param pType   the Type
63       * @param pParams the params
64       */
65      public GordianNTRUPrimeSpec(final GordianNTRUPrimeType pType,
66                                  final GordianNTRUPrimeParams pParams) {
67          /* Store parameters */
68          theType = pType;
69          theParams = pParams;
70  
71          /* Check validity */
72          isValid = checkValidity();
73      }
74  
75      /**
76       * Obtain the type.
77       *
78       * @return the type
79       */
80      public GordianNTRUPrimeType getType() {
81          return theType;
82      }
83  
84      /**
85       * Obtain the params.
86       *
87       * @return the params
88       */
89      public GordianNTRUPrimeParams getParams() {
90          return theParams;
91      }
92  
93      /**
94       * Is the keySpec valid?
95       *
96       * @return true/false.
97       */
98      public boolean isValid() {
99          return isValid;
100     }
101 
102     /**
103      * Check spec validity.
104      *
105      * @return valid true/false
106      */
107     private boolean checkValidity() {
108         return theType != null && theParams != null;
109     }
110 
111     @Override
112     public String toString() {
113         /* If we have not yet loaded the name */
114         if (theName == null) {
115             /* If the keySpec is valid */
116             if (isValid) {
117                 /* Load the name */
118                 theName = theType.toString() + "Prime" + SEP + theParams.toString();
119             } else {
120                 /* Report invalid spec */
121                 theName = "InvalidLMSKeySpec: " + theType + SEP + theParams;
122             }
123         }
124 
125         /* return the name */
126         return theName;
127     }
128 
129     @Override
130     public boolean equals(final Object pThat) {
131         /* Handle the trivial cases */
132         if (this == pThat) {
133             return true;
134         }
135         if (pThat == null) {
136             return false;
137         }
138 
139         /* Check values */
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      * Obtain a list of all possible specs.
152      *
153      * @return the list
154      */
155     public static List<GordianNTRUPrimeSpec> listPossibleKeySpecs() {
156         /* Create the list */
157         final List<GordianNTRUPrimeSpec> mySpecs = new ArrayList<>();
158 
159         /* Add the specs */
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         /* Return the list */
167         return mySpecs;
168     }
169 
170     /**
171      * NTRUPRIME Type.
172      */
173     public enum GordianNTRUPrimeType {
174         /**
175          * NTRULPrime.
176          */
177         NTRUL,
178 
179         /**
180          * SNTRUPrime.
181          */
182         SNTRU;
183     }
184 
185     /**
186      * NTRUPRIME Parameters.
187      */
188     public enum GordianNTRUPrimeParams {
189         /**
190          * PR653.
191          */
192         PR653,
193 
194         /**
195          * PR761.
196          */
197         PR761,
198 
199         /**
200          * PR857.
201          */
202         PR857,
203 
204         /**
205          * PR953.
206          */
207         PR953,
208 
209         /**
210          * PR1013.
211          */
212         PR1013,
213 
214         /**
215          * PR1277.
216          */
217         PR1277;
218 
219         /**
220          * Obtain NTRUL Parameters.
221          *
222          * @return the parameters.
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          * Obtain NTRUL ParameterSpec.
245          *
246          * @return the parameters.
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          * Obtain NTRUL algorithm Identifier.
269          *
270          * @return the identifier.
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          * Obtain NTRUL Parameters.
293          *
294          * @return the parameters.
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          * Obtain NTRUL ParameterSpec.
317          *
318          * @return the parameters.
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          * Obtain SNTRU algorithm Identifier.
341          *
342          * @return the identifier.
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 }