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