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 Mac.update(ByteBuffer) method 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 main(String[] args)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[16]; 50 random.nextBytes(keyBytes); 51 SecretKey key = new SecretKeySpec(keyBytes, "HmacMD5"); 52 53 Mac mac = Mac.getInstance("HmacMD5"); 54 mac.init(key); 55 byte[] macValue = mac.doFinal(t); 56 57 // test 1: ByteBuffer with an accessible backing array 58 ByteBuffer b1 = ByteBuffer.allocate(n + 256); 59 b1.position(random.nextInt(256)); 60 b1.limit(b1.position() + n); 61 ByteBuffer b2 = b1.slice(); 62 b2.put(t); 63 b2.clear(); 64 verify(mac, macValue, b2, random); 65 66 // test 2: direct ByteBuffer 67 ByteBuffer b3 = ByteBuffer.allocateDirect(t.length); 68 b3.put(t); 69 b3.clear(); 70 verify(mac, macValue, b3, random); 71 72 // test 3: ByteBuffer without an accessible backing array 73 b2.clear(); 74 ByteBuffer b4 = b2.asReadOnlyBuffer(); 75 verify(mac, macValue, b4, random); 76 77 System.out.println("All tests passed"); 78 } 79 verify(Mac mac, byte[] macValue, ByteBuffer b, Random random)80 private static void verify(Mac mac, byte[] macValue, ByteBuffer b, Random random) throws Exception { 81 int lim = b.limit(); 82 b.limit(random.nextInt(lim)); 83 mac.update(b); 84 if (b.hasRemaining()) { 85 throw new Exception("Buffer not consumed"); 86 } 87 b.limit(lim); 88 mac.update(b); 89 if (b.hasRemaining()) { 90 throw new Exception("Buffer not consumed"); 91 } 92 byte[] newMacValue = mac.doFinal(); 93 if (Arrays.equals(macValue, newMacValue) == false) { 94 throw new Exception("Mac did not verify"); 95 } 96 } 97 } 98