Path: blob/master/test/jdk/javax/crypto/CipherSpi/ResetByteBuffer.java
41149 views
/*1* Copyright (c) 2021, 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 826146226* @summary Verify that after the first doFinal() decryption op, the ByteBuffer27* is properly set for the second operation.28*/2930import javax.crypto.Cipher;31import javax.crypto.KeyGenerator;32import javax.crypto.SecretKey;33import java.nio.ByteBuffer;3435public class ResetByteBuffer {3637Cipher c;38SecretKey key;39ByteBuffer in, out;40byte[] data = new byte[1500];41byte encrypted[];4243public static final void main(String args[]) throws Exception {44// Cannot do encryption back to back with AES/GCM45// Tests GCM's ByteBuffer code46String algo = "AES/GCM/NoPadding";47new ResetByteBuffer(algo).decrypt(true).updateTest().updateTest();48new ResetByteBuffer(algo).decrypt(false).updateTest().updateTest();49new ResetByteBuffer(algo).decrypt(true).updateTest().doFinalTest();50new ResetByteBuffer(algo).decrypt(false).updateTest().doFinalTest();51new ResetByteBuffer(algo).decrypt(true).doFinalTest().updateTest();52new ResetByteBuffer(algo).decrypt(false).doFinalTest().updateTest();53new ResetByteBuffer(algo).decrypt(true).doFinalTest().doFinalTest();54new ResetByteBuffer(algo).decrypt(false).doFinalTest().doFinalTest();5556// Tests CipherCore code. Testing CBC should be enough to cover the57// other algorithms that use CipherCore58algo = "AES/CBC/PKCS5Padding";59new ResetByteBuffer(algo).encrypt(true).updateTest().updateTest();60new ResetByteBuffer(algo).encrypt(false).updateTest().updateTest();61new ResetByteBuffer(algo).encrypt(true).updateTest().doFinalTest();62new ResetByteBuffer(algo).encrypt(false).updateTest().doFinalTest();63new ResetByteBuffer(algo).encrypt(true).doFinalTest().updateTest();64new ResetByteBuffer(algo).encrypt(false).doFinalTest().updateTest();65new ResetByteBuffer(algo).encrypt(true).doFinalTest().doFinalTest();66new ResetByteBuffer(algo).encrypt(false).doFinalTest().doFinalTest();67new ResetByteBuffer(algo).decrypt(true).updateTest().updateTest();68new ResetByteBuffer(algo).decrypt(false).updateTest().updateTest();69new ResetByteBuffer(algo).decrypt(true).updateTest().doFinalTest();70new ResetByteBuffer(algo).decrypt(false).updateTest().doFinalTest();71new ResetByteBuffer(algo).decrypt(true).doFinalTest().updateTest();72new ResetByteBuffer(algo).decrypt(false).doFinalTest().updateTest();73new ResetByteBuffer(algo).decrypt(true).doFinalTest().doFinalTest();74new ResetByteBuffer(algo).decrypt(false).doFinalTest().doFinalTest();75}7677public ResetByteBuffer(String algo) throws Exception {78c = Cipher.getInstance(algo);79String a[] = algo.split("/");80KeyGenerator kg = KeyGenerator.getInstance(a[0]);81key = kg.generateKey();82// Setup encrypted data83c.init(Cipher.ENCRYPT_MODE, key, c.getParameters());84encrypted = new byte[c.getOutputSize(data.length)];85c.doFinal(data, 0, data.length, encrypted, 0);86}8788ResetByteBuffer decrypt(boolean direct) throws Exception {89// allocate bytebuffers90if (direct) {91in = ByteBuffer.allocateDirect(encrypted.length);92out = ByteBuffer.allocateDirect(encrypted.length);93} else {94in = ByteBuffer.allocate(encrypted.length);95out = ByteBuffer.allocate(encrypted.length);96}97in.put(encrypted);98in.flip();99c.init(Cipher.DECRYPT_MODE, key, c.getParameters());100return this;101}102103ResetByteBuffer encrypt(boolean direct) throws Exception {104// allocate bytebuffers105if (direct) {106in = ByteBuffer.allocateDirect(data.length);107out = ByteBuffer.allocateDirect(c.getOutputSize(data.length));108} else {109in = ByteBuffer.allocate(data.length);110out = ByteBuffer.allocate(c.getOutputSize(data.length));111}112c.init(Cipher.ENCRYPT_MODE, key, c.getParameters());113return this;114}115116ResetByteBuffer updateTest() throws Exception {117int updateLen = data.length / 2;118in.limit(updateLen);119c.update(in, out);120in.limit(in.capacity());121c.doFinal(in, out);122if (in.capacity() != in.position()) {123System.out.println("There is data remaining in the input buffer");124}125if (out.limit() != out.position()) {126System.out.println("There is data remaining in the output buffer");127}128in.flip();129out.position(0);130out.limit(out.capacity());131return this;132}133134ResetByteBuffer doFinalTest() throws Exception {135c.doFinal(in, out);136if (in.capacity() != in.position()) {137System.out.println("There is data remaining in the input buffer");138}139if (out.limit() != out.position()) {140System.out.println("There is data remaining in the output buffer");141}142in.flip();143out.position(0);144out.limit(out.capacity());145return this;146}147}148149150