test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58489 2faeaa5933a6
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     1
/*
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     4
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     8
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    13
 * accompanied this code).
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    14
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    18
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    21
 * questions.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    22
 */
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    23
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    24
/*
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    25
 * @test
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
    26
 * @bug 8080462 8229243
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    27
 * @library /test/lib ..
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    28
 * @modules jdk.crypto.cryptoki
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    29
 * @run main TestGCMKeyAndIvCheck
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    30
 * @summary Ensure that same key+iv can't be repeated used for encryption.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    31
 */
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    32
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    33
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    34
import java.security.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    35
import java.security.spec.AlgorithmParameterSpec;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    36
import javax.crypto.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    37
import javax.crypto.spec.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    38
import java.math.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    39
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    40
import java.util.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    41
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    42
public class TestGCMKeyAndIvCheck extends PKCS11Test {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    43
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    44
    private static final byte[] AAD = new byte[5];
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    45
    private static final byte[] PT = new byte[18];
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    46
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    48
        main(new TestGCMKeyAndIvCheck(), args);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    49
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    50
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    51
    private static void checkISE(Cipher c) throws Exception {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    52
        // Subsequent encryptions should fail
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    53
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    54
            c.updateAAD(AAD);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    55
            throw new Exception("Should throw ISE for updateAAD()");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    56
        } catch (IllegalStateException ise) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    57
            // expected
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    58
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    59
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    60
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    61
            c.update(PT);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    62
            throw new Exception("Should throw ISE for update()");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    63
        } catch (IllegalStateException ise) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    64
            // expected
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    65
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    66
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    67
            c.doFinal(PT);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    68
            throw new Exception("Should throw ISE for doFinal()");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    69
        } catch (IllegalStateException ise) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    70
            // expected
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    71
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    72
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    73
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    74
    public void test(String mode, Provider p) throws Exception {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    75
        Cipher c;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    76
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    77
            String transformation = "AES/" + mode + "/NoPadding";
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    78
            c = Cipher.getInstance(transformation, p);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    79
        } catch (GeneralSecurityException e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    80
            System.out.println("Skip testing " + p.getName() +
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    81
                    ", no support for " + mode);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    82
            return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    83
        }
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
    84
        System.out.println("Testing against " + p.getName());
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    85
        SecretKey key = new SecretKeySpec(new byte[16], "AES");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    86
        // First try parameter-less init.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    87
        c.init(Cipher.ENCRYPT_MODE, key);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    88
        c.updateAAD(AAD);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    89
        byte[] ctPlusTag = c.doFinal(PT);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    90
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    91
        // subsequent encryption should fail unless re-init w/ different key+iv
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    92
        checkISE(c);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    93
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    94
        // Validate the retrieved parameters against the IV and tag length.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    95
        AlgorithmParameters params = c.getParameters();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    96
        if (params == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    97
            throw new Exception("getParameters() should not return null");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    98
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    99
        byte[] iv = null;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   100
        int tagLength = 0; // in bits
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   101
        if (mode.equalsIgnoreCase("GCM")) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   102
            GCMParameterSpec spec = params.getParameterSpec(GCMParameterSpec.class);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   103
            tagLength = spec.getTLen();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   104
            iv = spec.getIV();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   105
        } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   106
            throw new RuntimeException("Error: Unsupported mode: " + mode);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   107
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   108
        if (tagLength != (ctPlusTag.length - PT.length)*8) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   109
            throw new Exception("Parameters contains incorrect TLen value");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   110
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   111
        if (!Arrays.equals(iv, c.getIV())) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   112
            throw new Exception("Parameters contains incorrect IV value");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   113
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   114
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   115
        c.init(Cipher.DECRYPT_MODE, key, params);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   116
        c.updateAAD(AAD);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   117
        byte[] recovered = c.doFinal(ctPlusTag);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   118
        if (!Arrays.equals(recovered, PT)) {
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
   119
            throw new Exception("Decryption result mismatch");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   120
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   121
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   122
        // Now try to encrypt again using the same key+iv; should fail also
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   123
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   124
            c.init(Cipher.ENCRYPT_MODE, key, params);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   125
            throw new Exception("Should throw exception when same key+iv is used");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   126
        } catch (InvalidAlgorithmParameterException iape) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   127
            // expected
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
   128
            System.out.println("Expected IAPE thrown");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   129
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   130
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   131
        // Now try to encrypt again using parameter-less init; should work
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   132
        c.init(Cipher.ENCRYPT_MODE, key);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   133
        c.doFinal(PT);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   134
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   135
        // make sure a different iv is used
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   136
        byte[] ivNew = c.getIV();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   137
        if (Arrays.equals(iv, ivNew)) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   138
            throw new Exception("IV should be different now");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   139
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   140
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   141
        // Now try to encrypt again using a different parameter; should work
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
   142
        AlgorithmParameterSpec spec2 = new GCMParameterSpec(128,
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
   143
            "Solaris PKCS11 lib does not allow all-zero IV".getBytes());
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   144
        c.init(Cipher.ENCRYPT_MODE, key, spec2);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   145
        c.updateAAD(AAD);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   146
        c.doFinal(PT);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   147
        // subsequent encryption should fail unless re-init w/ different key+iv
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   148
        checkISE(c);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   149
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   150
        // Now try decryption twice in a row; no re-init required and
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   151
        // same parameters is used.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   152
        c.init(Cipher.DECRYPT_MODE, key, params);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   153
        c.updateAAD(AAD);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   154
        recovered = c.doFinal(ctPlusTag);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   155
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   156
        c.updateAAD(AAD);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   157
        recovered = c.doFinal(ctPlusTag);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   158
        if (!Arrays.equals(recovered, PT)) {
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 55332
diff changeset
   159
            throw new Exception("Decryption result mismatch");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   160
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   161
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   162
        // Now try decryption again and re-init using the same parameters
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   163
        c.init(Cipher.DECRYPT_MODE, key, params);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   164
        c.updateAAD(AAD);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   165
        recovered = c.doFinal(ctPlusTag);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   166
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   167
        // init to decrypt w/o parameters; should fail with IKE as
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   168
        // javadoc specified
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   169
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   170
            c.init(Cipher.DECRYPT_MODE, key);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   171
            throw new Exception("Should throw IKE for dec w/o params");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   172
        } catch (InvalidKeyException ike) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   173
            // expected
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   174
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   175
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   176
        // Lastly, try encryption AND decryption w/ wrong type of parameters,
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   177
        // e.g. IvParameterSpec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   178
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   179
            c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   180
            throw new Exception("Should throw IAPE");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   181
        } catch (InvalidAlgorithmParameterException iape) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   182
            // expected
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   183
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   184
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   185
            c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   186
            throw new Exception("Should throw IAPE");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   187
        } catch (InvalidAlgorithmParameterException iape) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   188
            // expected
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   189
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   190
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   191
        System.out.println("Test Passed!");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   192
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   193
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   194
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   195
    public void main(Provider p) throws Exception {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   196
        test("GCM", p);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   197
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   198
}
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   199