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.KeyGenerator;
33 import javax.crypto.NoSuchPaddingException;
34 import java.security.InvalidAlgorithmParameterException;
35 import java.security.InvalidKeyException;
36 import java.security.Key;
37 import java.security.NoSuchAlgorithmException;
38 
39 public class DESedeBench extends CryptoBase {
40 
41     public static final int SET_SIZE = 128;
42 
43     @Param({"DESede/ECB/NoPadding", "DESede/ECB/PKCS5Padding", "DESede/CBC/NoPadding","DESede/CBC/PKCS5Padding"})
44     private String algorithm;
45 
46     @Param({"168"})
47     private int keyLength;
48 
49     @Param({""+16*1024})
50     private int dataSize;
51 
52     byte[][] data;
53     byte[][] encryptedData;
54     private Cipher encryptCipher;
55     private Cipher decryptCipher;
56     int index = 0;
57 
58     @Setup
setup()59     public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
60         setupProvider();
61         KeyGenerator kg = KeyGenerator.getInstance("DESede");
62         kg.init(keyLength);
63         Key key = kg.generateKey();
64         encryptCipher = makeCipher(prov, algorithm);
65         encryptCipher.init(Cipher.ENCRYPT_MODE, key);
66         decryptCipher = makeCipher(prov, algorithm);
67         decryptCipher.init(Cipher.DECRYPT_MODE, key, encryptCipher.getParameters());
68         data = fillRandom(new byte[SET_SIZE][dataSize]);
69         encryptedData = fillEncrypted(data, encryptCipher);
70     }
71 
72     @Benchmark
encrypt()73     public byte[] encrypt() throws BadPaddingException, IllegalBlockSizeException {
74         byte[] d = data[index];
75         index = (index +1) % SET_SIZE;
76         return encryptCipher.doFinal(d);
77     }
78 
79     @Benchmark
decrypt()80     public byte[] decrypt() throws BadPaddingException, IllegalBlockSizeException {
81         byte[] e = encryptedData[index];
82         index = (index +1) % SET_SIZE;
83         return decryptCipher.doFinal(e);
84     }
85 
86 }
87