src/java.base/share/classes/com/sun/crypto/provider/PBKDF2KeyImpl.java
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 54182 2e586b74722e
child 59024 b046ba510bbc
permissions -rw-r--r--
8229999: Apply java.io.Serial annotations to security types in java.base Reviewed-by: rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54182
2e586b74722e 8218723: Use SunJCE Mac in SecretKeyFactory PBKDF2 implementation
jnimeh
parents: 51504
diff changeset
     2
 * Copyright (c) 2005, 2019, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4348
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4348
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4348
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4348
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4348
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
    28
import java.io.ObjectStreamException;
48560
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
    29
import java.lang.ref.Reference;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 16910
diff changeset
    34
import java.util.Locale;
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 25859
diff changeset
    35
import java.security.MessageDigest;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.KeyRep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.Mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.spec.PBEKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
46097
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    44
import jdk.internal.ref.CleanerFactory;
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    45
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class represents a PBE key derived using PBKDF2 defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * in PKCS#5 v2.0. meaning that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * 1) the password must consist of characters which will be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    to bytes using UTF-8 character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 2) salt, iteration count, and to be derived key length are supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 54182
diff changeset
    58
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final long serialVersionUID = -2234868909660948157L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private char[] passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private byte[] salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int iterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private Mac prf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static byte[] getPasswordBytes(char[] passwd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Charset utf8 = Charset.forName("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        CharBuffer cb = CharBuffer.wrap(passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ByteBuffer bb = utf8.encode(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int len = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        byte[] passwdBytes = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        bb.get(passwdBytes, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return passwdBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Creates a PBE key from a given PBE key specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
46097
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    83
     * @param keySpec the given PBE key specification
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    84
     * @param prfAlgo the given PBE key algorithm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    PBKDF2KeyImpl(PBEKeySpec keySpec, String prfAlgo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        char[] passwd = keySpec.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (passwd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // Should allow an empty password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            this.passwd = new char[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            this.passwd = passwd.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // Convert the password from char[] to byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        byte[] passwdBytes = getPasswordBytes(this.passwd);
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
    97
        // remove local copy
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
    98
        if (passwd != null) Arrays.fill(passwd, '\0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   101
            this.salt = keySpec.getSalt();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   102
            if (salt == null) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   103
                throw new InvalidKeySpecException("Salt not found");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   104
            }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   105
            this.iterCount = keySpec.getIterationCount();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   106
            if (iterCount == 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   107
                throw new InvalidKeySpecException("Iteration count not found");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   108
            } else if (iterCount < 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   109
                throw new InvalidKeySpecException("Iteration count is negative");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   110
            }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   111
            int keyLength = keySpec.getKeyLength();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   112
            if (keyLength == 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   113
                throw new InvalidKeySpecException("Key length not found");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   114
            } else if (keyLength < 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   115
                throw new InvalidKeySpecException("Key length is negative");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   116
            }
54182
2e586b74722e 8218723: Use SunJCE Mac in SecretKeyFactory PBKDF2 implementation
jnimeh
parents: 51504
diff changeset
   117
            this.prf = Mac.getInstance(prfAlgo, SunJCE.getInstance());
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   118
            this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // not gonna happen; re-throw just in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            InvalidKeySpecException ike = new InvalidKeySpecException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ike.initCause(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw ike;
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   124
        } finally {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   125
            Arrays.fill(passwdBytes, (byte) 0x00);
46097
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
   126
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   127
            // Use the cleaner to zero the key when no longer referenced
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   128
            final byte[] k = this.key;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   129
            final char[] p = this.passwd;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   130
            CleanerFactory.cleaner().register(this,
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   131
                    () -> {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   132
                        Arrays.fill(k, (byte) 0x00);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   133
                        Arrays.fill(p, '\0');
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   134
                    });
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   135
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   138
    private static byte[] deriveKey(final Mac prf, final byte[] password,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   139
            byte[] salt, int iterCount, int keyLengthInBit) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int keyLength = keyLengthInBit/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        byte[] key = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int hlen = prf.getMacLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            int intL = (keyLength + hlen - 1)/hlen; // ceiling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            int intR = keyLength - (intL - 1)*hlen; // residue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            byte[] ui = new byte[hlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            byte[] ti = new byte[hlen];
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   148
            // SecretKeySpec cannot be used, since password can be empty here.
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   149
            SecretKey macKey = new SecretKey() {
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 54182
diff changeset
   150
                @java.io.Serial
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   151
                private static final long serialVersionUID = 7874493593505141603L;
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   152
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   153
                public String getAlgorithm() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   154
                    return prf.getAlgorithm();
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   155
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   156
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   157
                public String getFormat() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   158
                    return "RAW";
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   159
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   160
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   161
                public byte[] getEncoded() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   162
                    return password;
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   163
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   164
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   165
                public int hashCode() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   166
                    return Arrays.hashCode(password) * 41 +
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 16910
diff changeset
   167
                      prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode();
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   168
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   169
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   170
                public boolean equals(Object obj) {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   171
                    if (this == obj) return true;
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   172
                    if (this.getClass() != obj.getClass()) return false;
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   173
                    SecretKey sk = (SecretKey)obj;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   174
                    return prf.getAlgorithm().equalsIgnoreCase(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   175
                        sk.getAlgorithm()) &&
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 25859
diff changeset
   176
                        MessageDigest.isEqual(password, sk.getEncoded());
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   177
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   178
            };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            prf.init(macKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            byte[] ibytes = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            for (int i = 1; i <= intL; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                prf.update(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                ibytes[3] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                ibytes[2] = (byte) ((i >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                ibytes[1] = (byte) ((i >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                ibytes[0] = (byte) ((i >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                prf.update(ibytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                prf.doFinal(ui, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                System.arraycopy(ui, 0, ti, 0, ui.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                for (int j = 2; j <= iterCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    prf.update(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    prf.doFinal(ui, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    // XOR the intermediate Ui's together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    for (int k = 0; k < ui.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        ti[k] ^= ui[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (i == intL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    System.arraycopy(ti, 0, key, (i-1)*hlen, intR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    System.arraycopy(ti, 0, key, (i-1)*hlen, hlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } catch (GeneralSecurityException gse) {
54182
2e586b74722e 8218723: Use SunJCE Mac in SecretKeyFactory PBKDF2 implementation
jnimeh
parents: 51504
diff changeset
   207
            throw new RuntimeException("Error deriving PBKDF2 keys", gse);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public byte[] getEncoded() {
48560
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   213
        // The key is zeroized by finalize()
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   214
        // The reachability fence ensures finalize() isn't called early
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   215
        byte[] result = key.clone();
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   216
        Reference.reachabilityFence(this);
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   217
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return "PBKDF2With" + prf.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public int getIterationCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return iterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public char[] getPassword() {
48560
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   229
        // The password is zeroized by finalize()
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   230
        // The reachability fence ensures finalize() isn't called early
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   231
        char[] result = passwd.clone();
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   232
        Reference.reachabilityFence(this);
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
   233
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public byte[] getSalt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return salt.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Calculates a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i = 1; i < this.key.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            retval += this.key[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 16910
diff changeset
   253
        return(retval ^= getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (!(obj instanceof SecretKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        SecretKey that = (SecretKey) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (!(that.getAlgorithm().equalsIgnoreCase(getAlgorithm())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (!(that.getFormat().equalsIgnoreCase("RAW")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        byte[] thatEncoded = that.getEncoded();
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   270
        boolean ret = MessageDigest.isEqual(key, thatEncoded);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 48560
diff changeset
   271
        Arrays.fill(thatEncoded, (byte)0x00);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Replace the PBE key to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @return the standard KeyRep object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws ObjectStreamException if a new object representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * this PBE key could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 54182
diff changeset
   283
    @java.io.Serial
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   284
    private Object writeReplace() throws ObjectStreamException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return new KeyRep(KeyRep.Type.SECRET, getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                              getFormat(), getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}