Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Mac/MacSameTest.java
38855 views
/*1* Copyright (c) 1998, 2016, 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*/2223import java.security.InvalidKeyException;24import java.security.NoSuchAlgorithmException;25import java.security.NoSuchProviderException;26import java.security.Provider;27import java.security.SecureRandom;28import java.util.List;29import javax.crypto.Mac;30import javax.crypto.spec.SecretKeySpec;3132/**33* @test34* @bug 804860335* @summary Check if doFinal and update operation result in same Mac36* @author Yu-Ching Valerie Peng, Bill Situ, Alexander Fomin37* @library ..38* @run main/othervm MacSameTest39* @run main/othervm MacSameTest sm40* @key randomness41*/42public class MacSameTest extends PKCS11Test {4344private static final int MESSAGE_SIZE = 25;45private static final int OFFSET = 5;46private static final int KEY_SIZE = 70;4748/**49* Initialize a message, instantiate a Mac object,50* initialize the object with a SecretKey,51* feed the message into the Mac object52* all at once and get the output MAC as result1.53* Reset the Mac object, chop the message into three pieces,54* feed into the Mac object sequentially, and get the output MAC as result2.55* Finally, compare result1 and result2 and see if they are the same.56*57* @param args the command line arguments58*/59public static void main(String[] args) throws Exception {60main(new MacSameTest(), args);61}6263@Override64public void main(Provider p) {65List<String> algorithms = getSupportedAlgorithms("Mac", "Hmac", p);66boolean success = true;67for (String alg : algorithms) {68try {69doTest(alg, p);70} catch (Exception e) {71System.out.println("Unexpected exception: " + e);72e.printStackTrace();73success = false;74}75}7677if (!success) {78throw new RuntimeException("Test failed");79}80}8182private void doTest(String algo, Provider provider)83throws NoSuchAlgorithmException, NoSuchProviderException,84InvalidKeyException {85System.out.println("Test " + algo);86Mac mac;87try {88mac = Mac.getInstance(algo, provider);89} catch (NoSuchAlgorithmException nsae) {90if ("SunPKCS11-Solaris".equals(provider.getName())) {91// depending on Solaris configuration,92// it can support HMAC or not with Mac93System.out.println("Expected NoSuchAlgorithmException thrown: "94+ nsae);95return;96}97throw nsae;98}99100byte[] plain = new byte[MESSAGE_SIZE];101for (int i = 0; i < MESSAGE_SIZE; i++) {102plain[i] = (byte) (i % 256);103}104105byte[] tail = new byte[plain.length - OFFSET];106System.arraycopy(plain, OFFSET, tail, 0, tail.length);107108SecureRandom srdm = new SecureRandom();109byte[] keyVal = new byte[KEY_SIZE];110srdm.nextBytes(keyVal);111SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");112113mac.init(keySpec);114byte[] result1 = mac.doFinal(plain);115116mac.reset();117mac.update(plain[0]);118mac.update(plain, 1, OFFSET - 1);119byte[] result2 = mac.doFinal(tail);120121if (!java.util.Arrays.equals(result1, result2)) {122throw new RuntimeException("result1 and result2 are not the same");123}124}125126}127128129