1 /* 2 * Copyright (c) 2003, 2021, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.security.jca; 27 28 import java.lang.ref.*; 29 import java.security.*; 30 31 /** 32 * Collection of static utility methods used by the security framework. 33 * 34 * @author Andreas Sterbenz 35 * @since 1.5 36 */ 37 public final class JCAUtil { 38 JCAUtil()39 private JCAUtil() { 40 // no instantiation 41 } 42 43 // size of the temporary arrays we use. Should fit into the CPU's 1st 44 // level cache and could be adjusted based on the platform 45 private static final int ARRAY_SIZE = 4096; 46 47 /** 48 * Get the size of a temporary buffer array to use in order to be 49 * cache efficient. totalSize indicates the total amount of data to 50 * be buffered. Used by the engineUpdate(ByteBuffer) methods. 51 */ getTempArraySize(int totalSize)52 public static int getTempArraySize(int totalSize) { 53 return Math.min(ARRAY_SIZE, totalSize); 54 } 55 56 // cached SecureRandom instance 57 private static class CachedSecureRandomHolder { 58 public static SecureRandom instance = new SecureRandom(); 59 } 60 61 private static volatile SecureRandom def = null; 62 63 /** 64 * Get a SecureRandom instance. This method should be used by JDK 65 * internal code in favor of calling "new SecureRandom()". That needs to 66 * iterate through the provider table to find the default SecureRandom 67 * implementation, which is fairly inefficient. 68 */ getSecureRandom()69 public static SecureRandom getSecureRandom() { 70 return CachedSecureRandomHolder.instance; 71 } 72 73 // called by sun.security.jca.Providers class when provider list is changed clearDefSecureRandom()74 static void clearDefSecureRandom() { 75 def = null; 76 } 77 78 /** 79 * Get the default SecureRandom instance. This method is the 80 * optimized version of "new SecureRandom()" which re-uses the default 81 * SecureRandom impl if the provider table is the same. 82 */ getDefSecureRandom()83 public static SecureRandom getDefSecureRandom() { 84 SecureRandom result = def; 85 if (result == null) { 86 synchronized (JCAUtil.class) { 87 result = def; 88 if (result == null) { 89 def = result = new SecureRandom(); 90 } 91 } 92 } 93 return result; 94 95 } 96 } 97