Path: blob/master/test/jdk/com/sun/crypto/provider/NSASuiteB/TestHmacSHAOids.java
41155 views
/*1* Copyright (c) 2015, 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.util.Arrays;27import java.util.List;2829import javax.crypto.KeyGenerator;30import javax.crypto.Mac;31import javax.crypto.SecretKey;3233/*34* @test35* @bug 807528636* @summary Test the HmacSHA algorithm OIDs in JDK.37* OID and Algorithm transformation string should match.38* Both could be able to be used to generate the algorithm instance.39* @run main TestHmacSHAOids40*/41public class TestHmacSHAOids {4243private static final String PROVIDER_NAME = "SunJCE";44private static final byte[] INPUT = "1234567890".getBytes();4546private static final List<DataTuple> DATA = Arrays.asList(47new DataTuple("1.2.840.113549.2.7", "HmacSHA1"),48new DataTuple("1.2.840.113549.2.8", "HmacSHA224"),49new DataTuple("1.2.840.113549.2.9", "HmacSHA256"),50new DataTuple("1.2.840.113549.2.10", "HmacSHA384"),51new DataTuple("1.2.840.113549.2.11", "HmacSHA512"));5253public static void main(String[] args) throws Exception {54for (DataTuple dataTuple : DATA) {55runTest(dataTuple);56System.out.println("passed");57}58System.out.println("All tests passed");59}6061private static void runTest(DataTuple dataTuple)62throws NoSuchAlgorithmException, NoSuchProviderException,63InvalidKeyException {64Mac mcAlgorithm = Mac.getInstance(dataTuple.algorithm,65PROVIDER_NAME);66Mac mcOid = Mac.getInstance(dataTuple.oid, PROVIDER_NAME);6768if (mcAlgorithm == null) {69throw new RuntimeException(String.format(70"Test failed: Mac using algorithm "71+ "string %s getInstance failed.%n",72dataTuple.algorithm));73}7475if (mcOid == null) {76throw new RuntimeException(String.format(77"Test failed: Mac using OID %s getInstance failed.%n",78dataTuple.oid));79}8081if (!mcAlgorithm.getAlgorithm().equals(dataTuple.algorithm)) {82throw new RuntimeException(String.format(83"Test failed: Mac using algorithm string %s getInstance "84+ "doesn't generate expected algorithm.%n",85dataTuple.algorithm));86}8788KeyGenerator kg = KeyGenerator.getInstance(dataTuple.algorithm,89PROVIDER_NAME);90SecretKey key = kg.generateKey();9192mcAlgorithm.init(key);93mcAlgorithm.update(INPUT);9495mcOid.init(key);96mcOid.update(INPUT);9798// Comparison99if (!Arrays.equals(mcAlgorithm.doFinal(), mcOid.doFinal())) {100throw new RuntimeException("Digest comparison failed: "101+ "the two MACs are not the same");102}103}104105private static class DataTuple {106107private final String oid;108private final String algorithm;109110private DataTuple(String oid, String algorithm) {111this.oid = oid;112this.algorithm = algorithm;113}114}115}116117118