test/com/sun/crypto/provider/KeyGenerator/Test4628062.java

Print this page




   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  */
  23 
  24 /*
  25  * @test
  26  * @bug 4628062
  27  * @summary Verify that AES KeyGenerator supports default initialization
  28  *      when init is not called
  29  * @author Valerie Peng
  30  */
  31 import java.security.*;
  32 import javax.crypto.*;
  33 import java.util.*;
  34 
  35 public class Test4628062 {
  36 
  37     private static final String ALGO = "AES";
  38     private static final int[] KEYSIZES =
  39         { 16, 24, 32 }; // in bytes


  40 
  41     public boolean execute() throws Exception {
  42         KeyGenerator kg = KeyGenerator.getInstance(ALGO, "SunJCE");
  43 
  44         // TEST FIX 4628062
  45         Key keyWithDefaultSize = kg.generateKey();
  46         byte[] encoding = keyWithDefaultSize.getEncoded();
  47         if (encoding.length == 0) {

  48             throw new Exception("default key length is 0!");


  49         }
  50 
  51         // BONUS TESTS

  52         // 1. call init(int keysize) with various valid key sizes
  53         // and see if the generated key is the right size.
  54         for (int i=0; i<KEYSIZES.length; i++) {
  55             kg.init(KEYSIZES[i]*8); // in bits
  56             Key key = kg.generateKey();
  57             if (key.getEncoded().length != KEYSIZES[i]) {
  58                 throw new Exception("key is generated with the wrong length!");
  59             }
  60         }
  61         // 2. call init(int keysize) with invalid key size and see
  62         // if the expected InvalidParameterException is thrown.
  63         try {
  64             kg.init(KEYSIZES[0]*8+1);
  65         } catch (InvalidParameterException ex) {
  66         } catch (Exception ex) {
  67             throw new Exception("wrong exception is thrown for invalid key size!");
  68         }
  69 
  70         // passed all tests...hooray!
  71         return true;
  72     }
  73 
  74     public static void main (String[] args) throws Exception {
  75         Security.addProvider(new com.sun.crypto.provider.SunJCE());
  76 
  77         Test4628062 test = new Test4628062();
  78         String testName = test.getClass().getName();
  79         if (test.execute()) {
  80             System.out.println(testName + ": Passed!");












  81         }
  82     }
  83 }


   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  */
  23 
  24 /*
  25  * @test
  26  * @bug 4628062 4963723
  27  * @summary Verify that AES KeyGenerator supports default initialization
  28  *      when init is not called
  29  * @author Valerie Peng
  30  */
  31 import java.security.*;
  32 import javax.crypto.*;
  33 import java.util.*;
  34 
  35 public class Test4628062 {
  36 
  37     private static final int[] AES_SIZES = { 16, 24, 32 }; // in bytes
  38     private static final int[] HMACSHA224_SIZES = { 28 };
  39     private static final int[] HMACSHA256_SIZES = { 32 };
  40     private static final int[] HMACSHA384_SIZES = { 48 };
  41     private static final int[] HMACSHA512_SIZES = { 64 };
  42 
  43     public boolean execute(String algo, int[] keySizes) throws Exception {
  44         KeyGenerator kg = KeyGenerator.getInstance(algo, "SunJCE");
  45 
  46         // TEST FIX 4628062
  47         Key keyWithDefaultSize = kg.generateKey();
  48         byte[] encoding = keyWithDefaultSize.getEncoded();
  49         int defKeyLen = encoding.length ;
  50         if (defKeyLen == 0) {
  51             throw new Exception("default key length is 0!");
  52         } else if (defKeyLen != keySizes[0]) {
  53             throw new Exception("default key length mismatch!");
  54         }
  55 
  56         // BONUS TESTS
  57         if (keySizes.length > 1) {
  58             // 1. call init(int keysize) with various valid key sizes
  59             // and see if the generated key is the right size.
  60             for (int i=0; i<keySizes.length; i++) {
  61                 kg.init(keySizes[i]*8); // in bits
  62                 Key key = kg.generateKey();
  63                 if (key.getEncoded().length != keySizes[i]) {
  64                     throw new Exception("key is generated with the wrong length!");
  65                 }
  66             }
  67             // 2. call init(int keysize) with invalid key size and see
  68             // if the expected InvalidParameterException is thrown.
  69             try {
  70                 kg.init(keySizes[0]*8+1);
  71             } catch (InvalidParameterException ex) {
  72             } catch (Exception ex) {
  73                 throw new Exception("wrong exception is thrown for invalid key size!");
  74             }
  75         }
  76         // passed all tests...hooray!
  77         return true;
  78     }
  79 
  80     public static void main (String[] args) throws Exception {
  81         Security.addProvider(new com.sun.crypto.provider.SunJCE());
  82 
  83         Test4628062 test = new Test4628062();
  84         String testName = test.getClass().getName();
  85         if (test.execute("AES", AES_SIZES)) {
  86             System.out.println(testName + ": AES Passed!");
  87         }
  88         if (test.execute("HmacSHA224", HMACSHA224_SIZES)) {
  89             System.out.println(testName + ": HmacSHA224 Passed!");
  90         }
  91         if (test.execute("HmacSHA256", HMACSHA256_SIZES)) {
  92             System.out.println(testName + ": HmacSHA256 Passed!");
  93         }
  94         if (test.execute("HmacSHA384", HMACSHA384_SIZES)) {
  95             System.out.println(testName + ": HmacSHA384 Passed!");
  96         }
  97         if (test.execute("HmacSHA512", HMACSHA512_SIZES)) {
  98             System.out.println(testName + ": HmacSHA512 Passed!");
  99         }
 100     }
 101 }