提交 092dce14 编写于 作者: V valeriep

8004044: Lazily instantiate SunJCE.RANDOM

Summary: Replace the static initialization of SunJCE.RANDOM object w/ lazy initialization
Reviewed-by: mchung
上级 da007839
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -106,7 +106,7 @@ public final class AESKeyGenerator extends KeyGeneratorSpi { ...@@ -106,7 +106,7 @@ public final class AESKeyGenerator extends KeyGeneratorSpi {
SecretKeySpec aesKey = null; SecretKeySpec aesKey = null;
if (this.random == null) { if (this.random == null) {
this.random = SunJCE.RANDOM; this.random = SunJCE.getRandom();
} }
byte[] keyBytes = new byte[keySize]; byte[] keyBytes = new byte[keySize];
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -104,7 +104,7 @@ public final class BlowfishKeyGenerator extends KeyGeneratorSpi { ...@@ -104,7 +104,7 @@ public final class BlowfishKeyGenerator extends KeyGeneratorSpi {
*/ */
protected SecretKey engineGenerateKey() { protected SecretKey engineGenerateKey() {
if (this.random == null) { if (this.random == null) {
this.random = SunJCE.RANDOM; this.random = SunJCE.getRandom();
} }
byte[] keyBytes = new byte[this.keysize]; byte[] keyBytes = new byte[this.keysize];
......
...@@ -410,7 +410,7 @@ final class CipherCore { ...@@ -410,7 +410,7 @@ final class CipherCore {
} else { } else {
iv = new byte[blockSize]; iv = new byte[blockSize];
} }
SunJCE.RANDOM.nextBytes(iv); SunJCE.getRandom().nextBytes(iv);
} }
if (cipherMode == GCM_MODE) { if (cipherMode == GCM_MODE) {
algName = "GCM"; algName = "GCM";
...@@ -564,7 +564,7 @@ final class CipherCore { ...@@ -564,7 +564,7 @@ final class CipherCore {
} }
if (random == null) { if (random == null) {
random = SunJCE.RANDOM; random = SunJCE.getRandom();
} }
if (cipherMode == GCM_MODE) { if (cipherMode == GCM_MODE) {
ivBytes = new byte[GaloisCounterMode.DEFAULT_IV_LEN]; ivBytes = new byte[GaloisCounterMode.DEFAULT_IV_LEN];
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -102,7 +102,7 @@ public final class DESKeyGenerator extends KeyGeneratorSpi { ...@@ -102,7 +102,7 @@ public final class DESKeyGenerator extends KeyGeneratorSpi {
DESKey desKey = null; DESKey desKey = null;
if (this.random == null) { if (this.random == null) {
this.random = SunJCE.RANDOM; this.random = SunJCE.getRandom();
} }
try { try {
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -105,7 +105,7 @@ public final class DESedeKeyGenerator extends KeyGeneratorSpi { ...@@ -105,7 +105,7 @@ public final class DESedeKeyGenerator extends KeyGeneratorSpi {
*/ */
protected SecretKey engineGenerateKey() { protected SecretKey engineGenerateKey() {
if (this.random == null) { if (this.random == null) {
this.random = SunJCE.RANDOM; this.random = SunJCE.getRandom();
} }
byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN]; byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -217,7 +217,7 @@ public final class DESedeWrapCipher extends CipherSpi { ...@@ -217,7 +217,7 @@ public final class DESedeWrapCipher extends CipherSpi {
if (params == null) { if (params == null) {
iv = new byte[8]; iv = new byte[8];
if (random == null) { if (random == null) {
random = SunJCE.RANDOM; random = SunJCE.getRandom();
} }
random.nextBytes(iv); random.nextBytes(iv);
} }
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -140,7 +140,7 @@ public final class DHKeyPairGenerator extends KeyPairGeneratorSpi { ...@@ -140,7 +140,7 @@ public final class DHKeyPairGenerator extends KeyPairGeneratorSpi {
*/ */
public KeyPair generateKeyPair() { public KeyPair generateKeyPair() {
if (random == null) { if (random == null) {
random = SunJCE.RANDOM; random = SunJCE.getRandom();
} }
if (params == null) { if (params == null) {
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -131,7 +131,7 @@ extends AlgorithmParameterGeneratorSpi { ...@@ -131,7 +131,7 @@ extends AlgorithmParameterGeneratorSpi {
} }
if (this.random == null) if (this.random == null)
this.random = SunJCE.RANDOM; this.random = SunJCE.getRandom();
try { try {
AlgorithmParameterGenerator paramGen; AlgorithmParameterGenerator paramGen;
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -98,7 +98,7 @@ public final class HmacMD5KeyGenerator extends KeyGeneratorSpi { ...@@ -98,7 +98,7 @@ public final class HmacMD5KeyGenerator extends KeyGeneratorSpi {
*/ */
protected SecretKey engineGenerateKey() { protected SecretKey engineGenerateKey() {
if (this.random == null) { if (this.random == null) {
this.random = SunJCE.RANDOM; this.random = SunJCE.getRandom();
} }
byte[] keyBytes = new byte[this.keysize]; byte[] keyBytes = new byte[this.keysize];
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -89,7 +89,7 @@ public final class HmacPKCS12PBESHA1 extends HmacCore { ...@@ -89,7 +89,7 @@ public final class HmacPKCS12PBESHA1 extends HmacCore {
// generate default for salt and iteration count if necessary // generate default for salt and iteration count if necessary
if (salt == null) { if (salt == null) {
salt = new byte[20]; salt = new byte[20];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
} }
if (iCount == 0) iCount = 100; if (iCount == 0) iCount = 100;
} else if (!(params instanceof PBEParameterSpec)) { } else if (!(params instanceof PBEParameterSpec)) {
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -98,7 +98,7 @@ public final class HmacSHA1KeyGenerator extends KeyGeneratorSpi { ...@@ -98,7 +98,7 @@ public final class HmacSHA1KeyGenerator extends KeyGeneratorSpi {
*/ */
protected SecretKey engineGenerateKey() { protected SecretKey engineGenerateKey() {
if (this.random == null) { if (this.random == null) {
this.random = SunJCE.RANDOM; this.random = SunJCE.getRandom();
} }
byte[] keyBytes = new byte[this.keysize]; byte[] keyBytes = new byte[this.keysize];
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -69,7 +69,7 @@ final class ISO10126Padding implements Padding { ...@@ -69,7 +69,7 @@ final class ISO10126Padding implements Padding {
byte paddingOctet = (byte) (len & 0xff); byte paddingOctet = (byte) (len & 0xff);
byte[] padding = new byte[len]; byte[] padding = new byte[len];
SunJCE.RANDOM.nextBytes(padding); SunJCE.getRandom().nextBytes(padding);
padding[len-1] = paddingOctet; padding[len-1] = paddingOctet;
System.arraycopy(padding, 0, in, off, len); System.arraycopy(padding, 0, in, off, len);
return; return;
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -98,7 +98,7 @@ final class KeyGeneratorCore { ...@@ -98,7 +98,7 @@ final class KeyGeneratorCore {
// generate the key // generate the key
SecretKey implGenerateKey() { SecretKey implGenerateKey() {
if (random == null) { if (random == null) {
random = SunJCE.RANDOM; random = SunJCE.getRandom();
} }
byte[] b = new byte[(keySize + 7) >> 3]; byte[] b = new byte[(keySize + 7) >> 3];
random.nextBytes(b); random.nextBytes(b);
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -99,7 +99,7 @@ final class KeyProtector { ...@@ -99,7 +99,7 @@ final class KeyProtector {
{ {
// create a random salt (8 bytes) // create a random salt (8 bytes)
byte[] salt = new byte[8]; byte[] salt = new byte[8];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
// create PBE parameters from salt and iteration count // create PBE parameters from salt and iteration count
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20); PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);
...@@ -284,7 +284,7 @@ final class KeyProtector { ...@@ -284,7 +284,7 @@ final class KeyProtector {
{ {
// create a random salt (8 bytes) // create a random salt (8 bytes)
byte[] salt = new byte[8]; byte[] salt = new byte[8];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
// create PBE parameters from salt and iteration count // create PBE parameters from salt and iteration count
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20); PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -164,7 +164,7 @@ final class PBECipherCore { ...@@ -164,7 +164,7 @@ final class PBECipherCore {
AlgorithmParameters params = null; AlgorithmParameters params = null;
if (salt == null) { if (salt == null) {
salt = new byte[8]; salt = new byte[8];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
} }
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount); PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
try { try {
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -164,7 +164,7 @@ final class PBES1Core { ...@@ -164,7 +164,7 @@ final class PBES1Core {
AlgorithmParameters params = null; AlgorithmParameters params = null;
if (salt == null) { if (salt == null) {
salt = new byte[8]; salt = new byte[8];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
} }
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount); PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
try { try {
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -134,13 +134,13 @@ abstract class PBES2Core extends CipherSpi { ...@@ -134,13 +134,13 @@ abstract class PBES2Core extends CipherSpi {
if (salt == null) { if (salt == null) {
// generate random salt and use default iteration count // generate random salt and use default iteration count
salt = new byte[DEFAULT_SALT_LENGTH]; salt = new byte[DEFAULT_SALT_LENGTH];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
iCount = DEFAULT_COUNT; iCount = DEFAULT_COUNT;
} }
if (ivSpec == null) { if (ivSpec == null) {
// generate random IV // generate random IV
byte[] ivBytes = new byte[blkSize]; byte[] ivBytes = new byte[blkSize];
SunJCE.RANDOM.nextBytes(ivBytes); SunJCE.getRandom().nextBytes(ivBytes);
ivSpec = new IvParameterSpec(ivBytes); ivSpec = new IvParameterSpec(ivBytes);
} }
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount, ivSpec); PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount, ivSpec);
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -123,7 +123,7 @@ abstract class PBMAC1Core extends HmacCore { ...@@ -123,7 +123,7 @@ abstract class PBMAC1Core extends HmacCore {
// generate default for salt and iteration count if necessary // generate default for salt and iteration count if necessary
if (salt == null) { if (salt == null) {
salt = new byte[DEFAULT_SALT_LENGTH]; salt = new byte[DEFAULT_SALT_LENGTH];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
} }
if (iCount == 0) iCount = DEFAULT_COUNT; if (iCount == 0) iCount = DEFAULT_COUNT;
} else if (!(params instanceof PBEParameterSpec)) { } else if (!(params instanceof PBEParameterSpec)) {
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -227,7 +227,7 @@ final class PKCS12PBECipherCore { ...@@ -227,7 +227,7 @@ final class PKCS12PBECipherCore {
// follow the recommendation in PKCS12 v1.0 // follow the recommendation in PKCS12 v1.0
// section B.4 to generate salt and iCount. // section B.4 to generate salt and iCount.
salt = new byte[DEFAULT_SALT_LENGTH]; salt = new byte[DEFAULT_SALT_LENGTH];
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
iCount = DEFAULT_COUNT; iCount = DEFAULT_COUNT;
} }
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount); PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
...@@ -294,7 +294,7 @@ final class PKCS12PBECipherCore { ...@@ -294,7 +294,7 @@ final class PKCS12PBECipherCore {
if (random != null) { if (random != null) {
random.nextBytes(salt); random.nextBytes(salt);
} else { } else {
SunJCE.RANDOM.nextBytes(salt); SunJCE.getRandom().nextBytes(salt);
} }
} }
if (iCount == 0) iCount = DEFAULT_COUNT; if (iCount == 0) iCount = DEFAULT_COUNT;
......
...@@ -91,7 +91,12 @@ public final class SunJCE extends Provider { ...@@ -91,7 +91,12 @@ public final class SunJCE extends Provider {
/* Are we debugging? -- for developers */ /* Are we debugging? -- for developers */
static final boolean debug = false; static final boolean debug = false;
// 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 final SecureRandom RANDOM = new SecureRandom();
}
static SecureRandom getRandom() { return SecureRandomHolder.RANDOM; }
public SunJCE() { public SunJCE() {
/* We are the "SunJCE" provider */ /* We are the "SunJCE" provider */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册