test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPWithParams.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56592 b1902b22005e
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4923484
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary encryption/decryption test for using OAEPParameterSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.MGF1ParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.PSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.OAEPParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class TestOAEPWithParams {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static Provider cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static PublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static String MD[] = {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    50
        "MD5", "SHA1", "SHA-224", "SHA-256", "SHA-512/224", "SHA-512/256"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static int DATA_LENGTH[] = {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    53
        62, 54, 34, 30, 34, 30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        cp = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Testing provider " + cp.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Provider kfp = Security.getProvider("SunRsaSign");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        kpg.initialize(768);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        privateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        publicKey = kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (int i = 0; i < MD.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // basic test using MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            testEncryptDecrypt(MD[i], DATA_LENGTH[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("Done (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static void testEncryptDecrypt(String hashAlg, int dataLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                      "andMGF1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        byte[] pSrc1 = { (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                         (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        byte[] pSrc2 = { (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                         (byte) 0x02, (byte) 0x02, (byte) 0x03, (byte) 0x04
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        OAEPParameterSpec spec1 = new OAEPParameterSpec(hashAlg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            "MGF1", MGF1ParameterSpec.SHA1, new PSource.PSpecified(pSrc1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        OAEPParameterSpec spec2 = new OAEPParameterSpec(hashAlg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            "MGF1", MGF1ParameterSpec.SHA1, new PSource.PSpecified(pSrc2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        byte[] plainText = new byte[dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        byte[] cipherText, recovered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // do encryption with parameters#1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.out.println("Testing with user-supplied parameters...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        c.init(Cipher.ENCRYPT_MODE, publicKey, spec1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        cipherText = c.doFinal(plainText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // test#1: decrypt with parameters#1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        c.init(Cipher.DECRYPT_MODE, privateKey, spec1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        recovered = c.doFinal(cipherText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (Arrays.equals(plainText, recovered) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new Exception("Decrypted data does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // test#2: decrypt without parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        c.init(Cipher.DECRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            recovered = c.doFinal(cipherText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new Exception("Should throw BadPaddingException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (BadPaddingException bpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // test#3: decrypt with different parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        c.init(Cipher.DECRYPT_MODE, privateKey, spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            recovered = c.doFinal(cipherText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new Exception("Should throw BadPaddingException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (BadPaddingException bpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // do encryption without parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.println("Testing with cipher default parameters...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        c.init(Cipher.ENCRYPT_MODE, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        cipherText = c.doFinal(plainText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // test#1: decrypt with parameters got from cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        AlgorithmParameters params = c.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        c.init(Cipher.DECRYPT_MODE, privateKey, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        recovered = c.doFinal(cipherText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (Arrays.equals(plainText, recovered) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new Exception("Decrypted data does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // test#2: decrypt without parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        c.init(Cipher.DECRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        recovered = c.doFinal(cipherText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (Arrays.equals(plainText, recovered) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new Exception("Decrypted data does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // test#3: decrypt with different parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        c.init(Cipher.DECRYPT_MODE, privateKey, spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            recovered = c.doFinal(cipherText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new Exception("Should throw BadPaddingException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } catch (BadPaddingException bpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}