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