src/java.base/share/classes/sun/security/provider/AbstractHashDrbg.java
author ascarpino
Fri, 20 Jul 2018 09:55:15 -0700
changeset 51216 e429a304c97d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8204196: integer cleanup Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     1
/*
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    28
import sun.security.util.HexDumpEncoder;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    29
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
    30
import java.util.ArrayList;
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
    31
import java.util.List;
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    32
import java.util.Locale;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    33
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    34
public abstract class AbstractHashDrbg extends AbstractDrbg {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    35
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    36
    protected int outLen;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    37
    protected int seedLen;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    38
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    39
    private static int alg2strength(String algorithm) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    40
        switch (algorithm.toUpperCase(Locale.ROOT)) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    41
            case "SHA-224":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    42
            case "SHA-512/224":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    43
                return 192;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    44
            case "SHA-256":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    45
            case "SHA-512/256":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    46
            case "SHA-384":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    47
            case "SHA-512":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    48
                return 256;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    49
            default:
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    50
                throw new IllegalArgumentException(algorithm +
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    51
                        " not supported in Hash_DBRG");
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    52
        }
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
    protected void chooseAlgorithmAndStrength() {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    56
        if (requestedAlgorithm != null) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    57
            algorithm = requestedAlgorithm.toUpperCase(Locale.ROOT);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    58
            int supportedStrength = alg2strength(algorithm);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    59
            if (requestedInstantiationSecurityStrength >= 0) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    60
                int tryStrength = getStandardStrength(
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    61
                        requestedInstantiationSecurityStrength);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    62
                if (tryStrength > supportedStrength) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    63
                    throw new IllegalArgumentException(algorithm +
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    64
                            " does not support strength " +
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    65
                            requestedInstantiationSecurityStrength);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    66
                }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    67
                this.securityStrength = tryStrength;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    68
            } else {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    69
                this.securityStrength = DEFAULT_STRENGTH > supportedStrength ?
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    70
                        supportedStrength : DEFAULT_STRENGTH;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    71
            }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    72
        } else {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    73
            int tryStrength = (requestedInstantiationSecurityStrength < 0) ?
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    74
                    DEFAULT_STRENGTH : requestedInstantiationSecurityStrength;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    75
            tryStrength = getStandardStrength(tryStrength);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    76
            // The default algorithm which is enough for all strengths.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    77
            // Remember to sync with "securerandom.drbg.config" in java.security
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    78
            algorithm = "SHA-256";
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    79
            this.securityStrength = tryStrength;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    80
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    81
        switch (algorithm.toUpperCase(Locale.ROOT)) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    82
            case "SHA-224":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    83
            case "SHA-512/224":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    84
                this.seedLen = 440 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    85
                this.outLen = 224 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    86
                break;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    87
            case "SHA-256":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    88
            case "SHA-512/256":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    89
                this.seedLen = 440 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    90
                this.outLen = 256 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    91
                break;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    92
            case "SHA-384":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    93
                this.seedLen = 888 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    94
                this.outLen = 384 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    95
                break;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    96
            case "SHA-512":
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    97
                this.seedLen = 888 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    98
                this.outLen = 512 / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    99
                break;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   100
            default:
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   101
                throw new IllegalArgumentException(algorithm +
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   102
                        " not supported in Hash_DBRG");
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   103
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   104
        this.minLength = this.securityStrength / 8;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   105
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   106
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   107
    @Override
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   108
    public void instantiateAlgorithm(byte[] entropy) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   109
        if (debug != null) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   110
            debug.println(this, "instantiate");
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   111
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   112
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   113
        // 800-90Ar1 10.1.1.2: Hash_DRBG Instantiate Process.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   114
        // 800-90Ar1 10.1.2.3: Hmac_DRBG Instantiate Process.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   115
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   116
        // Step 1: entropy_input || nonce || personalization_string.
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   117
        List<byte[]> inputs = new ArrayList<>(3);
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   118
        inputs.add(entropy);
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   119
        inputs.add(nonce);
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   120
        if (personalizationString != null) {
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   121
            inputs.add(personalizationString);
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   122
        }
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   123
        hashReseedInternal(inputs);
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   124
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   125
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   126
    @Override
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   127
    protected void reseedAlgorithm(
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   128
            byte[] ei,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   129
            byte[] additionalInput) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   130
        if (debug != null) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   131
            debug.println(this, "reseedAlgorithm\n" +
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   132
                    new HexDumpEncoder().encodeBuffer(ei) + "\n" +
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   133
                    ((additionalInput == null) ? "" :
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   134
                        new HexDumpEncoder().encodeBuffer(additionalInput)));
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   135
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   136
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   137
        // 800-90Ar1 10.1.1.3: Hash_DRBG Reseed Process.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   138
        // 800-90Ar1 10.1.2.4: Hmac_DRBG Reseed Process.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   139
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   140
        // Step 1: entropy_input || additional_input.
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   141
        List<byte[]> inputs = new ArrayList<>(2);
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   142
        inputs.add(ei);
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   143
        if (additionalInput != null) {
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   144
            inputs.add(additionalInput);
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   145
        }
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   146
        hashReseedInternal(inputs);
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   147
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   148
39481
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   149
    /**
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   150
     * Operates on multiple inputs.
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   151
     * @param inputs not null, each element neither null
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   152
     */
63ceb7ef04d4 8158589: Possible integer overflow issues for DRBG
weijun
parents: 38853
diff changeset
   153
    protected abstract void hashReseedInternal(List<byte[]> inputs);
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   154
}