Path: blob/master/test/jdk/sun/security/pkcs11/MessageDigest/ReinitDigest.java
66646 views
/*1* Copyright (c) 2003, 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 4856966 8242332 826927626* @summary27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm ReinitDigest32* @run main/othervm -Djava.security.manager=allow ReinitDigest sm33*/34import java.security.MessageDigest;35import java.security.Provider;36import java.util.Arrays;37import java.util.Random;38import java.util.List;3940public class ReinitDigest extends PKCS11Test {4142public static void main(String[] args) throws Exception {43main(new ReinitDigest(), args);44}4546@Override47public void main(Provider p) throws Exception {48List<String> ALGS = getSupportedAlgorithms("MessageDigest", "SHA", p);49Random r = new Random();50byte[] data1 = new byte[10 * 1024];51byte[] data2 = new byte[10 * 1024];52r.nextBytes(data1);53r.nextBytes(data2);5455boolean success = true;56for (String alg : ALGS) {57try {58doTest(alg, p, data1, data2);59} catch (Exception e) {60System.out.println("Unexpected exception: " + e);61e.printStackTrace();62success = false;63}64}6566if (!success) {67throw new RuntimeException("Test failed");68}69System.out.println("All tests passed");70}7172private void doTest(String alg, Provider p, byte[] data1, byte[] data2)73throws Exception {74System.out.println("Testing " + alg);75MessageDigest md1 = MessageDigest.getInstance(alg, "SUN");76byte[] d1 = md1.digest(data1);77MessageDigest md2 = MessageDigest.getInstance(alg, p);78checkInstances(md1, md2);79byte[] d2 = md2.digest(data1);80check(d1, d2);81byte[] d3 = md2.digest(data1);82check(d1, d3);83md2.update(data2);84md2.update((byte) 0);85md2.reset();86byte[] d4 = md2.digest(data1);87check(d1, d4);88}8990private static void check(byte[] d1, byte[] d2) throws Exception {91if (Arrays.equals(d1, d2) == false) {92throw new RuntimeException("Digest mismatch");93}94}9596private static void checkInstances(MessageDigest md1, MessageDigest md2)97throws Exception {98if (md1.equals(md2)) {99throw new RuntimeException("MD instances should be different");100}101if (!md1.getAlgorithm().equals(md2.getAlgorithm())) {102throw new RuntimeException("Algorithm name should equal");103}104if (md1.getProvider().getName().equals(md2.getProvider().getName())) {105throw new RuntimeException("Provider name should be different");106}107}108}109110111