Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/HmacSaltLengths.java
41159 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 492586626* @summary ensures various salt lengths can be used for27* HmacPBESHA1.28* @author Valerie Peng29* @key randomness30*/3132import java.io.*;33import java.util.*;34import java.security.*;35import javax.crypto.*;36import javax.crypto.spec.*;37import javax.crypto.interfaces.PBEKey;3839public class HmacSaltLengths {4041private static final String[] ALGOS = {42"HmacPBESHA1",43"PBEWithHmacSHA1",44"PBEWithHmacSHA224",45"PBEWithHmacSHA256",46"PBEWithHmacSHA384",47"PBEWithHmacSHA512"48};4950private static void runTest(String alg, byte[] plaintext,51char[] password, Provider p)52throws Exception {53Mac mac = Mac.getInstance(alg, p);54PBEKeySpec pbeKeySpec = new PBEKeySpec(password);55SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);56SecretKey key = keyFac.generateSecret(pbeKeySpec);5758System.out.println("testing parameters with 4-byte salt...");59PBEParameterSpec pbeParamSpec = new PBEParameterSpec60(new byte[4], 1024);61try {62mac.init(key, pbeParamSpec);63throw new Exception("ERROR: should throw IAPE for short salts");64} catch (InvalidAlgorithmParameterException iape) {65// expected; do nothing66}6768System.out.println("testing parameters with 8-byte salt...");69pbeParamSpec = new PBEParameterSpec(new byte[8], 1024);70mac.init(key, pbeParamSpec);71mac.doFinal(plaintext);7273System.out.println("testing parameters with 20-byte salt...");74pbeParamSpec = new PBEParameterSpec(new byte[20], 1024);75mac.init(key, pbeParamSpec);76mac.doFinal(plaintext);7778System.out.println("testing parameters with 30-byte salt...");79pbeParamSpec = new PBEParameterSpec(new byte[30], 1024);80mac.init(key, pbeParamSpec);81mac.doFinal(plaintext);8283System.out.println("passed: " + alg);84}8586public static void main(String[] argv) throws Exception {87byte[] input = new byte[1024];88new SecureRandom().nextBytes(input);89char[] PASSWD = { 'p','a','s','s','w','o','r','d' };90long start = System.currentTimeMillis();91Provider p = Security.getProvider("SunJCE");92System.out.println("Testing provider " + p.getName() + "...");93for (String algo : ALGOS) {94runTest(algo, input, PASSWD, p);95}96System.out.println("All tests passed");97long stop = System.currentTimeMillis();98System.out.println("Done (" + (stop - start) + " ms).");99}100}101102class MyPBEKey implements PBEKey {103char[] passwd;104byte[] salt;105int iCount;106MyPBEKey(char[] passwd, byte[] salt, int iCount) {107this.passwd = passwd;108this.salt = salt;109this.iCount = iCount;110}111public char[] getPassword() { return passwd; }112public byte[] getSalt() { return salt; }113public int getIterationCount() { return iCount; }114public String getAlgorithm() { return "PBE"; }115public String getFormat() { return "RAW"; }116public byte[] getEncoded() { return null; }117}118119120