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.impl.ext.params;
19  
20  import org.bouncycastle.crypto.params.SkeinParameters;
21  
22  import java.util.Date;
23  import java.util.Enumeration;
24  import java.util.Hashtable;
25  import java.util.Locale;
26  
27  /**
28   * Extended Skein Parameters.
29   */
30  public class GordianSkeinParameters
31          extends SkeinParameters {
32      /**
33       * The maximum xofLen.
34       */
35      private long theMaxXofLen;
36  
37      /**
38       * The fanOut.
39       */
40      private short theFanOut;
41  
42      /**
43       * The maxDepth.
44       */
45      private short theMaxDepth;
46  
47      /**
48       * The leafLength.
49       */
50      private int theLeafLen;
51  
52      /**
53       * Obtain the maximum output length.
54       *
55       * @return the output length
56       */
57      public long getMaxOutputLength() {
58          return theMaxXofLen;
59      }
60  
61      /**
62       * Obtain the treeLeafLength.
63       *
64       * @return the leafLength
65       */
66      public int getTreeLeafLen() {
67          return theLeafLen;
68      }
69  
70      /**
71       * Obtain the treeFanOut.
72       *
73       * @return the fanOut
74       */
75      public short getTreeFanOut() {
76          return theFanOut;
77      }
78  
79      /**
80       * Obtain the treeMaxDepth.
81       *
82       * @return the maxDepth
83       */
84      public short getTreeMaxDepth() {
85          return theMaxDepth;
86      }
87  
88      /**
89       * A builder for {@link GordianSkeinParameters}.
90       */
91      public static class GordianSkeinParametersBuilder
92              extends SkeinParameters.Builder {
93          /**
94           * The maximum xofLen.
95           */
96          private long theMaxXofLen;
97  
98          /**
99           * The fanOut.
100          */
101         private short theFanOut;
102 
103         /**
104          * The maxDepth.
105          */
106         private short theMaxDepth;
107 
108         /**
109          * The leafLength.
110          */
111         private int theLeafLen;
112 
113         @Override
114         public GordianSkeinParametersBuilder set(final int type,
115                                                  final byte[] value) {
116             return (GordianSkeinParametersBuilder) super.set(type, value);
117         }
118 
119         @Override
120         public GordianSkeinParametersBuilder setKey(final byte[] key) {
121             return (GordianSkeinParametersBuilder) super.setKey(key);
122         }
123 
124         @Override
125         public GordianSkeinParametersBuilder setPersonalisation(final byte[] personalisation) {
126             return (GordianSkeinParametersBuilder) super.setPersonalisation(personalisation);
127         }
128 
129         @Override
130         public GordianSkeinParametersBuilder setPersonalisation(final Date date,
131                                                                 final String emailAddress,
132                                                                 final String distinguisher) {
133             return (GordianSkeinParametersBuilder) super.setPersonalisation(date, emailAddress, distinguisher);
134         }
135 
136         @Override
137         public GordianSkeinParametersBuilder setPersonalisation(final Date date,
138                                                                 final Locale dateLocale,
139                                                                 final String emailAddress,
140                                                                 final String distinguisher) {
141             return (GordianSkeinParametersBuilder) super.setPersonalisation(date, dateLocale, emailAddress, distinguisher);
142         }
143 
144         @Override
145         public GordianSkeinParametersBuilder setPublicKey(final byte[] publicKey) {
146             return (GordianSkeinParametersBuilder) super.setPublicKey(publicKey);
147         }
148 
149         @Override
150         public GordianSkeinParametersBuilder setKeyIdentifier(final byte[] keyId) {
151             return (GordianSkeinParametersBuilder) super.setKeyIdentifier(keyId);
152         }
153 
154         @Override
155         public GordianSkeinParametersBuilder setNonce(final byte[] nonce) {
156             return (GordianSkeinParametersBuilder) super.setNonce(nonce);
157         }
158 
159         /**
160          * Set the maximum output length. (-1=unlimited, 0=underlying)
161          *
162          * @param pMaxOutLen the maximum output length
163          * @return the Builder
164          */
165         public GordianSkeinParametersBuilder setMaxOutputLen(final long pMaxOutLen) {
166             theMaxXofLen = pMaxOutLen;
167             return this;
168         }
169 
170         /**
171          * Set the treeConfig.
172          *
173          * @param pFanOut   the fanOut (powers of two - 1-255).
174          * @param pMaxDepth the maxDepth (2-255).
175          * @param pLeafLen  the leafLength (powers of two times outputLength - 1-255).
176          * @return the Builder
177          */
178         public GordianSkeinParametersBuilder setTreeConfig(final int pFanOut,
179                                                            final int pMaxDepth,
180                                                            final int pLeafLen) {
181             theFanOut = (short) pFanOut;
182             theMaxDepth = (short) pMaxDepth;
183             theLeafLen = pLeafLen;
184             return this;
185         }
186 
187         @Override
188         @SuppressWarnings("unchecked")
189         public GordianSkeinParameters build() {
190             /* Build base parameters */
191             final SkeinParameters myBaseParms = super.build();
192             final GordianSkeinParameters myParams = new GordianSkeinParameters();
193 
194             /* Store base details */
195             final Hashtable<Integer, Object> myBaseStore = myBaseParms.getParameters();
196             final Hashtable<Integer, Object> myStore = myParams.getParameters();
197             final Enumeration<Integer> keys = myBaseStore.keys();
198             while (keys.hasMoreElements()) {
199                 final Integer key = keys.nextElement();
200                 myStore.put(key, myBaseStore.get(key));
201             }
202 
203             /* Record XofDetails */
204             myParams.theMaxXofLen = theMaxXofLen;
205 
206             /* Record tree details */
207             myParams.theFanOut = theFanOut;
208             myParams.theMaxDepth = theMaxDepth;
209             myParams.theLeafLen = theLeafLen;
210             return myParams;
211         }
212     }
213 }