4597N/A/*
4597N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4597N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4597N/A *
4597N/A * This code is free software; you can redistribute it and/or modify it
4597N/A * under the terms of the GNU General Public License version 2 only, as
4597N/A * published by the Free Software Foundation.
4597N/A *
4597N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4597N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4597N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4597N/A * version 2 for more details (a copy is included in the LICENSE file that
4597N/A * accompanied this code).
4597N/A *
4597N/A * You should have received a copy of the GNU General Public License version
4597N/A * 2 along with this work; if not, write to the Free Software Foundation,
4597N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4597N/A *
4597N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4597N/A * or visit www.oracle.com if you need additional information or have any
4597N/A * questions.
4597N/A */
4597N/A
4597N/A/*
4597N/A * @test
4597N/A * @bug 7088989
4597N/A * @summary Ensure the RSA ciphers and signatures works correctly
4597N/A */
4597N/Aimport java.io.*;
4597N/Aimport java.security.*;
4597N/Aimport java.security.spec.*;
4597N/Aimport java.util.*;
4597N/Aimport java.math.*;
4597N/Aimport javax.crypto.*;
4597N/A
4597N/Apublic class TestRSA extends UcryptoTest {
4597N/A
4597N/A // KAT
4597N/A private static final byte PLAINTEXT[] = Arrays.copyOf
4597N/A (new String("Known plaintext message utilized" +
4597N/A "for RSA Encryption & Decryption" +
4597N/A "block, SHA1, SHA256, SHA384 and" +
4597N/A "SHA512 RSA Signature KAT tests.").getBytes(), 128);
4597N/A
4597N/A private static final byte MOD[] = {
4597N/A (byte)0xd5, (byte)0x84, (byte)0x95, (byte)0x07, (byte)0xf4, (byte)0xd0,
4597N/A (byte)0x1f, (byte)0x82, (byte)0xf3, (byte)0x79, (byte)0xf4, (byte)0x99,
4597N/A (byte)0x48, (byte)0x10, (byte)0xe1, (byte)0x71, (byte)0xa5, (byte)0x62,
4597N/A (byte)0x22, (byte)0xa3, (byte)0x4b, (byte)0x00, (byte)0xe3, (byte)0x5b,
4597N/A (byte)0x3a, (byte)0xcc, (byte)0x10, (byte)0x83, (byte)0xe0, (byte)0xaf,
4597N/A (byte)0x61, (byte)0x13, (byte)0x54, (byte)0x6a, (byte)0xa2, (byte)0x6a,
4597N/A (byte)0x2c, (byte)0x5e, (byte)0xb3, (byte)0xcc, (byte)0xa3, (byte)0x71,
4597N/A (byte)0x9a, (byte)0xb2, (byte)0x3e, (byte)0x78, (byte)0xec, (byte)0xb5,
4597N/A (byte)0x0e, (byte)0x6e, (byte)0x31, (byte)0x3b, (byte)0x77, (byte)0x1f,
4597N/A (byte)0x6e, (byte)0x94, (byte)0x41, (byte)0x60, (byte)0xd5, (byte)0x6e,
4597N/A (byte)0xd9, (byte)0xc6, (byte)0xf9, (byte)0x29, (byte)0xc3, (byte)0x40,
4597N/A (byte)0x36, (byte)0x25, (byte)0xdb, (byte)0xea, (byte)0x0b, (byte)0x07,
4597N/A (byte)0xae, (byte)0x76, (byte)0xfd, (byte)0x99, (byte)0x29, (byte)0xf4,
4597N/A (byte)0x22, (byte)0xc1, (byte)0x1a, (byte)0x8f, (byte)0x05, (byte)0xfe,
4597N/A (byte)0x98, (byte)0x09, (byte)0x07, (byte)0x05, (byte)0xc2, (byte)0x0f,
4597N/A (byte)0x0b, (byte)0x11, (byte)0x83, (byte)0x39, (byte)0xca, (byte)0xc7,
4597N/A (byte)0x43, (byte)0x63, (byte)0xff, (byte)0x33, (byte)0x80, (byte)0xe7,
4597N/A (byte)0xc3, (byte)0x78, (byte)0xae, (byte)0xf1, (byte)0x73, (byte)0x52,
4597N/A (byte)0x98, (byte)0x1d, (byte)0xde, (byte)0x5c, (byte)0x53, (byte)0x6e,
4597N/A (byte)0x01, (byte)0x73, (byte)0x0d, (byte)0x12, (byte)0x7e, (byte)0x77,
4597N/A (byte)0x03, (byte)0xf1, (byte)0xef, (byte)0x1b, (byte)0xc8, (byte)0xa8,
4597N/A (byte)0x0f, (byte)0x97
4597N/A };
4597N/A
4597N/A private static final byte PUB_EXP[] = {(byte)0x01, (byte)0x00, (byte)0x01};
4597N/A
4597N/A private static final byte PRIV_EXP[] = {
4597N/A (byte)0x85, (byte)0x27, (byte)0x47, (byte)0x61, (byte)0x4c, (byte)0xd4,
4597N/A (byte)0xb5, (byte)0xb2, (byte)0x0e, (byte)0x70, (byte)0x91, (byte)0x8f,
4597N/A (byte)0x3d, (byte)0x97, (byte)0xf9, (byte)0x5f, (byte)0xcc, (byte)0x09,
4597N/A (byte)0x65, (byte)0x1c, (byte)0x7c, (byte)0x5b, (byte)0xb3, (byte)0x6d,
4597N/A (byte)0x63, (byte)0x3f, (byte)0x7b, (byte)0x55, (byte)0x22, (byte)0xbb,
4597N/A (byte)0x7c, (byte)0x48, (byte)0x77, (byte)0xae, (byte)0x80, (byte)0x56,
4597N/A (byte)0xc2, (byte)0x10, (byte)0xd5, (byte)0x03, (byte)0xdb, (byte)0x31,
4597N/A (byte)0xaf, (byte)0x8d, (byte)0x54, (byte)0xd4, (byte)0x48, (byte)0x99,
4597N/A (byte)0xa8, (byte)0xc4, (byte)0x23, (byte)0x43, (byte)0xb8, (byte)0x48,
4597N/A (byte)0x0b, (byte)0xc7, (byte)0xbc, (byte)0xf5, (byte)0xcc, (byte)0x64,
4597N/A (byte)0x72, (byte)0xbf, (byte)0x59, (byte)0x06, (byte)0x04, (byte)0x1c,
4597N/A (byte)0x32, (byte)0xf5, (byte)0x14, (byte)0x2e, (byte)0x6e, (byte)0xe2,
4597N/A (byte)0x0f, (byte)0x5c, (byte)0xde, (byte)0x36, (byte)0x3c, (byte)0x6e,
4597N/A (byte)0x7c, (byte)0x4d, (byte)0xcc, (byte)0xd3, (byte)0x00, (byte)0x6e,
4597N/A (byte)0xe5, (byte)0x45, (byte)0x46, (byte)0xef, (byte)0x4d, (byte)0x25,
4597N/A (byte)0x46, (byte)0x6d, (byte)0x7f, (byte)0xed, (byte)0xbb, (byte)0x4f,
4597N/A (byte)0x4d, (byte)0x9f, (byte)0xda, (byte)0x87, (byte)0x47, (byte)0x8f,
4597N/A (byte)0x74, (byte)0x44, (byte)0xb7, (byte)0xbe, (byte)0x9d, (byte)0xf5,
4597N/A (byte)0xdd, (byte)0xd2, (byte)0x4c, (byte)0xa5, (byte)0xab, (byte)0x74,
4597N/A (byte)0xe5, (byte)0x29, (byte)0xa1, (byte)0xd2, (byte)0x45, (byte)0x3b,
4597N/A (byte)0x33, (byte)0xde, (byte)0xd5, (byte)0xae, (byte)0xf7, (byte)0x03,
4597N/A (byte)0x10, (byte)0x21
4597N/A };
4597N/A
4597N/A private static final byte PRIME_P[] = {
4597N/A (byte)0xf9, (byte)0x74, (byte)0x8f, (byte)0x16, (byte)0x02, (byte)0x6b,
4597N/A (byte)0xa0, (byte)0xee, (byte)0x7f, (byte)0x28, (byte)0x97, (byte)0x91,
4597N/A (byte)0xdc, (byte)0xec, (byte)0xc0, (byte)0x7c, (byte)0x49, (byte)0xc2,
4597N/A (byte)0x85, (byte)0x76, (byte)0xee, (byte)0x66, (byte)0x74, (byte)0x2d,
4597N/A (byte)0x1a, (byte)0xb8, (byte)0xf7, (byte)0x2f, (byte)0x11, (byte)0x5b,
4597N/A (byte)0x36, (byte)0xd8, (byte)0x46, (byte)0x33, (byte)0x3b, (byte)0xd8,
4597N/A (byte)0xf3, (byte)0x2d, (byte)0xa1, (byte)0x03, (byte)0x83, (byte)0x2b,
4597N/A (byte)0xec, (byte)0x35, (byte)0x43, (byte)0x32, (byte)0xff, (byte)0xdd,
4597N/A (byte)0x81, (byte)0x7c, (byte)0xfd, (byte)0x65, (byte)0x13, (byte)0x04,
4597N/A (byte)0x7c, (byte)0xfc, (byte)0x03, (byte)0x97, (byte)0xf0, (byte)0xd5,
4597N/A (byte)0x62, (byte)0xdc, (byte)0x0d, (byte)0xbf
4597N/A };
4597N/A
4597N/A private static final byte PRIME_Q[] = {
4597N/A (byte)0xdb, (byte)0x1e, (byte)0xa7, (byte)0x3d, (byte)0xe7, (byte)0xfa,
4597N/A (byte)0x8b, (byte)0x04, (byte)0x83, (byte)0x48, (byte)0xf3, (byte)0xa5,
4597N/A (byte)0x31, (byte)0x9d, (byte)0x35, (byte)0x5e, (byte)0x4d, (byte)0x54,
4597N/A (byte)0x77, (byte)0xcc, (byte)0x84, (byte)0x09, (byte)0xf3, (byte)0x11,
4597N/A (byte)0x0d, (byte)0x54, (byte)0xed, (byte)0x85, (byte)0x39, (byte)0xa9,
4597N/A (byte)0xca, (byte)0xa8, (byte)0xea, (byte)0xae, (byte)0x19, (byte)0x9c,
4597N/A (byte)0x75, (byte)0xdb, (byte)0x88, (byte)0xb8, (byte)0x04, (byte)0x8d,
4597N/A (byte)0x54, (byte)0xc6, (byte)0xa4, (byte)0x80, (byte)0xf8, (byte)0x93,
4597N/A (byte)0xf0, (byte)0xdb, (byte)0x19, (byte)0xef, (byte)0xd7, (byte)0x87,
4597N/A (byte)0x8a, (byte)0x8f, (byte)0x5a, (byte)0x09, (byte)0x2e, (byte)0x54,
4597N/A (byte)0xf3, (byte)0x45, (byte)0x24, (byte)0x29
4597N/A };
4597N/A
4597N/A private static final byte EXP_P[] = {
4597N/A (byte)0x6a, (byte)0xd1, (byte)0x25, (byte)0x80, (byte)0x18, (byte)0x33,
4597N/A (byte)0x3c, (byte)0x2b, (byte)0x44, (byte)0x19, (byte)0xfe, (byte)0xa5,
4597N/A (byte)0x40, (byte)0x03, (byte)0xc4, (byte)0xfc, (byte)0xb3, (byte)0x9c,
4597N/A (byte)0xef, (byte)0x07, (byte)0x99, (byte)0x58, (byte)0x17, (byte)0xc1,
4597N/A (byte)0x44, (byte)0xa3, (byte)0x15, (byte)0x7d, (byte)0x7b, (byte)0x22,
4597N/A (byte)0x22, (byte)0xdf, (byte)0x03, (byte)0x58, (byte)0x66, (byte)0xf5,
4597N/A (byte)0x24, (byte)0x54, (byte)0x52, (byte)0x91, (byte)0x2d, (byte)0x76,
4597N/A (byte)0xfe, (byte)0x63, (byte)0x64, (byte)0x4e, (byte)0x0f, (byte)0x50,
4597N/A (byte)0x2b, (byte)0x65, (byte)0x79, (byte)0x1f, (byte)0xf1, (byte)0xbf,
4597N/A (byte)0xc7, (byte)0x41, (byte)0x26, (byte)0xcc, (byte)0xc6, (byte)0x1c,
4597N/A (byte)0xa9, (byte)0x83, (byte)0x6f, (byte)0x03
4597N/A };
4597N/A
4597N/A private static final byte EXP_Q[] = {
4597N/A (byte)0x12, (byte)0x84, (byte)0x1a, (byte)0x99, (byte)0xce, (byte)0x9a,
4597N/A (byte)0x8b, (byte)0x58, (byte)0xcc, (byte)0x47, (byte)0x43, (byte)0xdf,
4597N/A (byte)0x77, (byte)0xbb, (byte)0xd3, (byte)0x20, (byte)0xae, (byte)0xe4,
4597N/A (byte)0x2e, (byte)0x63, (byte)0x67, (byte)0xdc, (byte)0xf7, (byte)0x5f,
4597N/A (byte)0x3f, (byte)0x83, (byte)0x27, (byte)0xb7, (byte)0x14, (byte)0x52,
4597N/A (byte)0x56, (byte)0xbf, (byte)0xc3, (byte)0x65, (byte)0x06, (byte)0xe1,
4597N/A (byte)0x03, (byte)0xcc, (byte)0x93, (byte)0x57, (byte)0x09, (byte)0x7b,
4597N/A (byte)0x6f, (byte)0xe8, (byte)0x81, (byte)0x4a, (byte)0x2c, (byte)0xb7,
4597N/A (byte)0x43, (byte)0xa9, (byte)0x20, (byte)0x1d, (byte)0xf6, (byte)0x56,
4597N/A (byte)0x8b, (byte)0xcc, (byte)0xe5, (byte)0x4c, (byte)0xd5, (byte)0x4f,
4597N/A (byte)0x74, (byte)0x67, (byte)0x29, (byte)0x51
4597N/A };
4597N/A
4597N/A private static final byte CRT_COEFF[] = {
4597N/A (byte)0x23, (byte)0xab, (byte)0xf4, (byte)0x03, (byte)0x2f, (byte)0x29,
4597N/A (byte)0x95, (byte)0x74, (byte)0xac, (byte)0x1a, (byte)0x33, (byte)0x96,
4597N/A (byte)0x62, (byte)0xed, (byte)0xf7, (byte)0xf6, (byte)0xae, (byte)0x07,
4597N/A (byte)0x2a, (byte)0x2e, (byte)0xe8, (byte)0xab, (byte)0xfb, (byte)0x1e,
4597N/A (byte)0xb9, (byte)0xb2, (byte)0x88, (byte)0x1e, (byte)0x85, (byte)0x05,
4597N/A (byte)0x42, (byte)0x64, (byte)0x03, (byte)0xb2, (byte)0x8b, (byte)0xc1,
4597N/A (byte)0x81, (byte)0x75, (byte)0xd7, (byte)0xba, (byte)0xaa, (byte)0xd4,
4597N/A (byte)0x31, (byte)0x3c, (byte)0x8a, (byte)0x96, (byte)0x23, (byte)0x9d,
4597N/A (byte)0x3f, (byte)0x06, (byte)0x3e, (byte)0x44, (byte)0xa9, (byte)0x62,
4597N/A (byte)0x2f, (byte)0x61, (byte)0x5a, (byte)0x51, (byte)0x82, (byte)0x2c,
4597N/A (byte)0x04, (byte)0x85, (byte)0x73, (byte)0xd1
4597N/A };
4597N/A
4597N/A private static KeyPair genRSAKey(int keyLength) throws Exception {
4597N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
4597N/A kpg.initialize(keyLength);
4597N/A return kpg.generateKeyPair();
4597N/A }
4597N/A
4597N/A private static KeyPair genPredefinedRSAKeyPair() throws Exception {
4597N/A KeyFactory kf = KeyFactory.getInstance("RSA");
4597N/A BigInteger mod = new BigInteger(MOD);
4597N/A BigInteger pub = new BigInteger(PUB_EXP);
4597N/A
4597N/A PrivateKey privKey = kf.generatePrivate
4597N/A (new RSAPrivateCrtKeySpec
4597N/A (mod, pub, new BigInteger(PRIV_EXP),
4597N/A new BigInteger(PRIME_P), new BigInteger(PRIME_Q),
4597N/A new BigInteger(EXP_P), new BigInteger(EXP_Q),
4597N/A new BigInteger(CRT_COEFF)));
4597N/A PublicKey pubKey = kf.generatePublic(new RSAPublicKeySpec(mod, pub));
4597N/A return new KeyPair(pubKey, privKey);
4597N/A }
4597N/A
4597N/A private static final String CIP_ALGOS[] = {
4597N/A "RSA/ECB/NoPadding",
4597N/A "RSA/ECB/PKCS1Padding"
4597N/A };
4597N/A private static final int INPUT_SIZE_REDUCTION[] = {
4597N/A 0,
4597N/A 11,
4597N/A };
4597N/A private static final String SIG_ALGOS[] = {
4597N/A "MD5WithRSA",
4597N/A "SHA1WithRSA",
4597N/A "SHA256WithRSA",
4597N/A "SHA384WithRSA",
4597N/A "SHA512WithRSA"
4597N/A };
4597N/A
4597N/A private static KeyPair kp[] = null;
4597N/A
4597N/A public static void main(String argv[]) throws Exception {
4597N/A main(new TestRSA(), null);
4597N/A }
4597N/A
4597N/A public void doTest(Provider prov) throws Exception {
4597N/A // first test w/ predefine KeyPair
4597N/A KeyPair pkp = genPredefinedRSAKeyPair();
4597N/A System.out.println("Test against Predefined RSA Key Pair");
4597N/A testCipher(pkp, 128, true, prov);
4597N/A testSignature(pkp, true, prov);
4597N/A
4597N/A for (int i = 0; i < 10; i++) {
4597N/A // then test w/ various key lengths
4597N/A int keyLens[] = { 1024, 2048 };
4597N/A kp = new KeyPair[keyLens.length];
4597N/A
4597N/A testCipher(keyLens, false, prov);
4597N/A testSignature(keyLens, false, prov);
4597N/A }
4597N/A }
4597N/A
4597N/A
4597N/A private static void testCipher(KeyPair kp, int inputSizeInBytes,
4597N/A boolean checkInterop, Provider prov)
4597N/A throws Exception {
4597N/A Cipher c1, c2;
4597N/A for (int i = 0; i < CIP_ALGOS.length; i++) {
4597N/A String algo = CIP_ALGOS[i];
4597N/A try {
4597N/A c1 = Cipher.getInstance(algo, prov);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skip unsupported Cipher algo: " + algo);
4597N/A continue;
4597N/A }
4597N/A
4597N/A if (checkInterop) {
4597N/A c2 = Cipher.getInstance(algo, "SunJCE");
4597N/A } else {
4597N/A c2 = Cipher.getInstance(algo, prov);
4597N/A }
4597N/A byte[] data = Arrays.copyOf
4597N/A (PLAINTEXT, inputSizeInBytes - INPUT_SIZE_REDUCTION[i]);
4597N/A
4597N/A testEncryption(c1, c2, kp, data);
4597N/A }
4597N/A }
4597N/A
4597N/A private static void testCipher(int keyLens[], boolean checkInterop,
4597N/A Provider prov)
4597N/A throws Exception {
4597N/A // RSA CipherText will always differ due to the random nonce in padding
4597N/A // so we check whether both
4597N/A // 1) Java Encrypt/C Decrypt
4597N/A // 2) C Encrypt/Java Decrypt
4597N/A // works
4597N/A Cipher c1, c2;
4597N/A for (int i = 0; i < CIP_ALGOS.length; i++) {
4597N/A String algo = CIP_ALGOS[i];
4597N/A try {
4597N/A c1 = Cipher.getInstance(algo, prov);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skip unsupported Cipher algo: " + algo);
4597N/A continue;
4597N/A }
4597N/A
4597N/A if (checkInterop) {
4597N/A c2 = Cipher.getInstance(algo, "SunJCE");
4597N/A } else {
4597N/A c2 = Cipher.getInstance(algo, prov);
4597N/A }
4597N/A
4597N/A for (int h = 0; h < keyLens.length; h++) {
4597N/A // Defer key pair generation until now when it'll soon be used.
4597N/A if (kp[h] == null) {
4597N/A kp[h] = genRSAKey(keyLens[h]);
4597N/A }
4597N/A System.out.println("\tTesting Cipher " + algo + " w/ KeySize " + keyLens[h]);
4597N/A byte[] data = Arrays.copyOf
4597N/A (PLAINTEXT, keyLens[h]/8 - INPUT_SIZE_REDUCTION[i]);
4597N/A testEncryption(c1, c2, kp[h], data);
4597N/A }
4597N/A }
4597N/A }
4597N/A
4597N/A private static void testEncryption(Cipher c1, Cipher c2, KeyPair kp, byte[] data)
4597N/A throws Exception {
4597N/A // C1 Encrypt + C2 Decrypt
4597N/A byte[] out1 = null;
4597N/A byte[] recoveredText = null;
4597N/A try {
4597N/A c1.init(Cipher.ENCRYPT_MODE, kp.getPublic());
4597N/A out1 = c1.doFinal(data);
4597N/A c2.init(Cipher.DECRYPT_MODE, kp.getPrivate());
4597N/A recoveredText = c2.doFinal(out1);
4597N/A } catch (Exception ex) {
4597N/A System.out.println("\tDEC ERROR: unexpected exception");
4597N/A ex.printStackTrace();
4597N/A throw ex;
4597N/A }
4597N/A if(!Arrays.equals(recoveredText, data)) {
4597N/A throw new RuntimeException("\tDEC ERROR: different PT bytes!");
4597N/A }
4597N/A // C2 Encrypt + C1 Decrypt
4597N/A byte[] cipherText = null;
4597N/A try {
4597N/A c2.init(Cipher.ENCRYPT_MODE, kp.getPublic());
4597N/A cipherText = c2.doFinal(data);
4597N/A c1.init(Cipher.DECRYPT_MODE, kp.getPrivate());
4597N/A try {
4597N/A out1 = c1.doFinal(cipherText);
4597N/A } catch (Exception ex) {
4597N/A System.out.println("\tENC ERROR: invalid encrypted output");
4597N/A ex.printStackTrace();
4597N/A throw ex;
4597N/A }
4597N/A } catch (Exception ex) {
4597N/A System.out.println("\tENC ERROR: unexpected exception");
4597N/A ex.printStackTrace();
4597N/A throw ex;
4597N/A }
4597N/A if (!Arrays.equals(out1, data)) {
4597N/A throw new RuntimeException("\tENC ERROR: Decrypted result DIFF!");
4597N/A }
4597N/A System.out.println("\t=> PASS");
4597N/A }
4597N/A
4597N/A private static void testSignature(KeyPair kp, boolean checkInterop,
4597N/A Provider prov) throws Exception {
4597N/A byte[] data = PLAINTEXT;
4597N/A Signature sig1, sig2;
4597N/A for (int i = 0; i < SIG_ALGOS.length; i++) {
4597N/A String algo = SIG_ALGOS[i];
4597N/A try {
4597N/A sig1 = Signature.getInstance(algo, prov);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skip unsupported Signature algo: " + algo);
4597N/A continue;
4597N/A }
4597N/A
4597N/A if (checkInterop) {
4597N/A sig2 = Signature.getInstance(algo, "SunRsaSign");
4597N/A } else {
4597N/A sig2 = Signature.getInstance(algo, prov);
4597N/A }
4597N/A testSigning(sig1, sig2, kp, data);
4597N/A }
4597N/A }
4597N/A
4597N/A private static void testSignature(int keyLens[], boolean checkInterop,
4597N/A Provider prov) throws Exception {
4597N/A byte[] data = PLAINTEXT;
4597N/A Signature sig1, sig2;
4597N/A for (int i = 0; i < SIG_ALGOS.length; i++) {
4597N/A String algo = SIG_ALGOS[i];
4597N/A try {
4597N/A sig1 = Signature.getInstance(algo, prov);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skip unsupported Signature algo: " + algo);
4597N/A continue;
4597N/A }
4597N/A
4597N/A if (checkInterop) {
4597N/A sig2 = Signature.getInstance(algo, "SunRsaSign");
4597N/A } else {
4597N/A sig2 = Signature.getInstance(algo, prov);
4597N/A }
4597N/A
4597N/A for (int h = 0; h < keyLens.length; h++) {
4597N/A // Defer key pair generation until now when it'll soon be used.
4597N/A if (kp[h] == null) {
4597N/A kp[h] = genRSAKey(keyLens[h]);
4597N/A }
4597N/A System.out.println("\tTesting Signature " + algo + " w/ KeySize " + keyLens[h]);
4597N/A
4597N/A testSigning(sig1, sig2, kp[h], data);
4597N/A }
4597N/A }
4597N/A }
4597N/A
4597N/A private static void testSigning(Signature sig1, Signature sig2, KeyPair kp, byte[] data)
4597N/A throws Exception {
4597N/A boolean sameSig = false;
4597N/A byte[] out = null;
4597N/A try {
4597N/A sig1.initSign(kp.getPrivate());
4597N/A sig1.update(data);
4597N/A out = sig1.sign();
4597N/A } catch (Exception ex) {
4597N/A System.out.println("\tSIGN ERROR: unexpected exception!");
4597N/A ex.printStackTrace();
4597N/A }
4597N/A
4597N/A sig2.initSign(kp.getPrivate());
4597N/A sig2.update(data);
4597N/A byte[] out2 = sig2.sign();
4597N/A if (!Arrays.equals(out2, out)) {
4597N/A throw new RuntimeException("\tSIGN ERROR: Signature DIFF!");
4597N/A }
4597N/A
4597N/A boolean verify = false;
4597N/A try {
4597N/A System.out.println("\tVERIFY1 using native out");
4597N/A sig1.initVerify(kp.getPublic());
4597N/A sig1.update(data);
4597N/A verify = sig1.verify(out);
4597N/A if (!verify) {
4597N/A throw new RuntimeException("VERIFY1 FAIL!");
4597N/A }
4597N/A } catch (Exception ex) {
4597N/A System.out.println("\tVERIFY1 ERROR: unexpected exception!");
4597N/A ex.printStackTrace();
4597N/A throw ex;
4597N/A }
4597N/A System.out.println("\t=> PASS");
4597N/A }
4597N/A}