1 /* 2 * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.bench.javax.crypto.full; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.Param; 27 import org.openjdk.jmh.annotations.Setup; 28 29 import java.security.InvalidKeyException; 30 import java.security.KeyPair; 31 import java.security.KeyPairGenerator; 32 import java.security.NoSuchAlgorithmException; 33 import java.security.PrivateKey; 34 import java.security.PublicKey; 35 import java.security.Signature; 36 import java.security.SignatureException; 37 import java.util.Random; 38 39 public class SignatureBench extends CryptoBase { 40 41 public static final int SET_SIZE = 128; 42 43 @Param({"SHA256withDSA"}) 44 private String algorithm; 45 46 @Param({"1024", "16384"}) 47 int dataSize; 48 49 @Param({"1024"}) 50 private int keyLength; 51 52 private PrivateKey privateKey; 53 private PublicKey publicKey; 54 private byte[][] data; 55 private byte[][] signedData; 56 int index; 57 58 getKeyPairGeneratorName()59 private String getKeyPairGeneratorName() { 60 int withIndex = algorithm.lastIndexOf("with"); 61 if (withIndex < 0) { 62 return algorithm; 63 } 64 String tail = algorithm.substring(withIndex + 4); 65 return "ECDSA".equals(tail) ? "EC" : tail; 66 } 67 68 @Setup() setup()69 public void setup() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { 70 setupProvider(); 71 KeyPairGenerator kpg = KeyPairGenerator.getInstance(getKeyPairGeneratorName()); 72 kpg.initialize(keyLength); 73 KeyPair keys = kpg.generateKeyPair(); 74 this.privateKey = keys.getPrivate(); 75 this.publicKey = keys.getPublic(); 76 data = fillRandom(new byte[SET_SIZE][dataSize]); 77 signedData = new byte[data.length][]; 78 for (int i = 0; i < data.length; i++) { 79 signedData[i] = sign(data[i]); 80 } 81 82 } 83 sign(byte[] data)84 public byte[] sign(byte[] data) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { 85 Signature signature = (prov == null) ? Signature.getInstance(algorithm) : Signature.getInstance(algorithm, prov); 86 signature.initSign(privateKey); 87 signature.update(data); 88 return signature.sign(); 89 } 90 91 @Benchmark sign()92 public byte[] sign() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { 93 byte[] d = data[index]; 94 index = (index + 1) % SET_SIZE; 95 return sign(d); 96 } 97 98 @Benchmark verify()99 public boolean verify() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { 100 Signature signature = (prov == null) ? Signature.getInstance(algorithm) : Signature.getInstance(algorithm, prov); 101 signature.initVerify(publicKey); 102 byte[] d = data[index]; 103 byte[] s = signedData[index]; 104 index = (index + 1) % SET_SIZE; 105 signature.update(d); 106 return signature.verify(s); 107 } 108 109 public static class RSA extends SignatureBench { 110 111 @Param({"MD5withRSA", "SHA256withRSA"}) 112 private String algorithm; 113 114 @Param({"1024", "2048", "3072"}) 115 private int keyLength; 116 117 } 118 119 public static class ECDSA extends SignatureBench { 120 121 @Param({"SHA256withECDSA"}) 122 private String algorithm; 123 124 @Param({"256"}) 125 private int keyLength; 126 127 } 128 129 public static class EdDSA extends SignatureBench { 130 131 @Param({"EdDSA"}) 132 private String algorithm; 133 134 @Param({"255", "448"}) 135 private int keyLength; 136 137 } 138 139 } 140