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