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.mac;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.key.GordianKey;
20 import org.bouncycastle.util.Arrays;
21
22 /**
23 * Mac Parameters.
24 */
25 public final class GordianMacParameters {
26 /**
27 * The Key.
28 */
29 private GordianKey<GordianMacSpec> theKey;
30
31 /**
32 * The Nonce.
33 */
34 private byte[] theNonce;
35
36 /**
37 * Random Nonce requested?
38 */
39 private boolean randomNonce;
40
41 /**
42 * Personalisation.
43 */
44 private byte[] thePersonal;
45
46 /**
47 * Output length.
48 */
49 private long theOutLen;
50
51 /**
52 * The fanOut.
53 */
54 private short theFanOut;
55
56 /**
57 * The maxDepth.
58 */
59 private short theMaxDepth;
60
61 /**
62 * The leafLength.
63 */
64 private int theLeafLen;
65
66 /**
67 * Constructor.
68 */
69 private GordianMacParameters() {
70 }
71
72 /**
73 * Generate keyOnly Parameters.
74 *
75 * @param pKey the key
76 * @return the macParameters
77 */
78 public static GordianMacParameters key(final GordianKey<GordianMacSpec> pKey) {
79 final GordianMacParameters myParams = new GordianMacParameters();
80 myParams.theKey = pKey;
81 return myParams;
82 }
83
84 /**
85 * Obtain keyAndNonce Parameters.
86 *
87 * @param pKey the key
88 * @param pNonce the nonce
89 * @return the macParameters
90 */
91 public static GordianMacParameters keyAndNonce(final GordianKey<GordianMacSpec> pKey,
92 final byte[] pNonce) {
93 final GordianMacParameters myParams = new GordianMacParameters();
94 myParams.theKey = pKey;
95 myParams.theNonce = Arrays.clone(pNonce);
96 return myParams;
97 }
98
99 /**
100 * Obtain keyAndRandomNonce Parameters.
101 *
102 * @param pKey the key
103 * @return the macParameters
104 */
105 public static GordianMacParameters keyWithRandomNonce(final GordianKey<GordianMacSpec> pKey) {
106 final GordianMacParameters myParams = new GordianMacParameters();
107 myParams.theKey = pKey;
108 myParams.randomNonce = true;
109 return myParams;
110 }
111
112 /**
113 * Obtain the key.
114 *
115 * @return the key
116 */
117 public GordianKey<GordianMacSpec> getKey() {
118 return theKey;
119 }
120
121 /**
122 * Obtain the Nonce.
123 *
124 * @return the nonce
125 */
126 public byte[] getNonce() {
127 return Arrays.clone(theNonce);
128 }
129
130 /**
131 * Is the nonce randomly generated?
132 *
133 * @return true/false
134 */
135 public boolean randomNonce() {
136 return randomNonce;
137 }
138
139 /**
140 * Obtain the Personalisation.
141 *
142 * @return the personalisation
143 */
144 public byte[] getPersonal() {
145 return Arrays.clone(thePersonal);
146 }
147
148 /**
149 * Obtain the Output length.
150 *
151 * @return the outLength
152 */
153 public long getOutputLength() {
154 return theOutLen;
155 }
156
157 /**
158 * Obtain the treeLeafLength.
159 *
160 * @return the leafLength
161 */
162 public int getTreeLeafLen() {
163 return theLeafLen;
164 }
165
166 /**
167 * Obtain the treeFanOut.
168 *
169 * @return the fanOut
170 */
171 public short getTreeFanOut() {
172 return theFanOut;
173 }
174
175 /**
176 * Obtain the treeMaxDepth.
177 *
178 * @return the maxDepth
179 */
180 public short getTreeMaxDepth() {
181 return theMaxDepth;
182 }
183
184 /**
185 * Parameter Builder.
186 */
187 public static class GordianMacParametersBuilder {
188 /**
189 * The Key.
190 */
191 private GordianKey<GordianMacSpec> theKey;
192
193 /**
194 * The Nonce.
195 */
196 private byte[] theNonce;
197
198 /**
199 * Random Nonce requested?
200 */
201 private boolean randomNonce;
202
203 /**
204 * Personalisation.
205 */
206 private byte[] thePersonal;
207
208 /**
209 * Output length.
210 */
211 private long theOutLen;
212
213 /**
214 * The fanOut.
215 */
216 private short theFanOut = 1;
217
218 /**
219 * The maxDepth.
220 */
221 private short theMaxDepth = 1;
222
223 /**
224 * The leafLength.
225 */
226 private int theLeafLen;
227
228 /**
229 * Constructor.
230 */
231 public GordianMacParametersBuilder() {
232 }
233
234 /**
235 * Set the key.
236 *
237 * @param pKey the key
238 * @return the Builder
239 */
240 GordianMacParametersBuilder setKey(final GordianKey<GordianMacSpec> pKey) {
241 theKey = pKey;
242 return this;
243 }
244
245 /**
246 * Set the nonce.
247 *
248 * @param pNonce the nonce
249 * @return the Builder
250 */
251 GordianMacParametersBuilder setNonce(final byte[] pNonce) {
252 theNonce = Arrays.clone(pNonce);
253 randomNonce = false;
254 return this;
255 }
256
257 /**
258 * Use random nonce.
259 *
260 * @return the Builder
261 */
262 GordianMacParametersBuilder withRandomNonce() {
263 theNonce = null;
264 randomNonce = true;
265 return this;
266 }
267
268 /**
269 * Set the personalisation.
270 *
271 * @param pPersonal the personalisation
272 * @return the Builder
273 */
274 GordianMacParametersBuilder setPersonalisation(final byte[] pPersonal) {
275 thePersonal = Arrays.clone(pPersonal);
276 return this;
277 }
278
279 /**
280 * Set the output length.
281 *
282 * @param pOutLen the outputLen
283 * @return the Builder
284 */
285 GordianMacParametersBuilder setOutputLength(final long pOutLen) {
286 theOutLen = pOutLen;
287 return this;
288 }
289
290 /**
291 * Set the treeConfig.
292 *
293 * @param pFanOut the fanout.
294 * @param pMaxDepth the maxDepth.
295 * @param pLeafLen the leafLength.
296 * @return the Builder
297 */
298 public GordianMacParametersBuilder setTreeConfig(final int pFanOut,
299 final int pMaxDepth,
300 final int pLeafLen) {
301 theFanOut = (short) pFanOut;
302 theMaxDepth = (short) pMaxDepth;
303 theLeafLen = pLeafLen;
304 return this;
305 }
306
307 /**
308 * Build the parameters.
309 *
310 * @return the parameters
311 */
312 public GordianMacParameters build() {
313 /* Create params */
314 final GordianMacParameters myParams = new GordianMacParameters();
315
316 /* Record key and Nonce */
317 if (theKey != null) {
318 myParams.theKey = theKey;
319 }
320 if (theNonce != null) {
321 myParams.theNonce = theNonce;
322 } else if (randomNonce) {
323 myParams.randomNonce = true;
324 }
325
326 /* Record personalisation and output length */
327 if (thePersonal != null) {
328 myParams.thePersonal = thePersonal;
329 }
330 myParams.theOutLen = theOutLen;
331
332 /* Record tree details */
333 myParams.theFanOut = theFanOut;
334 myParams.theMaxDepth = theMaxDepth;
335 myParams.theLeafLen = theLeafLen;
336
337 /* Return the parameters */
338 return myParams;
339 }
340 }
341 }