New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8269827: JMH tests for AES/GCM byte[] and bytebuffers #4672
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
test/micro/org/openjdk/bench/javax/crypto/full/AESGCMByteBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. 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. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* 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.javax.crypto.full; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Setup; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.GCMParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* This performance tests runs AES/GCM encryption and decryption using heap and | ||
* direct ByteBuffers as input and output buffers for single and multi-part | ||
* operations. | ||
* | ||
* This test rotates the IV and creates a new GCMParameterSpec for each encrypt | ||
* benchmark operation | ||
*/ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to create a "small" version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I debated it.. I'm still not sure, but I guess I could. |
||
public class AESGCMByteBuffer extends CryptoBase { | ||
|
||
@Param({"128"}) | ||
private int keyLength; | ||
|
||
@Param({"1024", "1500", "4096", "16384"}) | ||
private int dataSize; | ||
|
||
@Param({"direct", "heap"}) | ||
private String dataMethod; | ||
|
||
byte[] data; | ||
ByteBuffer encryptedData; | ||
ByteBuffer in, out; | ||
private Cipher encryptCipher; | ||
private Cipher decryptCipher; | ||
SecretKeySpec ks; | ||
GCMParameterSpec gcm_spec; | ||
byte[] iv; | ||
|
||
private static final int IV_BUFFER_SIZE = 32; | ||
private static final int IV_MODULO = IV_BUFFER_SIZE - 16; | ||
int iv_index = 0; | ||
int updateLen = 0; | ||
|
||
private int next_iv_index() { | ||
int r = iv_index; | ||
iv_index = (iv_index + 1) % IV_MODULO; | ||
return r; | ||
} | ||
|
||
@Setup | ||
public void setup() throws Exception { | ||
setupProvider(); | ||
|
||
// Setup key material | ||
byte[] keystring = fillSecureRandom(new byte[keyLength / 8]); | ||
ks = new SecretKeySpec(keystring, "AES"); | ||
iv = fillSecureRandom(new byte[IV_BUFFER_SIZE]); | ||
gcm_spec = new GCMParameterSpec(96, iv, next_iv_index(), 16); | ||
|
||
// Setup Cipher classes | ||
encryptCipher = makeCipher(prov, "AES/GCM/NoPadding"); | ||
encryptCipher.init(Cipher.ENCRYPT_MODE, ks, gcm_spec); | ||
decryptCipher = makeCipher(prov, "AES/GCM/NoPadding"); | ||
decryptCipher.init(Cipher.DECRYPT_MODE, ks, | ||
encryptCipher.getParameters(). | ||
getParameterSpec(GCMParameterSpec.class)); | ||
|
||
// Setup input/output buffers | ||
data = fillRandom(new byte[dataSize]); | ||
if (dataMethod.equalsIgnoreCase("direct")) { | ||
in = ByteBuffer.allocateDirect(data.length); | ||
in.put(data); | ||
in.flip(); | ||
encryptedData = ByteBuffer.allocateDirect( | ||
encryptCipher.getOutputSize(data.length)); | ||
out = ByteBuffer.allocateDirect(encryptedData.capacity()); | ||
} else if (dataMethod.equalsIgnoreCase("heap")) { | ||
in = ByteBuffer.wrap(data); | ||
encryptedData = ByteBuffer.allocate( | ||
encryptCipher.getOutputSize(data.length)); | ||
out = ByteBuffer.allocate(encryptedData.capacity()); | ||
} | ||
|
||
encryptCipher.doFinal(in, encryptedData); | ||
encryptedData.flip(); | ||
in.flip(); | ||
updateLen = in.remaining() / 2; | ||
} | ||
|
||
@Benchmark | ||
public void encrypt() throws Exception { | ||
gcm_spec = new GCMParameterSpec(96, iv, next_iv_index(), 16); | ||
encryptCipher.init(Cipher.ENCRYPT_MODE, ks, gcm_spec); | ||
encryptCipher.doFinal(in, out); | ||
out.flip(); | ||
in.flip(); | ||
} | ||
|
||
@Benchmark | ||
public void encryptMultiPart() throws Exception { | ||
gcm_spec = new GCMParameterSpec(96, iv, next_iv_index(), 16); | ||
encryptCipher.init(Cipher.ENCRYPT_MODE, ks, gcm_spec); | ||
in.limit(updateLen); | ||
encryptCipher.update(in, out); | ||
in.limit(in.capacity()); | ||
encryptCipher.doFinal(in, out); | ||
out.flip(); | ||
in.flip(); | ||
} | ||
|
||
@Benchmark | ||
public void decrypt() throws Exception { | ||
decryptCipher.init(Cipher.DECRYPT_MODE, ks, | ||
encryptCipher.getParameters(). | ||
getParameterSpec(GCMParameterSpec.class)); | ||
decryptCipher.doFinal(encryptedData, out); | ||
encryptedData.flip(); | ||
out.flip(); | ||
} | ||
|
||
@Benchmark | ||
public void decryptMultiPart() throws Exception { | ||
decryptCipher.init(Cipher.DECRYPT_MODE, ks, | ||
encryptCipher.getParameters(). | ||
getParameterSpec(GCMParameterSpec.class)); | ||
|
||
int len = encryptedData.remaining(); | ||
encryptedData.limit(updateLen); | ||
decryptCipher.update(encryptedData, out); | ||
encryptedData.limit(len); | ||
|
||
decryptCipher.doFinal(encryptedData, out); | ||
encryptedData.flip(); | ||
out.flip(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
test/micro/org/openjdk/bench/javax/crypto/small/AESGCMByteBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. 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.javax.crypto.small; | ||
|
||
import org.openjdk.jmh.annotations.Param; | ||
|
||
public class AESGCMByteBuffer extends | ||
org.openjdk.bench.javax.crypto.full.AESGCMByteBuffer { | ||
|
||
@Param({"128"}) | ||
private int keyLength; | ||
|
||
@Param({"1024"}) | ||
private int dataSize; | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not an expert on this, but why no more AAD?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a performance test there isn't any value in adding an AAD. It's data sitting in a buffer that an be easily replicated with a multipart operation