4791N/A/*
4791N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4791N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4791N/A *
4791N/A * This code is free software; you can redistribute it and/or modify it
4791N/A * under the terms of the GNU General Public License version 2 only, as
4791N/A * published by the Free Software Foundation.
4791N/A *
4791N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4791N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4791N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4791N/A * version 2 for more details (a copy is included in the LICENSE file that
4791N/A * accompanied this code).
4791N/A *
4791N/A * You should have received a copy of the GNU General Public License version
4791N/A * 2 along with this work; if not, write to the Free Software Foundation,
4791N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4791N/A *
4791N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4791N/A * or visit www.oracle.com if you need additional information or have any
4791N/A * questions.
4791N/A */
4791N/A
4791N/A/**
4791N/A * @test
4791N/A * @bug 7142509
4791N/A * @summary Cipher.doFinal(ByteBuffer,ByteBuffer) fails to
4791N/A * process when in.remaining() == 0
4791N/A */
4791N/A
4791N/Aimport java.nio.ByteBuffer;
4791N/Aimport java.security.SecureRandom;
4791N/Aimport java.util.Arrays;
4791N/Aimport java.util.Random;
4791N/A
4791N/Aimport javax.crypto.Cipher;
4791N/Aimport javax.crypto.SecretKey;
4791N/Aimport javax.crypto.spec.SecretKeySpec;
4791N/A
4791N/A/*
4791N/A * Simple test case to show that Cipher.doFinal(ByteBuffer, ByteBuffer) fails to
4791N/A * process the data internally buffered inBB the cipher when input.remaining()
4791N/A * == 0 and at least one buffer is a direct buffer.
4791N/A */
4791N/Apublic class DirectBBRemaining {
4791N/A
4791N/A private static Random random = new SecureRandom();
4791N/A private static int testSizes = 40;
4791N/A private static int outputFrequency = 5;
4791N/A
4791N/A public static void main(String args[]) throws Exception {
4791N/A boolean failedOnce = false;
4791N/A Exception failedReason = null;
4791N/A
4791N/A byte[] keyBytes = new byte[8];
4791N/A random.nextBytes(keyBytes);
4791N/A SecretKey key = new SecretKeySpec(keyBytes, "DES");
4791N/A
4791N/A Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", "SunJCE");
4791N/A cipher.init(Cipher.ENCRYPT_MODE, key);
4791N/A
4791N/A /*
4791N/A * Iterate through various sizes to make sure that the code does empty
4791N/A * blocks, single partial blocks, 1 full block, full + partial blocks,
4791N/A * multiple full blocks, etc. 5 blocks (using DES) is probably overkill
4791N/A * but will feel more confident the fix isn't breaking anything.
4791N/A */
4791N/A System.out.println("Output test results for every "
4791N/A + outputFrequency + " tests...");
4791N/A
4791N/A for (int size = 0; size <= testSizes; size++) {
4791N/A boolean output = (size % outputFrequency) == 0;
4791N/A if (output) {
4791N/A System.out.print("\nTesting buffer size: " + size + ":");
4791N/A }
4791N/A
4791N/A int outSize = cipher.getOutputSize(size);
4791N/A
4791N/A try {
4791N/A encrypt(cipher, size,
4791N/A ByteBuffer.allocate(size),
4791N/A ByteBuffer.allocate(outSize),
4791N/A ByteBuffer.allocateDirect(size),
4791N/A ByteBuffer.allocateDirect(outSize),
4791N/A output);
4791N/A } catch (Exception e) {
4791N/A System.out.print("\n Failed with size " + size);
4791N/A failedOnce = true;
4791N/A failedReason = e;
4791N/A
4791N/A // If we got an exception, let's be safe for future
4791N/A // testing and reset the cipher to a known good state.
4791N/A cipher.init(Cipher.ENCRYPT_MODE, key);
4791N/A }
4791N/A }
4791N/A if (failedOnce) {
4791N/A throw failedReason;
4791N/A }
4791N/A System.out.println("\nTest Passed...");
4791N/A }
4791N/A
4791N/A private enum TestVariant {
4791N/A
4791N/A HEAP_HEAP, HEAP_DIRECT, DIRECT_HEAP, DIRECT_DIRECT
4791N/A };
4791N/A
4791N/A private static void encrypt(Cipher cipher, int size,
4791N/A ByteBuffer heapIn, ByteBuffer heapOut,
4791N/A ByteBuffer directIn, ByteBuffer directOut,
4791N/A boolean output) throws Exception {
4791N/A
4791N/A ByteBuffer inBB = null;
4791N/A ByteBuffer outBB = null;
4791N/A
4791N/A // Set up data and encrypt to known/expected values.
4791N/A byte[] testdata = new byte[size];
4791N/A random.nextBytes(testdata);
4791N/A byte[] expected = cipher.doFinal(testdata);
4791N/A
4791N/A for (TestVariant tv : TestVariant.values()) {
4791N/A if (output) {
4791N/A System.out.print(" " + tv);
4791N/A }
4791N/A
4791N/A switch (tv) {
4791N/A case HEAP_HEAP:
4791N/A inBB = heapIn;
4791N/A outBB = heapOut;
4791N/A break;
4791N/A case HEAP_DIRECT:
4791N/A inBB = heapIn;
4791N/A outBB = directOut;
4791N/A break;
4791N/A case DIRECT_HEAP:
4791N/A inBB = directIn;
4791N/A outBB = heapOut;
4791N/A break;
4791N/A case DIRECT_DIRECT:
4791N/A inBB = directIn;
4791N/A outBB = directOut;
4791N/A break;
4791N/A }
4791N/A
4791N/A inBB.clear();
4791N/A outBB.clear();
4791N/A
4791N/A inBB.put(testdata);
4791N/A inBB.flip();
4791N/A
4791N/A // Process all data in one shot, but don't call doFinal() yet.
4791N/A // May store up to n-1 bytes (w/block size n) internally.
4791N/A cipher.update(inBB, outBB);
4791N/A if (inBB.hasRemaining()) {
4791N/A throw new Exception("buffer not empty");
4791N/A }
4791N/A
4791N/A // finish encryption and process all data buffered
4791N/A cipher.doFinal(inBB, outBB);
4791N/A outBB.flip();
4791N/A
4791N/A // validate output size
4791N/A if (outBB.remaining() != expected.length) {
4791N/A throw new Exception(
4791N/A "incomplete encryption output, expected "
4791N/A + expected.length + " bytes but was only "
4791N/A + outBB.remaining() + " bytes");
4791N/A }
4791N/A
4791N/A // validate output data
4791N/A byte[] encrypted = new byte[outBB.remaining()];
4791N/A outBB.get(encrypted);
4791N/A if (!Arrays.equals(expected, encrypted)) {
4791N/A throw new Exception("bad encryption output");
4791N/A }
4791N/A
4791N/A if (!Arrays.equals(cipher.doFinal(), cipher.doFinal())) {
4791N/A throw new Exception("Internal buffers still held data!");
4791N/A }
4791N/A }
4791N/A }
4791N/A}