src/java.base/share/classes/sun/security/provider/MD2.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12685
diff changeset
     2
 * Copyright (c) 2003, 2012, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Implementation for the MD2 algorithm, see RFC1319. It is very slow and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * not particular secure. It is only supported to be able to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * RSA/Verisign root certificates signed using MD2withRSA. It should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * be used for anything else.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public final class MD2 extends DigestBase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // state, 48 ints
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    42
    private int[] X;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // checksum, 16 ints. they are really bytes, but byte arithmetic in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // the JVM is much slower that int arithmetic.
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    46
    private int[] C;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // temporary store for checksum C during final digest
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    49
    private byte[] cBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Create a new MD2 digest. Called by the JCA framework
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public MD2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super("MD2", 16, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        X = new int[48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        C = new int[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        cBytes = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    61
    public Object clone() throws CloneNotSupportedException {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    62
        MD2 copy = (MD2) super.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    63
        copy.X = copy.X.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    64
        copy.C = copy.C.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    65
        copy.cBytes = new byte[16];
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    66
        return copy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // reset state and checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Arrays.fill(X, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Arrays.fill(C, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // finish the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    void implDigest(byte[] out, int ofs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int padValue = 16 - ((int)bytesProcessed & 15);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        engineUpdate(PADDING[padValue], 0, padValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for (int i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            cBytes[i] = (byte)C[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        implCompress(cBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            out[ofs + i] = (byte)X[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // one iteration of the compression function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    void implCompress(byte[] b, int ofs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int k = b[ofs + i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            X[16 + i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            X[32 + i] = k ^ X[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // update the checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int t = C[15];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        for (int i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            t = (C[i] ^= S[X[16 + i] ^ t]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        t = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < 18; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            for (int j = 0; j < 48; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                t = (X[j] ^= S[t]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            t = (t + i) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // substitution table derived from Pi. Copied from the RFC.
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   112
    private static final int[] S = new int[] {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        31, 26, 219, 153, 141, 51, 159, 17, 131, 20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // digest padding. 17 element array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // padding[0] is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // padding[i] is an array of i time the byte value i (i = 1..16)
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   136
    private static final byte[][] PADDING;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        PADDING = new byte[17][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i = 1; i < 17; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            byte[] b = new byte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Arrays.fill(b, (byte)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            PADDING[i] = b;
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
}