1/* 2 * Copyright (C) 2012 Google Inc. 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 17package benchmarks.regression; 18 19import com.google.caliper.Param; 20import com.google.caliper.SimpleBenchmark; 21 22import java.io.ByteArrayInputStream; 23import java.io.InputStream; 24import java.security.KeyPair; 25import java.security.KeyPairGenerator; 26import java.security.spec.AlgorithmParameterSpec; 27 28import javax.crypto.CipherInputStream; 29import javax.crypto.KeyGenerator; 30import javax.crypto.SecretKey; 31 32import java.util.HashMap; 33import java.util.Map; 34 35import javax.crypto.Cipher; 36import javax.crypto.spec.IvParameterSpec; 37 38/** 39 * CipherInputStream benchmark. 40 */ 41public class CipherInputStreamBenchmark extends SimpleBenchmark { 42 43 private static final int DATA_SIZE = 1024 * 1024; 44 private static final byte[] DATA = new byte[DATA_SIZE]; 45 46 private static final int IV_SIZE = 16; 47 private static final byte[] IV = new byte[IV_SIZE]; 48 49 static { 50 for (int i = 0; i < DATA_SIZE; i++) { 51 DATA[i] = (byte) i; 52 } 53 for (int i = 0; i < IV_SIZE; i++) { 54 IV[i] = (byte) i; 55 } 56 } 57 58 private SecretKey key; 59 60 private byte[] output = new byte[8192]; 61 62 private Cipher cipherEncrypt; 63 64 private AlgorithmParameterSpec spec; 65 66 @Override protected void setUp() throws Exception { 67 KeyGenerator generator = KeyGenerator.getInstance("AES"); 68 generator.init(128); 69 key = generator.generateKey(); 70 71 spec = new IvParameterSpec(IV); 72 73 cipherEncrypt = Cipher.getInstance("AES/CBC/PKCS5Padding"); 74 cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, spec); 75 } 76 77 public void timeEncrypt(int reps) throws Exception { 78 for (int i = 0; i < reps; ++i) { 79 cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, spec); 80 InputStream is = new CipherInputStream(new ByteArrayInputStream(DATA), cipherEncrypt); 81 while (is.read(output) != -1) { 82 } 83 } 84 } 85} 86