Path: blob/master/src/java.base/share/classes/sun/security/provider/MD2.java
41159 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider;2627import java.util.Arrays;2829/**30* Implementation for the MD2 algorithm, see RFC1319. It is very slow and31* not particular secure. It is only supported to be able to verify32* RSA/Verisign root certificates signed using MD2withRSA. It should not33* be used for anything else.34*35* @since 1.536* @author Andreas Sterbenz37*/38public final class MD2 extends DigestBase {3940// state, 48 ints41private int[] X;4243// checksum, 16 ints. they are really bytes, but byte arithmetic in44// the JVM is much slower that int arithmetic.45private int[] C;4647// temporary store for checksum C during final digest48private byte[] cBytes;4950/**51* Create a new MD2 digest. Called by the JCA framework52*/53public MD2() {54super("MD2", 16, 16);55X = new int[48];56C = new int[16];57cBytes = new byte[16];58}5960public Object clone() throws CloneNotSupportedException {61MD2 copy = (MD2) super.clone();62copy.X = copy.X.clone();63copy.C = copy.C.clone();64copy.cBytes = new byte[16];65return copy;66}6768// reset state and checksum69void implReset() {70Arrays.fill(X, 0);71Arrays.fill(C, 0);72}7374// finish the digest75void implDigest(byte[] out, int ofs) {76int padValue = 16 - ((int)bytesProcessed & 15);77engineUpdate(PADDING[padValue], 0, padValue);78for (int i = 0; i < 16; i++) {79cBytes[i] = (byte)C[i];80}81implCompress(cBytes, 0);82for (int i = 0; i < 16; i++) {83out[ofs + i] = (byte)X[i];84}85}8687// one iteration of the compression function88void implCompress(byte[] b, int ofs) {89for (int i = 0; i < 16; i++) {90int k = b[ofs + i] & 0xff;91X[16 + i] = k;92X[32 + i] = k ^ X[i];93}9495// update the checksum96int t = C[15];97for (int i = 0; i < 16; i++) {98t = (C[i] ^= S[X[16 + i] ^ t]);99}100101t = 0;102for (int i = 0; i < 18; i++) {103for (int j = 0; j < 48; j++) {104t = (X[j] ^= S[t]);105}106t = (t + i) & 0xff;107}108}109110// substitution table derived from Pi. Copied from the RFC.111private static final int[] S = new int[] {11241, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,11319, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,11476, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,115138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,116245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,117148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,11839, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,119181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,120150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,121112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,12296, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,12385, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,124234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,125129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,1268, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,127203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,128166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,12931, 26, 219, 153, 141, 51, 159, 17, 131, 20,130};131132// digest padding. 17 element array.133// padding[0] is null134// padding[i] is an array of i time the byte value i (i = 1..16)135private static final byte[][] PADDING;136137static {138PADDING = new byte[17][];139for (int i = 1; i < 17; i++) {140byte[] b = new byte[i];141Arrays.fill(b, (byte)i);142PADDING[i] = b;143}144}145146}147148149