jdk/test/com/sun/crypto/provider/Mac/HmacSaltLengths.java
author katleman
Thu, 05 Jan 2012 08:42:37 -0800
changeset 11376 075fe3928b7f
parent 5506 202f599c92aa
child 14405 e7fff80005c1
permissions -rw-r--r--
Added tag jdk8-b20 for changeset 90e14ec89395
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4925866
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary ensures various salt lengths can be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * HmacPBESHA1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.interfaces.PBEKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class HmacSaltLengths {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static void runTest(String alg, byte[] plaintext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                                char[] password, Provider p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        Mac mac = Mac.getInstance(alg, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        SecretKey key = keyFac.generateSecret(pbeKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.println("testing parameters with 4-byte salt...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            (new byte[4], 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            mac.init(key, pbeParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new Exception("ERROR: should throw IAPE for short salts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            // expected; do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.println("testing parameters with 8-byte salt...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        pbeParamSpec = new PBEParameterSpec(new byte[8], 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        mac.init(key, pbeParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        mac.doFinal(plaintext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.out.println("testing parameters with 20-byte salt...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        pbeParamSpec = new PBEParameterSpec(new byte[20], 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        mac.init(key, pbeParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        mac.doFinal(plaintext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println("testing parameters with 30-byte salt...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        pbeParamSpec = new PBEParameterSpec(new byte[30], 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        mac.init(key, pbeParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        mac.doFinal(plaintext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.out.println("passed: " + alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        byte[] input = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        new SecureRandom().nextBytes(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Provider p = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.out.println("Testing provider " + p.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        runTest("HmacPBESHA1", input, PASSWD, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println("All tests passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.println("Done (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
class MyPBEKey implements PBEKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    char[] passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    byte[] salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    int iCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    MyPBEKey(char[] passwd, byte[] salt, int iCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.passwd = passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.salt = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.iCount = iCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public char[] getPassword() { return passwd; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public byte[] getSalt() { return salt; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public int getIterationCount() { return iCount; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public String getAlgorithm() { return "PBE"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public String getFormat() { return "RAW"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public byte[] getEncoded() { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}