test/jdk/com/oracle/security/ucrypto/TestAES.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45891 jdk/test/com/oracle/security/ucrypto/TestAES.java@55e3ff5a7b08
child 51637 e9177e7749e7
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:
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     1
/*
43698
e3e3ae809ab4 8173708: Re-enable AES cipher with CFB128 mode for Ucrypto provider
valeriep
parents: 41550
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     4
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     8
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    13
 * accompanied this code).
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    14
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    18
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    21
 * questions.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    22
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    23
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    24
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    25
 * @test
43698
e3e3ae809ab4 8173708: Re-enable AES cipher with CFB128 mode for Ucrypto provider
valeriep
parents: 41550
diff changeset
    26
 * @bug 7088989 8014374 8167512 8173708
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    27
 * @summary Ensure the AES ciphers of OracleUcrypto provider works correctly
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 27182
diff changeset
    28
 * @key randomness
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    29
 * @library /test/lib
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    30
 * @build jdk.test.lib.Platform
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    31
 *        jdk.test.lib.Utils
41550
563c1d911dcb 8165712: Grant permission to read specific properties instead of all to the jdk.crypto.ucrypto module
mullan
parents: 30046
diff changeset
    32
 * @run main TestAES
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    33
 * @run main/othervm -Dpolicy=empty.policy TestAES
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    34
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    35
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    36
import java.security.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    37
import java.security.spec.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    38
import java.util.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    39
import javax.crypto.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    40
import javax.crypto.spec.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    41
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    42
import jdk.test.lib.Platform;
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    43
import jdk.test.lib.Utils;
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    44
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    45
public class TestAES extends UcryptoTest {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    46
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    47
    private static final String[] PADDEDCIPHER_ALGOS = {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    48
        "AES/ECB/PKCS5Padding",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    49
        "AES/CBC/PKCS5Padding",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    50
        "AES/CFB128/PKCS5Padding"
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    51
    };
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    52
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    53
    private static final String[] CIPHER_ALGOS = {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    54
        "AES/ECB/NoPadding",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    55
        "AES/CBC/NoPadding",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    56
        "AES/CFB128/NoPadding",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    57
        "AES/CTR/NoPadding",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    58
    };
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    59
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    60
    private static final SecretKey CIPHER_KEY =
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    61
        new SecretKeySpec(new byte[16], "AES");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    62
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    63
    private static final boolean IS_BAD_SOLARIS = isBadSolaris();
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    64
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    66
        // It has to determine Solaris version before enabling security manager.
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    67
        // Because that needs some permissions, like java.io.FilePermission.
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    68
        String policy = System.getProperty("policy");
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    69
        if (policy != null) {
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    70
            enableSecurityManager(policy);
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    71
        }
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    72
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    73
        main(new TestAES(), null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    74
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    75
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    76
    // Enables security manager and uses the specified policy file to override
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    77
    // the default one.
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    78
    private static void enableSecurityManager(String policy) {
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    79
        String policyURL = "file://" + System.getProperty("test.src", ".") + "/"
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    80
                + policy;
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    81
        System.out.println("policyURL: " + policyURL);
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    82
        Security.setProperty("policy.url.1", policyURL);
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    83
        System.setSecurityManager(new SecurityManager());
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    84
    }
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
    85
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    86
    public void doTest(Provider prov) throws Exception {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    87
        // Provider for testing Interoperability
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    88
        Provider sunJCEProv = Security.getProvider("SunJCE");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    89
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    90
        testCipherInterop(CIPHER_ALGOS, CIPHER_KEY, prov, sunJCEProv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    91
        testCipherInterop(PADDEDCIPHER_ALGOS, CIPHER_KEY, prov, sunJCEProv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    92
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    93
        testCipherOffset(CIPHER_ALGOS, CIPHER_KEY, prov);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    94
        testCipherOffset(PADDEDCIPHER_ALGOS, CIPHER_KEY, prov);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    95
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    96
        testCipherKeyWrapping(PADDEDCIPHER_ALGOS, CIPHER_KEY, prov, sunJCEProv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    97
        testCipherGCM(CIPHER_KEY, prov);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    98
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    99
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   100
    private static void testCipherInterop(String[] algos, SecretKey key,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   101
                                          Provider p,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   102
                                          Provider interopP) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   103
        boolean testPassed = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   104
        byte[] in = new byte[32];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   105
        (new SecureRandom()).nextBytes(in);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   106
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   107
        for (String algo : algos) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   108
            try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   109
                // check ENC
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   110
                Cipher c;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   111
                try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   112
                    c = Cipher.getInstance(algo, p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   113
                } catch (NoSuchAlgorithmException nsae) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   114
                    System.out.println("Skipping Unsupported CIP algo: " + algo);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   115
                    continue;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   116
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   117
                c.init(Cipher.ENCRYPT_MODE, key, (AlgorithmParameters)null, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   118
                byte[] eout = c.doFinal(in, 0, in.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   119
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   120
                AlgorithmParameters params = c.getParameters();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   121
                Cipher c2 = Cipher.getInstance(algo, interopP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   122
                c2.init(Cipher.ENCRYPT_MODE, key, params, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   123
                byte[] eout2 = c2.doFinal(in, 0, in.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   124
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   125
                if (!Arrays.equals(eout, eout2)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   126
                    System.out.println(algo + ": DIFF FAILED");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   127
                    testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   128
                } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   129
                    System.out.println(algo + ": ENC Passed");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   130
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   131
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   132
                // check DEC
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   133
                c.init(Cipher.DECRYPT_MODE, key, params, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   134
                byte[] dout = c.doFinal(eout);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   135
                c2.init(Cipher.DECRYPT_MODE, key, params, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   136
                byte[] dout2 = c2.doFinal(eout2);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   137
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   138
                if (!Arrays.equals(dout, dout2)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   139
                    System.out.println(algo + ": DIFF FAILED");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   140
                    testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   141
                } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   142
                    System.out.println(algo + ": DEC Passed");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   143
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   144
            } catch(Exception ex) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   145
                System.out.println("Unexpected Exception: " + algo);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   146
                ex.printStackTrace();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   147
                testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   148
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   149
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   150
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   151
        if (!testPassed) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   152
            throw new RuntimeException("One or more CIPHER test failed!");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   153
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   154
            System.out.println("CIPHER Interop Tests Passed");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   155
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   156
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   157
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   158
    private static void testCipherOffset(String[] algos, SecretKey key,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   159
                                         Provider p) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   160
        boolean testPassed = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   161
        byte[] in = new byte[16];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   162
        (new SecureRandom()).nextBytes(in);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   163
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   164
        for (int i = 0; i < algos.length; i++) {
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   165
            if (IS_BAD_SOLARIS
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   166
                    && algos[i].indexOf("CFB128") != -1
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   167
                    && p.getName().equals("OracleUcrypto")) {
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   168
                System.out.println("Ignore cases on CFB128 due to a pre-S11.3 bug");
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   169
                continue;
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   170
            }
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   171
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   172
            for (int j = 1; j < (in.length - 1); j++) {
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   173
                System.out.println("Input offset size: " + j);
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   174
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   175
                try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   176
                    // check ENC
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   177
                    Cipher c;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   178
                    try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   179
                        c = Cipher.getInstance(algos[i], p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   180
                    } catch (NoSuchAlgorithmException nsae) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   181
                        System.out.println("Skip Unsupported CIP algo: " + algos[i]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   182
                        continue;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   183
                    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   184
                    c.init(Cipher.ENCRYPT_MODE, key, (AlgorithmParameters)null, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   185
                    byte[] eout = new byte[c.getOutputSize(in.length)];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   186
                    int firstPartLen = in.length - j - 1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   187
                    //System.out.print("1st UPDATE: " + firstPartLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   188
                    int k = c.update(in, 0, firstPartLen, eout, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   189
                    k += c.update(in, firstPartLen, 1, eout, k);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   190
                    k += c.doFinal(in, firstPartLen+1, j, eout, k);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   191
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   192
                    AlgorithmParameters params = c.getParameters();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   193
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   194
                    Cipher c2 = Cipher.getInstance(algos[i], p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   195
                    c2.init(Cipher.ENCRYPT_MODE, key, params, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   196
                    byte[] eout2 = new byte[c2.getOutputSize(in.length)];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   197
                    int k2 = c2.update(in, 0, j, eout2, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   198
                    k2 += c2.update(in, j, 1, eout2, k2);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   199
                    k2 += c2.doFinal(in, j+1, firstPartLen, eout2, k2);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   200
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   201
                    if (!checkArrays(eout, k, eout2, k2)) testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   202
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   203
                    // check DEC
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   204
                    c.init(Cipher.DECRYPT_MODE, key, params, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   205
                    byte[] dout = new byte[c.getOutputSize(eout.length)];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   206
                    k = c.update(eout, 0, firstPartLen, dout, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   207
                    k += c.update(eout, firstPartLen, 1, dout, k);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   208
                    k += c.doFinal(eout, firstPartLen+1, eout.length - firstPartLen - 1, dout, k);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   209
                    if (!checkArrays(in, in.length, dout, k)) testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   210
                } catch(Exception ex) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   211
                    System.out.println("Unexpected Exception: " + algos[i]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   212
                    ex.printStackTrace();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   213
                    testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   214
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   215
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   216
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   217
        if (!testPassed) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   218
            throw new RuntimeException("One or more CIPHER test failed!");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   219
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   220
            System.out.println("CIPHER Offset Tests Passed");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   221
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   222
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   223
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   224
    private static void testCipherKeyWrapping(String[] algos, SecretKey key,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   225
                                              Provider p, Provider interopP)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   226
        throws NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   227
        boolean testPassed = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   228
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   229
        // Test SecretKey, PrivateKey and PublicKey
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   230
        Key[] tbwKeys = new Key[3];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   231
        int[] tbwKeyTypes = { Cipher.SECRET_KEY, Cipher.PRIVATE_KEY, Cipher.PUBLIC_KEY };
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   232
        tbwKeys[0] = new SecretKeySpec(new byte[20], "Blowfish");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   233
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   234
        kpg.initialize(1024);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   235
        KeyPair kp = kpg.generateKeyPair();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   236
        tbwKeys[1] = kp.getPrivate();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   237
        tbwKeys[2] = kp.getPublic();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   238
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   239
        for (int i = 0; i < algos.length; i++) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   240
            try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   241
                System.out.println(algos[i] + " - Native WRAP/Java UNWRAP");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   242
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   243
                Cipher c1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   244
                try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   245
                    c1 = Cipher.getInstance(algos[i], p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   246
                } catch (NoSuchAlgorithmException nsae) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   247
                    System.out.println("Skipping Unsupported CIP algo: " + algos[i]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   248
                    continue;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   249
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   250
                c1.init(Cipher.WRAP_MODE, key, (AlgorithmParameters)null, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   251
                AlgorithmParameters params = c1.getParameters();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   252
                Cipher c2 = Cipher.getInstance(algos[i], interopP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   253
                c2.init(Cipher.UNWRAP_MODE, key, params, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   254
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   255
                for (int j = 0; j < tbwKeys.length ; j++) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   256
                    byte[] wrappedKey = c1.wrap(tbwKeys[j]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   257
                    Key recovered = c2.unwrap(wrappedKey,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   258
                                              tbwKeys[j].getAlgorithm(), tbwKeyTypes[j]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   259
                    if (!checkKeys(tbwKeys[j], recovered)) testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   260
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   261
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   262
                System.out.println(algos[i] + " - Java WRAP/Native UNWRAP");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   263
                c1 = Cipher.getInstance(algos[i], interopP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   264
                c1.init(Cipher.WRAP_MODE, key, (AlgorithmParameters)null, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   265
                params = c1.getParameters();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   266
                c2 = Cipher.getInstance(algos[i], p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   267
                c2.init(Cipher.UNWRAP_MODE, key, params, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   268
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   269
                for (int j = 0; j < tbwKeys.length ; j++) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   270
                    byte[] wrappedKey = c1.wrap(tbwKeys[j]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   271
                    Key recovered = c2.unwrap(wrappedKey,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   272
                                              tbwKeys[j].getAlgorithm(), tbwKeyTypes[j]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   273
                    if (!checkKeys(tbwKeys[j], recovered)) testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   274
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   275
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   276
            } catch(Exception ex) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   277
                System.out.println("Unexpected Exception: " + algos[i]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   278
                ex.printStackTrace();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   279
                testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   280
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   281
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   282
        if (!testPassed) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   283
            throw new RuntimeException("One or more CIPHER test failed!");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   284
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   285
            System.out.println("CIPHER KeyWrapping Tests Passed");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   286
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   287
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   288
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   289
    private static void testCipherGCM(SecretKey key,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   290
                                      Provider p) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   291
        boolean testPassed = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   292
        byte[] in = new byte[16];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   293
        (new SecureRandom()).nextBytes(in);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   294
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   295
        byte[] iv = new byte[16];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   296
        (new SecureRandom()).nextBytes(iv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   297
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   298
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   299
        String algo = "AES/GCM/NoPadding";
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   300
        int tagLen[] = { 128, 120, 112, 104, 96, 64, 32 };
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   301
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   302
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   303
            Cipher c;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   304
            try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   305
                c = Cipher.getInstance(algo, p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   306
            } catch (NoSuchAlgorithmException nsae) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   307
                System.out.println("Skipping Unsupported CIP algo: " + algo);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   308
                return;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   309
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   310
            for (int i = 0; i < tagLen.length; i++) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   311
                // change iv value to pass the key+iv uniqueness cehck for
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   312
                // GCM encryption
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   313
                iv[0] += 1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   314
                AlgorithmParameterSpec paramSpec = new GCMParameterSpec(tagLen[i], iv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   315
                // check ENC
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   316
                c.init(Cipher.ENCRYPT_MODE, key, paramSpec, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   317
                c.updateAAD(iv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   318
                byte[] eout = c.doFinal(in, 0, in.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   319
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   320
                AlgorithmParameters param = c.getParameters();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   321
                // check DEC
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   322
                c.init(Cipher.DECRYPT_MODE, key, param, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   323
                c.updateAAD(iv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   324
                byte[] dout = c.doFinal(eout, 0, eout.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   325
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   326
                if (!Arrays.equals(dout, in)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   327
                    System.out.println(algo + ": PT and RT DIFF FAILED");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   328
                    testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   329
                } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   330
                    System.out.println(algo + ": tagLen " + tagLen[i] + " done");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   331
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   332
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   333
        } catch(Exception ex) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   334
            System.out.println("Unexpected Exception: " + algo);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   335
            ex.printStackTrace();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   336
            testPassed = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   337
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   338
        if (!testPassed) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   339
            throw new RuntimeException("One or more CIPHER test failed!");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   340
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   341
            System.out.println("CIPHER GCM Tests Passed");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   342
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   343
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   344
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   345
    private static boolean checkArrays(byte[] a1, int a1Len, byte[] a2, int a2Len) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   346
        boolean equal = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   347
        if (a1Len != a2Len) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   348
            System.out.println("DIFFERENT OUT LENGTH");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   349
            equal = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   350
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   351
            for (int p = 0; p < a1Len; p++) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   352
                if (a1[p] != a2[p]) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   353
                    System.out.println("DIFF FAILED");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   354
                    equal = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   355
                    break;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   356
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   357
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   358
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   359
        return equal;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   360
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   361
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   362
    private static boolean checkKeys(Key k1, Key k2) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   363
        boolean equal = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   364
        if (!k1.getAlgorithm().equalsIgnoreCase(k2.getAlgorithm())) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   365
            System.out.println("DIFFERENT Key Algorithm");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   366
            equal = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   367
        } else if (!k1.getFormat().equalsIgnoreCase(k2.getFormat())) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   368
            System.out.println("DIFFERENT Key Format");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   369
            equal = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   370
        } else if (!Arrays.equals(k1.getEncoded(), k2.getEncoded())) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   371
            System.out.println("DIFFERENT Key Encoding");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   372
            equal = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   373
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   374
        return equal;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   375
    }
45891
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   376
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   377
    // The cases on CFB128 mode have to be skipped on pre-S11.3.
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   378
    private static boolean isBadSolaris() {
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   379
        return Platform.isSolaris()
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   380
                && Platform.getOsVersionMajor() <= 5
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   381
                && Platform.getOsVersionMinor() <= 11
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   382
                && Utils.distro().compareTo("11.3") < 0;
55e3ff5a7b08 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 43698
diff changeset
   383
    }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   384
}