src/java.base/share/classes/sun/security/provider/SHA2.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
/*
16871
f8cfd245e458 8000897: VM crash in CompileBroker
valeriep
parents: 12685
diff changeset
     2
 * Copyright (c) 2002, 2013, 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;
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    30
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-256 developed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the National Institute of Standards and Technology along with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * National Security Agency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>It implements java.security.MessageDigestSpi, and can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * through Java Cryptography Architecture (JCA), as a pluggable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * MessageDigest implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since       1.4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author      Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author      Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    47
abstract class SHA2 extends DigestBase {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int ITERATION = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Constants for each round
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int[] ROUND_CONSTS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // buffer used by implCompress()
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    71
    private int[] W;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // state of this object
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    74
    private int[] state;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    75
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    76
    // initial state value. different between SHA-224 and SHA-256
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    77
    private final int[] initialHashes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates a new SHA object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    82
    SHA2(String name, int digestLength, int[] initialHashes) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    83
        super(name, digestLength, 64);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    84
        this.initialHashes = initialHashes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        state = new int[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        W = new int[64];
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    87
        resetHashes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Resets the buffers and hash value to start a new hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    void implReset() {
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    94
        resetHashes();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    95
        Arrays.fill(W, 0);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    96
    }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    97
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    98
    private void resetHashes() {
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    99
        System.arraycopy(initialHashes, 0, state, 0, state.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    void implDigest(byte[] out, int ofs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        long bitsProcessed = bytesProcessed << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int index = (int)bytesProcessed & 0x3f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int padLen = (index < 56) ? (56 - index) : (120 - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        engineUpdate(padding, 0, padLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        i2bBig4((int)bitsProcessed, buffer, 60);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        implCompress(buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
16871
f8cfd245e458 8000897: VM crash in CompileBroker
valeriep
parents: 12685
diff changeset
   113
        i2bBig(state, 0, out, ofs, engineGetDigestLength());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * logical function ch(x,y,z) as defined in spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return (x and y) xor ((complement x) and z)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param y int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param z int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static int lf_ch(int x, int y, int z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return (x & y) ^ ((~x) & z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * logical function maj(x,y,z) as defined in spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return (x and y) xor (x and z) xor (y and z)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param y int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param z int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static int lf_maj(int x, int y, int z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return (x & y) ^ (x & z) ^ (y & z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * logical function R(x,s) - right shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return x right shift for s times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param s int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static int lf_R( int x, int s ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return (x >>> s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * logical function S(x,s) - right rotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return x circular right shift for s times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param s int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static int lf_S(int x, int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return (x >>> s) | (x << (32 - s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * logical function sigma0(x) - xor of results of right rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return S(x,2) xor S(x,13) xor S(x,22)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static int lf_sigma0(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return lf_S(x, 2) ^ lf_S(x, 13) ^ lf_S(x, 22);
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 sigma1(x) - xor of results of right rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return S(x,6) xor S(x,11) xor S(x,25)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static int lf_sigma1(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return lf_S( x, 6 ) ^ lf_S( x, 11 ) ^ lf_S( x, 25 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * logical function delta0(x) - xor of results of right shifts/rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static int lf_delta0(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return lf_S(x, 7) ^ lf_S(x, 18) ^ lf_R(x, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * logical function delta1(x) - xor of results of right shifts/rotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param x int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static int lf_delta1(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return lf_S(x, 17) ^ lf_S(x, 19) ^ lf_R(x, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Process the current block to update the state variable state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    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
   198
        implCompressCheck(buf, ofs);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   199
        implCompress0(buf, ofs);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   200
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   201
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   202
    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
   203
        Objects.requireNonNull(buf);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   204
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   205
        // The checks performed by the method 'b2iBig64'
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   206
        // 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
   207
        // '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
   208
        // intrinsic.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        b2iBig64(buf, ofs, W);
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   210
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   212
    // 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
   213
    // 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
   214
    // 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
   215
    // 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
   216
    // 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
   217
    @HotSpotIntrinsicCandidate
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   218
    private void implCompress0(byte[] buf, int ofs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // The first 16 ints are from the byte stream, compute the rest of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // the W[]'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (int t = 16; t < ITERATION; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            W[t] = lf_delta1(W[t-2]) + W[t-7] + lf_delta0(W[t-15])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                   + W[t-16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int a = state[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int b = state[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int c = state[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int d = state[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int e = state[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int f = state[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int g = state[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int h = state[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        for (int i = 0; i < ITERATION; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            int T1 = h + lf_sigma1(e) + lf_ch(e,f,g) + ROUND_CONSTS[i] + W[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            int T2 = lf_sigma0(a) + lf_maj(a,b,c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            h = g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            g = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            f = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            e = d + T1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            d = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            c = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            b = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            a = T1 + T2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        state[0] += a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        state[1] += b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        state[2] += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        state[3] += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        state[4] += e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        state[5] += f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        state[6] += g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        state[7] += h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   257
    public Object clone() throws CloneNotSupportedException {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   258
        SHA2 copy = (SHA2) super.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   259
        copy.state = copy.state.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   260
        copy.W = new int[64];
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   261
        return copy;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   262
    }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   263
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   264
    /**
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   265
     * SHA-224 implementation class.
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   266
     */
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   267
    public static final class SHA224 extends SHA2 {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   268
        private static final int[] INITIAL_HASHES = {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   269
            0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   270
            0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   271
        };
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   272
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   273
        public SHA224() {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   274
            super("SHA-224", 28, INITIAL_HASHES);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   275
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   276
    }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   277
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   278
    /**
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   279
     * SHA-256 implementation class.
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   280
     */
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   281
    public static final class SHA256 extends SHA2 {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   282
        private static final int[] INITIAL_HASHES = {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   283
            0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   284
            0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   285
        };
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   286
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   287
        public SHA256() {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   288
            super("SHA-256", 32, INITIAL_HASHES);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   289
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   290
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
}