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.cipher;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.base.GordianKeySpec;
20 import io.github.tonywasher.joceanus.gordianknot.api.key.GordianKey;
21
22 /**
23 * Cipher Parameters.
24 */
25 public interface GordianCipherParameters {
26 /**
27 * Obtain keySpec Parameters.
28 *
29 * @param <T> the keyType
30 * @param pKey the key
31 * @return the keySpec
32 */
33 static <T extends GordianKeySpec> GordianKeyCipherParameters<T> key(final GordianKey<T> pKey) {
34 return new GordianKeyCipherParameters<>(pKey);
35 }
36
37 /**
38 * Obtain keyAndNonce Parameters.
39 *
40 * @param <T> the keyType
41 * @param pKey the key
42 * @param pNonce the nonce
43 * @return the keySpec
44 */
45 static <T extends GordianKeySpec> GordianKeyCipherParameters<T> keyAndNonce(final GordianKey<T> pKey,
46 final byte[] pNonce) {
47 return new GordianKeyAndNonceCipherParameters<>(pKey, pNonce);
48 }
49
50 /**
51 * Obtain keyAndRandomNonce Parameters.
52 *
53 * @param <T> the keyType
54 * @param pKey the key
55 * @return the keySpec
56 */
57 static <T extends GordianKeySpec> GordianKeyCipherParameters<T> keyWithRandomNonce(final GordianKey<T> pKey) {
58 return new GordianKeyAndNonceCipherParameters<>(pKey);
59 }
60
61 /**
62 * Obtain aeadAndNonce Parameters.
63 *
64 * @param <T> the keyType
65 * @param pKey the key
66 * @param pInitialAEAD the initialAEAD
67 * @param pNonce the nonce
68 * @return the keySpec
69 */
70 static <T extends GordianKeySpec> GordianAEADCipherParameters<T> aeadAndNonce(final GordianKey<T> pKey,
71 final byte[] pInitialAEAD,
72 final byte[] pNonce) {
73 return new GordianAEADCipherParameters<>(pKey, pNonce, pInitialAEAD);
74 }
75
76 /**
77 * Obtain aeadAndRandomNonce Parameters.
78 *
79 * @param <T> the keyType
80 * @param pKey the key
81 * @param pInitialAEAD the initialAEAD
82 * @return the keySpec
83 */
84 static <T extends GordianKeySpec> GordianAEADCipherParameters<T> aeadWithRandomNonce(final GordianKey<T> pKey,
85 final byte[] pInitialAEAD) {
86 return new GordianAEADCipherParameters<>(pKey, null, pInitialAEAD);
87 }
88
89 /**
90 * Obtain pbe Parameters.
91 *
92 * @param pPBESpec the pbeSpec
93 * @param pPassword the password
94 * @return the keySpec
95 */
96 static GordianPBECipherParameters pbe(final GordianPBESpec pPBESpec,
97 final char[] pPassword) {
98 return new GordianPBECipherParameters(pPBESpec, pPassword);
99 }
100
101 /**
102 * Obtain pneAndNonce Parameters.
103 *
104 * @param pPBESpec the pbeSpec
105 * @param pPassword the password
106 * @param pNonce the nonce
107 * @return the keySpec
108 */
109 static GordianPBECipherParameters pbeAndNonce(final GordianPBESpec pPBESpec,
110 final char[] pPassword,
111 final byte[] pNonce) {
112 return new GordianPBECipherParameters(pPBESpec, pNonce, pPassword);
113 }
114
115 /**
116 * Key Parameters.
117 *
118 * @param <T> the keyType
119 */
120 class GordianKeyCipherParameters<T extends GordianKeySpec>
121 implements GordianCipherParameters {
122 /**
123 * The Key.
124 */
125 private final GordianKey<T> theKey;
126
127 /**
128 * Constructor.
129 *
130 * @param pKey the key
131 */
132 GordianKeyCipherParameters(final GordianKey<T> pKey) {
133 theKey = pKey;
134 }
135
136 /**
137 * Obtain the key.
138 *
139 * @return the key
140 */
141 public GordianKey<T> getKey() {
142 return theKey;
143 }
144 }
145
146 /**
147 * Nonce Parameters.
148 */
149 interface GordianNonceParameters {
150 /**
151 * Was a random Nonce requested?.
152 *
153 * @return true/false
154 */
155 boolean randomNonce();
156
157 /**
158 * Obtain the nonce.
159 *
160 * @return the nonce
161 */
162 byte[] getNonce();
163 }
164
165 /**
166 * KeyAndNonce Parameters.
167 *
168 * @param <T> the keyType
169 */
170 class GordianKeyAndNonceCipherParameters<T extends GordianKeySpec>
171 extends GordianKeyCipherParameters<T>
172 implements GordianNonceParameters {
173 /**
174 * The Nonce.
175 */
176 private final byte[] theNonce;
177
178 /**
179 * Random Nonce requested?
180 */
181 private final boolean randomNonce;
182
183 /**
184 * Constructor for random nonce.
185 *
186 * @param pKey the key
187 */
188 GordianKeyAndNonceCipherParameters(final GordianKey<T> pKey) {
189 super(pKey);
190 theNonce = null;
191 randomNonce = true;
192 }
193
194 /**
195 * Constructor.
196 *
197 * @param pKey the key
198 * @param pNonce the nonce
199 */
200 GordianKeyAndNonceCipherParameters(final GordianKey<T> pKey,
201 final byte[] pNonce) {
202 super(pKey);
203 theNonce = pNonce;
204 randomNonce = false;
205 }
206
207 @Override
208 public byte[] getNonce() {
209 return theNonce;
210 }
211
212 @Override
213 public boolean randomNonce() {
214 return randomNonce;
215 }
216 }
217
218 /**
219 * AEAD Parameters.
220 *
221 * @param <T> the keyType
222 */
223 class GordianAEADCipherParameters<T extends GordianKeySpec>
224 extends GordianKeyAndNonceCipherParameters<T> {
225 /**
226 * The InitialAEAD.
227 */
228 private final byte[] theInitialAEAD;
229
230 /**
231 * Constructor.
232 *
233 * @param pKey the key
234 * @param pInitialAEAD the initialAEAD
235 */
236 GordianAEADCipherParameters(final GordianKey<T> pKey,
237 final byte[] pInitialAEAD) {
238 super(pKey);
239 theInitialAEAD = pInitialAEAD;
240 }
241
242 /**
243 * Constructor.
244 *
245 * @param pKey the key
246 * @param pNonce the nonce
247 * @param pInitialAEAD the initialAEAD
248 */
249 GordianAEADCipherParameters(final GordianKey<T> pKey,
250 final byte[] pNonce,
251 final byte[] pInitialAEAD) {
252 super(pKey, pNonce);
253 theInitialAEAD = pInitialAEAD;
254 }
255
256 /**
257 * Obtain the initialAEAD.
258 *
259 * @return the initialAEAD
260 */
261 public byte[] getInitialAEAD() {
262 return theInitialAEAD;
263 }
264 }
265
266 /**
267 * PBE Parameters.
268 */
269 class GordianPBECipherParameters
270 implements GordianCipherParameters, GordianNonceParameters {
271 /**
272 * The PBESpec.
273 */
274 private final GordianPBESpec thePBESpec;
275
276 /**
277 * The Nonce.
278 */
279 private final byte[] theNonce;
280
281 /**
282 * Random Nonce requested?
283 */
284 private final boolean randomNonce;
285
286 /**
287 * The Password.
288 */
289 private final char[] thePassword;
290
291 /**
292 * Constructor for random nonce.
293 *
294 * @param pPBESpec the PBESpec
295 * @param pPassword the password
296 */
297 GordianPBECipherParameters(final GordianPBESpec pPBESpec,
298 final char[] pPassword) {
299 thePBESpec = pPBESpec;
300 theNonce = null;
301 randomNonce = true;
302 thePassword = pPassword;
303 }
304
305 /**
306 * Constructor.
307 *
308 * @param pPBESpec the PBESpec
309 * @param pNonce the nonce
310 * @param pPassword the password
311 */
312 GordianPBECipherParameters(final GordianPBESpec pPBESpec,
313 final byte[] pNonce,
314 final char[] pPassword) {
315 thePBESpec = pPBESpec;
316 theNonce = pNonce;
317 randomNonce = false;
318 thePassword = pPassword;
319 }
320
321 /**
322 * Obtain the PBESpec.
323 *
324 * @return the PBESpec
325 */
326 public GordianPBESpec getPBESpec() {
327 return thePBESpec;
328 }
329
330 @Override
331 public byte[] getNonce() {
332 return theNonce;
333 }
334
335 @Override
336 public boolean randomNonce() {
337 return randomNonce;
338 }
339
340 /**
341 * Obtain the password.
342 *
343 * @return the password
344 */
345 public char[] getPassword() {
346 return thePassword;
347 }
348 }
349 }