1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.keystore.cts; 18 19 import java.security.SecureRandom; 20 import java.util.concurrent.atomic.AtomicLong; 21 22 /** 23 * {@link SecureRandom} which counts how many bytes it has output. 24 */ 25 public class CountingSecureRandom extends SecureRandom { 26 27 private final SecureRandom mDelegate = new SecureRandom(); 28 private final AtomicLong mOutputSizeBytes = new AtomicLong(); 29 getOutputSizeBytes()30 public long getOutputSizeBytes() { 31 return mOutputSizeBytes.get(); 32 } 33 resetCounters()34 public void resetCounters() { 35 mOutputSizeBytes.set(0); 36 } 37 38 @Override generateSeed(int numBytes)39 public byte[] generateSeed(int numBytes) { 40 if (numBytes > 0) { 41 mOutputSizeBytes.addAndGet(numBytes); 42 } 43 return mDelegate.generateSeed(numBytes); 44 } 45 46 @Override getAlgorithm()47 public String getAlgorithm() { 48 return mDelegate.getAlgorithm(); 49 } 50 51 @Override nextBytes(byte[] bytes)52 public synchronized void nextBytes(byte[] bytes) { 53 if ((bytes != null) && (bytes.length > 0)) { 54 mOutputSizeBytes.addAndGet(bytes.length); 55 } 56 mDelegate.nextBytes(bytes); 57 } 58 59 @Override setSeed(byte[] seed)60 public synchronized void setSeed(byte[] seed) { 61 // Ignore seeding -- not needed in tests and may impact the quality of the output of the 62 // delegate SecureRandom by preventing it from self-seeding 63 } 64 65 @Override setSeed(long seed)66 public void setSeed(long seed) { 67 // Ignore seeding -- not needed in tests and may impact the quality of the output of the 68 // delegate SecureRandom by preventing it from self-seeding 69 } 70 71 @Override nextBoolean()72 public boolean nextBoolean() { 73 throw new UnsupportedOperationException(); 74 } 75 76 @Override nextDouble()77 public double nextDouble() { 78 throw new UnsupportedOperationException(); 79 } 80 81 @Override nextFloat()82 public float nextFloat() { 83 throw new UnsupportedOperationException(); 84 } 85 86 @Override nextGaussian()87 public synchronized double nextGaussian() { 88 throw new UnsupportedOperationException(); 89 } 90 91 @Override nextInt()92 public int nextInt() { 93 throw new UnsupportedOperationException(); 94 } 95 96 @Override nextInt(int n)97 public int nextInt(int n) { 98 throw new UnsupportedOperationException(); 99 } 100 101 @Override nextLong()102 public long nextLong() { 103 throw new UnsupportedOperationException(); 104 } 105 } 106