1 /*
2 * Copyright (c) 1998, 2018, 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 */
69 boolean success = true;
70 for (String alg : algorithms) {
71 try {
72 doTest(alg, p);
73 } catch (Exception e) {
74 System.out.println("Unexpected exception: " + e);
75 e.printStackTrace();
76 success = false;
77 }
78 }
79
80 if (!success) {
81 throw new RuntimeException("Test failed");
82 }
83 }
84
85 private void doTest(String algo, Provider provider)
86 throws NoSuchAlgorithmException, NoSuchProviderException,
87 InvalidKeyException {
88 System.out.println("Test " + algo);
89 Mac mac;
90 try {
91 mac = Mac.getInstance(algo, provider);
92 } catch (NoSuchAlgorithmException nsae) {
93 if ("SunPKCS11-Solaris".equals(provider.getName())) {
94 // depending on Solaris configuration,
95 // it can support HMAC or not with Mac
96 System.out.println("Expected NoSuchAlgorithmException thrown: "
97 + nsae);
98 return;
99 }
100 throw nsae;
101 }
102
103 byte[] plain = new byte[MESSAGE_SIZE];
104 for (int i = 0; i < MESSAGE_SIZE; i++) {
105 plain[i] = (byte) (i % 256);
106 }
107
108 byte[] tail = new byte[plain.length - OFFSET];
109 System.arraycopy(plain, OFFSET, tail, 0, tail.length);
110
111 SecureRandom srdm = new SecureRandom();
112 byte[] keyVal = new byte[KEY_SIZE];
113 srdm.nextBytes(keyVal);
114 SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");
115
116 mac.init(keySpec);
117 byte[] result1 = mac.doFinal(plain);
118
119 mac.reset();
120 mac.update(plain[0]);
121 mac.update(plain, 1, OFFSET - 1);
|
1 /*
2 * Copyright (c) 1998, 2020, 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 */
69 boolean success = true;
70 for (String alg : algorithms) {
71 try {
72 doTest(alg, p);
73 } catch (Exception e) {
74 System.out.println("Unexpected exception: " + e);
75 e.printStackTrace();
76 success = false;
77 }
78 }
79
80 if (!success) {
81 throw new RuntimeException("Test failed");
82 }
83 }
84
85 private void doTest(String algo, Provider provider)
86 throws NoSuchAlgorithmException, NoSuchProviderException,
87 InvalidKeyException {
88 System.out.println("Test " + algo);
89 Mac mac = Mac.getInstance(algo, provider);
90
91 byte[] plain = new byte[MESSAGE_SIZE];
92 for (int i = 0; i < MESSAGE_SIZE; i++) {
93 plain[i] = (byte) (i % 256);
94 }
95
96 byte[] tail = new byte[plain.length - OFFSET];
97 System.arraycopy(plain, OFFSET, tail, 0, tail.length);
98
99 SecureRandom srdm = new SecureRandom();
100 byte[] keyVal = new byte[KEY_SIZE];
101 srdm.nextBytes(keyVal);
102 SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");
103
104 mac.init(keySpec);
105 byte[] result1 = mac.doFinal(plain);
106
107 mac.reset();
108 mac.update(plain[0]);
109 mac.update(plain, 1, OFFSET - 1);
|