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  * @key randomness
  30  */
  31 
  32 import java.util.*;
  33 import java.nio.*;
  34 
  35 import java.security.*;
  36 
  37 import javax.crypto.*;
  38 import javax.crypto.spec.*;
  39 
  40 public class ByteBuffers {
  41 
  42     public static void main(String[] args) throws Exception {
  43         Provider p = Security.getProvider("SunJCE");
  44         Random random = new Random();
  45         int n = 10 * 1024;
  46         byte[] t = new byte[n];
  47         random.nextBytes(t);
  48 
  49         byte[] keyBytes = new byte[8];
  50         random.nextBytes(keyBytes);
  51         SecretKey key = new SecretKeySpec(keyBytes, "DES");
  52 
  53         Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
  54         cipher.init(Cipher.ENCRYPT_MODE, key);
  55 
  56         byte[] outBytes = cipher.doFinal(t);
  57 
  58         // create ByteBuffers for input (i1, i2, i3) and fill them
  59         ByteBuffer i0 = ByteBuffer.allocate(n + 256);
  60         i0.position(random.nextInt(256));
  61         i0.limit(i0.position() + n);
  62         ByteBuffer i1 = i0.slice();
  63         i1.put(t);
  64 
  65         ByteBuffer i2 = ByteBuffer.allocateDirect(t.length);
  66         i2.put(t);
  67 
  68         i1.clear();
  69         ByteBuffer i3 = i1.asReadOnlyBuffer();
  70 
  71         ByteBuffer o0 = ByteBuffer.allocate(n + 512);
  72         o0.position(random.nextInt(256));
  73         o0.limit(o0.position() + n + 256);
  74         ByteBuffer o1 = o0.slice();
  75 
  76         ByteBuffer o2 = ByteBuffer.allocateDirect(t.length + 256);
  77 
  78         crypt(cipher, i1, o1, outBytes, random);
  79         crypt(cipher, i2, o1, outBytes, random);
  80         crypt(cipher, i3, o1, outBytes, random);
  81         crypt(cipher, i1, o2, outBytes, random);
  82         crypt(cipher, i2, o2, outBytes, random);
  83         crypt(cipher, i3, o2, outBytes, random);
  84 
  85         System.out.println("All tests passed");
  86     }
  87 
  88     private static void crypt(Cipher cipher, ByteBuffer in, ByteBuffer out, byte[] outBytes, Random random) throws Exception {
  89         in.clear();
  90         out.clear();
  91         out.put(new byte[out.remaining()]);
  92         out.clear();
  93         int lim = in.limit();
  94         in.limit(random.nextInt(lim));
  95         cipher.update(in, out);
  96         if (in.hasRemaining()) {
  97             throw new Exception("Buffer not consumed");
  98         }
  99         in.limit(lim);
 100         cipher.doFinal(in, out);
 101         if (in.hasRemaining()) {
 102             throw new Exception("Buffer not consumed");
 103         }
 104         out.flip();
 105         byte[] b = new byte[out.remaining()];
 106         out.get(b);
 107         if (Arrays.equals(outBytes, b) == false) {
 108             throw new Exception("Encryption output mismatch");
 109         }
 110     }
 111 }