test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphers.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43248 jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java@5e15de85a1a0
child 51460 97e361fe3433
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
     1
/*
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
     2
 * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
     4
 *
628
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
     8
 *
628
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    13
 * accompanied this code).
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    14
 *
628
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
914de0870706 6706358: jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java has the wrong copyright notice.
wetmore
parents: 289
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 628
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 628
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 628
diff changeset
    21
 * questions.
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    22
 */
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    23
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    24
/*
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 8542
diff changeset
    25
 * @test
8542
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 5506
diff changeset
    26
 * @bug 4898461 6604496
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    27
 * @summary basic test for symmetric ciphers with padding
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    28
 * @author Valerie Peng
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    29
 * @library ..
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 8542
diff changeset
    30
 * @key randomness
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
    31
 * @modules jdk.crypto.cryptoki
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    32
 * @run main/othervm TestSymmCiphers
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    33
 * @run main/othervm TestSymmCiphers sm
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    34
 */
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    35
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    36
import java.io.ByteArrayOutputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    37
import java.nio.ByteBuffer;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    38
import java.security.AlgorithmParameters;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    39
import java.security.NoSuchAlgorithmException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    40
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    41
import java.util.Random;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    42
import javax.crypto.Cipher;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    43
import javax.crypto.KeyGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    44
import javax.crypto.SecretKey;
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    45
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    46
public class TestSymmCiphers extends PKCS11Test {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    47
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    48
    private static class CI { // class for holding Cipher Information
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    49
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    50
        String transformation;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    51
        String keyAlgo;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    52
        int dataSize;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    53
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    54
        CI(String transformation, String keyAlgo, int dataSize) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    55
            this.transformation = transformation;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    56
            this.keyAlgo = keyAlgo;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    57
            this.dataSize = dataSize;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    58
        }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    59
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    60
    private static final CI[] TEST_LIST = {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    61
        new CI("ARCFOUR", "ARCFOUR", 400),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    62
        new CI("RC4", "RC4", 401),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    63
        new CI("DES/CBC/NoPadding", "DES", 400),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    64
        new CI("DESede/CBC/NoPadding", "DESede", 160),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    65
        new CI("AES/CBC/NoPadding", "AES", 4800),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    66
        new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    67
        new CI("DES/cbc/PKCS5Padding", "DES", 6401),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    68
        new CI("DESede/CBC/PKCS5Padding", "DESede", 402),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    69
        new CI("AES/CBC/PKCS5Padding", "AES", 30),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    70
        new CI("Blowfish/CBC/PKCS5Padding", "Blowfish", 19),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    71
        new CI("DES/ECB/NoPadding", "DES", 400),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    72
        new CI("DESede/ECB/NoPadding", "DESede", 160),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    73
        new CI("AES/ECB/NoPadding", "AES", 4800),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    74
        new CI("DES/ECB/PKCS5Padding", "DES", 32),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    75
        new CI("DES/ECB/PKCS5Padding", "DES", 6400),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    76
        new CI("DESede/ECB/PKCS5Padding", "DESede", 400),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    77
        new CI("AES/ECB/PKCS5Padding", "AES", 64),
8542
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 5506
diff changeset
    78
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    79
        new CI("DES", "DES", 6400),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    80
        new CI("DESede", "DESede", 408),
8542
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 5506
diff changeset
    81
        new CI("AES", "AES", 128),
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 5506
diff changeset
    82
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 5506
diff changeset
    83
        new CI("AES/CTR/NoPadding", "AES", 3200)
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 5506
diff changeset
    84
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    85
    };
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    86
    private static StringBuffer debugBuf = new StringBuffer();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    87
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    88
    @Override
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    89
    public void main(Provider p) throws Exception {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    90
        // NSS reports CKR_DEVICE_ERROR when the data passed to
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    91
        // its EncryptUpdate/DecryptUpdate is not multiple of blocks
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    92
        int firstBlkSize = 16;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    93
        boolean status = true;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    94
        Random random = new Random();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    95
        try {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    96
            for (int i = 0; i < TEST_LIST.length; i++) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    97
                CI currTest = TEST_LIST[i];
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    98
                System.out.println("===" + currTest.transformation + "===");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
    99
                try {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   100
                    KeyGenerator kg =
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   101
                            KeyGenerator.getInstance(currTest.keyAlgo, p);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   102
                    SecretKey key = kg.generateKey();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   103
                    Cipher c1 = Cipher.getInstance(currTest.transformation, p);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   104
                    Cipher c2 = Cipher.getInstance(currTest.transformation,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   105
                            "SunJCE");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   106
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   107
                    byte[] plainTxt = new byte[currTest.dataSize];
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   108
                    random.nextBytes(plainTxt);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   109
                    System.out.println("Testing inLen = " + plainTxt.length);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   110
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   111
                    c2.init(Cipher.ENCRYPT_MODE, key);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   112
                    AlgorithmParameters params = c2.getParameters();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   113
                    byte[] answer = c2.doFinal(plainTxt);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   114
                    System.out.println("Encryption tests: START");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   115
                    test(c1, Cipher.ENCRYPT_MODE, key, params, firstBlkSize,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   116
                            plainTxt, answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   117
                    System.out.println("Encryption tests: DONE");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   118
                    c2.init(Cipher.DECRYPT_MODE, key, params);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   119
                    byte[] answer2 = c2.doFinal(answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   120
                    System.out.println("Decryption tests: START");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   121
                    test(c1, Cipher.DECRYPT_MODE, key, params, firstBlkSize,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   122
                            answer, answer2);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   123
                    System.out.println("Decryption tests: DONE");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   124
                } catch (NoSuchAlgorithmException nsae) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   125
                    System.out.println("Skipping unsupported algorithm: " +
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   126
                            nsae);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   127
                }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   128
            }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   129
        } catch (Exception ex) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   130
            // print out debug info when exception is encountered
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   131
            if (debugBuf != null) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   132
                System.out.println(debugBuf.toString());
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   133
                debugBuf = new StringBuffer();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   134
            }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   135
            throw ex;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   136
        }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   137
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   138
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   139
    private static void test(Cipher cipher, int mode, SecretKey key,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   140
            AlgorithmParameters params, int firstBlkSize,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   141
            byte[] in, byte[] answer) throws Exception {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   142
        // test setup
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   143
        long startTime, endTime;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   144
        cipher.init(mode, key, params);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   145
        int outLen = cipher.getOutputSize(in.length);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   146
        //debugOut("Estimated output size = " + outLen + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   147
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   148
        // test data preparation
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   149
        ByteBuffer inBuf = ByteBuffer.allocate(in.length);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   150
        inBuf.put(in);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   151
        inBuf.position(0);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   152
        ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   153
        inDirectBuf.put(in);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   154
        inDirectBuf.position(0);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   155
        ByteBuffer outBuf = ByteBuffer.allocate(outLen);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   156
        ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   157
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   158
        // test#1: byte[] in + byte[] out
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   159
        //debugOut("Test#1:\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   160
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   161
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   162
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   163
        startTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   164
        byte[] temp = cipher.update(in, 0, firstBlkSize);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   165
        if (temp != null && temp.length > 0) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   166
            baos.write(temp, 0, temp.length);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   167
        }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   168
        temp = cipher.doFinal(in, firstBlkSize, in.length - firstBlkSize);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   169
        if (temp != null && temp.length > 0) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   170
            baos.write(temp, 0, temp.length);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   171
        }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   172
        byte[] testOut1 = baos.toByteArray();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   173
        endTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   174
        perfOut("stream InBuf + stream OutBuf: " +
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   175
                (endTime - startTime));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   176
        match(testOut1, answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   177
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   178
        // test#2: Non-direct Buffer in + non-direct Buffer out
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   179
        //debugOut("Test#2:\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   180
        //debugOut("inputBuf: " + inBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   181
        //debugOut("outputBuf: " + outBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   182
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   183
        startTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   184
        cipher.update(inBuf, outBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   185
        cipher.doFinal(inBuf, outBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   186
        endTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   187
        perfOut("non-direct InBuf + non-direct OutBuf: " +
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   188
                (endTime - startTime));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   189
        match(outBuf, answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   190
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   191
        // test#3: Direct Buffer in + direc Buffer out
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   192
        //debugOut("Test#3:\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   193
        //debugOut("(pre) inputBuf: " + inDirectBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   194
        //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   195
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   196
        startTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   197
        cipher.update(inDirectBuf, outDirectBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   198
        cipher.doFinal(inDirectBuf, outDirectBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   199
        endTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   200
        perfOut("direct InBuf + direct OutBuf: " +
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   201
                (endTime - startTime));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   202
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   203
        //debugOut("(post) inputBuf: " + inDirectBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   204
        //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   205
        match(outDirectBuf, answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   206
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   207
        // test#4: Direct Buffer in + non-direct Buffer out
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   208
        //debugOut("Test#4:\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   209
        inDirectBuf.position(0);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   210
        outBuf.position(0);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   211
        //debugOut("inputBuf: " + inDirectBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   212
        //debugOut("outputBuf: " + outBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   213
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   214
        startTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   215
        cipher.update(inDirectBuf, outBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   216
        cipher.doFinal(inDirectBuf, outBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   217
        endTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   218
        perfOut("direct InBuf + non-direct OutBuf: " +
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   219
                (endTime - startTime));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   220
        match(outBuf, answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   221
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   222
        // test#5: Non-direct Buffer in + direct Buffer out
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   223
        //debugOut("Test#5:\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   224
        inBuf.position(0);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   225
        outDirectBuf.position(0);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   226
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   227
        //debugOut("(pre) inputBuf: " + inBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   228
        //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   229
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   230
        startTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   231
        cipher.update(inBuf, outDirectBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   232
        cipher.doFinal(inBuf, outDirectBuf);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   233
        endTime = System.nanoTime();
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   234
        perfOut("non-direct InBuf + direct OutBuf: " +
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   235
                (endTime - startTime));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   236
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   237
        //debugOut("(post) inputBuf: " + inBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   238
        //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   239
        match(outDirectBuf, answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   240
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   241
        debugBuf = null;
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   242
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   243
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   244
    private static void perfOut(String msg) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   245
        if (debugBuf != null) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   246
            debugBuf.append("PERF>" + msg);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   247
        }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   248
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   249
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   250
    private static void debugOut(String msg) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   251
        if (debugBuf != null) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   252
            debugBuf.append(msg);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   253
        }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   254
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   255
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   256
    private static void match(byte[] b1, byte[] b2) throws Exception {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   257
        if (b1.length != b2.length) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   258
            debugOut("got len   : " + b1.length + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   259
            debugOut("expect len: " + b2.length + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   260
            throw new Exception("mismatch - different length! got: " + b1.length + ", expect: " + b2.length + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   261
        } else {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   262
            for (int i = 0; i < b1.length; i++) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   263
                if (b1[i] != b2[i]) {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   264
                    debugOut("got   : " + toString(b1) + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   265
                    debugOut("expect: " + toString(b2) + "\n");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   266
                    throw new Exception("mismatch");
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   267
                }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   268
            }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   269
        }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   270
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   271
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   272
    private static void match(ByteBuffer bb, byte[] answer) throws Exception {
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   273
        byte[] bbTemp = new byte[bb.position()];
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   274
        bb.position(0);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   275
        bb.get(bbTemp, 0, bbTemp.length);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   276
        match(bbTemp, answer);
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   277
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   278
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   279
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   280
        main(new TestSymmCiphers(), args);
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   281
    }
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents:
diff changeset
   282
}