SignatureBenchmark.java revision ee41931d976501d0fb4516bd43919b9564558619
1/*
2 * Copyright (C) 2010 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.android.org.conscrypt.OpenSSLSignature;
20import com.google.caliper.Param;
21import com.google.caliper.SimpleBenchmark;
22import java.security.KeyPair;
23import java.security.KeyPairGenerator;
24import java.security.PrivateKey;
25import java.security.PublicKey;
26import java.security.Signature;
27import java.util.HashMap;
28import java.util.Map;
29
30/**
31 * Tests RSA and DSA signature creation and verification.
32 */
33public class SignatureBenchmark extends SimpleBenchmark {
34
35    private static final int DATA_SIZE = 8192;
36    private static final byte[] DATA = new byte[DATA_SIZE];
37    static {
38        for (int i = 0; i < DATA_SIZE; i++) {
39            DATA[i] = (byte)i;
40        }
41    }
42    @Param private Algorithm algorithm;
43
44    public enum Algorithm {
45        MD5WithRSA,
46        SHA1WithRSA,
47        SHA256WithRSA,
48        SHA384WithRSA,
49        SHA512WithRSA,
50        SHA1withDSA
51    };
52
53    @Param private Implementation implementation;
54
55    public enum Implementation { OpenSSL, BouncyCastle };
56
57    // Key generation and signing aren't part of the benchmark for verification
58    // so cache the results
59    private static Map<String,KeyPair> KEY_PAIRS = new HashMap<String,KeyPair>();
60    private static Map<String,byte[]> SIGNATURES = new HashMap<String,byte[]>();
61
62    private String signatureAlgorithm;
63    private byte[] signature;
64    private PrivateKey privateKey;
65    private PublicKey publicKey;
66
67    @Override protected void setUp() throws Exception {
68        this.signatureAlgorithm = algorithm.toString();
69
70        String keyAlgorithm = signatureAlgorithm.substring(signatureAlgorithm.length() - 3 ,
71                                                           signatureAlgorithm.length());
72        KeyPair keyPair = KEY_PAIRS.get(keyAlgorithm);
73        if (keyPair == null) {
74            KeyPairGenerator generator = KeyPairGenerator.getInstance(keyAlgorithm);
75            keyPair = generator.generateKeyPair();
76            KEY_PAIRS.put(keyAlgorithm, keyPair);
77        }
78        this.privateKey = keyPair.getPrivate();
79        this.publicKey = keyPair.getPublic();
80
81        this.signature = SIGNATURES.get(signatureAlgorithm);
82        if (this.signature == null) {
83            Signature signer = Signature.getInstance(signatureAlgorithm);
84            signer.initSign(keyPair.getPrivate());
85            signer.update(DATA);
86            this.signature = signer.sign();
87            SIGNATURES.put(signatureAlgorithm, signature);
88        }
89    }
90
91    public void timeSign(int reps) throws Exception {
92        for (int i = 0; i < reps; ++i) {
93            Signature signer;
94            switch (implementation) {
95                case OpenSSL:
96                    signer = Signature.getInstance(signatureAlgorithm, "AndroidOpenSSL");
97                    break;
98                case BouncyCastle:
99                    signer = Signature.getInstance(signatureAlgorithm, "BC");
100                    break;
101                default:
102                    throw new RuntimeException(implementation.toString());
103            }
104            signer.initSign(privateKey);
105            signer.update(DATA);
106            signer.sign();
107        }
108    }
109
110    public void timeVerify(int reps) throws Exception {
111        for (int i = 0; i < reps; ++i) {
112            Signature verifier;
113            switch (implementation) {
114                case OpenSSL:
115                    verifier = Signature.getInstance(signatureAlgorithm, "AndroidOpenSSL");
116                    break;
117                case BouncyCastle:
118                    verifier = Signature.getInstance(signatureAlgorithm, "BC");
119                    break;
120                default:
121                    throw new RuntimeException(implementation.toString());
122            }
123            verifier.initVerify(publicKey);
124            verifier.update(DATA);
125            verifier.verify(signature);
126        }
127    }
128}
129