Path: blob/master/test/jdk/sun/security/pkcs11/Mac/ReinitMac.java
41152 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* @summary27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm ReinitMac32* @run main/othervm -Djava.security.manager=allow ReinitMac sm33*/3435import java.security.Provider;36import java.util.Random;37import java.util.List;38import javax.crypto.Mac;39import javax.crypto.spec.SecretKeySpec;4041public class ReinitMac extends PKCS11Test {4243public static void main(String[] args) throws Exception {44main(new ReinitMac(), args);45}4647@Override48public void main(Provider p) throws Exception {49List<String> algorithms = getSupportedAlgorithms("Mac", "Hmac", p);50Random random = new Random();51byte[] data = new byte[10 * 1024];52random.nextBytes(data);53byte[] keyVal = new byte[16];54random.nextBytes(keyVal);5556boolean success = true;57for (String alg : algorithms) {58try {59doTest(alg, p, keyVal, data);60} catch (Exception e) {61System.out.println("Unexpected exception: " + e);62e.printStackTrace();63success = false;64}65}6667if (!success) {68throw new RuntimeException("Test failed");69} else {70System.out.println("All tests passed");71}72}7374private void doTest(String alg, Provider p, byte[] keyVal, byte[] data)75throws Exception {76System.out.println("Testing " + alg);77SecretKeySpec key = new SecretKeySpec(keyVal, alg);78Mac mac = Mac.getInstance(alg, p);79mac.init(key);80mac.init(key);81mac.update(data);82mac.init(key);83mac.doFinal();84mac.doFinal();85mac.update(data);86mac.doFinal();87mac.reset();88mac.reset();89mac.init(key);90mac.reset();91mac.update(data);92mac.reset();93}94}959697