jdk/test/com/sun/crypto/provider/Cipher/AES/TestCopySafe.java
author valeriep
Tue, 01 Jul 2014 20:35:43 +0000
changeset 25214 78a0f67da1d4
parent 21837 0c41fa97176a
permissions -rw-r--r--
8027575: b113 causing a lot of memory allocation and regression for wls_webapp_atomics Summary: Reduce buffer allocation and do multiple cipher encryption/decryption calls. Reviewed-by: xuelei, ascarpino
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     1
/*
25214
78a0f67da1d4 8027575: b113 causing a lot of memory allocation and regression for wls_webapp_atomics
valeriep
parents: 21837
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     4
 *
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     8
 *
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    13
 * accompanied this code).
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    14
 *
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    18
 *
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    21
 * questions.
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    22
 */
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    23
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    24
/*
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    25
 * @test
25214
78a0f67da1d4 8027575: b113 causing a lot of memory allocation and regression for wls_webapp_atomics
valeriep
parents: 21837
diff changeset
    26
 * @bug 8026943 8027575
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    27
 * @summary Verify that same buffer can be used as input and output when
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    28
 * using Cipher objects.
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    29
 * @author Valerie Peng
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    30
 */
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    31
import java.security.*;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    32
import java.security.spec.*;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    33
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    34
import java.util.Arrays;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    35
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    36
import javax.crypto.*;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    37
import javax.crypto.spec.*;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    38
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    39
public class TestCopySafe {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    40
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    41
    private static boolean DEBUG = false;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    42
    private static int INPUT_LENGTH = 160; // must be multiple of block size
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    43
    private static byte[] PT = new byte[INPUT_LENGTH];
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    44
    private static SecretKey KEY = new SecretKeySpec(new byte[16], "AES");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    45
    private static byte[] IV = new byte[16];
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    46
25214
78a0f67da1d4 8027575: b113 causing a lot of memory allocation and regression for wls_webapp_atomics
valeriep
parents: 21837
diff changeset
    47
    private static int[] OFFSETS = { 1, 8, 9, 16, 17, 32, 33 };
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    48
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    49
    private static final String[] MODES = {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    50
        "ECB", "CBC", "PCBC", "CTR", "CTS",
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    51
        "CFB", "CFB8", "CFB16", "CFB24", "CFB32", "CFB40",
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    52
        "CFB48", "CFB56", "CFB64",
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    53
        "OFB", "OFB8", "OFB16", "OFB24", "OFB32", "OFB40",
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    54
        "OFB48", "OFB56", "OFB64",
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    55
        "GCM"
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    56
    };
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    57
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    58
    public static void main(String[] argv) throws Exception {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    59
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    60
        Provider p = Security.getProvider("SunJCE");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    61
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    62
        AlgorithmParameterSpec params = null;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    63
        boolean result = true;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    64
        for (String mode : MODES) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    65
            String transformation = "AES/" + mode + "/NoPadding";
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    66
            boolean isGCM = (mode == "GCM");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    67
            if (isGCM) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    68
                params = new GCMParameterSpec(128, IV);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    69
            } else if (mode != "ECB") {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    70
                params = new IvParameterSpec(IV);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    71
            }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    72
            Cipher c = Cipher.getInstance(transformation, p);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    73
            System.out.println("Testing " + transformation + ":");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    74
            for (int offset : OFFSETS) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    75
                System.out.print("=> offset " + offset + ": ");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    76
                try {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    77
                    test(c, params, offset, isGCM);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    78
                    System.out.println("Passed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    79
                } catch(Exception ex) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    80
                    ex.printStackTrace();
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    81
                    result = false;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    82
                    continue;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    83
                }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    84
            }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    85
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    86
        if (!result) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    87
            throw new Exception("One or more test failed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    88
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    89
    }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    90
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    91
    private static void test(Cipher c, AlgorithmParameterSpec params,
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    92
        int offset, boolean isGCM) throws Exception {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    93
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    94
        // Test encryption first
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    95
        if (isGCM) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    96
            // re-init with only key value first to bypass the
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    97
            // Key+IV-uniqueness check for GCM encryption
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    98
            c.init(Cipher.ENCRYPT_MODE, KEY);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
    99
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   100
        c.init(Cipher.ENCRYPT_MODE, KEY, params);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   101
        byte[] answer = c.doFinal(PT);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   102
        byte[] pt2 = Arrays.copyOf(PT, answer.length + offset);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   103
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   104
        // #1: outOfs = inOfs = 0
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   105
        if (isGCM) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   106
            c.init(Cipher.ENCRYPT_MODE, KEY);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   107
            c.init(Cipher.ENCRYPT_MODE, KEY, params);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   108
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   109
        c.doFinal(pt2, 0, PT.length, pt2, 0);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   110
        if (!isTwoArraysEqual(pt2, 0, answer, 0, answer.length)) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   111
            throw new Exception("Enc#1 diff check failed!");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   112
        } else if (DEBUG) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   113
            System.out.println("Enc#1 diff check passed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   114
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   115
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   116
        // #2: inOfs = 0, outOfs = offset
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   117
        System.arraycopy(PT, 0, pt2, 0, PT.length);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   118
        if (isGCM) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   119
            c.init(Cipher.ENCRYPT_MODE, KEY);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   120
            c.init(Cipher.ENCRYPT_MODE, KEY, params);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   121
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   122
        c.doFinal(pt2, 0, PT.length, pt2, offset);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   123
        if (!isTwoArraysEqual(pt2, offset, answer, 0, answer.length)) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   124
            throw new Exception("Enc#2 diff check failed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   125
        } else if (DEBUG) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   126
            System.out.println("Enc#2 diff check passed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   127
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   128
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   129
        // #3: inOfs = offset, outOfs = 0
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   130
        System.arraycopy(PT, 0, pt2, offset, PT.length);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   131
        if (isGCM) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   132
            c.init(Cipher.ENCRYPT_MODE, KEY);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   133
            c.init(Cipher.ENCRYPT_MODE, KEY, params);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   134
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   135
        c.doFinal(pt2, offset, PT.length, pt2, 0);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   136
        if (!isTwoArraysEqual(pt2, 0, answer, 0, answer.length)) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   137
            throw new Exception("Enc#3 diff check failed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   138
        } else if (DEBUG) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   139
            System.out.println("Enc#3 diff check passed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   140
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   141
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   142
       // Test decryption now, we should get back PT as a result
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   143
        c.init(Cipher.DECRYPT_MODE, KEY, params);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   144
        pt2 = Arrays.copyOf(answer, answer.length + offset);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   145
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   146
        // #1: outOfs = inOfs = 0
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   147
        c.doFinal(pt2, 0, answer.length, pt2, 0);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   148
        if (!isTwoArraysEqual(pt2, 0, PT, 0, PT.length)) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   149
            throw new Exception("Dec#1 diff check failed!");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   150
        } else if (DEBUG) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   151
            System.out.println("Dec#1 diff check passed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   152
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   153
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   154
        // #2: inOfs = 0, outOfs = offset
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   155
        System.arraycopy(answer, 0, pt2, 0, answer.length);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   156
        c.doFinal(pt2, 0, answer.length, pt2, offset);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   157
        if (!isTwoArraysEqual(pt2, offset, PT, 0, PT.length)) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   158
            throw new Exception("Dec#2 diff check failed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   159
        } else if (DEBUG) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   160
            System.out.println("Dec#2 diff check passed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   161
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   162
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   163
        // #3: inOfs = offset, outOfs = 0
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   164
        System.arraycopy(answer, 0, pt2, offset, answer.length);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   165
        c.doFinal(pt2, offset, answer.length, pt2, 0);
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   166
        if (!isTwoArraysEqual(pt2, 0, PT, 0, PT.length)) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   167
            throw new Exception("Dec#3 diff check failed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   168
        } else if (DEBUG) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   169
            System.out.println("Dec#3 diff check passed");
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   170
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   171
    }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   172
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   173
    private static boolean isTwoArraysEqual(byte[] a, int aOff, byte[] b, int bOff,
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   174
        int len) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   175
        for (int i = 0; i < len; i++) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   176
            if (a[aOff + i] != b[bOff + i]) {
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   177
                return false;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   178
            }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   179
        }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   180
        return true;
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   181
    }
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   182
}
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
diff changeset
   183