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/GCMBufferTest.java
66649 views
1
/*
2
* Copyright (c) 2020, 2021, 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
/*
25
* @test
26
* @summary Use Cipher update and doFinal with a mixture of byte[], bytebuffer,
27
* and offset while verifying return values. Also using different and
28
* in-place buffers.
29
*
30
* in-place is not tested with different buffer types as it is not a logical
31
* scenario and is complicated by getOutputSize calculations.
32
*/
33
34
import javax.crypto.Cipher;
35
import javax.crypto.SecretKey;
36
import javax.crypto.spec.GCMParameterSpec;
37
import javax.crypto.spec.SecretKeySpec;
38
import java.io.ByteArrayOutputStream;
39
import java.nio.ByteBuffer;
40
import java.security.SecureRandom;
41
import java.util.Arrays;
42
import java.util.HashMap;
43
import java.util.HexFormat;
44
import java.util.List;
45
46
public class GCMBufferTest implements Cloneable {
47
48
// Data type for the operation
49
enum dtype { BYTE, HEAP, DIRECT };
50
// Data map
51
static HashMap<String, List<Data>> datamap = new HashMap<>();
52
// List of enum values for order of operation
53
List<dtype> ops;
54
55
static final int AESBLOCK = 16;
56
// The remaining input data length is inserted at the particular index
57
// in sizes[] during execution.
58
static final int REMAINDER = -1;
59
60
String algo;
61
boolean same = true;
62
int[] sizes;
63
boolean incremental = false;
64
// In some cases the theoretical check is too complicated to verify
65
boolean theoreticalCheck;
66
List<Data> dataSet;
67
int inOfs = 0, outOfs = 0;
68
static HexFormat hex = HexFormat.of();
69
70
static class Data {
71
int id;
72
SecretKey key;
73
byte[] iv;
74
byte[] pt;
75
byte[] aad;
76
byte[] ct;
77
byte[] tag;
78
79
Data(String keyalgo, int id, String key, String iv, byte[] pt, String aad,
80
String ct, String tag) {
81
this.id = id;
82
this.key = new SecretKeySpec(HexToBytes(key), keyalgo);
83
this.iv = HexToBytes(iv);
84
this.pt = pt;
85
this.aad = HexToBytes(aad);
86
this.ct = HexToBytes(ct);
87
this.tag = HexToBytes(tag);
88
}
89
90
Data(String keyalgo, int id, String key, String iv, String pt, String aad,
91
String ct, String tag) {
92
this(keyalgo, id, key, iv, HexToBytes(pt), aad, ct, tag);
93
}
94
95
Data(String keyalgo, int id, String key, int ptlen) {
96
this.id = id;
97
this.key = new SecretKeySpec(HexToBytes(key), keyalgo);
98
iv = new byte[16];
99
pt = new byte[ptlen];
100
tag = new byte[12];
101
aad = new byte[0];
102
byte[] tct = null;
103
try {
104
SecureRandom r = new SecureRandom();
105
r.nextBytes(iv);
106
r.nextBytes(pt);
107
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
108
c.init(Cipher.ENCRYPT_MODE, this.key,
109
new GCMParameterSpec(tag.length * 8, this.iv));
110
tct = c.doFinal(pt);
111
} catch (Exception e) {
112
throw new RuntimeException("Error in generating data for length " +
113
ptlen, e);
114
}
115
ct = new byte[ptlen];
116
System.arraycopy(tct, 0, ct, 0, ct.length);
117
System.arraycopy(tct, ct.length, tag, 0, tag.length);
118
}
119
120
private static final byte[] HexToBytes(String hexVal) {
121
if (hexVal == null) {
122
return new byte[0];
123
}
124
return hex.parseHex(hexVal);
125
}
126
127
}
128
129
/**
130
* Construct a test with an algorithm and a list of dtype.
131
* @param algo Algorithm string
132
* @param ops List of dtypes. If only one dtype is specified, only a
133
* doFinal operation will occur. If multiple dtypes are
134
* specified, the last is a doFinal, the others are updates.
135
*/
136
GCMBufferTest(String algo, List<dtype> ops) {
137
this.algo = algo;
138
this.ops = ops;
139
theoreticalCheck = true;
140
dataSet = datamap.get(algo);
141
}
142
143
public GCMBufferTest clone() throws CloneNotSupportedException{
144
return (GCMBufferTest)super.clone();
145
}
146
147
/**
148
* Define particular data sizes to be tested. "REMAINDER", which has a
149
* value of -1, can be used to insert the remaining input text length at
150
* that index during execution.
151
* @param sizes Data sizes for each dtype in the list.
152
*/
153
GCMBufferTest dataSegments(int[] sizes) {
154
this.sizes = sizes;
155
return this;
156
}
157
158
/**
159
* Do not perform in-place operations
160
*/
161
GCMBufferTest differentBufferOnly() {
162
this.same = false;
163
return this;
164
}
165
166
/**
167
* Enable incrementing through each data size available. This can only be
168
* used when the List has more than one dtype entry.
169
*/
170
GCMBufferTest incrementalSegments() {
171
this.incremental = true;
172
return this;
173
}
174
175
/**
176
* Specify a particular test dataset.
177
*
178
* @param id id value for the test data to used in this test.
179
*/
180
GCMBufferTest dataSet(int id) throws Exception {
181
for (Data d : datamap.get(algo)) {
182
if (d.id == id) {
183
dataSet = List.of(d);
184
return this;
185
}
186
}
187
throw new Exception("Unable to find dataSet id = " + id);
188
}
189
190
/**
191
* Set both input and output offsets to the same offset
192
* @param offset value for inOfs and outOfs
193
* @return
194
*/
195
GCMBufferTest offset(int offset) {
196
this.inOfs = offset;
197
this.outOfs = offset;
198
return this;
199
}
200
201
/**
202
* Set the input offset
203
* @param offset value for input offset
204
* @return
205
*/
206
GCMBufferTest inOfs(int offset) {
207
this.inOfs = offset;
208
return this;
209
}
210
211
/**
212
* Set the output offset
213
* @param offset value for output offset
214
* @return
215
*/
216
GCMBufferTest outOfs(int offset) {
217
this.outOfs = offset;
218
return this;
219
}
220
221
/**
222
* Reverse recursive loop that starts at the end-1 index, going to 0, in
223
* the size array to calculate all the possible sizes.
224
* It returns the remaining data size not used in the loop. This remainder
225
* is used for the end index which is the doFinal op.
226
*/
227
int inc(int index, int max, int total) {
228
if (sizes[index] == max - total) {
229
sizes[index + 1]++;
230
total++;
231
sizes[index] = 0;
232
} else if (index == 0) {
233
sizes[index]++;
234
}
235
236
total += sizes[index];
237
if (index > 0) {
238
return inc(index - 1, max, total);
239
}
240
return total;
241
}
242
243
// Call recursive loop and take returned remainder value for last index
244
boolean incrementSizes(int max) {
245
sizes[ops.size() - 1] = max - inc(ops.size() - 2, max, 0);
246
if (sizes[ops.size() - 2] == max) {
247
// We are at the end, exit test loop
248
return false;
249
}
250
return true;
251
}
252
253
void test() throws Exception {
254
int i = 1;
255
System.err.println("Algo: " + algo + " \tOps: " + ops.toString());
256
for (Data data : dataSet) {
257
258
// If incrementalSegments is enabled, run through that test only
259
if (incremental) {
260
if (ops.size() < 2) {
261
throw new Exception("To do incrementalSegments you must" +
262
"have more that 1 dtype in the list");
263
}
264
sizes = new int[ops.size()];
265
266
while (incrementSizes(data.pt.length)) {
267
System.err.print("Encrypt: Data Index: " + i + " \tSizes[ ");
268
for (int v : sizes) {
269
System.err.print(v + " ");
270
}
271
System.err.println("]");
272
encrypt(data);
273
}
274
Arrays.fill(sizes, 0);
275
276
while (incrementSizes(data.ct.length + data.tag.length)) {
277
System.err.print("Decrypt: Data Index: " + i + " \tSizes[ ");
278
for (int v : sizes) {
279
System.err.print(v + " ");
280
}
281
System.err.println("]");
282
decrypt(data);
283
}
284
285
} else {
286
// Default test of 0 and 2 offset doing in place and different
287
// i/o buffers
288
System.err.println("Encrypt: Data Index: " + i);
289
encrypt(data);
290
291
System.err.println("Decrypt: Data Index: " + i);
292
decrypt(data);
293
}
294
i++;
295
}
296
}
297
298
// Setup data for encryption
299
void encrypt(Data data) throws Exception {
300
byte[] input, output;
301
302
input = data.pt;
303
output = new byte[data.ct.length + data.tag.length];
304
System.arraycopy(data.ct, 0, output, 0, data.ct.length);
305
System.arraycopy(data.tag, 0, output, data.ct.length,
306
data.tag.length);
307
308
// Test different input/output buffers
309
System.err.println("\tinput len: " + input.length + " inOfs " +
310
inOfs + " outOfs " + outOfs + " in/out buffer: different");
311
crypto(true, data, input, output);
312
313
// Test with in-place buffers
314
if (same) {
315
System.err.println("\tinput len: " + input.length + " inOfs " +
316
inOfs + " outOfs " + outOfs + " in/out buffer: in-place");
317
cryptoSameBuffer(true, data, input, output);
318
}
319
}
320
321
// Setup data for decryption
322
void decrypt(Data data) throws Exception {
323
byte[] input, output;
324
325
input = new byte[data.ct.length + data.tag.length];
326
System.arraycopy(data.ct, 0, input, 0, data.ct.length);
327
System.arraycopy(data.tag, 0, input, data.ct.length, data.tag.length);
328
output = data.pt;
329
330
// Test different input/output buffers
331
System.err.println("\tinput len: " + input.length + " inOfs " +
332
inOfs + " outOfs " + outOfs + " in/out buffer: different");
333
crypto(false, data, input, output);
334
335
// Test with in-place buffers
336
if (same) {
337
System.err.println("\tinput len: " + input.length + " inOfs " +
338
inOfs + " outOfs " + outOfs + " in-place: same");
339
cryptoSameBuffer(false, data, input, output);
340
}
341
}
342
343
/**
344
* Perform cipher operation using different input and output buffers.
345
* This method allows mixing of data types (byte, heap, direct).
346
*/
347
void crypto(boolean encrypt, Data d, byte[] input, byte[] output)
348
throws Exception {
349
byte[] pt = new byte[input.length + inOfs];
350
System.arraycopy(input, 0, pt, inOfs, input.length);
351
byte[] expectedOut = new byte[output.length + outOfs];
352
System.arraycopy(output, 0, expectedOut, outOfs, output.length);
353
int plen = input.length / ops.size(); // partial input length
354
int theoreticallen;// expected output length
355
int dataoffset = 0; // offset of unconsumed data in pt
356
int index = 0; // index of which op we are on
357
int rlen; // result length
358
int pbuflen = 0; // plen remaining in the GCM internal buffers
359
360
Cipher cipher = Cipher.getInstance(algo);
361
cipher.init((encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE),
362
d.key, new GCMParameterSpec(d.tag.length * 8, d.iv));
363
cipher.updateAAD(d.aad);
364
365
ByteArrayOutputStream ba = new ByteArrayOutputStream();
366
ba.write(new byte[outOfs], 0, outOfs);
367
for (dtype v : ops) {
368
if (index < ops.size() - 1) {
369
if (sizes != null && input.length > 0) {
370
if (sizes[index] == -1) {
371
plen = input.length - dataoffset;
372
} else {
373
if (sizes[index] > input.length) {
374
plen = input.length;
375
} else {
376
plen = sizes[index];
377
}
378
}
379
}
380
381
int olen = cipher.getOutputSize(plen) + outOfs;
382
383
/*
384
* The theoretical limit is the length of the data sent to
385
* update() + any data might be setting in CipherCore or GCM
386
* internal buffers % the block size.
387
*/
388
theoreticallen = (plen + pbuflen) - ((plen + pbuflen) % AESBLOCK);
389
390
// Update operations
391
switch (v) {
392
case BYTE -> {
393
byte[] out = new byte[olen];
394
rlen = cipher.update(pt, dataoffset + inOfs, plen, out,
395
outOfs);
396
ba.write(out, outOfs, rlen);
397
}
398
case HEAP -> {
399
ByteBuffer b = ByteBuffer.allocate(plen + outOfs);
400
b.position(outOfs);
401
b.put(pt, dataoffset + inOfs, plen);
402
b.flip();
403
b.position(outOfs);
404
ByteBuffer out = ByteBuffer.allocate(olen);
405
out.position(outOfs);
406
rlen = cipher.update(b, out);
407
ba.write(out.array(), outOfs, rlen);
408
}
409
case DIRECT -> {
410
ByteBuffer b = ByteBuffer.allocateDirect(plen + outOfs);
411
b.position(outOfs);
412
b.put(pt, dataoffset + inOfs, plen);
413
b.flip();
414
b.position(outOfs);
415
ByteBuffer out = ByteBuffer.allocateDirect(olen);
416
out.position(outOfs);
417
rlen = cipher.update(b, out);
418
byte[] o = new byte[rlen];
419
out.flip();
420
out.position(outOfs);
421
out.get(o, 0, rlen);
422
ba.write(o);
423
}
424
default -> throw new Exception("Unknown op: " + v.name());
425
}
426
427
if (theoreticalCheck) {
428
pbuflen += plen - rlen;
429
if (encrypt && rlen != theoreticallen) {
430
throw new Exception("Wrong update return len (" +
431
v.name() + "): " + "rlen=" + rlen +
432
", expected output len=" + theoreticallen);
433
}
434
}
435
436
dataoffset += plen;
437
index++;
438
439
} else {
440
// doFinal operation
441
plen = input.length - dataoffset;
442
443
int olen = cipher.getOutputSize(plen) + outOfs;
444
switch (v) {
445
case BYTE -> {
446
byte[] out = new byte[olen];
447
rlen = cipher.doFinal(pt, dataoffset + inOfs,
448
plen, out, outOfs);
449
ba.write(out, outOfs, rlen);
450
}
451
case HEAP -> {
452
ByteBuffer b = ByteBuffer.allocate(plen + inOfs);
453
b.limit(b.capacity());
454
b.position(inOfs);
455
b.put(pt, dataoffset + inOfs, plen);
456
b.flip();
457
b.position(inOfs);
458
ByteBuffer out = ByteBuffer.allocate(olen);
459
out.limit(out.capacity());
460
out.position(outOfs);
461
rlen = cipher.doFinal(b, out);
462
ba.write(out.array(), outOfs, rlen);
463
}
464
case DIRECT -> {
465
ByteBuffer b = ByteBuffer.allocateDirect(plen + inOfs);
466
b.limit(b.capacity());
467
b.position(inOfs);
468
b.put(pt, dataoffset + inOfs, plen);
469
b.flip();
470
b.position(inOfs);
471
ByteBuffer out = ByteBuffer.allocateDirect(olen);
472
out.limit(out.capacity());
473
out.position(outOfs);
474
rlen = cipher.doFinal(b, out);
475
byte[] o = new byte[rlen];
476
out.flip();
477
out.position(outOfs);
478
out.get(o, 0, rlen);
479
ba.write(o);
480
}
481
default -> throw new Exception("Unknown op: " + v.name());
482
}
483
484
if (theoreticalCheck && rlen != olen - outOfs) {
485
throw new Exception("Wrong doFinal return len (" +
486
v.name() + "): " + "rlen=" + rlen +
487
", expected output len=" + (olen - outOfs));
488
}
489
490
// Verify results
491
byte[] ctresult = ba.toByteArray();
492
if (ctresult.length != expectedOut.length ||
493
Arrays.compare(ctresult, expectedOut) != 0) {
494
String s = "Ciphertext mismatch (" + v.name() +
495
"):\nresult (len=" + ctresult.length + "): " +
496
hex.formatHex(ctresult) +
497
"\nexpected (len=" + output.length + "): " +
498
hex.formatHex(output);
499
System.err.println(s);
500
throw new Exception(s);
501
502
}
503
}
504
}
505
}
506
507
/**
508
* Perform cipher operation using in-place buffers. This method does not
509
* allow mixing of data types (byte, heap, direct).
510
*
511
* Mixing data types makes no sense for in-place operations and would
512
* greatly complicate the test code.
513
*/
514
void cryptoSameBuffer(boolean encrypt, Data d, byte[] input, byte[] output) throws Exception {
515
516
byte[] data, out;
517
if (encrypt) {
518
data = new byte[output.length + Math.max(inOfs, outOfs)];
519
} else {
520
data = new byte[input.length + Math.max(inOfs, outOfs)];
521
}
522
523
ByteBuffer bbin = null, bbout = null;
524
System.arraycopy(input, 0, data, inOfs, input.length);
525
byte[] expectedOut = new byte[output.length + outOfs];
526
System.arraycopy(output, 0, expectedOut, outOfs, output.length);
527
int plen = input.length / ops.size(); // partial input length
528
int theorticallen = plen - (plen % AESBLOCK); // output length
529
int dataoffset = 0;
530
int index = 0;
531
int rlen = 0; // result length
532
int len = 0;
533
534
Cipher cipher = Cipher.getInstance(algo);
535
cipher.init((encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE),
536
d.key, new GCMParameterSpec(d.tag.length * 8, d.iv));
537
cipher.updateAAD(d.aad);
538
539
// Prepare data
540
switch (ops.get(0)) {
541
case HEAP -> {
542
bbin = ByteBuffer.wrap(data);
543
bbin.limit(input.length + inOfs);
544
bbout = bbin.duplicate();
545
}
546
case DIRECT -> {
547
bbin = ByteBuffer.allocateDirect(data.length);
548
bbout = bbin.duplicate();
549
bbin.put(data, 0, input.length + inOfs);
550
bbin.flip();
551
}
552
}
553
554
// Set data limits for bytebuffers
555
if (bbin != null) {
556
bbin.position(inOfs);
557
bbout.limit(output.length + outOfs);
558
bbout.position(outOfs);
559
}
560
561
// Iterate through each operation
562
for (dtype v : ops) {
563
if (index < ops.size() - 1) {
564
switch (v) {
565
case BYTE -> {
566
rlen = cipher.update(data, dataoffset + inOfs, plen,
567
data, len + outOfs);
568
}
569
case HEAP, DIRECT -> {
570
theorticallen = bbin.remaining() -
571
(bbin.remaining() % AESBLOCK);
572
rlen = cipher.update(bbin, bbout);
573
}
574
default -> throw new Exception("Unknown op: " + v.name());
575
}
576
577
// Check that the theoretical return value matches the actual.
578
if (theoreticalCheck && encrypt && rlen != theorticallen) {
579
throw new Exception("Wrong update return len (" +
580
v.name() + "): " + "rlen=" + rlen +
581
", expected output len=" + theorticallen);
582
}
583
584
dataoffset += plen;
585
len += rlen;
586
index++;
587
588
} else {
589
// Run doFinal op
590
plen = input.length - dataoffset;
591
592
switch (v) {
593
case BYTE -> {
594
rlen = cipher.doFinal(data, dataoffset + inOfs,
595
plen, data, len + outOfs);
596
out = Arrays.copyOfRange(data, 0,len + rlen + outOfs);
597
}
598
case HEAP, DIRECT -> {
599
rlen = cipher.doFinal(bbin, bbout);
600
bbout.flip();
601
out = new byte[bbout.remaining()];
602
bbout.get(out);
603
}
604
default -> throw new Exception("Unknown op: " + v.name());
605
}
606
len += rlen;
607
608
// Verify results
609
if (len != output.length ||
610
Arrays.compare(out, 0, len, expectedOut, 0,
611
output.length) != 0) {
612
String s = "Ciphertext mismatch (" + v.name() +
613
"):\nresult (len=" + len + "):\n" +
614
hex.formatHex(out) +
615
"\nexpected (len=" + output.length + "):\n" +
616
hex.formatHex(output);
617
System.err.println(s);
618
throw new Exception(s);
619
}
620
}
621
}
622
}
623
static void offsetTests(GCMBufferTest t) throws Exception {
624
t.clone().offset(2).test();
625
t.clone().inOfs(2).test();
626
// Test not designed for overlap situations
627
t.clone().outOfs(2).differentBufferOnly().test();
628
}
629
630
public static void main(String args[]) throws Exception {
631
GCMBufferTest t;
632
633
initTest();
634
635
// Test single byte array
636
new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.BYTE)).test();
637
offsetTests(new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.BYTE)));
638
// Test update-doFinal with byte arrays
639
new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.BYTE, dtype.BYTE)).test();
640
offsetTests(new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.BYTE, dtype.BYTE)));
641
// Test update-update-doFinal with byte arrays
642
new GCMBufferTest("AES/GCM/NoPadding",
643
List.of(dtype.BYTE, dtype.BYTE, dtype.BYTE)).test();
644
offsetTests(new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.BYTE, dtype.BYTE, dtype.BYTE)));
645
646
// Test single heap bytebuffer
647
new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.HEAP)).test();
648
offsetTests(new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.HEAP)));
649
// Test update-doFinal with heap bytebuffer
650
new GCMBufferTest("AES/GCM/NoPadding",
651
List.of(dtype.HEAP, dtype.HEAP)).test();
652
offsetTests(new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.HEAP, dtype.HEAP)));
653
// Test update-update-doFinal with heap bytebuffer
654
new GCMBufferTest("AES/GCM/NoPadding",
655
List.of(dtype.HEAP, dtype.HEAP, dtype.HEAP)).test();
656
offsetTests(new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.HEAP, dtype.HEAP, dtype.HEAP)));
657
658
// Test single direct bytebuffer
659
new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.DIRECT)).test();
660
offsetTests(new GCMBufferTest("AES/GCM/NoPadding", List.of(dtype.DIRECT)));
661
// Test update-doFinal with direct bytebuffer
662
new GCMBufferTest("AES/GCM/NoPadding",
663
List.of(dtype.DIRECT, dtype.DIRECT)).test();
664
offsetTests(new GCMBufferTest("AES/GCM/NoPadding",
665
List.of(dtype.DIRECT, dtype.DIRECT)));
666
// Test update-update-doFinal with direct bytebuffer
667
new GCMBufferTest("AES/GCM/NoPadding",
668
List.of(dtype.DIRECT, dtype.DIRECT, dtype.DIRECT)).test();
669
offsetTests(new GCMBufferTest("AES/GCM/NoPadding",
670
List.of(dtype.DIRECT, dtype.DIRECT, dtype.DIRECT)));
671
672
// Test update-update-doFinal with byte arrays and preset data sizes
673
t = new GCMBufferTest("AES/GCM/NoPadding",
674
List.of(dtype.BYTE, dtype.BYTE, dtype.BYTE)).dataSegments(
675
new int[] { 1, 1, GCMBufferTest.REMAINDER});
676
t.clone().test();
677
offsetTests(t.clone());
678
679
// Test update-doFinal with a byte array and a direct bytebuffer
680
t = new GCMBufferTest("AES/GCM/NoPadding",
681
List.of(dtype.BYTE, dtype.DIRECT)).differentBufferOnly();
682
t.clone().test();
683
offsetTests(t.clone());
684
// Test update-doFinal with a byte array and heap and direct bytebuffer
685
t = new GCMBufferTest("AES/GCM/NoPadding",
686
List.of(dtype.BYTE, dtype.HEAP, dtype.DIRECT)).differentBufferOnly();
687
t.clone().test();
688
offsetTests(t.clone());
689
690
// Test update-doFinal with a direct bytebuffer and a byte array.
691
t = new GCMBufferTest("AES/GCM/NoPadding",
692
List.of(dtype.DIRECT, dtype.BYTE)).differentBufferOnly();
693
t.clone().test();
694
offsetTests(t.clone());
695
696
// Test update-doFinal with a direct bytebuffer and a byte array with
697
// preset data sizes.
698
t = new GCMBufferTest("AES/GCM/NoPadding",
699
List.of(dtype.DIRECT, dtype.BYTE)).differentBufferOnly().
700
dataSegments(new int[] { 20, GCMBufferTest.REMAINDER });
701
t.clone().test();
702
offsetTests(t.clone());
703
// Test update-update-doFinal with a direct and heap bytebuffer and a
704
// byte array with preset data sizes.
705
t = new GCMBufferTest("AES/GCM/NoPadding",
706
List.of(dtype.DIRECT, dtype.BYTE, dtype.HEAP)).
707
differentBufferOnly().dataSet(5).
708
dataSegments(new int[] { 5000, 1000, GCMBufferTest.REMAINDER });
709
t.clone().test();
710
offsetTests(t.clone());
711
712
// Test update-update-doFinal with byte arrays, incrementing through
713
// every data size combination for the Data set 0
714
new GCMBufferTest("AES/GCM/NoPadding",
715
List.of(dtype.BYTE, dtype.BYTE, dtype.BYTE)).incrementalSegments().
716
dataSet(0).test();
717
// Test update-update-doFinal with direct bytebuffers, incrementing through
718
// every data size combination for the Data set 0
719
new GCMBufferTest("AES/GCM/NoPadding",
720
List.of(dtype.DIRECT, dtype.DIRECT, dtype.DIRECT)).
721
incrementalSegments().dataSet(0).test();
722
723
new GCMBufferTest("AES/GCM/NoPadding",
724
List.of(dtype.DIRECT, dtype.DIRECT, dtype.DIRECT)).
725
dataSegments(new int[] { 49, 0, 2 }).dataSet(0).test();
726
}
727
728
// Test data
729
static void initTest() {
730
datamap.put("AES/GCM/NoPadding", List.of(
731
// GCM KAT
732
new Data("AES", 0,
733
"141f1ce91989b07e7eb6ae1dbd81ea5e",
734
"49451da24bd6074509d3cebc2c0394c972e6934b45a1d91f3ce1d3ca69e19" +
735
"4aa1958a7c21b6f21d530ce6d2cc5256a3f846b6f9d2f38df0102c4791e5" +
736
"7df038f6e69085646007df999751e248e06c47245f4cd3b8004585a7470d" +
737
"ee1690e9d2d63169a58d243c0b57b3e5b4a481a3e4e8c60007094ef3adea" +
738
"2e8f05dd3a1396f",
739
"d384305af2388699aa302f510913fed0f2cb63ba42efa8c5c9de2922a2ec" +
740
"2fe87719dadf1eb0aef212b51e74c9c5b934104a43",
741
"630cf18a91cc5a6481ac9eefd65c24b1a3c93396bd7294d6b8ba3239517" +
742
"27666c947a21894a079ef061ee159c05beeb4",
743
"f4c34e5fbe74c0297313268296cd561d59ccc95bbfcdfcdc71b0097dbd83" +
744
"240446b28dc088abd42b0fc687f208190ff24c0548",
745
"dbb93bbb56d0439cd09f620a57687f5d"),
746
// GCM KAT
747
new Data("AES", 1, "11754cd72aec309bf52f7687212e8957",
748
"3c819d9a9bed087615030b65",
749
(String)null, null, null,
750
"250327c674aaf477aef2675748cf6971"),
751
// GCM KAT
752
new Data("AES", 2, "272f16edb81a7abbea887357a58c1917",
753
"794ec588176c703d3d2a7a07",
754
(String)null, null, null,
755
"b6e6f197168f5049aeda32dafbdaeb"),
756
// zero'd test data
757
new Data("AES", 3, "272f16edb81a7abbea887357a58c1917",
758
"794ec588176c703d3d2a7a07", new byte[256], null,
759
"15b461672153270e8ba1e6789f7641c5411f3e642abda731b6086f535c216457" +
760
"e87305bc59a1ff1f7e1e0bbdf302b75549b136606c67d7e5f71277aeca4bc670" +
761
"07a98f78e0cfa002ed183e62f07893ad31fe67aad1bb37e15b957a14d145f14f" +
762
"7483d041f2c3612ad5033155984470bdfc64d18df73c2745d92f28461bb09832" +
763
"33524811321ba87d213692825815dd13f528dba601a3c319cac6be9b48686c23" +
764
"a0ce23d5062916ea8827bbb243f585e446131489e951354c8ab24661f625c02e" +
765
"15536c5bb602244e98993ff745f3e523399b2059f0e062d8933fad2366e7e147" +
766
"510a931282bb0e3f635efe7bf05b1dd715f95f5858261b00735224256b6b3e80",
767
"08b3593840d4ed005f5234ae062a5c"),
768
// Random test data
769
new Data("AES", 4, "272f16edb81a7abbea887357a58c1917",
770
"794ec588176c703d3d2a7a07",
771
new byte[2075], null,
772
"15b461672153270e8ba1e6789f7641c5411f3e642abda731b6086f535c216457" +
773
"e87305bc59a1ff1f7e1e0bbdf302b75549b136606c67d7e5f71277aeca4bc670" +
774
"07a98f78e0cfa002ed183e62f07893ad31fe67aad1bb37e15b957a14d145f14f" +
775
"7483d041f2c3612ad5033155984470bdfc64d18df73c2745d92f28461bb09832" +
776
"33524811321ba87d213692825815dd13f528dba601a3c319cac6be9b48686c23" +
777
"a0ce23d5062916ea8827bbb243f585e446131489e951354c8ab24661f625c02e" +
778
"15536c5bb602244e98993ff745f3e523399b2059f0e062d8933fad2366e7e147" +
779
"510a931282bb0e3f635efe7bf05b1dd715f95f5858261b00735224256b6b3e80" +
780
"7364cb53ff6d4e88f928cf67ac70da127718a8a35542efbae9dd7567c818a074" +
781
"9a0c74bd69014639f59768bc55056d1166ea5523e8c66f9d78d980beb8f0d83b" +
782
"a9e2c5544b94dc3a1a4b6f0f95f897b010150e89ebcacf0daee3c2793d6501a0" +
783
"b58b411de273dee987e8e8cf8bb29ef2e7f655b46b55fabf64c6a4295e0d080b" +
784
"6a570ace90eb0fe0f5b5d878bdd90eddaa1150e4d5a6505b350aac814fe99615" +
785
"317ecd0516a464c7904011ef5922409c0d65b1e43b69d7c3293a8f7d3e9fbee9" +
786
"eb91ec0007a7d6f72e64deb675d459c5ba07dcfd58d08e6820b100465e6e04f0" +
787
"663e310584a00d36d23699c1bffc6afa094c75184fc7cde7ad35909c0f49f2f3" +
788
"fe1e6d745ab628d74ea56b757047de57ce18b4b3c71e8af31a6fac16189cb0a3" +
789
"a97a1bea447042ce382fcf726560476d759c24d5c735525ea26a332c2094408e" +
790
"671c7deb81d5505bbfd178f866a6f3a011b3cfdbe089b4957a790688028dfdf7" +
791
"9a096b3853f9d0d6d3feef230c7f5f46ffbf7486ebdaca5804dc5bf9d202415e" +
792
"e0d67b365c2f92a17ea740807e4f0b198b42b54f15faa9dff2c7c35d2cf8d72e" +
793
"b8f8b18875a2e7b5c43d1e0aa5139c461e8153c7f632895aa46ffe2b134e6a0d" +
794
"dfbf6a336e709adfe951bd52c4dfc7b07a15fb3888fc35b7e758922f87a104c4" +
795
"563c5c7839cfe5a7edbdb97264a7c4ebc90367b10cbe09dbf2390767ad7afaa8" +
796
"8fb46b39d3f55f216d2104e5cf040bf3d39b758bea28e2dbce576c808d17a8eb" +
797
"e2fd183ef42a774e39119dff1f539efeb6ad15d889dfcb0d54d0d4d4cc03c8d9" +
798
"aa6c9ebd157f5e7170183298d6a30ada8792dcf793d931e2a1eafccbc63c11c0" +
799
"c5c5ed60837f30017d693ccb294df392a8066a0594a56954aea7b78a16e9a11f" +
800
"4a8bc2104070a7319f5fab0d2c4ccad8ec5cd8f47c839179bfd54a7bf225d502" +
801
"cd0a318752fe763e8c09eb88fa57fc5399ad1f797d0595c7b8afdd23f13603e9" +
802
"6802192bb51433b7723f4e512bd4f799feb94b458e7f9792f5f9bd6733828f70" +
803
"a6b7ffbbc0bb7575021f081ec2a0d37fecd7cda2daec9a3a9d9dfe1c8034cead" +
804
"e4b56b581cc82bd5b74b2b30817967d9da33850336f171a4c68e2438e03f4b11" +
805
"96da92f01b3b7aeab795180ccf40a4b090b1175a1fc0b67c95f93105c3aef00e" +
806
"13d76cc402539192274fee703730cd0d1c5635257719cc96cacdbad00c6255e2" +
807
"bd40c775b43ad09599e84f2c3205d75a6661ca3f151183be284b354ce21457d1" +
808
"3ba65b9b2cdb81874bd14469c2008b3ddec78f7225ecc710cc70de7912ca6a6d" +
809
"348168322ab59fdafcf5c833bfa0ad4046f4b6da90e9f263db7079af592eda07" +
810
"5bf16c6b1a8346da9c292a48bf660860a4fc89eaef40bc132779938eca294569" +
811
"787c740af2b5a8de7f5e10ac750d1e3d0ef3ed168ba408a676e10b8a20bd4be8" +
812
"3e8336b45e54481726d73e1bd19f165a98e242aca0d8387f2dd22d02d74e23db" +
813
"4cef9a523587413e0a44d7e3260019a34d3a6b38426ae9fa4655be338d721970" +
814
"cb9fe76c073f26f9303093a033022cd2c62b2790bce633ba9026a1c93b6535f1" +
815
"1882bf5880e511b9e1b0b7d8f23a993aae5fd275faac3a5b4ccaf7c06b0b266a" +
816
"ee970a1e3a4cd7a41094f516960630534e692545b25a347c30e3f328bba4825f" +
817
"ed754e5525d846131ecba7ca120a6aeabc7bab9f59c890c80b7e31f9bc741591" +
818
"55d292433ce9558e104102f2cc63ee267c1c8333e841522707ea6d595cb802b9" +
819
"61697da77bbc4cb404ea62570ab335ebffa2023730732ac5ddba1c3dbb5be408" +
820
"3c50aea462c1ffa166d7cc3db4b742b747e81b452db2363e91374dee8c6b40f0" +
821
"e7fbf50e60eaf5cc5649f6bb553aae772c185026ceb052af088c545330a1ffbf" +
822
"50615b8c7247c6cd386afd7440654f4e15bcfae0c45442ec814fe88433a9d616" +
823
"ee6cc3f163f0d3d325526d05f25d3b37ad5eeb3ca77248ad86c9042b16c65554" +
824
"aebb6ad3e17b981492b13f42c5a5dc088e991da303e5a273fdbb8601aece4267" +
825
"47b01f6cb972e6da1743a0d7866cf206e95f23c6f8e337c901b9cd34a9a1fbbe" +
826
"1694f2c26b00dfa4d02c0d54540163e798fbdc9c25f30d6406f5b4c13f7ed619" +
827
"34e350f4059c13aa5e973307a9e3058917cda96fdd082e9c629ccfb2a9f98d12" +
828
"5c6e4703a7b0f348f5cdeb63cef2133d1c6c1a087591e0a2bca29d09c6565e66" +
829
"e91042f83b0e74e60a5d57562c23e2fbcd6599c29d7c19e47cf625c2ce24bb8a" +
830
"13f8e54041498437eec2cedd1e3d8e57a051baa962c0a62d70264d99c5ee716d" +
831
"5c8b9078db08c8b2c5613f464198a7aff43f76c5b4612b46a4f1cd2a494386c5" +
832
"7fd28f3d199f0ba8d8e39116cc7db16ce6188205ee49a9dce3d4fa32ea394919" +
833
"f6e91ef58b84d00b99596b4306c2d9f432d917bb4ac73384c42ae12adb4920d8" +
834
"c33a816febcb299dcddf3ec7a8eb6e04cdc90891c6e145bd9fc5f41dc4061a46" +
835
"9feba38545b64ec8203f386ceef52785619e991d274ae80af7e54af535e0b011" +
836
"5effdf847472992875e09398457604d04e0bb965db692c0cdcf11a",
837
"687cc09c89298491deb51061d709af"),
838
// Randomly generated data at the time of execution.
839
new Data("AES", 5, "11754cd72aec309bf52f7687212e8957", 12345)
840
)
841
);
842
}
843
}
844
845