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