1 /* 2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4844847 27 * @summary Test the Cipher.update/doFinal(ByteBuffer, ByteBuffer) methods 28 * @author Andreas Sterbenz 29 */ 30 31 import java.util.*; 32 import java.nio.*; 33 34 import java.security.*; 35 36 import javax.crypto.*; 37 import javax.crypto.spec.*; 38 39 public class ByteBuffers { 40 41 public static void main(String[] args) throws Exception { 42 Provider p = Security.getProvider("SunJCE"); 43 Random random = new Random(); 44 int n = 10 * 1024; 45 byte[] t = new byte[n]; 46 random.nextBytes(t); 47 48 byte[] keyBytes = new byte[8]; 49 random.nextBytes(keyBytes); 50 SecretKey key = new SecretKeySpec(keyBytes, "DES"); 51 52 Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 53 cipher.init(Cipher.ENCRYPT_MODE, key); 54 55 byte[] outBytes = cipher.doFinal(t); 56 57 // create ByteBuffers for input (i1, i2, i3) and fill them 58 ByteBuffer i0 = ByteBuffer.allocate(n + 256); 59 i0.position(random.nextInt(256)); 60 i0.limit(i0.position() + n); 61 ByteBuffer i1 = i0.slice(); 62 i1.put(t); 63 64 ByteBuffer i2 = ByteBuffer.allocateDirect(t.length); 65 i2.put(t); 66 67 i1.clear(); 68 ByteBuffer i3 = i1.asReadOnlyBuffer(); 69 70 ByteBuffer o0 = ByteBuffer.allocate(n + 512); 71 o0.position(random.nextInt(256)); 72 o0.limit(o0.position() + n + 256); 73 ByteBuffer o1 = o0.slice(); 74 75 ByteBuffer o2 = ByteBuffer.allocateDirect(t.length + 256); 76 77 crypt(cipher, i1, o1, outBytes, random); 78 crypt(cipher, i2, o1, outBytes, random); 79 crypt(cipher, i3, o1, outBytes, random); 80 crypt(cipher, i1, o2, outBytes, random); 81 crypt(cipher, i2, o2, outBytes, random); 82 crypt(cipher, i3, o2, outBytes, random); 83 84 System.out.println("All tests passed"); 85 } 86 87 private static void crypt(Cipher cipher, ByteBuffer in, ByteBuffer out, byte[] outBytes, Random random) throws Exception { 88 in.clear(); 89 out.clear(); 90 out.put(new byte[out.remaining()]); 91 out.clear(); 92 int lim = in.limit(); 93 in.limit(random.nextInt(lim)); 94 cipher.update(in, out); 95 if (in.hasRemaining()) { 96 throw new Exception("Buffer not consumed"); 97 } 98 in.limit(lim); 99 cipher.doFinal(in, out); 100 if (in.hasRemaining()) { 101 throw new Exception("Buffer not consumed"); 102 } 103 out.flip(); 104 byte[] b = new byte[out.remaining()]; 105 out.get(b); 106 if (Arrays.equals(outBytes, b) == false) { 107 throw new Exception("Encryption output mismatch"); 108 } 109 } 110 }