1 /*
2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.security.InvalidKeyException;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.NoSuchProviderException;
27 import java.util.Arrays;
28 import java.util.List;
29 
30 import javax.crypto.KeyGenerator;
31 import javax.crypto.Mac;
32 import javax.crypto.SecretKey;
33 
34 /*
35  * @test
36  * @bug 8075286
37  * @summary Test the HmacSHA algorithm OIDs in JDK.
38  *          OID and Algorithm transformation string should match.
39  *          Both could be able to be used to generate the algorithm instance.
40  * @run main TestHmacSHAOids
41  */
42 public class TestHmacSHAOids {
43 
44     private static final String PROVIDER_NAME = "SunJCE";
45     private static final byte[] INPUT = "1234567890".getBytes();
46 
47     private static final List<DataTuple> DATA = Arrays.asList(
48             new DataTuple("1.2.840.113549.2.7", "HmacSHA1"),
49             new DataTuple("1.2.840.113549.2.8", "HmacSHA224"),
50             new DataTuple("1.2.840.113549.2.9", "HmacSHA256"),
51             new DataTuple("1.2.840.113549.2.10", "HmacSHA384"),
52             new DataTuple("1.2.840.113549.2.11", "HmacSHA512"));
53 
main(String[] args)54     public static void main(String[] args) throws Exception {
55         for (DataTuple dataTuple : DATA) {
56             runTest(dataTuple);
57             System.out.println("passed");
58         }
59         System.out.println("All tests passed");
60     }
61 
runTest(DataTuple dataTuple)62     private static void runTest(DataTuple dataTuple)
63             throws NoSuchAlgorithmException, NoSuchProviderException,
64             InvalidKeyException {
65         Mac mcAlgorithm = Mac.getInstance(dataTuple.algorithm,
66                 PROVIDER_NAME);
67         Mac mcOid = Mac.getInstance(dataTuple.oid, PROVIDER_NAME);
68 
69         if (mcAlgorithm == null) {
70             throw new RuntimeException(String.format(
71                     "Test failed: Mac using algorithm "
72                             + "string %s getInstance failed.%n",
73                     dataTuple.algorithm));
74         }
75 
76         if (mcOid == null) {
77             throw new RuntimeException(String.format(
78                     "Test failed: Mac using OID %s getInstance failed.%n",
79                     dataTuple.oid));
80         }
81 
82         if (!mcAlgorithm.getAlgorithm().equals(dataTuple.algorithm)) {
83             throw new RuntimeException(String.format(
84                     "Test failed: Mac using algorithm string %s getInstance "
85                             + "doesn't generate expected algorithm.%n",
86                     dataTuple.algorithm));
87         }
88 
89         KeyGenerator kg = KeyGenerator.getInstance(dataTuple.algorithm,
90                 PROVIDER_NAME);
91         SecretKey key = kg.generateKey();
92 
93         mcAlgorithm.init(key);
94         mcAlgorithm.update(INPUT);
95 
96         mcOid.init(key);
97         mcOid.update(INPUT);
98 
99         // Comparison
100         if (!Arrays.equals(mcAlgorithm.doFinal(), mcOid.doFinal())) {
101             throw new RuntimeException("Digest comparison failed: "
102                     + "the two MACs are not the same");
103         }
104     }
105 
106     private static class DataTuple {
107 
108         private final String oid;
109         private final String algorithm;
110 
DataTuple(String oid, String algorithm)111         private DataTuple(String oid, String algorithm) {
112             this.oid = oid;
113             this.algorithm = algorithm;
114         }
115     }
116 }
117