blob: 219b361900976eb6847d6774e30195308c3ac398 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
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
32import java.io.*;
33import java.util.*;
34import java.security.*;
35import javax.crypto.*;
36import javax.crypto.spec.*;
37import javax.crypto.interfaces.PBEKey;
38
39public 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
91class 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}