1 /*
2  * Copyright (c) 2015, 2018, 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 javax.crypto.BadPaddingException;
30 import javax.crypto.Cipher;
31 import javax.crypto.IllegalBlockSizeException;
32 import javax.crypto.NoSuchPaddingException;
33 import java.security.InvalidKeyException;
34 import java.security.KeyPair;
35 import java.security.KeyPairGenerator;
36 import java.security.NoSuchAlgorithmException;
37 
38 public class RSABench extends CryptoBase {
39 
40     public static final int SET_SIZE = 128;
41 
42     @Param({"RSA/ECB/NoPadding", "RSA/ECB/PKCS1Padding", "RSA/ECB/OAEPPadding"})
43     protected String algorithm;
44 
45     @Param({"32"})
46     protected int dataSize;
47 
48     @Param({"1024", "2048", "3072"})
49     protected int keyLength;
50 
51 
52     private byte[][] data;
53     private byte[][] encryptedData;
54 
55     private Cipher encryptCipher;
56     private Cipher decryptCipher;
57     private int index = 0;
58 
59     @Setup()
setup()60     public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
61         setupProvider();
62         data = fillRandom(new byte[SET_SIZE][dataSize]);
63 
64         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
65         kpg.initialize(keyLength);
66         KeyPair keyPair = kpg.generateKeyPair();
67 
68         encryptCipher = makeCipher(prov, algorithm);
69         encryptCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
70 
71         decryptCipher = makeCipher(prov, algorithm);
72         decryptCipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
73         encryptedData = fillEncrypted(data, encryptCipher);
74 
75     }
76 
77     @Benchmark
encrypt()78     public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException {
79         byte[] d = data[index];
80         index = (index +1) % SET_SIZE;
81         return encryptCipher.doFinal(d);
82     }
83 
84     @Benchmark
decrypt()85     public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException {
86         byte[] e = encryptedData[index];
87         index = (index +1) % SET_SIZE;
88         return decryptCipher.doFinal(e);
89     }
90 
91     public static class Extra extends RSABench {
92 
93         @Param({"RSA/ECB/NoPadding", "RSA/ECB/PKCS1Padding"})
94         private String algorithm;
95 
96         @Param({"214"})
97         private int dataSize;
98 
99         @Param({"2048", "3072"})
100         private int keyLength;
101 
102     }
103 
104 }
105