J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4923484 |
| 27 | * @summary test ASN.1 encoding generation/parsing for the OAEPParameters |
| 28 | * implementation in SunJCE provider. |
| 29 | * @author Valerie Peng |
| 30 | */ |
| 31 | import java.math.BigInteger; |
| 32 | import java.util.*; |
| 33 | import java.security.*; |
| 34 | import java.security.spec.MGF1ParameterSpec; |
| 35 | import javax.crypto.*; |
| 36 | import javax.crypto.spec.OAEPParameterSpec; |
| 37 | import javax.crypto.spec.PSource; |
| 38 | |
| 39 | public class TestOAEPParameterSpec { |
| 40 | |
| 41 | private static Provider cp; |
| 42 | |
| 43 | private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec, |
| 44 | byte[] p) throws Exception { |
| 45 | OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1", |
| 46 | mgfSpec, new PSource.PSpecified(p)); |
| 47 | cp = Security.getProvider("SunJCE"); |
| 48 | System.out.println("Testing provider " + cp.getName() + "..."); |
| 49 | AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp); |
| 50 | |
| 51 | ap.init(spec); |
| 52 | byte[] encoding = ap.getEncoded(); |
| 53 | |
| 54 | AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp); |
| 55 | ap2.init(encoding); |
| 56 | |
| 57 | OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec |
| 58 | (OAEPParameterSpec.class); |
| 59 | return compareSpec(spec, spec2); |
| 60 | } |
| 61 | |
| 62 | private static boolean compareMD(OAEPParameterSpec s1, |
| 63 | OAEPParameterSpec s2) { |
| 64 | boolean result = false; |
| 65 | String alg1 = s1.getDigestAlgorithm().toUpperCase().trim(); |
| 66 | String alg2 = s2.getDigestAlgorithm().toUpperCase().trim(); |
| 67 | alg1 = alg1.replaceAll("\\-", ""); |
| 68 | alg2 = alg2.replaceAll("\\-", ""); |
| 69 | if (alg1.equals("SHA") || alg1.equals("SHA1")) { |
| 70 | result = (alg2.equals("SHA") || alg2.equals("SHA1")); |
| 71 | } else { |
| 72 | result = (alg1.equals(alg2)); |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | private static boolean compareMGF(OAEPParameterSpec s1, |
| 79 | OAEPParameterSpec s2) { |
| 80 | String alg1 = s1.getMGFAlgorithm(); |
| 81 | String alg2 = s2.getMGFAlgorithm(); |
| 82 | if (alg1.equals(alg2)) { |
| 83 | MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters(); |
| 84 | MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters(); |
| 85 | alg1 = mp1.getDigestAlgorithm(); |
| 86 | alg2 = mp2.getDigestAlgorithm(); |
| 87 | if (alg1.equals(alg2)) { |
| 88 | return true; |
| 89 | } else { |
| 90 | System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2); |
| 91 | return false; |
| 92 | } |
| 93 | } else { |
| 94 | System.out.println("MGF algos: " + alg1 + " vs " + alg2); |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private static boolean comparePSource(OAEPParameterSpec s1, |
| 100 | OAEPParameterSpec s2) { |
| 101 | PSource src1 = s1.getPSource(); |
| 102 | PSource src2 = s2.getPSource(); |
| 103 | String alg1 = src1.getAlgorithm(); |
| 104 | String alg2 = src2.getAlgorithm(); |
| 105 | if (alg1.equals(alg2)) { |
| 106 | // assumes they are PSource.PSpecified |
| 107 | return Arrays.equals(((PSource.PSpecified) src1).getValue(), |
| 108 | ((PSource.PSpecified) src2).getValue()); |
| 109 | } else { |
| 110 | System.out.println("PSource algos: " + alg1 + " vs " + alg2); |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private static boolean compareSpec(OAEPParameterSpec s1, |
| 116 | OAEPParameterSpec s2) { |
| 117 | return (compareMD(s1, s2) && compareMGF(s1, s2) && |
| 118 | comparePSource(s1, s2)); |
| 119 | } |
| 120 | |
| 121 | public static void main(String[] argv) throws Exception { |
| 122 | boolean status = true; |
| 123 | byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 }; |
| 124 | status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p); |
| 125 | status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p); |
| 126 | status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p); |
| 127 | status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]); |
| 128 | status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]); |
| 129 | status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]); |
| 130 | if (status) { |
| 131 | System.out.println("Test Passed"); |
| 132 | } else { |
| 133 | throw new Exception("One or More Test Failed"); |
| 134 | } |
| 135 | } |
| 136 | } |