Path: blob/master/test/jdk/sun/security/pkcs11/Mac/MacSameTest.java
41152 views
/*1* Copyright (c) 1998, 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 8048603 824233226* @summary Check if doFinal and update operation result in same Mac27* @author Yu-Ching Valerie Peng, Bill Situ, Alexander Fomin28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm MacSameTest31* @run main/othervm -Djava.security.manager=allow MacSameTest sm32* @key randomness33*/3435import java.security.InvalidKeyException;36import java.security.NoSuchAlgorithmException;37import java.security.NoSuchProviderException;38import java.security.Provider;39import java.security.SecureRandom;40import java.util.List;41import javax.crypto.Mac;42import javax.crypto.KeyGenerator;43import javax.crypto.SecretKey;44import javax.crypto.spec.SecretKeySpec;4546public class MacSameTest extends PKCS11Test {4748private static final int MESSAGE_SIZE = 25;49private static final int OFFSET = 5;50private static final int KEY_SIZE = 128;5152/**53* Initialize a message, instantiate a Mac object,54* initialize the object with a SecretKey,55* feed the message into the Mac object56* all at once and get the output MAC as result1.57* Reset the Mac object, chop the message into three pieces,58* feed into the Mac object sequentially, and get the output MAC as result2.59* Finally, compare result1 and result2 and see if they are the same.60*61* @param args the command line arguments62*/63public static void main(String[] args) throws Exception {64main(new MacSameTest(), args);65}6667@Override68public void main(Provider p) {69List<String> algorithms = getSupportedAlgorithms("Mac", "Hmac", p);70boolean success = true;71SecureRandom srdm = new SecureRandom();7273for (String alg : algorithms) {74// first try w/ java secret key object75byte[] keyVal = new byte[KEY_SIZE];76srdm.nextBytes(keyVal);77SecretKey skey = new SecretKeySpec(keyVal, alg);7879try {80doTest(alg, skey, p);81} catch (Exception e) {82System.out.println("Unexpected exception: " + e);83e.printStackTrace();84success = false;85}8687try {88KeyGenerator kg = KeyGenerator.getInstance(alg, p);89kg.init(KEY_SIZE);90skey = kg.generateKey();91doTest(alg, skey, p);92} catch (NoSuchAlgorithmException nsae) {93System.out.println("Skip test using native key for " + alg);94continue;95} catch (Exception e) {96System.out.println("Unexpected exception: " + e);97e.printStackTrace();98success = false;99}100}101102if (!success) {103throw new RuntimeException("Test failed");104}105}106107private void doTest(String algo, SecretKey key, Provider provider)108throws NoSuchAlgorithmException, NoSuchProviderException,109InvalidKeyException {110System.out.println("Test " + algo);111Mac mac = Mac.getInstance(algo, provider);112113byte[] plain = new byte[MESSAGE_SIZE];114for (int i = 0; i < MESSAGE_SIZE; i++) {115plain[i] = (byte) (i % 256);116}117118byte[] tail = new byte[plain.length - OFFSET];119System.arraycopy(plain, OFFSET, tail, 0, tail.length);120121mac.init(key);122byte[] result1 = mac.doFinal(plain);123124mac.reset();125mac.update(plain[0]);126mac.update(plain, 1, OFFSET - 1);127byte[] result2 = mac.doFinal(tail);128129if (!java.util.Arrays.equals(result1, result2)) {130throw new RuntimeException("result1 and result2 are not the same");131}132}133134}135136137