Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/TextLength/SameBufferOverwrite.java
41161 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.crypto.Cipher;24import javax.crypto.KeyGenerator;25import javax.crypto.SecretKey;26import java.security.AlgorithmParameters;27import java.util.Arrays;282930/*31* @test32* @summary Verify when decrypting over an existing buffer than padding does not33* overwrite past what the plaintext length is.34*35*/3637public class SameBufferOverwrite {3839private SecretKey skey;40private Cipher c;41private int start = 17, end = 17; // default4243SameBufferOverwrite(String algo, String transformation)44throws Exception {4546KeyGenerator kg = KeyGenerator.getInstance(algo, "SunJCE");47skey = kg.generateKey();48c = Cipher.getInstance(transformation, "SunJCE");49}5051/*52* Run the test53*/54void test() throws Exception {55byte[] in = new byte[end + (c.getBlockSize() - (end % c.getBlockSize()))];56Arrays.fill(in, (byte)8);57int len = start;58AlgorithmParameters params = null;5960System.out.println("Testing transformation: " + c.getAlgorithm() +61", byte length from " + start + " to " + end);62while (end >= len) {63// encrypt64c.init(Cipher.ENCRYPT_MODE, skey, params);65byte[] out = c.doFinal(in, 0, len);66System.out.println(" enc = " + byteToHex(out));67System.out.println(" => enc " + len + " bytes, ret " +68(out == null ? "null" : (out.length + " byte")));6970// decrypt71params = c.getParameters();72c.init(Cipher.DECRYPT_MODE, skey, params);73int rLen = c.doFinal(out, 0, out.length, in);74System.out.println(" dec = " + byteToHex(in));75System.out.println(" => dec " + out.length + " bytes, ret " +76rLen + " byte");77// check if more than rLen bytes are written into 'in'78for (int j = rLen; j < in.length; j++) {79if (in[j] != (byte) 8) {80throw new Exception("Value check failed at index " + j);81}82}83System.out.println(" Test Passed: len = " + len);84len++;8586// Because GCM doesn't allow params reuse87if (c.getAlgorithm().contains("GCM")) {88params = null;89}90}91}9293/**94* Builder method for the test to run data lengths from the start value to95* the end value. To do one length, have start and end equal that number.96* @param start starting data length97* @param end ending data length98*/99SameBufferOverwrite iterate(int start, int end) {100this.start = start;101this.end = end;102return this;103}104105public static void main(String args[]) throws Exception {106new SameBufferOverwrite("AES", "AES/GCM/NoPadding").iterate(1, 25).107test();108new SameBufferOverwrite("AES", "AES/CTR/NoPadding").iterate(1, 25).109test();110new SameBufferOverwrite("AES", "AES/CBC/PKCS5Padding").iterate(1, 25).111test();112new SameBufferOverwrite("AES", "AES/ECB/PKCS5Padding").iterate(1, 25).113test();114new SameBufferOverwrite("DES", "DES/CBC/PKCS5Padding").iterate(1, 17).115test();116new SameBufferOverwrite("DESede", "DESede/CBC/PKCS5Padding").iterate(1, 17).117test();118}119120private static String byteToHex(byte[] barray) {121StringBuilder s = new StringBuilder();122for (byte b : barray) {123s.append(String.format("%02x", b));124}125return s.toString();126}127}128129130