mirror of
https://github.com/openjdk/jdk.git
synced 2026-01-28 20:18:48 +00:00
258 lines
8.7 KiB
Java
258 lines
8.7 KiB
Java
/*
|
|
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
|
|
* Copyright (C) 2022, Tencent. All rights reserved.
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
*
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
* accompanied this code).
|
|
*
|
|
* You should have received a copy of the GNU General Public License version
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
*
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
* questions.
|
|
*/
|
|
package org.openjdk.bench.java.security;
|
|
|
|
import org.openjdk.jmh.annotations.*;
|
|
|
|
import java.security.*;
|
|
import java.security.spec.*;
|
|
import java.util.Random;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@BenchmarkMode(Mode.Throughput)
|
|
@OutputTimeUnit(TimeUnit.SECONDS)
|
|
@Warmup(iterations = 5, time = 1)
|
|
@Measurement(iterations = 5, time = 1)
|
|
@Fork(jvmArgs = {"-Xms1024m", "-Xmx1024m", "-Xmn768m", "-XX:+UseParallelGC"}, value = 3)
|
|
public class Signatures {
|
|
|
|
@State(Scope.Benchmark)
|
|
public static class test01 {
|
|
@Param({"64", "512", "2048", "16384"})
|
|
private int messageLength;
|
|
@Param({"secp256r1", "secp384r1", "secp521r1"})
|
|
private String algorithm;
|
|
}
|
|
@State(Scope.Benchmark)
|
|
public static class test02 {
|
|
@Param({"64", "512", "2048", "16384"})
|
|
private int messageLength;
|
|
@Param({"Ed25519", "Ed448"})
|
|
private String algorithm;
|
|
}
|
|
@State(Scope.Benchmark)
|
|
public static class test03 {
|
|
@Param({"64", "512", "2048", "16384"})
|
|
private int messageLength;
|
|
@Param({"SHA256withDSA", "SHA384withDSA", "SHA512withDSA"})
|
|
private String algorithm;
|
|
}
|
|
@State(Scope.Benchmark)
|
|
public static class test04 {
|
|
@Param({"64", "512", "2048", "16384"})
|
|
private int messageLength;
|
|
@Param({"SHA256withRSA", "SHA384withRSA", "SHA512withRSA"})
|
|
private String algorithm;
|
|
}
|
|
@State(Scope.Benchmark)
|
|
public static class test05 {
|
|
@Param({"64", "512", "2048", "16384"})
|
|
private int messageLength;
|
|
@Param({"SHA256", "SHA384", "SHA512"})
|
|
private String algorithm;
|
|
}
|
|
|
|
@Benchmark
|
|
public byte[] ECDSA(s1 state) throws Exception {
|
|
state.signer.update(state.message);
|
|
return state.signer.sign();
|
|
}
|
|
|
|
@Benchmark
|
|
public byte[] EdDSA(s2 state) throws Exception {
|
|
state.signer.update(state.message);
|
|
return state.signer.sign();
|
|
}
|
|
|
|
@Benchmark
|
|
public byte[] DSA(s3 state) throws Exception {
|
|
state.signer.update(state.message);
|
|
return state.signer.sign();
|
|
}
|
|
|
|
@Benchmark
|
|
public byte[] RSA(s4 state) throws Exception {
|
|
state.signer.update(state.message);
|
|
return state.signer.sign();
|
|
}
|
|
|
|
@Benchmark
|
|
public byte[] RSASSAPSS(s5 state) throws Exception {
|
|
state.signer.update(state.message);
|
|
return state.signer.sign();
|
|
}
|
|
|
|
@State(Scope.Thread)
|
|
public static class s1 {
|
|
private Signature signer;
|
|
private byte[] message;
|
|
|
|
@Setup
|
|
public void setup(test01 test) throws Exception {
|
|
message = new byte[test.messageLength];
|
|
(new Random(System.nanoTime())).nextBytes(message);
|
|
|
|
String signName = switch (test.algorithm) {
|
|
case "secp256r1" -> "SHA256withECDSA";
|
|
case "secp384r1" -> "SHA384withECDSA";
|
|
case "secp521r1" -> "SHA512withECDSA";
|
|
default -> throw new RuntimeException();
|
|
};
|
|
|
|
AlgorithmParameters params =
|
|
AlgorithmParameters.getInstance("EC", "SunEC");
|
|
params.init(new ECGenParameterSpec(test.algorithm));
|
|
ECGenParameterSpec ecParams =
|
|
params.getParameterSpec(ECGenParameterSpec.class);
|
|
|
|
KeyPairGenerator kpg =
|
|
KeyPairGenerator.getInstance("EC", "SunEC");
|
|
kpg.initialize(ecParams);
|
|
KeyPair kp = kpg.generateKeyPair();
|
|
|
|
signer = Signature.getInstance(signName, "SunEC");
|
|
signer.initSign(kp.getPrivate());
|
|
}
|
|
}
|
|
|
|
@State(Scope.Thread)
|
|
public static class s2 {
|
|
private Signature signer;
|
|
private byte[] message;
|
|
|
|
@Setup
|
|
public void setup(test02 test) throws Exception {
|
|
message = new byte[test.messageLength];
|
|
(new Random(System.nanoTime())).nextBytes(message);
|
|
|
|
KeyPairGenerator kpg =
|
|
KeyPairGenerator.getInstance(test.algorithm, "SunEC");
|
|
NamedParameterSpec spec = new NamedParameterSpec(test.algorithm);
|
|
kpg.initialize(spec);
|
|
KeyPair kp = kpg.generateKeyPair();
|
|
|
|
signer = Signature.getInstance(test.algorithm, "SunEC");
|
|
signer.initSign(kp.getPrivate());
|
|
}
|
|
}
|
|
|
|
@State(Scope.Thread)
|
|
public static class s3 {
|
|
private Signature signer;
|
|
private byte[] message;
|
|
|
|
@Setup
|
|
public void setup(test03 test) throws Exception {
|
|
message = new byte[test.messageLength];
|
|
(new Random(System.nanoTime())).nextBytes(message);
|
|
|
|
int keyLength = switch (test.algorithm) {
|
|
case "SHA256withDSA" -> 2048;
|
|
case "SHA384withDSA" -> 3072;
|
|
case "SHA512withDSA" -> 3072;
|
|
default -> throw new RuntimeException();
|
|
};
|
|
|
|
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
|
|
kpg.initialize(keyLength);
|
|
KeyPair kp = kpg.generateKeyPair();
|
|
|
|
signer = Signature.getInstance(test.algorithm);
|
|
signer.initSign(kp.getPrivate());
|
|
}
|
|
}
|
|
|
|
@State(Scope.Thread)
|
|
public static class s4 {
|
|
private Signature signer;
|
|
private byte[] message;
|
|
|
|
@Setup
|
|
public void setup(test04 test) throws Exception {
|
|
message = new byte[test.messageLength];
|
|
(new Random(System.nanoTime())).nextBytes(message);
|
|
|
|
int keyLength = switch (test.algorithm) {
|
|
case "SHA256withRSA" -> 2048;
|
|
case "SHA384withRSA" -> 3072;
|
|
case "SHA512withRSA" -> 4096;
|
|
default -> throw new RuntimeException();
|
|
};
|
|
|
|
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
|
|
kpg.initialize(keyLength);
|
|
KeyPair kp = kpg.generateKeyPair();
|
|
|
|
signer = Signature.getInstance(test.algorithm);
|
|
signer.initSign(kp.getPrivate());
|
|
}
|
|
}
|
|
|
|
@State(Scope.Thread)
|
|
public static class s5 {
|
|
private Signature signer;
|
|
private byte[] message;
|
|
|
|
@Setup
|
|
public void setup(test05 test) throws Exception {
|
|
message = new byte[test.messageLength];
|
|
(new Random(System.nanoTime())).nextBytes(message);
|
|
|
|
int keyLength = switch (test.algorithm) {
|
|
case "SHA256" -> 2048;
|
|
case "SHA384" -> 3072;
|
|
case "SHA512" -> 4096;
|
|
default -> throw new RuntimeException();
|
|
};
|
|
|
|
PSSParameterSpec spec = switch (test.algorithm) {
|
|
case "SHA256" ->
|
|
new PSSParameterSpec(
|
|
"SHA-256", "MGF1",
|
|
MGF1ParameterSpec.SHA256,
|
|
32, PSSParameterSpec.TRAILER_FIELD_BC);
|
|
case "SHA384" ->
|
|
new PSSParameterSpec(
|
|
"SHA-384", "MGF1",
|
|
MGF1ParameterSpec.SHA384,
|
|
48, PSSParameterSpec.TRAILER_FIELD_BC);
|
|
case "SHA512" ->
|
|
new PSSParameterSpec(
|
|
"SHA-512", "MGF1",
|
|
MGF1ParameterSpec.SHA512,
|
|
64, PSSParameterSpec.TRAILER_FIELD_BC);
|
|
default -> throw new RuntimeException();
|
|
};
|
|
|
|
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSASSA-PSS");
|
|
kpg.initialize(keyLength);
|
|
KeyPair kp = kpg.generateKeyPair();
|
|
|
|
signer = Signature.getInstance("RSASSA-PSS");
|
|
signer.setParameter(spec);
|
|
signer.initSign(kp.getPrivate());
|
|
}
|
|
}
|
|
}
|