src/java.base/share/classes/sun/security/provider/SHA5.java
author coffeys
Thu, 23 Aug 2018 11:37:14 +0100
changeset 51504 c9a3e3cac9c7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8209129: Further improvements to cipher buffer management Reviewed-by: weijun, igerasim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
     2
 * Copyright (c) 2002, 2016, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    28
import java.util.Arrays;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    29
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    31
import jdk.internal.HotSpotIntrinsicCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import static sun.security.provider.ByteArrayAccess.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class implements the Secure Hash Algorithm SHA-384 and SHA-512
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * developed by the National Institute of Standards and Technology along
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * with the National Security Agency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The two algorithms are almost identical. This file contains a base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * class SHA5 and two nested static subclasses as the classes to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * by the JCA framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>It implements java.security.MessageDigestSpi, and can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * through Java Cryptography Architecture (JCA), as a pluggable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * MessageDigest implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since       1.4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author      Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author      Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
abstract class SHA5 extends DigestBase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int ITERATION = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Constants for each round/iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final long[] ROUND_CONSTS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        0x428A2F98D728AE22L, 0x7137449123EF65CDL, 0xB5C0FBCFEC4D3B2FL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        0xE9B5DBA58189DBBCL, 0x3956C25BF348B538L, 0x59F111F1B605D019L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        0x923F82A4AF194F9BL, 0xAB1C5ED5DA6D8118L, 0xD807AA98A3030242L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        0x12835B0145706FBEL, 0x243185BE4EE4B28CL, 0x550C7DC3D5FFB4E2L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        0x72BE5D74F27B896FL, 0x80DEB1FE3B1696B1L, 0x9BDC06A725C71235L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        0xC19BF174CF692694L, 0xE49B69C19EF14AD2L, 0xEFBE4786384F25E3L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        0x0FC19DC68B8CD5B5L, 0x240CA1CC77AC9C65L, 0x2DE92C6F592B0275L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        0x4A7484AA6EA6E483L, 0x5CB0A9DCBD41FBD4L, 0x76F988DA831153B5L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        0x983E5152EE66DFABL, 0xA831C66D2DB43210L, 0xB00327C898FB213FL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        0xBF597FC7BEEF0EE4L, 0xC6E00BF33DA88FC2L, 0xD5A79147930AA725L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        0x06CA6351E003826FL, 0x142929670A0E6E70L, 0x27B70A8546D22FFCL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        0x2E1B21385C26C926L, 0x4D2C6DFC5AC42AEDL, 0x53380D139D95B3DFL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        0x650A73548BAF63DEL, 0x766A0ABB3C77B2A8L, 0x81C2C92E47EDAEE6L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        0x92722C851482353BL, 0xA2BFE8A14CF10364L, 0xA81A664BBC423001L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        0xC24B8B70D0F89791L, 0xC76C51A30654BE30L, 0xD192E819D6EF5218L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        0xD69906245565A910L, 0xF40E35855771202AL, 0x106AA07032BBD1B8L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        0x19A4C116B8D2D0C8L, 0x1E376C085141AB53L, 0x2748774CDF8EEB99L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        0x34B0BCB5E19B48A8L, 0x391C0CB3C5C95A63L, 0x4ED8AA4AE3418ACBL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        0x5B9CCA4F7763E373L, 0x682E6FF3D6B2B8A3L, 0x748F82EE5DEFB2FCL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        0x78A5636F43172F60L, 0x84C87814A1F0AB72L, 0x8CC702081A6439ECL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        0x90BEFFFA23631E28L, 0xA4506CEBDE82BDE9L, 0xBEF9A3F7B2C67915L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        0xC67178F2E372532BL, 0xCA273ECEEA26619CL, 0xD186B8C721C0C207L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        0xEADA7DD6CDE0EB1EL, 0xF57D4F7FEE6ED178L, 0x06F067AA72176FBAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        0x0A637DC5A2C898A6L, 0x113F9804BEF90DAEL, 0x1B710B35131C471BL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        0x28DB77F523047D84L, 0x32CAAB7B40C72493L, 0x3C9EBE0A15C9BEBCL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        0x431D67C49C100D4CL, 0x4CC5D4BECB3E42B6L, 0x597F299CFC657E2AL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        0x5FCB6FAB3AD6FAECL, 0x6C44198C4A475817L
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // buffer used by implCompress()
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    86
    private long[] W;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // state of this object
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    89
    private long[] state;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // initial state value. different between SHA-384 and SHA-512
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private final long[] initialHashes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Creates a new SHA object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    SHA5(String name, int digestLength, long[] initialHashes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        super(name, digestLength, 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.initialHashes = initialHashes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        state = new long[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        W = new long[80];
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   102
        resetHashes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    final void implReset() {
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   106
        resetHashes();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   107
        Arrays.fill(W, 0L);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   108
    }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   109
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   110
    private void resetHashes() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.arraycopy(initialHashes, 0, state, 0, state.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    final void implDigest(byte[] out, int ofs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        long bitsProcessed = bytesProcessed << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int index = (int)bytesProcessed & 0x7f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int padLen = (index < 112) ? (112 - index) : (240 - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        engineUpdate(padding, 0, padLen + 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        i2bBig4((int)(bitsProcessed >>> 32), buffer, 120);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        i2bBig4((int)bitsProcessed, buffer, 124);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        implCompress(buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   125
        int len = engineGetDigestLength();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   126
        if (len == 28) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   127
            // Special case for SHA-512/224
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   128
            l2bBig(state, 0, out, ofs, 24);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   129
            i2bBig4((int)(state[3] >> 32), out, ofs + 24);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   130
        } else {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   131
            l2bBig(state, 0, out, ofs, len);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   132
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * logical function ch(x,y,z) as defined in spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return (x and y) xor ((complement x) and z)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param y long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param z long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static long lf_ch(long x, long y, long z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return (x & y) ^ ((~x) & z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * logical function maj(x,y,z) as defined in spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return (x and y) xor (x and z) xor (y and z)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param y long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param z long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static long lf_maj(long x, long y, long z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return (x & y) ^ (x & z) ^ (y & z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * logical function R(x,s) - right shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return x right shift for s times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param s int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static long lf_R(long x, int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return (x >>> s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * logical function S(x,s) - right rotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return x circular right shift for s times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param s int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static long lf_S(long x, int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return (x >>> s) | (x << (64 - s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * logical function sigma0(x) - xor of results of right rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return S(x,28) xor S(x,34) xor S(x,39)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static long lf_sigma0(long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return lf_S(x, 28) ^ lf_S(x, 34) ^ lf_S(x, 39);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * logical function sigma1(x) - xor of results of right rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return S(x,14) xor S(x,18) xor S(x,41)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static long lf_sigma1(long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return lf_S(x, 14) ^ lf_S(x, 18) ^ lf_S(x, 41);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * logical function delta0(x) - xor of results of right shifts/rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private static long lf_delta0(long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return lf_S(x, 1) ^ lf_S(x, 8) ^ lf_R(x, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * logical function delta1(x) - xor of results of right shifts/rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param x long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private static long lf_delta1(long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return lf_S(x, 19) ^ lf_S(x, 61) ^ lf_R(x, 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Compute the hash for the current block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * This is in the same vein as Peter Gutmann's algorithm listed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * the back of Applied Cryptography, Compact implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * "old" NIST Secure Hash Algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    final void implCompress(byte[] buf, int ofs) {
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   221
        implCompressCheck(buf, ofs);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   222
        implCompress0(buf, ofs);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   223
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   224
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   225
    private void implCompressCheck(byte[] buf, int ofs) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   226
        Objects.requireNonNull(buf);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   227
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   228
        // The checks performed by the method 'b2iBig128'
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   229
        // are sufficient for the case when the method
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   230
        // 'implCompressImpl' is replaced with a compiler
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   231
        // intrinsic.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        b2lBig128(buf, ofs, W);
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   233
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   235
    // The method 'implCompressImpl' seems not to use its parameters.
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   236
    // The method can, however, be replaced with a compiler intrinsic
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   237
    // that operates directly on the array 'buf' (starting from
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   238
    // offset 'ofs') and not on array 'W', therefore 'buf' and 'ofs'
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   239
    // must be passed as parameter to the method.
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   240
    @HotSpotIntrinsicCandidate
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   241
    private final void implCompress0(byte[] buf, int ofs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // The first 16 longs are from the byte stream, compute the rest of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // the W[]'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (int t = 16; t < ITERATION; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            W[t] = lf_delta1(W[t-2]) + W[t-7] + lf_delta0(W[t-15])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                   + W[t-16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        long a = state[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        long b = state[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        long c = state[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        long d = state[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        long e = state[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        long f = state[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        long g = state[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        long h = state[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (int i = 0; i < ITERATION; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            long T1 = h + lf_sigma1(e) + lf_ch(e,f,g) + ROUND_CONSTS[i] + W[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            long T2 = lf_sigma0(a) + lf_maj(a,b,c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            h = g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            g = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            f = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            e = d + T1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            d = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            c = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            b = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            a = T1 + T2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        state[0] += a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        state[1] += b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        state[2] += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        state[3] += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        state[4] += e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        state[5] += f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        state[6] += g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        state[7] += h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   280
    public Object clone() throws CloneNotSupportedException {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   281
        SHA5 copy = (SHA5) super.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   282
        copy.state = copy.state.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   283
        copy.W = new long[80];
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   284
        return copy;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   285
    }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   286
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * SHA-512 implementation class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public static final class SHA512 extends SHA5 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        private static final long[] INITIAL_HASHES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            0x6a09e667f3bcc908L, 0xbb67ae8584caa73bL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            0x3c6ef372fe94f82bL, 0xa54ff53a5f1d36f1L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            0x510e527fade682d1L, 0x9b05688c2b3e6c1fL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            0x1f83d9abfb41bd6bL, 0x5be0cd19137e2179L
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        public SHA512() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            super("SHA-512", 64, INITIAL_HASHES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * SHA-384 implementation class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public static final class SHA384 extends SHA5 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        private static final long[] INITIAL_HASHES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            0xcbbb9d5dc1059ed8L, 0x629a292a367cd507L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            0x9159015a3070dd17L, 0x152fecd8f70e5939L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            0x67332667ffc00b31L, 0x8eb44a8768581511L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            0xdb0c2e0d64f98fa7L, 0x47b5481dbefa4fa4L
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public SHA384() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            super("SHA-384", 48, INITIAL_HASHES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   320
    public static final class SHA512_224 extends SHA5 {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   321
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   322
        private static final long[] INITIAL_HASHES = {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   323
                0x8C3D37C819544DA2L, 0x73E1996689DCD4D6L,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   324
                0x1DFAB7AE32FF9C82L, 0x679DD514582F9FCFL,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   325
                0x0F6D2B697BD44DA8L, 0x77E36F7304C48942L,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   326
                0x3F9D85A86A1D36C8L, 0x1112E6AD91D692A1L
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   327
        };
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   328
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   329
        public SHA512_224() {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   330
            super("SHA-512/224", 28, INITIAL_HASHES);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   331
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   332
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   333
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   334
    public static final class SHA512_256 extends SHA5 {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   335
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   336
        private static final long[] INITIAL_HASHES = {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   337
                0x22312194FC2BF72CL, 0x9F555FA3C84C64C2L,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   338
                0x2393B86B6F53B151L, 0x963877195940EABDL,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   339
                0x96283EE2A88EFFE3L, 0xBE5E1E2553863992L,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   340
                0x2B0199FC2C85B8AAL, 0x0EB72DDC81C52CA2L
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   341
        };
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   342
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   343
        public SHA512_256() {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   344
            super("SHA-512/256", 32, INITIAL_HASHES);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   345
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 31671
diff changeset
   346
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}