Path: blob/master/test/jdk/sun/security/pkcs11/Signature/ByteBuffers.java
41153 views
/*1* Copyright (c) 2003, 2020, 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 4856966 824233226* @summary Test the Signature.update(ByteBuffer) method27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm ByteBuffers32* @run main/othervm -Djava.security.manager=allow ByteBuffers sm33*/3435import java.nio.ByteBuffer;36import java.security.KeyPair;37import java.security.KeyPairGenerator;38import java.security.Provider;39import java.security.Signature;40import java.util.Random;4142public class ByteBuffers extends PKCS11Test {4344public static void main(String[] args) throws Exception {45main(new ByteBuffers(), args);46}4748@Override49public void main(Provider p) throws Exception {5051Random random = new Random();52int n = 10 * 1024;53byte[] t = new byte[n];54random.nextBytes(t);5556KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);57kpg.initialize(2048);58KeyPair kp = kpg.generateKeyPair();5960Signature sig = Signature.getInstance("SHA256withRSA", p);61sig.initSign(kp.getPrivate());62sig.update(t);63byte[] signature = sig.sign();6465sig.initVerify(kp.getPublic());6667// test 1: ByteBuffer with an accessible backing array68ByteBuffer b1 = ByteBuffer.allocate(n + 256);69b1.position(random.nextInt(256));70b1.limit(b1.position() + n);71ByteBuffer b2 = b1.slice();72b2.put(t);73b2.clear();74verify(sig, signature, b2, random);7576// test 2: direct ByteBuffer77ByteBuffer b3 = ByteBuffer.allocateDirect(t.length);78b3.put(t);79b3.clear();80verify(sig, signature, b3, random);8182// test 3: ByteBuffer without an accessible backing array83b2.clear();84ByteBuffer b4 = b2.asReadOnlyBuffer();85verify(sig, signature, b4, random);8687System.out.println("All tests passed");88}8990private static void verify(Signature sig, byte[] signature, ByteBuffer b, Random random) throws Exception {91int lim = b.limit();92b.limit(random.nextInt(lim));93sig.update(b);94if (b.hasRemaining()) {95throw new Exception("Buffer not consumed");96}97b.limit(lim);98sig.update(b);99if (b.hasRemaining()) {100throw new Exception("Buffer not consumed");101}102if (sig.verify(signature) == false) {103throw new Exception("Signature did not verify");104}105}106}107108109