< prev index next >

test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPPadding.java

Print this page


   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


 113                 MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
 114         test(new OAEPParameterSpec("SHA-384", "MGF1",
 115                 MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
 116         test(new OAEPParameterSpec("SHA-384", "MGF1",
 117                 MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
 118         test(new OAEPParameterSpec("SHA-384", "MGF1",
 119                 MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
 120         test(new OAEPParameterSpec("SHA-384", "MGF1",
 121                 MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
 122         // SHA-512
 123         test(new OAEPParameterSpec("SHA-512", "MGF1",
 124                 MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
 125         test(new OAEPParameterSpec("SHA-512", "MGF1",
 126                 MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
 127         test(new OAEPParameterSpec("SHA-512", "MGF1",
 128                 MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
 129         test(new OAEPParameterSpec("SHA-512", "MGF1",
 130                 MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
 131         test(new OAEPParameterSpec("SHA-512", "MGF1",
 132                 MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));










 133         if (failed) {
 134             throw new Exception("Test failed");
 135         }
 136     }
 137 
 138     /*
 139      * Test with one byte, the max bytes, and the max + 1 bytes allowed by
 140      * the RSA key size and the digest algorithm
 141      */
 142     static void test(OAEPParameterSpec spec) throws Exception {
 143         int dlen = 0;
 144         String algo;
 145 
 146         // For default OAEPParameterSpec case (SHA1)
 147         if (spec == null) {
 148             dlen = 20;
 149             algo = "Default";
 150         } else {
 151             // Use the digest algorith provided in the spec
 152             algo = spec.getDigestAlgorithm();
 153             if (algo.equals("MD5")) {
 154                 dlen = 16;
 155             } else if (algo.equals("SHA1")) {
 156                 dlen = 20;
 157             } else if (algo.equals("SHA-224")) {
 158                 dlen = 28;
 159             } else if (algo.equals("SHA-256")) {
 160                 dlen = 32;
 161             } else if (algo.equals("SHA-384")) {
 162                 dlen = 48;
 163             } else if (algo.equals("SHA-512")) {
 164                 dlen = 64;
 165             }
 166         }
 167 
 168         // OAEP maximum length for a given digest algorith & RSA key length
 169         int max = ((publicKey.getModulus().bitLength() / 8) - (2 * dlen) - 2);
 170 
 171         // Test with data length of 1
 172         try {
 173             testEncryptDecrypt(spec, 1);
 174         } catch (Exception e) {
 175             System.out.println(algo + " failed with data length of 1");
 176             e.printStackTrace();
 177             failed = true;
 178         }
 179 


   1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


 113                 MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
 114         test(new OAEPParameterSpec("SHA-384", "MGF1",
 115                 MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
 116         test(new OAEPParameterSpec("SHA-384", "MGF1",
 117                 MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
 118         test(new OAEPParameterSpec("SHA-384", "MGF1",
 119                 MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
 120         test(new OAEPParameterSpec("SHA-384", "MGF1",
 121                 MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
 122         // SHA-512
 123         test(new OAEPParameterSpec("SHA-512", "MGF1",
 124                 MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
 125         test(new OAEPParameterSpec("SHA-512", "MGF1",
 126                 MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
 127         test(new OAEPParameterSpec("SHA-512", "MGF1",
 128                 MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
 129         test(new OAEPParameterSpec("SHA-512", "MGF1",
 130                 MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
 131         test(new OAEPParameterSpec("SHA-512", "MGF1",
 132                 MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
 133         // SHA-512/224 and SHA-512/256
 134         test(new OAEPParameterSpec("SHA-512/224", "MGF1",
 135                 MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
 136         test(new OAEPParameterSpec("SHA-512/224", "MGF1",
 137                 MGF1ParameterSpec.SHA512_224, PSource.PSpecified.DEFAULT));
 138         test(new OAEPParameterSpec("SHA-512/256", "MGF1",
 139                 MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
 140         test(new OAEPParameterSpec("SHA-512/256", "MGF1",
 141                 MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
 142 
 143         if (failed) {
 144             throw new Exception("Test failed");
 145         }
 146     }
 147 
 148     /*
 149      * Test with one byte, the max bytes, and the max + 1 bytes allowed by
 150      * the RSA key size and the digest algorithm
 151      */
 152     static void test(OAEPParameterSpec spec) throws Exception {
 153         int dlen = 0;
 154         String algo;
 155 
 156         // For default OAEPParameterSpec case (SHA1)
 157         if (spec == null) {
 158             dlen = 20;
 159             algo = "Default";
 160         } else {
 161             // Use the digest algorith provided in the spec
 162             algo = spec.getDigestAlgorithm();
 163             if (algo.equals("MD5")) {
 164                 dlen = 16;
 165             } else if (algo.equals("SHA1")) {
 166                 dlen = 20;
 167             } else if (algo.equals("SHA-224") || algo.equals("SHA-512/224")) {
 168                 dlen = 28;
 169             } else if (algo.equals("SHA-256") || algo.equals("SHA-512/256")) {
 170                 dlen = 32;
 171             } else if (algo.equals("SHA-384")) {
 172                 dlen = 48;
 173             } else if (algo.equals("SHA-512")) {
 174                 dlen = 64;
 175             }
 176         }
 177 
 178         // OAEP maximum length for a given digest algorith & RSA key length
 179         int max = ((publicKey.getModulus().bitLength() / 8) - (2 * dlen) - 2);
 180 
 181         // Test with data length of 1
 182         try {
 183             testEncryptDecrypt(spec, 1);
 184         } catch (Exception e) {
 185             System.out.println(algo + " failed with data length of 1");
 186             e.printStackTrace();
 187             failed = true;
 188         }
 189 


< prev index next >