Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/MacSameTest.java
41159 views
/*1* Copyright (c) 1998, 2014, 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.SecureRandom;27import javax.crypto.Mac;28import javax.crypto.spec.SecretKeySpec;2930/**31* @test32* @bug 804860333* @summary Check if doFinal and update operation result in same Mac34* @author Yu-Ching Valerie Peng, Bill Situ, Alexander Fomin35* @build Utils36* @run main MacSameTest37* @key randomness38*/39public class MacSameTest implements MacTest {4041private static final int MESSAGE_SIZE = 25;42private static final int OFFSET = 5;43private static final int KEY_SIZE = 70;4445/**46* Initialize a message, instantiate a Mac object,47* initialize the object with a SecretKey,48* feed the message into the Mac object49* all at once and get the output MAC as result1.50* Reset the Mac object, chop the message into three pieces,51* feed into the Mac object sequentially, and get the output MAC as result2.52* Finally, compare result1 and result2 and see if they are the same.53*54* @param args the command line arguments55*/56public static void main(String[] args) {57Utils.runTests(new MacSameTest());58}5960@Override61public void doTest(String algo) throws NoSuchAlgorithmException,62NoSuchProviderException, InvalidKeyException {63Mac mac;64try {65mac = Mac.getInstance(algo, "SunJCE");66} catch (NoSuchAlgorithmException nsae) {67// depending on Solaris configuration,68// it can support HMAC or not with Mac69System.out.println("Expected NoSuchAlgorithmException thrown: "70+ nsae);71return;72}7374byte[] plain = new byte[MESSAGE_SIZE];75for (int i = 0; i < MESSAGE_SIZE; i++) {76plain[i] = (byte) (i % 256);77}7879byte[] tail = new byte[plain.length - OFFSET];80System.arraycopy(plain, OFFSET, tail, 0, tail.length);8182SecureRandom srdm = new SecureRandom();83byte[] keyVal = new byte[KEY_SIZE];84srdm.nextBytes(keyVal);85SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");8687mac.init(keySpec);88byte[] result1 = mac.doFinal(plain);8990mac.reset();91mac.update(plain[0]);92mac.update(plain, 1, OFFSET - 1);93byte[] result2 = mac.doFinal(tail);9495if (!java.util.Arrays.equals(result1, result2)) {96throw new RuntimeException("result1 and result2 are not the same");97}98}99100}101102103