test/hotspot/jtreg/compiler/codegen/aes/TestAESBase.java
author valeriep
Mon, 29 Jul 2019 20:18:43 +0000
changeset 57586 f459f98aa30d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding Summary: Updated test to use NoPadding as default if the mode is one of GCM, CTR and CTS Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     1
/*
57586
f459f98aa30d 8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     4
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     8
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    13
 * accompanied this code).
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    14
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    18
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    21
 * questions.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    22
 */
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    23
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    24
package compiler.codegen.aes;
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    25
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 27699
diff changeset
    26
import jdk.test.lib.Utils;
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    27
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    28
import javax.crypto.Cipher;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    29
import javax.crypto.SecretKey;
31404
63e8fcd70bfc 8073108: Use x86 and SPARC CPU instructions for GHASH acceleration
ascarpino
parents: 30604
diff changeset
    30
import javax.crypto.spec.GCMParameterSpec;
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    31
import javax.crypto.spec.IvParameterSpec;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    32
import javax.crypto.spec.SecretKeySpec;
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    33
import java.security.AlgorithmParameters;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    34
import java.util.Random;
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    35
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    36
/**
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    37
 * @author Tom Deneau
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    38
 */
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    39
public abstract class TestAESBase {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    40
    int msgSize = Integer.getInteger("msgSize", 646);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    41
    boolean checkOutput = Boolean.getBoolean("checkOutput");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    42
    boolean noReinit = Boolean.getBoolean("noReinit");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    43
    boolean testingMisalignment;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    44
    private static final int ALIGN = 8;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    45
    int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    46
    int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    47
    int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    48
    int lastChunkSize = Integer.getInteger("lastChunkSize", 32);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    49
    int keySize = Integer.getInteger("keySize", 128);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    50
    int inputLength;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    51
    int encodeLength;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    52
    int decodeLength;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    53
    int decodeMsgSize;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    54
    String algorithm = System.getProperty("algorithm", "AES");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    55
    String mode = System.getProperty("mode", "CBC");
57586
f459f98aa30d 8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents: 47216
diff changeset
    56
    String paddingStr = System.getProperty("paddingStr",
f459f98aa30d 8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents: 47216
diff changeset
    57
        (mode.equals("GCM") || mode.equals("CTR") || mode.equals("CTS")) ?
f459f98aa30d 8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents: 47216
diff changeset
    58
         "NoPadding" : "PKCS5Padding");
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    59
    byte[] input;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    60
    byte[] encode;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    61
    byte[] expectedEncode;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    62
    byte[] decode;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    63
    byte[] expectedDecode;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    64
    final Random random = Utils.getRandomInstance();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    65
    Cipher cipher;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    66
    Cipher dCipher;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    67
    AlgorithmParameters algParams = null;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    68
    SecretKey key;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    69
    GCMParameterSpec gcm_spec;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    70
    byte[] aad = {0x11, 0x22, 0x33, 0x44, 0x55};
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    71
    int tlen = 12;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    72
    byte[] iv = new byte[16];
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    73
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    74
    static int numThreads = 0;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    75
    int threadId;
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    76
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    77
    static synchronized int getThreadId() {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    78
        int id = numThreads;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    79
        numThreads++;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    80
        return id;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    81
    }
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    82
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    83
    abstract public void run();
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    84
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    85
    public void prepare() {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    86
        try {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    87
            System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr +
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    88
                    ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit +
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    89
                    ", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" +
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    90
                    encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" + lastChunkSize);
24328
bddefb356fba 8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents: 14834
diff changeset
    91
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    92
            if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN != 0)
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    93
                testingMisalignment = true;
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    94
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    95
            int keyLenBytes = (keySize == 0 ? 16 : keySize / 8);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    96
            byte keyBytes[] = new byte[keyLenBytes];
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    97
            if (keySize == 128)
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    98
                keyBytes = new byte[]{-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
    99
            else
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   100
                random.nextBytes(keyBytes);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   101
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   102
            key = new SecretKeySpec(keyBytes, algorithm);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   103
            if (threadId == 0) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   104
                System.out.println("Algorithm: " + key.getAlgorithm() + "("
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   105
                        + key.getEncoded().length * 8 + "bit)");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   106
            }
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   107
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   108
            cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   109
            dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   110
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   111
            // CBC or CTR init
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   112
            if (mode.equals("CBC") || mode.equals("CTR")) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   113
                IvParameterSpec initVector = new IvParameterSpec(iv);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   114
                cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   115
                algParams = cipher.getParameters();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   116
                dCipher.init(Cipher.DECRYPT_MODE, key, initVector);
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   117
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   118
                // GCM init
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   119
            } else if (mode.equals("GCM")) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   120
                gcm_init(true);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   121
                gcm_init(false);
31771
c9f593020799 8130341: GHASH 32bit intrinsics has AEADBadTagException
ascarpino
parents: 31404
diff changeset
   122
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   123
                // ECB init
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   124
            } else {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   125
                cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   126
                dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   127
            }
31771
c9f593020799 8130341: GHASH 32bit intrinsics has AEADBadTagException
ascarpino
parents: 31404
diff changeset
   128
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   129
            if (threadId == 0) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   130
                childShowCipher();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   131
            }
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   132
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   133
            inputLength = msgSize + encInputOffset;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   134
            if (testingMisalignment) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   135
                encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   136
                encodeLength += cipher.getOutputSize(lastChunkSize);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   137
                decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   138
                decodeLength += dCipher.getOutputSize(lastChunkSize);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   139
            } else {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   140
                encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   141
                decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   142
            }
24328
bddefb356fba 8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents: 14834
diff changeset
   143
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   144
            input = new byte[inputLength];
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   145
            for (int i = encInputOffset, j = 0; i < inputLength; i++, j++) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   146
                input[i] = (byte) (j & 0xff);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   147
            }
24328
bddefb356fba 8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents: 14834
diff changeset
   148
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   149
            // do one encode and decode in preparation
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   150
            encode = new byte[encodeLength];
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   151
            decode = new byte[decodeLength];
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   152
            if (testingMisalignment) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   153
                decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   154
                decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize));
24328
bddefb356fba 8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents: 14834
diff changeset
   155
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   156
                int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   157
                dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize));
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   158
            } else {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   159
                decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   160
                dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   161
            }
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   162
            if (checkOutput) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   163
                expectedEncode = (byte[]) encode.clone();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   164
                expectedDecode = (byte[]) decode.clone();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   165
                showArray(key.getEncoded(), "key:    ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   166
                showArray(input, "input:  ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   167
                showArray(encode, "encode: ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   168
                showArray(decode, "decode: ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   169
            }
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   170
        } catch (Exception e) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   171
            e.printStackTrace();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   172
            System.exit(1);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   173
        }
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   174
    }
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   175
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   176
    void showArray(byte b[], String name) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   177
        System.out.format("%s [%d]: ", name, b.length);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   178
        for (int i = 0; i < Math.min(b.length, 32); i++) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   179
            System.out.format("%02x ", b[i] & 0xff);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   180
        }
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   181
        System.out.println();
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   182
    }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   183
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   184
    void compareArrays(byte b[], byte exp[]) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   185
        if (b.length != exp.length) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   186
            System.out.format("different lengths for actual and expected output arrays\n");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   187
            showArray(b, "test: ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   188
            showArray(exp, "exp : ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   189
            System.exit(1);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   190
        }
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   191
        for (int i = 0; i < exp.length; i++) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   192
            if (b[i] != exp[i]) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   193
                System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   194
                showArray(b, "test: ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   195
                showArray(exp, "exp : ");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   196
                System.exit(1);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   197
            }
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   198
        }
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   199
    }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   200
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   201
    void showCipher(Cipher c, String kind) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   202
        System.out.println(kind + " cipher provider: " + cipher.getProvider());
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   203
        System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   204
    }
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   205
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   206
    abstract void childShowCipher();
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   207
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   208
    void gcm_init(boolean encrypt) throws Exception {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   209
        gcm_spec = new GCMParameterSpec(tlen * 8, iv);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   210
        if (encrypt) {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   211
            // Get a new instance everytime because of reuse IV restrictions
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   212
            cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   213
            cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   214
            cipher.updateAAD(aad);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   215
        } else {
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   216
            dCipher.init(Cipher.DECRYPT_MODE, key, gcm_spec);
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   217
            dCipher.updateAAD(aad);
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   218
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   219
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35154
diff changeset
   220
        }
31771
c9f593020799 8130341: GHASH 32bit intrinsics has AEADBadTagException
ascarpino
parents: 31404
diff changeset
   221
    }
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   222
}