Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AEAD/Encrypt.java
66649 views
1
/*
2
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.nio.ByteBuffer;
25
import java.security.AlgorithmParameters;
26
import java.security.Provider;
27
import java.security.Security;
28
import java.util.ArrayList;
29
import java.util.Arrays;
30
import java.util.HexFormat;
31
import java.util.List;
32
import javax.crypto.SecretKey;
33
import javax.crypto.Cipher;
34
import javax.crypto.KeyGenerator;
35
36
/*
37
* @test
38
* @bug 8048596
39
* @summary AEAD encryption/decryption test
40
*/
41
42
/*
43
* The test does the following:
44
* - create an input text and additional data
45
* - generate a secret key
46
* - instantiate a cipher according to the GCM transformation
47
* - generate an outputText using a single-part encryption/decryption
48
* in AEAD mode
49
* - perform 16 different combinations of multiple-part encryption/decryption
50
* operation in AEAD mode (in encryption mode new Cipher object is created
51
* and initialized with the same secret key and parameters)
52
* - check that all 17 results are equal
53
*
54
* Combinations:
55
*
56
* combination #1
57
* updateAAD(byte[] src)
58
* update(byte[], int, int)
59
* doFinal(byte[], int, int)
60
*
61
* combination #2
62
* updateAAD(byte[] src)
63
* update(byte[], int, int)
64
* doFinal(byte[], int, int, byte[], int)
65
*
66
* combination #3
67
* updateAAD(byte[] src)
68
* update(byte[], int, int, byte[], int)
69
* doFinal(byte[], int, int)
70
*
71
* combination #4
72
* updateAAD(byte[] src)
73
* update(byte[], int, int, byte[], int)
74
* doFinal(byte[], int, int, byte[], int)
75
*
76
* combination #5 - #8 are similar to #1 -#4,
77
* but with updateAAD(byte[] src, int offset, int len)
78
*
79
* combination #9 - #12 are similar to #1 - #4,
80
* but with updateAAD(ByteBuffer src)
81
*
82
* combination #13 - #16 are similar to #9 - #12 but with directly allocated
83
* ByteBuffer and update(ByteBuffer input, ByteBuffer output)
84
*
85
*/
86
public class Encrypt {
87
88
private static final String ALGORITHMS[] = { "AES" };
89
private static final int KEY_STRENGTHS[] = { 128, 192, 256 };
90
private static final int TEXT_LENGTHS[] = { 0, 256, 1024 };
91
private static final int AAD_LENGTHS[] = { 0, 8, 128, 256, 1024 };
92
private static final int ARRAY_OFFSET = 8;
93
94
private final String transformation;
95
private final Provider provider;
96
private final SecretKey key;
97
private final int textLength;
98
private final int AADLength;
99
100
/**
101
* @param provider Security provider
102
* @param algorithm Security algorithm to test
103
* @param mode The mode (GCM is only expected)
104
* @param padding Algorithm padding
105
* @param keyStrength key length
106
* @param textLength Plain text length
107
* @param AADLength Additional data length
108
*/
109
public Encrypt(Provider provider, String algorithm, String mode,
110
String padding, int keyStrength, int textLength, int AADLength)
111
throws Exception {
112
113
// init a secret Key
114
KeyGenerator kg = KeyGenerator.getInstance(algorithm, provider);
115
kg.init(keyStrength);
116
key = kg.generateKey();
117
118
this.provider = provider;
119
this.transformation = algorithm + "/" + mode + "/" + padding;
120
this.textLength = textLength;
121
this.AADLength = AADLength;
122
}
123
124
public static void main(String[] args) throws Exception {
125
Provider p = Security.getProvider("SunJCE");
126
for (String alg : ALGORITHMS) {
127
for (int keyStrength : KEY_STRENGTHS) {
128
if (keyStrength > Cipher.getMaxAllowedKeyLength(alg)) {
129
// skip this if this key length is larger than what's
130
// configured in the JCE jurisdiction policy files
131
continue;
132
}
133
for (int textLength : TEXT_LENGTHS) {
134
for (int AADLength : AAD_LENGTHS) {
135
Encrypt test = new Encrypt(p, alg,
136
"GCM", "NoPadding", keyStrength, textLength,
137
AADLength);
138
Cipher cipher = test.createCipher(Cipher.ENCRYPT_MODE,
139
null);
140
AlgorithmParameters params = cipher.getParameters();
141
test.doTest(params);
142
System.out.println("Test " + alg + ":"
143
+ keyStrength + ":" + textLength + ":"
144
+ AADLength + " passed");
145
}
146
}
147
}
148
}
149
}
150
151
public void doTest(AlgorithmParameters params) throws Exception {
152
System.out.println("Test transformation = " + transformation
153
+ ", textLength = " + textLength
154
+ ", AADLength = " + AADLength);
155
byte[] input = Helper.generateBytes(textLength);
156
byte[] AAD = Helper.generateBytes(AADLength);
157
byte[] result = execute(Cipher.ENCRYPT_MODE, AAD, input, params);
158
result = execute(Cipher.DECRYPT_MODE, AAD, result, params);
159
if (!Arrays.equals(input, result)) {
160
throw new RuntimeException("Test failed");
161
}
162
System.out.println("Test passed");
163
}
164
165
/**
166
* Create a Cipher object for the requested encryption/decryption mode.
167
*
168
* @param mode encryption or decryption mode
169
* @return Cipher object initiated to perform requested mode operation
170
*/
171
private Cipher createCipher(int mode, AlgorithmParameters params)
172
throws Exception {
173
Cipher ci;
174
if (Cipher.ENCRYPT_MODE == mode) {
175
// create a new Cipher object for encryption
176
ci = Cipher.getInstance(transformation, provider);
177
178
// initiate it with the saved parameters
179
if (params != null) {
180
ci.init(Cipher.ENCRYPT_MODE, key, params);
181
} else {
182
// initiate the cipher without parameters
183
ci.init(Cipher.ENCRYPT_MODE, key);
184
}
185
} else {
186
// it is expected that parameters already generated
187
// before decryption
188
ci = Cipher.getInstance(transformation, provider);
189
ci.init(Cipher.DECRYPT_MODE, key, params);
190
}
191
192
return ci;
193
}
194
195
/**
196
* Test AEAD combinations
197
*
198
* @param mode decryption or encryption
199
* @param AAD additional data for AEAD operations
200
* @param inputText plain text to decrypt/encrypt
201
* @return output text after encrypt/decrypt
202
*/
203
public byte[] execute(int mode, byte[] AAD, byte[] inputText,
204
AlgorithmParameters params) throws Exception {
205
206
Cipher cipher = createCipher(mode, params);
207
208
// results of each combination will be saved in the outputTexts
209
List<byte[]> outputTexts = new ArrayList<>();
210
211
// generate a standard outputText using a single-part en/de-cryption
212
cipher.updateAAD(AAD);
213
byte[] output = cipher.doFinal(inputText);
214
215
// execute multiple-part encryption/decryption combinations
216
combination_1(outputTexts, mode, AAD, inputText, params);
217
combination_2(outputTexts, mode, AAD, inputText, params);
218
combination_3(outputTexts, mode, AAD, inputText, params);
219
combination_4(outputTexts, mode, AAD, inputText, params);
220
combination_5(outputTexts, mode, AAD, inputText, params);
221
combination_6(outputTexts, mode, AAD, inputText, params);
222
combination_7(outputTexts, mode, AAD, inputText, params);
223
combination_8(outputTexts, mode, AAD, inputText, params);
224
combination_9(outputTexts, mode, AAD, inputText, params);
225
combination_10(outputTexts, mode, AAD, inputText, params);
226
combination_11(outputTexts, mode, AAD, inputText, params);
227
combination_12(outputTexts, mode, AAD, inputText, params);
228
combination_13(outputTexts, mode, AAD, inputText, params);
229
combination_14(outputTexts, mode, AAD, inputText, params);
230
combination_15(outputTexts, mode, AAD, inputText, params);
231
combination_16(outputTexts, mode, AAD, inputText, params);
232
233
for (int k = 0; k < outputTexts.size(); k++) {
234
HexFormat hex = HexFormat.of().withUpperCase();
235
if (!Arrays.equals(output, outputTexts.get(k))) {
236
System.out.println("Combination #" + (k + 1) + "\nresult " +
237
hex.formatHex(outputTexts.get(k)) +
238
"\nexpected: " + hex.formatHex(output));
239
throw new RuntimeException("Combination #" + (k + 1) + " failed");
240
}
241
}
242
return output;
243
}
244
245
/*
246
* Execute multiple-part encryption/decryption combination #1:
247
* updateAAD(byte[] src)
248
* update(byte[], int, int)
249
* doFinal(byte[], int, int)
250
*/
251
private void combination_1(List<byte[]> results, int mode, byte[] AAD,
252
byte[] plainText, AlgorithmParameters params) throws Exception {
253
Cipher c = createCipher(mode, params);
254
c.updateAAD(AAD);
255
byte[] part11 = c.update(plainText, 0, plainText.length);
256
int part11_length = part11 == null ? 0 : part11.length;
257
byte[] part12 = c.doFinal();
258
byte[] outputText1 = new byte[part11_length + part12.length];
259
if (part11 != null) {
260
System.arraycopy(part11, 0, outputText1, 0, part11_length);
261
}
262
System.arraycopy(part12, 0, outputText1, part11_length, part12.length);
263
results.add(outputText1);
264
}
265
266
/*
267
* Execute multiple-part encryption/decryption combination #2:
268
* updateAAD(byte[] src)
269
* update(byte[], int, int)
270
* doFinal(byte[], int, int, byte[], int)
271
*/
272
private void combination_2(List<byte[]> results, int mode, byte[] AAD,
273
byte[] plainText, AlgorithmParameters params) throws Exception {
274
Cipher c = createCipher(mode, params);
275
c.updateAAD(AAD);
276
int t = 0;
277
int offset = 0;
278
if (plainText.length > ARRAY_OFFSET) {
279
t = plainText.length - ARRAY_OFFSET;
280
offset = ARRAY_OFFSET;
281
}
282
byte[] part21 = c.update(plainText, 0, t);
283
byte[] part22 = new byte[c.getOutputSize(plainText.length)];
284
int len2 = c.doFinal(plainText, t, offset, part22, 0);
285
int part21Length = part21 != null ? part21.length : 0;
286
byte[] outputText2 = new byte[part21Length + len2];
287
if (part21 != null) {
288
System.arraycopy(part21, 0, outputText2, 0, part21Length);
289
}
290
System.arraycopy(part22, 0, outputText2, part21Length, len2);
291
results.add(outputText2);
292
}
293
294
/*
295
* Execute multiple-part encryption/decryption combination #3
296
* updateAAD(byte[] src)
297
* update(byte[], int, int, byte[], int)
298
* doFinal(byte[], int, int)
299
*/
300
private void combination_3(List<byte[]> results, int mode, byte[] AAD,
301
byte[] plainText, AlgorithmParameters params) throws Exception {
302
Cipher ci = createCipher(mode, params);
303
ci.updateAAD(AAD);
304
byte[] part31 = new byte[ci.getOutputSize(plainText.length)];
305
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
306
int len = ci.update(plainText, 0, plainText.length - offset, part31, 0);
307
byte[] part32 = ci.doFinal(plainText, plainText.length - offset,
308
offset);
309
byte[] outputText3 = new byte[len + part32.length];
310
System.arraycopy(part31, 0, outputText3, 0, len);
311
System.arraycopy(part32, 0, outputText3, len, part32.length);
312
results.add(outputText3);
313
}
314
315
/*
316
* Execute multiple-part encryption/decryption combination #4:
317
* updateAAD(byte[] src)
318
* update(byte[], int, int, byte[], int)
319
* doFinal(byte[], int, int, byte[], int)
320
*/
321
private void combination_4(List<byte[]> results, int mode, byte[] AAD,
322
byte[] plainText, AlgorithmParameters params) throws Exception {
323
Cipher ci = createCipher(mode, params);
324
ci.updateAAD(AAD);
325
byte[] part41 = new byte[ci.getOutputSize(plainText.length)];
326
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
327
int len = ci.update(plainText, 0, plainText.length - offset, part41, 0);
328
int rest4 = ci.doFinal(plainText, plainText.length - offset, offset,
329
part41, len);
330
byte[] outputText4 = new byte[len + rest4];
331
System.arraycopy(part41, 0, outputText4, 0, outputText4.length);
332
results.add(outputText4);
333
}
334
335
/*
336
* Execute multiple-part encryption/decryption combination #5:
337
* updateAAD(byte[] src, int offset, int len)
338
* update(byte[], int, int)
339
* doFinal(byte[], int, int)
340
*/
341
private void combination_5(List<byte[]> results, int mode, byte[] AAD,
342
byte[] plainText, AlgorithmParameters params) throws Exception {
343
Cipher c = createCipher(mode, params);
344
c.updateAAD(AAD, 0, AAD.length);
345
byte[] part51 = c.update(plainText, 0, plainText.length);
346
byte[] part52 = c.doFinal();
347
int part51Length = part51 != null ? part51.length : 0;
348
byte[] outputText5 = new byte[part51Length + part52.length];
349
if (part51 != null) {
350
System.arraycopy(part51, 0, outputText5, 0, part51Length);
351
}
352
System.arraycopy(part52, 0, outputText5, part51Length, part52.length);
353
results.add(outputText5);
354
}
355
356
/*
357
* Execute multiple-part encryption/decryption combination #6:
358
* updateAAD(byte[] src, int offset, int len)
359
* updateAAD(byte[] src, int offset, int len)
360
* update(byte[], int, int) doFinal(byte[], int, int, byte[], int)
361
*/
362
private void combination_6(List<byte[]> results, int mode, byte[] AAD,
363
byte[] plainText, AlgorithmParameters params) throws Exception {
364
Cipher c = createCipher(mode, params);
365
c.updateAAD(AAD, 0, AAD.length / 2);
366
c.updateAAD(AAD, AAD.length / 2, AAD.length - AAD.length / 2);
367
int t = 0;
368
int offset = 0;
369
if (plainText.length > ARRAY_OFFSET) {
370
t = plainText.length - ARRAY_OFFSET;
371
offset = ARRAY_OFFSET;
372
}
373
byte[] part61 = c.update(plainText, 0, t);
374
byte[] part62 = new byte[c.getOutputSize(plainText.length)];
375
int len = c.doFinal(plainText, t, offset, part62, 0);
376
int part61Length = part61 != null ? part61.length : 0;
377
byte[] outputText6 = new byte[part61Length + len];
378
if (part61 != null) {
379
System.arraycopy(part61, 0, outputText6, 0, part61Length);
380
}
381
System.arraycopy(part62, 0, outputText6, part61Length, len);
382
results.add(outputText6);
383
}
384
385
/*
386
* Execute multiple-part encryption/decryption combination #7
387
* updateAAD(byte[] src, int offset, int len)
388
* updateAAD(byte[] src, src.length, 0)
389
* update(byte[], int, int, byte[], int) doFinal(byte[],int, int)
390
*/
391
private void combination_7(List<byte[]> results, int mode, byte[] AAD,
392
byte[] plainText, AlgorithmParameters params) throws Exception {
393
Cipher ci = createCipher(mode, params);
394
ci.updateAAD(AAD, 0, AAD.length);
395
ci.updateAAD(AAD, AAD.length, 0);
396
byte[] part71 = new byte[ci.getOutputSize(plainText.length)];
397
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
398
int len = ci.update(plainText, 0, plainText.length - offset, part71, 0);
399
byte[] part72 = ci.doFinal(plainText, plainText.length - offset, offset);
400
byte[] outputText7 = new byte[len + part72.length];
401
System.arraycopy(part71, 0, outputText7, 0, len);
402
System.arraycopy(part72, 0, outputText7, len, part72.length);
403
results.add(outputText7);
404
}
405
406
/*
407
* Execute multiple-part encryption/decryption combination #8:
408
* updateAAD(byte[] src, 0, 0)
409
* updateAAD(byte[] src, 0, src.length)
410
* update(byte[], int, int, byte[], int)
411
* doFinal(byte[], int, int, byte[], int)
412
*/
413
private void combination_8(List<byte[]> results, int mode, byte[] AAD,
414
byte[] plainText, AlgorithmParameters params) throws Exception {
415
Cipher ci = createCipher(mode, params);
416
ci.updateAAD(AAD, 0, 0);
417
ci.updateAAD(AAD, 0, AAD.length);
418
byte[] part81 = new byte[ci.getOutputSize(plainText.length)];
419
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
420
int len = ci.update(plainText, 0, plainText.length - offset, part81, 0);
421
int rest = ci.doFinal(plainText, plainText.length - offset, offset,
422
part81, len);
423
byte[] outputText8 = new byte[len + rest];
424
System.arraycopy(part81, 0, outputText8, 0, outputText8.length);
425
results.add(outputText8);
426
}
427
428
/*
429
* Execute multiple-part encryption/decryption combination #9:
430
* updateAAD(ByteBuffer src)
431
* update(byte[], int, int) doFinal(byte[], int, int)
432
*/
433
private void combination_9(List<byte[]> results, int mode, byte[] AAD,
434
byte[] plainText, AlgorithmParameters params) throws Exception {
435
436
// prepare ByteBuffer to test
437
ByteBuffer buf = ByteBuffer.allocate(AAD.length);
438
buf.put(AAD);
439
buf.position(0);
440
buf.limit(AAD.length);
441
442
// Get Cipher object and do the combination
443
Cipher c = createCipher(mode, params);
444
c.updateAAD(buf);
445
byte[] part91 = c.update(plainText, 0, plainText.length);
446
int part91_length = part91 == null ? 0 : part91.length;
447
byte[] part92 = c.doFinal();
448
byte[] outputText9 = new byte[part91_length + part92.length];
449
450
// form result of the combination
451
if (part91 != null) {
452
System.arraycopy(part91, 0, outputText9, 0, part91_length);
453
}
454
System.arraycopy(part92, 0, outputText9, part91_length, part92.length);
455
results.add(outputText9);
456
}
457
458
/*
459
* Execute multiple-part encryption/decryption combination #10:
460
* updateAAD(ByteBuffer src)
461
* updateAAD(ByteBuffer src) update(byte[], int, int)
462
* doFinal(byte[], int, int, byte[], int)
463
*/
464
private void combination_10(List<byte[]> results, int mode, byte[] AAD,
465
byte[] plainText, AlgorithmParameters params) throws Exception {
466
467
// prepare ByteBuffer to test
468
ByteBuffer buf = ByteBuffer.allocate(AAD.length);
469
buf.put(AAD);
470
buf.position(0);
471
buf.limit(AAD.length / 2);
472
473
// get a Cipher object and do the combination
474
Cipher c = createCipher(mode, params);
475
476
// process the first half of AAD data
477
c.updateAAD(buf);
478
479
// process the rest of AAD data
480
buf.limit(AAD.length);
481
c.updateAAD(buf);
482
483
// prapare variables for the combination
484
int t = 0;
485
int offset = 0;
486
if (plainText.length > ARRAY_OFFSET) {
487
t = plainText.length - ARRAY_OFFSET;
488
offset = ARRAY_OFFSET;
489
}
490
491
// encrypt the text
492
byte[] part10_1 = c.update(plainText, 0, t);
493
int part10_1_Length = part10_1 != null ? part10_1.length : 0;
494
byte[] part10_2 = new byte[c.getOutputSize(plainText.length)];
495
int len2 = c.doFinal(plainText, t, offset, part10_2, 0);
496
497
// form the combination's result
498
byte[] outputText10 = new byte[part10_1_Length + len2];
499
if (part10_1 != null) {
500
System.arraycopy(part10_1, 0, outputText10, 0, part10_1_Length);
501
}
502
System.arraycopy(part10_2, 0, outputText10, part10_1_Length, len2);
503
results.add(outputText10);
504
}
505
506
/*
507
* Execute multiple-part encryption/decryption combination #11
508
* updateAAD(ByteBuffer src1)
509
* updateAAD(ByteBuffer src2)
510
* update(byte[],int, int, byte[], int)
511
* doFinal(byte[], int, int)
512
*/
513
private void combination_11(List<byte[]> results, int mode, byte[] AAD,
514
byte[] plainText, AlgorithmParameters params) throws Exception {
515
516
// prepare ByteBuffer1 to test
517
ByteBuffer buf1 = ByteBuffer.allocate(AAD.length / 2);
518
buf1.put(AAD, 0, AAD.length / 2);
519
buf1.position(0);
520
buf1.limit(AAD.length / 2);
521
522
// get a Cipher object and do combination
523
Cipher ci = createCipher(mode, params);
524
525
// process the first half of AAD data
526
ci.updateAAD(buf1);
527
528
// prepare ByteBuffer2 to test
529
ByteBuffer buf2 = ByteBuffer.allocate(AAD.length - AAD.length / 2);
530
buf2.put(AAD, AAD.length / 2, AAD.length - AAD.length / 2);
531
buf2.position(0);
532
buf2.limit(AAD.length - AAD.length / 2);
533
534
// process the rest of AAD data
535
ci.updateAAD(buf2);
536
537
// encrypt plain text
538
byte[] part11_1 = new byte[ci.getOutputSize(plainText.length)];
539
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
540
int len_11 = ci.update(plainText, 0, plainText.length - offset,
541
part11_1, 0);
542
byte[] part11_2 = ci.doFinal(plainText, plainText.length - offset,
543
offset);
544
byte[] outputText11 = new byte[len_11 + part11_2.length];
545
System.arraycopy(part11_1, 0, outputText11, 0, len_11);
546
System.arraycopy(part11_2, 0, outputText11, len_11, part11_2.length);
547
results.add(outputText11);
548
}
549
550
/*
551
* Execute multiple-part encryption/decryption combination #12:
552
* updateAAD(ByteBuffer src)
553
* updateAAD(ByteBuffer emptyByteBuffer)
554
* update(byte[], int, int, byte[], int)
555
* doFinal(byte[], int, int, byte[], int)
556
*/
557
private void combination_12(List<byte[]> results, int mode, byte[] AAD,
558
byte[] plainText, AlgorithmParameters params) throws Exception {
559
560
// prepare ByteBuffer to test
561
ByteBuffer buf = ByteBuffer.allocate(AAD.length);
562
buf.put(AAD);
563
buf.position(0);
564
buf.limit(AAD.length);
565
Cipher ci = createCipher(mode, params);
566
ci.updateAAD(buf);
567
568
// prepare an empty ByteBuffer
569
ByteBuffer emptyBuf = ByteBuffer.allocate(0);
570
emptyBuf.put(new byte[0]);
571
ci.updateAAD(emptyBuf);
572
byte[] part12_1 = new byte[ci.getOutputSize(plainText.length)];
573
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
574
int len12 = ci.update(plainText, 0, plainText.length - offset,
575
part12_1, 0);
576
int rest12 = ci.doFinal(plainText, plainText.length - offset, offset,
577
part12_1, len12);
578
byte[] outputText12 = new byte[len12 + rest12];
579
System.arraycopy(part12_1, 0, outputText12, 0, outputText12.length);
580
results.add(outputText12);
581
}
582
583
/*
584
* Execute multiple-part encryption/decryption combination #13:
585
* updateAAD(ByteBuffer src), where src is directly allocated
586
* update(ByteBuffer input, ByteBuffer out)
587
* doFinal(ByteBuffer input, ByteBuffer out)
588
*/
589
private void combination_13(List<byte[]> results, int mode, byte[] AAD,
590
byte[] plainText, AlgorithmParameters params) throws Exception {
591
Cipher c = createCipher(mode, params);
592
593
// prepare ByteBuffer to test
594
ByteBuffer buf = ByteBuffer.allocateDirect(AAD.length);
595
buf.put(AAD);
596
buf.position(0);
597
buf.limit(AAD.length);
598
c.updateAAD(buf);
599
600
// prepare buffers to encrypt/decrypt
601
ByteBuffer in = ByteBuffer.allocateDirect(plainText.length);
602
in.put(plainText);
603
in.position(0);
604
in.limit(plainText.length);
605
ByteBuffer output = ByteBuffer.allocateDirect(
606
c.getOutputSize(in.limit()));
607
output.position(0);
608
output.limit(c.getOutputSize(in.limit()));
609
610
// process input text
611
c.update(in, output);
612
c.doFinal(in, output);
613
int resultSize = output.position();
614
byte[] result13 = new byte[resultSize];
615
output.position(0);
616
output.limit(resultSize);
617
output.get(result13, 0, resultSize);
618
results.add(result13);
619
}
620
621
/*
622
* Execute multiple-part encryption/decryption combination #14:
623
* updateAAD(ByteBuffer src) updateAAD(ByteBuffer src),
624
* where src is directly allocated
625
* update(ByteBuffer input, ByteBuffer out)
626
* doFinal(ByteBuffer input, ByteBuffer out)
627
*/
628
private void combination_14(List<byte[]> results, int mode, byte[] AAD,
629
byte[] plainText, AlgorithmParameters params) throws Exception {
630
Cipher c = createCipher(mode, params);
631
// prepare ByteBuffer to test
632
ByteBuffer buf = ByteBuffer.allocateDirect(AAD.length);
633
buf.put(AAD);
634
635
// process the first half of AAD data
636
buf.position(0);
637
buf.limit(AAD.length / 2);
638
c.updateAAD(buf);
639
640
// process the rest of AAD data
641
buf.limit(AAD.length);
642
c.updateAAD(buf);
643
644
// prepare buffers to encrypt/decrypt
645
ByteBuffer in = ByteBuffer.allocate(plainText.length);
646
in.put(plainText);
647
in.position(0);
648
in.limit(plainText.length);
649
ByteBuffer out = ByteBuffer.allocate(c.getOutputSize(in.limit()));
650
out.position(0);
651
out.limit(c.getOutputSize(in.limit()));
652
653
// process input text
654
c.update(in, out);
655
c.doFinal(in, out);
656
int resultSize = out.position();
657
byte[] result14 = new byte[resultSize];
658
out.position(0);
659
out.limit(resultSize);
660
out.get(result14, 0, resultSize);
661
results.add(result14);
662
}
663
664
/*
665
* Execute multiple-part encryption/decryption combination #15
666
* updateAAD(ByteBuffer src1), where src1 is directly allocated
667
* updateAAD(ByteBuffer src2), where src2 is directly allocated
668
* doFinal(ByteBuffer input, ByteBuffer out)
669
*/
670
private void combination_15(List<byte[]> results, int mode, byte[] AAD,
671
byte[] plainText, AlgorithmParameters params) throws Exception {
672
Cipher c = createCipher(mode, params);
673
674
// prepare ByteBuffer1 to test
675
ByteBuffer buf1 = ByteBuffer.allocateDirect(AAD.length / 2);
676
buf1.put(AAD, 0, AAD.length / 2);
677
buf1.position(0);
678
buf1.limit(AAD.length / 2);
679
680
// process the first half of AAD data
681
c.updateAAD(buf1);
682
683
// prepare ByteBuffer2 to test
684
ByteBuffer buf2 = ByteBuffer.allocateDirect(
685
AAD.length - AAD.length / 2);
686
buf2.put(AAD, AAD.length / 2, AAD.length - AAD.length / 2);
687
buf2.position(0);
688
buf2.limit(AAD.length - AAD.length / 2);
689
690
// process the rest of AAD data
691
c.updateAAD(buf2);
692
693
// prepare buffers to encrypt/decrypt
694
ByteBuffer in = ByteBuffer.allocateDirect(plainText.length);
695
in.put(plainText);
696
in.position(0);
697
in.limit(plainText.length);
698
ByteBuffer output = ByteBuffer.allocateDirect(
699
c.getOutputSize(in.limit()));
700
output.position(0);
701
output.limit(c.getOutputSize(in.limit()));
702
703
// process input text
704
c.doFinal(in, output);
705
int resultSize = output.position();
706
byte[] result15 = new byte[resultSize];
707
output.position(0);
708
output.limit(resultSize);
709
output.get(result15, 0, resultSize);
710
results.add(result15);
711
}
712
713
/*
714
* Execute multiple-part encryption/decryption combination #16:
715
* updateAAD(ByteBuffer src)
716
* updateAAD(ByteBuffer emptyByteBuffer)
717
* update(ByteBuffer input, ByteBuffer out)
718
* doFinal(EmptyByteBuffer, ByteBuffer out)
719
*/
720
private void combination_16(List<byte[]> results, int mode, byte[] AAD,
721
byte[] plainText, AlgorithmParameters params) throws Exception {
722
Cipher c = createCipher(mode, params);
723
724
// prepare ByteBuffer to test
725
ByteBuffer buf = ByteBuffer.allocateDirect(AAD.length);
726
buf.put(AAD);
727
buf.position(0);
728
buf.limit(AAD.length);
729
c.updateAAD(buf);
730
731
// prepare empty ByteBuffer
732
ByteBuffer emptyBuf = ByteBuffer.allocateDirect(0);
733
emptyBuf.put(new byte[0]);
734
c.updateAAD(emptyBuf);
735
736
// prepare buffers to encrypt/decrypt
737
ByteBuffer in = ByteBuffer.allocateDirect(plainText.length);
738
in.put(plainText);
739
in.position(0);
740
in.limit(plainText.length);
741
ByteBuffer output = ByteBuffer.allocateDirect(
742
c.getOutputSize(in.limit()));
743
output.position(0);
744
output.limit(c.getOutputSize(in.limit()));
745
746
// process input text with an empty buffer
747
c.update(in, output);
748
ByteBuffer emptyBuf2 = ByteBuffer.allocate(0);
749
emptyBuf2.put(new byte[0]);
750
c.doFinal(emptyBuf2, output);
751
int resultSize = output.position();
752
byte[] result16 = new byte[resultSize];
753
output.position(0);
754
output.limit(resultSize);
755
output.get(result16, 0, resultSize);
756
results.add(result16);
757
}
758
}
759
760