jdk/test/com/sun/crypto/provider/Cipher/AES/TestAESCiphers/Dynamic.java
author valeriep
Fri, 06 Mar 2015 00:49:38 +0000
changeset 29366 fe7c66943ba1
child 42362 7b70c5bfe82e
permissions -rw-r--r--
8044193: Need to add known answer tests for AES cipher Summary: Added more tests for AES cipher using known test vectors. Reviewed-by: valeriep Contributed-by: Zaiyao Liu <zaiyao.liu@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29366
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     1
/*
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     4
 *
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     8
 *
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    13
 * accompanied this code).
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    14
 *
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    18
 *
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    21
 * questions.
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    22
 */
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    23
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    24
import java.io.PrintStream;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    25
import java.security.NoSuchAlgorithmException;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    26
import java.security.spec.AlgorithmParameterSpec;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    27
import java.util.Arrays;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    28
import java.util.Random;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    29
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    30
import javax.crypto.Cipher;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    31
import javax.crypto.KeyGenerator;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    32
import javax.crypto.SecretKey;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    33
import javax.crypto.spec.IvParameterSpec;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    34
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    35
public class Dynamic {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    36
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    37
    static final String ALGORITHM = "AES";
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    38
    static final String[] MODE = {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    39
        "ECb", "CbC", "CTR", "PCBC", "OFB", "OFB150", "cFB", "CFB7",
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    40
        "cFB8", "cFB16", "cFB24", "cFB32", "Cfb40", "cfB48", "cfB56",
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    41
        "cfB64", "cfB72", "cfB80", "cfB88", "cfB96", "cfb104", "cfB112",
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    42
        "cfB120", "cfB128", "OFB8", "OFB16", "OFB24", "OFB32", "OFB40",
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    43
        "OFB48", "OFB56", "OFB64", "OFB72", "OFB80", "OFB88", "OFB96",
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    44
        "OFB104", "OFB112", "OFB120", "OFB128", "GCM"
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    45
    };
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    46
    static final String[] PADDING = {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    47
        "NoPadding", "PKCS5Padding", "ISO10126Padding"
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    48
    };
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    49
    static final String SUNJCE = "SunJCE";
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    50
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    51
    Cipher ci = null;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    52
    byte[] iv = null;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    53
    AlgorithmParameterSpec aps = null;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    54
    SecretKey key = null;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    55
    int keyStrength;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    56
    static int DefaultSize = 128;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    57
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    58
    public void run(String[] argv) throws Exception {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    59
        if (!runAllTest(argv, System.out)) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    60
            throw new Exception("Test Failed");
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    61
        }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    62
    }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    63
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    64
    protected boolean runAllTest(String argv[], PrintStream out) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    65
        boolean result = true;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    66
        StringBuilder failedList = new StringBuilder();
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    67
        int failedCnt = 0;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    68
        int testCount = 0;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    69
        int padKinds; // how many kinds of padding mode such as PKCS5padding and
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    70
        // NoPadding.
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    71
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    72
        try {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    73
            for (int i = 0; i < 3; i++) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    74
                keyStrength = DefaultSize + i * 64; // obtain the key size 128,
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    75
                // 192, 256
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    76
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    77
                for (int j = 0; j < MODE.length; j++) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    78
                    if (MODE[j].equalsIgnoreCase("ECB")
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    79
                            || MODE[j].equalsIgnoreCase("PCBC")
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    80
                            || MODE[j].equalsIgnoreCase("CBC")) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    81
                        padKinds = PADDING.length;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    82
                    } else {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    83
                        padKinds = 1;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    84
                    }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    85
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    86
                    for (int k = 0; k < padKinds; k++) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    87
                        testCount++;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    88
                        try {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    89
                            if (!runTest(ALGORITHM, MODE[j], PADDING[k])) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    90
                                result = false;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    91
                                failedCnt++;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    92
                                failedList.append(ALGORITHM + "/" + MODE[j]
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    93
                                        + "/" + PADDING[k] + " ");
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    94
                            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    95
                        } catch (Exception e) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    96
                            e.printStackTrace();
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    97
                            result = false;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    98
                            failedCnt++;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
    99
                            failedList.append(ALGORITHM + "/" + MODE[j] + "/"
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   100
                                    + PADDING[k] + " ");
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   101
                        }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   102
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   103
                    }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   104
                }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   105
            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   106
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   107
            if (result) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   108
                out.println("STATUS:Passed. Test " + testCount
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   109
                        + " cases, All Passed");
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   110
                return true;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   111
            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   112
            out.println("STATUS:Failed. " + failedCnt + " Failed: "
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   113
                    + failedList);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   114
            return false;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   115
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   116
        } catch (Exception ex) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   117
            ex.printStackTrace();
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   118
            out.println("STATUS:Failed. Unexpected Exception: " + ex);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   119
            return false;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   120
        }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   121
    }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   122
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   123
    protected boolean runTest(String algo, String mo, String pad)
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   124
            throws Exception {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   125
        boolean result = true;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   126
        try {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   127
            byte[] plainText = new byte[160000];
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   128
            new Random().nextBytes(plainText);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   129
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   130
            String transformation = algo + "/" + mo + "/" + pad;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   131
            ci = Cipher.getInstance(transformation, SUNJCE);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   132
            KeyGenerator kg = KeyGenerator.getInstance(algo, SUNJCE);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   133
            if (keyStrength > Cipher.getMaxAllowedKeyLength(transformation)) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   134
                // skip if this key length is larger than what's
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   135
                // configured in the jce jurisdiction policy files
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   136
                System.out.println(keyStrength
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   137
                        + " is larger than what's configured "
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   138
                        + "in the jce jurisdiction policy files");
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   139
                return result;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   140
            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   141
            kg.init(keyStrength);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   142
            key = kg.generateKey();
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   143
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   144
            if (!mo.equalsIgnoreCase("GCM")) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   145
                ci.init(Cipher.ENCRYPT_MODE, key, aps);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   146
            } else {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   147
                ci.init(Cipher.ENCRYPT_MODE, key);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   148
            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   149
            byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   150
            int offset = ci.update(plainText, 0, plainText.length, cipherText,
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   151
                    0);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   152
            ci.doFinal(cipherText, offset);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   153
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   154
            if (!mo.equalsIgnoreCase("ECB")) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   155
                iv = ci.getIV();
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   156
                aps = new IvParameterSpec(iv);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   157
            } else {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   158
                aps = null;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   159
            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   160
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   161
            if (!mo.equalsIgnoreCase("GCM")) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   162
                ci.init(Cipher.DECRYPT_MODE, key, aps);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   163
            } else {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   164
                ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   165
            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   166
            byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   167
            int len = ci.doFinal(cipherText, 0, cipherText.length,
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   168
                    recoveredText);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   169
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   170
            byte[] tmp = new byte[len];
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   171
            for (int i = 0; i < len; i++) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   172
                tmp[i] = recoveredText[i];
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   173
            }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   174
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   175
            result = Arrays.equals(plainText, tmp);
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   176
        } catch (NoSuchAlgorithmException nsaEx) {
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   177
            nsaEx.printStackTrace();
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   178
            // CFB7 and OFB150 are negative test,SunJCE not support this
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   179
            // algorithm
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   180
            result = mo.equalsIgnoreCase("CFB7")
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   181
                    || mo.equalsIgnoreCase("OFB150");
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   182
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   183
        }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   184
        return result;
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   185
    }
fe7c66943ba1 8044193: Need to add known answer tests for AES cipher
valeriep
parents:
diff changeset
   186
}