src/java.base/share/classes/sun/security/provider/MoreDrbgParameters.java
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 47216 71c04702a3d5
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:
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     4
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    10
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    15
 * accompanied this code).
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    16
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    20
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    23
 * questions.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    24
 */
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    25
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    26
package sun.security.provider;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    27
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    28
import java.io.IOException;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    29
import java.io.Serializable;
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    30
import java.security.DrbgParameters;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    31
import java.security.SecureRandomParameters;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    32
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    33
/**
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    34
 * Exported and non-exported parameters that can be used by DRBGs.
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    35
 */
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    36
public class MoreDrbgParameters implements SecureRandomParameters, Serializable {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    37
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
    38
    @java.io.Serial
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    39
    private static final long serialVersionUID = 9L;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    40
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    41
    final transient EntropySource es;
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    42
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    43
    final String mech;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    44
    final String algorithm;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    45
    final boolean usedf;
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    46
    final int strength;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    47
    final DrbgParameters.Capability capability;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    48
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    49
    // The following 2 fields will be reassigned in readObject and
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    50
    // thus cannot be final
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    51
    byte[] nonce;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    52
    byte[] personalizationString;
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    53
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    54
    /**
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    55
     * Creates a new {@code MoreDrbgParameters} object.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    56
     *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    57
     * @param es the {@link EntropySource} to use. If set to {@code null},
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    58
     *           a default entropy source will be used.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    59
     * @param mech mech name. If set to {@code null}, the one in
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    60
     *             securerandom.drbg.config is used. This argument is ignored
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    61
     *             when passing to HashDrbg/HmacDrbg/CtrDrbg.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    62
     * @param algorithm the requested algorithm to use. If set to {@code null},
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    63
     *                  the algorithm will be decided by strength.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    64
     * @param nonce the nonce to use. If set to {@code null},
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    65
     *              a nonce will be assigned.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    66
     * @param usedf whether a derivation function should be used
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    67
     * @param config a {@link DrbgParameters.Instantiation} object
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    68
     */
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    69
    public MoreDrbgParameters(EntropySource es, String mech,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    70
                              String algorithm, byte[] nonce, boolean usedf,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    71
                              DrbgParameters.Instantiation config) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    72
        this.mech = mech;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    73
        this.algorithm = algorithm;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    74
        this.es = es;
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    75
        this.nonce = (nonce == null) ? null : nonce.clone();
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    76
        this.usedf = usedf;
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    77
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    78
        this.strength = config.getStrength();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    79
        this.capability = config.getCapability();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    80
        this.personalizationString = config.getPersonalizationString();
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    81
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    82
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    83
    @Override
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    84
    public String toString() {
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    85
        return mech + "," + algorithm + "," + usedf + "," + strength
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    86
                + "," + capability + "," + personalizationString;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    87
    }
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    88
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
    89
    @java.io.Serial
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    90
    private void readObject(java.io.ObjectInputStream s)
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    91
            throws IOException, ClassNotFoundException {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    92
        s.defaultReadObject();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    93
        if (nonce != null) {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    94
            nonce = nonce.clone();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    95
        }
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    96
        if (personalizationString != null) {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    97
            personalizationString = personalizationString.clone();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    98
        }
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    99
        if (capability == null) {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
   100
            throw new IllegalArgumentException("Input data is corrupted");
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
   101
        }
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   102
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   103
}