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.CipherParameters;
21  import org.bouncycastle.util.Arrays;
22  
23  /**
24   * Blake2 Parameters.
25   */
26  public class GordianBlake2Parameters
27          implements CipherParameters {
28      /**
29       * The key.
30       */
31      private byte[] theKey;
32  
33      /**
34       * The salt.
35       */
36      private byte[] theSalt;
37  
38      /**
39       * The personalisation.
40       */
41      private byte[] thePersonal;
42  
43      /**
44       * The maximum xofLen.
45       */
46      private long theMaxXofLen;
47  
48      /**
49       * The fanOut.
50       */
51      private short theFanOut;
52  
53      /**
54       * The maxDepth.
55       */
56      private short theMaxDepth;
57  
58      /**
59       * The leafLength.
60       */
61      private int theLeafLen;
62  
63      /**
64       * Obtain the key.
65       *
66       * @return the key
67       */
68      public byte[] getKey() {
69          return Arrays.clone(theKey);
70      }
71  
72      /**
73       * Obtain the salt.
74       *
75       * @return the salt
76       */
77      public byte[] getSalt() {
78          return Arrays.clone(theSalt);
79      }
80  
81      /**
82       * Obtain the personalisation.
83       *
84       * @return the personalisation
85       */
86      public byte[] getPersonalisation() {
87          return Arrays.clone(thePersonal);
88      }
89  
90      /**
91       * Obtain the maximum output length.
92       *
93       * @return the output length
94       */
95      public long getMaxOutputLength() {
96          return theMaxXofLen;
97      }
98  
99      /**
100      * Obtain the treeLeafLength.
101      *
102      * @return the leafLength
103      */
104     public int getTreeLeafLen() {
105         return theLeafLen;
106     }
107 
108     /**
109      * Obtain the treeFanOut.
110      *
111      * @return the fanOut
112      */
113     public short getTreeFanOut() {
114         return theFanOut;
115     }
116 
117     /**
118      * Obtain the treeMaxDepth.
119      *
120      * @return the maxDepth
121      */
122     public short getTreeMaxDepth() {
123         return theMaxDepth;
124     }
125 
126     /**
127      * Parameter Builder.
128      */
129     public static class GordianBlake2ParametersBuilder {
130         /**
131          * The key.
132          */
133         private byte[] theKey;
134 
135         /**
136          * The salt.
137          */
138         private byte[] theSalt;
139 
140         /**
141          * The personalisation.
142          */
143         private byte[] thePersonal;
144 
145         /**
146          * The maximum xofLen.
147          */
148         private long theMaxXofLen;
149 
150         /**
151          * The fanOut.
152          */
153         private short theFanOut = 1;
154 
155         /**
156          * The maxDepth.
157          */
158         private short theMaxDepth = 1;
159 
160         /**
161          * The leafLength.
162          */
163         private int theLeafLen;
164 
165         /**
166          * Set the key.
167          *
168          * @param pKey the key
169          * @return the Builder
170          */
171         public GordianBlake2ParametersBuilder setKey(final byte[] pKey) {
172             theKey = Arrays.clone(pKey);
173             return this;
174         }
175 
176         /**
177          * Set the salt.
178          *
179          * @param pSalt the salt
180          * @return the Builder
181          */
182         public GordianBlake2ParametersBuilder setSalt(final byte[] pSalt) {
183             theSalt = Arrays.clone(pSalt);
184             return this;
185         }
186 
187         /**
188          * Set the personalisation.
189          *
190          * @param pPersonal the personalisation
191          * @return the Builder
192          */
193         public GordianBlake2ParametersBuilder setPersonalisation(final byte[] pPersonal) {
194             thePersonal = Arrays.clone(pPersonal);
195             return this;
196         }
197 
198         /**
199          * Set the maximum output length. (-1=unlimited)
200          *
201          * @param pMaxOutLen the maximum output length
202          * @return the Builder
203          */
204         public GordianBlake2ParametersBuilder setMaxOutputLen(final long pMaxOutLen) {
205             theMaxXofLen = pMaxOutLen;
206             return this;
207         }
208 
209         /**
210          * Set the treeConfig.
211          *
212          * @param pFanOut   the fanOut (0=unlimited, 1-255).
213          * @param pMaxDepth the maxDepth (2-255).
214          * @param pLeafLen  the leafLength (in bytes).
215          * @return the Builder
216          */
217         public GordianBlake2ParametersBuilder setTreeConfig(final int pFanOut,
218                                                             final int pMaxDepth,
219                                                             final int pLeafLen) {
220             theFanOut = (short) pFanOut;
221             theMaxDepth = (short) pMaxDepth;
222             theLeafLen = pLeafLen;
223             return this;
224         }
225 
226         /**
227          * Build the parameters.
228          *
229          * @return the parameters
230          */
231         public GordianBlake2Parameters build() {
232             /* Create params */
233             final GordianBlake2Parameters myParams = new GordianBlake2Parameters();
234 
235             /* Record key and Salt */
236             if (theKey != null) {
237                 myParams.theKey = theKey;
238             }
239             if (theSalt != null) {
240                 myParams.theSalt = theSalt;
241             }
242 
243             /* Record personalisation and xof length */
244             if (thePersonal != null) {
245                 myParams.thePersonal = thePersonal;
246             }
247             myParams.theMaxXofLen = theMaxXofLen;
248 
249             /* Record tree details */
250             myParams.theFanOut = theFanOut;
251             myParams.theMaxDepth = theMaxDepth;
252             myParams.theLeafLen = theLeafLen;
253 
254             /* Return the parameters */
255             return myParams;
256         }
257     }
258 }