|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Red Hat, Inc. |
| 3 | + * |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +import javax.crypto.Cipher; |
| 26 | +import javax.crypto.spec.IvParameterSpec; |
| 27 | +import javax.crypto.spec.SecretKeySpec; |
| 28 | +import java.nio.ByteBuffer; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.security.Key; |
| 31 | +import java.security.Provider; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.HexFormat; |
| 34 | +import java.util.stream.IntStream; |
| 35 | + |
| 36 | +/* |
| 37 | + * @test |
| 38 | + * @bug 8330842 |
| 39 | + * @summary test AES CTS multipart operations with SunPKCS11 |
| 40 | + * @library /test/lib .. |
| 41 | + * @run main/othervm/timeout=120 TestCipherTextStealingMultipart |
| 42 | + */ |
| 43 | + |
| 44 | +public class TestCipherTextStealingMultipart extends PKCS11Test { |
| 45 | + private static final String LF = System.lineSeparator(); |
| 46 | + private static final String ALGORITHM = "AES/CTS/NoPadding"; |
| 47 | + private static final int BLOCK_SIZE = 16; |
| 48 | + private static final Key KEY = |
| 49 | + new SecretKeySpec("AbCdEfGhIjKlMnOp".getBytes(), "AES"); |
| 50 | + private static final IvParameterSpec IV = |
| 51 | + new IvParameterSpec("1234567890aBcDeF".getBytes()); |
| 52 | + |
| 53 | + private static final StringBuilder chunksDesc = new StringBuilder(); |
| 54 | + private static Provider sunPKCS11; |
| 55 | + private static Cipher sunJCECipher; |
| 56 | + |
| 57 | + private static byte[][] generateChunks(int totalLength, int[] chunkSizes) { |
| 58 | + chunksDesc.setLength(0); |
| 59 | + chunksDesc.append("Testing with ").append(totalLength) |
| 60 | + .append(" bytes distributed in ").append(chunkSizes.length) |
| 61 | + .append(" multipart updates:").append(LF); |
| 62 | + int byteIdx = 0; |
| 63 | + byte[][] plaintextChunks = new byte[chunkSizes.length][]; |
| 64 | + for (int chunkIdx = 0; chunkIdx < chunkSizes.length; chunkIdx++) { |
| 65 | + byte[] chunk = new byte[chunkSizes[chunkIdx]]; |
| 66 | + for (int i = 0; i < chunk.length; i++) { |
| 67 | + chunk[i] = (byte) ('A' + byteIdx++ / BLOCK_SIZE); |
| 68 | + } |
| 69 | + chunksDesc.append(" ").append(repr(chunk)).append(LF); |
| 70 | + plaintextChunks[chunkIdx] = chunk; |
| 71 | + } |
| 72 | + return plaintextChunks; |
| 73 | + } |
| 74 | + |
| 75 | + private static byte[] computeExpected(byte[] jointPlaintext) |
| 76 | + throws Exception { |
| 77 | + byte[] ciphertext = sunJCECipher.doFinal(jointPlaintext); |
| 78 | + if (ciphertext.length != jointPlaintext.length) { |
| 79 | + throw new Exception("In CTS mode, ciphertext and plaintext should" + |
| 80 | + " have the same length. However, SunJCE's CTS cipher " + |
| 81 | + "returned a ciphertext of " + ciphertext.length + " bytes" + |
| 82 | + " and plaintext has " + jointPlaintext.length + " bytes."); |
| 83 | + } |
| 84 | + return ciphertext; |
| 85 | + } |
| 86 | + |
| 87 | + private static byte[] join(byte[][] inputChunks, int totalLength) { |
| 88 | + ByteBuffer outputBuf = ByteBuffer.allocate(totalLength); |
| 89 | + for (byte[] inputChunk : inputChunks) { |
| 90 | + outputBuf.put(inputChunk); |
| 91 | + } |
| 92 | + return outputBuf.array(); |
| 93 | + } |
| 94 | + |
| 95 | + private static byte[][] split(byte[] input, int[] chunkSizes) { |
| 96 | + ByteBuffer inputBuf = ByteBuffer.wrap(input); |
| 97 | + byte[][] outputChunks = new byte[chunkSizes.length][]; |
| 98 | + for (int chunkIdx = 0; chunkIdx < chunkSizes.length; chunkIdx++) { |
| 99 | + byte[] chunk = new byte[chunkSizes[chunkIdx]]; |
| 100 | + inputBuf.get(chunk); |
| 101 | + outputChunks[chunkIdx] = chunk; |
| 102 | + } |
| 103 | + return outputChunks; |
| 104 | + } |
| 105 | + |
| 106 | + private enum CheckType {CIPHERTEXT, PLAINTEXT} |
| 107 | + |
| 108 | + private enum OutputType {BYTE_ARRAY, DIRECT_BYTE_BUFFER} |
| 109 | + |
| 110 | + private static void check(CheckType checkType, OutputType outputType, |
| 111 | + byte[] expected, ByteBuffer actualBuf) throws Exception { |
| 112 | + byte[] actual; |
| 113 | + if (actualBuf.hasArray()) { |
| 114 | + actual = actualBuf.array(); |
| 115 | + } else { |
| 116 | + actual = new byte[actualBuf.position()]; |
| 117 | + actualBuf.position(0).get(actual); |
| 118 | + } |
| 119 | + if (!Arrays.equals(actual, expected)) { |
| 120 | + throw new Exception("After " + switch (checkType) { |
| 121 | + case CIPHERTEXT -> "encrypting"; |
| 122 | + case PLAINTEXT -> "decrypting"; |
| 123 | + } + " into a " + switch (outputType) { |
| 124 | + case BYTE_ARRAY -> "byte[]"; |
| 125 | + case DIRECT_BYTE_BUFFER -> "direct ByteBuffer"; |
| 126 | + } + ", " + checkType.name().toLowerCase() + "s don't match:" + LF + |
| 127 | + " Expected: " + repr(expected) + LF + |
| 128 | + " Actual: " + repr(actual)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static ByteBuffer encryptOrDecryptMultipart(int operation, |
| 133 | + OutputType outputType, byte[][] inputChunks, int totalLength) |
| 134 | + throws Exception { |
| 135 | + Cipher cipher = Cipher.getInstance(ALGORITHM, sunPKCS11); |
| 136 | + cipher.init(operation, KEY, IV); |
| 137 | + ByteBuffer output = null; |
| 138 | + int outOfs = 1; |
| 139 | + switch (outputType) { |
| 140 | + case BYTE_ARRAY -> { |
| 141 | + output = ByteBuffer.allocate(totalLength); |
| 142 | + for (byte[] inputChunk : inputChunks) { |
| 143 | + output.put(cipher.update(inputChunk)); |
| 144 | + } |
| 145 | + // Check that the output array offset does not affect the |
| 146 | + // penultimate block length calculation. |
| 147 | + byte[] tmpOut = new byte[cipher.getOutputSize(0) + outOfs]; |
| 148 | + cipher.doFinal(tmpOut, outOfs); |
| 149 | + output.put(tmpOut, outOfs, tmpOut.length - outOfs); |
| 150 | + } |
| 151 | + case DIRECT_BYTE_BUFFER -> { |
| 152 | + output = ByteBuffer.allocateDirect(totalLength); |
| 153 | + for (byte[] inputChunk : inputChunks) { |
| 154 | + cipher.update(ByteBuffer.wrap(inputChunk), output); |
| 155 | + } |
| 156 | + // Check that the output array offset does not affect the |
| 157 | + // penultimate block length calculation. |
| 158 | + ByteBuffer tmpOut = ByteBuffer.allocateDirect( |
| 159 | + cipher.getOutputSize(0) + outOfs); |
| 160 | + tmpOut.position(outOfs); |
| 161 | + cipher.doFinal(ByteBuffer.allocate(0), tmpOut); |
| 162 | + tmpOut.position(outOfs); |
| 163 | + output.put(tmpOut); |
| 164 | + } |
| 165 | + } |
| 166 | + return output; |
| 167 | + } |
| 168 | + |
| 169 | + private static void doMultipart(int... chunkSizes) throws Exception { |
| 170 | + int totalLength = IntStream.of(chunkSizes).sum(); |
| 171 | + byte[][] plaintextChunks = generateChunks(totalLength, chunkSizes); |
| 172 | + byte[] jointPlaintext = join(plaintextChunks, totalLength); |
| 173 | + byte[] expectedCiphertext = computeExpected(jointPlaintext); |
| 174 | + byte[][] ciphertextChunks = split(expectedCiphertext, chunkSizes); |
| 175 | + |
| 176 | + for (OutputType outputType : OutputType.values()) { |
| 177 | + // Encryption test |
| 178 | + check(CheckType.CIPHERTEXT, outputType, expectedCiphertext, |
| 179 | + encryptOrDecryptMultipart(Cipher.ENCRYPT_MODE, outputType, |
| 180 | + plaintextChunks, totalLength)); |
| 181 | + // Decryption test |
| 182 | + check(CheckType.PLAINTEXT, outputType, jointPlaintext, |
| 183 | + encryptOrDecryptMultipart(Cipher.DECRYPT_MODE, outputType, |
| 184 | + ciphertextChunks, totalLength)); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private static String repr(byte[] data) { |
| 189 | + if (data == null) { |
| 190 | + return "<null>"; |
| 191 | + } |
| 192 | + if (data.length == 0) { |
| 193 | + return "<empty []>"; |
| 194 | + } |
| 195 | + String lenRepr = " (" + data.length + " bytes)"; |
| 196 | + for (byte b : data) { |
| 197 | + if (b < 32 || b > 126) { |
| 198 | + return HexFormat.ofDelimiter(":").formatHex(data) + lenRepr; |
| 199 | + } |
| 200 | + } |
| 201 | + return new String(data, StandardCharsets.US_ASCII) + lenRepr; |
| 202 | + } |
| 203 | + |
| 204 | + private static void initialize() throws Exception { |
| 205 | + sunJCECipher = Cipher.getInstance(ALGORITHM, "SunJCE"); |
| 206 | + sunJCECipher.init(Cipher.ENCRYPT_MODE, KEY, IV); |
| 207 | + } |
| 208 | + |
| 209 | + public static void main(String[] args) throws Exception { |
| 210 | + initialize(); |
| 211 | + main(new TestCipherTextStealingMultipart(), args); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public void main(Provider p) throws Exception { |
| 216 | + sunPKCS11 = p; |
| 217 | + try { |
| 218 | + // Test relevant combinations for 2, 3, and 4 update operations |
| 219 | + int aesBSize = 16; |
| 220 | + int[] points = new int[]{1, aesBSize - 1, aesBSize, aesBSize + 1}; |
| 221 | + for (int size1 : points) { |
| 222 | + for (int size2 : points) { |
| 223 | + if (size1 + size2 >= aesBSize) { |
| 224 | + doMultipart(size1, size2); |
| 225 | + } |
| 226 | + for (int size3 : points) { |
| 227 | + if (size1 + size2 + size3 >= aesBSize) { |
| 228 | + doMultipart(size1, size2, size3); |
| 229 | + } |
| 230 | + for (int size4 : points) { |
| 231 | + if (size1 + size2 + size3 + size4 >= aesBSize) { |
| 232 | + doMultipart(size1, size2, size3, size4); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + doMultipart(17, 17, 17, 17, 17); |
| 239 | + doMultipart(4, 2, 7, 1, 6, 12); |
| 240 | + doMultipart(2, 15, 21, 26, 31, 26, 5, 30); |
| 241 | + doMultipart(7, 12, 26, 8, 15, 2, 17, 16, 21, 2, 32, 29); |
| 242 | + doMultipart(6, 7, 6, 1, 5, 16, 14, 1, 10, 16, 17, 8, 1, 13, 12); |
| 243 | + doMultipart(16, 125, 19, 32, 32, 16, 17, |
| 244 | + 31, 19, 13, 16, 16, 32, 16, 16); |
| 245 | + doMultipart(5, 30, 11, 9, 6, 14, 20, 6, |
| 246 | + 5, 18, 31, 33, 15, 29, 7, 9); |
| 247 | + doMultipart(105, 8, 21, 27, 30, 101, 15, 20, |
| 248 | + 23, 33, 26, 6, 8, 2, 13, 17); |
| 249 | + } catch (Exception e) { |
| 250 | + System.out.print(chunksDesc); |
| 251 | + throw e; |
| 252 | + } |
| 253 | + System.out.println("TEST PASS - OK"); |
| 254 | + } |
| 255 | +} |
0 commit comments