jdk/test/sun/security/pkcs11/Cipher/TestRSACipher.java
author darcy
Wed, 29 Apr 2015 10:25:53 -0700
changeset 30046 cf2c86e1819e
parent 8578 f5d3509ad92b
child 35379 1e8e336ef66b
permissions -rw-r--r--
8078334: Mark regression tests using randomness Reviewed-by: xuelei, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    26
 * @bug 4898468 6994008
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary basic test for RSA cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 8578
diff changeset
    30
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class TestRSACipher extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    42
    private static final String[] RSA_ALGOS =
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    43
        { "RSA/ECB/PKCS1Padding", "RSA" };
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        try {
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    47
            Cipher.getInstance(RSA_ALGOS[0], p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            System.out.println("Not supported by provider, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        kpg.initialize(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        PublicKey publicKey = kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        PrivateKey privateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        byte[] b, e, d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        b = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        random.nextBytes(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    62
        for (String rsaAlgo: RSA_ALGOS) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    63
            Cipher c1 = Cipher.getInstance(rsaAlgo, p);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    64
            Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    66
            c1.init(Cipher.ENCRYPT_MODE, publicKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    67
            e = c1.doFinal(b);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    68
            c1.init(Cipher.DECRYPT_MODE, privateKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    69
            d = c1.doFinal(e);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    70
            match(b, d);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    71
            c2.init(Cipher.DECRYPT_MODE, privateKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    72
            d = c2.doFinal(e);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    73
            match(b, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    75
            // invalid data
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    76
            c1.init(Cipher.DECRYPT_MODE, publicKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    77
            try {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    78
                d = c1.doFinal(e);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    79
                throw new Exception("completed call");
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    80
            } catch (BadPaddingException ee) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    81
                ee.printStackTrace();
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    82
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    84
            c1.init(Cipher.ENCRYPT_MODE, privateKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    85
            e = c1.doFinal(b);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    86
            c1.init(Cipher.DECRYPT_MODE, publicKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    87
            d = c1.doFinal(e);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    88
            match(b, d);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    89
            c2.init(Cipher.DECRYPT_MODE, publicKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    90
            d = c2.doFinal(e);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    91
            match(b, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    93
            // reinit tests
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    94
            c1.init(Cipher.ENCRYPT_MODE, privateKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    95
            c1.init(Cipher.ENCRYPT_MODE, privateKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    96
            e = c1.doFinal(b);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    97
            e = c1.doFinal(b);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    98
            c1.update(b);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    99
            c1.update(b);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   100
            c1.init(Cipher.ENCRYPT_MODE, privateKey);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   101
            e = c1.doFinal();
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   102
            e = c1.doFinal();
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   103
            c1.update(b);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   104
            e = c1.doFinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   106
            c1.update(new byte[256]);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   107
            try {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   108
                e = c1.doFinal();
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   109
                throw new Exception("completed call");
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   110
            } catch (IllegalBlockSizeException ee) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   111
                System.out.println(ee);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
   112
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static void match(byte[] b1, byte[] b2) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (Arrays.equals(b1, b2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.out.println(toString(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println(toString(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new Exception("mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        main(new TestRSACipher());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}