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