test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEP.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
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
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    26
 * @bug 4894151 8146293
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary encryption/decryption test for OAEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 12685
diff changeset
    29
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class TestOAEP {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Provider cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static PublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        cp = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println("Testing provider " + cp.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        Provider kfp = Security.getProvider("SunRsaSign");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        kpg.initialize(768);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        privateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        publicKey = kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Cipher.getInstance("RSA/ECB/OAEPwithSHA-1andMGF1Padding");
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    62
        Cipher.getInstance("RSA/ECB/OAEPwithSHA-224andMGF1Padding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Cipher.getInstance("RSA/ECB/OAEPwithSHA-256andMGF1Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        Cipher.getInstance("RSA/ECB/OAEPwithSHA-384andMGF1Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Cipher.getInstance("RSA/ECB/OAEPwithSHA-512andMGF1Padding");
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    66
        Cipher.getInstance("RSA/ECB/OAEPwithSHA-512/224andMGF1Padding");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    67
        Cipher.getInstance("RSA/ECB/OAEPwithSHA-512/256andMGF1Padding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // basic test using MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        testEncryptDecrypt("MD5", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        testEncryptDecrypt("MD5", 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        testEncryptDecrypt("MD5", 62);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            testEncryptDecrypt("MD5", 63);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // basic test using SHA-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        testEncryptDecrypt("SHA1", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        testEncryptDecrypt("SHA1", 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        testEncryptDecrypt("SHA1", 54);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            testEncryptDecrypt("SHA1", 55);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // tests alias works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        testEncryptDecrypt("SHA-1", 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    95
        String[] HASH_ALG_224 = { "SHA-224", "SHA-512/224" };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    96
        for (String ha : HASH_ALG_224) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    97
            testEncryptDecrypt(ha, 0);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    98
            testEncryptDecrypt(ha, 16);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    99
            testEncryptDecrypt(ha, 38);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   101
                testEncryptDecrypt(ha, 39);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   102
                throw new Exception("Unexpectedly completed call");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   103
            } catch (IllegalBlockSizeException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   104
                // ok
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   105
                System.out.println(e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   106
            }
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   107
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   108
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   109
        String[] HASH_ALG_256 = { "SHA-256", "SHA-512/256" };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   110
        for (String ha : HASH_ALG_256) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   111
            testEncryptDecrypt(ha, 0);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   112
            testEncryptDecrypt(ha, 16);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   113
            testEncryptDecrypt(ha, 30);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   114
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   115
                testEncryptDecrypt(ha, 31);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   116
                throw new Exception("Unexpectedly completed call");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   117
            } catch (IllegalBlockSizeException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   118
                // ok
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   119
                System.out.println(e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   120
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // 768 bit key too short for OAEP with 64 byte digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            testEncryptDecrypt("SHA-512", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Cipher c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        byte[] enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        byte[] data = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        random.nextBytes(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            c = Cipher.getInstance("RSA/ECB/OAEPwithFOOandMGF1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        c = Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // cannot "sign" using OAEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            c.init(Cipher.ENCRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // cannot "verify" using OAEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            c.init(Cipher.DECRYPT_MODE, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // decryption failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        c.init(Cipher.DECRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            c.doFinal(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // wrong hash length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        c.init(Cipher.ENCRYPT_MODE, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        enc = c.doFinal(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        c = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        c.init(Cipher.DECRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            c.doFinal(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
/* MD2 not supported with OAEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // wrong hash value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        c = Cipher.getInstance("RSA/ECB/OAEPwithMD2andMGF1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        c.init(Cipher.DECRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            c.doFinal(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // wrong padding type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        c = Cipher.getInstance("RSA/ECB/PKCS1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        c.init(Cipher.ENCRYPT_MODE, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        enc = c.doFinal(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        c = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        c.init(Cipher.DECRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            c.doFinal(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new Exception("Unexpectedly completed call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        System.out.println("Done (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   218
    // NOTE: OAEP can process up to (modLen - 2*digestLen - 2) bytes of data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static void testEncryptDecrypt(String hashAlg, int dataLength) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg + "andMGF1Padding", cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        c.init(Cipher.ENCRYPT_MODE, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        byte[] data = new byte[dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        byte[] enc = c.doFinal(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        c.init(Cipher.DECRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        byte[] dec = c.doFinal(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (Arrays.equals(data, dec) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new Exception("Data does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}