2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4925866
|
|
27 |
* @summary ensures various salt lengths can be used for
|
|
28 |
* HmacPBESHA1.
|
|
29 |
* @author Valerie Peng
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.util.*;
|
|
34 |
import java.security.*;
|
|
35 |
import javax.crypto.*;
|
|
36 |
import javax.crypto.spec.*;
|
|
37 |
import javax.crypto.interfaces.PBEKey;
|
|
38 |
|
|
39 |
public class HmacSaltLengths {
|
|
40 |
|
|
41 |
private static void runTest(String alg, byte[] plaintext,
|
|
42 |
char[] password, Provider p)
|
|
43 |
throws Exception {
|
|
44 |
Mac mac = Mac.getInstance(alg, p);
|
|
45 |
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
|
|
46 |
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
|
|
47 |
SecretKey key = keyFac.generateSecret(pbeKeySpec);
|
|
48 |
|
|
49 |
System.out.println("testing parameters with 4-byte salt...");
|
|
50 |
PBEParameterSpec pbeParamSpec = new PBEParameterSpec
|
|
51 |
(new byte[4], 1024);
|
|
52 |
try {
|
|
53 |
mac.init(key, pbeParamSpec);
|
|
54 |
throw new Exception("ERROR: should throw IAPE for short salts");
|
|
55 |
} catch (InvalidAlgorithmParameterException iape) {
|
|
56 |
// expected; do nothing
|
|
57 |
}
|
|
58 |
|
|
59 |
System.out.println("testing parameters with 8-byte salt...");
|
|
60 |
pbeParamSpec = new PBEParameterSpec(new byte[8], 1024);
|
|
61 |
mac.init(key, pbeParamSpec);
|
|
62 |
mac.doFinal(plaintext);
|
|
63 |
|
|
64 |
System.out.println("testing parameters with 20-byte salt...");
|
|
65 |
pbeParamSpec = new PBEParameterSpec(new byte[20], 1024);
|
|
66 |
mac.init(key, pbeParamSpec);
|
|
67 |
mac.doFinal(plaintext);
|
|
68 |
|
|
69 |
System.out.println("testing parameters with 30-byte salt...");
|
|
70 |
pbeParamSpec = new PBEParameterSpec(new byte[30], 1024);
|
|
71 |
mac.init(key, pbeParamSpec);
|
|
72 |
mac.doFinal(plaintext);
|
|
73 |
|
|
74 |
System.out.println("passed: " + alg);
|
|
75 |
}
|
|
76 |
|
|
77 |
public static void main(String[] argv) throws Exception {
|
|
78 |
byte[] input = new byte[1024];
|
|
79 |
new SecureRandom().nextBytes(input);
|
|
80 |
char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
|
|
81 |
long start = System.currentTimeMillis();
|
|
82 |
Provider p = Security.getProvider("SunJCE");
|
|
83 |
System.out.println("Testing provider " + p.getName() + "...");
|
|
84 |
runTest("HmacPBESHA1", input, PASSWD, p);
|
|
85 |
System.out.println("All tests passed");
|
|
86 |
long stop = System.currentTimeMillis();
|
|
87 |
System.out.println("Done (" + (stop - start) + " ms).");
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
class MyPBEKey implements PBEKey {
|
|
92 |
char[] passwd;
|
|
93 |
byte[] salt;
|
|
94 |
int iCount;
|
|
95 |
MyPBEKey(char[] passwd, byte[] salt, int iCount) {
|
|
96 |
this.passwd = passwd;
|
|
97 |
this.salt = salt;
|
|
98 |
this.iCount = iCount;
|
|
99 |
}
|
|
100 |
public char[] getPassword() { return passwd; }
|
|
101 |
public byte[] getSalt() { return salt; }
|
|
102 |
public int getIterationCount() { return iCount; }
|
|
103 |
public String getAlgorithm() { return "PBE"; }
|
|
104 |
public String getFormat() { return "RAW"; }
|
|
105 |
public byte[] getEncoded() { return null; }
|
|
106 |
}
|