Home | History | Annotate | Download | only in regression
      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 
     17 package benchmarks.regression;
     18 
     19 import com.google.caliper.BeforeExperiment;
     20 import com.google.caliper.Param;
     21 import java.security.KeyPair;
     22 import java.security.KeyPairGenerator;
     23 import java.security.PrivateKey;
     24 import java.security.PublicKey;
     25 import java.security.Signature;
     26 import java.util.HashMap;
     27 import java.util.Map;
     28 
     29 /**
     30  * Tests RSA and DSA signature creation and verification.
     31  */
     32 public class SignatureBenchmark {
     33 
     34     private static final int DATA_SIZE = 8192;
     35     private static final byte[] DATA = new byte[DATA_SIZE];
     36     static {
     37         for (int i = 0; i < DATA_SIZE; i++) {
     38             DATA[i] = (byte)i;
     39         }
     40     }
     41     @Param private Algorithm algorithm;
     42 
     43     public enum Algorithm {
     44         MD5WithRSA,
     45         SHA1WithRSA,
     46         SHA256WithRSA,
     47         SHA384WithRSA,
     48         SHA512WithRSA,
     49         SHA1withDSA
     50     };
     51 
     52     @Param private Implementation implementation;
     53 
     54     public enum Implementation { OpenSSL, BouncyCastle };
     55 
     56     // Key generation and signing aren't part of the benchmark for verification
     57     // so cache the results
     58     private static Map<String,KeyPair> KEY_PAIRS = new HashMap<String,KeyPair>();
     59     private static Map<String,byte[]> SIGNATURES = new HashMap<String,byte[]>();
     60 
     61     private String signatureAlgorithm;
     62     private byte[] signature;
     63     private PrivateKey privateKey;
     64     private PublicKey publicKey;
     65 
     66     @BeforeExperiment
     67     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