View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 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  
18  package io.github.tonywasher.joceanus.gordianknot.api.mac.spec;
19  
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
21  import io.github.tonywasher.joceanus.gordianknot.api.cipher.spec.GordianSymKeySpec;
22  import io.github.tonywasher.joceanus.gordianknot.api.cipher.spec.GordianSymKeySpecBuilder;
23  import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestSpec;
24  import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestSpecBuilder;
25  import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestSubSpec.GordianDigestState;
26  import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestType;
27  
28  /**
29   * Mac Specification Builder.
30   */
31  public interface GordianMacSpecBuilder {
32      /**
33       * Define MacType.
34       *
35       * @param pType the type
36       * @return the Builder
37       */
38      GordianMacSpecBuilder withType(GordianMacType pType);
39  
40      /**
41       * Define keyLength.
42       *
43       * @param pLength the keyLength
44       * @return the Builder
45       */
46      GordianMacSpecBuilder withKeyLength(GordianLength pLength);
47  
48      /**
49       * Define digestSubSpec.
50       *
51       * @param pDigestSpec the digest subSpec
52       * @return the Builder
53       */
54      GordianMacSpecBuilder withDigestSubSpec(GordianDigestSpec pDigestSpec);
55  
56      /**
57       * Define symKeySubSpec.
58       *
59       * @param pSymKeySpec the symKeySpec subSpec
60       * @return the Builder
61       */
62      GordianMacSpecBuilder withSymKeySubSpec(GordianSymKeySpec pSymKeySpec);
63  
64      /**
65       * Define sipHashSubSpec.
66       *
67       * @param pSipHashSpec the sipHashSpec subSpec
68       * @return the Builder
69       */
70      GordianMacSpecBuilder withSipHashSubSpec(GordianSipHashType pSipHashSpec);
71  
72      /**
73       * Define lengthSubSpec.
74       *
75       * @param pLength the length subSpec
76       * @return the Builder
77       */
78      GordianMacSpecBuilder withLengthSubSpec(GordianLength pLength);
79  
80      /**
81       * Access digestSpecBuilder.
82       *
83       * @return the digestSpec builder
84       */
85      GordianDigestSpecBuilder usingDigestSpecBuilder();
86  
87      /**
88       * Access symKeySpecBuilder.
89       *
90       * @return the symKeySpec builder
91       */
92      GordianSymKeySpecBuilder usingSymKeySpecBuilder();
93  
94      /**
95       * Build macSpec.
96       *
97       * @return the macSpec
98       */
99      GordianMacSpec build();
100 
101     /**
102      * Create generic.
103      *
104      * @param pMacType   the macType
105      * @param pKeyLength the keyLength
106      * @return the MacSpec
107      */
108     default GordianMacSpec mac(final GordianMacType pMacType,
109                                final GordianLength pKeyLength) {
110         return withType(pMacType).withKeyLength(pKeyLength).build();
111     }
112 
113     /**
114      * Create generic.
115      *
116      * @param pMacType    the macType
117      * @param pSymKeySpec the symKeySpec
118      * @return the MacSpec
119      */
120     default GordianMacSpec mac(final GordianMacType pMacType,
121                                final GordianSymKeySpec pSymKeySpec) {
122         return withType(pMacType).withSymKeySubSpec(pSymKeySpec).build();
123     }
124 
125     /**
126      * Create hMacSpec.
127      *
128      * @param pDigestType the digestType
129      * @return the MacSpec
130      */
131     default GordianMacSpec hMac(final GordianDigestType pDigestType) {
132         return hMac(pDigestType, GordianLength.LEN_128);
133     }
134 
135     /**
136      * Create hMacSpec.
137      *
138      * @param pDigestType the digestType
139      * @param pKeyLength  the keyLength
140      * @return the MacSpec
141      */
142     default GordianMacSpec hMac(final GordianDigestType pDigestType,
143                                 final GordianLength pKeyLength) {
144         return hMac(usingDigestSpecBuilder().withType(pDigestType).build(), pKeyLength);
145     }
146 
147     /**
148      * Create hMacSpec.
149      *
150      * @param pDigest the digestSpec
151      * @return the MacSpec
152      */
153     default GordianMacSpec hMac(final GordianDigestSpec pDigest) {
154         return hMac(pDigest, pDigest.getDigestLength());
155     }
156 
157     /**
158      * Create hMacSpec.
159      *
160      * @param pDigestSpec the digestSpec
161      * @param pKeyLength  the keyLength
162      * @return the MacSpec
163      */
164     default GordianMacSpec hMac(final GordianDigestSpec pDigestSpec,
165                                 final GordianLength pKeyLength) {
166         return withType(GordianMacType.HMAC).withDigestSubSpec(pDigestSpec).withKeyLength(pKeyLength).build();
167     }
168 
169     /**
170      * Create gMacSpec.
171      *
172      * @param pSymKeySpec the symKeySpec
173      * @return the MacSpec
174      */
175     default GordianMacSpec gMac(final GordianSymKeySpec pSymKeySpec) {
176         return withType(GordianMacType.GMAC).withSymKeySubSpec(pSymKeySpec).build();
177     }
178 
179     /**
180      * Create cMacSpec.
181      *
182      * @param pSymKeySpec the symKeySpec
183      * @return the MacSpec
184      */
185     default GordianMacSpec cMac(final GordianSymKeySpec pSymKeySpec) {
186         return withType(GordianMacType.CMAC).withSymKeySubSpec(pSymKeySpec).build();
187     }
188 
189     /**
190      * Create skeinMacSpec.
191      *
192      * @param pKeyLength the keyLength
193      * @return the MacSpec
194      */
195     default GordianMacSpec kMac(final GordianLength pKeyLength) {
196         return kMac(pKeyLength, usingDigestSpecBuilder().shake128());
197     }
198 
199     /**
200      * Create KMACSpec.
201      *
202      * @param pKeyLength the keyLength
203      * @param pSpec      the Shake Spec
204      * @return the MacSpec
205      */
206     default GordianMacSpec kMac(final GordianLength pKeyLength,
207                                 final GordianDigestSpec pSpec) {
208         return withType(GordianMacType.KMAC).withKeyLength(pKeyLength).withDigestSubSpec(pSpec).build();
209     }
210 
211     /**
212      * Create poly1305MacSpec.
213      *
214      * @return the MacSpec
215      */
216     default GordianMacSpec poly1305Mac() {
217         return withType(GordianMacType.POLY1305).withKeyLength(GordianLength.LEN_256).build();
218     }
219 
220     /**
221      * Create poly1305MacSpec.
222      *
223      * @param pSymKeySpec the symKeySpec
224      * @return the MacSpec
225      */
226     default GordianMacSpec poly1305Mac(final GordianSymKeySpec pSymKeySpec) {
227         return withType(GordianMacType.POLY1305).withSymKeySubSpec(pSymKeySpec).build();
228     }
229 
230     /**
231      * Create skeinMacSpec.
232      *
233      * @param pKeyLength the keyLength
234      * @return the MacSpec
235      */
236     default GordianMacSpec skeinMac(final GordianLength pKeyLength) {
237         return skeinMac(pKeyLength, GordianLength.LEN_256);
238     }
239 
240     /**
241      * Create skeinMacSpec.
242      *
243      * @param pKeyLength the keyLength
244      * @param pLength    the length
245      * @return the MacSpec
246      */
247     default GordianMacSpec skeinMac(final GordianLength pKeyLength,
248                                     final GordianLength pLength) {
249         return skeinMac(pKeyLength, usingDigestSpecBuilder().skein(pLength));
250     }
251 
252     /**
253      * Create skeinMacSpec.
254      *
255      * @param pKeyLength the keyLength
256      * @param pState     the digestState
257      * @param pLength    the length
258      * @return the MacSpec
259      */
260     default GordianMacSpec skeinMac(final GordianLength pKeyLength,
261                                     final GordianDigestState pState,
262                                     final GordianLength pLength) {
263         return skeinMac(pKeyLength, usingDigestSpecBuilder().skein(pState, pLength));
264     }
265 
266     /**
267      * Create skeinMacSpec.
268      *
269      * @param pKeyLength the keyLength
270      * @param pSpec      the skeinDigestSpec
271      * @return the MacSpec
272      */
273     default GordianMacSpec skeinMac(final GordianLength pKeyLength,
274                                     final GordianDigestSpec pSpec) {
275         return withType(GordianMacType.SKEIN).withKeyLength(pKeyLength).withDigestSubSpec(pSpec).build();
276     }
277 
278     /**
279      * Create skeinXMacSpec.
280      *
281      * @param pKeyLength the keyLength
282      * @param pState     the state
283      * @return the MacSpec
284      */
285     default GordianMacSpec skeinXMac(final GordianLength pKeyLength,
286                                      final GordianDigestState pState) {
287         return skeinMac(pKeyLength, usingDigestSpecBuilder().skeinX(pState));
288     }
289 
290     /**
291      * Create blake2sMacSpec.
292      *
293      * @param pKeyLength the length
294      * @param pLength    the length
295      * @return the MacSpec
296      */
297     default GordianMacSpec blake2sMac(final GordianLength pKeyLength,
298                                       final GordianLength pLength) {
299         return blake2Mac(pKeyLength, usingDigestSpecBuilder().blake2s(pLength));
300     }
301 
302     /**
303      * Create blake2bMacSpec.
304      *
305      * @param pKeyLength the length
306      * @param pLength    the length
307      * @return the MacSpec
308      */
309     default GordianMacSpec blake2bMac(final GordianLength pKeyLength,
310                                       final GordianLength pLength) {
311         return blake2Mac(pKeyLength, usingDigestSpecBuilder().blake2b(pLength));
312     }
313 
314     /**
315      * Create blake2MacSpec.
316      *
317      * @param pKeyLength the keyLength
318      * @param pSpec      the blake digestSpec
319      * @return the MacSpec
320      */
321     default GordianMacSpec blake2Mac(final GordianLength pKeyLength,
322                                      final GordianDigestSpec pSpec) {
323         return withType(GordianMacType.BLAKE2).withKeyLength(pKeyLength).withDigestSubSpec(pSpec).build();
324     }
325 
326     /**
327      * Create blake2MacSpec.
328      *
329      * @param pKeyLength the keyLength
330      * @param pState     the blake state
331      * @return the MacSpec
332      */
333     default GordianMacSpec blake2XMac(final GordianLength pKeyLength,
334                                       final GordianDigestState pState) {
335         return withType(GordianMacType.BLAKE2).withKeyLength(pKeyLength)
336                 .withDigestSubSpec(usingDigestSpecBuilder().blake2X(pState)).build();
337     }
338 
339     /**
340      * Create blake3MacSpec.
341      *
342      * @param pMacLength the macLength
343      * @return the MacSpec
344      */
345     default GordianMacSpec blake3Mac(final GordianLength pMacLength) {
346         return withType(GordianMacType.BLAKE3).withKeyLength(GordianLength.LEN_256)
347                 .withDigestSubSpec(usingDigestSpecBuilder().blake3(pMacLength)).build();
348     }
349 
350     /**
351      * Create kalynaMacSpec.
352      *
353      * @param pKeySpec the keySpec
354      * @return the MacSpec
355      */
356     default GordianMacSpec kalynaMac(final GordianSymKeySpec pKeySpec) {
357         return withType(GordianMacType.KALYNA).withSymKeySubSpec(pKeySpec).build();
358     }
359 
360     /**
361      * Create kupynaMacSpec.
362      *
363      * @param pKeyLength the keyLength
364      * @return the MacSpec
365      */
366     default GordianMacSpec kupynaMac(final GordianLength pKeyLength) {
367         return kupynaMac(pKeyLength, GordianLength.LEN_256);
368     }
369 
370     /**
371      * Create kupynaMacSpec.
372      *
373      * @param pKeyLength the keyLength
374      * @param pLength    the length
375      * @return the MacSpec
376      */
377     default GordianMacSpec kupynaMac(final GordianLength pKeyLength,
378                                      final GordianLength pLength) {
379         return withType(GordianMacType.KUPYNA).withKeyLength(pKeyLength)
380                 .withDigestSubSpec(usingDigestSpecBuilder().kupyna(pLength)).build();
381     }
382 
383     /**
384      * Create vmpcMacSpec.
385      *
386      * @param pKeyLength the keyLength
387      * @return the MacSpec
388      */
389     default GordianMacSpec vmpcMac(final GordianLength pKeyLength) {
390         return withType(GordianMacType.VMPC).withKeyLength(pKeyLength).build();
391     }
392 
393     /**
394      * Create gostMacSpec.
395      *
396      * @return the MacSpec
397      */
398     default GordianMacSpec gostMac() {
399         return withType(GordianMacType.GOST).withKeyLength(GordianLength.LEN_256).build();
400     }
401 
402     /**
403      * Create sipHashSpec.
404      *
405      * @param pSpec the sipHashSpec
406      * @return the MacSpec
407      */
408     default GordianMacSpec sipHash(final GordianSipHashType pSpec) {
409         return withType(GordianMacType.SIPHASH).withSipHashSubSpec(pSpec).build();
410     }
411 
412     /**
413      * Create cbcMacSpec.
414      *
415      * @param pSymKeySpec the symKeySpec
416      * @return the MacSpec
417      */
418     default GordianMacSpec cbcMac(final GordianSymKeySpec pSymKeySpec) {
419         return withType(GordianMacType.CBCMAC).withSymKeySubSpec(pSymKeySpec).build();
420     }
421 
422     /**
423      * Create cfbMacSpec.
424      *
425      * @param pSymKeySpec the symKeySpec
426      * @return the MacSpec
427      */
428     default GordianMacSpec cfbMac(final GordianSymKeySpec pSymKeySpec) {
429         return withType(GordianMacType.CFBMAC).withSymKeySubSpec(pSymKeySpec).build();
430     }
431 
432     /**
433      * Create zucMacSpec.
434      *
435      * @param pKeyLength the keyLength
436      * @param pLength    the length
437      * @return the MacSpec
438      */
439     default GordianMacSpec zucMac(final GordianLength pKeyLength,
440                                   final GordianLength pLength) {
441         return withType(GordianMacType.ZUC).withKeyLength(pKeyLength).withLengthSubSpec(pLength).build();
442     }
443 }