|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Google LLC. 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 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8371864 |
| 27 | + * @run main/othervm/timeout=600 TestGCMSplitBound |
| 28 | + * @requires (os.simpleArch == "x64" & (vm.cpu.features ~= ".*avx2.*" | |
| 29 | + * vm.cpu.features ~= ".*avx512.*")) |
| 30 | + * @summary Test GaloisCounterMode.implGCMCrypt0 AVX512/AVX2 intrinsics. |
| 31 | + */ |
| 32 | + |
| 33 | +import java.security.SecureRandom; |
| 34 | +import java.security.spec.AlgorithmParameterSpec; |
| 35 | +import java.time.Duration; |
| 36 | +import java.util.Arrays; |
| 37 | +import javax.crypto.Cipher; |
| 38 | +import javax.crypto.SecretKey; |
| 39 | +import javax.crypto.spec.GCMParameterSpec; |
| 40 | +import javax.crypto.spec.SecretKeySpec; |
| 41 | + |
| 42 | +public class TestGCMSplitBound { |
| 43 | + |
| 44 | + static final SecureRandom SECURE_RANDOM = newDefaultSecureRandom(); |
| 45 | + |
| 46 | + private static SecureRandom newDefaultSecureRandom() { |
| 47 | + SecureRandom retval = new SecureRandom(); |
| 48 | + retval.nextLong(); // force seeding |
| 49 | + return retval; |
| 50 | + } |
| 51 | + |
| 52 | + private static byte[] randBytes(int size) { |
| 53 | + byte[] rand = new byte[size]; |
| 54 | + SECURE_RANDOM.nextBytes(rand); |
| 55 | + return rand; |
| 56 | + } |
| 57 | + |
| 58 | + private static final int IV_SIZE_IN_BYTES = 12; |
| 59 | + private static final int TAG_SIZE_IN_BYTES = 16; |
| 60 | + |
| 61 | + private Cipher getCipher(final byte[] key, final byte[] aad, |
| 62 | + final byte[] nonce, int mode) |
| 63 | + throws Exception { |
| 64 | + SecretKey keySpec = new SecretKeySpec(key, "AES"); |
| 65 | + AlgorithmParameterSpec params = |
| 66 | + new GCMParameterSpec(8 * TAG_SIZE_IN_BYTES, nonce, 0, nonce.length); |
| 67 | + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 68 | + cipher.init(mode, keySpec, params); |
| 69 | + if (aad != null && aad.length != 0) { |
| 70 | + cipher.updateAAD(aad); |
| 71 | + } |
| 72 | + return cipher; |
| 73 | + } |
| 74 | + |
| 75 | + private byte[] gcmEncrypt(final byte[] key, final byte[] plaintext, |
| 76 | + final byte[] aad) |
| 77 | + throws Exception { |
| 78 | + byte[] nonce = randBytes(IV_SIZE_IN_BYTES); |
| 79 | + Cipher cipher = getCipher(key, aad, nonce, Cipher.ENCRYPT_MODE); |
| 80 | + int outputSize = cipher.getOutputSize(plaintext.length); |
| 81 | + int len = IV_SIZE_IN_BYTES + outputSize; |
| 82 | + byte[] output = new byte[len]; |
| 83 | + System.arraycopy(nonce, 0, output, 0, IV_SIZE_IN_BYTES); |
| 84 | + cipher.doFinal(plaintext, 0, plaintext.length, output, |
| 85 | + IV_SIZE_IN_BYTES); |
| 86 | + return output; |
| 87 | + } |
| 88 | + |
| 89 | + private byte[] gcmDecrypt(final byte[] key, final byte[] ciphertext, |
| 90 | + final byte[] aad) |
| 91 | + throws Exception { |
| 92 | + byte[] nonce = new byte[IV_SIZE_IN_BYTES]; |
| 93 | + System.arraycopy(ciphertext, 0, nonce, 0, IV_SIZE_IN_BYTES); |
| 94 | + Cipher cipher = getCipher(key, aad, nonce, Cipher.DECRYPT_MODE); |
| 95 | + return cipher.doFinal(ciphertext, IV_SIZE_IN_BYTES, |
| 96 | + ciphertext.length - IV_SIZE_IN_BYTES); |
| 97 | + } |
| 98 | + |
| 99 | + // x86-64 parallel intrinsic data size |
| 100 | + private static final int PARALLEL_LEN = 512; |
| 101 | + // max data size for x86-64 intrinsic |
| 102 | + private static final int SPLIT_LEN = 1048576; // 1MB |
| 103 | + |
| 104 | + private void encryptAndDecrypt(byte[] key, byte[] aad, byte[] message, |
| 105 | + int messageSize) |
| 106 | + throws Exception { |
| 107 | + byte[] ciphertext = gcmEncrypt(key, message, aad); |
| 108 | + byte[] decrypted = gcmDecrypt(key, ciphertext, aad); |
| 109 | + if (ciphertext == null) { |
| 110 | + throw new RuntimeException("ciphertext is null"); |
| 111 | + } |
| 112 | + if (Arrays.compare(decrypted, 0, messageSize, |
| 113 | + message, 0, messageSize) != 0) { |
| 114 | + throw new RuntimeException( |
| 115 | + "Decrypted message is different from the original message"); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void run() throws Exception { |
| 120 | + byte[] aad = randBytes(20); |
| 121 | + byte[] key = randBytes(16); |
| 122 | + // Force JIT. |
| 123 | + for (int i = 0; i < 100000; i++) { |
| 124 | + byte[] message = randBytes(PARALLEL_LEN); |
| 125 | + encryptAndDecrypt(key, aad, message, PARALLEL_LEN); |
| 126 | + } |
| 127 | + for (int messageSize = SPLIT_LEN - 300; messageSize <= SPLIT_LEN + 300; |
| 128 | + messageSize++) { |
| 129 | + byte[] message = randBytes(messageSize); |
| 130 | + try { |
| 131 | + encryptAndDecrypt(key, aad, message, messageSize); |
| 132 | + } catch (Exception e) { |
| 133 | + throw new RuntimeException("Failed for messageSize " |
| 134 | + + Integer.toHexString(messageSize), e); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public static void main(String[] args) throws Exception { |
| 140 | + TestGCMSplitBound test = new TestGCMSplitBound(); |
| 141 | + for (int i = 0; i < 3; i++) { |
| 142 | + test.run(); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments