632N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
632N/A *
632N/A * This code is free software; you can redistribute it and/or modify it
632N/A * under the terms of the GNU General Public License version 2 only, as
632N/A * published by the Free Software Foundation.
632N/A *
632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
632N/A * version 2 for more details (a copy is included in the LICENSE file that
632N/A * accompanied this code).
632N/A *
632N/A * You should have received a copy of the GNU General Public License version
632N/A * 2 along with this work; if not, write to the Free Software Foundation,
632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
632N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
632N/A */
632N/A
632N/A/**
632N/A * @test
632N/A * @bug 4925866
632N/A * @summary ensures various salt lengths can be used for
632N/A * HmacPBESHA1.
632N/A * @author Valerie Peng
632N/A */
632N/A
632N/Aimport java.io.*;
632N/Aimport java.util.*;
632N/Aimport java.security.*;
632N/Aimport javax.crypto.*;
632N/Aimport javax.crypto.spec.*;
632N/Aimport javax.crypto.interfaces.PBEKey;
632N/A
632N/Apublic class HmacSaltLengths {
632N/A
632N/A private static void runTest(String alg, byte[] plaintext,
632N/A char[] password, Provider p)
632N/A throws Exception {
632N/A Mac mac = Mac.getInstance(alg, p);
632N/A PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
632N/A SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
632N/A SecretKey key = keyFac.generateSecret(pbeKeySpec);
632N/A
632N/A System.out.println("testing parameters with 4-byte salt...");
632N/A PBEParameterSpec pbeParamSpec = new PBEParameterSpec
632N/A (new byte[4], 1024);
632N/A try {
632N/A mac.init(key, pbeParamSpec);
632N/A throw new Exception("ERROR: should throw IAPE for short salts");
632N/A } catch (InvalidAlgorithmParameterException iape) {
632N/A // expected; do nothing
632N/A }
632N/A
632N/A System.out.println("testing parameters with 8-byte salt...");
pbeParamSpec = new PBEParameterSpec(new byte[8], 1024);
mac.init(key, pbeParamSpec);
mac.doFinal(plaintext);
System.out.println("testing parameters with 20-byte salt...");
pbeParamSpec = new PBEParameterSpec(new byte[20], 1024);
mac.init(key, pbeParamSpec);
mac.doFinal(plaintext);
System.out.println("testing parameters with 30-byte salt...");
pbeParamSpec = new PBEParameterSpec(new byte[30], 1024);
mac.init(key, pbeParamSpec);
mac.doFinal(plaintext);
System.out.println("passed: " + alg);
}
public static void main(String[] argv) throws Exception {
byte[] input = new byte[1024];
new SecureRandom().nextBytes(input);
char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
long start = System.currentTimeMillis();
Provider p = Security.getProvider("SunJCE");
System.out.println("Testing provider " + p.getName() + "...");
runTest("HmacPBESHA1", input, PASSWD, p);
System.out.println("All tests passed");
long stop = System.currentTimeMillis();
System.out.println("Done (" + (stop - start) + " ms).");
}
}
class MyPBEKey implements PBEKey {
char[] passwd;
byte[] salt;
int iCount;
MyPBEKey(char[] passwd, byte[] salt, int iCount) {
this.passwd = passwd;
this.salt = salt;
this.iCount = iCount;
}
public char[] getPassword() { return passwd; }
public byte[] getSalt() { return salt; }
public int getIterationCount() { return iCount; }
public String getAlgorithm() { return "PBE"; }
public String getFormat() { return "RAW"; }
public byte[] getEncoded() { return null; }
}