changeset 6341:3c5a62290939

8004044: Lazily instantiate SunJCE.RANDOM Summary: Replace the static initialization of SunJCE.RANDOM object w/ lazy initialization Reviewed-by: mchung
author valeriep
date Tue, 08 Jan 2013 11:55:21 -0800
parents 5333a4c8cade
children 9b6a29cb04ac
files src/share/classes/com/sun/crypto/provider/AESKeyGenerator.java src/share/classes/com/sun/crypto/provider/BlowfishKeyGenerator.java src/share/classes/com/sun/crypto/provider/CipherCore.java src/share/classes/com/sun/crypto/provider/DESKeyGenerator.java src/share/classes/com/sun/crypto/provider/DESedeKeyGenerator.java src/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java src/share/classes/com/sun/crypto/provider/DHKeyPairGenerator.java src/share/classes/com/sun/crypto/provider/DHParameterGenerator.java src/share/classes/com/sun/crypto/provider/HmacMD5KeyGenerator.java src/share/classes/com/sun/crypto/provider/HmacPKCS12PBESHA1.java src/share/classes/com/sun/crypto/provider/HmacSHA1KeyGenerator.java src/share/classes/com/sun/crypto/provider/ISO10126Padding.java src/share/classes/com/sun/crypto/provider/KeyGeneratorCore.java src/share/classes/com/sun/crypto/provider/KeyProtector.java src/share/classes/com/sun/crypto/provider/PBECipherCore.java src/share/classes/com/sun/crypto/provider/PBES1Core.java src/share/classes/com/sun/crypto/provider/PBES2Core.java src/share/classes/com/sun/crypto/provider/PBMAC1Core.java src/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java src/share/classes/com/sun/crypto/provider/SunJCE.java
diffstat 20 files changed, 47 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/crypto/provider/AESKeyGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/AESKeyGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -106,7 +106,7 @@
         SecretKeySpec aesKey = null;
 
         if (this.random == null) {
-            this.random = SunJCE.RANDOM;
+            this.random = SunJCE.getRandom();
         }
 
         byte[] keyBytes = new byte[keySize];
--- a/src/share/classes/com/sun/crypto/provider/BlowfishKeyGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/BlowfishKeyGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -104,7 +104,7 @@
      */
     protected SecretKey engineGenerateKey() {
         if (this.random == null) {
-            this.random = SunJCE.RANDOM;
+            this.random = SunJCE.getRandom();
         }
 
         byte[] keyBytes = new byte[this.keysize];
--- a/src/share/classes/com/sun/crypto/provider/CipherCore.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/CipherCore.java	Tue Jan 08 11:55:21 2013 -0800
@@ -410,7 +410,7 @@
             } else {
                 iv = new byte[blockSize];
             }
-            SunJCE.RANDOM.nextBytes(iv);
+            SunJCE.getRandom().nextBytes(iv);
         }
         if (cipherMode == GCM_MODE) {
             algName = "GCM";
@@ -564,7 +564,7 @@
             }
 
             if (random == null) {
-                random = SunJCE.RANDOM;
+                random = SunJCE.getRandom();
             }
             if (cipherMode == GCM_MODE) {
                 ivBytes = new byte[GaloisCounterMode.DEFAULT_IV_LEN];
--- a/src/share/classes/com/sun/crypto/provider/DESKeyGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/DESKeyGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -102,7 +102,7 @@
         DESKey desKey = null;
 
         if (this.random == null) {
-            this.random = SunJCE.RANDOM;
+            this.random = SunJCE.getRandom();
         }
 
         try {
--- a/src/share/classes/com/sun/crypto/provider/DESedeKeyGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/DESedeKeyGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -105,7 +105,7 @@
      */
     protected SecretKey engineGenerateKey() {
         if (this.random == null) {
-            this.random = SunJCE.RANDOM;
+            this.random = SunJCE.getRandom();
         }
 
         byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
--- a/src/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -217,7 +217,7 @@
             if (params == null) {
                 iv = new byte[8];
                 if (random == null) {
-                    random = SunJCE.RANDOM;
+                    random = SunJCE.getRandom();
                 }
                 random.nextBytes(iv);
             }
--- a/src/share/classes/com/sun/crypto/provider/DHKeyPairGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/DHKeyPairGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -140,7 +140,7 @@
      */
     public KeyPair generateKeyPair() {
         if (random == null) {
-            random = SunJCE.RANDOM;
+            random = SunJCE.getRandom();
         }
 
         if (params == null) {
--- a/src/share/classes/com/sun/crypto/provider/DHParameterGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/DHParameterGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -131,7 +131,7 @@
         }
 
         if (this.random == null)
-            this.random = SunJCE.RANDOM;
+            this.random = SunJCE.getRandom();
 
         try {
             AlgorithmParameterGenerator paramGen;
--- a/src/share/classes/com/sun/crypto/provider/HmacMD5KeyGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/HmacMD5KeyGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -98,7 +98,7 @@
      */
     protected SecretKey engineGenerateKey() {
         if (this.random == null) {
-            this.random = SunJCE.RANDOM;
+            this.random = SunJCE.getRandom();
         }
 
         byte[] keyBytes = new byte[this.keysize];
--- a/src/share/classes/com/sun/crypto/provider/HmacPKCS12PBESHA1.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/HmacPKCS12PBESHA1.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -89,7 +89,7 @@
             // generate default for salt and iteration count if necessary
             if (salt == null) {
                 salt = new byte[20];
-                SunJCE.RANDOM.nextBytes(salt);
+                SunJCE.getRandom().nextBytes(salt);
             }
             if (iCount == 0) iCount = 100;
         } else if (!(params instanceof PBEParameterSpec)) {
--- a/src/share/classes/com/sun/crypto/provider/HmacSHA1KeyGenerator.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/HmacSHA1KeyGenerator.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -98,7 +98,7 @@
      */
     protected SecretKey engineGenerateKey() {
         if (this.random == null) {
-            this.random = SunJCE.RANDOM;
+            this.random = SunJCE.getRandom();
         }
 
         byte[] keyBytes = new byte[this.keysize];
--- a/src/share/classes/com/sun/crypto/provider/ISO10126Padding.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/ISO10126Padding.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -69,7 +69,7 @@
 
         byte paddingOctet = (byte) (len & 0xff);
         byte[] padding = new byte[len];
-        SunJCE.RANDOM.nextBytes(padding);
+        SunJCE.getRandom().nextBytes(padding);
         padding[len-1] = paddingOctet;
         System.arraycopy(padding, 0, in, off, len);
         return;
--- a/src/share/classes/com/sun/crypto/provider/KeyGeneratorCore.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/KeyGeneratorCore.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -98,7 +98,7 @@
     // generate the key
     SecretKey implGenerateKey() {
         if (random == null) {
-            random = SunJCE.RANDOM;
+            random = SunJCE.getRandom();
         }
         byte[] b = new byte[(keySize + 7) >> 3];
         random.nextBytes(b);
--- a/src/share/classes/com/sun/crypto/provider/KeyProtector.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/KeyProtector.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -99,7 +99,7 @@
     {
         // create a random salt (8 bytes)
         byte[] salt = new byte[8];
-        SunJCE.RANDOM.nextBytes(salt);
+        SunJCE.getRandom().nextBytes(salt);
 
         // create PBE parameters from salt and iteration count
         PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);
@@ -284,7 +284,7 @@
     {
         // create a random salt (8 bytes)
         byte[] salt = new byte[8];
-        SunJCE.RANDOM.nextBytes(salt);
+        SunJCE.getRandom().nextBytes(salt);
 
         // create PBE parameters from salt and iteration count
         PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);
--- a/src/share/classes/com/sun/crypto/provider/PBECipherCore.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/PBECipherCore.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -164,7 +164,7 @@
         AlgorithmParameters params = null;
         if (salt == null) {
             salt = new byte[8];
-            SunJCE.RANDOM.nextBytes(salt);
+            SunJCE.getRandom().nextBytes(salt);
         }
         PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
         try {
--- a/src/share/classes/com/sun/crypto/provider/PBES1Core.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/PBES1Core.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -164,7 +164,7 @@
         AlgorithmParameters params = null;
         if (salt == null) {
             salt = new byte[8];
-            SunJCE.RANDOM.nextBytes(salt);
+            SunJCE.getRandom().nextBytes(salt);
         }
         PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
         try {
--- a/src/share/classes/com/sun/crypto/provider/PBES2Core.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/PBES2Core.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -134,13 +134,13 @@
         if (salt == null) {
             // generate random salt and use default iteration count
             salt = new byte[DEFAULT_SALT_LENGTH];
-            SunJCE.RANDOM.nextBytes(salt);
+            SunJCE.getRandom().nextBytes(salt);
             iCount = DEFAULT_COUNT;
         }
         if (ivSpec == null) {
             // generate random IV
             byte[] ivBytes = new byte[blkSize];
-            SunJCE.RANDOM.nextBytes(ivBytes);
+            SunJCE.getRandom().nextBytes(ivBytes);
             ivSpec = new IvParameterSpec(ivBytes);
         }
         PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount, ivSpec);
--- a/src/share/classes/com/sun/crypto/provider/PBMAC1Core.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/PBMAC1Core.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -123,7 +123,7 @@
             // generate default for salt and iteration count if necessary
             if (salt == null) {
                 salt = new byte[DEFAULT_SALT_LENGTH];
-                SunJCE.RANDOM.nextBytes(salt);
+                SunJCE.getRandom().nextBytes(salt);
             }
             if (iCount == 0) iCount = DEFAULT_COUNT;
         } else if (!(params instanceof PBEParameterSpec)) {
--- a/src/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java	Tue Jan 08 11:55:21 2013 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -227,7 +227,7 @@
             // follow the recommendation in PKCS12 v1.0
             // section B.4 to generate salt and iCount.
             salt = new byte[DEFAULT_SALT_LENGTH];
-            SunJCE.RANDOM.nextBytes(salt);
+            SunJCE.getRandom().nextBytes(salt);
             iCount = DEFAULT_COUNT;
         }
         PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
@@ -294,7 +294,7 @@
                 if (random != null) {
                     random.nextBytes(salt);
                 } else {
-                    SunJCE.RANDOM.nextBytes(salt);
+                    SunJCE.getRandom().nextBytes(salt);
                 }
             }
             if (iCount == 0) iCount = DEFAULT_COUNT;
--- a/src/share/classes/com/sun/crypto/provider/SunJCE.java	Mon Jan 07 14:40:07 2013 -0800
+++ b/src/share/classes/com/sun/crypto/provider/SunJCE.java	Tue Jan 08 11:55:21 2013 -0800
@@ -91,7 +91,12 @@
     /* Are we debugging? -- for developers */
     static final boolean debug = false;
 
-    static final SecureRandom RANDOM = new SecureRandom();
+    // lazy initialize SecureRandom to avoid potential recursion if Sun
+    // provider has not been installed yet
+    private static class SecureRandomHolder {
+        static final SecureRandom RANDOM = new SecureRandom();
+    }
+    static SecureRandom getRandom() { return SecureRandomHolder.RANDOM; }
 
     public SunJCE() {
         /* We are the "SunJCE" provider */