• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package benchmarks.regression;
18 
19 import com.google.caliper.Param;
20 import com.google.caliper.SimpleBenchmark;
21 import java.security.KeyPair;
22 import java.security.KeyPairGenerator;
23 import java.security.spec.AlgorithmParameterSpec;
24 
25 import javax.crypto.KeyGenerator;
26 import javax.crypto.SecretKey;
27 import java.util.HashMap;
28 import java.util.Map;
29 
30 import javax.crypto.Cipher;
31 import javax.crypto.spec.IvParameterSpec;
32 
33 /**
34  * Cipher benchmarks. Only runs on AES currently because of the combinatorial
35  * explosion of the test as it stands.
36  */
37 public class CipherBenchmark extends SimpleBenchmark {
38 
39     private static final int DATA_SIZE = 8192;
40     private static final byte[] DATA = new byte[DATA_SIZE];
41 
42     private static final int IV_SIZE = 16;
43 
44     private static final byte[] IV = new byte[IV_SIZE];
45 
46     static {
47         for (int i = 0; i < DATA_SIZE; i++) {
48             DATA[i] = (byte) i;
49         }
50         for (int i = 0; i < IV_SIZE; i++) {
51             IV[i] = (byte) i;
52         }
53     }
54 
55     @Param private Algorithm algorithm;
56 
57     public enum Algorithm {
58         AES,
59     };
60 
61     @Param private Mode mode;
62 
63     public enum Mode {
64         CBC,
65         CFB,
66         CTR,
67         ECB,
68         OFB,
69     };
70 
71     @Param private Padding padding;
72 
73     public enum Padding {
74         NOPADDING,
75         PKCS1PADDING,
76     };
77 
78     @Param({"128", "192", "256"}) private int keySize;
79 
80     @Param({"16", "32", "64", "128", "1024", "8192"}) private int inputSize;
81 
82     @Param private Implementation implementation;
83 
84     public enum Implementation { OpenSSL, BouncyCastle };
85 
86     private String providerName;
87 
88     // Key generation isn't part of the benchmark so cache the results
89     private static Map<Integer, SecretKey> KEY_SIZES = new HashMap<Integer, SecretKey>();
90 
91     private String cipherAlgorithm;
92     private SecretKey key;
93 
94     private byte[] output = new byte[DATA.length];
95 
96     private Cipher cipherEncrypt;
97 
98     private Cipher cipherDecrypt;
99 
100     private AlgorithmParameterSpec spec;
101 
setUp()102     @Override protected void setUp() throws Exception {
103         cipherAlgorithm = algorithm.toString() + "/" + mode.toString() + "/"
104                 + padding.toString();
105 
106         String keyAlgorithm = algorithm.toString();
107         key = KEY_SIZES.get(keySize);
108         if (key == null) {
109             KeyGenerator generator = KeyGenerator.getInstance(keyAlgorithm);
110             generator.init(keySize);
111             key = generator.generateKey();
112             KEY_SIZES.put(keySize, key);
113         }
114 
115         switch (implementation) {
116             case OpenSSL:
117                 providerName = "AndroidOpenSSL";
118                 break;
119             case BouncyCastle:
120                 providerName = "BC";
121                 break;
122             default:
123                 throw new RuntimeException(implementation.toString());
124         }
125 
126         if (mode != Mode.ECB) {
127             spec = new IvParameterSpec(IV);
128         }
129 
130         cipherEncrypt = Cipher.getInstance(cipherAlgorithm, providerName);
131         cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, spec);
132 
133         cipherDecrypt = Cipher.getInstance(cipherAlgorithm, providerName);
134         cipherDecrypt.init(Cipher.DECRYPT_MODE, key, spec);
135     }
136 
timeEncrypt(int reps)137     public void timeEncrypt(int reps) throws Exception {
138         for (int i = 0; i < reps; ++i) {
139             cipherEncrypt.doFinal(DATA, 0, inputSize, output);
140         }
141     }
142 
timeDecrypt(int reps)143     public void timeDecrypt(int reps) throws Exception {
144         for (int i = 0; i < reps; ++i) {
145             cipherDecrypt.doFinal(DATA, 0, inputSize, output);
146         }
147     }
148 }
149