Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestCopySafe.java
41161 views
/*1* Copyright (c) 2013, 2014, 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*/2223/*24* @test25* @bug 8026943 802757526* @summary Verify that same buffer can be used as input and output when27* using Cipher objects.28* @author Valerie Peng29*/30import java.security.*;31import java.security.spec.*;3233import java.util.Arrays;3435import javax.crypto.*;36import javax.crypto.spec.*;3738public class TestCopySafe {3940private static boolean DEBUG = false;41private static int INPUT_LENGTH = 160; // must be multiple of block size42private static byte[] PT = new byte[INPUT_LENGTH];43private static SecretKey KEY = new SecretKeySpec(new byte[16], "AES");44private static byte[] IV = new byte[16];4546private static int[] OFFSETS = { 1, 8, 9, 16, 17, 32, 33 };4748private static final String[] MODES = {49"ECB", "CBC", "PCBC", "CTR", "CTS",50"CFB", "CFB8", "CFB16", "CFB24", "CFB32", "CFB40",51"CFB48", "CFB56", "CFB64",52"OFB", "OFB8", "OFB16", "OFB24", "OFB32", "OFB40",53"OFB48", "OFB56", "OFB64",54"GCM"55};5657public static void main(String[] argv) throws Exception {5859Provider p = Security.getProvider("SunJCE");6061AlgorithmParameterSpec params = null;62boolean result = true;63for (String mode : MODES) {64String transformation = "AES/" + mode + "/NoPadding";65boolean isGCM = (mode == "GCM");66if (isGCM) {67params = new GCMParameterSpec(128, IV);68} else if (mode != "ECB") {69params = new IvParameterSpec(IV);70}71Cipher c = Cipher.getInstance(transformation, p);72System.out.println("Testing " + transformation + ":");73for (int offset : OFFSETS) {74System.out.print("=> offset " + offset + ": ");75try {76test(c, params, offset, isGCM);77System.out.println("Passed");78} catch(Exception ex) {79ex.printStackTrace();80result = false;81continue;82}83}84}85if (!result) {86throw new Exception("One or more test failed");87}88}8990private static void test(Cipher c, AlgorithmParameterSpec params,91int offset, boolean isGCM) throws Exception {9293// Test encryption first94if (isGCM) {95// re-init with only key value first to bypass the96// Key+IV-uniqueness check for GCM encryption97c.init(Cipher.ENCRYPT_MODE, KEY);98}99c.init(Cipher.ENCRYPT_MODE, KEY, params);100byte[] answer = c.doFinal(PT);101byte[] pt2 = Arrays.copyOf(PT, answer.length + offset);102103// #1: outOfs = inOfs = 0104if (isGCM) {105c.init(Cipher.ENCRYPT_MODE, KEY);106c.init(Cipher.ENCRYPT_MODE, KEY, params);107}108c.doFinal(pt2, 0, PT.length, pt2, 0);109if (!isTwoArraysEqual(pt2, 0, answer, 0, answer.length)) {110throw new Exception("Enc#1 diff check failed!");111} else if (DEBUG) {112System.out.println("Enc#1 diff check passed");113}114115// #2: inOfs = 0, outOfs = offset116System.arraycopy(PT, 0, pt2, 0, PT.length);117if (isGCM) {118c.init(Cipher.ENCRYPT_MODE, KEY);119c.init(Cipher.ENCRYPT_MODE, KEY, params);120}121c.doFinal(pt2, 0, PT.length, pt2, offset);122if (!isTwoArraysEqual(pt2, offset, answer, 0, answer.length)) {123throw new Exception("Enc#2 diff check failed");124} else if (DEBUG) {125System.out.println("Enc#2 diff check passed");126}127128// #3: inOfs = offset, outOfs = 0129System.arraycopy(PT, 0, pt2, offset, PT.length);130if (isGCM) {131c.init(Cipher.ENCRYPT_MODE, KEY);132c.init(Cipher.ENCRYPT_MODE, KEY, params);133}134c.doFinal(pt2, offset, PT.length, pt2, 0);135if (!isTwoArraysEqual(pt2, 0, answer, 0, answer.length)) {136throw new Exception("Enc#3 diff check failed");137} else if (DEBUG) {138System.out.println("Enc#3 diff check passed");139}140141// Test decryption now, we should get back PT as a result142c.init(Cipher.DECRYPT_MODE, KEY, params);143pt2 = Arrays.copyOf(answer, answer.length + offset);144145// #1: outOfs = inOfs = 0146c.doFinal(pt2, 0, answer.length, pt2, 0);147if (!isTwoArraysEqual(pt2, 0, PT, 0, PT.length)) {148throw new Exception("Dec#1 diff check failed!");149} else if (DEBUG) {150System.out.println("Dec#1 diff check passed");151}152153// #2: inOfs = 0, outOfs = offset154System.arraycopy(answer, 0, pt2, 0, answer.length);155c.doFinal(pt2, 0, answer.length, pt2, offset);156if (!isTwoArraysEqual(pt2, offset, PT, 0, PT.length)) {157throw new Exception("Dec#2 diff check failed");158} else if (DEBUG) {159System.out.println("Dec#2 diff check passed");160}161162// #3: inOfs = offset, outOfs = 0163System.arraycopy(answer, 0, pt2, offset, answer.length);164c.doFinal(pt2, offset, answer.length, pt2, 0);165if (!isTwoArraysEqual(pt2, 0, PT, 0, PT.length)) {166throw new Exception("Dec#3 diff check failed");167} else if (DEBUG) {168System.out.println("Dec#3 diff check passed");169}170}171172private static boolean isTwoArraysEqual(byte[] a, int aOff, byte[] b, int bOff,173int len) {174for (int i = 0; i < len; i++) {175if (a[aOff + i] != b[bOff + i]) {176return false;177}178}179return true;180}181}182183184185